Skip to content

Commit

Permalink
TKSS-1014: Remove NativeMAC
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshajiang committed Dec 26, 2024
1 parent 9a45a24 commit 0339bfa
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ private static void copyNativeLib(String libName, Path libPath)
native long sm3Clone(long pointer);

/* ***** SM3HMAC ***** */
native long sm3hmacCreateMac();
native void sm3hmacFreeMac(long pointer);

native long sm3hmacCreateCtx(long macPointer, byte[] key);
native long sm3hmacCreateCtx(byte[] key);
native void sm3hmacFreeCtx(long pointer);
native int sm3hmacUpdate(long pointer, byte[] data);
native byte[] sm3hmacFinal(long pointer);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,16 @@ final class NativeSM3HMac extends NativeRef implements Cloneable {

private static final Sweeper SWEEPER = Sweeper.instance();

private static final NativeMAC MAC = new NativeMAC();
static {
SWEEPER.register(NativeSM3HMac.class, new SweepNativeRef(MAC));
}

public NativeSM3HMac(byte[] key) {
super(createCtx(MAC.pointer, key));
super(createCtx(key));
}

private static long createCtx(long macPointer, byte[] key) {
if (macPointer == 0) {
throw new IllegalArgumentException("macPointer is invalid");
}

private static long createCtx(byte[] key) {
if (key == null || key.length == 0) {
throw new IllegalStateException("Key must not be null or empty");
}

return nativeCrypto().sm3hmacCreateCtx(macPointer, key);
return nativeCrypto().sm3hmacCreateCtx(key);
}

public NativeSM3HMac(long pointer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final class SM2Cipher extends CipherSpi {

private static final byte[] B0 = new byte[0];

private volatile NativeSM2Cipher sm2;
private NativeSM2Cipher sm2;
private final ByteArrayWriter buffer = new ByteArrayWriter();

private boolean encrypted = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public final class SM2KeyAgreement extends KeyAgreementSpi {
private SM2KeyAgreementParamSpec paramSpec;
private SM2PublicKey peerEphemeralPublicKey;

private volatile NativeSM2KeyAgreement sm2;
private NativeSM2KeyAgreement sm2;

@Override
protected void engineInit(Key key, SecureRandom random) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public final class SM2Signature extends SignatureSpi {

private static final Sweeper SWEEPER = Sweeper.instance();

private volatile NativeSM2Signature sm2;
private NativeSM2Signature sm2;

private SM2PrivateKey privateKey;
private SM2PublicKey publicKey;
Expand Down
20 changes: 2 additions & 18 deletions kona-crypto/src/main/jni/include/kona/kona_jni.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions kona-crypto/src/main/jni/include/kona/kona_sm3.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "kona/kona_common.h"

EVP_MAC* hmac();

EVP_MD_CTX* sm3_create_ctx();
int sm3_reset(EVP_MD_CTX* ctx);

Expand Down
36 changes: 15 additions & 21 deletions kona-crypto/src/main/jni/kona_sm3.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
#include "kona/kona_common.h"
#include "kona/kona_sm3.h"

EVP_MAC* hmac() {
static EVP_MAC* hmac = NULL;
if (hmac == NULL) {
EVP_MAC* mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
if (mac == NULL) {
OPENSSL_print_err();
}
hmac = mac;
}

return hmac;
}

EVP_MD_CTX* sm3_create_ctx() {
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
if (ctx == NULL) {
Expand Down Expand Up @@ -219,28 +232,9 @@ int sm3hmac_reset(EVP_MAC_CTX* ctx) {
}
}

JNIEXPORT jlong JNICALL Java_com_tencent_kona_crypto_provider_nativeImpl_NativeCrypto_sm3hmacCreateMac
(JNIEnv* env, jobject thisObj) {
EVP_MAC* mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
if (mac == NULL) {
OPENSSL_print_err();
return OPENSSL_FAILURE;
}

return (jlong)mac;
}

JNIEXPORT void JNICALL Java_com_tencent_kona_crypto_provider_nativeImpl_NativeCrypto_sm3hmacFreeMac
(JNIEnv* env, jobject thisObj, jlong pointer) {
EVP_MAC* mac = (EVP_MAC*)pointer;
if (mac != NULL) {
EVP_MAC_free(mac);
}
}

JNIEXPORT jlong JNICALL Java_com_tencent_kona_crypto_provider_nativeImpl_NativeCrypto_sm3hmacCreateCtx
(JNIEnv* env, jobject thisObj, jlong macPointer, jbyteArray key) {
EVP_MAC* mac = (EVP_MAC*)macPointer;
(JNIEnv* env, jobject thisObj, jbyteArray key) {
EVP_MAC* mac = hmac();
if (mac == NULL) {
return OPENSSL_FAILURE;
}
Expand Down
Binary file modified kona-crypto/src/main/resources/libKonaCrypto-linux-aarch64.so
Binary file not shown.
Binary file modified kona-crypto/src/main/resources/libKonaCrypto-linux-x86_64.so
Binary file not shown.

0 comments on commit 0339bfa

Please sign in to comment.