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-993: Run tests on the native crypto provider #994

Merged
merged 1 commit into from
Dec 18, 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
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved.
* 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
Expand Down Expand Up @@ -82,7 +82,7 @@ private static KeyPair keyPair(BigInteger priKeyValue) {
@State(Scope.Thread)
public static class CipherHolder {

@Param({"KonaCrypto", "BC"})
@Param({"KonaCrypto", "KonaCrypto-Native", "BC"})
String provider;

@Param({"Small", "Mid", "Big"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,13 @@

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.Cipher;
import java.security.KeyPair;
import java.security.Security;
import java.util.concurrent.TimeUnit;

import static com.tencent.kona.crypto.TestUtils.PROVIDER;

/**
* The JMH-based performance test for SM2 decryption.
*/
Expand Down Expand Up @@ -71,7 +58,19 @@ public static class EncrypterHolder {

@Setup(Level.Trial)
public void setup() throws Exception {
encrypter = Cipher.getInstance("SM2", PROVIDER);
encrypter = Cipher.getInstance("SM2", "KonaCrypto");
encrypter.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
}
}

@State(Scope.Benchmark)
public static class EncrypterHolderNative {

Cipher encrypter;

@Setup(Level.Trial)
public void setup() throws Exception {
encrypter = Cipher.getInstance("SM2", "KonaCrypto-Native");
encrypter.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
}
}
Expand All @@ -97,12 +96,32 @@ public static class DecrypterHolder {
@Setup(Level.Trial)
public void setup() throws Exception {
ciphertext = ciphertext();
decrypter = Cipher.getInstance("SM2", PROVIDER);
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 {

byte[] ciphertext;
Cipher decrypter;

@Setup(Level.Trial)
public void setup() throws Exception {
ciphertext = ciphertext();
decrypter = Cipher.getInstance("SM2", "KonaCrypto-Native");
decrypter.init(Cipher.DECRYPT_MODE, KEY_PAIR.getPrivate());
}

private byte[] ciphertext() throws Exception {
Cipher cipher = Cipher.getInstance("SM2", PROVIDER);
Cipher cipher = Cipher.getInstance("SM2", "KonaCrypto-Native");
cipher.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
return cipher.doFinal(MESSAGE);
}
Expand Down Expand Up @@ -133,6 +152,11 @@ 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);
Expand All @@ -143,6 +167,11 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import java.util.concurrent.TimeUnit;

import static com.tencent.kona.crypto.CryptoUtils.toBytes;
import static com.tencent.kona.crypto.TestUtils.PROVIDER;
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;
Expand Down Expand Up @@ -113,7 +112,28 @@ public void setup() throws Exception {
new SM2PublicKey(toBytes(PEER_PUB_KEY)),
true,
16);
keyAgreement = KeyAgreement.getInstance("SM2", PROVIDER);
keyAgreement = KeyAgreement.getInstance("SM2", "KonaCrypto");
keyAgreement.init(
new SM2PrivateKey(toBytes(TMP_PRI_KEY)), paramSpec);
}
}

@State(Scope.Benchmark)
public static class KeyAgreementNativeHolder {

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-Native");
keyAgreement.init(
new SM2PrivateKey(toBytes(TMP_PRI_KEY)), paramSpec);
}
Expand Down Expand Up @@ -173,6 +193,12 @@ public byte[] generateSecret(KeyAgreementHolder holder) throws InvalidKeyExcepti
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved.
* 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
Expand Down Expand Up @@ -39,8 +39,6 @@
import java.security.spec.ECGenParameterSpec;
import java.util.concurrent.TimeUnit;

import static com.tencent.kona.crypto.TestUtils.PROVIDER;

/**
* The JMH-based performance test for SM2 key pair generation.
*/
Expand All @@ -64,7 +62,18 @@ public static class KeyPairGenHolder {

@Setup
public void setup() throws Exception {
keyPairGenerator = KeyPairGenerator.getInstance("SM2", PROVIDER);
keyPairGenerator = KeyPairGenerator.getInstance("SM2", "KonaCrypto");
}
}

@State(Scope.Benchmark)
public static class KeyPairGenHolderNative {

KeyPairGenerator keyPairGenerator;

@Setup
public void setup() throws Exception {
keyPairGenerator = KeyPairGenerator.getInstance("SM2", "KonaCrypto-Native");
}
}

Expand All @@ -85,6 +94,11 @@ 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022, 2023, THL A29 Limited, a Tencent company. All rights reserved.
* 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
Expand Down Expand Up @@ -89,7 +89,7 @@ private static KeyPair keyPair(BigInteger priKeyValue) {
@State(Scope.Thread)
public static class SignerHolder {

@Param({"KonaCrypto", "BC"})
@Param({"KonaCrypto", "KonaCrypto-Native", "BC"})
String provider;

@Param({"Small", "Mid", "Big"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import java.security.interfaces.ECPublicKey;
import java.util.concurrent.TimeUnit;

import static com.tencent.kona.crypto.TestUtils.PROVIDER;
import static com.tencent.kona.crypto.CryptoUtils.toBytes;

/**
Expand Down Expand Up @@ -77,7 +76,21 @@ public static class SignerHolder {

@Setup(Level.Trial)
public void setup() throws Exception {
signer = Signature.getInstance("SM2", PROVIDER);
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 {

Signature signer;

@Setup(Level.Trial)
public void setup() throws Exception {
signer = Signature.getInstance("SM2", "KonaCrypto-Native");
signer.setParameter(new SM2SignatureParameterSpec(
ID, (ECPublicKey) KEY_PAIR.getPublic()));
signer.initSign(KEY_PAIR.getPrivate());
Expand Down Expand Up @@ -108,14 +121,40 @@ public static class VerifierHolder {
public void setup() throws Exception {
signature = signature();

verifier = Signature.getInstance("SM2", 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", PROVIDER);
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 {

byte[] signature;
Signature verifier;

@Setup(Level.Trial)
public void setup() throws Exception {
signature = signature();

verifier = Signature.getInstance("SM2", "KonaCrypto-Native");
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");
signer.setParameter(new SM2SignatureParameterSpec(
ID, (ECPublicKey) KEY_PAIR.getPublic()));
signer.initSign(KEY_PAIR.getPrivate());
Expand Down Expand Up @@ -155,6 +194,12 @@ public byte[] sign(SignerHolder holder) throws Exception {
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);
Expand All @@ -167,6 +212,12 @@ public boolean verify(VerifierHolder holder) throws Exception {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.security.Security;
import java.util.concurrent.TimeUnit;

import static com.tencent.kona.crypto.TestUtils.PROVIDER;
import static com.tencent.kona.crypto.CryptoUtils.toBytes;

/**
Expand Down Expand Up @@ -71,7 +70,19 @@ public static class MacHolder {

@Setup(Level.Trial)
public void setup() throws Exception {
mac = Mac.getInstance("HmacSM3", PROVIDER);
mac = Mac.getInstance("HmacSM3", "KonaCrypto");
mac.init(SECRET_KEY);
}
}

@State(Scope.Benchmark)
public static class MacHolderNative {

Mac mac;

@Setup(Level.Trial)
public void setup() throws Exception {
mac = Mac.getInstance("HmacSM3", "KonaCrypto-Native");
mac.init(SECRET_KEY);
}
}
Expand All @@ -93,6 +104,11 @@ 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);
Expand Down
Loading
Loading