Skip to content

Commit

Permalink
Add encryption and mac benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffery.wsj committed Aug 28, 2023
1 parent 93080b2 commit 955b4ca
Show file tree
Hide file tree
Showing 7 changed files with 495 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/jce/benchmark/encrypt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

echo "SM4 and AES Encrypt Benchmark:"

mvn clean package && java -cp target/benchmark-1.0-SNAPSHOT-jar-with-dependencies.jar com.alibaba.dragonwell.security.jce.benchmark.SMCryptoBenchmark
102 changes: 102 additions & 0 deletions examples/jce/benchmark/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba.dragonwell.security.jce.benchmark</groupId>
<artifactId>benchmark</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>benchmark</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<jmh.version>1.35</jmh.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
<dependency>
<groupId>com.tencent.kona</groupId>
<artifactId>kona-provider</artifactId>
<version>1.0.7.1</version>
</dependency>
<dependency>
<groupId>com.tencent.kona</groupId>
<artifactId>kona-ssl</artifactId>
<version>1.0.7.1</version>
</dependency>
<dependency>
<groupId>com.tencent.kona</groupId>
<artifactId>kona-crypto</artifactId>
<version>1.0.7.1</version>
</dependency>
<dependency>
<groupId>com.tencent.kona</groupId>
<artifactId>kona-pkix</artifactId>
<version>1.0.7.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.dragonwell</groupId>
<artifactId>security-native</artifactId>
<version>1.2.0</version>
<classifier>${os.detected.classifier}</classifier>
</dependency>
<dependency>
<groupId>software.amazon.cryptools</groupId>
<artifactId>AmazonCorrettoCryptoProvider</artifactId>
<version>[2.0, 3.0)</version>
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
5 changes: 5 additions & 0 deletions examples/jce/benchmark/sha.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

echo "SHA Digest Benchmark:"

mvn clean package && java -cp target/benchmark-1.0-SNAPSHOT-jar-with-dependencies.jar com.alibaba.dragonwell.security.jce.benchmark.MacBenchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.alibaba.dragonwell.security.jce.benchmark;

import java.security.Provider;
import java.util.concurrent.TimeUnit;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import com.alibaba.dragonwell.security.DragonwellSecurityProvider;
import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider;
import com.tencent.kona.KonaProvider;

@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 6, time = 1)
@Measurement(iterations = 6, time = 1)
@Threads(1)
@Fork(1)
@State(value = Scope.Thread)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class MacBenchmark {
private static final String HMAC_SHA256_ALGORITHM = "HmacSHA256";
private static final String HMAC_SHA384_ALGORITHM = "HmacSHA384";
private static final String HMAC_SHA512_ALGORITHM = "HmacSHA512";
public static final int AES_KEY_SIZE = 128;

@Param(value = { HMAC_SHA256_ALGORITHM, HMAC_SHA384_ALGORITHM, HMAC_SHA512_ALGORITHM })
private String MacAlgorithm;
@Param(value = { "4096", "8192", "12288" })
private String PlainTextLength;
@Param(value = { "DRAGONWELL", "BC", "SunJCE" })
private String Provider;

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(MacBenchmark.class.getSimpleName())
.result("dragonwell_mac_benchmark.json")
.resultFormat(ResultFormatType.JSON).build();
new Runner(opt).run();
}

private byte[] digest(MacBenchmark.MacBenchmarkContext context, String provider, String len, String algo) throws Exception {
Mac mac = Mac.getInstance(algo, context.chooseProvider(provider));
mac.init(context.getSecretKeySpec(algo));
return mac.doFinal(context.getPlainText(len));
}

@Benchmark
public void macDigestBenchMark(MacBenchmark.MacBenchmarkContext context, Blackhole blackhole) throws Exception {
blackhole.consume(digest(context, Provider, PlainTextLength, MacAlgorithm));
}

@State(Scope.Thread)
public static class MacBenchmarkContext {
private SecretKeySpec Secret_Key_Spec_256 = null;
private SecretKeySpec Secret_Key_Spec_384 = null;
private SecretKeySpec Secret_Key_Spec_512 = null;

private void initMacContext() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(AES_KEY_SIZE);
Secret_Key_Spec_256 = new SecretKeySpec(keyGenerator.generateKey().getEncoded(), HMAC_SHA256_ALGORITHM);
Secret_Key_Spec_384 = new SecretKeySpec(keyGenerator.generateKey().getEncoded(), HMAC_SHA384_ALGORITHM);
Secret_Key_Spec_512 = new SecretKeySpec(keyGenerator.generateKey().getEncoded(), HMAC_SHA512_ALGORITHM);
}

@Setup
public void createMacContext() throws Exception {
initMacContext();
}

public SecretKeySpec getSecretKeySpec(String algo) {
switch (algo) {
case HMAC_SHA256_ALGORITHM:
return Secret_Key_Spec_256;
case HMAC_SHA384_ALGORITHM:
return Secret_Key_Spec_384;
case HMAC_SHA512_ALGORITHM:
return Secret_Key_Spec_512;
default:
return null;
}
}

public byte[] getPlainText(String len) {
return Utils.getPlainText(len);
}

public Provider chooseProvider(String provider) {
return Utils.chooseProvider(provider);
}
}
}
Loading

0 comments on commit 955b4ca

Please sign in to comment.