Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TKSS-1016: NativeSM2KeyAgreement should be created in constructor #1017

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.openjdk.jmh.annotations.*;

import javax.crypto.KeyAgreement;
import java.security.InvalidKeyException;
import java.util.concurrent.TimeUnit;

import static com.tencent.kona.crypto.CryptoUtils.toBytes;
Expand Down Expand Up @@ -72,11 +71,12 @@ public static class KeyAgreementHolder {
@Param({"KonaCrypto", "KonaCrypto-Native"})
String provider;

SM2KeyAgreementParamSpec paramSpec;
KeyAgreement keyAgreement;

@Setup(Level.Invocation)
@Setup(Level.Trial)
public void setup() throws Exception {
SM2KeyAgreementParamSpec paramSpec = new SM2KeyAgreementParamSpec(
paramSpec = new SM2KeyAgreementParamSpec(
toBytes(ID),
new SM2PrivateKey(toBytes(PRI_KEY)),
new SM2PublicKey(toBytes(PUB_KEY)),
Expand All @@ -85,13 +85,13 @@ public void setup() throws Exception {
true,
16);
keyAgreement = KeyAgreement.getInstance("SM2", provider);
keyAgreement.init(
new SM2PrivateKey(toBytes(TMP_PRI_KEY)), paramSpec);
}
}

@Benchmark
public byte[] generateSecret(KeyAgreementHolder holder) throws InvalidKeyException {
public byte[] generateSecret(KeyAgreementHolder holder) throws Exception {
holder.keyAgreement.init(
new SM2PrivateKey(toBytes(TMP_PRI_KEY)), holder.paramSpec);
holder.keyAgreement.doPhase(new SM2PublicKey(toBytes(PEER_TMP_PUB_KEY)), true);
return holder.keyAgreement.generateSecret();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public final class SM2KeyAgreement extends KeyAgreementSpi {
private SM2KeyAgreementParamSpec paramSpec;
private SM2PublicKey peerEphemeralPublicKey;

private NativeSM2KeyAgreement sm2;
private final NativeSM2KeyAgreement sm2;

public SM2KeyAgreement() {
sm2 = new NativeSM2KeyAgreement();
SWEEPER.register(this, new SweepNativeRef(sm2));
}

@Override
protected void engineInit(Key key, SecureRandom random) {
Expand Down Expand Up @@ -85,10 +90,6 @@ protected void engineInit(Key key, AlgorithmParameterSpec params,
ephemeralPrivateKey = new SM2PrivateKey((ECPrivateKey) key);
paramSpec = (SM2KeyAgreementParamSpec) params;
peerEphemeralPublicKey = null;

sm2 = new NativeSM2KeyAgreement();

SWEEPER.register(this, new SweepNativeRef(sm2));
}

@Override
Expand Down
Loading