3
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 /* OMAC1 Support by Tom St Denis (for 64 and 128 bit block ciphers only) */ |
|
12 #include "mycrypt.h" |
|
13 |
|
14 #ifdef OMAC |
|
15 |
|
16 int omac_done(omac_state *state, unsigned char *out, unsigned long *outlen) |
|
17 { |
143
|
18 int err, mode; |
|
19 unsigned x; |
3
|
20 |
143
|
21 _ARGCHK(state != NULL); |
|
22 _ARGCHK(out != NULL); |
|
23 _ARGCHK(outlen != NULL); |
3
|
24 if ((err = cipher_is_valid(state->cipher_idx)) != CRYPT_OK) { |
|
25 return err; |
|
26 } |
|
27 |
|
28 if ((state->buflen > (int)sizeof(state->block)) || (state->buflen < 0) || |
|
29 (state->blklen > (int)sizeof(state->block)) || (state->buflen > state->blklen)) { |
|
30 return CRYPT_INVALID_ARG; |
|
31 } |
|
32 |
|
33 /* figure out mode */ |
|
34 if (state->buflen != state->blklen) { |
|
35 /* add the 0x80 byte */ |
|
36 state->block[state->buflen++] = 0x80; |
|
37 |
|
38 /* pad with 0x00 */ |
|
39 while (state->buflen < state->blklen) { |
|
40 state->block[state->buflen++] = 0x00; |
|
41 } |
|
42 mode = 1; |
|
43 } else { |
|
44 mode = 0; |
|
45 } |
|
46 |
|
47 /* now xor prev + Lu[mode] */ |
143
|
48 for (x = 0; x < (unsigned)state->blklen; x++) { |
3
|
49 state->block[x] ^= state->prev[x] ^ state->Lu[mode][x]; |
|
50 } |
|
51 |
|
52 /* encrypt it */ |
|
53 cipher_descriptor[state->cipher_idx].ecb_encrypt(state->block, state->block, &state->key); |
|
54 |
|
55 /* output it */ |
143
|
56 for (x = 0; x < (unsigned)state->blklen && x < *outlen; x++) { |
3
|
57 out[x] = state->block[x]; |
|
58 } |
|
59 *outlen = x; |
|
60 |
|
61 #ifdef CLEAN_STACK |
|
62 zeromem(state, sizeof(*state)); |
|
63 #endif |
|
64 return CRYPT_OK; |
|
65 } |
|
66 |
|
67 #endif |
|
68 |