JCE: add SHA224withRSA and SHA224withECDSA to Signature implementation

pull/104/head
Chris Conlon 2025-03-03 14:00:27 -07:00
parent dcddf92aaf
commit 9e025b75ce
4 changed files with 81 additions and 8 deletions

View File

@ -116,10 +116,12 @@ The JCE provider currently supports the following algorithms:
Signature Class Signature Class
MD5withRSA MD5withRSA
SHA1withRSA SHA1withRSA
SHA224withRSA
SHA256withRSA SHA256withRSA
SHA384withRSA SHA384withRSA
SHA512withRSA SHA512withRSA
SHA1withECDSA SHA1withECDSA
SHA224withECDSA
SHA256withECDSA SHA256withECDSA
SHA384withECDSA SHA384withECDSA
SHA512withECDSA SHA512withECDSA

View File

@ -114,6 +114,12 @@ public final class WolfCryptProvider extends Provider {
put("Signature.SHA1withECDSA", put("Signature.SHA1withECDSA",
"com.wolfssl.provider.jce.WolfCryptSignature$wcSHA1wECDSA"); "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()) { if (FeatureDetect.Sha256Enabled()) {
put("Signature.SHA256withRSA", put("Signature.SHA256withRSA",
"com.wolfssl.provider.jce.WolfCryptSignature$wcSHA256wRSA"); "com.wolfssl.provider.jce.WolfCryptSignature$wcSHA256wRSA");

View File

@ -39,6 +39,7 @@ import javax.crypto.ShortBufferException;
import com.wolfssl.wolfcrypt.Asn; import com.wolfssl.wolfcrypt.Asn;
import com.wolfssl.wolfcrypt.Md5; import com.wolfssl.wolfcrypt.Md5;
import com.wolfssl.wolfcrypt.Sha; import com.wolfssl.wolfcrypt.Sha;
import com.wolfssl.wolfcrypt.Sha224;
import com.wolfssl.wolfcrypt.Sha256; import com.wolfssl.wolfcrypt.Sha256;
import com.wolfssl.wolfcrypt.Sha384; import com.wolfssl.wolfcrypt.Sha384;
import com.wolfssl.wolfcrypt.Sha512; import com.wolfssl.wolfcrypt.Sha512;
@ -60,14 +61,16 @@ public class WolfCryptSignature extends SignatureSpi {
enum DigestType { enum DigestType {
WC_MD5, WC_MD5,
WC_SHA1, WC_SHA1,
WC_SHA224,
WC_SHA256, WC_SHA256,
WC_SHA384, WC_SHA384,
WC_SHA512 WC_SHA512
} }
/* internal hash type sums */ /* internal hash type sums (asn.h) */
private int MD5h = 649; private int MD5h = 649;
private int SHAh = 88; private int SHAh = 88;
private int SHA224h = 417;
private int SHA256h = 414; private int SHA256h = 414;
private int SHA384h = 415; private int SHA384h = 415;
private int SHA512h = 416; private int SHA512h = 416;
@ -79,6 +82,7 @@ public class WolfCryptSignature extends SignatureSpi {
/* internal hash objects */ /* internal hash objects */
private Md5 md5 = null; private Md5 md5 = null;
private Sha sha = null; private Sha sha = null;
private Sha224 sha224 = null;
private Sha256 sha256 = null; private Sha256 sha256 = null;
private Sha384 sha384 = null; private Sha384 sha384 = null;
private Sha512 sha512 = null; private Sha512 sha512 = null;
@ -127,6 +131,12 @@ public class WolfCryptSignature extends SignatureSpi {
this.internalHashSum = SHAh; this.internalHashSum = SHAh;
break; break;
case WC_SHA224:
this.sha224 = new Sha224();
this.digestSz = Sha224.DIGEST_SIZE;
this.internalHashSum = SHA224h;
break;
case WC_SHA256: case WC_SHA256:
this.sha256 = new Sha256(); this.sha256 = new Sha256();
this.digestSz = Sha256.DIGEST_SIZE; this.digestSz = Sha256.DIGEST_SIZE;
@ -255,6 +265,10 @@ public class WolfCryptSignature extends SignatureSpi {
this.sha.init(); this.sha.init();
break; break;
case WC_SHA224:
this.sha224.init();
break;
case WC_SHA256: case WC_SHA256:
this.sha256.init(); this.sha256.init();
break; break;
@ -321,6 +335,10 @@ public class WolfCryptSignature extends SignatureSpi {
this.sha.init(); this.sha.init();
break; break;
case WC_SHA224:
this.sha224.init();
break;
case WC_SHA256: case WC_SHA256:
this.sha256.init(); this.sha256.init();
break; break;
@ -366,6 +384,10 @@ public class WolfCryptSignature extends SignatureSpi {
this.sha.digest(digest); this.sha.digest(digest);
break; break;
case WC_SHA224:
this.sha224.digest(digest);
break;
case WC_SHA256: case WC_SHA256:
this.sha256.digest(digest); this.sha256.digest(digest);
break; break;
@ -452,6 +474,10 @@ public class WolfCryptSignature extends SignatureSpi {
this.sha.update(b, off, len); this.sha.update(b, off, len);
break; break;
case WC_SHA224:
this.sha224.update(b, off, len);
break;
case WC_SHA256: case WC_SHA256:
this.sha256.update(b, off, len); this.sha256.update(b, off, len);
break; break;
@ -490,6 +516,10 @@ public class WolfCryptSignature extends SignatureSpi {
this.sha.digest(digest); this.sha.digest(digest);
break; break;
case WC_SHA224:
this.sha224.digest(digest);
break;
case WC_SHA256: case WC_SHA256:
this.sha256.digest(digest); this.sha256.digest(digest);
break; break;
@ -581,6 +611,8 @@ public class WolfCryptSignature extends SignatureSpi {
return "MD5"; return "MD5";
case WC_SHA1: case WC_SHA1:
return "SHA"; return "SHA";
case WC_SHA224:
return "SHA224";
case WC_SHA256: case WC_SHA256:
return "SHA256"; return "SHA256";
case WC_SHA384: case WC_SHA384:
@ -608,6 +640,9 @@ public class WolfCryptSignature extends SignatureSpi {
if (this.sha != null) if (this.sha != null)
this.sha.releaseNativeStruct(); this.sha.releaseNativeStruct();
if (this.sha224 != null)
this.sha224.releaseNativeStruct();
if (this.sha256 != null) if (this.sha256 != null)
this.sha256.releaseNativeStruct(); 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 * 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 * wolfJCE SHA256wECDSA signature class
*/ */

View File

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