diff --git a/benchmarks/README.md b/benchmarks/README.md index 786e0d0d..e0f8f2ed 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -25,3 +25,12 @@ Use `-PaccpLocalJar="PATH_TO_LOCAL_JAR"`: ```bash ./gradlew -PaccpLocalJar="../../build/cmake/AmazonCorrettoCryptoProvider.jar" lib:jmh ``` + +### Benchmarking ACCP that is bundled in JDK + +Some customers bundle ACCP directly with there JDKs. To run the benchmarks with such a setup, +one can use the following command: + +```bash + ./gradlew -PuseBundledAccp -Dorg.gradle.java.home= lib:jmh +``` \ No newline at end of file diff --git a/benchmarks/lib/build.gradle.kts b/benchmarks/lib/build.gradle.kts index 0f2b2970..765328bb 100644 --- a/benchmarks/lib/build.gradle.kts +++ b/benchmarks/lib/build.gradle.kts @@ -32,21 +32,16 @@ dependencies { "2.+" - if (project.hasProperty("accpLocalJar")) { - jmh(files(accpLocalJar)) - } else { - jmh("software.amazon.cryptools:${accpArtifactId}:$publishedAccpVersion:${osdetector.classifier}") + if (!project.hasProperty("useBundledAccp")) { + if (project.hasProperty("accpLocalJar")) { + jmh(files(accpLocalJar)) + } else { + jmh("software.amazon.cryptools:${accpArtifactId}:$publishedAccpVersion:${osdetector.classifier}") + } } } -// Apply a specific Java toolchain to ease working on different environments. -java { - toolchain { - languageVersion.set(JavaLanguageVersion.of(11)) - } -} - jmh { if (project.hasProperty("includeBenchmark")) { includes.add(includeBenchmark) @@ -75,4 +70,4 @@ jmhReport { tasks.jmh { finalizedBy(tasks.jmhReport) -} \ No newline at end of file +} diff --git a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/AesGcmBase.java b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/AesGcmBase.java index 5ec8ba4e..4b93055f 100644 --- a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/AesGcmBase.java +++ b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/AesGcmBase.java @@ -19,7 +19,7 @@ public class AesGcmBase { protected byte[] ciphertext; protected void setup(int keyBits, String provider) throws Exception { - BenchmarkUtils.setupProvider(provider); + BenchmarkUtils.setupProviders(); key = new SecretKeySpec(BenchmarkUtils.getRandBytes(keyBits / 8), "AES"); params1 = new GCMParameterSpec(128, BenchmarkUtils.getRandBytes(12)); params2 = new GCMParameterSpec(128, BenchmarkUtils.getRandBytes(12)); diff --git a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/AesKwp.java b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/AesKwp.java index aeaf5fa6..a6dc7569 100644 --- a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/AesKwp.java +++ b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/AesKwp.java @@ -41,7 +41,7 @@ public class AesKwp { @Setup public void setup() throws Exception { - BenchmarkUtils.setupProvider(provider); + BenchmarkUtils.setupProviders(); kek = new SecretKeySpec(BenchmarkUtils.getRandBytes(kekBits / 8), "AES"); // Bouncy Castle and SunJCE have wierd names for this... String algorithm = "AES/KWP/NoPadding"; diff --git a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/BenchmarkUtils.java b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/BenchmarkUtils.java index 04c1896b..0835efb2 100644 --- a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/BenchmarkUtils.java +++ b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/BenchmarkUtils.java @@ -2,75 +2,38 @@ // SPDX-License-Identifier: Apache-2.0 package com.amazon.corretto.crypto.provider.benchmarks; +import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider; +import org.bouncycastle.jce.provider.BouncyCastleProvider; + import java.security.Provider; import java.security.SecureRandom; import java.security.Security; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider; -import org.bouncycastle.jce.provider.BouncyCastleProvider; class BenchmarkUtils { - private BenchmarkUtils() {} + private BenchmarkUtils() { + } - private static final SecureRandom sr = new SecureRandom(); - private static final Set NON_DEFAULT_PROVIDERS = - new HashSet(Arrays.asList("BC", "BCFIPS", "AmazonCorrettoCryptoProvider")); - private static final Provider[] DEFAULT_PROVIDERS; + private static final SecureRandom sr = new SecureRandom(); - static { - DEFAULT_PROVIDERS = Security.getProviders(); - for (Provider provider : DEFAULT_PROVIDERS) { - if (NON_DEFAULT_PROVIDERS.contains(provider.getName())) { - throw new RuntimeException("Provider prematurely (statically) registered: " + provider); - } - } - } + private static final Provider bc = new BouncyCastleProvider(); - static byte[] getRandBytes(int n) { - byte[] ret = new byte[n]; - final int bcMaxSize = 32768; - for (int ii = 0; ii < n; ii += bcMaxSize) { - byte[] data = new byte[bcMaxSize]; - sr.nextBytes(data); - System.arraycopy(data, 0, ret, ii, Math.min(bcMaxSize, n - ii)); + static byte[] getRandBytes(int n) { + final byte[] ret = new byte[n]; + final int bcMaxSize = 32768; + final byte[] data = new byte[bcMaxSize]; + for (int ii = 0; ii < n; ii += bcMaxSize) { + sr.nextBytes(data); + System.arraycopy(data, 0, ret, ii, Math.min(bcMaxSize, n - ii)); + } + return ret; } - return ret; - } - static void setupProvider(String providerName) { - removeAllProviders(); - switch (providerName) { - case "AmazonCorrettoCryptoProvider": - installDefaultProviders(); + // In all the benchmarks, the getInstance methods of crypto services are invoked by passing + // the name of the algorithm and the provider that we are interested in. As a result, we + // just need to ensure that the provider has been installed. + static void setupProviders() { AmazonCorrettoCryptoProvider.install(); AmazonCorrettoCryptoProvider.INSTANCE.assertHealthy(); - break; - case "BC": - Security.insertProviderAt(new BouncyCastleProvider(), 1); - break; - case "SUN": - case "SunEC": - case "SunJCE": - case "SunRsaSign": - installDefaultProviders(); - break; - default: - throw new RuntimeException("Unrecognized provider: " + providerName); - } - } - - static void installDefaultProviders() { - for (Provider provider : DEFAULT_PROVIDERS) { - Security.addProvider(provider); - } - } - - static void removeAllProviders() { - for (Provider provider : Security.getProviders()) { - Security.removeProvider(provider.getName()); + Security.addProvider(bc); } - } } diff --git a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Drbg.java b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Drbg.java index 15295684..129e338c 100644 --- a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Drbg.java +++ b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Drbg.java @@ -25,7 +25,7 @@ public class Drbg { @Setup public void setup() throws Exception { - BenchmarkUtils.setupProvider(provider); + BenchmarkUtils.setupProviders(); data = new byte[size]; final String algorithm; switch (provider) { diff --git a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Hashes.java b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Hashes.java index 3cf988c6..d8c9d5d1 100644 --- a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Hashes.java +++ b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Hashes.java @@ -26,7 +26,7 @@ public class Hashes { @Setup public void setup() throws Exception { - BenchmarkUtils.setupProvider(provider); + BenchmarkUtils.setupProviders(); data_8B = BenchmarkUtils.getRandBytes(8); data_1KiB = BenchmarkUtils.getRandBytes(1024); data_64KiB = BenchmarkUtils.getRandBytes(64 * 1024); diff --git a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Hmac.java b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Hmac.java index f17e16a7..25798772 100644 --- a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Hmac.java +++ b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/Hmac.java @@ -27,7 +27,7 @@ public class Hmac { @Setup public void setup() throws Exception { - BenchmarkUtils.setupProvider(provider); + BenchmarkUtils.setupProviders(); final String algorithm = "Hmac" + hash; data_8B = BenchmarkUtils.getRandBytes(8); data_1KiB = BenchmarkUtils.getRandBytes(1024); diff --git a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyAgreementEcdh.java b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyAgreementEcdh.java index 5d4ffae3..90fd6563 100644 --- a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyAgreementEcdh.java +++ b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyAgreementEcdh.java @@ -29,7 +29,7 @@ public class KeyAgreementEcdh { @Setup public void setup() throws Exception { - BenchmarkUtils.setupProvider(provider); + BenchmarkUtils.setupProviders(); final KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", provider); kpg.initialize(new ECGenParameterSpec(curve)); alice = kpg.generateKeyPair(); diff --git a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyGenEc.java b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyGenEc.java index 52f290c3..2e8fec6f 100644 --- a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyGenEc.java +++ b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyGenEc.java @@ -25,7 +25,7 @@ public class KeyGenEc { @Setup public void setup() throws Exception { - BenchmarkUtils.setupProvider(provider); + BenchmarkUtils.setupProviders(); kpg = KeyPairGenerator.getInstance("EC", provider); kpg.initialize(new ECGenParameterSpec(curve)); } diff --git a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyGenRsa.java b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyGenRsa.java index fe2435b1..220ba70c 100644 --- a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyGenRsa.java +++ b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/KeyGenRsa.java @@ -25,7 +25,7 @@ public class KeyGenRsa { @Setup public void setup() throws Exception { - BenchmarkUtils.setupProvider(provider); + BenchmarkUtils.setupProviders(); kpg = KeyPairGenerator.getInstance("RSA", provider); kpg.initialize(new RSAKeyGenParameterSpec(bits, RSAKeyGenParameterSpec.F4)); } diff --git a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/RsaCipherOneShot.java b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/RsaCipherOneShot.java index 350fc1d7..9b514bb9 100644 --- a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/RsaCipherOneShot.java +++ b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/RsaCipherOneShot.java @@ -33,7 +33,7 @@ public class RsaCipherOneShot { @Setup public void setup() throws Exception { - BenchmarkUtils.setupProvider(provider); + BenchmarkUtils.setupProviders(); String rsaProvider = provider; if (provider.equals("SunJCE")) { rsaProvider = "SunRsaSign"; diff --git a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/SignatureBase.java b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/SignatureBase.java index b60def02..40300312 100644 --- a/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/SignatureBase.java +++ b/benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/SignatureBase.java @@ -22,7 +22,7 @@ protected void setup( String sigAlg, AlgorithmParameterSpec sigParams) throws Exception { - BenchmarkUtils.setupProvider(provider); + BenchmarkUtils.setupProviders(); final KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlg, provider); kpg.initialize(keyParams); keyPair = kpg.generateKeyPair();