Skip to content

Commit ee34bb5

Browse files
committed
TKSS-993: Run tests on the native crypto provider
1 parent efd5c10 commit ee34bb5

36 files changed

+385
-104
lines changed

kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2CipherConstantTimeTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved.
2+
* Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -82,7 +82,7 @@ private static KeyPair keyPair(BigInteger priKeyValue) {
8282
@State(Scope.Thread)
8383
public static class CipherHolder {
8484

85-
@Param({"KonaCrypto", "BC"})
85+
@Param({"KonaCrypto", "KonaCrypto-Native", "BC"})
8686
String provider;
8787

8888
@Param({"Small", "Mid", "Big"})

kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2CipherPerfTest.java

+46-17
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,13 @@
2121

2222
import com.tencent.kona.crypto.TestUtils;
2323
import org.bouncycastle.jce.provider.BouncyCastleProvider;
24-
import org.openjdk.jmh.annotations.Benchmark;
25-
import org.openjdk.jmh.annotations.BenchmarkMode;
26-
import org.openjdk.jmh.annotations.Fork;
27-
import org.openjdk.jmh.annotations.Level;
28-
import org.openjdk.jmh.annotations.Measurement;
29-
import org.openjdk.jmh.annotations.Mode;
30-
import org.openjdk.jmh.annotations.OutputTimeUnit;
31-
import org.openjdk.jmh.annotations.Scope;
32-
import org.openjdk.jmh.annotations.Setup;
33-
import org.openjdk.jmh.annotations.State;
34-
import org.openjdk.jmh.annotations.Threads;
35-
import org.openjdk.jmh.annotations.Warmup;
24+
import org.openjdk.jmh.annotations.*;
3625

3726
import javax.crypto.Cipher;
3827
import java.security.KeyPair;
3928
import java.security.Security;
4029
import java.util.concurrent.TimeUnit;
4130

42-
import static com.tencent.kona.crypto.TestUtils.PROVIDER;
43-
4431
/**
4532
* The JMH-based performance test for SM2 decryption.
4633
*/
@@ -71,7 +58,19 @@ public static class EncrypterHolder {
7158

7259
@Setup(Level.Trial)
7360
public void setup() throws Exception {
74-
encrypter = Cipher.getInstance("SM2", PROVIDER);
61+
encrypter = Cipher.getInstance("SM2", "KonaCrypto");
62+
encrypter.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
63+
}
64+
}
65+
66+
@State(Scope.Benchmark)
67+
public static class EncrypterHolderNative {
68+
69+
Cipher encrypter;
70+
71+
@Setup(Level.Trial)
72+
public void setup() throws Exception {
73+
encrypter = Cipher.getInstance("SM2", "KonaCrypto-Native");
7574
encrypter.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
7675
}
7776
}
@@ -97,12 +96,32 @@ public static class DecrypterHolder {
9796
@Setup(Level.Trial)
9897
public void setup() throws Exception {
9998
ciphertext = ciphertext();
100-
decrypter = Cipher.getInstance("SM2", PROVIDER);
99+
decrypter = Cipher.getInstance("SM2", "KonaCrypto");
100+
decrypter.init(Cipher.DECRYPT_MODE, KEY_PAIR.getPrivate());
101+
}
102+
103+
private byte[] ciphertext() throws Exception {
104+
Cipher cipher = Cipher.getInstance("SM2", "KonaCrypto");
105+
cipher.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
106+
return cipher.doFinal(MESSAGE);
107+
}
108+
}
109+
110+
@State(Scope.Benchmark)
111+
public static class DecrypterHolderNative {
112+
113+
byte[] ciphertext;
114+
Cipher decrypter;
115+
116+
@Setup(Level.Trial)
117+
public void setup() throws Exception {
118+
ciphertext = ciphertext();
119+
decrypter = Cipher.getInstance("SM2", "KonaCrypto-Native");
101120
decrypter.init(Cipher.DECRYPT_MODE, KEY_PAIR.getPrivate());
102121
}
103122

104123
private byte[] ciphertext() throws Exception {
105-
Cipher cipher = Cipher.getInstance("SM2", PROVIDER);
124+
Cipher cipher = Cipher.getInstance("SM2", "KonaCrypto-Native");
106125
cipher.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
107126
return cipher.doFinal(MESSAGE);
108127
}
@@ -133,6 +152,11 @@ public byte[] encrypt(EncrypterHolder holder) throws Exception {
133152
return holder.encrypter.doFinal(MESSAGE);
134153
}
135154

155+
@Benchmark
156+
public byte[] encryptNative(EncrypterHolderNative holder) throws Exception {
157+
return holder.encrypter.doFinal(MESSAGE);
158+
}
159+
136160
@Benchmark
137161
public byte[] encryptBC(EncrypterHolderBC holder) throws Exception {
138162
return holder.encrypter.doFinal(MESSAGE);
@@ -143,6 +167,11 @@ public byte[] decrypt(DecrypterHolder holder) throws Exception {
143167
return holder.decrypter.doFinal(holder.ciphertext);
144168
}
145169

170+
@Benchmark
171+
public byte[] decryptNative(DecrypterHolderNative holder) throws Exception {
172+
return holder.decrypter.doFinal(holder.ciphertext);
173+
}
174+
146175
@Benchmark
147176
public byte[] decryptBC(DecrypterHolderBC holder) throws Exception {
148177
return holder.decrypter.doFinal(holder.ciphertext);

kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2KeyAgreementPerfTest.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import java.util.concurrent.TimeUnit;
5757

5858
import static com.tencent.kona.crypto.CryptoUtils.toBytes;
59-
import static com.tencent.kona.crypto.TestUtils.PROVIDER;
6059
import static com.tencent.kona.crypto.spec.SM2ParameterSpec.COFACTOR;
6160
import static com.tencent.kona.crypto.spec.SM2ParameterSpec.CURVE;
6261
import static com.tencent.kona.crypto.spec.SM2ParameterSpec.GENERATOR;
@@ -113,7 +112,28 @@ public void setup() throws Exception {
113112
new SM2PublicKey(toBytes(PEER_PUB_KEY)),
114113
true,
115114
16);
116-
keyAgreement = KeyAgreement.getInstance("SM2", PROVIDER);
115+
keyAgreement = KeyAgreement.getInstance("SM2", "KonaCrypto");
116+
keyAgreement.init(
117+
new SM2PrivateKey(toBytes(TMP_PRI_KEY)), paramSpec);
118+
}
119+
}
120+
121+
@State(Scope.Benchmark)
122+
public static class KeyAgreementNativeHolder {
123+
124+
KeyAgreement keyAgreement;
125+
126+
@Setup(Level.Invocation)
127+
public void setup() throws Exception {
128+
SM2KeyAgreementParamSpec paramSpec = new SM2KeyAgreementParamSpec(
129+
toBytes(ID),
130+
new SM2PrivateKey(toBytes(PRI_KEY)),
131+
new SM2PublicKey(toBytes(PUB_KEY)),
132+
toBytes(PEER_ID),
133+
new SM2PublicKey(toBytes(PEER_PUB_KEY)),
134+
true,
135+
16);
136+
keyAgreement = KeyAgreement.getInstance("SM2", "KonaCrypto-Native");
117137
keyAgreement.init(
118138
new SM2PrivateKey(toBytes(TMP_PRI_KEY)), paramSpec);
119139
}
@@ -173,6 +193,12 @@ public byte[] generateSecret(KeyAgreementHolder holder) throws InvalidKeyExcepti
173193
return holder.keyAgreement.generateSecret();
174194
}
175195

196+
@Benchmark
197+
public byte[] generateSecretNative(KeyAgreementNativeHolder holder) throws InvalidKeyException {
198+
holder.keyAgreement.doPhase(new SM2PublicKey(toBytes(PEER_TMP_PUB_KEY)), true);
199+
return holder.keyAgreement.generateSecret();
200+
}
201+
176202
@Benchmark
177203
public byte[] generateSecretBC(KeyAgreementHolderBC holder) {
178204
return holder.keyAgreement.calculateKey(128, holder.params);

kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2KeyPairGenPerfTest.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved.
2+
* Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,8 +39,6 @@
3939
import java.security.spec.ECGenParameterSpec;
4040
import java.util.concurrent.TimeUnit;
4141

42-
import static com.tencent.kona.crypto.TestUtils.PROVIDER;
43-
4442
/**
4543
* The JMH-based performance test for SM2 key pair generation.
4644
*/
@@ -64,7 +62,18 @@ public static class KeyPairGenHolder {
6462

6563
@Setup
6664
public void setup() throws Exception {
67-
keyPairGenerator = KeyPairGenerator.getInstance("SM2", PROVIDER);
65+
keyPairGenerator = KeyPairGenerator.getInstance("SM2", "KonaCrypto");
66+
}
67+
}
68+
69+
@State(Scope.Benchmark)
70+
public static class KeyPairGenHolderNative {
71+
72+
KeyPairGenerator keyPairGenerator;
73+
74+
@Setup
75+
public void setup() throws Exception {
76+
keyPairGenerator = KeyPairGenerator.getInstance("SM2", "KonaCrypto-Native");
6877
}
6978
}
7079

@@ -85,6 +94,11 @@ public KeyPair genKeyPair(KeyPairGenHolder holder) {
8594
return holder.keyPairGenerator.generateKeyPair();
8695
}
8796

97+
@Benchmark
98+
public KeyPair genKeyPairNative(KeyPairGenHolderNative holder) {
99+
return holder.keyPairGenerator.generateKeyPair();
100+
}
101+
88102
@Benchmark
89103
public KeyPair genKeyPairBC(KeyPairGenHolderBC holder) {
90104
return holder.keyPairGenerator.generateKeyPair();

kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2SignatureConstantTimeTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved.
2+
* Copyright (C) 2022, 2024, THL A29 Limited, a Tencent company. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -89,7 +89,7 @@ private static KeyPair keyPair(BigInteger priKeyValue) {
8989
@State(Scope.Thread)
9090
public static class SignerHolder {
9191

92-
@Param({"KonaCrypto", "BC"})
92+
@Param({"KonaCrypto", "KonaCrypto-Native", "BC"})
9393
String provider;
9494

9595
@Param({"Small", "Mid", "Big"})

kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM2SignaturePerfTest.java

+55-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.security.interfaces.ECPublicKey;
4444
import java.util.concurrent.TimeUnit;
4545

46-
import static com.tencent.kona.crypto.TestUtils.PROVIDER;
4746
import static com.tencent.kona.crypto.CryptoUtils.toBytes;
4847

4948
/**
@@ -77,7 +76,21 @@ public static class SignerHolder {
7776

7877
@Setup(Level.Trial)
7978
public void setup() throws Exception {
80-
signer = Signature.getInstance("SM2", PROVIDER);
79+
signer = Signature.getInstance("SM2", "KonaCrypto");
80+
signer.setParameter(new SM2SignatureParameterSpec(
81+
ID, (ECPublicKey) KEY_PAIR.getPublic()));
82+
signer.initSign(KEY_PAIR.getPrivate());
83+
}
84+
}
85+
86+
@State(Scope.Benchmark)
87+
public static class SignerHolderNative {
88+
89+
Signature signer;
90+
91+
@Setup(Level.Trial)
92+
public void setup() throws Exception {
93+
signer = Signature.getInstance("SM2", "KonaCrypto-Native");
8194
signer.setParameter(new SM2SignatureParameterSpec(
8295
ID, (ECPublicKey) KEY_PAIR.getPublic()));
8396
signer.initSign(KEY_PAIR.getPrivate());
@@ -108,14 +121,40 @@ public static class VerifierHolder {
108121
public void setup() throws Exception {
109122
signature = signature();
110123

111-
verifier = Signature.getInstance("SM2", PROVIDER);
124+
verifier = Signature.getInstance("SM2", "KonaCrypto");
112125
verifier.setParameter(new SM2SignatureParameterSpec(
113126
ID, (ECPublicKey) KEY_PAIR.getPublic()));
114127
verifier.initVerify(KEY_PAIR.getPublic());
115128
}
116129

117130
private byte[] signature() throws Exception {
118-
Signature signer = Signature.getInstance("SM2", PROVIDER);
131+
Signature signer = Signature.getInstance("SM2", "KonaCrypto");
132+
signer.setParameter(new SM2SignatureParameterSpec(
133+
ID, (ECPublicKey) KEY_PAIR.getPublic()));
134+
signer.initSign(KEY_PAIR.getPrivate());
135+
signer.update(MESSAGE);
136+
return signer.sign();
137+
}
138+
}
139+
140+
@State(Scope.Benchmark)
141+
public static class VerifierHolderNative {
142+
143+
byte[] signature;
144+
Signature verifier;
145+
146+
@Setup(Level.Trial)
147+
public void setup() throws Exception {
148+
signature = signature();
149+
150+
verifier = Signature.getInstance("SM2", "KonaCrypto-Native");
151+
verifier.setParameter(new SM2SignatureParameterSpec(
152+
ID, (ECPublicKey) KEY_PAIR.getPublic()));
153+
verifier.initVerify(KEY_PAIR.getPublic());
154+
}
155+
156+
private byte[] signature() throws Exception {
157+
Signature signer = Signature.getInstance("SM2", "KonaCrypto-Native");
119158
signer.setParameter(new SM2SignatureParameterSpec(
120159
ID, (ECPublicKey) KEY_PAIR.getPublic()));
121160
signer.initSign(KEY_PAIR.getPrivate());
@@ -155,6 +194,12 @@ public byte[] sign(SignerHolder holder) throws Exception {
155194
return holder.signer.sign();
156195
}
157196

197+
@Benchmark
198+
public byte[] signNative(SignerHolderNative holder) throws Exception {
199+
holder.signer.update(MESSAGE);
200+
return holder.signer.sign();
201+
}
202+
158203
@Benchmark
159204
public byte[] signBC(SignerHolderBC holder) throws Exception {
160205
holder.signer.update(MESSAGE);
@@ -167,6 +212,12 @@ public boolean verify(VerifierHolder holder) throws Exception {
167212
return holder.verifier.verify(holder.signature);
168213
}
169214

215+
@Benchmark
216+
public boolean verifyNative(VerifierHolderNative holder) throws Exception {
217+
holder.verifier.update(MESSAGE);
218+
return holder.verifier.verify(holder.signature);
219+
}
220+
170221
@Benchmark
171222
public boolean verifyBC(VerifierHolderBC holder) throws Exception {
172223
holder.verifier.update(MESSAGE);

kona-crypto/src/jmh/java/com/tencent/kona/crypto/perf/SM3HMacPerfTest.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import java.security.Security;
4242
import java.util.concurrent.TimeUnit;
4343

44-
import static com.tencent.kona.crypto.TestUtils.PROVIDER;
4544
import static com.tencent.kona.crypto.CryptoUtils.toBytes;
4645

4746
/**
@@ -71,7 +70,19 @@ public static class MacHolder {
7170

7271
@Setup(Level.Trial)
7372
public void setup() throws Exception {
74-
mac = Mac.getInstance("HmacSM3", PROVIDER);
73+
mac = Mac.getInstance("HmacSM3", "KonaCrypto");
74+
mac.init(SECRET_KEY);
75+
}
76+
}
77+
78+
@State(Scope.Benchmark)
79+
public static class MacHolderNative {
80+
81+
Mac mac;
82+
83+
@Setup(Level.Trial)
84+
public void setup() throws Exception {
85+
mac = Mac.getInstance("HmacSM3", "KonaCrypto-Native");
7586
mac.init(SECRET_KEY);
7687
}
7788
}
@@ -93,6 +104,11 @@ public byte[] mac(MacHolder holder) throws Exception {
93104
return holder.mac.doFinal(MESSAGE);
94105
}
95106

107+
@Benchmark
108+
public byte[] macNative(MacHolderNative holder) throws Exception {
109+
return holder.mac.doFinal(MESSAGE);
110+
}
111+
96112
@Benchmark
97113
public byte[] macBC(MacHolderBC holder) throws Exception {
98114
return holder.mac.doFinal(MESSAGE);

0 commit comments

Comments
 (0)