Skip to content

Commit 9e025b7

Browse files
committed
JCE: add SHA224withRSA and SHA224withECDSA to Signature implementation
1 parent dcddf92 commit 9e025b7

File tree

4 files changed

+81
-8
lines changed

4 files changed

+81
-8
lines changed

README_JCE.md

+2
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,12 @@ The JCE provider currently supports the following algorithms:
116116
Signature Class
117117
MD5withRSA
118118
SHA1withRSA
119+
SHA224withRSA
119120
SHA256withRSA
120121
SHA384withRSA
121122
SHA512withRSA
122123
SHA1withECDSA
124+
SHA224withECDSA
123125
SHA256withECDSA
124126
SHA384withECDSA
125127
SHA512withECDSA

src/main/java/com/wolfssl/provider/jce/WolfCryptProvider.java

+6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ private void registerServices() {
114114
put("Signature.SHA1withECDSA",
115115
"com.wolfssl.provider.jce.WolfCryptSignature$wcSHA1wECDSA");
116116
}
117+
if (FeatureDetect.Sha224Enabled()) {
118+
put("Signature.SHA224withRSA",
119+
"com.wolfssl.provider.jce.WolfCryptSignature$wcSHA224wRSA");
120+
put("Signature.SHA224withECDSA",
121+
"com.wolfssl.provider.jce.WolfCryptSignature$wcSHA224wECDSA");
122+
}
117123
if (FeatureDetect.Sha256Enabled()) {
118124
put("Signature.SHA256withRSA",
119125
"com.wolfssl.provider.jce.WolfCryptSignature$wcSHA256wRSA");

src/main/java/com/wolfssl/provider/jce/WolfCryptSignature.java

+66-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.wolfssl.wolfcrypt.Asn;
4040
import com.wolfssl.wolfcrypt.Md5;
4141
import com.wolfssl.wolfcrypt.Sha;
42+
import com.wolfssl.wolfcrypt.Sha224;
4243
import com.wolfssl.wolfcrypt.Sha256;
4344
import com.wolfssl.wolfcrypt.Sha384;
4445
import com.wolfssl.wolfcrypt.Sha512;
@@ -60,14 +61,16 @@ enum KeyType {
6061
enum DigestType {
6162
WC_MD5,
6263
WC_SHA1,
64+
WC_SHA224,
6365
WC_SHA256,
6466
WC_SHA384,
6567
WC_SHA512
6668
}
6769

68-
/* internal hash type sums */
70+
/* internal hash type sums (asn.h) */
6971
private int MD5h = 649;
7072
private int SHAh = 88;
73+
private int SHA224h = 417;
7174
private int SHA256h = 414;
7275
private int SHA384h = 415;
7376
private int SHA512h = 416;
@@ -79,6 +82,7 @@ enum DigestType {
7982
/* internal hash objects */
8083
private Md5 md5 = null;
8184
private Sha sha = null;
85+
private Sha224 sha224 = null;
8286
private Sha256 sha256 = null;
8387
private Sha384 sha384 = null;
8488
private Sha512 sha512 = null;
@@ -127,6 +131,12 @@ private WolfCryptSignature(KeyType ktype, DigestType dtype)
127131
this.internalHashSum = SHAh;
128132
break;
129133

134+
case WC_SHA224:
135+
this.sha224 = new Sha224();
136+
this.digestSz = Sha224.DIGEST_SIZE;
137+
this.internalHashSum = SHA224h;
138+
break;
139+
130140
case WC_SHA256:
131141
this.sha256 = new Sha256();
132142
this.digestSz = Sha256.DIGEST_SIZE;
@@ -255,6 +265,10 @@ protected synchronized void engineInitSign(PrivateKey privateKey)
255265
this.sha.init();
256266
break;
257267

268+
case WC_SHA224:
269+
this.sha224.init();
270+
break;
271+
258272
case WC_SHA256:
259273
this.sha256.init();
260274
break;
@@ -321,6 +335,10 @@ protected synchronized void engineInitVerify(PublicKey publicKey)
321335
this.sha.init();
322336
break;
323337

338+
case WC_SHA224:
339+
this.sha224.init();
340+
break;
341+
324342
case WC_SHA256:
325343
this.sha256.init();
326344
break;
@@ -366,6 +384,10 @@ protected synchronized byte[] engineSign() throws SignatureException {
366384
this.sha.digest(digest);
367385
break;
368386

387+
case WC_SHA224:
388+
this.sha224.digest(digest);
389+
break;
390+
369391
case WC_SHA256:
370392
this.sha256.digest(digest);
371393
break;
@@ -452,6 +474,10 @@ protected synchronized void engineUpdate(byte[] b, int off, int len)
452474
this.sha.update(b, off, len);
453475
break;
454476

477+
case WC_SHA224:
478+
this.sha224.update(b, off, len);
479+
break;
480+
455481
case WC_SHA256:
456482
this.sha256.update(b, off, len);
457483
break;
@@ -490,6 +516,10 @@ protected synchronized boolean engineVerify(byte[] sigBytes)
490516
this.sha.digest(digest);
491517
break;
492518

519+
case WC_SHA224:
520+
this.sha224.digest(digest);
521+
break;
522+
493523
case WC_SHA256:
494524
this.sha256.digest(digest);
495525
break;
@@ -581,6 +611,8 @@ private String digestToString(DigestType type) {
581611
return "MD5";
582612
case WC_SHA1:
583613
return "SHA";
614+
case WC_SHA224:
615+
return "SHA224";
584616
case WC_SHA256:
585617
return "SHA256";
586618
case WC_SHA384:
@@ -608,6 +640,9 @@ protected synchronized void finalize() throws Throwable {
608640
if (this.sha != null)
609641
this.sha.releaseNativeStruct();
610642

643+
if (this.sha224 != null)
644+
this.sha224.releaseNativeStruct();
645+
611646
if (this.sha256 != null)
612647
this.sha256.releaseNativeStruct();
613648

@@ -668,6 +703,21 @@ public wcSHA1wRSA() throws NoSuchAlgorithmException {
668703
}
669704
}
670705

706+
/**
707+
* wolfJCE SHA224wRSA signature class
708+
*/
709+
public static final class wcSHA224wRSA extends WolfCryptSignature {
710+
/**
711+
* Create new wcSHA224wRSA object
712+
*
713+
* @throws NoSuchAlgorithmException if signature type is not
714+
* available in native wolfCrypt library
715+
*/
716+
public wcSHA224wRSA() throws NoSuchAlgorithmException {
717+
super(KeyType.WC_RSA, DigestType.WC_SHA224);
718+
}
719+
}
720+
671721
/**
672722
* wolfJCE SHA256wRSA signature class
673723
*/
@@ -728,6 +778,21 @@ public wcSHA1wECDSA() throws NoSuchAlgorithmException {
728778
}
729779
}
730780

781+
/**
782+
* wolfJCE SHA224wECDSA signature class
783+
*/
784+
public static final class wcSHA224wECDSA extends WolfCryptSignature {
785+
/**
786+
* Create new wcSHA224wECDSA object
787+
*
788+
* @throws NoSuchAlgorithmException if signature type is not
789+
* available in native wolfCrypt library
790+
*/
791+
public wcSHA224wECDSA() throws NoSuchAlgorithmException {
792+
super(KeyType.WC_ECDSA, DigestType.WC_SHA224);
793+
}
794+
}
795+
731796
/**
732797
* wolfJCE SHA256wECDSA signature class
733798
*/

src/test/java/com/wolfssl/provider/jce/test/WolfCryptSignatureTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ public class WolfCryptSignatureTest {
5858

5959
private static String wolfJCEAlgos[] = {
6060
"SHA1withRSA",
61+
"SHA224withRSA",
6162
"SHA256withRSA",
6263
"SHA384withRSA",
6364
"SHA512withRSA",
6465
"SHA1withECDSA",
66+
"SHA224withECDSA",
6567
"SHA256withECDSA",
6668
"SHA384withECDSA",
6769
"SHA512withECDSA"
@@ -84,8 +86,6 @@ protected void starting(Description desc) {
8486
public static void testProviderInstallationAtRuntime()
8587
throws NoSuchProviderException {
8688

87-
Signature sig;
88-
8989
System.out.println("JCE WolfCryptSignature Class");
9090

9191
/* install wolfJCE provider at runtime */
@@ -98,7 +98,8 @@ public static void testProviderInstallationAtRuntime()
9898
* compiled out */
9999
for (int i = 0; i < wolfJCEAlgos.length; i++) {
100100
try {
101-
sig = Signature.getInstance(wolfJCEAlgos[i], "wolfJCE");
101+
Signature sig =
102+
Signature.getInstance(wolfJCEAlgos[i], "wolfJCE");
102103
assertNotNull(sig);
103104
enabledAlgos.add(wolfJCEAlgos[i]);
104105
} catch (NoSuchAlgorithmException e) {
@@ -111,17 +112,16 @@ public static void testProviderInstallationAtRuntime()
111112
public void testGetSignatureFromProvider()
112113
throws NoSuchProviderException, NoSuchAlgorithmException {
113114

114-
Signature sig;
115-
116115
/* try to get all available options we expect to have */
117116
for (int i = 0; i < enabledAlgos.size(); i++) {
118-
sig = Signature.getInstance(enabledAlgos.get(i), "wolfJCE");
117+
Signature sig =
118+
Signature.getInstance(enabledAlgos.get(i), "wolfJCE");
119119
assertNotNull(sig);
120120
}
121121

122122
/* asking for a bad algo should throw an exception */
123123
try {
124-
sig = Signature.getInstance("invalidalgo", "wolfJCE");
124+
Signature.getInstance("invalidalgo", "wolfJCE");
125125
fail("Requesting an invalid algorithm from Signature " +
126126
"object should throw an exception");
127127
} catch (NoSuchAlgorithmException e) { }

0 commit comments

Comments
 (0)