Skip to content

Commit

Permalink
JCE: add SHA-224 MessageDigest implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cconlon committed Mar 5, 2025
1 parent bcbc411 commit 0149a37
Show file tree
Hide file tree
Showing 6 changed files with 391 additions and 0 deletions.
1 change: 1 addition & 0 deletions README_JCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions scripts/infer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,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 \
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/wolfssl/provider/jce/WolfCryptProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ private void registerServices() {
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");
Expand Down
Loading

0 comments on commit 0149a37

Please sign in to comment.