-
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
364 additions
and
197 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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
package com.amazon.corretto.crypto.provider.benchmarks; | ||
|
||
import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
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.infra.Blackhole; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.GCMParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.util.Random; | ||
|
||
@State(Scope.Thread) | ||
public class CipherReuse { | ||
private final static String AES = "AES"; | ||
private final static String AES_GCM = "AES/GCM/NoPadding"; | ||
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 { | ||
iv[0]++; | ||
final SecretKey sk; | ||
if (newKey) { | ||
keyb[0]++; | ||
sk = new SecretKeySpec(keyb, AES); | ||
} else { | ||
sk = key; | ||
} | ||
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, AmazonCorrettoCryptoProvider.INSTANCE); | ||
} catch (final Exception 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.