Skip to content

Commit f665b74

Browse files
committed
JCE: add Mac AESCMAC / AES-CMAC support
1 parent 968cf09 commit f665b74

File tree

8 files changed

+847
-43
lines changed

8 files changed

+847
-43
lines changed

README_JCE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ The JCE provider currently supports the following algorithms:
115115
RSA/ECB/PKCS1Padding
116116

117117
Mac Class
118+
AESCMAC (aliased also as: AES-CMAC)
118119
HmacMD5
119120
HmacSHA1
120121
HmacSHA224

jni/jni_aescmac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_AesCmac_wc_1CmacUpdate__Ljava_
242242
JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_wolfcrypt_AesCmac_wc_1CmacFinal(
243243
JNIEnv* env, jobject this)
244244
{
245+
jbyteArray result = NULL;
245246
#ifdef WOLFSSL_CMAC
246247
int ret = 0;
247-
jbyteArray result = NULL;
248248
word32 macSz = AES_BLOCK_SIZE;
249249
Cmac* cmac = NULL;
250250
byte tmp[AES_BLOCK_SIZE];

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

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@
3737
import com.wolfssl.wolfcrypt.Sha512;
3838
import com.wolfssl.wolfcrypt.Sha3;
3939
import com.wolfssl.wolfcrypt.Hmac;
40+
import com.wolfssl.wolfcrypt.AesCmac;
41+
import com.wolfssl.wolfcrypt.Aes;
4042

4143
/**
4244
* wolfCrypt JCE Mac wrapper
4345
*/
4446
public class WolfCryptMac extends MacSpi {
4547

46-
enum HmacType {
48+
enum MacType {
4749
WC_HMAC_MD5,
4850
WC_HMAC_SHA,
4951
WC_HMAC_SHA224,
@@ -53,75 +55,93 @@ enum HmacType {
5355
WC_HMAC_SHA3_224,
5456
WC_HMAC_SHA3_256,
5557
WC_HMAC_SHA3_384,
56-
WC_HMAC_SHA3_512
58+
WC_HMAC_SHA3_512,
59+
WC_AES_CMAC
5760
}
5861

5962
private Hmac hmac = null;
63+
private AesCmac aesCmac = null;
6064
private int nativeHmacType = 0;
6165
private int digestSize = 0;
66+
private MacType macType;
6267

6368
/* for debug logging */
6469
private String algString;
6570

66-
private WolfCryptMac(HmacType type)
71+
private WolfCryptMac(MacType type)
6772
throws NoSuchAlgorithmException {
6873

69-
hmac = new Hmac();
74+
this.macType = type;
7075

7176
switch (type) {
7277
case WC_HMAC_MD5:
78+
hmac = new Hmac();
7379
this.digestSize = Md5.DIGEST_SIZE;
7480
this.nativeHmacType = Hmac.MD5;
7581
break;
7682

7783
case WC_HMAC_SHA:
84+
hmac = new Hmac();
7885
this.digestSize = Sha.DIGEST_SIZE;
7986
this.nativeHmacType = Hmac.SHA;
8087
break;
8188

8289
case WC_HMAC_SHA224:
90+
hmac = new Hmac();
8391
this.digestSize = Sha224.DIGEST_SIZE;
8492
this.nativeHmacType = Hmac.SHA224;
8593
break;
8694

8795
case WC_HMAC_SHA256:
96+
hmac = new Hmac();
8897
this.digestSize = Sha256.DIGEST_SIZE;
8998
this.nativeHmacType = Hmac.SHA256;
9099
break;
91100

92101
case WC_HMAC_SHA384:
102+
hmac = new Hmac();
93103
this.digestSize = Sha384.DIGEST_SIZE;
94104
this.nativeHmacType = Hmac.SHA384;
95105
break;
96106

97107
case WC_HMAC_SHA512:
108+
hmac = new Hmac();
98109
this.digestSize = Sha512.DIGEST_SIZE;
99110
this.nativeHmacType = Hmac.SHA512;
100111
break;
101112

102113
case WC_HMAC_SHA3_224:
114+
hmac = new Hmac();
103115
this.digestSize = Sha3.DIGEST_SIZE_224;
104116
this.nativeHmacType = Hmac.SHA3_224;
105117
break;
106118

107119
case WC_HMAC_SHA3_256:
120+
hmac = new Hmac();
108121
this.digestSize = Sha3.DIGEST_SIZE_256;
109122
this.nativeHmacType = Hmac.SHA3_256;
110123
break;
111124

112125
case WC_HMAC_SHA3_384:
126+
hmac = new Hmac();
113127
this.digestSize = Sha3.DIGEST_SIZE_384;
114128
this.nativeHmacType = Hmac.SHA3_384;
115129
break;
116130

117131
case WC_HMAC_SHA3_512:
132+
hmac = new Hmac();
118133
this.digestSize = Sha3.DIGEST_SIZE_512;
119134
this.nativeHmacType = Hmac.SHA3_512;
120135
break;
121136

137+
case WC_AES_CMAC:
138+
aesCmac = new AesCmac();
139+
this.digestSize = Aes.BLOCK_SIZE;
140+
break;
141+
122142
default:
123143
throw new NoSuchAlgorithmException(
124-
"Unsupported HMAC type");
144+
"Unsupported MAC type");
125145
}
126146

127147
if (WolfCryptDebug.DEBUG) {
@@ -132,7 +152,13 @@ private WolfCryptMac(HmacType type)
132152
@Override
133153
protected byte[] engineDoFinal() {
134154

135-
byte[] out = this.hmac.doFinal();
155+
byte[] out = null;
156+
157+
if (macType == MacType.WC_AES_CMAC) {
158+
out = this.aesCmac.doFinal();
159+
} else {
160+
out = this.hmac.doFinal();
161+
}
136162

137163
if (out != null) {
138164
log("final digest generated, len: " + out.length);
@@ -163,33 +189,53 @@ protected void engineInit(Key key, AlgorithmParameterSpec params)
163189
if (encodedKey == null)
164190
throw new InvalidKeyException("Key does not support encoding");
165191

166-
this.hmac.setKey(nativeHmacType, encodedKey);
192+
try {
193+
if (macType == MacType.WC_AES_CMAC) {
194+
this.aesCmac.setKey(encodedKey);
195+
} else {
196+
this.hmac.setKey(nativeHmacType, encodedKey);
197+
}
198+
} catch (com.wolfssl.wolfcrypt.WolfCryptException e) {
199+
throw new InvalidKeyException("Invalid key: " + e.getMessage());
200+
}
167201

168202
log("init with key and spec");
169203
}
170204

171205
@Override
172206
protected void engineReset() {
173-
this.hmac.reset();
207+
if (macType == MacType.WC_AES_CMAC) {
208+
this.aesCmac.reset();
209+
} else {
210+
this.hmac.reset();
211+
}
174212

175213
log("engine reset");
176214
}
177215

178216
@Override
179217
protected void engineUpdate(byte input) {
180-
this.hmac.update(input);
218+
if (macType == MacType.WC_AES_CMAC) {
219+
this.aesCmac.update(input);
220+
} else {
221+
this.hmac.update(input);
222+
}
181223

182224
log("update with single byte");
183225
}
184226

185227
@Override
186228
protected void engineUpdate(byte[] input, int offset, int len) {
187-
this.hmac.update(input, offset, len);
229+
if (macType == MacType.WC_AES_CMAC) {
230+
this.aesCmac.update(input, offset, len);
231+
} else {
232+
this.hmac.update(input, offset, len);
233+
}
188234

189235
log("update, offset: " + offset + ", len: " + len);
190236
}
191237

192-
private String typeToString(HmacType type) {
238+
private String typeToString(MacType type) {
193239
switch (type) {
194240
case WC_HMAC_MD5:
195241
return "MD5";
@@ -209,6 +255,10 @@ private String typeToString(HmacType type) {
209255
return "SHA3-256";
210256
case WC_HMAC_SHA3_384:
211257
return "SHA3-384";
258+
case WC_HMAC_SHA3_512:
259+
return "SHA3-512";
260+
case WC_AES_CMAC:
261+
return "AES-CMAC";
212262
default:
213263
return "None";
214264
}
@@ -225,6 +275,8 @@ protected void finalize() throws Throwable {
225275
try {
226276
if (this.hmac != null)
227277
this.hmac.releaseNativeStruct();
278+
if (this.aesCmac != null)
279+
this.aesCmac.releaseNativeStruct();
228280
} finally {
229281
super.finalize();
230282
}
@@ -241,7 +293,7 @@ public static final class wcHmacMD5 extends WolfCryptMac {
241293
* native wolfCrypt level.
242294
*/
243295
public wcHmacMD5() throws NoSuchAlgorithmException {
244-
super(HmacType.WC_HMAC_MD5);
296+
super(MacType.WC_HMAC_MD5);
245297
}
246298
}
247299

@@ -256,7 +308,7 @@ public static final class wcHmacSHA1 extends WolfCryptMac {
256308
* native wolfCrypt level.
257309
*/
258310
public wcHmacSHA1() throws NoSuchAlgorithmException {
259-
super(HmacType.WC_HMAC_SHA);
311+
super(MacType.WC_HMAC_SHA);
260312
}
261313
}
262314

@@ -271,7 +323,7 @@ public static final class wcHmacSHA224 extends WolfCryptMac {
271323
* native wolfCrypt level.
272324
*/
273325
public wcHmacSHA224() throws NoSuchAlgorithmException {
274-
super(HmacType.WC_HMAC_SHA224);
326+
super(MacType.WC_HMAC_SHA224);
275327
}
276328
}
277329

@@ -286,7 +338,7 @@ public static final class wcHmacSHA256 extends WolfCryptMac {
286338
* native wolfCrypt level.
287339
*/
288340
public wcHmacSHA256() throws NoSuchAlgorithmException {
289-
super(HmacType.WC_HMAC_SHA256);
341+
super(MacType.WC_HMAC_SHA256);
290342
}
291343
}
292344

@@ -301,7 +353,7 @@ public static final class wcHmacSHA384 extends WolfCryptMac {
301353
* native wolfCrypt level.
302354
*/
303355
public wcHmacSHA384() throws NoSuchAlgorithmException {
304-
super(HmacType.WC_HMAC_SHA384);
356+
super(MacType.WC_HMAC_SHA384);
305357
}
306358
}
307359

@@ -316,7 +368,7 @@ public static final class wcHmacSHA512 extends WolfCryptMac {
316368
* native wolfCrypt level.
317369
*/
318370
public wcHmacSHA512() throws NoSuchAlgorithmException {
319-
super(HmacType.WC_HMAC_SHA512);
371+
super(MacType.WC_HMAC_SHA512);
320372
}
321373
}
322374

@@ -331,7 +383,7 @@ public static final class wcHmacSHA3_224 extends WolfCryptMac {
331383
* native wolfCrypt level.
332384
*/
333385
public wcHmacSHA3_224() throws NoSuchAlgorithmException {
334-
super(HmacType.WC_HMAC_SHA3_224);
386+
super(MacType.WC_HMAC_SHA3_224);
335387
}
336388
}
337389

@@ -346,7 +398,7 @@ public static final class wcHmacSHA3_256 extends WolfCryptMac {
346398
* native wolfCrypt level.
347399
*/
348400
public wcHmacSHA3_256() throws NoSuchAlgorithmException {
349-
super(HmacType.WC_HMAC_SHA3_256);
401+
super(MacType.WC_HMAC_SHA3_256);
350402
}
351403
}
352404

@@ -361,7 +413,7 @@ public static final class wcHmacSHA3_384 extends WolfCryptMac {
361413
* native wolfCrypt level.
362414
*/
363415
public wcHmacSHA3_384() throws NoSuchAlgorithmException {
364-
super(HmacType.WC_HMAC_SHA3_384);
416+
super(MacType.WC_HMAC_SHA3_384);
365417
}
366418
}
367419

@@ -376,7 +428,22 @@ public static final class wcHmacSHA3_512 extends WolfCryptMac {
376428
* native wolfCrypt level.
377429
*/
378430
public wcHmacSHA3_512() throws NoSuchAlgorithmException {
379-
super(HmacType.WC_HMAC_SHA3_512);
431+
super(MacType.WC_HMAC_SHA3_512);
432+
}
433+
}
434+
435+
/**
436+
* wolfJCE AES-CMAC class
437+
*/
438+
public static final class wcAesCmac extends WolfCryptMac {
439+
/**
440+
* Create new wcAesCmac object
441+
*
442+
* @throws NoSuchAlgorithmException if AES-CMAC is not available at
443+
* native wolfCrypt level.
444+
*/
445+
public wcAesCmac() throws NoSuchAlgorithmException {
446+
super(MacType.WC_AES_CMAC);
380447
}
381448
}
382449
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ private void registerServices() {
209209
put("Mac.HmacSHA3-512",
210210
"com.wolfssl.provider.jce.WolfCryptMac$wcHmacSHA3_512");
211211
}
212+
if (FeatureDetect.AesCmacEnabled()) {
213+
put("Mac.AESCMAC",
214+
"com.wolfssl.provider.jce.WolfCryptMac$wcAesCmac");
215+
put("Alg.Alias.Mac.AES-CMAC", "AESCMAC");
216+
}
212217

213218
/* Cipher */
214219
if (FeatureDetect.AesCbcEnabled()) {

0 commit comments

Comments
 (0)