Skip to content

Commit

Permalink
Fixing variable decleration
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Tjaden committed Dec 23, 2024
1 parent ea66edc commit 11c0de6
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions examples/provider/CryptoBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.security.Security;
import java.util.Arrays;

import com.wolfssl.provider.jsse.WolfSSLProvider;
import com.wolfssl.provider.jce.WolfCryptProvider;

public class CryptoBenchmark {
/* Constants for benchmark configuration */
Expand All @@ -28,26 +28,53 @@ private static byte[] generateRandomData(int size) {
* Returns array containing encrypt and decrypt times in nanoseconds
*/
private static void benchmarkCipher() throws Exception {
/* Key generation variables */
KeyGenerator keyGen;
SecretKey key;

/* IV generation variables */
byte[] ivBytes;
IvParameterSpec iv;

/* Test data variables */
byte[] testData;
byte[] encryptedData;
byte[] encrypted;

/* Cipher variables */
Cipher cipher;

/* Timing variables */
long startTime;
long endTime;
long encryptTime;
long decryptTime;
double encryptThroughput;
double decryptThroughput;

/* Provider info */
Provider provider;

/* Generate a random key and IV */
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen = KeyGenerator.getInstance("AES");
keyGen.init(AES_KEY_SIZE);
SecretKey key = keyGen.generateKey();
key = keyGen.generateKey();

byte[] ivBytes = new byte[AES_BLOCK_SIZE];
ivBytes = new byte[AES_BLOCK_SIZE];
new SecureRandom().nextBytes(ivBytes);
IvParameterSpec iv = new IvParameterSpec(ivBytes);
iv = new IvParameterSpec(ivBytes);

/* Generate random test data */
byte[] testData = generateRandomData(DATA_SIZE);
testData = generateRandomData(DATA_SIZE);

/* Initialize cipher for warmup */
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

/* Warm up phase */
System.out.println("Warming up...");
for (int i = 0; i < WARMUP_ITERATIONS; i++) {
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] encrypted = cipher.doFinal(testData);
encrypted = cipher.doFinal(testData);

cipher.init(Cipher.DECRYPT_MODE, key, iv);
cipher.doFinal(encrypted);
Expand All @@ -58,25 +85,25 @@ private static void benchmarkCipher() throws Exception {
System.out.println("Iterations per test: " + TEST_ITERATIONS);

/* Benchmark encryption */
long startTime = System.nanoTime();
startTime = System.nanoTime();
for (int i = 0; i < TEST_ITERATIONS; i++) {
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
cipher.doFinal(testData);
}
long endTime = System.nanoTime();
long encryptTime = (endTime - startTime) / TEST_ITERATIONS;
endTime = System.nanoTime();
encryptTime = (endTime - startTime) / TEST_ITERATIONS;

/* Benchmark decryption */
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] encryptedData = cipher.doFinal(testData);
encryptedData = cipher.doFinal(testData);

startTime = System.nanoTime();
for (int i = 0; i < TEST_ITERATIONS; i++) {
cipher.init(Cipher.DECRYPT_MODE, key, iv);
cipher.doFinal(encryptedData);
}
endTime = System.nanoTime();
long decryptTime = (endTime - startTime) / TEST_ITERATIONS;
decryptTime = (endTime - startTime) / TEST_ITERATIONS;

/* Print results */
System.out.println("\nResults (average time per operation):");
Expand All @@ -86,15 +113,15 @@ private static void benchmarkCipher() throws Exception {
String.format("%.2f", decryptTime/1000000.0) + " ms)");

/* Calculate and print throughput */
double encryptThroughput = (DATA_SIZE / (encryptTime / 1000000000.0)) / (1024 * 1024);
double decryptThroughput = (DATA_SIZE / (decryptTime / 1000000000.0)) / (1024 * 1024);
encryptThroughput = (DATA_SIZE / (encryptTime / 1000000000.0)) / (1024 * 1024);
decryptThroughput = (DATA_SIZE / (decryptTime / 1000000000.0)) / (1024 * 1024);

System.out.println("\nThroughput:");
System.out.println(" Encryption: " + String.format("%.2f", encryptThroughput) + " MB/s");
System.out.println(" Decryption: " + String.format("%.2f", decryptThroughput) + " MB/s");

/* Print provider information */
Provider provider = Security.getProvider(cipher.getProvider().getName());
provider = Security.getProvider(cipher.getProvider().getName());
System.out.println("\nProvider Information:");
System.out.println(" Name: " + provider.getName());
System.out.println(" Version: " + provider.getVersionStr());
Expand All @@ -104,7 +131,7 @@ private static void benchmarkCipher() throws Exception {
public static void main(String[] args) {
try {
/* Register wolfJCE as the default provider */
Security.insertProviderAt(new WolfSSLProvider(), 1);
Security.insertProviderAt(new WolfCryptProvider(), 1);

/* Run Cipher benchmark */
benchmarkCipher();
Expand Down

0 comments on commit 11c0de6

Please sign in to comment.