From 20ab17fafc81b24c115ce00c882dc18c188c0f03 Mon Sep 17 00:00:00 2001 From: ChessNineeee <709030194@qq.com> Date: Mon, 19 Apr 2021 22:01:16 +0800 Subject: [PATCH] =?UTF-8?q?add:=E6=B7=BB=E5=8A=A0sm234=E7=9A=84jmh?= =?UTF-8?q?=E7=89=88=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95=EF=BC=8C=E4=B8=8E?= =?UTF-8?q?=E5=8A=9B=E5=AE=8F=E5=B8=88=E5=85=84=E7=9A=84=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E8=BF=9B=E8=A1=8C=E5=AF=B9=E6=AF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 9 +- .../java/minsecurity/crypto/SM2Benchmark.java | 91 +++++++++++++++++++ .../java/minsecurity/crypto/SM3Benchmark.java | 42 +++++++++ .../java/minsecurity/crypto/SM4Benchmark.java | 71 +++++++++++++++ src/test/java/security/KeychainBenchmark.java | 12 +-- 5 files changed, 218 insertions(+), 7 deletions(-) create mode 100644 src/test/java/minsecurity/crypto/SM2Benchmark.java create mode 100644 src/test/java/minsecurity/crypto/SM3Benchmark.java create mode 100644 src/test/java/minsecurity/crypto/SM4Benchmark.java diff --git a/pom.xml b/pom.xml index c8b5ed9..877c447 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,7 @@ org.apache.maven.plugins maven-compiler-plugin + 3.1 8 8 @@ -119,7 +120,13 @@ provided - + + + org.junit.jupiter + junit-jupiter-api + 5.6.0 + test + \ No newline at end of file diff --git a/src/test/java/minsecurity/crypto/SM2Benchmark.java b/src/test/java/minsecurity/crypto/SM2Benchmark.java new file mode 100644 index 0000000..788ef08 --- /dev/null +++ b/src/test/java/minsecurity/crypto/SM2Benchmark.java @@ -0,0 +1,91 @@ +package minsecurity.crypto; + + +import minsecurity.crypto.sm2.SM2Base; +import minsecurity.crypto.sm2.SM2PrivateKey; +import minsecurity.crypto.sm2.SM2PublicKey; +import minsecurity.identity.IdentityBenchmark; +import org.bouncycastle.crypto.AsymmetricCipherKeyPair; +import org.bouncycastle.crypto.CryptoException; +import org.bouncycastle.crypto.InvalidCipherTextException; +import org.bouncycastle.crypto.params.ECPrivateKeyParameters; +import org.bouncycastle.crypto.params.ECPublicKeyParameters; +import org.bouncycastle.pqc.math.linearalgebra.ByteUtils; +import org.openjdk.jmh.annotations.*; +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 java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@State(Scope.Thread) +@Fork(1) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@Warmup(iterations = 3) +@Measurement(iterations = 5) +public class SM2Benchmark { + private static String content = "7b227462734365727469666963617465223a7b2276657273696f6e223a312c2273657269616c4e756d626572223a312c227075626c69634b6579223a22424a304d7047714e5958706b695879575a4d6e5838776d6b694a51326f576d37336257615932525473706b695058342f37705443666332764e7272596e766c6a644d3448647850537531753272616363506c74485245593d222c227369676e6174757265416c676f726974686d223a302c227075626c69634b6579416c676f726974686d223a302c226973737565546f223a226973737565546f222c22697373756572223a22697373756572222c226e6f744265666f7265223a313631353032303130393339352c226e6f744166746572223a313631353032303130393539352c226b65795573616765223a322c2274696d657374616d70223a313631353032303130393339352c226361223a747275657d2c227369676e6174757265416c676f726974686d223a302c227369676e617475726556616c7565223a6e756c6c7d"; + private static SM2PrivateKey sm2PrivateKey; + + private static SM2PublicKey sm2PublicKey; + private static byte[] digest; + private static byte[] encBytes; + private static String content2; + static { + try { + init(); // 初始化类成员公私钥 + digest = sm2PrivateKey.sign(ByteUtils.fromHexString(content)); // 初始化类成员摘要 + content2 = "hello world"; + encBytes = sm2PublicKey.encrypt(content2.getBytes()); + } catch (CryptoException e) { + e.printStackTrace(); + } + } + + private static void init(){ + AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter(); + ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate(); + ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic(); + byte[] d = priKey.getD().toByteArray(); + // d = Arrays.copyOf(d,32); + byte[] x = pubKey.getQ().getAffineXCoord().getEncoded(); + byte[] y = pubKey.getQ().getAffineYCoord().getEncoded(); + + // BigInteger bigInteger = priKey.getD(); + sm2PrivateKey = new SM2PrivateKey(d); + sm2PublicKey = new SM2PublicKey(x,y); + } + + @Benchmark + public void testSign() throws CryptoException { + SM2Benchmark.sm2PrivateKey.sign(ByteUtils.fromHexString(content)); + } + + @Benchmark + public void testVerify() throws CryptoException { + SM2Benchmark.sm2PublicKey.verify(ByteUtils.fromHexString(content), digest); + } + + @Benchmark + public void testGenKeyPair(){ + SM2Base.generateKeyPairParameter(); + } + + @Benchmark + public void testEncrypt() throws InvalidCipherTextException { + sm2PublicKey.encrypt(content2.getBytes()); + } + + @Benchmark + public void testDecrypt() throws InvalidCipherTextException { + sm2PrivateKey.decrypt(encBytes); + } + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(SM2Benchmark.class.getSimpleName()) + .build(); + new Runner(opt).run(); + } +} diff --git a/src/test/java/minsecurity/crypto/SM3Benchmark.java b/src/test/java/minsecurity/crypto/SM3Benchmark.java new file mode 100644 index 0000000..6f109d6 --- /dev/null +++ b/src/test/java/minsecurity/crypto/SM3Benchmark.java @@ -0,0 +1,42 @@ +package minsecurity.crypto; + +import org.openjdk.jmh.annotations.*; +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 java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@State(Scope.Thread) +@Fork(1) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@Warmup(iterations = 3) +@Measurement(iterations = 5) +public class SM3Benchmark { + private static HashAlgo hashAlgo; + private static String srcString = "123456"; + private static byte[] srcData = srcString.getBytes(); + static { + hashAlgo = new HashAlgo(); + } + + @Benchmark + public void testSM3(){ + hashAlgo.sm3(srcData); + } + + @Benchmark + public void testSM3ByString(){ + hashAlgo.sm3(srcString); + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(SM3Benchmark.class.getSimpleName()) + .build(); + new Runner(opt).run(); + } +} diff --git a/src/test/java/minsecurity/crypto/SM4Benchmark.java b/src/test/java/minsecurity/crypto/SM4Benchmark.java new file mode 100644 index 0000000..af4c852 --- /dev/null +++ b/src/test/java/minsecurity/crypto/SM4Benchmark.java @@ -0,0 +1,71 @@ +package minsecurity.crypto; + +import minsecurity.identity.IdentityBenchmark; +import org.openjdk.jmh.annotations.*; +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 javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.util.concurrent.TimeUnit; + +import static minsecurity.crypto.TestSM4.SRC_DATA; +import static minsecurity.crypto.TestSM4.SRC_DATA_16B; + +@BenchmarkMode(Mode.AverageTime) +@State(Scope.Thread) +@Fork(4) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@Warmup(iterations = 3) +@Measurement(iterations = 5) +public class SM4Benchmark { + private static byte[] key; + private static byte[] iv; + private static byte[] cipherText; + private static byte[] cipherText2; + + static { + try { + key = SM4.generateKey(); + iv = SM4.generateKey(); + cipherText = SM4.encrypt_ECB_Padding(key, SRC_DATA_16B); + cipherText2 = SM4.encrypt_CBC_Padding(key, iv, SRC_DATA); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Benchmark + public void testECBEncrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException { + SM4.encrypt_ECB_Padding(key, SRC_DATA_16B); + } + + @Benchmark + public void testECBDecrypt() throws BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException { + SM4.decrypt_ECB_Padding(key, cipherText); + } + + @Benchmark + public void testCBCEncrypt() throws NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException { + SM4.encrypt_CBC_Padding(key, iv, SRC_DATA); + } + + @Benchmark + public void testCBCDecrypt() throws BadPaddingException, NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException { + SM4.decrypt_CBC_Padding(key, iv, cipherText2); + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(SM4Benchmark.class.getSimpleName()) + .build(); + new Runner(opt).run(); + } +} diff --git a/src/test/java/security/KeychainBenchmark.java b/src/test/java/security/KeychainBenchmark.java index 44c9ff5..5c549fb 100644 --- a/src/test/java/security/KeychainBenchmark.java +++ b/src/test/java/security/KeychainBenchmark.java @@ -20,12 +20,12 @@ import packet.Interest; import java.util.concurrent.TimeUnit; -@BenchmarkMode(Mode.AverageTime) -@State(Scope.Thread) -@Fork(1) -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@Warmup(iterations = 3) -@Measurement(iterations = 5) +@BenchmarkMode(Mode.AverageTime) // 统计平均响应时间 +@State(Scope.Thread) // 每个进行基准测试的线程都会独享一个对象示例 +@Fork(1) // 开启一个线程进行测试 +@OutputTimeUnit(TimeUnit.MILLISECONDS) // 输出的时间单位 +@Warmup(iterations = 3) // 微基准测试前进行三次预热执行 +@Measurement(iterations = 5) // 进行五次微基准测试 public class KeychainBenchmark { /** * 随机生成身份数据