8215140: Port missing crypto JMH micros from jmh-jdk-microbenchmarks

Reviewed-by: redestad
This commit is contained in:
Eric Caspole 2018-12-11 14:09:55 -05:00
parent a16d122b07
commit b29a8205f1
17 changed files with 1150 additions and 0 deletions

View File

@ -0,0 +1,85 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.full;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidParameterSpecException;
public class AESBench extends CryptoBase {
public static final int SET_SIZE = 128;
@Param({"AES/ECB/NoPadding", "AES/ECB/PKCS5Padding", "AES/CBC/NoPadding", "AES/CBC/PKCS5Padding"})
private String algorithm;
@Param({"128"})
private int keyLength;
@Param({"" + 16 * 1024})
private int dataSize;
byte[][] data;
byte[][] encryptedData;
private Cipher encryptCipher;
private Cipher decryptCipher;
int index = 0;
@Setup
public void setup() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidParameterSpecException {
setupProvider();
byte[] keystring = fillSecureRandom(new byte[keyLength / 8]);
SecretKeySpec ks = new SecretKeySpec(keystring, "AES");
encryptCipher = makeCipher(prov, algorithm);
encryptCipher.init(Cipher.ENCRYPT_MODE, ks);
decryptCipher = makeCipher(prov, algorithm);
decryptCipher.init(Cipher.DECRYPT_MODE, ks, encryptCipher.getParameters());
data = fillRandom(new byte[SET_SIZE][dataSize]);
encryptedData = fillEncrypted(data, encryptCipher);
}
@Benchmark
public byte[] encrypt() throws BadPaddingException, IllegalBlockSizeException {
byte[] d = data[index];
index = (index +1) % SET_SIZE;
return encryptCipher.doFinal(d);
}
@Benchmark
public byte[] decrypt() throws BadPaddingException, IllegalBlockSizeException {
byte[] e = encryptedData[index];
index = (index +1) % SET_SIZE;
return decryptCipher.doFinal(e);
}
}

View File

@ -0,0 +1,106 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.full;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidParameterSpecException;
public class AESGCMBench extends CryptoBase {
@Param({"AES/GCM/NoPadding","AES/GCM/PKCS5Padding"})
private String algorithm;
@Param({"128"})
private int keyLength;
@Param({""+16*1024})
private int dataSize;
byte[] data;
byte[] encryptedData;
private Cipher encryptCipher;
private Cipher decryptCipher;
SecretKeySpec ks;
GCMParameterSpec gcm_spec;
byte[] aad;
byte[] iv;
public static final int IV_BUFFER_SIZE = 32;
public static final int IV_MODULO = IV_BUFFER_SIZE - 16;
int iv_index = 0;
private int next_iv_index() {
int r = iv_index;
iv_index = (iv_index + 1) % IV_MODULO;
return r;
}
@Setup
public void setup() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidParameterSpecException {
setupProvider();
assert algorithm.split("/")[1].compareToIgnoreCase("GCM") == 0;
byte[] keystring = fillSecureRandom(new byte[keyLength / 8]);
ks = new SecretKeySpec(keystring, "AES");
iv = fillSecureRandom(new byte[IV_BUFFER_SIZE]);
gcm_spec = new GCMParameterSpec(96, iv, next_iv_index(), 16);
aad = fillSecureRandom(new byte[5]);
encryptCipher = makeCipher(prov, algorithm);
encryptCipher.init(Cipher.ENCRYPT_MODE, ks, gcm_spec);
encryptCipher.updateAAD(aad);
decryptCipher = makeCipher(prov, algorithm);
decryptCipher.init(Cipher.DECRYPT_MODE, ks, encryptCipher.getParameters().getParameterSpec(GCMParameterSpec.class));
decryptCipher.updateAAD(aad);
data = fillRandom(new byte[dataSize]);
encryptedData = encryptCipher.doFinal(data);
}
@Benchmark
public byte[] encrypt() throws BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException {
gcm_spec = new GCMParameterSpec(96, iv, next_iv_index(), 16);
encryptCipher.init(Cipher.ENCRYPT_MODE, ks, gcm_spec);
encryptCipher.updateAAD(aad);
return encryptCipher.doFinal(data);
}
@Benchmark
public byte[] decrypt() throws BadPaddingException, IllegalBlockSizeException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException {
decryptCipher.init(Cipher.DECRYPT_MODE, ks, encryptCipher.getParameters().getParameterSpec(GCMParameterSpec.class));
decryptCipher.updateAAD(aad);
return decryptCipher.doFinal(encryptedData);
}
}

View File

@ -0,0 +1,102 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.full;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Fork(5)
@Warmup(iterations = 3, time = 3)
@Measurement(iterations = 8, time = 2)
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Thread)
@BenchmarkMode(Mode.Throughput)
public class CryptoBase {
@Param({""})
private String provider;
public Provider prov = null;
@Setup
public void setupProvider() {
if (provider != null && !provider.isEmpty()) {
prov = Security.getProvider(provider);
if (prov == null) {
throw new RuntimeException("Can't find prodiver \"" + provider + "\"");
}
}
}
public static Cipher makeCipher(Provider prov, String algorithm) throws NoSuchPaddingException, NoSuchAlgorithmException {
return (prov == null) ? Cipher.getInstance(algorithm) : Cipher.getInstance(algorithm, prov);
}
public static byte[][] fillRandom(byte[][] data) {
Random rnd = new Random();
for (byte[] d : data) {
rnd.nextBytes(d);
}
return data;
}
public static byte[] fillRandom(byte[] data) {
Random rnd = new Random();
rnd.nextBytes(data);
return data;
}
public static byte[] fillSecureRandom(byte[] data) {
SecureRandom rnd = new SecureRandom();
rnd.nextBytes(data);
return data;
}
public static byte[][] fillEncrypted(byte[][] data, Cipher encryptCipher) throws BadPaddingException, IllegalBlockSizeException {
byte[][] encryptedData = new byte[data.length][];
for (int i = 0; i < encryptedData.length; i++) {
encryptedData[i] = encryptCipher.doFinal(data[i]);
}
return encryptedData;
}
}

View File

@ -0,0 +1,86 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.full;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
public class DESedeBench extends CryptoBase {
public static final int SET_SIZE = 128;
@Param({"DESede/ECB/NoPadding", "DESede/ECB/PKCS5Padding", "DESede/CBC/NoPadding","DESede/CBC/PKCS5Padding"})
private String algorithm;
@Param({"168"})
private int keyLength;
@Param({""+16*1024})
private int dataSize;
byte[][] data;
byte[][] encryptedData;
private Cipher encryptCipher;
private Cipher decryptCipher;
int index = 0;
@Setup
public void setup() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
setupProvider();
KeyGenerator kg = KeyGenerator.getInstance("DESede");
kg.init(keyLength);
Key key = kg.generateKey();
encryptCipher = makeCipher(prov, algorithm);
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = makeCipher(prov, algorithm);
decryptCipher.init(Cipher.DECRYPT_MODE, key, encryptCipher.getParameters());
data = fillRandom(new byte[SET_SIZE][dataSize]);
encryptedData = fillEncrypted(data, encryptCipher);
}
@Benchmark
public byte[] encrypt() throws BadPaddingException, IllegalBlockSizeException {
byte[] d = data[index];
index = (index +1) % SET_SIZE;
return encryptCipher.doFinal(d);
}
@Benchmark
public byte[] decrypt() throws BadPaddingException, IllegalBlockSizeException {
byte[] e = encryptedData[index];
index = (index +1) % SET_SIZE;
return decryptCipher.doFinal(e);
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.full;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
public class KeyPairGeneratorBench extends CryptoBase {
private KeyPairGenerator generator;
@Param({"DSA", "DiffieHellman"})
private String algorithm;
@Param({"1024", "2048"})
private int keyLength;
@Setup
public void setup() throws NoSuchAlgorithmException {
setupProvider();
generator = (prov == null) ? KeyPairGenerator.getInstance(algorithm) : KeyPairGenerator.getInstance(algorithm, prov);
generator.initialize(keyLength);
}
@Benchmark
public KeyPair generateKeyPair() {
return generator.generateKeyPair();
}
public static class RSA extends KeyPairGeneratorBench {
@Param({"RSA"})
private String algorithm;
@Param({"1024", "2048", "3072"})
private int keyLength;
}
public static class EC extends KeyPairGeneratorBench {
@Param({"EC"})
private String algorithm;
@Param({"160", "224", "256"})
private int keyLength;
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.full;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class MacBench extends CryptoBase {
public static final int SET_SIZE = 128;
@Param({"HmacMD5", "HmacSHA1", "HmacSHA256", "HmacSHA512"})
private String algorithm;
@Param({"128", "1024"})
int dataSize;
private byte[][] data;
private Mac mac;
int index = 0;
@Setup
public void setup() throws NoSuchAlgorithmException, InvalidKeyException {
setupProvider();
mac = (prov == null) ? Mac.getInstance(algorithm) : Mac.getInstance(algorithm, prov);
mac.init(KeyGenerator.getInstance(algorithm).generateKey());
data = fillRandom(new byte[SET_SIZE][dataSize]);
}
@Benchmark
public byte[] mac() throws NoSuchAlgorithmException {
byte[] d = data[index];
index = (index + 1) % SET_SIZE;
return mac.doFinal(d);
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.full;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MessageDigestBench extends CryptoBase {
public static final int SET_SIZE = 128;
@Param({"MD5", "SHA", "SHA-256", "SHA-384", "SHA-512"})
private String algorithm;
/*
* Note: dataSize value shouldn't be small unless you want to evaluate MessageDigest.getInstance performance.
* Small value causes large impact of MessageDigest.getInstance including lock contention in multi-threaded
* execution.
*/
@Param({""+1024*1024})
int dataSize;
private byte[][] data;
int index = 0;
@Setup
public void setup() {
setupProvider();
data = fillRandom(new byte[SET_SIZE][dataSize]);
}
@Benchmark
public byte[] digest() throws NoSuchAlgorithmException {
MessageDigest md = (prov == null) ? MessageDigest.getInstance(algorithm) : MessageDigest.getInstance(algorithm, prov);
byte[] d = data[index];
index = (index +1) % SET_SIZE;
return md.digest(d);
}
}

View File

@ -0,0 +1,104 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.full;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
public class RSABench extends CryptoBase {
public static final int SET_SIZE = 128;
@Param({"RSA/ECB/NoPadding", "RSA/ECB/PKCS1Padding", "RSA/ECB/OAEPPadding"})
protected String algorithm;
@Param({"32"})
protected int dataSize;
@Param({"1024", "2048", "3072"})
protected int keyLength;
private byte[][] data;
private byte[][] encryptedData;
private Cipher encryptCipher;
private Cipher decryptCipher;
private int index = 0;
@Setup()
public void setup() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
setupProvider();
data = fillRandom(new byte[SET_SIZE][dataSize]);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(keyLength);
KeyPair keyPair = kpg.generateKeyPair();
encryptCipher = makeCipher(prov, algorithm);
encryptCipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
decryptCipher = makeCipher(prov, algorithm);
decryptCipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
encryptedData = fillEncrypted(data, encryptCipher);
}
@Benchmark
public byte[] encrypt() throws BadPaddingException, IllegalBlockSizeException {
byte[] d = data[index];
index = (index +1) % SET_SIZE;
return encryptCipher.doFinal(d);
}
@Benchmark
public byte[] decrypt() throws BadPaddingException, IllegalBlockSizeException {
byte[] e = encryptedData[index];
index = (index +1) % SET_SIZE;
return decryptCipher.doFinal(e);
}
public static class Extra extends RSABench {
@Param({"RSA/ECB/NoPadding", "RSA/ECB/PKCS1Padding"})
private String algorithm;
@Param({"214"})
private int dataSize;
@Param({"2048", "3072"})
private int keyLength;
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.full;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.SecureRandom;
public class SecureRandomBench extends CryptoBase {
@Param({"NativePRNG", "SHA1PRNG"})
private String algorithm;
@Param({"64"})
int dataSize;
@Param({"true", "false"})
// if shared - use the single SecureRandom instance for all threads
// otherwise - each thread uses its own SecureRandom instance
boolean shared;
private byte[] bytes;
private SecureRandom rnd;
private static SecureRandom sharedRnd;
private static synchronized SecureRandom getSharedInstance(String algorithm, Provider prov) throws NoSuchAlgorithmException {
if (sharedRnd == null) {
sharedRnd = (prov == null) ? SecureRandom.getInstance(algorithm) : SecureRandom.getInstance(algorithm, prov);
}
return sharedRnd;
}
@Setup
public void setup() throws NoSuchAlgorithmException {
setupProvider();
bytes = new byte[dataSize];
if (shared) {
rnd = getSharedInstance(algorithm, prov);
} else {
rnd = (prov == null) ? SecureRandom.getInstance(algorithm) : SecureRandom.getInstance(algorithm, prov);
}
}
@Benchmark
public byte[] nextBytes() {
rnd.nextBytes(bytes);
return bytes;
}
}

View File

@ -0,0 +1,125 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.full;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.util.Random;
public class SignatureBench extends CryptoBase {
public static final int SET_SIZE = 128;
@Param({"SHA256withDSA"})
private String algorithm;
@Param({"1024", "16384"})
int dataSize;
@Param({"1024"})
private int keyLength;
private PrivateKey privateKey;
private PublicKey publicKey;
private byte[][] data;
private byte[][] signedData;
int index;
private String getKeyPairGeneratorName() {
String tail = algorithm.substring(algorithm.lastIndexOf("with") + 4);
return "ECDSA".equals(tail) ? "EC" : tail;
}
@Setup()
public void setup() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
setupProvider();
KeyPairGenerator kpg = KeyPairGenerator.getInstance(getKeyPairGeneratorName());
kpg.initialize(keyLength);
KeyPair keys = kpg.generateKeyPair();
this.privateKey = keys.getPrivate();
this.publicKey = keys.getPublic();
data = fillRandom(new byte[SET_SIZE][dataSize]);
signedData = new byte[data.length][];
for (int i = 0; i < data.length; i++) {
signedData[i] = sign(data[i]);
}
}
public byte[] sign(byte[] data) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
Signature signature = (prov == null) ? Signature.getInstance(algorithm) : Signature.getInstance(algorithm, prov);
signature.initSign(privateKey);
signature.update(data);
return signature.sign();
}
@Benchmark
public byte[] sign() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
byte[] d = data[index];
index = (index + 1) % SET_SIZE;
return sign(d);
}
@Benchmark
public boolean verify() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
Signature signature = (prov == null) ? Signature.getInstance(algorithm) : Signature.getInstance(algorithm, prov);
signature.initVerify(publicKey);
byte[] d = data[index];
byte[] s = signedData[index];
index = (index + 1) % SET_SIZE;
signature.update(d);
return signature.verify(s);
}
public static class RSA extends SignatureBench {
@Param({"MD5withRSA", "SHA256withRSA"})
private String algorithm;
@Param({"1024", "2048", "3072"})
private int keyLength;
}
public static class ECDSA extends SignatureBench {
@Param({"SHA256withECDSA"})
private String algorithm;
@Param({"160", "224", "256"})
private int keyLength;
}
}

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.small;
import org.openjdk.jmh.annotations.Param;
public class AESBench extends org.openjdk.bench.javax.crypto.full.AESBench {
@Param({"AES/ECB/NoPadding", "AES/CBC/NoPadding" })
private String algorithm;
@Param({"128"})
private int keyLength;
@Param({"1024"})
private int dataSize;
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.small;
import org.openjdk.jmh.annotations.Param;
public class AESGCMBench extends org.openjdk.bench.javax.crypto.full.AESGCMBench {
@Param({"AES/GCM/NoPadding"})
private String algorithm;
@Param({"128"})
private int keyLength;
@Param({"1024"})
private int dataSize;
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.small;
import org.openjdk.jmh.annotations.Param;
public class KeyPairGeneratorBench extends org.openjdk.bench.javax.crypto.full.KeyPairGeneratorBench {
@Param({"DSA", "RSA"})
private String algorithm;
@Param({"2048"})
private int keyLength;
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.small;
import org.openjdk.jmh.annotations.Param;
public class MessageDigestBench extends org.openjdk.bench.javax.crypto.full.MessageDigestBench {
@Param({"SHA1", "SHA-256"})
private String algorithm;
}

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.small;
import org.openjdk.jmh.annotations.Param;
public class RSABench extends org.openjdk.bench.javax.crypto.full.RSABench {
@Param({"RSA/ECB/NoPadding"})
protected String algorithm;
@Param({"32"})
protected int dataSize;
@Param({"2048"})
protected int keyLength;
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.small;
public class SecureRandomBench extends org.openjdk.bench.javax.crypto.full.SecureRandomBench {
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.javax.crypto.small;
import org.openjdk.jmh.annotations.Param;
public abstract class SignatureBench extends org.openjdk.bench.javax.crypto.full.SignatureBench {
public static class RSA extends SignatureBench {
@Param({"SHA256withRSA"})
private String algorithm;
@Param({"2048"})
private int keyLength;
@Param({"1024"})
int dataSize;
}
public static class DSA extends SignatureBench {
@Param({"SHA256withDSA"})
private String algorithm;
@Param({"1024"})
int dataSize;
@Param({"1024"})
private int keyLength;
}
}