Skip to content

Commit aa49b15

Browse files
committed
JCE: add HmacSHA224 support to KeyGenerator implementation
1 parent 9e025b7 commit aa49b15

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

README_JCE.md

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ The JCE provider currently supports the following algorithms:
134134
KeyGenerator
135135
AES
136136
HmacSHA1
137+
HmacSHA224
137138
HmacSHA256
138139
HmacSHA384
139140
HmacSHA512

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.wolfssl.wolfcrypt.Fips;
2525
import com.wolfssl.wolfcrypt.Aes;
26+
import com.wolfssl.wolfcrypt.Sha224;
2627
import com.wolfssl.wolfcrypt.Sha256;
2728
import com.wolfssl.wolfcrypt.Sha384;
2829
import com.wolfssl.wolfcrypt.Sha512;
@@ -45,6 +46,7 @@ enum AlgoType {
4546
WC_INVALID,
4647
WC_AES,
4748
WC_HMAC_SHA1,
49+
WC_HMAC_SHA224,
4850
WC_HMAC_SHA256,
4951
WC_HMAC_SHA384,
5052
WC_HMAC_SHA512
@@ -54,7 +56,6 @@ enum AlgoType {
5456
private String algString = null;
5557

5658
private int keySizeBits = 0;
57-
private AlgorithmParameterSpec algoParams = null;
5859
private SecureRandom random = null;
5960

6061
/**
@@ -75,6 +76,10 @@ private WolfCryptKeyGenerator(AlgoType type) {
7576
/* SunJCE default key size for HmacSHA1 is 64 bytes */
7677
this.keySizeBits = (Sha512.DIGEST_SIZE * 8);
7778
break;
79+
case WC_HMAC_SHA224:
80+
this.algString = "HmacSHA224";
81+
this.keySizeBits = (Sha224.DIGEST_SIZE * 8);
82+
break;
7883
case WC_HMAC_SHA256:
7984
this.algString = "HmacSHA256";
8085
this.keySizeBits = (Sha256.DIGEST_SIZE * 8);
@@ -222,6 +227,7 @@ protected SecretKey engineGenerateKey() {
222227
switch (this.algoType) {
223228
case WC_AES:
224229
case WC_HMAC_SHA1:
230+
case WC_HMAC_SHA224:
225231
case WC_HMAC_SHA256:
226232
case WC_HMAC_SHA384:
227233
case WC_HMAC_SHA512:
@@ -259,6 +265,20 @@ public wcHMACSha1KeyGenerator() {
259265
}
260266
}
261267

268+
/**
269+
* KeyGenerator(HmacSHA224) class, called by WolfCryptProvider.
270+
*/
271+
public static final class wcHMACSha224KeyGenerator
272+
extends WolfCryptKeyGenerator {
273+
274+
/**
275+
* Constructor for wcHMACSha224KeyGenerator.
276+
*/
277+
public wcHMACSha224KeyGenerator() {
278+
super(AlgoType.WC_HMAC_SHA224);
279+
}
280+
}
281+
262282
/**
263283
* KeyGenerator(HmacSHA256) class, called by WolfCryptProvider.
264284
*/

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

+24-10
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,30 @@ private void registerServices() {
201201
}
202202

203203
/* KeyGenerator */
204-
put("KeyGenerator.AES",
205-
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcAESKeyGenerator");
206-
put("KeyGenerator.HmacSHA1",
207-
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha1KeyGenerator");
208-
put("KeyGenerator.HmacSHA256",
209-
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha256KeyGenerator");
210-
put("KeyGenerator.HmacSHA384",
211-
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha384KeyGenerator");
212-
put("KeyGenerator.HmacSHA512",
213-
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha512KeyGenerator");
204+
if (FeatureDetect.AesEnabled()) {
205+
put("KeyGenerator.AES",
206+
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcAESKeyGenerator");
207+
}
208+
if (FeatureDetect.HmacShaEnabled()) {
209+
put("KeyGenerator.HmacSHA1",
210+
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha1KeyGenerator");
211+
}
212+
if (FeatureDetect.HmacSha224Enabled()) {
213+
put("KeyGenerator.HmacSHA224",
214+
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha224KeyGenerator");
215+
}
216+
if (FeatureDetect.HmacSha256Enabled()) {
217+
put("KeyGenerator.HmacSHA256",
218+
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha256KeyGenerator");
219+
}
220+
if (FeatureDetect.HmacSha384Enabled()) {
221+
put("KeyGenerator.HmacSHA384",
222+
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha384KeyGenerator");
223+
}
224+
if (FeatureDetect.HmacSha512Enabled()) {
225+
put("KeyGenerator.HmacSHA512",
226+
"com.wolfssl.provider.jce.WolfCryptKeyGenerator$wcHMACSha512KeyGenerator");
227+
}
214228

215229
/* KeyPairGenerator */
216230
if (FeatureDetect.RsaKeyGenEnabled()) {

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

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import com.wolfssl.wolfcrypt.Fips;
4444
import com.wolfssl.wolfcrypt.Aes;
45+
import com.wolfssl.wolfcrypt.Sha224;
4546
import com.wolfssl.wolfcrypt.Sha256;
4647
import com.wolfssl.wolfcrypt.Sha384;
4748
import com.wolfssl.wolfcrypt.Sha512;
@@ -52,6 +53,7 @@ public class WolfCryptKeyGeneratorTest {
5253
private static String[] keyAlgorithms = {
5354
"AES",
5455
"HmacSHA1",
56+
"HmacSHA224",
5557
"HmacSHA256",
5658
"HmacSHA384",
5759
"HmacSHA512"
@@ -121,6 +123,14 @@ public void testHmacSHA1KeyGeneration()
121123
testKeyGenerationDefaultKeySize("HmacSHA1", Sha512.DIGEST_SIZE * 8);
122124
}
123125

126+
@Test
127+
public void testHmacSHA224KeyGeneration()
128+
throws NoSuchProviderException, NoSuchAlgorithmException {
129+
130+
testKeyGeneration("HmacSHA224", new int[] { 224 });
131+
testKeyGenerationDefaultKeySize("HmacSHA224", Sha224.DIGEST_SIZE * 8);
132+
}
133+
124134
@Test
125135
public void testHmacSHA256KeyGeneration()
126136
throws NoSuchProviderException, NoSuchAlgorithmException {

0 commit comments

Comments
 (0)