Skip to content

Commit

Permalink
JCE: add SHA224withRSA and SHA224withECDSA to Signature implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cconlon committed Mar 5, 2025
1 parent dcddf92 commit 9e025b7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README_JCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ The JCE provider currently supports the following algorithms:
Signature Class
MD5withRSA
SHA1withRSA
SHA224withRSA
SHA256withRSA
SHA384withRSA
SHA512withRSA
SHA1withECDSA
SHA224withECDSA
SHA256withECDSA
SHA384withECDSA
SHA512withECDSA
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/wolfssl/provider/jce/WolfCryptProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ private void registerServices() {
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");
Expand Down
67 changes: 66 additions & 1 deletion src/main/java/com/wolfssl/provider/jce/WolfCryptSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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;
Expand All @@ -60,14 +61,16 @@ enum KeyType {
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;
Expand All @@ -79,6 +82,7 @@ enum DigestType {
/* 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;
Expand Down Expand Up @@ -127,6 +131,12 @@ private WolfCryptSignature(KeyType ktype, DigestType dtype)
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;
Expand Down Expand Up @@ -255,6 +265,10 @@ protected synchronized void engineInitSign(PrivateKey privateKey)
this.sha.init();
break;

case WC_SHA224:
this.sha224.init();
break;

case WC_SHA256:
this.sha256.init();
break;
Expand Down Expand Up @@ -321,6 +335,10 @@ protected synchronized void engineInitVerify(PublicKey publicKey)
this.sha.init();
break;

case WC_SHA224:
this.sha224.init();
break;

case WC_SHA256:
this.sha256.init();
break;
Expand Down Expand Up @@ -366,6 +384,10 @@ protected synchronized byte[] engineSign() throws SignatureException {
this.sha.digest(digest);
break;

case WC_SHA224:
this.sha224.digest(digest);
break;

case WC_SHA256:
this.sha256.digest(digest);
break;
Expand Down Expand Up @@ -452,6 +474,10 @@ protected synchronized void engineUpdate(byte[] b, int off, int len)
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;
Expand Down Expand Up @@ -490,6 +516,10 @@ protected synchronized boolean engineVerify(byte[] sigBytes)
this.sha.digest(digest);
break;

case WC_SHA224:
this.sha224.digest(digest);
break;

case WC_SHA256:
this.sha256.digest(digest);
break;
Expand Down Expand Up @@ -581,6 +611,8 @@ private String digestToString(DigestType type) {
return "MD5";
case WC_SHA1:
return "SHA";
case WC_SHA224:
return "SHA224";
case WC_SHA256:
return "SHA256";
case WC_SHA384:
Expand Down Expand Up @@ -608,6 +640,9 @@ protected synchronized void finalize() throws Throwable {
if (this.sha != null)
this.sha.releaseNativeStruct();

if (this.sha224 != null)
this.sha224.releaseNativeStruct();

if (this.sha256 != null)
this.sha256.releaseNativeStruct();

Expand Down Expand Up @@ -668,6 +703,21 @@ public wcSHA1wRSA() throws NoSuchAlgorithmException {
}
}

/**
* 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
*/
Expand Down Expand Up @@ -728,6 +778,21 @@ public wcSHA1wECDSA() throws NoSuchAlgorithmException {
}
}

/**
* 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ public class WolfCryptSignatureTest {

private static String wolfJCEAlgos[] = {
"SHA1withRSA",
"SHA224withRSA",
"SHA256withRSA",
"SHA384withRSA",
"SHA512withRSA",
"SHA1withECDSA",
"SHA224withECDSA",
"SHA256withECDSA",
"SHA384withECDSA",
"SHA512withECDSA"
Expand All @@ -84,8 +86,6 @@ protected void starting(Description desc) {
public static void testProviderInstallationAtRuntime()
throws NoSuchProviderException {

Signature sig;

System.out.println("JCE WolfCryptSignature Class");

/* install wolfJCE provider at runtime */
Expand All @@ -98,7 +98,8 @@ public static void testProviderInstallationAtRuntime()
* 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) {
Expand All @@ -111,17 +112,16 @@ public static void testProviderInstallationAtRuntime()
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) { }
Expand Down

0 comments on commit 9e025b7

Please sign in to comment.