Skip to content

Commit b316794

Browse files
committed
JCE: add support for HmacSHA3 KeyGenerator
1 parent 32498c6 commit b316794

File tree

4 files changed

+181
-2
lines changed

4 files changed

+181
-2
lines changed

README_JCE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ The JCE provider currently supports the following algorithms:
166166
HmacSHA256
167167
HmacSHA384
168168
HmacSHA512
169+
HmacSHA3-224
170+
HmacSHA3-256
171+
HmacSHA3-384
172+
HmacSHA3-512
169173

170174
KeyPairGenerator Class
171175
RSA

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

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.wolfssl.wolfcrypt.Sha256;
2828
import com.wolfssl.wolfcrypt.Sha384;
2929
import com.wolfssl.wolfcrypt.Sha512;
30+
import com.wolfssl.wolfcrypt.Sha3;
3031
import javax.crypto.KeyGeneratorSpi;
3132
import javax.crypto.SecretKey;
3233
import javax.crypto.spec.SecretKeySpec;
@@ -49,7 +50,11 @@ enum AlgoType {
4950
WC_HMAC_SHA224,
5051
WC_HMAC_SHA256,
5152
WC_HMAC_SHA384,
52-
WC_HMAC_SHA512
53+
WC_HMAC_SHA512,
54+
WC_HMAC_SHA3_224,
55+
WC_HMAC_SHA3_256,
56+
WC_HMAC_SHA3_384,
57+
WC_HMAC_SHA3_512
5358
}
5459

5560
private AlgoType algoType = AlgoType.WC_INVALID;
@@ -92,6 +97,22 @@ private WolfCryptKeyGenerator(AlgoType type) {
9297
this.algString = "HmacSHA512";
9398
this.keySizeBits = (Sha512.DIGEST_SIZE * 8);
9499
break;
100+
case WC_HMAC_SHA3_224:
101+
this.algString = "HmacSHA3-224";
102+
this.keySizeBits = (Sha3.DIGEST_SIZE_224 * 8);
103+
break;
104+
case WC_HMAC_SHA3_256:
105+
this.algString = "HmacSHA3-256";
106+
this.keySizeBits = (Sha3.DIGEST_SIZE_256 * 8);
107+
break;
108+
case WC_HMAC_SHA3_384:
109+
this.algString = "HmacSHA3-384";
110+
this.keySizeBits = (Sha3.DIGEST_SIZE_384 * 8);
111+
break;
112+
case WC_HMAC_SHA3_512:
113+
this.algString = "HmacSHA3-512";
114+
this.keySizeBits = (Sha3.DIGEST_SIZE_512 * 8);
115+
break;
95116
}
96117

97118
log("created KeyGenerator(" + this.algString + ")");
@@ -232,6 +253,10 @@ protected SecretKey engineGenerateKey() {
232253
case WC_HMAC_SHA256:
233254
case WC_HMAC_SHA384:
234255
case WC_HMAC_SHA512:
256+
case WC_HMAC_SHA3_224:
257+
case WC_HMAC_SHA3_256:
258+
case WC_HMAC_SHA3_384:
259+
case WC_HMAC_SHA3_512:
235260
return new SecretKeySpec(keyArr, this.algString);
236261
default:
237262
return null;
@@ -321,5 +346,61 @@ public wcHMACSha512KeyGenerator() {
321346
super(AlgoType.WC_HMAC_SHA512);
322347
}
323348
}
349+
350+
/**
351+
* KeyGenerator(HmacSHA3-224) class, called by WolfCryptProvider.
352+
*/
353+
public static final class wcHMACSha3_224KeyGenerator
354+
extends WolfCryptKeyGenerator {
355+
356+
/**
357+
* Constructor for wcHMACSha3_224KeyGenerator.
358+
*/
359+
public wcHMACSha3_224KeyGenerator() {
360+
super(AlgoType.WC_HMAC_SHA3_224);
361+
}
362+
}
363+
364+
/**
365+
* KeyGenerator(HmacSHA3-256) class, called by WolfCryptProvider.
366+
*/
367+
public static final class wcHMACSha3_256KeyGenerator
368+
extends WolfCryptKeyGenerator {
369+
370+
/**
371+
* Constructor for wcHMACSha3_256KeyGenerator.
372+
*/
373+
public wcHMACSha3_256KeyGenerator() {
374+
super(AlgoType.WC_HMAC_SHA3_256);
375+
}
376+
}
377+
378+
/**
379+
* KeyGenerator(HmacSHA3-384) class, called by WolfCryptProvider.
380+
*/
381+
public static final class wcHMACSha3_384KeyGenerator
382+
extends WolfCryptKeyGenerator {
383+
384+
/**
385+
* Constructor for wcHMACSha3_384KeyGenerator.
386+
*/
387+
public wcHMACSha3_384KeyGenerator() {
388+
super(AlgoType.WC_HMAC_SHA3_384);
389+
}
390+
}
391+
392+
/**
393+
* KeyGenerator(HmacSHA3-512) class, called by WolfCryptProvider.
394+
*/
395+
public static final class wcHMACSha3_512KeyGenerator
396+
extends WolfCryptKeyGenerator {
397+
398+
/**
399+
* Constructor for wcHMACSha3_512KeyGenerator.
400+
*/
401+
public wcHMACSha3_512KeyGenerator() {
402+
super(AlgoType.WC_HMAC_SHA3_512);
403+
}
404+
}
324405
}
325406

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,22 @@ private void registerServices() {
364364
put("KeyGenerator.HmacSHA512",
365365
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha512KeyGenerator");
366366
}
367+
if (FeatureDetect.HmacSha3_224Enabled()) {
368+
put("KeyGenerator.HmacSHA3-224",
369+
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha3_224KeyGenerator");
370+
}
371+
if (FeatureDetect.HmacSha3_256Enabled()) {
372+
put("KeyGenerator.HmacSHA3-256",
373+
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha3_256KeyGenerator");
374+
}
375+
if (FeatureDetect.HmacSha3_384Enabled()) {
376+
put("KeyGenerator.HmacSHA3-384",
377+
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha3_384KeyGenerator");
378+
}
379+
if (FeatureDetect.HmacSha3_512Enabled()) {
380+
put("KeyGenerator.HmacSHA3-512",
381+
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha3_512KeyGenerator");
382+
}
367383

368384
/* KeyPairGenerator */
369385
if (FeatureDetect.RsaKeyGenEnabled()) {

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.wolfssl.wolfcrypt.Sha256;
4848
import com.wolfssl.wolfcrypt.Sha384;
4949
import com.wolfssl.wolfcrypt.Sha512;
50+
import com.wolfssl.wolfcrypt.Sha3;
5051
import com.wolfssl.provider.jce.WolfCryptProvider;
5152

5253
public class WolfCryptKeyGeneratorTest {
@@ -57,7 +58,11 @@ public class WolfCryptKeyGeneratorTest {
5758
"HmacSHA224",
5859
"HmacSHA256",
5960
"HmacSHA384",
60-
"HmacSHA512"
61+
"HmacSHA512",
62+
"HmacSHA3-224",
63+
"HmacSHA3-256",
64+
"HmacSHA3-384",
65+
"HmacSHA3-512"
6166
};
6267

6368
private static int[] aesKeySizes = { 128, 192, 256 };
@@ -98,6 +103,23 @@ public void testGetKeyGeneratorFromProvider()
98103
!FeatureDetect.HmacSha224Enabled()) {
99104
continue;
100105
}
106+
/* Skip HmacSHA3 algorithms if not supported by native wolfSSL */
107+
if (alg.equals("HmacSHA3-224") &&
108+
!FeatureDetect.HmacSha3_224Enabled()) {
109+
continue;
110+
}
111+
if (alg.equals("HmacSHA3-256") &&
112+
!FeatureDetect.HmacSha3_256Enabled()) {
113+
continue;
114+
}
115+
if (alg.equals("HmacSHA3-384") &&
116+
!FeatureDetect.HmacSha3_384Enabled()) {
117+
continue;
118+
}
119+
if (alg.equals("HmacSHA3-512") &&
120+
!FeatureDetect.HmacSha3_512Enabled()) {
121+
continue;
122+
}
101123
kg = KeyGenerator.getInstance(alg, "wolfJCE");
102124
assertNotNull(kg);
103125
}
@@ -166,6 +188,62 @@ public void testHmacSHA512KeyGeneration()
166188
testKeyGenerationDefaultKeySize("HmacSHA512", Sha512.DIGEST_SIZE * 8);
167189
}
168190

191+
@Test
192+
public void testHmacSHA3_224KeyGeneration()
193+
throws NoSuchProviderException, NoSuchAlgorithmException {
194+
195+
/* Skip test if HmacSHA3-224 is not supported by native wolfSSL */
196+
if (!FeatureDetect.HmacSha3_224Enabled()) {
197+
return;
198+
}
199+
200+
testKeyGeneration("HmacSHA3-224", new int[] { 224 });
201+
testKeyGenerationDefaultKeySize("HmacSHA3-224",
202+
Sha3.DIGEST_SIZE_224 * 8);
203+
}
204+
205+
@Test
206+
public void testHmacSHA3_256KeyGeneration()
207+
throws NoSuchProviderException, NoSuchAlgorithmException {
208+
209+
/* Skip test if HmacSHA3-256 is not supported by native wolfSSL */
210+
if (!FeatureDetect.HmacSha3_256Enabled()) {
211+
return;
212+
}
213+
214+
testKeyGeneration("HmacSHA3-256", new int[] { 256 });
215+
testKeyGenerationDefaultKeySize("HmacSHA3-256",
216+
Sha3.DIGEST_SIZE_256 * 8);
217+
}
218+
219+
@Test
220+
public void testHmacSHA3_384KeyGeneration()
221+
throws NoSuchProviderException, NoSuchAlgorithmException {
222+
223+
/* Skip test if HmacSHA3-384 is not supported by native wolfSSL */
224+
if (!FeatureDetect.HmacSha3_384Enabled()) {
225+
return;
226+
}
227+
228+
testKeyGeneration("HmacSHA3-384", new int[] { 384 });
229+
testKeyGenerationDefaultKeySize("HmacSHA3-384",
230+
Sha3.DIGEST_SIZE_384 * 8);
231+
}
232+
233+
@Test
234+
public void testHmacSHA3_512KeyGeneration()
235+
throws NoSuchProviderException, NoSuchAlgorithmException {
236+
237+
/* Skip test if HmacSHA3-512 is not supported by native wolfSSL */
238+
if (!FeatureDetect.HmacSha3_512Enabled()) {
239+
return;
240+
}
241+
242+
testKeyGeneration("HmacSHA3-512", new int[] { 512 });
243+
testKeyGenerationDefaultKeySize("HmacSHA3-512",
244+
Sha3.DIGEST_SIZE_512 * 8);
245+
}
246+
169247
/**
170248
* Test that KeyGenerator generates expected default sized keys when
171249
* no size is explicitly given.

0 commit comments

Comments
 (0)