comparison omac_init.c @ 3:7faae8f46238 libtomcrypt-orig

Branch renaming
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:25:41 +0000
parents
children 5d99163f7e32
comparison
equal deleted inserted replaced
-1:000000000000 3:7faae8f46238
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_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen)
17 {
18 int err, x, y, mask, msb, len;
19
20 _ARGCHK(omac != NULL);
21 _ARGCHK(key != NULL);
22
23 /* schedule the key */
24 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
25 return err;
26 }
27
28 /* now setup the system */
29 switch (cipher_descriptor[cipher].block_length) {
30 case 8: mask = 0x1B;
31 len = 8;
32 break;
33 case 16: mask = 0x87;
34 len = 16;
35 break;
36 default: return CRYPT_INVALID_ARG;
37 }
38
39 if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &omac->key)) != CRYPT_OK) {
40 return err;
41 }
42
43 /* ok now we need Lu and Lu^2 [calc one from the other] */
44
45 /* first calc L which is Ek(0) */
46 zeromem(omac->Lu[0], cipher_descriptor[cipher].block_length);
47 cipher_descriptor[cipher].ecb_encrypt(omac->Lu[0], omac->Lu[0], &omac->key);
48
49 /* now do the mults, whoopy! */
50 for (x = 0; x < 2; x++) {
51 /* if msb(L * u^(x+1)) = 0 then just shift, otherwise shift and xor constant mask */
52 msb = omac->Lu[x][0] >> 7;
53
54 /* shift left */
55 for (y = 0; y < (len - 1); y++) {
56 omac->Lu[x][y] = ((omac->Lu[x][y] << 1) | (omac->Lu[x][y+1] >> 7)) & 255;
57 }
58 omac->Lu[x][len - 1] = ((omac->Lu[x][len - 1] << 1) ^ (msb ? mask : 0)) & 255;
59
60 /* copy up as require */
61 if (x == 0) {
62 memcpy(omac->Lu[1], omac->Lu[0], sizeof(omac->Lu[0]));
63 }
64 }
65
66 /* setup state */
67 omac->cipher_idx = cipher;
68 omac->buflen = 0;
69 omac->blklen = len;
70 zeromem(omac->prev, sizeof(omac->prev));
71 zeromem(omac->block, sizeof(omac->block));
72
73 return CRYPT_OK;
74 }
75
76 #endif