Download this file

CMac.java    371 lines (307 with data), 10.5 kB

  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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package org.bouncycastle.crypto.macs;
/*
* This is a modified version of Bouncycastles Bcrypt,
* that allows block sizes > 128.
* This class was modified for Peafactory, but the
* modification may become a part of Bouncycastle later.
*/
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.ISO7816d4Padding;
import org.bouncycastle.crypto.params.KeyParameter;
/**
* CMAC - as specified at www.nuee.nagoya-u.ac.jp/labs/tiwata/omac/omac.html
* <p>
* CMAC is analogous to OMAC1 - see also en.wikipedia.org/wiki/CMAC
* </p><p>
* CMAC is a NIST recomendation - see
* csrc.nist.gov/CryptoToolkit/modes/800-38_Series_Publications/SP800-38B.pdf
* </p><p>
* CMAC/OMAC1 is a blockcipher-based message authentication code designed and
* analyzed by Tetsu Iwata and Kaoru Kurosawa.
* </p><p>
* CMAC/OMAC1 is a simple variant of the CBC MAC (Cipher Block Chaining Message
* Authentication Code). OMAC stands for One-Key CBC MAC.
* </p><p>
* It supports 128- or 64-bits block ciphers, with any key size, and returns
* a MAC with dimension less or equal to the block size of the underlying
* cipher.
* </p>
*/
public class CMac implements Mac
{
private static final byte CONSTANT_64 = (byte)0x1b;
private static final byte CONSTANT_128 = (byte)0x87;
//========================================================
//=================== this was modified: =================
// for constants see http://www.hpl.hp.com/techreports/98/HPL-98-135.pdf
private static final byte CONSTANT_160 = (byte)0x2d;//Rijndael(160)
private static final byte CONSTANT_192 = (byte)0x87;//Rijndael(192)
private static final byte CONSTANT_224a = (byte)0x3;//Rijndael(224)
private static final byte CONSTANT_224b = (byte)0x9;
private static final byte CONSTANT_256a = (byte)0x4;//Threefish, Shacal-2, Rijndael(256)
private static final byte CONSTANT_256b = (byte)0x425;
private static final byte CONSTANT_512a = (byte)0x1;//Threefish(512)
private static final byte CONSTANT_512b = (byte)0x25;
private static final byte CONSTANT_1024a = (byte)0x8;//Threefish(1024)
private static final byte CONSTANT_1024b = (byte)0x0;
private static final byte CONSTANT_1024c = (byte)0x43;
private byte[] ZEROES;
private byte[] mac;
private byte[] buf;
private int bufOff;
private BlockCipher cipher;
private int macSize;
private byte[] L, Lu, Lu2;
/**
* create a standard MAC based on a CBC block cipher (64 or 128 bit block).
* This will produce an authentication code the length of the block size
* of the cipher.
*
* @param cipher the cipher to be used as the basis of the MAC generation.
*/
public CMac(BlockCipher cipher)
{
this(cipher, cipher.getBlockSize() * 8);
}
/**
* create a standard MAC based on a block cipher with the size of the
* MAC been given in bits.
* <p>
* Note: the size of the MAC must be at least 24 bits (FIPS Publication 81),
* or 16 bits if being used as a data authenticator (FIPS Publication 113),
* and in general should be less than the size of the block cipher as it reduces
* the chance of an exhaustive attack (see Handbook of Applied Cryptography).
*
* @param cipher the cipher to be used as the basis of the MAC generation.
* @param macSizeInBits the size of the MAC in bits, must be a multiple of 8 and &lt;= 128.
*/
public CMac(BlockCipher cipher, int macSizeInBits)
{
if ((macSizeInBits % 8) != 0)
{
throw new IllegalArgumentException("MAC size must be multiple of 8");
}
if (macSizeInBits > (cipher.getBlockSize() * 8))
{
throw new IllegalArgumentException(
"MAC size must be less or equal to "
+ (cipher.getBlockSize() * 8));
}
//========================================================
//=================== this was modified: =================
int blockSize = cipher.getBlockSize();
if (blockSize != 8 && blockSize != 16
&& blockSize != 20 && blockSize != 24
&& blockSize != 28 && blockSize != 32
&& blockSize != 64 && blockSize != 128)
{
throw new IllegalArgumentException(
"Block size must be 64, 128, 160, 192, 224, 256, 512 or 1024 bits");
}
this.cipher = new CBCBlockCipher(cipher);
this.macSize = macSizeInBits / 8;
mac = new byte[cipher.getBlockSize()];
buf = new byte[cipher.getBlockSize()];
ZEROES = new byte[cipher.getBlockSize()];
bufOff = 0;
}
public String getAlgorithmName()
{
return cipher.getAlgorithmName();
}
private static int shiftLeft(byte[] block, byte[] output)
{
int i = block.length;
int bit = 0;
while (--i >= 0)
{
int b = block[i] & 0xff;
output[i] = (byte)((b << 1) | bit);
bit = (b >>> 7) & 1;
}
return bit;
}
//========================================================
//=================== this was modified: =================
private static byte[] doubleLu(byte[] in)
{
byte[] ret = new byte[in.length];
int carry = shiftLeft(in, ret);
int xor = 0;
switch(in.length)
{
case 8:
{
/*
* NOTE: This construction is an attempt at a constant-time implementation.
*/
xor = 0xff & CONSTANT_64;
ret[in.length - 1] ^= (xor >>> ((1 - carry) << 3));
break;
// not constant-time:
// if(carry != 0) ret[in.length - 1] ^= CONSTANT_64;
}
case 16:
{
xor = 0xff & CONSTANT_128;
ret[in.length - 1] ^= (xor >>> ((1 - carry) << 3));
break;
}
case 20:
{
xor = 0xff & CONSTANT_160;
ret[in.length - 1] ^= (xor >>> ((1 - carry) << 3));
break;
}
case 24:
{
xor = 0xff & CONSTANT_192;
ret[in.length - 1] ^= (xor >>> ((1 - carry) << 3));
break;
}
case 28:
{
xor = 0xff & CONSTANT_224a;
ret[in.length - 2] ^= (xor >>> ((1 - carry) << 3));
xor = 0xff & CONSTANT_224b;
ret[in.length - 1] ^= (xor >>> ((1 - carry) << 3));
break;
}
case 32:
{
xor = 0xff & CONSTANT_256a;
ret[in.length - 2] ^= (xor >>> ((1 - carry) << 3));
xor = 0xff & CONSTANT_256b;
ret[in.length - 1] ^= (xor >>> ((1 - carry) << 3));
break;
}
case 64:
{
xor = 0xff & CONSTANT_512a;
ret[in.length - 2] ^= (xor >>> ((1 - carry) << 3));
xor = 0xff & CONSTANT_512b;
ret[in.length - 1] ^= (xor >>> ((1 - carry) << 3));
break;
}
case 128:
{
xor = 0xff & CONSTANT_1024a;
ret[in.length - 3] ^= (xor >>> ((1 - carry) << 3));
xor = 0xff & CONSTANT_1024b;
ret[in.length - 2] ^= (xor >>> ((1 - carry) << 3));
xor = 0xff & CONSTANT_1024c;
ret[in.length - 1] ^= (xor >>> ((1 - carry) << 3));
break;
}
default: // this was checked before...
{
throw new IllegalArgumentException("Invalid block size.");
}
}
return ret;
}
public void init(CipherParameters params)
{
validate(params);
cipher.init(true, params);
//initializes the L, Lu, Lu2 numbers
L = new byte[ZEROES.length];
cipher.processBlock(ZEROES, 0, L, 0);
Lu = doubleLu(L);
Lu2 = doubleLu(Lu);
reset();
}
void validate(CipherParameters params)
{
if (params != null)
{
if (!(params instanceof KeyParameter))
{
// CMAC mode does not permit IV to underlying CBC mode
throw new IllegalArgumentException("CMac mode only permits key to be set.");
}
}
}
public int getMacSize()
{
return macSize;
}
public void update(byte in)
{
if (bufOff == buf.length)
{
cipher.processBlock(buf, 0, mac, 0);
bufOff = 0;
}
buf[bufOff++] = in;
}
public void update(byte[] in, int inOff, int len)
{
if (len < 0)
{
throw new IllegalArgumentException(
"Can't have a negative input length!");
}
int blockSize = cipher.getBlockSize();
int gapLen = blockSize - bufOff;
if (len > gapLen)
{
System.arraycopy(in, inOff, buf, bufOff, gapLen);
cipher.processBlock(buf, 0, mac, 0);
bufOff = 0;
len -= gapLen;
inOff += gapLen;
while (len > blockSize)
{
cipher.processBlock(in, inOff, mac, 0);
len -= blockSize;
inOff += blockSize;
}
}
System.arraycopy(in, inOff, buf, bufOff, len);
bufOff += len;
}
public int doFinal(byte[] out, int outOff)
{
int blockSize = cipher.getBlockSize();
byte[] lu;
if (bufOff == blockSize)
{
lu = Lu;
}
else
{
new ISO7816d4Padding().addPadding(buf, bufOff);
lu = Lu2;
}
for (int i = 0; i < mac.length; i++)
{
buf[i] ^= lu[i];
}
cipher.processBlock(buf, 0, mac, 0);
System.arraycopy(mac, 0, out, outOff, macSize);
reset();
return macSize;
}
/**
* Reset the mac generator.
*/
public void reset()
{
/*
* clean the buffer.
*/
for (int i = 0; i < buf.length; i++)
{
buf[i] = 0;
}
bufOff = 0;
/*
* reset the underlying cipher.
*/
cipher.reset();
}
}