-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
369 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
benchmarks/lib/src/jmh/java/com/amazon/corretto/crypto/provider/benchmarks/CipherReuse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.amazon.corretto.crypto.provider.benchmarks; | ||
|
||
import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider; | ||
import lombok.SneakyThrows; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
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 org.openjdk.jmh.infra.Blackhole; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.NoSuchPaddingException; | ||
import javax.crypto.spec.GCMParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Random; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@State(Scope.Thread) | ||
public class CipherReuse { | ||
Cipher shared; | ||
SecretKeySpec key; | ||
Random random = new Random(); | ||
byte[] iv = new byte[12]; | ||
byte[] input = new byte[1000]; | ||
byte[] keyb = new byte[32]; | ||
byte[] aad = new byte[100]; | ||
|
||
@Param({"true", "false"}) | ||
private boolean newKey; | ||
|
||
@Setup | ||
public void setup() throws Exception { | ||
shared = getAesGcmCipherFromAccp(); | ||
key = new SecretKeySpec(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8}, "AES"); | ||
random.nextBytes(input); | ||
random.nextBytes(aad); | ||
} | ||
|
||
@Benchmark | ||
public void newInstance(Blackhole blackhole) throws Exception { | ||
blackhole.consume(encrypt(getAesGcmCipherFromAccp())); | ||
} | ||
|
||
@Benchmark | ||
public void reuse(Blackhole blackhole) throws Exception { | ||
blackhole.consume(encrypt(shared)); | ||
} | ||
|
||
byte[] encrypt(Cipher cipher) throws Exception { | ||
random.nextBytes(iv); | ||
keyb[0]++; | ||
final String name = random.nextBoolean() ? "AES" : "aes"; | ||
final var sk = newKey ? new SecretKeySpec(keyb, name) : new SecretKeySpec(key.getEncoded(), name); | ||
cipher.init(Cipher.ENCRYPT_MODE, sk, new GCMParameterSpec(128, iv)); | ||
cipher.updateAAD(aad); | ||
final var cipherText = cipher.doFinal(input); | ||
cipher.init(Cipher.DECRYPT_MODE, sk, new GCMParameterSpec(128, iv)); | ||
cipher.updateAAD(aad); | ||
return cipher.doFinal(cipherText); | ||
} | ||
|
||
private Cipher getAesGcmCipherFromAccp() { | ||
try { | ||
return Cipher.getInstance("AES/GCM/NoPadding", AmazonCorrettoCryptoProvider.INSTANCE); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} catch (NoSuchPaddingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.