1 /* 2 * Copyright (c) 2015, 2018, 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.SecretKeySpec; 34 import java.security.InvalidAlgorithmParameterException; 35 import java.security.InvalidKeyException; 36 import java.security.NoSuchAlgorithmException; 37 import java.security.spec.InvalidParameterSpecException; 38 39 public class AESBench extends CryptoBase { 40 41 public static final int SET_SIZE = 128; 42 43 @Param({"AES/ECB/NoPadding", "AES/ECB/PKCS5Padding", "AES/CBC/NoPadding", "AES/CBC/PKCS5Padding"}) 44 private String algorithm; 45 46 @Param({"128", "192", "256"}) 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 int index = 0; 57 58 @Setup setup()59 public void setup() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidParameterSpecException { 60 setupProvider(); 61 byte[] keystring = fillSecureRandom(new byte[keyLength / 8]); 62 SecretKeySpec ks = new SecretKeySpec(keystring, "AES"); 63 encryptCipher = makeCipher(prov, algorithm); 64 encryptCipher.init(Cipher.ENCRYPT_MODE, ks); 65 decryptCipher = makeCipher(prov, algorithm); 66 decryptCipher.init(Cipher.DECRYPT_MODE, ks, encryptCipher.getParameters()); 67 data = fillRandom(new byte[SET_SIZE][dataSize]); 68 encryptedData = fillEncrypted(data, encryptCipher); 69 } 70 71 @Benchmark encrypt()72 public byte[] encrypt() throws BadPaddingException, IllegalBlockSizeException { 73 byte[] d = data[index]; 74 index = (index +1) % SET_SIZE; 75 return encryptCipher.doFinal(d); 76 } 77 78 @Benchmark decrypt()79 public byte[] decrypt() throws BadPaddingException, IllegalBlockSizeException { 80 byte[] e = encryptedData[index]; 81 index = (index +1) % SET_SIZE; 82 return decryptCipher.doFinal(e); 83 } 84 85 } 86