Merge pull request #104 from cconlon/sha224

Add SHA-224 support to `MessageDigest`, `Mac`, `Signature`, `KeyGenerator`
pull/105/head
JacobBarthelmeh 2025-03-05 14:06:46 -07:00 committed by GitHub
commit 613e4f4914
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 1612 additions and 29 deletions

View File

@ -88,6 +88,7 @@ The JCE provider currently supports the following algorithms:
MessageDigest Class
MD5
SHA-1
SHA-224
SHA-256
SHA-384
SHA-512
@ -107,6 +108,7 @@ The JCE provider currently supports the following algorithms:
Mac Class
HmacMD5
HmacSHA1
HmacSHA224
HmacSHA256
HmacSHA384
HmacSHA512
@ -114,10 +116,12 @@ The JCE provider currently supports the following algorithms:
Signature Class
MD5withRSA
SHA1withRSA
SHA224withRSA
SHA256withRSA
SHA384withRSA
SHA512withRSA
SHA1withECDSA
SHA224withECDSA
SHA256withECDSA
SHA384withECDSA
SHA512withECDSA
@ -130,6 +134,7 @@ The JCE provider currently supports the following algorithms:
KeyGenerator
AES
HmacSHA1
HmacSHA224
HmacSHA256
HmacSHA384
HmacSHA512

View File

@ -73,6 +73,14 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeMd5
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha
(JNIEnv *, jclass);
/*
* Class: com_wolfssl_wolfcrypt_Hmac
* Method: getCodeSha224
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha224
(JNIEnv *, jclass);
/*
* Class: com_wolfssl_wolfcrypt_Hmac
* Method: getCodeSha256

View File

@ -0,0 +1,75 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_wolfssl_wolfcrypt_Sha224 */
#ifndef _Included_com_wolfssl_wolfcrypt_Sha224
#define _Included_com_wolfssl_wolfcrypt_Sha224
#ifdef __cplusplus
extern "C" {
#endif
#undef com_wolfssl_wolfcrypt_Sha224_NULL
#define com_wolfssl_wolfcrypt_Sha224_NULL 0LL
#undef com_wolfssl_wolfcrypt_Sha224_TYPE
#define com_wolfssl_wolfcrypt_Sha224_TYPE 5L
#undef com_wolfssl_wolfcrypt_Sha224_DIGEST_SIZE
#define com_wolfssl_wolfcrypt_Sha224_DIGEST_SIZE 28L
/*
* Class: com_wolfssl_wolfcrypt_Sha224
* Method: mallocNativeStruct_internal
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_com_wolfssl_wolfcrypt_Sha224_mallocNativeStruct_1internal
(JNIEnv *, jobject);
/*
* Class: com_wolfssl_wolfcrypt_Sha224
* Method: native_init_internal
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1init_1internal
(JNIEnv *, jobject);
/*
* Class: com_wolfssl_wolfcrypt_Sha224
* Method: native_copy_internal
* Signature: (Lcom/wolfssl/wolfcrypt/Sha224;)V
*/
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1copy_1internal
(JNIEnv *, jobject, jobject);
/*
* Class: com_wolfssl_wolfcrypt_Sha224
* Method: native_update_internal
* Signature: (Ljava/nio/ByteBuffer;II)V
*/
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1update_1internal__Ljava_nio_ByteBuffer_2II
(JNIEnv *, jobject, jobject, jint, jint);
/*
* Class: com_wolfssl_wolfcrypt_Sha224
* Method: native_update_internal
* Signature: ([BII)V
*/
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1update_1internal___3BII
(JNIEnv *, jobject, jbyteArray, jint, jint);
/*
* Class: com_wolfssl_wolfcrypt_Sha224
* Method: native_final_internal
* Signature: (Ljava/nio/ByteBuffer;I)V
*/
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1final_1internal__Ljava_nio_ByteBuffer_2I
(JNIEnv *, jobject, jobject, jint);
/*
* Class: com_wolfssl_wolfcrypt_Sha224
* Method: native_final_internal
* Signature: ([B)V
*/
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1final_1internal___3B
(JNIEnv *, jobject, jbyteArray);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -46,6 +46,9 @@
#ifndef NO_SHA
#define SHA_DIGEST_SIZE WC_SHA_DIGEST_SIZE
#endif
#ifdef WOLFSSL_SHA224
#define SHA224_DIGEST_SIZE WC_SHA224_DIGEST_SIZE
#endif
#ifndef NO_SHA256
#define SHA256_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
#endif
@ -61,44 +64,44 @@
/* copy from cyassl/hmac.c */
static WC_INLINE int GetHashSizeByType(int type)
{
if (!(type == WC_MD5 || type == WC_SHA || type == WC_SHA256
|| type == WC_SHA384 || type == WC_SHA512))
if (!(type == WC_MD5 || type == WC_SHA || type == WC_SHA224
|| type == WC_SHA256 || type == WC_SHA384 || type == WC_SHA512)) {
return BAD_FUNC_ARG;
}
switch (type) {
#ifndef NO_MD5
case WC_MD5:
return MD5_DIGEST_SIZE;
break;
#endif
#ifndef NO_SHA
case WC_SHA:
return SHA_DIGEST_SIZE;
break;
#endif
#ifdef WOLFSSL_SHA224
case WC_SHA224:
return SHA224_DIGEST_SIZE;
#endif
#ifndef NO_SHA256
case WC_SHA256:
return SHA256_DIGEST_SIZE;
break;
#endif
#if defined(CYASSL_SHA384) || defined(WOLFSSL_SHA384)
case WC_SHA384:
return SHA384_DIGEST_SIZE;
break;
#endif
#if defined(CYASSL_SHA512) || defined(WOLFSSL_SHA512)
case WC_SHA512:
return SHA512_DIGEST_SIZE;
break;
#endif
default:
return BAD_FUNC_ARG;
break;
}
}
@ -355,6 +358,20 @@ Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha(
#endif
}
JNIEXPORT jint JNICALL
Java_com_wolfssl_wolfcrypt_Hmac_getCodeSha224(
JNIEnv* env, jobject this)
{
#ifdef WOLFSSL_SHA224
jint result = WC_SHA224;
LogStr("WC_SHA224 = %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)

View File

@ -32,6 +32,10 @@
#include <wolfssl/wolfcrypt/sha512.h>
#include <com_wolfssl_wolfcrypt_Sha.h>
#include <com_wolfssl_wolfcrypt_Sha224.h>
#include <com_wolfssl_wolfcrypt_Sha256.h>
#include <com_wolfssl_wolfcrypt_Sha384.h>
#include <com_wolfssl_wolfcrypt_Sha512.h>
#include <wolfcrypt_jni_NativeStruct.h>
#include <wolfcrypt_jni_error.h>
@ -45,6 +49,12 @@
#define SHA_DIGEST_SIZE WC_SHA_DIGEST_SIZE
#define SHA_PAD_SIZE WC_SHA_PAD_SIZE
#endif
#ifndef NO_SHA224
#define Sha224 wc_Sha224
#define SHA224_BLOCK_SIZE WC_SHA224_BLOCK_SIZE
#define SHA224_DIGEST_SIZE WC_SHA224_DIGEST_SIZE
#define SHA224_PAD_SIZE WC_SHA224_PAD_SIZE
#endif
#ifndef NO_SHA256
#define Sha256 wc_Sha256
#define SHA256_BLOCK_SIZE WC_SHA256_BLOCK_SIZE
@ -90,6 +100,32 @@ Java_com_wolfssl_wolfcrypt_Sha_mallocNativeStruct_1internal(
#endif
}
JNIEXPORT jlong JNICALL Java_com_wolfssl_wolfcrypt_Sha224_mallocNativeStruct_1internal
(JNIEnv* env, jobject this)
{
#ifdef WOLFSSL_SHA224
Sha224* sha = NULL;
sha = (Sha224*) XMALLOC(sizeof(Sha224), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (sha == NULL) {
throwOutOfMemoryException(env, "Failed to allocate Sha224 object");
}
else {
XMEMSET(sha, 0, sizeof(Sha224));
}
LogStr("new Sha224() = %p\n", sha);
return (jlong)(uintptr_t)sha;
#else
(void)env;
(void)this;
throwNotCompiledInException(env);
return (jlong)0;
#endif
}
JNIEXPORT jlong JNICALL
Java_com_wolfssl_wolfcrypt_Sha256_mallocNativeStruct_1internal(
JNIEnv* env, jobject this)
@ -946,3 +982,231 @@ Java_com_wolfssl_wolfcrypt_Sha512_native_1final_1internal___3B(
#endif
}
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1init_1internal
(JNIEnv* env, jobject this)
{
#ifdef WOLFSSL_SHA224
int ret = 0;
Sha224* sha = (Sha224*) getNativeStruct(env, this);
if ((*env)->ExceptionOccurred(env)) {
/* getNativeStruct may throw exception, prevent throwing another */
return;
}
if (sha == NULL) {
ret = BAD_FUNC_ARG;
}
else {
ret = wc_InitSha224(sha);
}
if (ret != 0) {
throwWolfCryptExceptionFromError(env, ret);
}
#else
(void)env;
(void)this;
throwNotCompiledInException(env);
#endif
}
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1copy_1internal
(JNIEnv* env, jobject this, jobject toBeCopied)
{
#ifdef WOLFSSL_SHA224
int ret = 0;
Sha224* sha = NULL;
Sha224* tbc = NULL; /* tbc = to be copied */
if (this == NULL || toBeCopied == NULL) {
throwWolfCryptExceptionFromError(env, BAD_FUNC_ARG);
return;
}
sha = (Sha224*) getNativeStruct(env, this);
if ((*env)->ExceptionOccurred(env)) {
/* getNativeStruct may throw exception, prevent throwing another */
return;
}
tbc = (Sha224*) getNativeStruct(env, toBeCopied);
if ((*env)->ExceptionOccurred(env)) {
/* getNativeStruct may throw exception, prevent throwing another */
return;
}
ret = wc_Sha224Copy(tbc, sha);
if (ret != 0) {
throwWolfCryptExceptionFromError(env, ret);
}
#else
(void)env;
(void)this;
throwNotCompiledInException(env);
#endif
}
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1update_1internal__Ljava_nio_ByteBuffer_2II
(JNIEnv* env, jobject this, jobject data_buffer, jint position, jint len)
{
#ifdef WOLFSSL_SHA224
int ret = 0;
Sha224* sha = NULL;
byte* data = NULL;
sha = (Sha224*) getNativeStruct(env, this);
if ((*env)->ExceptionOccurred(env)) {
/* getNativeStruct may throw exception, prevent throwing another */
return;
}
data = getDirectBufferAddress(env, data_buffer);
if (sha == NULL || data == NULL) {
ret = BAD_FUNC_ARG;
}
else {
ret = wc_Sha224Update(sha, data + position, len);
}
if (ret != 0) {
throwWolfCryptExceptionFromError(env, ret);
}
LogStr("wc_Sha224Update(sha=%p, data, len) = %d\n", sha, ret);
LogStr("data[%u]: [%p]\n", (word32)len, data);
LogHex(data, 0, len);
#else
(void)env;
(void)this;
(void)data_buffer;
(void)position;
(void)len;
throwNotCompiledInException(env);
#endif
}
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1update_1internal___3BII
(JNIEnv* env, jobject this, jbyteArray data_buffer, jint offset, jint len)
{
#ifdef WOLFSSL_SHA224
int ret = 0;
Sha224* sha = NULL;
byte* data = NULL;
word32 dataSz = 0;
sha = (Sha224*) getNativeStruct(env, this);
if ((*env)->ExceptionOccurred(env)) {
/* getNativeStruct may throw exception, prevent throwing another */
return;
}
data = getByteArray(env, data_buffer);
dataSz = getByteArrayLength(env, data_buffer);
if (sha == NULL || data == NULL ||
(word32)(offset + len) > dataSz) {
ret = BAD_FUNC_ARG;
}
else {
ret = wc_Sha224Update(sha, data + offset, len);
}
if (ret != 0) {
throwWolfCryptExceptionFromError(env, ret);
}
LogStr("wc_Sha224Update(sha=%p, data, len) = %d\n", sha, ret);
LogStr("data[%u]: [%p]\n", (word32)len, data + offset);
LogHex(data, offset, len);
releaseByteArray(env, data_buffer, data, JNI_ABORT);
#else
(void)env;
(void)this;
(void)data_buffer;
(void)offset;
(void)len;
throwNotCompiledInException(env);
#endif
}
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1final_1internal__Ljava_nio_ByteBuffer_2I
(JNIEnv* env, jobject this, jobject hash_buffer, jint position)
{
#ifdef WOLFSSL_SHA224
int ret = 0;
Sha224* sha = NULL;
byte* hash = NULL;
sha = (Sha224*) getNativeStruct(env, this);
if ((*env)->ExceptionOccurred(env)) {
/* getNativeStruct may throw exception, prevent throwing another */
return;
}
hash = getDirectBufferAddress(env, hash_buffer);
if (sha == NULL || hash == NULL) {
ret = BAD_FUNC_ARG;
}
else {
ret = wc_Sha224Final(sha, hash + position);
}
if (ret != 0) {
throwWolfCryptExceptionFromError(env, ret);
}
LogStr("wc_Sha224Final(sha=%p, hash) = %d\n", sha, ret);
LogStr("hash[%u]: [%p]\n", (word32)SHA224_DIGEST_SIZE, hash);
LogHex(hash, 0, SHA224_DIGEST_SIZE);
#else
(void)env;
(void)this;
(void)hash_buffer;
(void)position;
throwNotCompiledInException(env);
#endif
}
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Sha224_native_1final_1internal___3B
(JNIEnv* env, jobject this, jbyteArray hash_buffer)
{
#ifdef WOLFSSL_SHA224
int ret = 0;
Sha224* sha = NULL;
byte* hash = NULL;
sha = (Sha224*) getNativeStruct(env, this);
if ((*env)->ExceptionOccurred(env)) {
/* getNativeStruct may throw exception, prevent throwing another */
return;
}
hash = getByteArray(env, hash_buffer);
if (sha == NULL || hash == NULL) {
ret = BAD_FUNC_ARG;
}
else {
ret = wc_Sha224Final(sha, hash);
}
if (ret != 0) {
throwWolfCryptExceptionFromError(env, ret);
}
LogStr("wc_Sha224Final(sha=%p, hash) = %d\n", sha, ret);
LogStr("hash[%u]: [%p]\n", (word32)SHA224_DIGEST_SIZE, hash);
LogHex(hash, 0, SHA224_DIGEST_SIZE);
releaseByteArray(env, hash_buffer, hash, ret);
#else
(void)env;
(void)this;
(void)hash_buffer;
throwNotCompiledInException(env);
#endif
}

View File

@ -53,6 +53,7 @@ infer --fail-on-issue run -- javac \
src/main/java/com/wolfssl/wolfcrypt/Rng.java \
src/main/java/com/wolfssl/wolfcrypt/Rsa.java \
src/main/java/com/wolfssl/wolfcrypt/Sha.java \
src/main/java/com/wolfssl/wolfcrypt/Sha224.java \
src/main/java/com/wolfssl/wolfcrypt/Sha256.java \
src/main/java/com/wolfssl/wolfcrypt/Sha384.java \
src/main/java/com/wolfssl/wolfcrypt/Sha512.java \
@ -70,6 +71,7 @@ infer --fail-on-issue run -- javac \
src/main/java/com/wolfssl/provider/jce/WolfCryptMac.java \
src/main/java/com/wolfssl/provider/jce/WolfCryptMessageDigestMd5.java \
src/main/java/com/wolfssl/provider/jce/WolfCryptMessageDigestSha.java \
src/main/java/com/wolfssl/provider/jce/WolfCryptMessageDigestSha224.java \
src/main/java/com/wolfssl/provider/jce/WolfCryptMessageDigestSha256.java \
src/main/java/com/wolfssl/provider/jce/WolfCryptMessageDigestSha384.java \
src/main/java/com/wolfssl/provider/jce/WolfCryptMessageDigestSha512.java \

View File

@ -23,6 +23,7 @@ package com.wolfssl.provider.jce;
import com.wolfssl.wolfcrypt.Fips;
import com.wolfssl.wolfcrypt.Aes;
import com.wolfssl.wolfcrypt.Sha224;
import com.wolfssl.wolfcrypt.Sha256;
import com.wolfssl.wolfcrypt.Sha384;
import com.wolfssl.wolfcrypt.Sha512;
@ -45,6 +46,7 @@ public class WolfCryptKeyGenerator extends KeyGeneratorSpi {
WC_INVALID,
WC_AES,
WC_HMAC_SHA1,
WC_HMAC_SHA224,
WC_HMAC_SHA256,
WC_HMAC_SHA384,
WC_HMAC_SHA512
@ -54,7 +56,6 @@ public class WolfCryptKeyGenerator extends KeyGeneratorSpi {
private String algString = null;
private int keySizeBits = 0;
private AlgorithmParameterSpec algoParams = null;
private SecureRandom random = null;
/**
@ -75,6 +76,10 @@ public class WolfCryptKeyGenerator extends KeyGeneratorSpi {
/* SunJCE default key size for HmacSHA1 is 64 bytes */
this.keySizeBits = (Sha512.DIGEST_SIZE * 8);
break;
case WC_HMAC_SHA224:
this.algString = "HmacSHA224";
this.keySizeBits = (Sha224.DIGEST_SIZE * 8);
break;
case WC_HMAC_SHA256:
this.algString = "HmacSHA256";
this.keySizeBits = (Sha256.DIGEST_SIZE * 8);
@ -222,6 +227,7 @@ public class WolfCryptKeyGenerator extends KeyGeneratorSpi {
switch (this.algoType) {
case WC_AES:
case WC_HMAC_SHA1:
case WC_HMAC_SHA224:
case WC_HMAC_SHA256:
case WC_HMAC_SHA384:
case WC_HMAC_SHA512:
@ -259,6 +265,20 @@ public class WolfCryptKeyGenerator extends KeyGeneratorSpi {
}
}
/**
* KeyGenerator(HmacSHA224) class, called by WolfCryptProvider.
*/
public static final class wcHMACSha224KeyGenerator
extends WolfCryptKeyGenerator {
/**
* Constructor for wcHMACSha224KeyGenerator.
*/
public wcHMACSha224KeyGenerator() {
super(AlgoType.WC_HMAC_SHA224);
}
}
/**
* KeyGenerator(HmacSHA256) class, called by WolfCryptProvider.
*/

View File

@ -31,6 +31,7 @@ import javax.crypto.SecretKey;
import com.wolfssl.wolfcrypt.Md5;
import com.wolfssl.wolfcrypt.Sha;
import com.wolfssl.wolfcrypt.Sha224;
import com.wolfssl.wolfcrypt.Sha256;
import com.wolfssl.wolfcrypt.Sha384;
import com.wolfssl.wolfcrypt.Sha512;
@ -44,6 +45,7 @@ public class WolfCryptMac extends MacSpi {
enum HmacType {
WC_HMAC_MD5,
WC_HMAC_SHA,
WC_HMAC_SHA224,
WC_HMAC_SHA256,
WC_HMAC_SHA384,
WC_HMAC_SHA512
@ -72,6 +74,11 @@ public class WolfCryptMac extends MacSpi {
this.nativeHmacType = Hmac.SHA;
break;
case WC_HMAC_SHA224:
this.digestSize = Sha224.DIGEST_SIZE;
this.nativeHmacType = Hmac.SHA224;
break;
case WC_HMAC_SHA256:
this.digestSize = Sha256.DIGEST_SIZE;
this.nativeHmacType = Hmac.SHA256;
@ -163,6 +170,8 @@ public class WolfCryptMac extends MacSpi {
return "MD5";
case WC_HMAC_SHA:
return "SHA";
case WC_HMAC_SHA224:
return "SHA224";
case WC_HMAC_SHA256:
return "SHA256";
case WC_HMAC_SHA384:
@ -219,6 +228,21 @@ public class WolfCryptMac extends MacSpi {
}
}
/**
* wolfJCE HMAC-SHA2-224 class
*/
public static final class wcHmacSHA224 extends WolfCryptMac {
/**
* Create new wcHmacSHA224 object
*
* @throws NoSuchAlgorithmException if HMAC-SHA2-224 is not available at
* native wolfCrypt level.
*/
public wcHmacSHA224() throws NoSuchAlgorithmException {
super(HmacType.WC_HMAC_SHA224);
}
}
/**
* wolfJCE HMAC-SHA2-256 class
*/

View File

@ -0,0 +1,127 @@
/* WolfCryptMessageDigestSha224.java
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* 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-1335, USA
*/
package com.wolfssl.provider.jce;
import java.security.MessageDigestSpi;
import javax.crypto.ShortBufferException;
import com.wolfssl.wolfcrypt.Sha224;
/**
* wolfCrypt JCE SHA2-224 MessageDigest wrapper
*/
public final class WolfCryptMessageDigestSha224
extends MessageDigestSpi implements Cloneable {
/* internal reference to wolfCrypt JNI Sha object */
private Sha224 sha;
/**
* Create new WolfCryptMessageDigestSha224 object
*/
public WolfCryptMessageDigestSha224() {
sha = new Sha224();
sha.init();
}
/**
* Create new WolfCryptMessageDigestSha224 based on existing Sha224 object.
* Existing object should already be initialized.
*
* @param sha initialized Sha224 object to be used with this MessageDigest
*/
private WolfCryptMessageDigestSha224(Sha224 sha) {
this.sha = sha;
}
@Override
protected byte[] engineDigest() {
byte[] digest = new byte[Sha224.DIGEST_SIZE];
try {
this.sha.digest(digest);
} catch (ShortBufferException e) {
throw new RuntimeException(e.getMessage());
}
log("generated final digest, len: " + digest.length);
return digest;
}
@Override
protected void engineReset() {
this.sha.init();
log("engine reset");
}
@Override
protected void engineUpdate(byte input) {
byte[] tmp = new byte[1];
tmp[0] = input;
this.sha.update(tmp, 1);
log("update with single byte");
}
@Override
protected void engineUpdate(byte[] input, int offset, int len) {
this.sha.update(input, offset, len);
log("update, offset: " + offset + ", len: " + len);
}
@Override
protected int engineGetDigestLength() {
return this.sha.digestSize();
}
private void log(String msg) {
WolfCryptDebug.print("[MessageDigest, SHA224] " + msg);
}
@Override
public Object clone() {
Sha224 shaCopy = (Sha224)this.sha.clone();
return new WolfCryptMessageDigestSha224(shaCopy);
}
@SuppressWarnings("deprecation")
@Override
protected void finalize() throws Throwable {
try {
if (this.sha != null)
this.sha.releaseNativeStruct();
} finally {
super.finalize();
}
}
}

View File

@ -78,6 +78,10 @@ public final class WolfCryptProvider extends Provider {
put("MessageDigest.SHA-1",
"com.wolfssl.provider.jce.WolfCryptMessageDigestSha");
}
if (FeatureDetect.Sha224Enabled()) {
put("MessageDigest.SHA-224",
"com.wolfssl.provider.jce.WolfCryptMessageDigestSha224");
}
if (FeatureDetect.Sha256Enabled()) {
put("MessageDigest.SHA-256",
"com.wolfssl.provider.jce.WolfCryptMessageDigestSha256");
@ -110,6 +114,12 @@ public final class WolfCryptProvider extends Provider {
put("Signature.SHA1withECDSA",
"com.wolfssl.provider.jce.WolfCryptSignature$wcSHA1wECDSA");
}
if (FeatureDetect.Sha224Enabled()) {
put("Signature.SHA224withRSA",
"com.wolfssl.provider.jce.WolfCryptSignature$wcSHA224wRSA");
put("Signature.SHA224withECDSA",
"com.wolfssl.provider.jce.WolfCryptSignature$wcSHA224wECDSA");
}
if (FeatureDetect.Sha256Enabled()) {
put("Signature.SHA256withRSA",
"com.wolfssl.provider.jce.WolfCryptSignature$wcSHA256wRSA");
@ -138,6 +148,10 @@ public final class WolfCryptProvider extends Provider {
put("Mac.HmacSHA1",
"com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA1");
}
if (FeatureDetect.HmacSha224Enabled()) {
put("Mac.HmacSHA224",
"com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA224");
}
if (FeatureDetect.HmacSha256Enabled()) {
put("Mac.HmacSHA256",
"com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA256");
@ -187,16 +201,30 @@ public final class WolfCryptProvider extends Provider {
}
/* KeyGenerator */
put("KeyGenerator.AES",
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcAESKeyGenerator");
put("KeyGenerator.HmacSHA1",
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha1KeyGenerator");
put("KeyGenerator.HmacSHA256",
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha256KeyGenerator");
put("KeyGenerator.HmacSHA384",
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha384KeyGenerator");
put("KeyGenerator.HmacSHA512",
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha512KeyGenerator");
if (FeatureDetect.AesEnabled()) {
put("KeyGenerator.AES",
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcAESKeyGenerator");
}
if (FeatureDetect.HmacShaEnabled()) {
put("KeyGenerator.HmacSHA1",
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha1KeyGenerator");
}
if (FeatureDetect.HmacSha224Enabled()) {
put("KeyGenerator.HmacSHA224",
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha224KeyGenerator");
}
if (FeatureDetect.HmacSha256Enabled()) {
put("KeyGenerator.HmacSHA256",
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha256KeyGenerator");
}
if (FeatureDetect.HmacSha384Enabled()) {
put("KeyGenerator.HmacSHA384",
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha384KeyGenerator");
}
if (FeatureDetect.HmacSha512Enabled()) {
put("KeyGenerator.HmacSHA512",
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha512KeyGenerator");
}
/* KeyPairGenerator */
if (FeatureDetect.RsaKeyGenEnabled()) {

View File

@ -39,6 +39,7 @@ import javax.crypto.ShortBufferException;
import com.wolfssl.wolfcrypt.Asn;
import com.wolfssl.wolfcrypt.Md5;
import com.wolfssl.wolfcrypt.Sha;
import com.wolfssl.wolfcrypt.Sha224;
import com.wolfssl.wolfcrypt.Sha256;
import com.wolfssl.wolfcrypt.Sha384;
import com.wolfssl.wolfcrypt.Sha512;
@ -60,14 +61,16 @@ public class WolfCryptSignature extends SignatureSpi {
enum DigestType {
WC_MD5,
WC_SHA1,
WC_SHA224,
WC_SHA256,
WC_SHA384,
WC_SHA512
}
/* internal hash type sums */
/* internal hash type sums (asn.h) */
private int MD5h = 649;
private int SHAh = 88;
private int SHA224h = 417;
private int SHA256h = 414;
private int SHA384h = 415;
private int SHA512h = 416;
@ -79,6 +82,7 @@ public class WolfCryptSignature extends SignatureSpi {
/* internal hash objects */
private Md5 md5 = null;
private Sha sha = null;
private Sha224 sha224 = null;
private Sha256 sha256 = null;
private Sha384 sha384 = null;
private Sha512 sha512 = null;
@ -127,6 +131,12 @@ public class WolfCryptSignature extends SignatureSpi {
this.internalHashSum = SHAh;
break;
case WC_SHA224:
this.sha224 = new Sha224();
this.digestSz = Sha224.DIGEST_SIZE;
this.internalHashSum = SHA224h;
break;
case WC_SHA256:
this.sha256 = new Sha256();
this.digestSz = Sha256.DIGEST_SIZE;
@ -255,6 +265,10 @@ public class WolfCryptSignature extends SignatureSpi {
this.sha.init();
break;
case WC_SHA224:
this.sha224.init();
break;
case WC_SHA256:
this.sha256.init();
break;
@ -321,6 +335,10 @@ public class WolfCryptSignature extends SignatureSpi {
this.sha.init();
break;
case WC_SHA224:
this.sha224.init();
break;
case WC_SHA256:
this.sha256.init();
break;
@ -366,6 +384,10 @@ public class WolfCryptSignature extends SignatureSpi {
this.sha.digest(digest);
break;
case WC_SHA224:
this.sha224.digest(digest);
break;
case WC_SHA256:
this.sha256.digest(digest);
break;
@ -452,6 +474,10 @@ public class WolfCryptSignature extends SignatureSpi {
this.sha.update(b, off, len);
break;
case WC_SHA224:
this.sha224.update(b, off, len);
break;
case WC_SHA256:
this.sha256.update(b, off, len);
break;
@ -490,6 +516,10 @@ public class WolfCryptSignature extends SignatureSpi {
this.sha.digest(digest);
break;
case WC_SHA224:
this.sha224.digest(digest);
break;
case WC_SHA256:
this.sha256.digest(digest);
break;
@ -581,6 +611,8 @@ public class WolfCryptSignature extends SignatureSpi {
return "MD5";
case WC_SHA1:
return "SHA";
case WC_SHA224:
return "SHA224";
case WC_SHA256:
return "SHA256";
case WC_SHA384:
@ -608,6 +640,9 @@ public class WolfCryptSignature extends SignatureSpi {
if (this.sha != null)
this.sha.releaseNativeStruct();
if (this.sha224 != null)
this.sha224.releaseNativeStruct();
if (this.sha256 != null)
this.sha256.releaseNativeStruct();
@ -668,6 +703,21 @@ public class WolfCryptSignature extends SignatureSpi {
}
}
/**
* wolfJCE SHA224wRSA signature class
*/
public static final class wcSHA224wRSA extends WolfCryptSignature {
/**
* Create new wcSHA224wRSA object
*
* @throws NoSuchAlgorithmException if signature type is not
* available in native wolfCrypt library
*/
public wcSHA224wRSA() throws NoSuchAlgorithmException {
super(KeyType.WC_RSA, DigestType.WC_SHA224);
}
}
/**
* wolfJCE SHA256wRSA signature class
*/
@ -728,6 +778,21 @@ public class WolfCryptSignature extends SignatureSpi {
}
}
/**
* wolfJCE SHA224wECDSA signature class
*/
public static final class wcSHA224wECDSA extends WolfCryptSignature {
/**
* Create new wcSHA224wECDSA object
*
* @throws NoSuchAlgorithmException if signature type is not
* available in native wolfCrypt library
*/
public wcSHA224wECDSA() throws NoSuchAlgorithmException {
super(KeyType.WC_ECDSA, DigestType.WC_SHA224);
}
}
/**
* wolfJCE SHA256wECDSA signature class
*/

View File

@ -29,7 +29,7 @@ import java.nio.ByteBuffer;
public class Hmac extends NativeStruct {
private enum hashType {
typeMD5, typeSHA, typeSHA256, typeSHA384, typeSHA512;
typeMD5, typeSHA, typeSHA224, typeSHA256, typeSHA384, typeSHA512;
}
/* types may be -1 if not compiled in at native level */
@ -37,6 +37,8 @@ public class Hmac extends NativeStruct {
public static final int MD5 = getHashCode(hashType.typeMD5);
/** HMAC-SHA-1 type */
public static final int SHA = getHashCode(hashType.typeSHA);
/** HMAC-SHA2-224 type */
public static final int SHA224 = getHashCode(hashType.typeSHA224);
/** HMAC-SHA2-256 type */
public static final int SHA256 = getHashCode(hashType.typeSHA256);
/** HMAC-SHA2-384 type */
@ -95,6 +97,7 @@ public class Hmac extends NativeStruct {
private native int wc_HmacSizeByType(int type);
private native static int getCodeMd5();
private native static int getCodeSha();
private native static int getCodeSha224();
private native static int getCodeSha256();
private native static int getCodeSha384();
private native static int getCodeSha512();
@ -318,6 +321,9 @@ public class Hmac extends NativeStruct {
if (type == MD5) {
return "HmacMD5";
}
else if (type == SHA224) {
return "HmacSHA224";
}
else if (type == SHA256) {
return "HmacSHA256";
}
@ -359,6 +365,8 @@ public class Hmac extends NativeStruct {
return getCodeMd5();
case typeSHA:
return getCodeSha();
case typeSHA224:
return getCodeSha224();
case typeSHA256:
return getCodeSha256();
case typeSHA384:
@ -370,4 +378,3 @@ public class Hmac extends NativeStruct {
}
}
}

View File

@ -0,0 +1,239 @@
/* Sha224.java
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* 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-1335, USA
*/
package com.wolfssl.wolfcrypt;
import java.nio.ByteBuffer;
/**
* Wrapper for the native WolfCrypt SHA2-224 implementation
*/
public class Sha224 extends MessageDigest implements Cloneable {
/** SHA2-224 hash type */
public static final int TYPE = 5; /* hash type unique */
/** SHA2-224 digest size */
public static final int DIGEST_SIZE = 28;
/** Array to init Sha224 with, will be reset to null once initialized */
private byte[] initialData = null;
/* native JNI methods, internally reach back and grab/use pointer
* from NativeStruct.java. We wrap calls to these below in order to
* synchronize access to native pointer between threads */
private native long mallocNativeStruct_internal() throws OutOfMemoryError;
private native void native_init_internal();
private native void native_copy_internal(Sha224 toBeCopied);
private native void native_update_internal(ByteBuffer data, int offset,
int len);
private native void native_update_internal(byte[] data, int offset,
int len);
private native void native_final_internal(ByteBuffer hash, int offset);
private native void native_final_internal(byte[] hash);
/**
* Malloc native JNI Sha224 structure
*
* @return native allocated pointer
*
* @throws OutOfMemoryError when malloc fails with memory error
*/
protected long mallocNativeStruct()
throws OutOfMemoryError {
synchronized (pointerLock) {
return mallocNativeStruct_internal();
}
}
/**
* Initialize Sha224 object
*
* @throws WolfCryptException if native operation fails
*/
protected void native_init()
throws WolfCryptException {
synchronized (pointerLock) {
native_init_internal();
/* Check if we need to init with passed in data */
if (this.initialData != null) {
update(this.initialData);
this.initialData = null;
}
}
}
/**
* Copy existing native WC_SHA224 struct (Sha224 object) into this one.
* Copies structure state using wc_Sha224Copy().
*
* @param toBeCopied initialized Sha224 object to be copied.
*
* @throws WolfCryptException if native operation fails
*/
protected void native_copy(Sha224 toBeCopied)
throws WolfCryptException {
synchronized (pointerLock) {
native_copy_internal(toBeCopied);
}
}
/**
* Native SHA2-224 update
*
* @param data input data
* @param offset offset into input data
* @param len length of input data
*
* @throws WolfCryptException if native operation fails
*/
protected void native_update(ByteBuffer data, int offset, int len)
throws WolfCryptException {
synchronized (pointerLock) {
native_update_internal(data, offset, len);
}
}
/**
* Native SHA2-224 update
*
* @param data input data
* @param offset offset into input data
* @param len length of input data
*
* @throws WolfCryptException if native operation fails
*/
protected void native_update(byte[] data, int offset, int len)
throws WolfCryptException {
synchronized (pointerLock) {
native_update_internal(data, offset, len);
}
}
/**
* Native SHA2-224 final, calculate final digest
*
* @param hash output buffer to place digest
* @param offset offset into output buffer to write digest
*
* @throws WolfCryptException if native operation fails
*/
protected void native_final(ByteBuffer hash, int offset)
throws WolfCryptException {
synchronized (pointerLock) {
native_final_internal(hash, offset);
}
}
/**
* Native SHA2-224 final, calculate final digest
*
* @param hash output buffer to place digest
*
* @throws WolfCryptException if native operation fails
*/
protected void native_final(byte[] hash)
throws WolfCryptException {
synchronized (pointerLock) {
native_final_internal(hash);
}
}
/**
* Create new SHA2-224 object.
*
* @throws WolfCryptException if SHA-224 has not been compiled into native
* wolfCrypt library.
*/
public Sha224() {
if (!FeatureDetect.Sha224Enabled()) {
throw new WolfCryptException(
WolfCryptError.NOT_COMPILED_IN.getCode());
}
/* Internal state is initialized on first use */
}
/**
* Create new SHA2-224 object by making a copy of the one given.
*
* @param sha224 Initialized/created Sha224 object to be copied
*
* @throws WolfCryptException to indicate this constructor has been
* deprecated, along with instructions on what API to call
*
* @deprecated This constructor has been deprecated to avoid storage
* of a second Sha224 object inside this Sha224 object, and to
* avoid potential incomplete object creation issues between
* subclass/superclasses. Please refactor existing code to
* call Sha224.clone() to get a copy of an existing Sha224
* object.
*/
@Deprecated
public Sha224(Sha224 sha224) {
throw new WolfCryptException(
"Constructor deprecated, use Sha224.clone() to duplicate " +
"Sha224 object");
}
/**
* Create new SHA2-224 object.
*
* @param data input data to hash
*
* @throws WolfCryptException if SHA-224 has not been compiled into native
* wolfCrypt library.
*/
public Sha224(byte[] data) {
if (!FeatureDetect.Sha224Enabled()) {
throw new WolfCryptException(
WolfCryptError.NOT_COMPILED_IN.getCode());
}
/* Internal state is initialized on first use */
this.initialData = data.clone();
}
/**
* Get SHA2-224 digest size
*
* @return SHA2-224 digest size
*/
public int digestSize() {
return DIGEST_SIZE;
}
@Override
public Object clone() {
Sha224 shaCopy = new Sha224();
/* Initialize NativeStruct, since is done on first use */
shaCopy.checkStateAndInitialize();
shaCopy.native_copy(this);
return shaCopy;
}
}

View File

@ -42,6 +42,7 @@ import javax.crypto.SecretKey;
import com.wolfssl.wolfcrypt.Fips;
import com.wolfssl.wolfcrypt.Aes;
import com.wolfssl.wolfcrypt.Sha224;
import com.wolfssl.wolfcrypt.Sha256;
import com.wolfssl.wolfcrypt.Sha384;
import com.wolfssl.wolfcrypt.Sha512;
@ -52,6 +53,7 @@ public class WolfCryptKeyGeneratorTest {
private static String[] keyAlgorithms = {
"AES",
"HmacSHA1",
"HmacSHA224",
"HmacSHA256",
"HmacSHA384",
"HmacSHA512"
@ -121,6 +123,14 @@ public class WolfCryptKeyGeneratorTest {
testKeyGenerationDefaultKeySize("HmacSHA1", Sha512.DIGEST_SIZE * 8);
}
@Test
public void testHmacSHA224KeyGeneration()
throws NoSuchProviderException, NoSuchAlgorithmException {
testKeyGeneration("HmacSHA224", new int[] { 224 });
testKeyGenerationDefaultKeySize("HmacSHA224", Sha224.DIGEST_SIZE * 8);
}
@Test
public void testHmacSHA256KeyGeneration()
throws NoSuchProviderException, NoSuchAlgorithmException {

View File

@ -55,6 +55,7 @@ public class WolfCryptMacTest {
private static String wolfJCEAlgos[] = {
"HmacMD5",
"HmacSHA1",
"HmacSHA224",
"HmacSHA256",
"HmacSHA384",
"HmacSHA512"
@ -67,6 +68,7 @@ public class WolfCryptMacTest {
private static int wolfJCEMacLengths[] = {
16,
20,
28,
32,
48,
64
@ -337,6 +339,155 @@ public class WolfCryptMacTest {
}
}
@Test
public void testMacSha224SingleUpdate()
throws InvalidKeyException, NoSuchAlgorithmException,
NoSuchProviderException {
HmacVector[] vectors = new HmacVector[] {
/* HMAC vectors { key, input, output } */
/* Test vectors match test.c, from RFC 4231 section 4 */
new HmacVector(
new byte[] {
(byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b,
(byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b,
(byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b,
(byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b,
(byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b
},
"Hi There".getBytes(),
new byte[] {
(byte)0x89, (byte)0x6f, (byte)0xb1, (byte)0x12,
(byte)0x8a, (byte)0xbb, (byte)0xdf, (byte)0x19,
(byte)0x68, (byte)0x32, (byte)0x10, (byte)0x7c,
(byte)0xd4, (byte)0x9d, (byte)0xf3, (byte)0x3f,
(byte)0x47, (byte)0xb4, (byte)0xb1, (byte)0x16,
(byte)0x99, (byte)0x12, (byte)0xba, (byte)0x4f,
(byte)0x53, (byte)0x68, (byte)0x4b, (byte)0x22
}
),
new HmacVector(
"Jefe".getBytes(),
"what do ya want for nothing?".getBytes(),
new byte[] {
(byte)0xa3, (byte)0x0e, (byte)0x01, (byte)0x09,
(byte)0x8b, (byte)0xc6, (byte)0xdb, (byte)0xbf,
(byte)0x45, (byte)0x69, (byte)0x0f, (byte)0x3a,
(byte)0x7e, (byte)0x9e, (byte)0x6d, (byte)0x0f,
(byte)0x8b, (byte)0xbe, (byte)0xa2, (byte)0xa3,
(byte)0x9e, (byte)0x61, (byte)0x48, (byte)0x00,
(byte)0x8f, (byte)0xd0, (byte)0x5e, (byte)0x44
}
),
new HmacVector(
new byte[] {
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA
},
new byte[] {
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD
},
new byte[] {
(byte)0x7f, (byte)0xb3, (byte)0xcb, (byte)0x35,
(byte)0x88, (byte)0xc6, (byte)0xc1, (byte)0xf6,
(byte)0xff, (byte)0xa9, (byte)0x69, (byte)0x4d,
(byte)0x7d, (byte)0x6a, (byte)0xd2, (byte)0x64,
(byte)0x93, (byte)0x65, (byte)0xb0, (byte)0xc1,
(byte)0xf6, (byte)0x5d, (byte)0x69, (byte)0xd1,
(byte)0xec, (byte)0x83, (byte)0x33, (byte)0xea
}
),
new HmacVector(
new byte[] {
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA
},
"Test Using Larger Than Block-Size Key - Hash Key First".getBytes(),
new byte[] {
(byte)0x95, (byte)0xe9, (byte)0xa0, (byte)0xdb,
(byte)0x96, (byte)0x20, (byte)0x95, (byte)0xad,
(byte)0xae, (byte)0xbe, (byte)0x9b, (byte)0x2d,
(byte)0x6f, (byte)0x0d, (byte)0xbc, (byte)0xe2,
(byte)0xd4, (byte)0x99, (byte)0xf1, (byte)0x12,
(byte)0xf2, (byte)0xd2, (byte)0xb7, (byte)0x27,
(byte)0x3f, (byte)0xa6, (byte)0x87, (byte)0x0e
}
)
};
for (int i = 0; i < vectors.length; i++) {
if ((i == 1) && Fips.enabled) {
/* FIPS doesn't allow short key lengths */
continue;
}
SecretKeySpec keyspec =
new SecretKeySpec(vectors[i].getKey(), "SHA224");
try {
Mac mac = Mac.getInstance("HmacSHA224", "wolfJCE");
mac.init(keyspec);
mac.update(vectors[i].getInput());
byte out[] = mac.doFinal();
assertArrayEquals(out, vectors[i].getOutput());
} catch (NoSuchAlgorithmException e) {
/* skip test if not available */
Assume.assumeTrue(false);
}
}
}
@Test
public void testMacSha256SingleUpdate()
throws InvalidKeyException, NoSuchAlgorithmException,
@ -815,6 +966,40 @@ public class WolfCryptMacTest {
}
);
HmacVector sha224Vector = new HmacVector(
new byte[] {
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA
},
new byte[] {
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,
(byte)0xDD, (byte)0xDD
},
new byte[] {
(byte)0x7f, (byte)0xb3, (byte)0xcb, (byte)0x35,
(byte)0x88, (byte)0xc6, (byte)0xc1, (byte)0xf6,
(byte)0xff, (byte)0xa9, (byte)0x69, (byte)0x4d,
(byte)0x7d, (byte)0x6a, (byte)0xd2, (byte)0x64,
(byte)0x93, (byte)0x65, (byte)0xb0, (byte)0xc1,
(byte)0xf6, (byte)0x5d, (byte)0x69, (byte)0xd1,
(byte)0xec, (byte)0x83, (byte)0x33, (byte)0xea
}
);
HmacVector sha256Vector = new HmacVector(
new byte[] {
(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,
@ -940,6 +1125,10 @@ public class WolfCryptMacTest {
threadRunnerMacTest("HmacSHA1", "SHA1", sha1Vector);
}
if (enabledAlgos.contains("HmacSHA224")) {
threadRunnerMacTest("HmacSHA224", "SHA224", sha224Vector);
}
if (enabledAlgos.contains("HmacSHA256")) {
threadRunnerMacTest("HmacSHA256", "SHA256", sha256Vector);
}

View File

@ -0,0 +1,257 @@
/* wolfCryptMessageDigestSha224Test.java
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* 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-1335, USA
*/
package com.wolfssl.provider.jce.test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.Test;
import org.junit.Assume;
import org.junit.BeforeClass;
import java.util.Random;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.security.Security;
import java.security.Provider;
import java.security.MessageDigest;
import java.security.NoSuchProviderException;
import java.security.NoSuchAlgorithmException;
import com.wolfssl.wolfcrypt.Sha224;
import com.wolfssl.provider.jce.WolfCryptProvider;
import com.wolfssl.wolfcrypt.FeatureDetect;
public class WolfCryptMessageDigestSha224Test {
@Rule(order = Integer.MIN_VALUE)
public TestRule testWatcher = new TestWatcher() {
protected void starting(Description desc) {
System.out.println("\t" + desc.getMethodName());
}
};
@BeforeClass
public static void testProviderInstallationAtRuntime()
throws NoSuchProviderException {
System.out.println("JCE WolfCryptMessageDigestSha224 Class");
/* install wolfJCE provider at runtime */
Security.insertProviderAt(new WolfCryptProvider(), 1);
Provider p = Security.getProvider("wolfJCE");
assertNotNull(p);
try {
MessageDigest.getInstance("SHA-224", "wolfJCE");
} catch (NoSuchAlgorithmException e) {
/* if we also detect algo is compiled out, skip tests */
if (FeatureDetect.Sha224Enabled() == false) {
System.out.println("JSSE SHA-224 Test skipped");
Assume.assumeTrue(false);
}
}
}
static DigestVector vectors[] = new DigestVector[] {
new DigestVector(
new String("").getBytes(),
new byte[] {
(byte)0xd1, (byte)0x4a, (byte)0x02, (byte)0x8c,
(byte)0x2a, (byte)0x3a, (byte)0x2b, (byte)0xc9,
(byte)0x47, (byte)0x61, (byte)0x02, (byte)0xbb,
(byte)0x28, (byte)0x82, (byte)0x34, (byte)0xc4,
(byte)0x15, (byte)0xa2, (byte)0xb0, (byte)0x1f,
(byte)0x82, (byte)0x8e, (byte)0xa6, (byte)0x2a,
(byte)0xc5, (byte)0xb3, (byte)0xe4, (byte)0x2f
}
),
new DigestVector(
new String("abc").getBytes(),
new byte[] {
(byte)0x23, (byte)0x09, (byte)0x7d, (byte)0x22,
(byte)0x34, (byte)0x05, (byte)0xd8, (byte)0x22,
(byte)0x86, (byte)0x42, (byte)0xa4, (byte)0x77,
(byte)0xbd, (byte)0xa2, (byte)0x55, (byte)0xb3,
(byte)0x2a, (byte)0xad, (byte)0xbc, (byte)0xe4,
(byte)0xbd, (byte)0xa0, (byte)0xb3, (byte)0xf7,
(byte)0xe3, (byte)0x6c, (byte)0x9d, (byte)0xa7
}
),
new DigestVector(
new String("abcdbcdecdefdefgefghfghighijhij" +
"kijkljklmklmnlmnomnopnopq").getBytes(),
new byte[] {
(byte)0x75, (byte)0x38, (byte)0x8b, (byte)0x16,
(byte)0x51, (byte)0x27, (byte)0x76, (byte)0xcc,
(byte)0x5d, (byte)0xba, (byte)0x5d, (byte)0xa1,
(byte)0xfd, (byte)0x89, (byte)0x01, (byte)0x50,
(byte)0xb0, (byte)0xc6, (byte)0x45, (byte)0x5c,
(byte)0xb4, (byte)0xf5, (byte)0x8b, (byte)0x19,
(byte)0x52, (byte)0x52, (byte)0x25, (byte)0x25
}
)
};
@Test
public void testSha224SingleUpdate()
throws NoSuchProviderException, NoSuchAlgorithmException {
byte[] output;
MessageDigest sha224 = MessageDigest.getInstance("SHA-224", "wolfJCE");
for (int i = 0; i < vectors.length; i++) {
sha224.update(vectors[i].getInput());
output = sha224.digest();
assertEquals(vectors[i].getOutput().length, output.length);
assertArrayEquals(vectors[i].getOutput(), output);
}
}
@Test
public void testSha224SingleByteUpdate()
throws NoSuchProviderException, NoSuchAlgorithmException {
byte[] output;
MessageDigest sha224 = MessageDigest.getInstance("SHA-224", "wolfJCE");
for (int i = 0; i < vectors[1].getInput().length; i++) {
sha224.update(vectors[1].getInput()[i]);
}
output = sha224.digest();
assertEquals(vectors[1].getOutput().length, output.length);
assertArrayEquals(vectors[1].getOutput(), output);
}
@Test
public void testSha224Reset()
throws NoSuchProviderException, NoSuchAlgorithmException {
byte[] output;
MessageDigest sha224 = MessageDigest.getInstance("SHA-224", "wolfJCE");
for (int i = 0; i < vectors[1].getInput().length; i++) {
sha224.update(vectors[1].getInput()[i]);
}
sha224.reset();
for (int i = 0; i < vectors[1].getInput().length; i++) {
sha224.update(vectors[1].getInput()[i]);
}
output = sha224.digest();
assertEquals(vectors[1].getOutput().length, output.length);
assertArrayEquals(vectors[1].getOutput(), output);
}
@Test
public void testSha224Clone()
throws NoSuchProviderException, NoSuchAlgorithmException,
CloneNotSupportedException {
byte[] output;
byte[] output2;
MessageDigest sha224 = MessageDigest.getInstance("SHA-224", "wolfJCE");
for (int i = 0; i < vectors[1].getInput().length; i++) {
sha224.update(vectors[1].getInput()[i]);
}
/* Try to clone existing MessageDigest, should copy over same state */
MessageDigest sha224Copy = (MessageDigest)sha224.clone();
output = sha224.digest();
output2 = sha224Copy.digest();
assertEquals(vectors[1].getOutput().length, output.length);
assertEquals(vectors[1].getOutput().length, output2.length);
assertArrayEquals(vectors[1].getOutput(), output);
assertArrayEquals(vectors[1].getOutput(), output2);
}
@Test
public void testSha224Interop()
throws NoSuchProviderException, NoSuchAlgorithmException {
String input = "Bozeman, MT";
String input2 = "wolfSSL is an Open Source Internet security " +
"company, focused primarily on SSL/TLS and " +
"cryptography. Main products include the wolfSSL " +
"embedded SSL/TLS library, wolfCrypt cryptography " +
"library, wolfMQTT, and wolfSSH. Products are " +
"dual licensed under both GPLv2 and a commercial" +
"license.";
byte[] wolfOutput;
byte[] interopOutput;
MessageDigest sha224 = MessageDigest.getInstance("SHA-224");
Provider provider = sha224.getProvider();
/* if we have another MessageDigest provider, test against it */
if (!provider.equals("wolfJCE")) {
/* short message */
sha224.update(input.getBytes());
interopOutput = sha224.digest();
MessageDigest wolfSha224 =
MessageDigest.getInstance("SHA-224", "wolfJCE");
wolfSha224.update(input.getBytes());
wolfOutput = wolfSha224.digest();
assertArrayEquals(wolfOutput, interopOutput);
/* long message */
sha224.update(input2.getBytes());
interopOutput = sha224.digest();
wolfSha224.update(input2.getBytes());
wolfOutput = wolfSha224.digest();
assertArrayEquals(wolfOutput, interopOutput);
}
}
@Test
public void testSha224GetDigestLength()
throws NoSuchProviderException, NoSuchAlgorithmException {
MessageDigest sha224 = MessageDigest.getInstance("SHA-224", "wolfJCE");
assertEquals(Sha224.DIGEST_SIZE, sha224.getDigestLength());
}
}

View File

@ -58,10 +58,12 @@ public class WolfCryptSignatureTest {
private static String wolfJCEAlgos[] = {
"SHA1withRSA",
"SHA224withRSA",
"SHA256withRSA",
"SHA384withRSA",
"SHA512withRSA",
"SHA1withECDSA",
"SHA224withECDSA",
"SHA256withECDSA",
"SHA384withECDSA",
"SHA512withECDSA"
@ -84,8 +86,6 @@ public class WolfCryptSignatureTest {
public static void testProviderInstallationAtRuntime()
throws NoSuchProviderException {
Signature sig;
System.out.println("JCE WolfCryptSignature Class");
/* install wolfJCE provider at runtime */
@ -98,7 +98,8 @@ public class WolfCryptSignatureTest {
* compiled out */
for (int i = 0; i < wolfJCEAlgos.length; i++) {
try {
sig = Signature.getInstance(wolfJCEAlgos[i], "wolfJCE");
Signature sig =
Signature.getInstance(wolfJCEAlgos[i], "wolfJCE");
assertNotNull(sig);
enabledAlgos.add(wolfJCEAlgos[i]);
} catch (NoSuchAlgorithmException e) {
@ -111,17 +112,16 @@ public class WolfCryptSignatureTest {
public void testGetSignatureFromProvider()
throws NoSuchProviderException, NoSuchAlgorithmException {
Signature sig;
/* try to get all available options we expect to have */
for (int i = 0; i < enabledAlgos.size(); i++) {
sig = Signature.getInstance(enabledAlgos.get(i), "wolfJCE");
Signature sig =
Signature.getInstance(enabledAlgos.get(i), "wolfJCE");
assertNotNull(sig);
}
/* asking for a bad algo should throw an exception */
try {
sig = Signature.getInstance("invalidalgo", "wolfJCE");
Signature.getInstance("invalidalgo", "wolfJCE");
fail("Requesting an invalid algorithm from Signature " +
"object should throw an exception");
} catch (NoSuchAlgorithmException e) { }

View File

@ -29,6 +29,7 @@ import org.junit.runners.Suite.SuiteClasses;
@SuiteClasses({
WolfCryptMessageDigestMd5Test.class,
WolfCryptMessageDigestShaTest.class,
WolfCryptMessageDigestSha224Test.class,
WolfCryptMessageDigestSha256Test.class,
WolfCryptMessageDigestSha384Test.class,
WolfCryptMessageDigestSha512Test.class,

View File

@ -0,0 +1,244 @@
/* Sha224Test.java
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* 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-1335, USA
*/
package com.wolfssl.wolfcrypt.test;
import static org.junit.Assert.*;
import java.nio.ByteBuffer;
import java.util.Random;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import javax.crypto.ShortBufferException;
import org.junit.Test;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import com.wolfssl.wolfcrypt.Sha224;
import com.wolfssl.wolfcrypt.NativeStruct;
import com.wolfssl.wolfcrypt.WolfCryptException;
import com.wolfssl.wolfcrypt.WolfCryptError;
public class Sha224Test {
private ByteBuffer data = ByteBuffer.allocateDirect(64);
private ByteBuffer result = ByteBuffer.allocateDirect(Sha224.DIGEST_SIZE);
private ByteBuffer expected = ByteBuffer.allocateDirect(Sha224.DIGEST_SIZE);
static String[] dataVector = new String[] {
"",
"abc",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
};
static String[] hashVector = new String[] {
"d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7",
"75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525"
};
@Rule(order = Integer.MIN_VALUE)
public TestRule testWatcher = new TestWatcher() {
protected void starting(Description desc) {
System.out.println("\t" + desc.getMethodName());
}
};
@BeforeClass
public static void checkSha224IsAvailable() {
try {
Sha224 sha = new Sha224();
System.out.println("JNI Sha224 Class");
} catch (WolfCryptException e) {
if (e.getError() == WolfCryptError.NOT_COMPILED_IN) {
System.out.println("Sha224Test skipped: " + e.getError());
Assume.assumeTrue(false);
}
}
}
@Test
public void constructorShouldNotInitializeNativeStruct() {
assertEquals(NativeStruct.NULL, new Sha224().getNativeStruct());
}
@Test
public void hashShouldMatchUsingByteBuffer() throws ShortBufferException {
for (int i = 0; i < dataVector.length; i++) {
Sha224 sha = new Sha224();
byte[] input = dataVector[i].getBytes();
data.put(input).rewind();
expected.put(Util.h2b(hashVector[i])).rewind();
sha.update(data, input.length);
sha.digest(result);
data.rewind();
result.rewind();
assertEquals(expected, result);
}
}
@Test
public void hashShouldMatchUsingByteArray() {
for (int i = 0; i < dataVector.length; i++) {
Sha224 sha = new Sha224();
byte[] data = dataVector[i].getBytes();
byte[] expected = Util.h2b(hashVector[i]);
sha.update(data);
byte[] result = sha.digest();
assertArrayEquals(expected, result);
}
}
@Test
public void releaseAndReInitObject() {
Sha224 sha = new Sha224();
byte[] data = dataVector[0].getBytes();
byte[] expected = Util.h2b(hashVector[0]);
byte[] result = null;
sha.update(data);
result = sha.digest();
assertArrayEquals(expected, result);
sha.releaseNativeStruct();
/* test re-initializing object */
sha = new Sha224();
result = null;
sha.update(data);
result = sha.digest();
sha.releaseNativeStruct();
}
@Test
public void reuseObject() {
Sha224 sha = new Sha224();
byte[] data = dataVector[0].getBytes();
byte[] data2 = dataVector[1].getBytes();
byte[] expected = Util.h2b(hashVector[0]);
byte[] expected2 = Util.h2b(hashVector[1]);
byte[] result = null;
byte[] result2 = null;
sha.update(data);
result = sha.digest();
assertArrayEquals(expected, result);
/* test reusing existing object after a call to digest() */
sha.update(data2);
result2 = sha.digest();
assertArrayEquals(expected2, result2);
sha.releaseNativeStruct();
}
@Test
public void copyObject() {
Sha224 sha = null;
Sha224 shaCopy = null;
byte[] data = dataVector[0].getBytes();
byte[] expected = Util.h2b(hashVector[0]);
byte[] result = null;
byte[] result2 = null;
sha = new Sha224();
sha.update(data);
/* test making copy of Sha224, should retain same state */
shaCopy = (Sha224)sha.clone();
result = sha.digest();
result2 = shaCopy.digest();
assertArrayEquals(expected, result);
assertArrayEquals(expected, result2);
sha.releaseNativeStruct();
shaCopy.releaseNativeStruct();
}
@Test
public void threadedHashTest() throws InterruptedException {
int numThreads = 100;
ExecutorService service = Executors.newFixedThreadPool(numThreads);
final CountDownLatch latch = new CountDownLatch(numThreads);
final LinkedBlockingQueue<byte[]> results = new LinkedBlockingQueue<>();
final byte[] rand10kBuf = new byte[10240];
/* fill large input buffer with random bytes */
new Random().nextBytes(rand10kBuf);
/* generate hash over input data concurrently across numThreads */
for (int i = 0; i < numThreads; i++) {
service.submit(new Runnable() {
@Override public void run() {
Sha224 sha = new Sha224();
/* process/update in 1024-byte chunks */
for (int j = 0; j < rand10kBuf.length; j+= 1024) {
sha.update(rand10kBuf, j, 1024);
}
/* get final hash */
byte[] hash = sha.digest();
results.add(hash.clone());
sha.releaseNativeStruct();
latch.countDown();
}
});
}
/* wait for all threads to complete */
latch.await();
/* compare all digests, all should be the same across threads */
Iterator<byte[]> listIterator = results.iterator();
byte[] current = listIterator.next();
while (listIterator.hasNext()) {
byte[] next = listIterator.next();
if (!Arrays.equals(current, next)) {
fail("Found two non-identical digests in thread test");
}
if (listIterator.hasNext()) {
current = listIterator.next();
}
}
}
}

View File

@ -33,6 +33,7 @@ import org.junit.runners.Suite.SuiteClasses;
ChachaTest.class,
Md5Test.class,
ShaTest.class,
Sha224Test.class,
Sha256Test.class,
Sha384Test.class,
Sha512Test.class,