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 |
|
12 /* EAX Implementation by Tom St Denis */ |
|
13 #include "mycrypt.h" |
|
14 |
|
15 #ifdef EAX_MODE |
|
16 |
|
17 int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen, |
|
18 const unsigned char *nonce, unsigned long noncelen, |
|
19 const unsigned char *header, unsigned long headerlen) |
|
20 { |
143
|
21 unsigned char *buf; |
3
|
22 int err, blklen; |
143
|
23 omac_state *omac; |
3
|
24 unsigned long len; |
|
25 |
|
26 |
|
27 _ARGCHK(eax != NULL); |
|
28 _ARGCHK(key != NULL); |
|
29 _ARGCHK(nonce != NULL); |
|
30 if (headerlen > 0) { |
|
31 _ARGCHK(header != NULL); |
|
32 } |
|
33 |
|
34 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { |
|
35 return err; |
|
36 } |
|
37 blklen = cipher_descriptor[cipher].block_length; |
|
38 |
143
|
39 /* allocate ram */ |
|
40 buf = XMALLOC(MAXBLOCKSIZE); |
|
41 omac = XMALLOC(sizeof(omac_state)); |
|
42 |
|
43 if (buf == NULL || omac == NULL) { |
|
44 if (buf != NULL) { |
|
45 XFREE(buf); |
|
46 } |
|
47 if (omac != NULL) { |
|
48 XFREE(omac); |
|
49 } |
|
50 return CRYPT_MEM; |
|
51 } |
|
52 |
3
|
53 /* N = OMAC_0K(nonce) */ |
143
|
54 zeromem(buf, MAXBLOCKSIZE); |
|
55 if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) { |
|
56 goto __ERR; |
3
|
57 } |
|
58 |
|
59 /* omac the [0]_n */ |
143
|
60 if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) { |
|
61 goto __ERR; |
3
|
62 } |
|
63 /* omac the nonce */ |
143
|
64 if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) { |
|
65 goto __ERR; |
3
|
66 } |
|
67 /* store result */ |
|
68 len = sizeof(eax->N); |
143
|
69 if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) { |
|
70 goto __ERR; |
3
|
71 } |
|
72 |
|
73 /* H = OMAC_1K(header) */ |
143
|
74 zeromem(buf, MAXBLOCKSIZE); |
3
|
75 buf[blklen - 1] = 1; |
|
76 |
|
77 if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) { |
143
|
78 goto __ERR; |
3
|
79 } |
|
80 |
|
81 /* omac the [1]_n */ |
|
82 if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) { |
143
|
83 goto __ERR; |
3
|
84 } |
|
85 /* omac the header */ |
|
86 if (headerlen != 0) { |
|
87 if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) { |
143
|
88 goto __ERR; |
3
|
89 } |
|
90 } |
|
91 |
|
92 /* note we don't finish the headeromac, this allows us to add more header later */ |
|
93 |
|
94 /* setup the CTR mode */ |
|
95 if ((err = ctr_start(cipher, eax->N, key, keylen, 0, &eax->ctr)) != CRYPT_OK) { |
143
|
96 goto __ERR; |
3
|
97 } |
|
98 /* use big-endian counter */ |
|
99 eax->ctr.mode = 1; |
|
100 |
|
101 /* setup the OMAC for the ciphertext */ |
|
102 if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) { |
143
|
103 goto __ERR; |
3
|
104 } |
|
105 |
|
106 /* omac [2]_n */ |
143
|
107 zeromem(buf, MAXBLOCKSIZE); |
3
|
108 buf[blklen-1] = 2; |
|
109 if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) { |
143
|
110 goto __ERR; |
3
|
111 } |
|
112 |
143
|
113 err = CRYPT_OK; |
|
114 __ERR: |
3
|
115 #ifdef CLEAN_STACK |
143
|
116 zeromem(buf, MAXBLOCKSIZE); |
|
117 zeromem(omac, sizeof(omac_state)); |
3
|
118 #endif |
143
|
119 |
|
120 XFREE(omac); |
|
121 XFREE(buf); |
|
122 |
|
123 return err; |
3
|
124 } |
|
125 |
|
126 #endif |