Skip to content

Commit

Permalink
JNI: wrap native SHA-3 support in com.wolfssl.wolfcrypt.Hmac class
Browse files Browse the repository at this point in the history
  • Loading branch information
cconlon committed Mar 5, 2025
1 parent 89403b8 commit 53a4002
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 41 deletions.
32 changes: 32 additions & 0 deletions jni/include/com_wolfssl_wolfcrypt_Hmac.h

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

135 changes: 95 additions & 40 deletions jni/jni_hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
static WC_INLINE int GetHashSizeByType(int type)
{
if (!(type == WC_MD5 || type == WC_SHA || type == WC_SHA224
|| type == WC_SHA256 || type == WC_SHA384 || type == WC_SHA512)) {
|| type == WC_SHA256 || type == WC_SHA384 || type == WC_SHA512
|| type == WC_SHA3_224 || type == WC_SHA3_256 || type == WC_SHA3_384
|| type == WC_SHA3_512)) {
return BAD_FUNC_ARG;
}

Expand Down Expand Up @@ -100,14 +102,27 @@ static WC_INLINE int GetHashSizeByType(int type)
return SHA512_DIGEST_SIZE;
#endif

#if defined(WOLFSSL_SHA3)
case WC_SHA3_224:
return WC_SHA3_224_DIGEST_SIZE;

case WC_SHA3_256:
return WC_SHA3_256_DIGEST_SIZE;

case WC_SHA3_384:
return WC_SHA3_384_DIGEST_SIZE;

case WC_SHA3_512:
return WC_SHA3_512_DIGEST_SIZE;
#endif

default:
return BAD_FUNC_ARG;
}
}

JNIEXPORT jlong JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_mallocNativeStruct(
JNIEnv* env, jobject this)
JNIEXPORT jlong JNICALL Java_com_wolfssl_wolfcrypt_Hmac_mallocNativeStruct
(JNIEnv* env, jobject this)
{
#ifndef NO_HMAC
Hmac* hmac = NULL;
Expand All @@ -131,9 +146,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_mallocNativeStruct(
#endif
}

JNIEXPORT void JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacSetKey(
JNIEnv* env, jobject this, jint type, jbyteArray key_object)
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacSetKey
(JNIEnv* env, jobject this, jint type, jbyteArray key_object)
{
#ifndef NO_HMAC
int ret = 0;
Expand Down Expand Up @@ -165,9 +179,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacSetKey(
#endif
}

JNIEXPORT void JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate__B(
JNIEnv* env, jobject this, jbyte data)
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate__B
(JNIEnv* env, jobject this, jbyte data)
{
#ifndef NO_HMAC
int ret = 0;
Expand All @@ -191,9 +204,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate__B(
#endif
}

JNIEXPORT void JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate___3BII(
JNIEnv* env, jobject this, jbyteArray data_object, jint offset, jint length)
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate___3BII
(JNIEnv* env, jobject this, jbyteArray data_object, jint offset, jint length)
{
#ifndef NO_HMAC
int ret = 0;
Expand Down Expand Up @@ -225,9 +237,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate___3BII(
#endif
}

JNIEXPORT void JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate__Ljava_nio_ByteBuffer_2II(
JNIEnv* env, jobject this, jobject data_object, jint offset, jint length)
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate__Ljava_nio_ByteBuffer_2II
(JNIEnv* env, jobject this, jobject data_object, jint offset, jint length)
{
#ifndef NO_HMAC
int ret = 0;
Expand Down Expand Up @@ -257,9 +268,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate__Ljava_nio_ByteBuffer_2II(
#endif
}

JNIEXPORT jbyteArray JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacFinal(
JNIEnv* env, jobject this)
JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacFinal
(JNIEnv* env, jobject this)
{
jbyteArray result = NULL;

Expand Down Expand Up @@ -308,9 +318,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacFinal(
return result;
}

JNIEXPORT jint JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacSizeByType(
JNIEnv* env, jobject this, jint type)
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacSizeByType
(JNIEnv* env, jobject this, jint type)
{
jint result = 0;

Expand All @@ -330,9 +339,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacSizeByType(
return result;
}

JNIEXPORT jint JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_getCodeMd5(
JNIEnv* env, jobject this)
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeMd5
(JNIEnv* env, jobject this)
{
#ifndef NO_MD5
jint result = WC_MD5;
Expand All @@ -344,9 +352,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_getCodeMd5(
#endif
}

JNIEXPORT jint JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha(
JNIEnv* env, jobject this)
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha
(JNIEnv* env, jobject this)
{
#ifndef NO_SHA
jint result = WC_SHA;
Expand All @@ -358,9 +365,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha(
#endif
}

JNIEXPORT jint JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha224(
JNIEnv* env, jobject this)
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha224
(JNIEnv* env, jobject this)
{
#ifdef WOLFSSL_SHA224
jint result = WC_SHA224;
Expand All @@ -372,9 +378,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha224(
#endif
}

JNIEXPORT jint JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha256(
JNIEnv* env, jobject this)
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha256
(JNIEnv* env, jobject this)
{
#ifndef NO_SHA256
jint result = WC_SHA256;
Expand All @@ -386,9 +391,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha256(
#endif
}

JNIEXPORT jint JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha384(
JNIEnv* env, jobject this)
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha384
(JNIEnv* env, jobject this)
{
#ifdef WOLFSSL_SHA384
jint result = WC_SHA384;
Expand All @@ -400,9 +404,8 @@ Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha384(
#endif
}

JNIEXPORT jint JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha512(
JNIEnv* env, jobject this)
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha512
(JNIEnv* env, jobject this)
{
#ifdef WOLFSSL_SHA512
jint result = WC_SHA512;
Expand All @@ -414,3 +417,55 @@ Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha512(
#endif
}

JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha3_1224
(JNIEnv* env, jclass this)
{
#ifdef WOLFSSL_SHA3
jint result = WC_SHA3_224;
LogStr("WC_SHA3_224 = %d\n", result);
return result;
#else
/* not compiled in */
return (jint) -1;
#endif
}

JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha3_1256
(JNIEnv* env, jclass this)
{
#ifdef WOLFSSL_SHA3
jint result = WC_SHA3_256;
LogStr("WC_SHA3_256 = %d\n", result);
return result;
#else
/* not compiled in */
return (jint) -1;
#endif
}

JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha3_1384
(JNIEnv* env, jclass this)
{
#ifdef WOLFSSL_SHA3
jint result = WC_SHA3_384;
LogStr("WC_SHA3_384 = %d\n", result);
return result;
#else
/* not compiled in */
return (jint) -1;
#endif
}

JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha3_1512
(JNIEnv* env, jclass this)
{
#ifdef WOLFSSL_SHA3
jint result = WC_SHA3_512;
LogStr("WC_SHA3_512 = %d\n", result);
return result;
#else
/* not compiled in */
return (jint) -1;
#endif
}

35 changes: 34 additions & 1 deletion src/main/java/com/wolfssl/wolfcrypt/Hmac.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
public class Hmac extends NativeStruct {

private enum hashType {
typeMD5, typeSHA, typeSHA224, typeSHA256, typeSHA384, typeSHA512;
typeMD5, typeSHA, typeSHA224, typeSHA256, typeSHA384, typeSHA512,
typeSHA3_224, typeSHA3_256, typeSHA3_384, typeSHA3_512;
}

/* types may be -1 if not compiled in at native level */
Expand All @@ -45,6 +46,14 @@ private enum hashType {
public static final int SHA384 = getHashCode(hashType.typeSHA384);
/** HMAC-SHA2-512 type */
public static final int SHA512 = getHashCode(hashType.typeSHA512);
/** HMAC-SHA3-224 type */
public static final int SHA3_224 = getHashCode(hashType.typeSHA3_224);
/** HMAC-SHA3-256 type */
public static final int SHA3_256 = getHashCode(hashType.typeSHA3_256);
/** HMAC-SHA3-384 type */
public static final int SHA3_384 = getHashCode(hashType.typeSHA3_384);
/** HMAC-SHA3-512 type */
public static final int SHA3_512 = getHashCode(hashType.typeSHA3_512);

private WolfCryptState state = WolfCryptState.UNINITIALIZED;
private int type = -1;
Expand Down Expand Up @@ -102,6 +111,10 @@ public Hmac(int type, byte[] key) {
private native static int getCodeSha384();
private native static int getCodeSha512();
private native static int getCodeBlake2b();
private native static int getCodeSha3_224();
private native static int getCodeSha3_256();
private native static int getCodeSha3_384();
private native static int getCodeSha3_512();

/**
* Malloc native JNI Hmac structure
Expand Down Expand Up @@ -333,6 +346,18 @@ else if (type == SHA384) {
else if (type == SHA512) {
return "HmacSHA512";
}
else if (type == SHA3_224) {
return "HmacSHA3-224";
}
else if (type == SHA3_256) {
return "HmacSHA3-256";
}
else if (type == SHA3_384) {
return "HmacSHA3-384";
}
else if (type == SHA3_512) {
return "HmacSHA3-512";
}
else {
return "";
}
Expand Down Expand Up @@ -373,6 +398,14 @@ private static int getHashCode(hashType hash) {
return getCodeSha384();
case typeSHA512:
return getCodeSha512();
case typeSHA3_224:
return getCodeSha3_224();
case typeSHA3_256:
return getCodeSha3_256();
case typeSHA3_384:
return getCodeSha3_384();
case typeSHA3_512:
return getCodeSha3_512();
default:
return WolfCrypt.FAILURE;
}
Expand Down
Loading

0 comments on commit 53a4002

Please sign in to comment.