Skip to content

Commit

Permalink
Benchmarking ACCP when its bundled with JDK
Browse files Browse the repository at this point in the history
  • Loading branch information
amirhosv committed Feb 12, 2024
1 parent d8f3400 commit 8473323
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 80 deletions.
9 changes: 9 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 their JDKs. To run the benchmarks with such a setup,
one can use the following command:

```bash
./gradlew -PuseBundledAccp -Dorg.gradle.java.home=<PATH_TO_YOUR_CUSTOM_JDK> lib:jmh
```
19 changes: 7 additions & 12 deletions benchmarks/lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -75,4 +70,4 @@ jmhReport {

tasks.jmh {
finalizedBy(tasks.jmhReport)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 8473323

Please sign in to comment.