1 /* 2 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.bench.javax.crypto.full; 24 25 import org.openjdk.jmh.annotations.Benchmark; 26 import org.openjdk.jmh.annotations.Param; 27 import org.openjdk.jmh.annotations.Setup; 28 29 import javax.crypto.BadPaddingException; 30 import javax.crypto.Cipher; 31 import javax.crypto.IllegalBlockSizeException; 32 import javax.crypto.NoSuchPaddingException; 33 import javax.crypto.spec.GCMParameterSpec; 34 import javax.crypto.spec.SecretKeySpec; 35 import java.security.InvalidAlgorithmParameterException; 36 import java.security.InvalidKeyException; 37 import java.security.NoSuchAlgorithmException; 38 import java.security.spec.InvalidParameterSpecException; 39 40 41 public class AESGCMBench extends CryptoBase { 42 43 @Param({"AES/GCM/NoPadding"}) 44 private String algorithm; 45 46 @Param({"128"}) 47 private int keyLength; 48 49 @Param({""+16*1024}) 50 private int dataSize; 51 52 byte[] data; 53 byte[] encryptedData; 54 private Cipher encryptCipher; 55 private Cipher decryptCipher; 56 SecretKeySpec ks; 57 GCMParameterSpec gcm_spec; 58 byte[] aad; 59 byte[] iv; 60 61 public static final int IV_BUFFER_SIZE = 32; 62 public static final int IV_MODULO = IV_BUFFER_SIZE - 16; 63 int iv_index = 0; 64 next_iv_index()65 private int next_iv_index() { 66 int r = iv_index; 67 iv_index = (iv_index + 1) % IV_MODULO; 68 return r; 69 } 70 71 @Setup setup()72 public void setup() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidParameterSpecException { 73 setupProvider(); 74 assert algorithm.split("/")[1].compareToIgnoreCase("GCM") == 0; 75 76 byte[] keystring = fillSecureRandom(new byte[keyLength / 8]); 77 ks = new SecretKeySpec(keystring, "AES"); 78 iv = fillSecureRandom(new byte[IV_BUFFER_SIZE]); 79 gcm_spec = new GCMParameterSpec(96, iv, next_iv_index(), 16); 80 aad = fillSecureRandom(new byte[5]); 81 encryptCipher = makeCipher(prov, algorithm); 82 encryptCipher.init(Cipher.ENCRYPT_MODE, ks, gcm_spec); 83 encryptCipher.updateAAD(aad); 84 decryptCipher = makeCipher(prov, algorithm); 85 decryptCipher.init(Cipher.DECRYPT_MODE, ks, encryptCipher.getParameters().getParameterSpec(GCMParameterSpec.class)); 86 decryptCipher.updateAAD(aad); 87 data = fillRandom(new byte[dataSize]); 88 encryptedData = encryptCipher.doFinal(data); 89 } 90 91 @Benchmark encrypt()92 public byte[] encrypt() throws BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException { 93 gcm_spec = new GCMParameterSpec(96, iv, next_iv_index(), 16); 94 encryptCipher.init(Cipher.ENCRYPT_MODE, ks, gcm_spec); 95 encryptCipher.updateAAD(aad); 96 return encryptCipher.doFinal(data); 97 } 98 99 @Benchmark decrypt()100 public byte[] decrypt() throws BadPaddingException, IllegalBlockSizeException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException { 101 decryptCipher.init(Cipher.DECRYPT_MODE, ks, encryptCipher.getParameters().getParameterSpec(GCMParameterSpec.class)); 102 decryptCipher.updateAAD(aad); 103 return decryptCipher.doFinal(encryptedData); 104 } 105 106 } 107