From b6d982a7fc156ccfede3399c38c177c6f31d54c8 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Thu, 9 Jan 2020 10:51:22 -0700 Subject: [PATCH] remove BLAKE2b, fix hash algo conditional compiles --- build.xml | 1 + .../com_wolfssl_wolfcrypt_FeatureDetect.h | 53 +++++++ .../com_wolfssl_wolfcrypt_Hmac_hashType.h | 13 ++ jni/include/com_wolfssl_wolfcrypt_WolfCrypt.h | 2 + jni/jni_feature_detect.c | 87 ++++++++++++ jni/jni_hmac.c | 41 ++++-- jni/jni_sha.c | 12 +- makefile | 2 +- makefile.linux | 2 +- makefile.macosx | 2 +- .../provider/jce/WolfCryptProvider.java | 108 +++++++++------ .../com/wolfssl/wolfcrypt/FeatureDetect.java | 72 ++++++++++ src/main/java/com/wolfssl/wolfcrypt/Hmac.java | 26 ++-- .../provider/jce/test/WolfCryptMacTest.java | 128 ++++++++++++----- .../test/WolfCryptMessageDigestMd5Test.java | 21 ++- .../WolfCryptMessageDigestSha256Test.java | 21 ++- .../WolfCryptMessageDigestSha384Test.java | 21 ++- .../WolfCryptMessageDigestSha512Test.java | 21 ++- .../test/WolfCryptMessageDigestShaTest.java | 21 ++- .../jce/test/WolfCryptSignatureTest.java | 60 +++++--- .../com/wolfssl/wolfcrypt/test/HmacTest.java | 129 +++++++++++------- .../com/wolfssl/wolfcrypt/test/Md5Test.java | 16 +++ .../wolfssl/wolfcrypt/test/Sha256Test.java | 16 +++ .../wolfssl/wolfcrypt/test/Sha384Test.java | 16 +++ .../wolfssl/wolfcrypt/test/Sha512Test.java | 16 +++ .../com/wolfssl/wolfcrypt/test/ShaTest.java | 16 +++ 26 files changed, 717 insertions(+), 206 deletions(-) create mode 100644 jni/include/com_wolfssl_wolfcrypt_FeatureDetect.h create mode 100644 jni/include/com_wolfssl_wolfcrypt_Hmac_hashType.h create mode 100644 jni/jni_feature_detect.c create mode 100644 src/main/java/com/wolfssl/wolfcrypt/FeatureDetect.java diff --git a/build.xml b/build.xml index b67d95f..815b789 100644 --- a/build.xml +++ b/build.xml @@ -176,6 +176,7 @@ description="Generate javah headers"> + diff --git a/jni/include/com_wolfssl_wolfcrypt_FeatureDetect.h b/jni/include/com_wolfssl_wolfcrypt_FeatureDetect.h new file mode 100644 index 0000000..5e2745b --- /dev/null +++ b/jni/include/com_wolfssl_wolfcrypt_FeatureDetect.h @@ -0,0 +1,53 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_wolfssl_wolfcrypt_FeatureDetect */ + +#ifndef _Included_com_wolfssl_wolfcrypt_FeatureDetect +#define _Included_com_wolfssl_wolfcrypt_FeatureDetect +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_wolfssl_wolfcrypt_FeatureDetect + * Method: Md5Enabled + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_com_wolfssl_wolfcrypt_FeatureDetect_Md5Enabled + (JNIEnv *, jclass); + +/* + * Class: com_wolfssl_wolfcrypt_FeatureDetect + * Method: ShaEnabled + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_com_wolfssl_wolfcrypt_FeatureDetect_ShaEnabled + (JNIEnv *, jclass); + +/* + * Class: com_wolfssl_wolfcrypt_FeatureDetect + * Method: Sha256Enabled + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_com_wolfssl_wolfcrypt_FeatureDetect_Sha256Enabled + (JNIEnv *, jclass); + +/* + * Class: com_wolfssl_wolfcrypt_FeatureDetect + * Method: Sha384Enabled + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_com_wolfssl_wolfcrypt_FeatureDetect_Sha384Enabled + (JNIEnv *, jclass); + +/* + * Class: com_wolfssl_wolfcrypt_FeatureDetect + * Method: Sha512Enabled + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_com_wolfssl_wolfcrypt_FeatureDetect_Sha512Enabled + (JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/jni/include/com_wolfssl_wolfcrypt_Hmac_hashType.h b/jni/include/com_wolfssl_wolfcrypt_Hmac_hashType.h new file mode 100644 index 0000000..b670d7e --- /dev/null +++ b/jni/include/com_wolfssl_wolfcrypt_Hmac_hashType.h @@ -0,0 +1,13 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_wolfssl_wolfcrypt_Hmac_hashType */ + +#ifndef _Included_com_wolfssl_wolfcrypt_Hmac_hashType +#define _Included_com_wolfssl_wolfcrypt_Hmac_hashType +#ifdef __cplusplus +extern "C" { +#endif +#ifdef __cplusplus +} +#endif +#endif diff --git a/jni/include/com_wolfssl_wolfcrypt_WolfCrypt.h b/jni/include/com_wolfssl_wolfcrypt_WolfCrypt.h index a420e33..367522e 100644 --- a/jni/include/com_wolfssl_wolfcrypt_WolfCrypt.h +++ b/jni/include/com_wolfssl_wolfcrypt_WolfCrypt.h @@ -9,6 +9,8 @@ extern "C" { #endif #undef com_wolfssl_wolfcrypt_WolfCrypt_SUCCESS #define com_wolfssl_wolfcrypt_WolfCrypt_SUCCESS 0L +#undef com_wolfssl_wolfcrypt_WolfCrypt_FAILURE +#define com_wolfssl_wolfcrypt_WolfCrypt_FAILURE -1L #undef com_wolfssl_wolfcrypt_WolfCrypt_SIZE_OF_128_BITS #define com_wolfssl_wolfcrypt_WolfCrypt_SIZE_OF_128_BITS 16L #undef com_wolfssl_wolfcrypt_WolfCrypt_SIZE_OF_160_BITS diff --git a/jni/jni_feature_detect.c b/jni/jni_feature_detect.c new file mode 100644 index 0000000..d32602a --- /dev/null +++ b/jni/jni_feature_detect.c @@ -0,0 +1,87 @@ +/* jni_feature_detect.c + * + * Copyright (C) 2006-2020 wolfSSL Inc. + * + * This file is part of wolfSSL. (formerly known as CyaSSL) + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef __ANDROID__ + #include +#endif +#include +#include + +JNIEXPORT jboolean JNICALL Java_com_wolfssl_wolfcrypt_FeatureDetect_Md5Enabled + (JNIEnv* env, jclass jcl) +{ + (void)env; + (void)jcl; +#ifndef NO_MD5 + return JNI_TRUE; +#else + return JNI_FALSE; +#endif +} + +JNIEXPORT jboolean JNICALL Java_com_wolfssl_wolfcrypt_FeatureDetect_ShaEnabled + (JNIEnv* env, jclass jcl) +{ + (void)env; + (void)jcl; +#ifndef NO_SHA + return JNI_TRUE; +#else + return JNI_FALSE; +#endif +} + +JNIEXPORT jboolean JNICALL Java_com_wolfssl_wolfcrypt_FeatureDetect_Sha256Enabled + (JNIEnv* env, jclass jcl) +{ + (void)env; + (void)jcl; +#ifndef NO_SHA256 + return JNI_TRUE; +#else + return JNI_FALSE; +#endif +} + +JNIEXPORT jboolean JNICALL Java_com_wolfssl_wolfcrypt_FeatureDetect_Sha384Enabled + (JNIEnv* env, jclass jcl) +{ + (void)env; + (void)jcl; +#ifdef WOLFSSL_SHA384 + return JNI_TRUE; +#else + return JNI_FALSE; +#endif +} + +JNIEXPORT jboolean JNICALL Java_com_wolfssl_wolfcrypt_FeatureDetect_Sha512Enabled + (JNIEnv* env, jclass jcl) +{ + (void)env; + (void)jcl; +#ifdef WOLFSSL_SHA512 + return JNI_TRUE; +#else + return JNI_FALSE; +#endif +} + diff --git a/jni/jni_hmac.c b/jni/jni_hmac.c index 49c9de4..056b7a1 100644 --- a/jni/jni_hmac.c +++ b/jni/jni_hmac.c @@ -39,7 +39,7 @@ static WC_INLINE int GetHashSizeByType(int type) { if (!(type == WC_MD5 || type == WC_SHA || type == WC_SHA256 - || type == WC_SHA384 || type == WC_SHA512 || type == BLAKE2B_ID)) + || type == WC_SHA384 || type == WC_SHA512)) return BAD_FUNC_ARG; switch (type) { @@ -73,12 +73,6 @@ static WC_INLINE int GetHashSizeByType(int type) break; #endif - #ifdef HAVE_BLAKE2 - case BLAKE2B_ID: - return BLAKE2B_OUTBYTES; - break; - #endif - default: return BAD_FUNC_ARG; break; @@ -308,52 +302,69 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeMd5( JNIEnv* env, jobject this) { +#ifndef NO_MD5 jint result = WC_MD5; LogStr("WC_MD5 = %d\n", result); return result; +#else + /* not compiled in */ + return (jint) -1; +#endif } JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha( JNIEnv* env, jobject this) { +#ifndef NO_SHA jint result = WC_SHA; LogStr("WC_SHA = %d\n", result); return result; +#else + /* not compiled in */ + return (jint) -1; +#endif } JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha256( JNIEnv* env, jobject this) { +#ifndef NO_SHA256 jint result = WC_SHA256; LogStr("WC_SHA256 = %d\n", result); return result; +#else + /* not compiled in */ + return (jint) -1; +#endif } JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha384( JNIEnv* env, jobject this) { +#ifdef WOLFSSL_SHA384 jint result = WC_SHA384; LogStr("WC_SHA384 = %d\n", result); return result; +#else + /* not compiled in */ + return (jint) -1; +#endif } JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha512( JNIEnv* env, jobject this) { +#ifdef WOLFSSL_SHA512 jint result = WC_SHA512; LogStr("WC_SHA512 = %d\n", result); return result; +#else + /* not compiled in */ + return (jint) -1; +#endif } -JNIEXPORT jint JNICALL -Java_com_wolfssl_wolfcrypt_Hmac_getCodeBlake2b( - JNIEnv* env, jobject this) -{ - jint result = BLAKE2B_ID; - LogStr("BLAKE2B_ID = %d", result); - return result; -} diff --git a/jni/jni_sha.c b/jni/jni_sha.c index 91c329a..ec6ec60 100644 --- a/jni/jni_sha.c +++ b/jni/jni_sha.c @@ -80,7 +80,7 @@ Java_com_wolfssl_wolfcrypt_Sha384_mallocNativeStruct( { jlong ret = 0; -#ifdef WOLFSSL_SHA512 +#ifdef WOLFSSL_SHA384 ret = (jlong) XMALLOC(sizeof(Sha384), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (!ret) @@ -433,7 +433,7 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha384_native_1init( JNIEnv* env, jobject this) { -#ifdef WOLFSSL_SHA512 +#ifdef WOLFSSL_SHA384 int ret = 0; Sha384* sha = (Sha384*) getNativeStruct(env, this); if ((*env)->ExceptionOccurred(env)) { @@ -456,7 +456,7 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha384_native_1update__Ljava_nio_ByteBuffer_2II( JNIEnv* env, jobject this, jobject data_buffer, jint position, jint len) { -#ifdef WOLFSSL_SHA512 +#ifdef WOLFSSL_SHA384 int ret = 0; Sha384* sha = NULL; byte* data = NULL; @@ -489,7 +489,7 @@ Java_com_wolfssl_wolfcrypt_Sha384_native_1update___3BII( JNIEnv* env, jobject this, jbyteArray data_buffer, jint offset, jint len) { -#ifdef WOLFSSL_SHA512 +#ifdef WOLFSSL_SHA384 int ret = 0; Sha384* sha = NULL; byte* data = NULL; @@ -525,7 +525,7 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha384_native_1final__Ljava_nio_ByteBuffer_2I( JNIEnv* env, jobject this, jobject hash_buffer, jint position) { -#ifdef WOLFSSL_SHA512 +#ifdef WOLFSSL_SHA384 int ret = 0; Sha384* sha = NULL; byte* hash = NULL; @@ -557,7 +557,7 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha384_native_1final___3B( JNIEnv* env, jobject this, jbyteArray hash_buffer) { -#ifdef WOLFSSL_SHA512 +#ifdef WOLFSSL_SHA384 int ret = 0; Sha384* sha = NULL; byte* hash = NULL; diff --git a/makefile b/makefile index 49adfe4..1ff7e7a 100644 --- a/makefile +++ b/makefile @@ -5,7 +5,7 @@ INC_PATH = $(SRC_PATH)/include OBJ_LIST = jni_fips.o jni_native_struct.o jni_aes.o jni_des3.o jni_md5.o \ jni_sha.o jni_hmac.o jni_rng.o jni_rsa.o jni_dh.o jni_ecc.o \ jni_ed25519.o jni_curve25519.o jni_chacha.o jni_error.o jni_asn.o \ - jni_logging.o + jni_logging.o jni_feature_detect.o OBJS = $(patsubst %,$(OUT_PATH)/%,$(OBJ_LIST)) TARGET = $(OUT_PATH)/libwolfcryptjni.jnilib diff --git a/makefile.linux b/makefile.linux index d188bb3..3841c1b 100644 --- a/makefile.linux +++ b/makefile.linux @@ -5,7 +5,7 @@ INC_PATH = $(SRC_PATH)/include OBJ_LIST = jni_fips.o jni_native_struct.o jni_aes.o jni_des3.o jni_md5.o \ jni_sha.o jni_hmac.o jni_rng.o jni_rsa.o jni_dh.o jni_ecc.o \ jni_ed25519.o jni_curve25519.o jni_chacha.o jni_error.o jni_asn.o \ - jni_logging.o + jni_logging.o jni_feature_detect.o OBJS = $(patsubst %,$(OUT_PATH)/%,$(OBJ_LIST)) TARGET = $(OUT_PATH)/libwolfcryptjni.so diff --git a/makefile.macosx b/makefile.macosx index 49adfe4..1ff7e7a 100644 --- a/makefile.macosx +++ b/makefile.macosx @@ -5,7 +5,7 @@ INC_PATH = $(SRC_PATH)/include OBJ_LIST = jni_fips.o jni_native_struct.o jni_aes.o jni_des3.o jni_md5.o \ jni_sha.o jni_hmac.o jni_rng.o jni_rsa.o jni_dh.o jni_ecc.o \ jni_ed25519.o jni_curve25519.o jni_chacha.o jni_error.o jni_asn.o \ - jni_logging.o + jni_logging.o jni_feature_detect.o OBJS = $(patsubst %,$(OUT_PATH)/%,$(OBJ_LIST)) TARGET = $(OUT_PATH)/libwolfcryptjni.jnilib diff --git a/src/main/java/com/wolfssl/provider/jce/WolfCryptProvider.java b/src/main/java/com/wolfssl/provider/jce/WolfCryptProvider.java index b045d63..dc89f57 100644 --- a/src/main/java/com/wolfssl/provider/jce/WolfCryptProvider.java +++ b/src/main/java/com/wolfssl/provider/jce/WolfCryptProvider.java @@ -22,6 +22,7 @@ package com.wolfssl.provider.jce; import java.security.Provider; +import com.wolfssl.wolfcrypt.FeatureDetect; /** * wolfCrypt JCE Provider implementation @@ -35,16 +36,26 @@ public final class WolfCryptProvider extends Provider { super("wolfJCE", 1.0, "wolfCrypt JCE Provider"); /* MessageDigest */ - put("MessageDigest.MD5", - "com.wolfssl.provider.jce.WolfCryptMessageDigestMd5"); - put("MessageDigest.SHA-1", - "com.wolfssl.provider.jce.WolfCryptMessageDigestSha"); - put("MessageDigest.SHA-256", - "com.wolfssl.provider.jce.WolfCryptMessageDigestSha256"); - put("MessageDigest.SHA-384", - "com.wolfssl.provider.jce.WolfCryptMessageDigestSha384"); - put("MessageDigest.SHA-512", - "com.wolfssl.provider.jce.WolfCryptMessageDigestSha512"); + if (FeatureDetect.Md5Enabled()) { + put("MessageDigest.MD5", + "com.wolfssl.provider.jce.WolfCryptMessageDigestMd5"); + } + if (FeatureDetect.ShaEnabled()) { + put("MessageDigest.SHA-1", + "com.wolfssl.provider.jce.WolfCryptMessageDigestSha"); + } + if (FeatureDetect.Sha256Enabled()) { + put("MessageDigest.SHA-256", + "com.wolfssl.provider.jce.WolfCryptMessageDigestSha256"); + } + if (FeatureDetect.Sha384Enabled()) { + put("MessageDigest.SHA-384", + "com.wolfssl.provider.jce.WolfCryptMessageDigestSha384"); + } + if (FeatureDetect.Sha512Enabled()) { + put("MessageDigest.SHA-512", + "com.wolfssl.provider.jce.WolfCryptMessageDigestSha512"); + } /* SecureRandom */ /* TODO: May need to add "SHA1PRNG" alias, other JCA consumemrs may @@ -53,37 +64,56 @@ public final class WolfCryptProvider extends Provider { "com.wolfssl.provider.jce.WolfCryptRandom"); /* Signature */ - put("Signature.MD5withRSA", - "com.wolfssl.provider.jce.WolfCryptSignature$wcMD5wRSA"); - put("Signature.SHA1withRSA", - "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA1wRSA"); - put("Signature.SHA256withRSA", - "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA256wRSA"); - put("Signature.SHA384withRSA", - "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA384wRSA"); - put("Signature.SHA512withRSA", - "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA512wRSA"); - - put("Signature.SHA1withECDSA", - "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA1wECDSA"); - put("Signature.SHA256withECDSA", - "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA256wECDSA"); - put("Signature.SHA384withECDSA", - "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA384wECDSA"); - put("Signature.SHA512withECDSA", - "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA512wECDSA"); + if (FeatureDetect.Md5Enabled()) { + put("Signature.MD5withRSA", + "com.wolfssl.provider.jce.WolfCryptSignature$wcMD5wRSA"); + } + if (FeatureDetect.ShaEnabled()) { + put("Signature.SHA1withRSA", + "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA1wRSA"); + put("Signature.SHA1withECDSA", + "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA1wECDSA"); + } + if (FeatureDetect.Sha256Enabled()) { + put("Signature.SHA256withRSA", + "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA256wRSA"); + put("Signature.SHA256withECDSA", + "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA256wECDSA"); + } + if (FeatureDetect.Sha384Enabled()) { + put("Signature.SHA384withRSA", + "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA384wRSA"); + put("Signature.SHA384withECDSA", + "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA384wECDSA"); + } + if (FeatureDetect.Sha512Enabled()) { + put("Signature.SHA512withRSA", + "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA512wRSA"); + put("Signature.SHA512withECDSA", + "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA512wECDSA"); + } /* Mac */ - put("Mac.HmacMD5", - "com.wolfssl.provider.jce.WolfCryptMac$wcHmacMD5"); - put("Mac.HmacSHA1", - "com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA1"); - put("Mac.HmacSHA256", - "com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA256"); - put("Mac.HmacSHA384", - "com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA384"); - put("Mac.HmacSHA512", - "com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA512"); + if (FeatureDetect.Md5Enabled()) { + put("Mac.HmacMD5", + "com.wolfssl.provider.jce.WolfCryptMac$wcHmacMD5"); + } + if (FeatureDetect.ShaEnabled()) { + put("Mac.HmacSHA1", + "com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA1"); + } + if (FeatureDetect.Sha256Enabled()) { + put("Mac.HmacSHA256", + "com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA256"); + } + if (FeatureDetect.Sha384Enabled()) { + put("Mac.HmacSHA384", + "com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA384"); + } + if (FeatureDetect.Sha512Enabled()) { + put("Mac.HmacSHA512", + "com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA512"); + } /* Cipher */ put("Cipher.AES/CBC/NoPadding", diff --git a/src/main/java/com/wolfssl/wolfcrypt/FeatureDetect.java b/src/main/java/com/wolfssl/wolfcrypt/FeatureDetect.java new file mode 100644 index 0000000..463d5d7 --- /dev/null +++ b/src/main/java/com/wolfssl/wolfcrypt/FeatureDetect.java @@ -0,0 +1,72 @@ +/* FeatureDetect.java + * + * Copyright (C) 2006-2020 wolfSSL Inc. + * + * This file is part of wolfSSL. (formerly known as CyaSSL) + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +package com.wolfssl.wolfcrypt; + +/** + * Native feature detection class + * Used to expose native preprocessor values to Java + * + * @author wolfSSL + * @version 1.0, January 2020 + */ +public class FeatureDetect { + + /** + * Tests if MD5 is compiled into the native wolfSSL library. + * + * @return true if enabled, otherwise false if not compiled in. + */ + public static native boolean Md5Enabled(); + + /** + * Tests if SHA-1 is compiled into the native wolfSSL library. + * + * @return true if enabled, otherwise false if not compiled in. + */ + public static native boolean ShaEnabled(); + + /** + * Tests if SHA-256 is compiled into the native wolfSSL library. + * + * @return true if enabled, otherwise false if not compiled in. + */ + public static native boolean Sha256Enabled(); + + /** + * Tests if SHA-384 is compiled into the native wolfSSL library. + * + * @return true if enabled, otherwise false if not compiled in. + */ + public static native boolean Sha384Enabled(); + + /** + * Tests if SHA-512 is compiled into the native wolfSSL library. + * + * @return true if enabled, otherwise false if not compiled in. + */ + public static native boolean Sha512Enabled(); + + static { + System.loadLibrary("wolfcryptjni"); + } +} + diff --git a/src/main/java/com/wolfssl/wolfcrypt/Hmac.java b/src/main/java/com/wolfssl/wolfcrypt/Hmac.java index 14791b8..18cd982 100644 --- a/src/main/java/com/wolfssl/wolfcrypt/Hmac.java +++ b/src/main/java/com/wolfssl/wolfcrypt/Hmac.java @@ -22,6 +22,7 @@ package com.wolfssl.wolfcrypt; import com.wolfssl.wolfcrypt.WolfCrypt; +import com.wolfssl.wolfcrypt.WolfCryptException; import java.nio.ByteBuffer; /** @@ -33,15 +34,15 @@ import java.nio.ByteBuffer; public class Hmac extends NativeStruct { private enum hashType { - typeMD5, typeSHA, typeSHA256, typeSHA384, typeSHA512, typeBLAKE2b; + typeMD5, typeSHA, typeSHA256, typeSHA384, typeSHA512; } + /* types may be -1 if not compiled in at native level */ public static final int MD5 = getHashCode(hashType.typeMD5); public static final int SHA = getHashCode(hashType.typeSHA); public static final int SHA256 = getHashCode(hashType.typeSHA256); public static final int SHA384 = getHashCode(hashType.typeSHA384); public static final int SHA512 = getHashCode(hashType.typeSHA512); - public static final int BLAKE2b = getHashCode(hashType.typeBLAKE2b); private WolfCryptState state = WolfCryptState.UNINITIALIZED; private int type = -1; @@ -80,7 +81,20 @@ public class Hmac extends NativeStruct { protected native long mallocNativeStruct() throws OutOfMemoryError; + /* check if type is -1, if so that type is not compiled in at native + * wolfSSL level. Throw exception if so. */ + private void checkHashTypeCompiledIn(int type) throws WolfCryptException { + WolfCryptError notCompiledIn = WolfCryptError.NOT_COMPILED_IN; + if (type == -1) { + throw new WolfCryptException(notCompiledIn.getCode()); + } + } + public void setKey(int type, byte[] key) { + + /* verify hash type is compiled in */ + checkHashTypeCompiledIn(type); + wc_HmacSetKey(type, key); this.type = type; this.key = key; @@ -162,7 +176,7 @@ public class Hmac extends NativeStruct { if (type == MD5) { return "HmacMD5"; - } + } else if (type == SHA256) { return "HmacSHA256"; } @@ -172,9 +186,7 @@ public class Hmac extends NativeStruct { else if (type == SHA512) { return "HmacSHA512"; } - else if (type == BLAKE2b) { - return "HmacBLAKE2b"; - } else { + else { return ""; } @@ -205,8 +217,6 @@ public class Hmac extends NativeStruct { return getCodeSha384(); case typeSHA512: return getCodeSha512(); - case typeBLAKE2b: - return getCodeBlake2b(); default: return WolfCrypt.FAILURE; } diff --git a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMacTest.java b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMacTest.java index 07800f9..d62cb53 100644 --- a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMacTest.java +++ b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMacTest.java @@ -23,11 +23,15 @@ package com.wolfssl.provider.jce.test; import static org.junit.Assert.*; import org.junit.Test; +import org.junit.Assume; import org.junit.BeforeClass; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; +import java.util.Arrays; +import java.util.ArrayList; + import java.security.Security; import java.security.Provider; import java.security.NoSuchProviderException; @@ -39,7 +43,7 @@ import com.wolfssl.provider.jce.WolfCryptProvider; public class WolfCryptMacTest { - private String wolfJCEAlgos[] = { + private static String wolfJCEAlgos[] = { "HmacMD5", "HmacSHA1", "HmacSHA256", @@ -47,8 +51,11 @@ public class WolfCryptMacTest { "HmacSHA512" }; + private static ArrayList enabledAlgos = + new ArrayList(); + /* expected digest sizes, order must match wolfJCEAlgos */ - private int wolfJCEMacLengths[] = { + private static int wolfJCEMacLengths[] = { 16, 20, 32, @@ -56,14 +63,32 @@ public class WolfCryptMacTest { 64 }; + private static ArrayList enabledAlgoLengths = + new ArrayList(); + @BeforeClass - public static void testProviderInstallationAtRuntime() { + public static void testProviderInstallationAtRuntime() + throws NoSuchProviderException { + + Mac mac; /* install wolfJCE provider at runtime */ Security.addProvider(new WolfCryptProvider()); Provider p = Security.getProvider("wolfJCE"); assertNotNull(p); + + /* populate enabledAlgos, some native features may be + * compiled out */ + for (int i = 0; i < wolfJCEAlgos.length; i++) { + try { + mac = Mac.getInstance(wolfJCEAlgos[i], "wolfJCE"); + enabledAlgos.add(wolfJCEAlgos[i]); + enabledAlgoLengths.add(wolfJCEMacLengths[i]); + } catch (NoSuchAlgorithmException e) { + /* algo not compiled in */ + } + } } @Test @@ -73,8 +98,8 @@ public class WolfCryptMacTest { Mac mac; /* try to get all available options we expect to have */ - for (int i = 0; i < wolfJCEAlgos.length; i++) { - mac = Mac.getInstance(wolfJCEAlgos[i], "wolfJCE"); + for (int i = 0; i < enabledAlgos.size(); i++) { + mac = Mac.getInstance(enabledAlgos.get(i), "wolfJCE"); } /* getting a garbage algorithm should throw an exception */ @@ -93,12 +118,12 @@ public class WolfCryptMacTest { Mac mac; - for (int i = 0; i < wolfJCEAlgos.length; i++) { - mac = Mac.getInstance(wolfJCEAlgos[i], "wolfJCE"); + for (int i = 0; i < enabledAlgos.size(); i++) { + mac = Mac.getInstance(enabledAlgos.get(i), "wolfJCE"); - if (mac.getMacLength() != wolfJCEMacLengths[i]) + if (mac.getMacLength() != enabledAlgoLengths.get(i)) fail("Expected MAC length did not match, " + - "algo = " + wolfJCEAlgos[i]); + "algo = " + enabledAlgos.get(i)); } } @@ -175,13 +200,20 @@ public class WolfCryptMacTest { SecretKeySpec keyspec = new SecretKeySpec(vectors[i].getKey(), "MD5"); - Mac mac = Mac.getInstance("HmacMD5", "wolfJCE"); - mac.init(keyspec); - mac.update(vectors[i].getInput()); + try { + Mac mac = Mac.getInstance("HmacMD5", "wolfJCE"); - byte out[] = mac.doFinal(); + mac.init(keyspec); + mac.update(vectors[i].getInput()); - assertArrayEquals(out, vectors[i].getOutput()); + byte out[] = mac.doFinal(); + + assertArrayEquals(out, vectors[i].getOutput()); + + } catch (NoSuchAlgorithmException e) { + /* skip test if not available */ + Assume.assumeTrue(false); + } } } @@ -263,13 +295,20 @@ public class WolfCryptMacTest { SecretKeySpec keyspec = new SecretKeySpec(vectors[i].getKey(), "SHA1"); - Mac mac = Mac.getInstance("HmacSHA1", "wolfJCE"); - mac.init(keyspec); - mac.update(vectors[i].getInput()); + try { + Mac mac = Mac.getInstance("HmacSHA1", "wolfJCE"); - byte out[] = mac.doFinal(); + mac.init(keyspec); + mac.update(vectors[i].getInput()); - assertArrayEquals(out, vectors[i].getOutput()); + byte out[] = mac.doFinal(); + + assertArrayEquals(out, vectors[i].getOutput()); + + } catch (NoSuchAlgorithmException e) { + /* skip test if not available */ + Assume.assumeTrue(false); + } } } @@ -360,13 +399,20 @@ public class WolfCryptMacTest { SecretKeySpec keyspec = new SecretKeySpec(vectors[i].getKey(), "SHA256"); - Mac mac = Mac.getInstance("HmacSHA256", "wolfJCE"); - mac.init(keyspec); - mac.update(vectors[i].getInput()); + try { + Mac mac = Mac.getInstance("HmacSHA256", "wolfJCE"); - byte out[] = mac.doFinal(); + mac.init(keyspec); + mac.update(vectors[i].getInput()); - assertArrayEquals(out, vectors[i].getOutput()); + byte out[] = mac.doFinal(); + + assertArrayEquals(out, vectors[i].getOutput()); + + } catch (NoSuchAlgorithmException e) { + /* skip test if not available */ + Assume.assumeTrue(false); + } } } @@ -469,13 +515,20 @@ public class WolfCryptMacTest { SecretKeySpec keyspec = new SecretKeySpec(vectors[i].getKey(), "SHA384"); - Mac mac = Mac.getInstance("HmacSHA384", "wolfJCE"); - mac.init(keyspec); - mac.update(vectors[i].getInput()); + try { + Mac mac = Mac.getInstance("HmacSHA384", "wolfJCE"); - byte out[] = mac.doFinal(); + mac.init(keyspec); + mac.update(vectors[i].getInput()); - assertArrayEquals(out, vectors[i].getOutput()); + byte out[] = mac.doFinal(); + + assertArrayEquals(out, vectors[i].getOutput()); + + } catch (NoSuchAlgorithmException e) { + /* skip test if not available */ + Assume.assumeTrue(false); + } } } @@ -590,13 +643,20 @@ public class WolfCryptMacTest { SecretKeySpec keyspec = new SecretKeySpec(vectors[i].getKey(), "SHA512"); - Mac mac = Mac.getInstance("HmacSHA512", "wolfJCE"); - mac.init(keyspec); - mac.update(vectors[i].getInput()); + try { + Mac mac = Mac.getInstance("HmacSHA512", "wolfJCE"); - byte out[] = mac.doFinal(); + mac.init(keyspec); + mac.update(vectors[i].getInput()); - assertArrayEquals(out, vectors[i].getOutput()); + byte out[] = mac.doFinal(); + + assertArrayEquals(out, vectors[i].getOutput()); + + } catch (NoSuchAlgorithmException e) { + /* skip test if not available */ + Assume.assumeTrue(false); + } } } diff --git a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestMd5Test.java b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestMd5Test.java index 0b81bb4..886eeeb 100644 --- a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestMd5Test.java +++ b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestMd5Test.java @@ -23,6 +23,7 @@ package com.wolfssl.provider.jce.test; import static org.junit.Assert.*; import org.junit.Test; +import org.junit.Assume; import org.junit.BeforeClass; import java.security.Security; @@ -32,24 +33,30 @@ import java.security.NoSuchProviderException; import java.security.NoSuchAlgorithmException; import com.wolfssl.provider.jce.WolfCryptProvider; +import com.wolfssl.wolfcrypt.FeatureDetect; public class WolfCryptMessageDigestMd5Test { @BeforeClass - public static void testProviderInstallationAtRuntime() { + public static void testProviderInstallationAtRuntime() + throws NoSuchProviderException { /* install wolfJCE provider at runtime */ Security.addProvider(new WolfCryptProvider()); Provider p = Security.getProvider("wolfJCE"); assertNotNull(p); - } - @Test - public void testGetMessageDigestFromProvider() - throws NoSuchProviderException, NoSuchAlgorithmException { - - MessageDigest md5 = MessageDigest.getInstance("MD5", "wolfJCE"); + try { + MessageDigest md5 = MessageDigest.getInstance("MD5", + "wolfJCE"); + } catch (NoSuchAlgorithmException e) { + /* if we also detect algo is compiled out, skip tests */ + if (FeatureDetect.Md5Enabled() == false) { + System.out.println("JSSE MD5 Test skipped"); + Assume.assumeTrue(false); + } + } } @Test diff --git a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha256Test.java b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha256Test.java index 0c4046a..dffd482 100644 --- a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha256Test.java +++ b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha256Test.java @@ -23,6 +23,7 @@ package com.wolfssl.provider.jce.test; import static org.junit.Assert.*; import org.junit.Test; +import org.junit.Assume; import org.junit.BeforeClass; import java.security.Security; @@ -32,24 +33,30 @@ import java.security.NoSuchProviderException; import java.security.NoSuchAlgorithmException; import com.wolfssl.provider.jce.WolfCryptProvider; +import com.wolfssl.wolfcrypt.FeatureDetect; public class WolfCryptMessageDigestSha256Test { @BeforeClass - public static void testProviderInstallationAtRuntime() { + public static void testProviderInstallationAtRuntime() + throws NoSuchProviderException { /* install wolfJCE provider at runtime */ Security.addProvider(new WolfCryptProvider()); Provider p = Security.getProvider("wolfJCE"); assertNotNull(p); - } - @Test - public void testGetMessageDigestFromProvider() - throws NoSuchProviderException, NoSuchAlgorithmException { - - MessageDigest sha256 = MessageDigest.getInstance("SHA-256", "wolfJCE"); + try { + MessageDigest sha256 = MessageDigest.getInstance("SHA-256", + "wolfJCE"); + } catch (NoSuchAlgorithmException e) { + /* if we also detect algo is compiled out, skip tests */ + if (FeatureDetect.Sha256Enabled() == false) { + System.out.println("JSSE SHA-256 Test skipped"); + Assume.assumeTrue(false); + } + } } @Test diff --git a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha384Test.java b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha384Test.java index f2a9e55..068884f 100644 --- a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha384Test.java +++ b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha384Test.java @@ -23,6 +23,7 @@ package com.wolfssl.provider.jce.test; import static org.junit.Assert.*; import org.junit.Test; +import org.junit.Assume; import org.junit.BeforeClass; import java.security.Security; @@ -32,24 +33,30 @@ import java.security.NoSuchProviderException; import java.security.NoSuchAlgorithmException; import com.wolfssl.provider.jce.WolfCryptProvider; +import com.wolfssl.wolfcrypt.FeatureDetect; public class WolfCryptMessageDigestSha384Test { @BeforeClass - public static void testProviderInstallationAtRuntime() { + public static void testProviderInstallationAtRuntime() + throws NoSuchProviderException { /* install wolfJCE provider at runtime */ Security.addProvider(new WolfCryptProvider()); Provider p = Security.getProvider("wolfJCE"); assertNotNull(p); - } - @Test - public void testGetMessageDigestFromProvider() - throws NoSuchProviderException, NoSuchAlgorithmException { - - MessageDigest sha384 = MessageDigest.getInstance("SHA-384", "wolfJCE"); + try { + MessageDigest sha384 = MessageDigest.getInstance("SHA-384", + "wolfJCE"); + } catch (NoSuchAlgorithmException e) { + /* if we also detect algo is compiled out, skip tests */ + if (FeatureDetect.Sha384Enabled() == false) { + System.out.println("JSSE SHA-384 Test skipped"); + Assume.assumeTrue(false); + } + } } @Test diff --git a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha512Test.java b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha512Test.java index 28ca6fc..44e24d5 100644 --- a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha512Test.java +++ b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha512Test.java @@ -23,6 +23,7 @@ package com.wolfssl.provider.jce.test; import static org.junit.Assert.*; import org.junit.Test; +import org.junit.Assume; import org.junit.BeforeClass; import java.security.Security; @@ -32,24 +33,30 @@ import java.security.NoSuchProviderException; import java.security.NoSuchAlgorithmException; import com.wolfssl.provider.jce.WolfCryptProvider; +import com.wolfssl.wolfcrypt.FeatureDetect; public class WolfCryptMessageDigestSha512Test { @BeforeClass - public static void testProviderInstallationAtRuntime() { + public static void testProviderInstallationAtRuntime() + throws NoSuchProviderException { /* install wolfJCE provider at runtime */ Security.addProvider(new WolfCryptProvider()); Provider p = Security.getProvider("wolfJCE"); assertNotNull(p); - } - @Test - public void testGetMessageDigestFromProvider() - throws NoSuchProviderException, NoSuchAlgorithmException { - - MessageDigest sha512 = MessageDigest.getInstance("SHA-512", "wolfJCE"); + try { + MessageDigest sha512 = MessageDigest.getInstance("SHA-512", + "wolfJCE"); + } catch (NoSuchAlgorithmException e) { + /* if we also detect algo is compiled out, skip tests */ + if (FeatureDetect.Sha512Enabled() == false) { + System.out.println("JSSE SHA-512 Test skipped"); + Assume.assumeTrue(false); + } + } } @Test diff --git a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestShaTest.java b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestShaTest.java index 0e4ee78..366bd2f 100644 --- a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestShaTest.java +++ b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestShaTest.java @@ -23,6 +23,7 @@ package com.wolfssl.provider.jce.test; import static org.junit.Assert.*; import org.junit.Test; +import org.junit.Assume; import org.junit.BeforeClass; import java.security.Security; @@ -32,24 +33,30 @@ import java.security.NoSuchProviderException; import java.security.NoSuchAlgorithmException; import com.wolfssl.provider.jce.WolfCryptProvider; +import com.wolfssl.wolfcrypt.FeatureDetect; public class WolfCryptMessageDigestShaTest { @BeforeClass - public static void testProviderInstallationAtRuntime() { + public static void testProviderInstallationAtRuntime() + throws NoSuchProviderException { /* install wolfJCE provider at runtime */ Security.addProvider(new WolfCryptProvider()); Provider p = Security.getProvider("wolfJCE"); assertNotNull(p); - } - @Test - public void testGetMessageDigestFromProvider() - throws NoSuchProviderException, NoSuchAlgorithmException { - - MessageDigest sha = MessageDigest.getInstance("SHA-1", "wolfJCE"); + try { + MessageDigest sha = MessageDigest.getInstance("SHA-1", + "wolfJCE"); + } catch (NoSuchAlgorithmException e) { + /* if we also detect algo is compiled out, skip tests */ + if (FeatureDetect.ShaEnabled() == false) { + System.out.println("JSSE SHA-1 Test skipped"); + Assume.assumeTrue(false); + } + } } @Test diff --git a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptSignatureTest.java b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptSignatureTest.java index a723ae2..ec4e226 100644 --- a/src/test/java/com/wolfssl/provider/jce/test/WolfCryptSignatureTest.java +++ b/src/test/java/com/wolfssl/provider/jce/test/WolfCryptSignatureTest.java @@ -25,6 +25,9 @@ import static org.junit.Assert.*; import org.junit.Test; import org.junit.BeforeClass; +import java.util.Arrays; +import java.util.ArrayList; + import java.security.Security; import java.security.Provider; import java.security.SecureRandom; @@ -45,7 +48,7 @@ import com.wolfssl.provider.jce.WolfCryptProvider; public class WolfCryptSignatureTest { - private String wolfJCEAlgos[] = { + private static String wolfJCEAlgos[] = { "SHA1withRSA", "SHA256withRSA", "SHA384withRSA", @@ -56,14 +59,31 @@ public class WolfCryptSignatureTest { "SHA512withECDSA" }; + private static ArrayList enabledAlgos = + new ArrayList(); + @BeforeClass - public static void testProviderInstallationAtRuntime() { + public static void testProviderInstallationAtRuntime() + throws NoSuchProviderException { + + Signature sig; /* install wolfJCE provider at runtime */ Security.addProvider(new WolfCryptProvider()); Provider p = Security.getProvider("wolfJCE"); assertNotNull(p); + + /* populate enabledAlgos, some native features may be + * compiled out */ + for (int i = 0; i < wolfJCEAlgos.length; i++) { + try { + sig = Signature.getInstance(wolfJCEAlgos[i], "wolfJCE"); + enabledAlgos.add(wolfJCEAlgos[i]); + } catch (NoSuchAlgorithmException e) { + /* algo not compiled in */ + } + } } @Test @@ -73,8 +93,8 @@ public class WolfCryptSignatureTest { Signature sig; /* try to get all available options we expect to have */ - for (int i = 0; i < wolfJCEAlgos.length; i++) { - sig = Signature.getInstance(wolfJCEAlgos[i], "wolfJCE"); + for (int i = 0; i < enabledAlgos.size(); i++) { + sig = Signature.getInstance(enabledAlgos.get(i), "wolfJCE"); } /* asking for a bad algo should throw an exception */ @@ -95,12 +115,12 @@ public class WolfCryptSignatureTest { byte[] toSignBuf = toSign.getBytes(); byte[] signature = null; - for (int i = 0; i < wolfJCEAlgos.length; i++) { + for (int i = 0; i < enabledAlgos.size(); i++) { Signature signer = - Signature.getInstance(wolfJCEAlgos[i], "wolfJCE"); + Signature.getInstance(enabledAlgos.get(i), "wolfJCE"); Signature verifier = - Signature.getInstance(wolfJCEAlgos[i], "wolfJCE"); + Signature.getInstance(enabledAlgos.get(i), "wolfJCE"); assertNotNull(signer); assertNotNull(verifier); @@ -110,7 +130,7 @@ public class WolfCryptSignatureTest { assertNotNull(rand); /* generate key pair */ - KeyPair pair = generateKeyPair(wolfJCEAlgos[i], rand); + KeyPair pair = generateKeyPair(enabledAlgos.get(i), rand); assertNotNull(pair); PrivateKey priv = pair.getPrivate(); @@ -144,12 +164,12 @@ public class WolfCryptSignatureTest { byte[] toSignBuf = toSign.getBytes(); byte[] signature = null; - for (int i = 0; i < wolfJCEAlgos.length; i++) { + for (int i = 0; i < enabledAlgos.size(); i++) { Signature signer = - Signature.getInstance(wolfJCEAlgos[i], "wolfJCE"); + Signature.getInstance(enabledAlgos.get(i), "wolfJCE"); Signature verifier = - Signature.getInstance(wolfJCEAlgos[i], "wolfJCE"); + Signature.getInstance(enabledAlgos.get(i), "wolfJCE"); assertNotNull(signer); assertNotNull(verifier); @@ -159,7 +179,7 @@ public class WolfCryptSignatureTest { assertNotNull(rand); /* generate key pair */ - KeyPair pair = generateKeyPair(wolfJCEAlgos[i], rand); + KeyPair pair = generateKeyPair(enabledAlgos.get(i), rand); assertNotNull(pair); PrivateKey priv = pair.getPrivate(); @@ -198,12 +218,12 @@ public class WolfCryptSignatureTest { byte[] toSignBuf = toSign.getBytes(); byte[] signature; - for (int i = 0; i < wolfJCEAlgos.length; i++) { + for (int i = 0; i < enabledAlgos.size(); i++) { Signature signer = - Signature.getInstance(wolfJCEAlgos[i], "wolfJCE"); + Signature.getInstance(enabledAlgos.get(i), "wolfJCE"); Signature verifier = - Signature.getInstance(wolfJCEAlgos[i]); + Signature.getInstance(enabledAlgos.get(i)); assertNotNull(signer); assertNotNull(verifier); @@ -220,7 +240,7 @@ public class WolfCryptSignatureTest { assertNotNull(rand); /* generate key pair */ - KeyPair pair = generateKeyPair(wolfJCEAlgos[i], rand); + KeyPair pair = generateKeyPair(enabledAlgos.get(i), rand); assertNotNull(pair); PrivateKey priv = pair.getPrivate(); @@ -254,12 +274,12 @@ public class WolfCryptSignatureTest { byte[] toSignBuf = toSign.getBytes(); byte[] signature; - for (int i = 0; i < wolfJCEAlgos.length; i++) { + for (int i = 0; i < enabledAlgos.size(); i++) { Signature signer = - Signature.getInstance(wolfJCEAlgos[i]); + Signature.getInstance(enabledAlgos.get(i)); Signature verifier = - Signature.getInstance(wolfJCEAlgos[i], "wolfJCE"); + Signature.getInstance(enabledAlgos.get(i), "wolfJCE"); assertNotNull(signer); assertNotNull(verifier); @@ -276,7 +296,7 @@ public class WolfCryptSignatureTest { assertNotNull(rand); /* generate key pair */ - KeyPair pair = generateKeyPair(wolfJCEAlgos[i], rand); + KeyPair pair = generateKeyPair(enabledAlgos.get(i), rand); assertNotNull(pair); PrivateKey priv = pair.getPrivate(); diff --git a/src/test/java/com/wolfssl/wolfcrypt/test/HmacTest.java b/src/test/java/com/wolfssl/wolfcrypt/test/HmacTest.java index 81dc0ff..0108fbd 100644 --- a/src/test/java/com/wolfssl/wolfcrypt/test/HmacTest.java +++ b/src/test/java/com/wolfssl/wolfcrypt/test/HmacTest.java @@ -54,7 +54,7 @@ public class HmacTest { @Test public void shaHmacShouldMatch() { - String[] keyVector = new String[] { + String[] keyVector = new String[] { "fd42f5044e3f70825102017f8521", "7da600a31369689ae60b73e30bd9", "c545cc0ef4adf1c98bd9e0e4ba04", @@ -74,23 +74,32 @@ public class HmacTest { "fb6788ccd29e16544e5b52c963279b6a9eb21537" }; for (int i = 0; i < dataVector.length; i++) { - Hmac hmac = new Hmac(); - byte[] key = Util.h2b(keyVector[i]); - byte[] data = Util.h2b(dataVector[i]); - byte[] expected = Util.h2b(hashVector[i]); + try { + Hmac hmac = new Hmac(); + byte[] key = Util.h2b(keyVector[i]); + byte[] data = Util.h2b(dataVector[i]); + byte[] expected = Util.h2b(hashVector[i]); - hmac.setKey(Hmac.SHA, key); + hmac.setKey(Hmac.SHA, key); - for (byte b : data) - hmac.update(b); + for (byte b : data) + hmac.update(b); - assertArrayEquals(expected, hmac.doFinal()); + assertArrayEquals(expected, hmac.doFinal()); - hmac.reset(); + hmac.reset(); - assertArrayEquals(expected, hmac.doFinal(data)); - } - } + assertArrayEquals(expected, hmac.doFinal(data)); + } catch (WolfCryptException e) { + if (e.getError() == WolfCryptError.NOT_COMPILED_IN) { + System.out.println("Hmac SHA1 test skipped: " + + e.getError()); + } else { + throw e; + } + } + } + } @Test public void sha256HmacShouldMatch() { @@ -115,23 +124,32 @@ public class HmacTest { "e35457edf1dc47f70be1e7dfd4f2332b704a0febd0dbca26a6bb63d1c7bef647" }; for (int i = 0; i < dataVector.length; i++) { - Hmac hmac = new Hmac(Hmac.SHA256, Util.h2b(keyVector[i])); - byte[] data = Util.h2b(dataVector[i]); - byte[] expected = Util.h2b(hashVector[i]); + try { + Hmac hmac = new Hmac(Hmac.SHA256, Util.h2b(keyVector[i])); + byte[] data = Util.h2b(dataVector[i]); + byte[] expected = Util.h2b(hashVector[i]); - hmac.update(data); + hmac.update(data); - assertArrayEquals(expected, hmac.doFinal()); + assertArrayEquals(expected, hmac.doFinal()); - assertEquals(expected.length, hmac.getMacLength()); + assertEquals(expected.length, hmac.getMacLength()); - hmac.reset(); + hmac.reset(); - buffer.put(data).rewind(); - hmac.update(buffer); - buffer.rewind(); + buffer.put(data).rewind(); + hmac.update(buffer); + buffer.rewind(); - assertArrayEquals(expected, hmac.doFinal()); + assertArrayEquals(expected, hmac.doFinal()); + } catch (WolfCryptException e) { + if (e.getError() == WolfCryptError.NOT_COMPILED_IN) { + System.out.println("Hmac SHA256 test skipped: " + + e.getError()); + } else { + throw e; + } + } } } @@ -157,22 +175,32 @@ public class HmacTest { "914551534846b871c2c70d5e30856021c2b1ff7490354e987423069db694de2f1e960ae84b341c2a0bf5301f7bc77ade" }; for (int i = 0; i < dataVector.length; i++) { - Hmac hmac = new Hmac(); - byte[] key = Util.h2b(keyVector[i]); - byte[] data = Util.h2b(dataVector[i]); - byte[] expected = Util.h2b(hashVector[i]); + try { + Hmac hmac = new Hmac(); + byte[] key = Util.h2b(keyVector[i]); + byte[] data = Util.h2b(dataVector[i]); + byte[] expected = Util.h2b(hashVector[i]); - hmac.setKey(Hmac.SHA384, key); + hmac.setKey(Hmac.SHA384, key); - /* first half */ - hmac.update(data, 0, data.length / 2); + /* first half */ + hmac.update(data, 0, data.length / 2); - /* second half */ - hmac.update(data, data.length / 2, data.length / 2); + /* second half */ + hmac.update(data, data.length / 2, data.length / 2); - assertArrayEquals(expected, hmac.doFinal()); + assertArrayEquals(expected, hmac.doFinal()); - assertEquals("HmacSHA384", hmac.getAlgorithm()); + assertEquals("HmacSHA384", hmac.getAlgorithm()); + + } catch (WolfCryptException e) { + if (e.getError() == WolfCryptError.NOT_COMPILED_IN) { + System.out.println("Hmac SHA384 test skipped: " + + e.getError()); + } else { + throw e; + } + } } } @@ -198,22 +226,31 @@ public class HmacTest { "74ec06a96c017463d3cbff306f45386b9b1c082592ee923206c3847dde7f11ff417cd9172a18c2040c877bbc58b5e057667f8136bdd39038addc1f0e8eabe3f0" }; for (int i = 0; i < dataVector.length; i++) { - Hmac hmac = new Hmac(); - byte[] key = Util.h2b(keyVector[i]); - byte[] data = Util.h2b(dataVector[i]); - byte[] expected = Util.h2b(hashVector[i]); + try { + Hmac hmac = new Hmac(); + byte[] key = Util.h2b(keyVector[i]); + byte[] data = Util.h2b(dataVector[i]); + byte[] expected = Util.h2b(hashVector[i]); - hmac.setKey(Hmac.SHA512, key); - hmac.update(data); + hmac.setKey(Hmac.SHA512, key); + hmac.update(data); - assertArrayEquals(expected, hmac.doFinal()); + assertArrayEquals(expected, hmac.doFinal()); - hmac.reset(); + hmac.reset(); - hmac.update(data); + hmac.update(data); - assertArrayEquals(expected, hmac.doFinal()); + assertArrayEquals(expected, hmac.doFinal()); + + } catch (WolfCryptException e) { + if (e.getError() == WolfCryptError.NOT_COMPILED_IN) { + System.out.println("Hmac SHA512 test skipped: " + + e.getError()); + } else { + throw e; + } + } } } - } diff --git a/src/test/java/com/wolfssl/wolfcrypt/test/Md5Test.java b/src/test/java/com/wolfssl/wolfcrypt/test/Md5Test.java index 32b67bd..302ccc2 100644 --- a/src/test/java/com/wolfssl/wolfcrypt/test/Md5Test.java +++ b/src/test/java/com/wolfssl/wolfcrypt/test/Md5Test.java @@ -24,12 +24,28 @@ package com.wolfssl.wolfcrypt.test; import static org.junit.Assert.*; import org.junit.Test; +import org.junit.Assume; +import org.junit.BeforeClass; import com.wolfssl.wolfcrypt.Md5; import com.wolfssl.wolfcrypt.NativeStruct; +import com.wolfssl.wolfcrypt.WolfCryptError; +import com.wolfssl.wolfcrypt.WolfCryptException; public class Md5Test { + @BeforeClass + public static void checkMd5IsAvailable() { + try { + Md5 md5 = new Md5(); + } catch (WolfCryptException e) { + if (e.getError() == WolfCryptError.NOT_COMPILED_IN) { + System.out.println("MD5 skipped: " + e.getError()); + Assume.assumeTrue(false); + } + } + } + @Test public void constructorShouldInitializeNativeStruct() { assertNotEquals(NativeStruct.NULL, new Md5().getNativeStruct()); diff --git a/src/test/java/com/wolfssl/wolfcrypt/test/Sha256Test.java b/src/test/java/com/wolfssl/wolfcrypt/test/Sha256Test.java index dad92f8..d40b37b 100644 --- a/src/test/java/com/wolfssl/wolfcrypt/test/Sha256Test.java +++ b/src/test/java/com/wolfssl/wolfcrypt/test/Sha256Test.java @@ -28,15 +28,31 @@ import java.nio.ByteBuffer; import javax.crypto.ShortBufferException; import org.junit.Test; +import org.junit.Assume; +import org.junit.BeforeClass; import com.wolfssl.wolfcrypt.Sha256; import com.wolfssl.wolfcrypt.NativeStruct; +import com.wolfssl.wolfcrypt.WolfCryptException; +import com.wolfssl.wolfcrypt.WolfCryptError; public class Sha256Test { private ByteBuffer data = ByteBuffer.allocateDirect(32); private ByteBuffer result = ByteBuffer.allocateDirect(Sha256.DIGEST_SIZE); private ByteBuffer expected = ByteBuffer.allocateDirect(Sha256.DIGEST_SIZE); + @BeforeClass + public static void checkSha256IsAvailable() { + try { + Sha256 sha = new Sha256(); + } catch (WolfCryptException e) { + if (e.getError() == WolfCryptError.NOT_COMPILED_IN) { + System.out.println("Sha256Test skipped: " + e.getError()); + Assume.assumeTrue(false); + } + } + } + @Test public void constructorShouldInitializeNativeStruct() { assertNotEquals(NativeStruct.NULL, new Sha256().getNativeStruct()); diff --git a/src/test/java/com/wolfssl/wolfcrypt/test/Sha384Test.java b/src/test/java/com/wolfssl/wolfcrypt/test/Sha384Test.java index a388ac9..a433036 100644 --- a/src/test/java/com/wolfssl/wolfcrypt/test/Sha384Test.java +++ b/src/test/java/com/wolfssl/wolfcrypt/test/Sha384Test.java @@ -28,15 +28,31 @@ import java.nio.ByteBuffer; import javax.crypto.ShortBufferException; import org.junit.Test; +import org.junit.Assume; +import org.junit.BeforeClass; import com.wolfssl.wolfcrypt.Sha384; import com.wolfssl.wolfcrypt.NativeStruct; +import com.wolfssl.wolfcrypt.WolfCryptException; +import com.wolfssl.wolfcrypt.WolfCryptError; public class Sha384Test { private ByteBuffer data = ByteBuffer.allocateDirect(32); private ByteBuffer result = ByteBuffer.allocateDirect(Sha384.DIGEST_SIZE); private ByteBuffer expected = ByteBuffer.allocateDirect(Sha384.DIGEST_SIZE); + @BeforeClass + public static void checkSha384IsAvailable() { + try { + Sha384 sha = new Sha384(); + } catch (WolfCryptException e) { + if (e.getError() == WolfCryptError.NOT_COMPILED_IN) { + System.out.println("Sha384Test skipped: " + e.getError()); + Assume.assumeTrue(false); + } + } + } + @Test public void constructorShouldInitializeNativeStruct() { assertNotEquals(NativeStruct.NULL, new Sha384().getNativeStruct()); diff --git a/src/test/java/com/wolfssl/wolfcrypt/test/Sha512Test.java b/src/test/java/com/wolfssl/wolfcrypt/test/Sha512Test.java index a6a6262..2c3624f 100644 --- a/src/test/java/com/wolfssl/wolfcrypt/test/Sha512Test.java +++ b/src/test/java/com/wolfssl/wolfcrypt/test/Sha512Test.java @@ -28,15 +28,31 @@ import java.nio.ByteBuffer; import javax.crypto.ShortBufferException; import org.junit.Test; +import org.junit.Assume; +import org.junit.BeforeClass; import com.wolfssl.wolfcrypt.Sha512; import com.wolfssl.wolfcrypt.NativeStruct; +import com.wolfssl.wolfcrypt.WolfCryptException; +import com.wolfssl.wolfcrypt.WolfCryptError; public class Sha512Test { private ByteBuffer data = ByteBuffer.allocateDirect(32); private ByteBuffer result = ByteBuffer.allocateDirect(Sha512.DIGEST_SIZE); private ByteBuffer expected = ByteBuffer.allocateDirect(Sha512.DIGEST_SIZE); + @BeforeClass + public static void checkSha512IsAvailable() { + try { + Sha512 sha = new Sha512(); + } catch (WolfCryptException e) { + if (e.getError() == WolfCryptError.NOT_COMPILED_IN) { + System.out.println("Sha512Test skipped: " + e.getError()); + Assume.assumeTrue(false); + } + } + } + @Test public void constructorShouldInitializeNativeStruct() { assertNotEquals(NativeStruct.NULL, new Sha512().getNativeStruct()); diff --git a/src/test/java/com/wolfssl/wolfcrypt/test/ShaTest.java b/src/test/java/com/wolfssl/wolfcrypt/test/ShaTest.java index e34c49c..0eff306 100644 --- a/src/test/java/com/wolfssl/wolfcrypt/test/ShaTest.java +++ b/src/test/java/com/wolfssl/wolfcrypt/test/ShaTest.java @@ -28,15 +28,31 @@ import java.nio.ByteBuffer; import javax.crypto.ShortBufferException; import org.junit.Test; +import org.junit.Assume; +import org.junit.BeforeClass; import com.wolfssl.wolfcrypt.Sha; import com.wolfssl.wolfcrypt.NativeStruct; +import com.wolfssl.wolfcrypt.WolfCryptException; +import com.wolfssl.wolfcrypt.WolfCryptError; public class ShaTest { private ByteBuffer data = ByteBuffer.allocateDirect(32); private ByteBuffer result = ByteBuffer.allocateDirect(Sha.DIGEST_SIZE); private ByteBuffer expected = ByteBuffer.allocateDirect(Sha.DIGEST_SIZE); + @BeforeClass + public static void checkShaIsAvailable() { + try { + Sha sha = new Sha(); + } catch (WolfCryptException e) { + if (e.getError() == WolfCryptError.NOT_COMPILED_IN) { + System.out.println("ShaTest skipped: " + e.getError()); + Assume.assumeTrue(false); + } + } + } + @Test public void constructorShouldInitializeNativeStruct() { assertNotEquals(NativeStruct.NULL, new Sha().getNativeStruct());