-
Notifications
You must be signed in to change notification settings - Fork 5
/
Hmac.java
351 lines (342 loc) · 13.3 KB
/
Hmac.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import javacard.framework.*;
import javacard.security.*;
/*
* This implementation of HMAC is a wrapper that either uses a native implementation
* (ALG_HMAC_SHA_256 and so on), or fall back to a pure software implementation.
* The software implementation optionally uses masking to try limiting some leaks.
*/
public class Hmac {
/* !!WARNING: using native HMAC can provoke unexpected errors on
* cards that do not retur an error when getting an instance, but
* raise an exception when using the instance ... Turn to 'false'
* to fall back to software HMAC if this happens.
*/
private static final boolean TRY_USE_NATIVE_HMAC = true;
/* If we have native HMAC support, use it */
private boolean use_native_hmac = false;
Signature hmac_instance = null;
HMACKey hmac_key = null;
/* The message digest instances */
private MessageDigest md_i = null;
private MessageDigest md_o = null;
private MessageDigest local_md = null;
private byte[] ipad = null;
private byte[] opad = null;
private byte[] local_key;
private byte[] dgst_i;
private byte digest = 0;
/* For random masking */
/* !!WARNING: the ipad/opad masking can be hard on the eeprom initialization, and some
* Javacards won't be compatible with this. Turn to false if you have an error at
* first applet selection when eeprom is initialized by the applet ...
* (masking has been tested to be working on NXP J3D081 smart cards)
*/
private static final boolean USE_HMAC_MASKING = true;
private RandomData random = null;
private byte[] orig_ipad_masks = null;
private byte[] orig_opad_masks = null;
private byte[] ipad_masks = null;
private byte[] opad_masks = null;
protected Hmac(byte digest_type){
if(TRY_USE_NATIVE_HMAC == true){
/* Check native HMAC support on the card
* NOTE: HMAC implementation is unfortunately not present on the tested cards so far ...
* but we prepare its usage on compatible cards!
*/
use_native_hmac = true;
try {
switch(digest_type){
case MessageDigest.ALG_SHA_224:
CryptoException.throwIt(CryptoException.NO_SUCH_ALGORITHM);
break;
case MessageDigest.ALG_SHA_256:
try {
hmac_key = (HMACKey) KeyBuilder.buildKey(KeyBuilder.TYPE_HMAC_TRANSIENT_DESELECT, KeyBuilder.LENGTH_HMAC_SHA_256_BLOCK_64, true);
}
catch(CryptoException e){
hmac_key = (HMACKey) KeyBuilder.buildKey(KeyBuilder.TYPE_HMAC_TRANSIENT_DESELECT, KeyBuilder.LENGTH_HMAC_SHA_256_BLOCK_64, false);
}
hmac_instance = Signature.getInstance(Signature.ALG_HMAC_SHA_256, false);
break;
case MessageDigest.ALG_SHA_384:
try {
hmac_key = (HMACKey) KeyBuilder.buildKey(KeyBuilder.TYPE_HMAC_TRANSIENT_DESELECT, KeyBuilder.LENGTH_HMAC_SHA_384_BLOCK_128, true);
}
catch(CryptoException e){
hmac_key = (HMACKey) KeyBuilder.buildKey(KeyBuilder.TYPE_HMAC_TRANSIENT_DESELECT, KeyBuilder.LENGTH_HMAC_SHA_384_BLOCK_128, false);
}
hmac_instance = Signature.getInstance(Signature.ALG_HMAC_SHA_384, false);
break;
case MessageDigest.ALG_SHA_512:
try {
hmac_key = (HMACKey) KeyBuilder.buildKey(KeyBuilder.TYPE_HMAC_TRANSIENT_DESELECT, KeyBuilder.LENGTH_HMAC_SHA_512_BLOCK_128, true);
}
catch(CryptoException e){
hmac_key = (HMACKey) KeyBuilder.buildKey(KeyBuilder.TYPE_HMAC_TRANSIENT_DESELECT, KeyBuilder.LENGTH_HMAC_SHA_512_BLOCK_128, false);
}
hmac_instance = Signature.getInstance(Signature.ALG_HMAC_SHA_512, false);
break;
}
}
catch(CryptoException exception){
/* If we got an exception, this means that the card does not have a
* native HMAC support. Fallback to the software one!
*/
use_native_hmac = false;
}
}
if(use_native_hmac == false){
if((ipad != null) || (opad != null) || (md_i != null) || (md_o != null)){
CryptoException.throwIt(CryptoException.INVALID_INIT);
}
switch(digest_type){
case MessageDigest.ALG_SHA_224:
case MessageDigest.ALG_SHA_256:
case MessageDigest.ALG_SHA_384:
case MessageDigest.ALG_SHA_512:
md_i = MessageDigest.getInstance(digest_type, false);
md_o = MessageDigest.getInstance(digest_type, false);
local_md = MessageDigest.getInstance(digest_type, false);
/**/
ipad = JCSystem.makeTransientByteArray((short) (2 * md_i.getLength()), JCSystem.CLEAR_ON_DESELECT);
opad = JCSystem.makeTransientByteArray((short) (2 * md_i.getLength()), JCSystem.CLEAR_ON_DESELECT);
/**/
if(USE_HMAC_MASKING == true){
random = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
orig_ipad_masks = new byte[ipad.length];
random.generateData(orig_ipad_masks, (short) 0, (short) orig_ipad_masks.length);
orig_opad_masks = new byte[opad.length];
random.generateData(orig_opad_masks, (short) 0, (short) orig_opad_masks.length);
ipad_masks = JCSystem.makeTransientByteArray((short) ipad.length, JCSystem.CLEAR_ON_DESELECT);
opad_masks = JCSystem.makeTransientByteArray((short) opad.length, JCSystem.CLEAR_ON_DESELECT);
}
/**/
local_key = JCSystem.makeTransientByteArray((short) (md_i.getLength()), JCSystem.CLEAR_ON_DESELECT);
dgst_i = JCSystem.makeTransientByteArray((short) (md_i.getLength()), JCSystem.CLEAR_ON_DESELECT);
break;
default:
CryptoException.throwIt(CryptoException.NO_SUCH_ALGORITHM);
}
digest = digest_type;
}
}
public void hmac_init(byte[] key){
if(use_native_hmac == true){
hmac_key.setKey(key, (short) 0, (short) key.length);
hmac_instance.init(hmac_key, Signature.MODE_SIGN);
return;
}
else{
try {
/* In order to limit a possible leakage of the key, we use some basic masking for ipad and opad */
short i;
md_i.reset();
md_o.reset();
if(USE_HMAC_MASKING == true){
random.generateData(ipad_masks, (short) 0, (short) ipad_masks.length);
for(i = 0; i < (short) ipad.length; i++){
ipad[i] = (byte)(ipad_masks[i] ^ orig_ipad_masks[i]);
}
random.generateData(opad_masks, (short) 0, (short) opad_masks.length);
for(i = 0; i < (short) opad.length; i++){
opad[i] = (byte)(opad_masks[i] ^ orig_opad_masks[i]);
}
}
if(key.length > ipad.length){
/* Key length is > block size */
local_md.reset();
local_md.update(key, (short) 0, (short) key.length);
/* [RB] NOTE: some javacard throw an exception when the input buffer is null even if the size is 0 ...
* Hence, we use a dummy value
*/
local_md.doFinal(ipad, (short) 0, (short) 0, local_key, (short) 0);
for(i = 0; i < ipad.length; i++){
if(i < local_key.length){
if(USE_HMAC_MASKING == true){
byte msk = (byte)(ipad_masks[i] ^ orig_ipad_masks[i] ^ 0x36);
ipad[i] ^= (local_key[i] ^ msk);
msk = (byte)(opad_masks[i] ^ orig_opad_masks[i] ^ 0x5c);
opad[i] ^= (local_key[i] ^ msk);
}
else{
ipad[i] = (byte)(local_key[i] ^ 0x36);
opad[i] = (byte)(local_key[i] ^ 0x5c);
}
}
else{
if(USE_HMAC_MASKING == true){
byte msk = (byte)(ipad_masks[i] ^ orig_ipad_masks[i] ^ 0x36);
ipad[i] ^= msk;
msk = (byte)(opad_masks[i] ^ orig_opad_masks[i] ^ 0x5c);
opad[i] ^= msk;
}
else{
ipad[i] = 0x36;
opad[i] = 0x5c;
}
}
}
}
else{
/* Key length is <= block size */
for(i = 0; i < ipad.length; i++){
if(i < key.length){
if(USE_HMAC_MASKING == true){
byte msk = (byte)(ipad_masks[i] ^ orig_ipad_masks[i] ^ 0x36);
ipad[i] ^= (key[i] ^ msk);
msk = (byte)(opad_masks[i] ^ orig_opad_masks[i] ^ 0x5c);
opad[i] ^= (key[i] ^ msk);
}
else{
ipad[i] = (byte)(key[i] ^ 0x36);
opad[i] = (byte)(key[i] ^ 0x5c);
}
}
else{
if(USE_HMAC_MASKING == true){
byte msk = (byte)(ipad_masks[i] ^ orig_ipad_masks[i] ^ 0x36);
ipad[i] ^= msk;
msk = (byte)(opad_masks[i] ^ orig_opad_masks[i] ^ 0x5c);
opad[i] ^= msk;
}
else{
ipad[i] = 0x36;
opad[i] = 0x5c;
}
}
}
}
md_i.update(ipad, (short) 0, (short) ipad.length);
md_o.update(opad, (short) 0, (short) opad.length);
return;
}
catch(CryptoException exception)
{
switch(exception.getReason()){
case CryptoException.ILLEGAL_USE:
ISOException.throwIt((short) 0xAAD0);
break;
case CryptoException.ILLEGAL_VALUE:
ISOException.throwIt((short) 0xAAD1);
break;
case CryptoException.INVALID_INIT:
ISOException.throwIt((short) 0xAAD2);
break;
case CryptoException.NO_SUCH_ALGORITHM:
ISOException.throwIt((short) 0xAAD3);
break;
case CryptoException.UNINITIALIZED_KEY:
ISOException.throwIt((short) 0xAAD4);
break;
default:
ISOException.throwIt((short) 0xAAD5);
break;
}
}
return;
}
}
public void hmac_update(byte[] indata, short indataoffset, short indatalen){
if(use_native_hmac == true){
hmac_instance.update(indata, indataoffset, indatalen);
return;
}
else{
try{
/* Update the internal context */
md_i.update(indata, indataoffset, indatalen);
}
catch(CryptoException exception)
{
switch(exception.getReason()){
case CryptoException.ILLEGAL_USE:
ISOException.throwIt((short) 0xAAD0);
break;
case CryptoException.ILLEGAL_VALUE:
ISOException.throwIt((short) 0xAAD1);
break;
case CryptoException.INVALID_INIT:
ISOException.throwIt((short) 0xAAD2);
break;
case CryptoException.NO_SUCH_ALGORITHM:
ISOException.throwIt((short) 0xAAD3);
break;
case CryptoException.UNINITIALIZED_KEY:
ISOException.throwIt((short) 0xAAD4);
break;
default:
ISOException.throwIt((short) 0xAAD5);
break;
}
}
return;
}
}
public short hmac_finalize(byte[] hmac, short hmac_offset){
if(use_native_hmac == true){
hmac_instance.sign(null, (short) 0, (short) 0, hmac, hmac_offset);
return hmac_instance.getLength();
}
else{
try{
if((md_i == null) || (md_o == null)){
CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
}
/* Finalize the input hash */
/* [RB] NOTE: some cards throw an exception when the input buffer is null even if the size is 0 ...
* Hence, we us a dummy value with ipad
*/
md_i.doFinal(ipad, (short) 0, (short) 0, dgst_i, (short) 0);
md_o.doFinal(dgst_i, (short) 0, (short) dgst_i.length, hmac, hmac_offset);
return md_o.getLength();
}
catch(CryptoException exception)
{
switch(exception.getReason()){
case CryptoException.ILLEGAL_USE:
ISOException.throwIt((short) 0xAAD0);
break;
case CryptoException.ILLEGAL_VALUE:
ISOException.throwIt((short) 0xAAD1);
break;
case CryptoException.INVALID_INIT:
ISOException.throwIt((short) 0xAAD2);
break;
case CryptoException.NO_SUCH_ALGORITHM:
ISOException.throwIt((short) 0xAAD3);
break;
case CryptoException.UNINITIALIZED_KEY:
ISOException.throwIt((short) 0xAAD4);
break;
default:
ISOException.throwIt((short) 0xAAD5);
break;
}
}
return 0;
}
}
public short hmac_len(){
if(use_native_hmac == true){
return hmac_instance.getLength();
}
else{
if((md_i == null) || (md_o == null)){
CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
}
switch(digest){
case MessageDigest.ALG_SHA_224:
return 28;
case MessageDigest.ALG_SHA_256:
return 32;
case MessageDigest.ALG_SHA_384:
return 48;
case MessageDigest.ALG_SHA_512:
return 64;
default:
CryptoException.throwIt(CryptoException.NO_SUCH_ALGORITHM);
}
return 0;
}
}
}