diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/ECOperatorPerfTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/ECOperatorPerfTest.java deleted file mode 100644 index 33dae094..00000000 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/ECOperatorPerfTest.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -package com.tencent.kona.crypto.perf; - -import com.tencent.kona.crypto.CryptoUtils; -import com.tencent.kona.crypto.spec.SM2ParameterSpec; -import com.tencent.kona.sun.security.ec.ECOperations; -import org.bouncycastle.math.ec.FixedPointCombMultiplier; -import org.bouncycastle.math.ec.custom.gm.SM2P256V1Curve; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; - -import java.math.BigInteger; -import java.security.spec.ECPoint; -import java.util.concurrent.TimeUnit; - -/** - * The JMH-based performance test for EC operations. - */ -@Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) -@Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) -@Threads(1) -@BenchmarkMode(Mode.Throughput) -@OutputTimeUnit(TimeUnit.SECONDS) -public class ECOperatorPerfTest { - - private static final SM2ParameterSpec SM2SPEC = SM2ParameterSpec.instance(); - private static final ECPoint GENERATOR = SM2SPEC.getGenerator(); - - private final static byte[] PRIV_KEY = CryptoUtils.toBytes( - "00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000"); - private static final BigInteger PRIV_KEY_BIG_INT = CryptoUtils.toBigInt(PRIV_KEY); - - @State(Scope.Benchmark) - public static class OperatorHolderBC { - - FixedPointCombMultiplier multiplier; - org.bouncycastle.math.ec.ECPoint generator; - - @Setup(Level.Trial) - public void setup() throws Exception { - multiplier = new FixedPointCombMultiplier(); - - SM2P256V1Curve curve = new SM2P256V1Curve(); - generator = curve.createPoint(GENERATOR.getAffineX(), GENERATOR.getAffineY()); - - } - } - - @Benchmark - public Object multiple() { - return ECOperations.SM2OPS.multiply(GENERATOR, PRIV_KEY); - } - - @Benchmark - public Object multipleBC(OperatorHolderBC holder) { - return holder.multiplier.multiply(holder.generator, PRIV_KEY_BIG_INT); - } -} diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2CipherConstantTimeTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2CipherConstantTimeTest.java index 9445c606..0158d3b6 100644 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2CipherConstantTimeTest.java +++ b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2CipherConstantTimeTest.java @@ -25,7 +25,6 @@ import com.tencent.kona.crypto.spec.SM2ParameterSpec; import com.tencent.kona.crypto.util.Constants; import com.tencent.kona.sun.security.ec.ECOperator; -import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; @@ -43,14 +42,13 @@ import javax.crypto.Cipher; import java.math.BigInteger; import java.security.KeyPair; -import java.security.Security; import java.util.concurrent.TimeUnit; /** * The JMH-based performance test for checking constant-time issue. */ @Warmup(iterations = 3, time = 5) -@Measurement(iterations = 3, time = 5) +@Measurement(iterations = 5, time = 5) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) @BenchmarkMode(Mode.Throughput) @@ -69,7 +67,6 @@ public class SM2CipherConstantTimeTest { static { TestUtils.addProviders(); - Security.addProvider(new BouncyCastleProvider()); } private static KeyPair keyPair(BigInteger priKeyValue) { @@ -82,7 +79,7 @@ private static KeyPair keyPair(BigInteger priKeyValue) { @State(Scope.Thread) public static class CipherHolder { - @Param({"KonaCrypto", "KonaCrypto-Native", "BC"}) + @Param({"KonaCrypto", "KonaCrypto-Native"}) String provider; @Param({"Small", "Mid", "Big"}) diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2CipherPerfTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2CipherPerfTest.java index 12de4e86..20d3fda1 100644 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2CipherPerfTest.java +++ b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2CipherPerfTest.java @@ -20,19 +20,17 @@ package com.tencent.kona.crypto.perf; import com.tencent.kona.crypto.TestUtils; -import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.openjdk.jmh.annotations.*; import javax.crypto.Cipher; import java.security.KeyPair; -import java.security.Security; import java.util.concurrent.TimeUnit; /** * The JMH-based performance test for SM2 decryption. */ @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) @BenchmarkMode(Mode.Throughput) @@ -41,7 +39,6 @@ public class SM2CipherPerfTest { static { TestUtils.addProviders(); - Security.addProvider(new BouncyCastleProvider()); } private final static String PUB_KEY @@ -49,40 +46,28 @@ public class SM2CipherPerfTest { private final static String PRI_KEY = "3B03B35C2F26DBC56F6D33677F1B28AF15E45FE9B594A6426BDCAD4A69FF976B"; private final static KeyPair KEY_PAIR = TestUtils.keyPair(PUB_KEY, PRI_KEY); - private static final byte[] MESSAGE = TestUtils.dataKB(1); + + private final static byte[] SMALL_DATA = TestUtils.data(128); + private final static byte[] MEDIUM_DATA = TestUtils.dataKB(1); + private final static byte[] BIG_DATA = TestUtils.dataMB(1); @State(Scope.Benchmark) public static class EncrypterHolder { - Cipher encrypter; + @Param({"KonaCrypto", "KonaCrypto-Native"}) + String provider; - @Setup(Level.Trial) - public void setup() throws Exception { - encrypter = Cipher.getInstance("SM2", "KonaCrypto"); - encrypter.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic()); - } - } - - @State(Scope.Benchmark) - public static class EncrypterHolderNative { + @Param({"Small", "Mid", "Big"}) + String dataType; + byte[] data; Cipher encrypter; @Setup(Level.Trial) public void setup() throws Exception { - encrypter = Cipher.getInstance("SM2", "KonaCrypto-Native"); - encrypter.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic()); - } - } - - @State(Scope.Benchmark) - public static class EncrypterHolderBC { - - Cipher encrypter; + data = data(dataType); - @Setup(Level.Trial) - public void setup() throws Exception { - encrypter = Cipher.getInstance("SM2", "BC"); + encrypter = Cipher.getInstance("SM2", provider); encrypter.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic()); } } @@ -90,25 +75,11 @@ public void setup() throws Exception { @State(Scope.Benchmark) public static class DecrypterHolder { - byte[] ciphertext; - Cipher decrypter; + @Param({"KonaCrypto", "KonaCrypto-Native"}) + String provider; - @Setup(Level.Trial) - public void setup() throws Exception { - ciphertext = ciphertext(); - decrypter = Cipher.getInstance("SM2", "KonaCrypto"); - decrypter.init(Cipher.DECRYPT_MODE, KEY_PAIR.getPrivate()); - } - - private byte[] ciphertext() throws Exception { - Cipher cipher = Cipher.getInstance("SM2", "KonaCrypto"); - cipher.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic()); - return cipher.doFinal(MESSAGE); - } - } - - @State(Scope.Benchmark) - public static class DecrypterHolderNative { + @Param({"Small", "Mid", "Big"}) + String dataType; byte[] ciphertext; Cipher decrypter; @@ -116,64 +87,34 @@ public static class DecrypterHolderNative { @Setup(Level.Trial) public void setup() throws Exception { ciphertext = ciphertext(); - decrypter = Cipher.getInstance("SM2", "KonaCrypto-Native"); + decrypter = Cipher.getInstance("SM2", provider); decrypter.init(Cipher.DECRYPT_MODE, KEY_PAIR.getPrivate()); } private byte[] ciphertext() throws Exception { - Cipher cipher = Cipher.getInstance("SM2", "KonaCrypto-Native"); + Cipher cipher = Cipher.getInstance("SM2", provider); cipher.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic()); - return cipher.doFinal(MESSAGE); + return cipher.doFinal(data(dataType)); } } - @State(Scope.Benchmark) - public static class DecrypterHolderBC { - - byte[] ciphertext; - Cipher decrypter; - - @Setup(Level.Trial) - public void setup() throws Exception { - ciphertext = ciphertext(); - decrypter = Cipher.getInstance("SM2", "BC"); - decrypter.init(Cipher.DECRYPT_MODE, KEY_PAIR.getPrivate()); - } - - private byte[] ciphertext() throws Exception { - Cipher cipher = Cipher.getInstance("SM2", "BC"); - cipher.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic()); - return cipher.doFinal(MESSAGE); + private static byte[] data(String dataType) { + switch (dataType) { + case "Small": return SMALL_DATA; + case "Mid": return MEDIUM_DATA; + case "Big": return BIG_DATA; + default: throw new IllegalArgumentException( + "Unsupported data type: " + dataType); } } @Benchmark public byte[] encrypt(EncrypterHolder holder) throws Exception { - return holder.encrypter.doFinal(MESSAGE); - } - - @Benchmark - public byte[] encryptNative(EncrypterHolderNative holder) throws Exception { - return holder.encrypter.doFinal(MESSAGE); - } - - @Benchmark - public byte[] encryptBC(EncrypterHolderBC holder) throws Exception { - return holder.encrypter.doFinal(MESSAGE); + return holder.encrypter.doFinal(holder.data); } @Benchmark public byte[] decrypt(DecrypterHolder holder) throws Exception { return holder.decrypter.doFinal(holder.ciphertext); } - - @Benchmark - public byte[] decryptNative(DecrypterHolderNative holder) throws Exception { - return holder.decrypter.doFinal(holder.ciphertext); - } - - @Benchmark - public byte[] decryptBC(DecrypterHolderBC holder) throws Exception { - return holder.decrypter.doFinal(holder.ciphertext); - } } diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2KeyAgreementPerfTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2KeyAgreementPerfTest.java index c9b0ba63..33f2efd5 100644 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2KeyAgreementPerfTest.java +++ b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2KeyAgreementPerfTest.java @@ -23,49 +23,19 @@ import com.tencent.kona.crypto.provider.SM2PrivateKey; import com.tencent.kona.crypto.provider.SM2PublicKey; import com.tencent.kona.crypto.spec.SM2KeyAgreementParamSpec; -import org.bouncycastle.crypto.agreement.SM2KeyExchange; -import org.bouncycastle.crypto.params.ECDomainParameters; -import org.bouncycastle.crypto.params.ECPrivateKeyParameters; -import org.bouncycastle.crypto.params.ECPublicKeyParameters; -import org.bouncycastle.crypto.params.ParametersWithID; -import org.bouncycastle.crypto.params.SM2KeyExchangePrivateParameters; -import org.bouncycastle.crypto.params.SM2KeyExchangePublicParameters; -import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; -import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; -import org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.bouncycastle.math.ec.ECCurve; -import org.bouncycastle.math.ec.ECPoint; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.annotations.*; import javax.crypto.KeyAgreement; import java.security.InvalidKeyException; -import java.security.Security; -import java.security.spec.ECFieldFp; import java.util.concurrent.TimeUnit; import static com.tencent.kona.crypto.CryptoUtils.toBytes; -import static com.tencent.kona.crypto.spec.SM2ParameterSpec.COFACTOR; -import static com.tencent.kona.crypto.spec.SM2ParameterSpec.CURVE; -import static com.tencent.kona.crypto.spec.SM2ParameterSpec.GENERATOR; -import static com.tencent.kona.crypto.spec.SM2ParameterSpec.ORDER; /** * The JMH-based performance test for SM2 encryption. */ @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) @BenchmarkMode(Mode.Throughput) @@ -94,32 +64,13 @@ public class SM2KeyAgreementPerfTest { static { TestUtils.addProviders(); - Security.addProvider(new BouncyCastleProvider()); } @State(Scope.Benchmark) public static class KeyAgreementHolder { - KeyAgreement keyAgreement; - - @Setup(Level.Invocation) - public void setup() throws Exception { - SM2KeyAgreementParamSpec paramSpec = new SM2KeyAgreementParamSpec( - toBytes(ID), - new SM2PrivateKey(toBytes(PRI_KEY)), - new SM2PublicKey(toBytes(PUB_KEY)), - toBytes(PEER_ID), - new SM2PublicKey(toBytes(PEER_PUB_KEY)), - true, - 16); - keyAgreement = KeyAgreement.getInstance("SM2", "KonaCrypto"); - keyAgreement.init( - new SM2PrivateKey(toBytes(TMP_PRI_KEY)), paramSpec); - } - } - - @State(Scope.Benchmark) - public static class KeyAgreementNativeHolder { + @Param({"KonaCrypto", "KonaCrypto-Native"}) + String provider; KeyAgreement keyAgreement; @@ -133,74 +84,15 @@ public void setup() throws Exception { new SM2PublicKey(toBytes(PEER_PUB_KEY)), true, 16); - keyAgreement = KeyAgreement.getInstance("SM2", "KonaCrypto-Native"); + keyAgreement = KeyAgreement.getInstance("SM2", provider); keyAgreement.init( new SM2PrivateKey(toBytes(TMP_PRI_KEY)), paramSpec); } } - @State(Scope.Benchmark) - public static class KeyAgreementHolderBC { - - SM2KeyExchange keyAgreement; - ParametersWithID params; - - @Setup(Level.Invocation) - public void setup() throws Exception { - ECCurve ecCurve = new ECCurve.Fp( - ((ECFieldFp) CURVE.getField()).getP(), - CURVE.getA(), CURVE.getB(), ORDER, COFACTOR); - ECPoint genPoint = ecCurve.createPoint( - GENERATOR.getAffineX(), GENERATOR.getAffineY()); - ECDomainParameters ecDomainParams = new ECDomainParameters( - ecCurve, genPoint, ORDER); - - BCECPrivateKey privateKey = new BCECPrivateKey( - new SM2PrivateKey(toBytes(PRI_KEY)), null); - BCECPrivateKey tmpPrivateKey = new BCECPrivateKey( - new SM2PrivateKey(toBytes(TMP_PRI_KEY)), null); - - BCECPublicKey peerPublicKey = new BCECPublicKey( - new SM2PublicKey(toBytes(PEER_PUB_KEY)), null); - BCECPublicKey peerTmpPublicKey = new BCECPublicKey( - new SM2PublicKey(toBytes(PEER_TMP_PUB_KEY)), null); - - ECPrivateKeyParameters privateKeyParams = new ECPrivateKeyParameters( - privateKey.getS(), ecDomainParams); - ECPrivateKeyParameters tmpPrivateKeyParams = new ECPrivateKeyParameters( - tmpPrivateKey.getS(), ecDomainParams); - keyAgreement = new SM2KeyExchange(); - keyAgreement.init(new ParametersWithID( - new SM2KeyExchangePrivateParameters( - true, privateKeyParams, tmpPrivateKeyParams), - toBytes(ID))); - - ECPublicKeyParameters peerPublicKeyParams = new ECPublicKeyParameters( - EC5Util.convertPoint(ecCurve, peerPublicKey.getW()), - ecDomainParams); - ECPublicKeyParameters peerTmpPublicKeyParams = new ECPublicKeyParameters( - EC5Util.convertPoint(ecCurve, peerTmpPublicKey.getW()), - ecDomainParams); - params = new ParametersWithID(new SM2KeyExchangePublicParameters( - peerPublicKeyParams, peerTmpPublicKeyParams), - toBytes(PEER_ID)); - } - } - @Benchmark public byte[] generateSecret(KeyAgreementHolder holder) throws InvalidKeyException { holder.keyAgreement.doPhase(new SM2PublicKey(toBytes(PEER_TMP_PUB_KEY)), true); return holder.keyAgreement.generateSecret(); } - - @Benchmark - public byte[] generateSecretNative(KeyAgreementNativeHolder holder) throws InvalidKeyException { - holder.keyAgreement.doPhase(new SM2PublicKey(toBytes(PEER_TMP_PUB_KEY)), true); - return holder.keyAgreement.generateSecret(); - } - - @Benchmark - public byte[] generateSecretBC(KeyAgreementHolderBC holder) { - return holder.keyAgreement.calculateKey(128, holder.params); - } } diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2KeyPairGenPerfTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2KeyPairGenPerfTest.java index bf9a9bd3..b4749405 100644 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2KeyPairGenPerfTest.java +++ b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2KeyPairGenPerfTest.java @@ -20,30 +20,17 @@ package com.tencent.kona.crypto.perf; import com.tencent.kona.crypto.TestUtils; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -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.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.annotations.*; import java.security.KeyPair; import java.security.KeyPairGenerator; -import java.security.Security; -import java.security.spec.ECGenParameterSpec; import java.util.concurrent.TimeUnit; /** * The JMH-based performance test for SM2 key pair generation. */ @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) @BenchmarkMode(Mode.Throughput) @@ -52,40 +39,19 @@ public class SM2KeyPairGenPerfTest { static { TestUtils.addProviders(); - Security.addProvider(new BouncyCastleProvider()); } @State(Scope.Benchmark) public static class KeyPairGenHolder { - KeyPairGenerator keyPairGenerator; - - @Setup - public void setup() throws Exception { - keyPairGenerator = KeyPairGenerator.getInstance("SM2", "KonaCrypto"); - } - } - - @State(Scope.Benchmark) - public static class KeyPairGenHolderNative { + @Param({"KonaCrypto", "KonaCrypto-Native"}) + String provider; KeyPairGenerator keyPairGenerator; @Setup public void setup() throws Exception { - keyPairGenerator = KeyPairGenerator.getInstance("SM2", "KonaCrypto-Native"); - } - } - - @State(Scope.Benchmark) - public static class KeyPairGenHolderBC { - - KeyPairGenerator keyPairGenerator; - - @Setup - public void setup() throws Exception { - keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC"); - keyPairGenerator.initialize(new ECGenParameterSpec("sm2p256v1")); + keyPairGenerator = KeyPairGenerator.getInstance("SM2", provider); } } @@ -93,14 +59,4 @@ public void setup() throws Exception { public KeyPair genKeyPair(KeyPairGenHolder holder) { return holder.keyPairGenerator.generateKeyPair(); } - - @Benchmark - public KeyPair genKeyPairNative(KeyPairGenHolderNative holder) { - return holder.keyPairGenerator.generateKeyPair(); - } - - @Benchmark - public KeyPair genKeyPairBC(KeyPairGenHolderBC holder) { - return holder.keyPairGenerator.generateKeyPair(); - } } diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2SignaturePerfTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2SignaturePerfTest.java index c068288d..0ba07710 100644 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2SignaturePerfTest.java +++ b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2SignaturePerfTest.java @@ -21,24 +21,9 @@ import com.tencent.kona.crypto.TestUtils; import com.tencent.kona.crypto.spec.SM2SignatureParameterSpec; -import org.bouncycastle.asn1.gm.GMObjectIdentifiers; -import org.bouncycastle.jcajce.spec.SM2ParameterSpec; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.annotations.*; import java.security.KeyPair; -import java.security.Security; import java.security.Signature; import java.security.interfaces.ECPublicKey; import java.util.concurrent.TimeUnit; @@ -49,7 +34,7 @@ * The JMH-based performance test for SM2 signature. */ @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) @BenchmarkMode(Mode.Throughput) @@ -58,7 +43,6 @@ public class SM2SignaturePerfTest { static { TestUtils.addProviders(); - Security.addProvider(new BouncyCastleProvider()); } private final static String PUB_KEY @@ -67,160 +51,87 @@ public class SM2SignaturePerfTest { = "3B03B35C2F26DBC56F6D33677F1B28AF15E45FE9B594A6426BDCAD4A69FF976B"; private final static KeyPair KEY_PAIR = TestUtils.keyPair(PUB_KEY, PRI_KEY); private final static byte[] ID = toBytes("01234567"); - private final static byte[] MESSAGE = TestUtils.dataKB(1); + + private final static byte[] SMALL_DATA = TestUtils.data(128); + private final static byte[] MEDIUM_DATA = TestUtils.dataKB(1); + private final static byte[] BIG_DATA = TestUtils.dataMB(1); @State(Scope.Benchmark) public static class SignerHolder { - Signature signer; + @Param({"KonaCrypto", "KonaCrypto-Native"}) + String provider; - @Setup(Level.Trial) - public void setup() throws Exception { - signer = Signature.getInstance("SM2", "KonaCrypto"); - signer.setParameter(new SM2SignatureParameterSpec( - ID, (ECPublicKey) KEY_PAIR.getPublic())); - signer.initSign(KEY_PAIR.getPrivate()); - } - } - - @State(Scope.Benchmark) - public static class SignerHolderNative { + @Param({"Small", "Mid", "Big"}) + String dataType; + byte[] data; Signature signer; @Setup(Level.Trial) public void setup() throws Exception { - signer = Signature.getInstance("SM2", "KonaCrypto-Native"); + data = data(dataType); + + signer = Signature.getInstance("SM2", provider); signer.setParameter(new SM2SignatureParameterSpec( ID, (ECPublicKey) KEY_PAIR.getPublic())); signer.initSign(KEY_PAIR.getPrivate()); } } - @State(Scope.Benchmark) - public static class SignerHolderBC { - - Signature signer; - - @Setup(Level.Trial) - public void setup() throws Exception { - signer = Signature.getInstance( - GMObjectIdentifiers.sm2sign_with_sm3.toString(), "BC"); - signer.setParameter(new SM2ParameterSpec(ID)); - signer.initSign(KEY_PAIR.getPrivate()); - } - } - @State(Scope.Benchmark) public static class VerifierHolder { - byte[] signature; - Signature verifier; - - @Setup(Level.Trial) - public void setup() throws Exception { - signature = signature(); + @Param({"KonaCrypto", "KonaCrypto-Native"}) + String provider; - verifier = Signature.getInstance("SM2", "KonaCrypto"); - verifier.setParameter(new SM2SignatureParameterSpec( - ID, (ECPublicKey) KEY_PAIR.getPublic())); - verifier.initVerify(KEY_PAIR.getPublic()); - } - - private byte[] signature() throws Exception { - Signature signer = Signature.getInstance("SM2", "KonaCrypto"); - signer.setParameter(new SM2SignatureParameterSpec( - ID, (ECPublicKey) KEY_PAIR.getPublic())); - signer.initSign(KEY_PAIR.getPrivate()); - signer.update(MESSAGE); - return signer.sign(); - } - } - - @State(Scope.Benchmark) - public static class VerifierHolderNative { + @Param({"Small", "Mid", "Big"}) + String dataType; + byte[] data; byte[] signature; Signature verifier; @Setup(Level.Trial) public void setup() throws Exception { + data = data(dataType); signature = signature(); - verifier = Signature.getInstance("SM2", "KonaCrypto-Native"); + verifier = Signature.getInstance("SM2", provider); verifier.setParameter(new SM2SignatureParameterSpec( ID, (ECPublicKey) KEY_PAIR.getPublic())); verifier.initVerify(KEY_PAIR.getPublic()); } private byte[] signature() throws Exception { - Signature signer = Signature.getInstance("SM2", "KonaCrypto-Native"); + Signature signer = Signature.getInstance("SM2", provider); signer.setParameter(new SM2SignatureParameterSpec( ID, (ECPublicKey) KEY_PAIR.getPublic())); signer.initSign(KEY_PAIR.getPrivate()); - signer.update(MESSAGE); + signer.update(data); return signer.sign(); } } - @State(Scope.Benchmark) - public static class VerifierHolderBC { - - byte[] signature; - Signature verifier; - - @Setup(Level.Trial) - public void setup() throws Exception { - signature = signature(); - - verifier = Signature.getInstance( - GMObjectIdentifiers.sm2sign_with_sm3.toString(), "BC"); - verifier.setParameter(new SM2ParameterSpec(ID)); - verifier.initVerify(KEY_PAIR.getPublic()); - } - - private byte[] signature() throws Exception { - Signature signer = Signature.getInstance( - GMObjectIdentifiers.sm2sign_with_sm3.toString(), "BC"); - signer.setParameter(new SM2ParameterSpec(ID)); - signer.initSign(KEY_PAIR.getPrivate()); - return signer.sign(); + private static byte[] data(String dataType) { + switch (dataType) { + case "Small": return SMALL_DATA; + case "Mid": return MEDIUM_DATA; + case "Big": return BIG_DATA; + default: throw new IllegalArgumentException( + "Unsupported data type: " + dataType); } } @Benchmark public byte[] sign(SignerHolder holder) throws Exception { - holder.signer.update(MESSAGE); - return holder.signer.sign(); - } - - @Benchmark - public byte[] signNative(SignerHolderNative holder) throws Exception { - holder.signer.update(MESSAGE); - return holder.signer.sign(); - } - - @Benchmark - public byte[] signBC(SignerHolderBC holder) throws Exception { - holder.signer.update(MESSAGE); + holder.signer.update(holder.data); return holder.signer.sign(); } @Benchmark public boolean verify(VerifierHolder holder) throws Exception { - holder.verifier.update(MESSAGE); - return holder.verifier.verify(holder.signature); - } - - @Benchmark - public boolean verifyNative(VerifierHolderNative holder) throws Exception { - holder.verifier.update(MESSAGE); - return holder.verifier.verify(holder.signature); - } - - @Benchmark - public boolean verifyBC(VerifierHolderBC holder) throws Exception { - holder.verifier.update(MESSAGE); + holder.verifier.update(holder.data); return holder.verifier.verify(holder.signature); } } diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3EnginePerfTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3EnginePerfTest.java deleted file mode 100644 index 7031c584..00000000 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3EnginePerfTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -package com.tencent.kona.crypto.perf; - -import com.tencent.kona.crypto.TestUtils; -import com.tencent.kona.crypto.provider.SM3Engine; -import org.bouncycastle.crypto.digests.SM3Digest; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; - -import java.util.concurrent.TimeUnit; - -/** - * The JMH-based performance test for SM3 engine. - */ -@Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) -@Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) -@Threads(1) -@BenchmarkMode(Mode.Throughput) -@OutputTimeUnit(TimeUnit.SECONDS) -public class SM3EnginePerfTest { - - private final static byte[] MESSAGE = TestUtils.dataMB(1); - - @State(Scope.Benchmark) - public static class EngineHolder { - - SM3Engine engine; - byte[] digest = new byte[32]; - - @Setup(Level.Trial) - public void setup() throws Exception { - engine = new SM3Engine(); - } - } - - @State(Scope.Benchmark) - public static class EngineHolderBC { - - SM3Digest engine; - byte[] digest = new byte[32]; - - @Setup(Level.Trial) - public void setup() throws Exception { - engine = new SM3Digest(); - } - } - - @Benchmark - public byte[] digest(EngineHolder holder) { - holder.engine.update(MESSAGE, 0, MESSAGE.length); - holder.engine.doFinal(holder.digest, 0); - return holder.digest; - } - - @Benchmark - public byte[] digestBC(EngineHolderBC holder) { - holder.engine.update(MESSAGE, 0, MESSAGE.length); - holder.engine.doFinal(holder.digest, 0); - return holder.digest; - } -} diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3HMacPerfTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3HMacPerfTest.java index 96d0a749..e6716be0 100644 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3HMacPerfTest.java +++ b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3HMacPerfTest.java @@ -20,25 +20,12 @@ package com.tencent.kona.crypto.perf; import com.tencent.kona.crypto.TestUtils; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.annotations.*; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; -import java.security.Security; import java.util.concurrent.TimeUnit; import static com.tencent.kona.crypto.CryptoUtils.toBytes; @@ -47,7 +34,7 @@ * The JMH-based performance test for SM3 HMAC. */ @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) @BenchmarkMode(Mode.Throughput) @@ -56,61 +43,48 @@ public class SM3HMacPerfTest { private static final byte[] KEY = toBytes("0123456789abcdef0123456789abcdef"); private static final SecretKey SECRET_KEY = new SecretKeySpec(KEY, "SM4"); - private static final byte[] MESSAGE = TestUtils.dataMB(1); + + private final static byte[] SMALL_DATA = TestUtils.data(128); + private final static byte[] MEDIUM_DATA = TestUtils.dataKB(1); + private final static byte[] BIG_DATA = TestUtils.dataMB(1); static { TestUtils.addProviders(); - Security.addProvider(new BouncyCastleProvider()); } @State(Scope.Benchmark) public static class MacHolder { - Mac mac; - - @Setup(Level.Trial) - public void setup() throws Exception { - mac = Mac.getInstance("HmacSM3", "KonaCrypto"); - mac.init(SECRET_KEY); - } - } + @Param({"KonaCrypto", "KonaCrypto-Native"}) + String provider; - @State(Scope.Benchmark) - public static class MacHolderNative { + @Param({"Small", "Mid", "Big"}) + String dataType; + byte[] data; Mac mac; @Setup(Level.Trial) public void setup() throws Exception { - mac = Mac.getInstance("HmacSM3", "KonaCrypto-Native"); + data = data(dataType); + + mac = Mac.getInstance("HmacSM3", provider); mac.init(SECRET_KEY); } } - @State(Scope.Benchmark) - public static class MacHolderBC { - - Mac mac; - - @Setup(Level.Trial) - public void setup() throws Exception { - mac = Mac.getInstance("HMACSM3", "BC"); - mac.init(SECRET_KEY); + private static byte[] data(String dataType) { + switch (dataType) { + case "Small": return SMALL_DATA; + case "Mid": return MEDIUM_DATA; + case "Big": return BIG_DATA; + default: throw new IllegalArgumentException( + "Unsupported data type: " + dataType); } } @Benchmark public byte[] mac(MacHolder holder) throws Exception { - return holder.mac.doFinal(MESSAGE); - } - - @Benchmark - public byte[] macNative(MacHolderNative holder) throws Exception { - return holder.mac.doFinal(MESSAGE); - } - - @Benchmark - public byte[] macBC(MacHolderBC holder) throws Exception { - return holder.mac.doFinal(MESSAGE); + return holder.mac.doFinal(holder.data); } } diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3MessageDigestPerfTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3MessageDigestPerfTest.java index a20bbc64..ca2f1b07 100644 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3MessageDigestPerfTest.java +++ b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3MessageDigestPerfTest.java @@ -20,87 +20,62 @@ package com.tencent.kona.crypto.perf; import com.tencent.kona.crypto.TestUtils; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.annotations.*; import java.security.MessageDigest; -import java.security.Security; import java.util.concurrent.TimeUnit; /** * The JMH-based performance test for SM3 message digest. */ @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) @BenchmarkMode(Mode.Throughput) @OutputTimeUnit(TimeUnit.SECONDS) public class SM3MessageDigestPerfTest { - private final static byte[] MESSAGE = TestUtils.dataMB(1); + private final static byte[] SMALL_DATA = TestUtils.data(128); + private final static byte[] MEDIUM_DATA = TestUtils.dataKB(1); + private final static byte[] BIG_DATA = TestUtils.dataMB(1); static { TestUtils.addProviders(); - Security.addProvider(new BouncyCastleProvider()); } @State(Scope.Benchmark) public static class MessageDigestHolder { - MessageDigest md; - - @Setup(Level.Trial) - public void setup() throws Exception { - md = MessageDigest.getInstance("SM3", "KonaCrypto"); - } - } + @Param({"KonaCrypto", "KonaCrypto-Native"}) + String provider; - @State(Scope.Benchmark) - public static class MessageDigestHolderNative { + @Param({"Small", "Mid", "Big"}) + String dataType; + byte[] data; MessageDigest md; @Setup(Level.Trial) public void setup() throws Exception { - md = MessageDigest.getInstance("SM3", "KonaCrypto-Native"); + data = data(dataType); + + md = MessageDigest.getInstance("SM3", provider); } } - @State(Scope.Benchmark) - public static class MessageDigestHolderBC { - - MessageDigest md; - - @Setup(Level.Trial) - public void setup() throws Exception { - md = MessageDigest.getInstance("SM3", "BC"); + private static byte[] data(String dataType) { + switch (dataType) { + case "Small": return SMALL_DATA; + case "Mid": return MEDIUM_DATA; + case "Big": return BIG_DATA; + default: throw new IllegalArgumentException( + "Unsupported data type: " + dataType); } } @Benchmark public byte[] digest(MessageDigestHolder holder) { - return holder.md.digest(MESSAGE); - } - - @Benchmark - public byte[] digestNative(MessageDigestHolderNative holder) { - return holder.md.digest(MESSAGE); - } - - @Benchmark - public byte[] digestBC(MessageDigestHolderBC holder) { - return holder.md.digest(MESSAGE); + return holder.md.digest(holder.data); } } diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4ConstantTimeTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4ConstantTimeTest.java index fb4057f8..650fd2a9 100644 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4ConstantTimeTest.java +++ b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4ConstantTimeTest.java @@ -20,7 +20,6 @@ package com.tencent.kona.crypto.perf; import com.tencent.kona.crypto.TestUtils; -import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; @@ -37,7 +36,6 @@ import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; -import java.security.Security; import java.util.concurrent.TimeUnit; import static com.tencent.kona.crypto.CryptoUtils.toBytes; @@ -63,13 +61,12 @@ public class SM4ConstantTimeTest { static { TestUtils.addProviders(); - Security.addProvider(new BouncyCastleProvider()); } @State(Scope.Benchmark) public static class CipherHolder { - @Param({"KonaCrypto", "KonaCrypto-Native", "BC"}) + @Param({"KonaCrypto", "KonaCrypto-Native"}) String provider; @Param({"Small", "Mid", "Big"}) @@ -99,16 +96,17 @@ public void setup() throws Exception { cipher = Cipher.getInstance("SM4/ECB/NoPadding", provider); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "SM4")); - switch (dataType) { - case "Small": - data = MESG_SMALL; - break; - case "Mid": - data = MESG_MID; - break; - case "Big": - data = MESG_BIG; - } + data = data(dataType); + } + } + + private static byte[] data(String dataType) { + switch (dataType) { + case "Small": return MESG_SMALL; + case "Mid": return MESG_MID; + case "Big": return MESG_BIG; + default: throw new IllegalArgumentException( + "Unsupported data type: " + dataType); } } diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4DecrypterPerfTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4DecrypterPerfTest.java index 18bb30b5..5b9e897a 100644 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4DecrypterPerfTest.java +++ b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4DecrypterPerfTest.java @@ -21,19 +21,7 @@ import com.tencent.kona.crypto.TestUtils; import com.tencent.kona.crypto.util.Constants; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.annotations.*; import javax.crypto.Cipher; import javax.crypto.SecretKey; @@ -41,7 +29,6 @@ import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; -import java.security.Security; import java.util.concurrent.TimeUnit; import static com.tencent.kona.crypto.CryptoUtils.toBytes; @@ -50,7 +37,7 @@ * The JMH-based performance test for SM4 cipher. */ @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) @BenchmarkMode(Mode.Throughput) @@ -66,86 +53,24 @@ public class SM4DecrypterPerfTest { private static final GCMParameterSpec GCM_PARAM_SPEC = new GCMParameterSpec(Constants.SM4_GCM_TAG_LEN * 8, GCM_IV); - private final static byte[] MESSAGE = TestUtils.dataMB(1); + private final static byte[] SMALL_DATA = TestUtils.data(128); + private final static byte[] MEDIUM_DATA = TestUtils.dataKB(1); + private final static byte[] BIG_DATA = TestUtils.dataMB(1); static { TestUtils.addProviders(); - Security.addProvider(new BouncyCastleProvider()); } @State(Scope.Benchmark) public static class DecrypterHolder { - byte[] ciphertextCBCPadding; - byte[] ciphertextCBCNoPadding; - byte[] ciphertextCTRNoPadding; - byte[] ciphertextECBNoPadding; - byte[] ciphertextGCMNoPadding; + @Param({"KonaCrypto", "KonaCrypto-Native"}) + String provider; - Cipher decrypterCBCPadding; - Cipher decrypterCBCNoPadding; - Cipher decrypterECBNoPadding; - Cipher decrypterCTRNoPadding; - Cipher decrypterGCMNoPadding; + @Param({"Small", "Mid", "Big"}) + String dataType; - @Setup(Level.Invocation) - public void setup() throws Exception { - setupCiphertexts(); - setupDecrypters(); - } - - private void setupCiphertexts() throws Exception { - Cipher cipher = Cipher.getInstance("SM4/CBC/PKCS7Padding", "KonaCrypto"); - cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - ciphertextCBCPadding = cipher.doFinal(MESSAGE); - - cipher = Cipher.getInstance("SM4/CBC/NoPadding", "KonaCrypto"); - cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - ciphertextCBCNoPadding = cipher.doFinal(MESSAGE); - - cipher = Cipher.getInstance("SM4/ECB/NoPadding", "KonaCrypto"); - cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY); - ciphertextECBNoPadding = cipher.doFinal(MESSAGE); - - cipher = Cipher.getInstance("SM4/CTR/NoPadding", "KonaCrypto"); - cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - ciphertextCTRNoPadding = cipher.doFinal(MESSAGE); - - cipher = Cipher.getInstance("SM4/GCM/NoPadding", "KonaCrypto"); - cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, GCM_PARAM_SPEC); - ciphertextGCMNoPadding = cipher.doFinal(MESSAGE); - } - - private void setupDecrypters() throws Exception { - decrypterCBCPadding = Cipher.getInstance( - "SM4/CBC/PKCS7Padding", "KonaCrypto"); - decrypterCBCPadding.init( - Cipher.DECRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - - decrypterCBCNoPadding = Cipher.getInstance( - "SM4/CBC/NoPadding", "KonaCrypto"); - decrypterCBCNoPadding.init( - Cipher.DECRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - - decrypterECBNoPadding = Cipher.getInstance( - "SM4/ECB/NoPadding", "KonaCrypto"); - decrypterECBNoPadding.init( - Cipher.DECRYPT_MODE, SECRET_KEY); - - decrypterCTRNoPadding = Cipher.getInstance( - "SM4/CTR/NoPadding", "KonaCrypto"); - decrypterCTRNoPadding.init( - Cipher.DECRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - - decrypterGCMNoPadding = Cipher.getInstance( - "SM4/GCM/NoPadding", "KonaCrypto"); - decrypterGCMNoPadding.init( - Cipher.DECRYPT_MODE, SECRET_KEY, GCM_PARAM_SPEC); - } - } - - @State(Scope.Benchmark) - public static class DecrypterHolderNative { + byte[] data; byte[] ciphertextCBCPadding; byte[] ciphertextCBCNoPadding; @@ -166,123 +91,65 @@ public void setup() throws Exception { } private void setupCiphertexts() throws Exception { - Cipher cipher = Cipher.getInstance("SM4/CBC/PKCS7Padding", "KonaCrypto-Native"); + data = data(dataType); + + Cipher cipher = Cipher.getInstance( + "SM4/CBC/PKCS7Padding", provider); cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - ciphertextCBCPadding = cipher.doFinal(MESSAGE); + ciphertextCBCPadding = cipher.doFinal(data); - cipher = Cipher.getInstance("SM4/CBC/NoPadding", "KonaCrypto-Native"); + cipher = Cipher.getInstance("SM4/CBC/NoPadding", provider); cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - ciphertextCBCNoPadding = cipher.doFinal(MESSAGE); + ciphertextCBCNoPadding = cipher.doFinal(data); - cipher = Cipher.getInstance("SM4/ECB/NoPadding", "KonaCrypto-Native"); + cipher = Cipher.getInstance("SM4/ECB/NoPadding", provider); cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY); - ciphertextECBNoPadding = cipher.doFinal(MESSAGE); + ciphertextECBNoPadding = cipher.doFinal(data); - cipher = Cipher.getInstance("SM4/CTR/NoPadding", "KonaCrypto-Native"); + cipher = Cipher.getInstance("SM4/CTR/NoPadding", provider); cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - ciphertextCTRNoPadding = cipher.doFinal(MESSAGE); + ciphertextCTRNoPadding = cipher.doFinal(data); - cipher = Cipher.getInstance("SM4/GCM/NoPadding", "KonaCrypto-Native"); + cipher = Cipher.getInstance("SM4/GCM/NoPadding", provider); cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, GCM_PARAM_SPEC); - ciphertextGCMNoPadding = cipher.doFinal(MESSAGE); + ciphertextGCMNoPadding = cipher.doFinal(data); } private void setupDecrypters() throws Exception { decrypterCBCPadding = Cipher.getInstance( - "SM4/CBC/PKCS7Padding", "KonaCrypto-Native"); + "SM4/CBC/PKCS7Padding", provider); decrypterCBCPadding.init( Cipher.DECRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); decrypterCBCNoPadding = Cipher.getInstance( - "SM4/CBC/NoPadding", "KonaCrypto-Native"); + "SM4/CBC/NoPadding", provider); decrypterCBCNoPadding.init( Cipher.DECRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); decrypterECBNoPadding = Cipher.getInstance( - "SM4/ECB/NoPadding", "KonaCrypto-Native"); + "SM4/ECB/NoPadding", provider); decrypterECBNoPadding.init( Cipher.DECRYPT_MODE, SECRET_KEY); decrypterCTRNoPadding = Cipher.getInstance( - "SM4/CTR/NoPadding", "KonaCrypto-Native"); + "SM4/CTR/NoPadding", provider); decrypterCTRNoPadding.init( Cipher.DECRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); decrypterGCMNoPadding = Cipher.getInstance( - "SM4/GCM/NoPadding", "KonaCrypto-Native"); + "SM4/GCM/NoPadding", provider); decrypterGCMNoPadding.init( Cipher.DECRYPT_MODE, SECRET_KEY, GCM_PARAM_SPEC); } } - @State(Scope.Benchmark) - public static class DecrypterHolderBC { - - byte[] ciphertextCBCPadding; - byte[] ciphertextCBCNoPadding; - byte[] ciphertextECBNoPadding; - byte[] ciphertextCTRNoPadding; - byte[] ciphertextGCMNoPadding; - - Cipher decrypterCBCPadding; - Cipher decrypterCBCNoPadding; - Cipher decrypterECBNoPadding; - Cipher decrypterCTRNoPadding; - Cipher decrypterGCMNoPadding; - - @Setup(Level.Invocation) - public void setup() throws Exception { - setupCiphertexts(); - setupDecrypters(); - } - - private void setupCiphertexts() throws Exception { - Cipher cipher = Cipher.getInstance("SM4/CBC/PKCS7Padding", "BC"); - cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - ciphertextCBCPadding = cipher.doFinal(MESSAGE); - - cipher = Cipher.getInstance("SM4/CBC/NoPadding", "BC"); - cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - ciphertextCBCNoPadding = cipher.doFinal(MESSAGE); - - cipher = Cipher.getInstance("SM4/ECB/NoPadding", "BC"); - cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY); - ciphertextECBNoPadding = cipher.doFinal(MESSAGE); - - cipher = Cipher.getInstance("SM4/CTR/NoPadding", "BC"); - cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - ciphertextCTRNoPadding = cipher.doFinal(MESSAGE); - - cipher = Cipher.getInstance("SM4/GCM/NoPadding", "BC"); - cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, GCM_PARAM_SPEC); - ciphertextGCMNoPadding = cipher.doFinal(MESSAGE); - } - - private void setupDecrypters() throws Exception { - decrypterCBCPadding = Cipher.getInstance( - "SM4/CBC/PKCS7Padding", "BC"); - decrypterCBCPadding.init( - Cipher.DECRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - - decrypterCBCNoPadding = Cipher.getInstance( - "SM4/CBC/NoPadding", "BC"); - decrypterCBCNoPadding.init( - Cipher.DECRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - - decrypterECBNoPadding = Cipher.getInstance( - "SM4/ECB/NoPadding", "BC"); - decrypterECBNoPadding.init( - Cipher.DECRYPT_MODE, SECRET_KEY); - - decrypterCTRNoPadding = Cipher.getInstance( - "SM4/CTR/NoPadding", "BC"); - decrypterCTRNoPadding.init( - Cipher.DECRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - - decrypterGCMNoPadding = Cipher.getInstance( - "SM4/GCM/NoPadding", "BC"); - decrypterGCMNoPadding.init( - Cipher.DECRYPT_MODE, SECRET_KEY, GCM_PARAM_SPEC); + private static byte[] data(String dataType) { + switch (dataType) { + case "Small": return SMALL_DATA; + case "Mid": return MEDIUM_DATA; + case "Big": return BIG_DATA; + default: throw new IllegalArgumentException( + "Unsupported data type: " + dataType); } } @@ -291,73 +158,23 @@ public byte[] cbcPadding(DecrypterHolder holder) throws Exception { return holder.decrypterCBCPadding.doFinal(holder.ciphertextCBCPadding); } - @Benchmark - public byte[] cbcPaddingNative(DecrypterHolderNative holder) throws Exception { - return holder.decrypterCBCPadding.doFinal(holder.ciphertextCBCPadding); - } - - @Benchmark - public byte[] cbcPaddingBC(DecrypterHolderBC holder) throws Exception { - return holder.decrypterCBCPadding.doFinal(holder.ciphertextCBCPadding); - } - @Benchmark public byte[] cbcNoPadding(DecrypterHolder holder) throws Exception { return holder.decrypterCBCNoPadding.doFinal(holder.ciphertextCBCNoPadding); } - @Benchmark - public byte[] cbcNoPaddingNative(DecrypterHolderNative holder) throws Exception { - return holder.decrypterCBCNoPadding.doFinal(holder.ciphertextCBCNoPadding); - } - - @Benchmark - public byte[] cbcNoPaddingBC(DecrypterHolderBC holder) throws Exception { - return holder.decrypterCBCNoPadding.doFinal(holder.ciphertextCBCNoPadding); - } - @Benchmark public byte[] ecb(DecrypterHolder holder) throws Exception { return holder.decrypterECBNoPadding.doFinal(holder.ciphertextECBNoPadding); } - @Benchmark - public byte[] ecbNative(DecrypterHolderNative holder) throws Exception { - return holder.decrypterECBNoPadding.doFinal(holder.ciphertextECBNoPadding); - } - - @Benchmark - public byte[] ecbBC(DecrypterHolderBC holder) throws Exception { - return holder.decrypterECBNoPadding.doFinal(holder.ciphertextECBNoPadding); - } - @Benchmark public byte[] ctr(DecrypterHolder holder) throws Exception { return holder.decrypterCTRNoPadding.doFinal(holder.ciphertextCTRNoPadding); } - @Benchmark - public byte[] ctrNative(DecrypterHolderNative holder) throws Exception { - return holder.decrypterCTRNoPadding.doFinal(holder.ciphertextCTRNoPadding); - } - - @Benchmark - public byte[] ctrBC(DecrypterHolderBC holder) throws Exception { - return holder.decrypterCTRNoPadding.doFinal(holder.ciphertextCTRNoPadding); - } - @Benchmark public byte[] gcm(DecrypterHolder holder) throws Exception { return holder.decrypterGCMNoPadding.doFinal(holder.ciphertextGCMNoPadding); } - - @Benchmark - public byte[] gcmNative(DecrypterHolderNative holder) throws Exception { - return holder.decrypterGCMNoPadding.doFinal(holder.ciphertextGCMNoPadding); - } - - @Benchmark - public byte[] gcmBC(DecrypterHolderBC holder) throws Exception { - return holder.decrypterGCMNoPadding.doFinal(holder.ciphertextGCMNoPadding); - } } diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4EncrypterPerfTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4EncrypterPerfTest.java index 64b7bdf1..a758edfa 100644 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4EncrypterPerfTest.java +++ b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4EncrypterPerfTest.java @@ -21,19 +21,7 @@ import com.tencent.kona.crypto.TestUtils; import com.tencent.kona.crypto.util.Constants; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.annotations.*; import javax.crypto.Cipher; import javax.crypto.SecretKey; @@ -41,7 +29,6 @@ import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; -import java.security.Security; import java.util.concurrent.TimeUnit; import static com.tencent.kona.crypto.CryptoUtils.toBytes; @@ -50,7 +37,7 @@ * The JMH-based performance test for SM4 encryption. */ @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) @BenchmarkMode(Mode.Throughput) @@ -66,52 +53,25 @@ public class SM4EncrypterPerfTest { private static final GCMParameterSpec GCM_PARAM_SPEC = new GCMParameterSpec(Constants.SM4_GCM_TAG_LEN * 8, GCM_IV); - private final static byte[] DATA = TestUtils.dataMB(1); + private final static byte[] SMALL_DATA = TestUtils.data(128); + private final static byte[] MEDIUM_DATA = TestUtils.dataKB(1); + private final static byte[] BIG_DATA = TestUtils.dataMB(1); static { TestUtils.addProviders(); - Security.addProvider(new BouncyCastleProvider()); } @State(Scope.Benchmark) public static class EncrypterHolder { - Cipher encrypterCBCPadding; - Cipher encrypterCBCNoPadding; - Cipher encrypterECBNoPadding; - Cipher encrypterCTRNoPadding; - Cipher encrypterGCMNoPadding; - - @Setup(Level.Invocation) - public void setup() throws Exception { - encrypterCBCPadding = Cipher.getInstance( - "SM4/CBC/PKCS7Padding", "KonaCrypto"); - encrypterCBCPadding.init( - Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - encrypterCBCNoPadding = Cipher.getInstance( - "SM4/CBC/NoPadding", "KonaCrypto"); - encrypterCBCNoPadding.init( - Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); + @Param({"KonaCrypto", "KonaCrypto-Native"}) + String provider; - encrypterECBNoPadding = Cipher.getInstance( - "SM4/ECB/NoPadding", "KonaCrypto"); - encrypterECBNoPadding.init( - Cipher.ENCRYPT_MODE, SECRET_KEY); + @Param({"Small", "Mid", "Big"}) + String dataType; - encrypterCTRNoPadding = Cipher.getInstance( - "SM4/CTR/NoPadding", "KonaCrypto"); - encrypterCTRNoPadding.init( - Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); + byte[] data; - encrypterGCMNoPadding = Cipher.getInstance( - "SM4/GCM/NoPadding", "KonaCrypto"); - encrypterGCMNoPadding.init( - Cipher.ENCRYPT_MODE, SECRET_KEY, GCM_PARAM_SPEC); - } - } - - @State(Scope.Benchmark) - public static class EncrypterHolderNative { Cipher encrypterCBCPadding; Cipher encrypterCBCNoPadding; Cipher encrypterECBNoPadding; @@ -120,142 +80,67 @@ public static class EncrypterHolderNative { @Setup(Level.Invocation) public void setup() throws Exception { + data = data(dataType); + encrypterCBCPadding = Cipher.getInstance( - "SM4/CBC/PKCS7Padding", "KonaCrypto-Native"); + "SM4/CBC/PKCS7Padding", provider); encrypterCBCPadding.init( Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); encrypterCBCNoPadding = Cipher.getInstance( - "SM4/CBC/NoPadding", "KonaCrypto-Native"); + "SM4/CBC/NoPadding", provider); encrypterCBCNoPadding.init( Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); encrypterECBNoPadding = Cipher.getInstance( - "SM4/ECB/NoPadding", "KonaCrypto-Native"); + "SM4/ECB/NoPadding", provider); encrypterECBNoPadding.init( Cipher.ENCRYPT_MODE, SECRET_KEY); encrypterCTRNoPadding = Cipher.getInstance( - "SM4/CTR/NoPadding", "KonaCrypto-Native"); + "SM4/CTR/NoPadding", provider); encrypterCTRNoPadding.init( Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); encrypterGCMNoPadding = Cipher.getInstance( - "SM4/GCM/NoPadding", "KonaCrypto-Native"); + "SM4/GCM/NoPadding", provider); encrypterGCMNoPadding.init( Cipher.ENCRYPT_MODE, SECRET_KEY, GCM_PARAM_SPEC); } } - @State(Scope.Benchmark) - public static class EncrypterHolderBC { - Cipher encrypterCBCPadding; - Cipher encrypterCBCNoPadding; - Cipher encrypterECBNoPadding; - Cipher encrypterCTRNoPadding; - Cipher encrypterGCMNoPadding; - - @Setup(Level.Invocation) - public void setup() throws Exception { - encrypterCBCPadding = Cipher.getInstance( - "SM4/CBC/PKCS7Padding", "BC"); - encrypterCBCPadding.init( - Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - - encrypterCBCNoPadding = Cipher.getInstance( - "SM4/CBC/NoPadding", "BC"); - encrypterCBCNoPadding.init( - Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - - encrypterECBNoPadding = Cipher.getInstance( - "SM4/ECB/NoPadding", "BC"); - encrypterECBNoPadding.init( - Cipher.ENCRYPT_MODE, SECRET_KEY); - - encrypterCTRNoPadding = Cipher.getInstance( - "SM4/CTR/NoPadding", "BC"); - encrypterCTRNoPadding.init( - Cipher.ENCRYPT_MODE, SECRET_KEY, IV_PARAM_SPEC); - - encrypterGCMNoPadding = Cipher.getInstance( - "SM4/GCM/NoPadding", "BC"); - encrypterGCMNoPadding.init( - Cipher.ENCRYPT_MODE, SECRET_KEY, GCM_PARAM_SPEC); + private static byte[] data(String dataType) { + switch (dataType) { + case "Small": return SMALL_DATA; + case "Mid": return MEDIUM_DATA; + case "Big": return BIG_DATA; + default: throw new IllegalArgumentException( + "Unsupported data type: " + dataType); } } @Benchmark public byte[] cbcPadding(EncrypterHolder holder) throws Exception { - return holder.encrypterCBCPadding.doFinal(DATA); - } - - @Benchmark - public byte[] cbcPaddingNative(EncrypterHolderNative holder) throws Exception { - return holder.encrypterCBCPadding.doFinal(DATA); - } - - @Benchmark - public byte[] cbcPaddingBC(EncrypterHolderBC holder) throws Exception { - return holder.encrypterCBCPadding.doFinal(DATA); + return holder.encrypterCBCPadding.doFinal(holder.data); } @Benchmark public byte[] cbcNoPadding(EncrypterHolder holder) throws Exception { - return holder.encrypterCBCNoPadding.doFinal(DATA); - } - - @Benchmark - public byte[] cbcNoPaddingNative(EncrypterHolderNative holder) throws Exception { - return holder.encrypterCBCNoPadding.doFinal(DATA); - } - - @Benchmark - public byte[] cbcNoPaddingBC(EncrypterHolderBC holder) throws Exception { - return holder.encrypterCBCNoPadding.doFinal(DATA); + return holder.encrypterCBCNoPadding.doFinal(holder.data); } @Benchmark public byte[] ctr(EncrypterHolder holder) throws Exception { - return holder.encrypterCTRNoPadding.doFinal(DATA); - } - - @Benchmark - public byte[] ctrNative(EncrypterHolderNative holder) throws Exception { - return holder.encrypterCTRNoPadding.doFinal(DATA); - } - - @Benchmark - public byte[] ctrBC(EncrypterHolderBC holder) throws Exception { - return holder.encrypterCTRNoPadding.doFinal(DATA); + return holder.encrypterCTRNoPadding.doFinal(holder.data); } @Benchmark public byte[] ecb(EncrypterHolder holder) throws Exception { - return holder.encrypterECBNoPadding.doFinal(DATA); - } - - @Benchmark - public byte[] ecbNative(EncrypterHolderNative holder) throws Exception { - return holder.encrypterECBNoPadding.doFinal(DATA); - } - - @Benchmark - public byte[] ecbBC(EncrypterHolderBC holder) throws Exception { - return holder.encrypterECBNoPadding.doFinal(DATA); + return holder.encrypterECBNoPadding.doFinal(holder.data); } @Benchmark public byte[] gcm(EncrypterHolder holder) throws Exception { - return holder.encrypterGCMNoPadding.doFinal(DATA); - } - - @Benchmark - public byte[] gcmNative(EncrypterHolderNative holder) throws Exception { - return holder.encrypterGCMNoPadding.doFinal(DATA); - } - - @Benchmark - public byte[] gcmBC(EncrypterHolderBC holder) throws Exception { - return holder.encrypterGCMNoPadding.doFinal(DATA); + return holder.encrypterGCMNoPadding.doFinal(holder.data); } } diff --git a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4EnginePerfTest.java b/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4EnginePerfTest.java deleted file mode 100644 index 319bf986..00000000 --- a/kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM4EnginePerfTest.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -package com.tencent.kona.crypto.perf; - -import com.tencent.kona.crypto.provider.SM4Engine; -import com.tencent.kona.crypto.provider.nativeImpl.NativeSM4Engine; -import org.bouncycastle.crypto.params.KeyParameter; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; - -import java.util.concurrent.TimeUnit; - -/** - * The JMH-based performance test for SM4 engine. - */ -@Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) -@Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) -@Threads(1) -@BenchmarkMode(Mode.Throughput) -@OutputTimeUnit(TimeUnit.SECONDS) -public class SM4EnginePerfTest { - - private static final byte[] KEY = { - (byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67, - (byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef, - (byte)0xfe, (byte)0xdc, (byte)0xba, (byte)0x98, - (byte)0x76, (byte)0x54, (byte)0x32, (byte)0x10 - }; - - private static final byte[] DATA = { - (byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67, - (byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef, - (byte)0xfe, (byte)0xdc, (byte)0xba, (byte)0x98, - (byte)0x76, (byte)0x54, (byte)0x32, (byte)0x10 - }; - - @State(Scope.Benchmark) - public static class EngineHolder { - - SM4Engine engine; - - @Setup(Level.Invocation) - public void setup() { - engine = new SM4Engine(KEY, true); - } - } - - @State(Scope.Benchmark) - public static class EngineHolderNative { - - NativeSM4Engine engine; - - @Setup(Level.Invocation) - public void setup() { - engine = new NativeSM4Engine(KEY, true); - } - } - - @State(Scope.Benchmark) - public static class EngineHolderBC { - - org.bouncycastle.crypto.engines.SM4Engine engine; - - @Setup(Level.Invocation) - public void setup() { - engine = new org.bouncycastle.crypto.engines.SM4Engine(); - engine.init(true, new KeyParameter(KEY)); - } - } - - @Benchmark - public byte[] processBlock(EngineHolder holder) { - byte[] ciphertext = new byte[16]; - holder.engine.processBlock(DATA, 0, ciphertext, 0); - for (int i = 1; i < 10000; i++) { - holder.engine.processBlock(ciphertext, 0, ciphertext, 0); - } - return ciphertext; - } - - @Benchmark - public byte[] processBlockNative(EngineHolderNative holder) { - byte[] ciphertext = new byte[16]; - holder.engine.processBlock(DATA, 0, ciphertext, 0); - for (int i = 1; i < 10000; i++) { - holder.engine.processBlock(ciphertext, 0, ciphertext, 0); - } - return ciphertext; - } - - @Benchmark - public byte[] processBlockBC(EngineHolderBC holder) { - byte[] ciphertext = new byte[16]; - holder.engine.processBlock(DATA, 0, ciphertext, 0); - for (int i = 1; i < 10000; i++) { - holder.engine.processBlock(ciphertext, 0, ciphertext, 0); - } - return ciphertext; - } -} diff --git a/kona-pkix/src/jmh/java/com/tencent/kona/pkix/perf/KeyStorePerfTest.java b/kona-pkix/src/jmh/java/com/tencent/kona/pkix/perf/KeyStorePerfTest.java index 6b337bef..8f695056 100644 --- a/kona-pkix/src/jmh/java/com/tencent/kona/pkix/perf/KeyStorePerfTest.java +++ b/kona-pkix/src/jmh/java/com/tencent/kona/pkix/perf/KeyStorePerfTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023, THL A29 Limited, a Tencent company. All rights reserved. + * Copyright (C) 2023, 2024, THL A29 Limited, a Tencent company. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -50,7 +50,7 @@ * The JMH-based performance test for keystore operations. */ @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) @BenchmarkMode(Mode.Throughput) diff --git a/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/KonaSSLTlcpHandshakePerfTest.java b/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/KonaSSLTlcpHandshakePerfTest.java index 8d6af01c..a602bbcd 100644 --- a/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/KonaSSLTlcpHandshakePerfTest.java +++ b/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/KonaSSLTlcpHandshakePerfTest.java @@ -53,7 +53,7 @@ import static com.tencent.kona.ssl.tlcp.TlcpUtils.*; @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) @BenchmarkMode(Mode.Throughput) diff --git a/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/KonaSSLTlsHandshakePerfTest.java b/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/KonaSSLTlsHandshakePerfTest.java index 91b48580..d850705f 100644 --- a/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/KonaSSLTlsHandshakePerfTest.java +++ b/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/KonaSSLTlsHandshakePerfTest.java @@ -57,7 +57,7 @@ import java.util.concurrent.TimeUnit; @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @BenchmarkMode(Mode.Throughput) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1) diff --git a/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/SunJSSETlsHandshakePerfTest.java b/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/SunJSSETlsHandshakePerfTest.java index 9c5824a4..478659d7 100644 --- a/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/SunJSSETlsHandshakePerfTest.java +++ b/kona-ssl/src/jmh/java/com/tencent/kona/ssl/perf/SunJSSETlsHandshakePerfTest.java @@ -56,7 +56,7 @@ import java.util.concurrent.TimeUnit; @Warmup(iterations = 5, time = 5) -@Measurement(iterations = 5, time = 10) +@Measurement(iterations = 5, time = 5) @BenchmarkMode(Mode.Throughput) @Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"}) @Threads(1)