comparison eax_init.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 7faae8f46238
children
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
16 16
17 int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen, 17 int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen,
18 const unsigned char *nonce, unsigned long noncelen, 18 const unsigned char *nonce, unsigned long noncelen,
19 const unsigned char *header, unsigned long headerlen) 19 const unsigned char *header, unsigned long headerlen)
20 { 20 {
21 unsigned char buf[MAXBLOCKSIZE]; 21 unsigned char *buf;
22 int err, blklen; 22 int err, blklen;
23 omac_state omac; 23 omac_state *omac;
24 unsigned long len; 24 unsigned long len;
25 25
26 26
27 _ARGCHK(eax != NULL); 27 _ARGCHK(eax != NULL);
28 _ARGCHK(key != NULL); 28 _ARGCHK(key != NULL);
34 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { 34 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
35 return err; 35 return err;
36 } 36 }
37 blklen = cipher_descriptor[cipher].block_length; 37 blklen = cipher_descriptor[cipher].block_length;
38 38
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
39 /* N = OMAC_0K(nonce) */ 53 /* N = OMAC_0K(nonce) */
40 zeromem(buf, sizeof(buf)); 54 zeromem(buf, MAXBLOCKSIZE);
41 if ((err = omac_init(&omac, cipher, key, keylen)) != CRYPT_OK) { 55 if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
42 return err; 56 goto __ERR;
43 } 57 }
44 58
45 /* omac the [0]_n */ 59 /* omac the [0]_n */
46 if ((err = omac_process(&omac, buf, blklen)) != CRYPT_OK) { 60 if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) {
47 return err; 61 goto __ERR;
48 } 62 }
49 /* omac the nonce */ 63 /* omac the nonce */
50 if ((err = omac_process(&omac, nonce, noncelen)) != CRYPT_OK) { 64 if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) {
51 return err; 65 goto __ERR;
52 } 66 }
53 /* store result */ 67 /* store result */
54 len = sizeof(eax->N); 68 len = sizeof(eax->N);
55 if ((err = omac_done(&omac, eax->N, &len)) != CRYPT_OK) { 69 if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) {
56 return err; 70 goto __ERR;
57 } 71 }
58 72
59 /* H = OMAC_1K(header) */ 73 /* H = OMAC_1K(header) */
60 zeromem(buf, sizeof(buf)); 74 zeromem(buf, MAXBLOCKSIZE);
61 buf[blklen - 1] = 1; 75 buf[blklen - 1] = 1;
62 76
63 if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) { 77 if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) {
64 return err; 78 goto __ERR;
65 } 79 }
66 80
67 /* omac the [1]_n */ 81 /* omac the [1]_n */
68 if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) { 82 if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) {
69 return err; 83 goto __ERR;
70 } 84 }
71 /* omac the header */ 85 /* omac the header */
72 if (headerlen != 0) { 86 if (headerlen != 0) {
73 if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) { 87 if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) {
74 return err; 88 goto __ERR;
75 } 89 }
76 } 90 }
77 91
78 /* note we don't finish the headeromac, this allows us to add more header later */ 92 /* note we don't finish the headeromac, this allows us to add more header later */
79 93
80 /* setup the CTR mode */ 94 /* setup the CTR mode */
81 if ((err = ctr_start(cipher, eax->N, key, keylen, 0, &eax->ctr)) != CRYPT_OK) { 95 if ((err = ctr_start(cipher, eax->N, key, keylen, 0, &eax->ctr)) != CRYPT_OK) {
82 return err; 96 goto __ERR;
83 } 97 }
84 /* use big-endian counter */ 98 /* use big-endian counter */
85 eax->ctr.mode = 1; 99 eax->ctr.mode = 1;
86 100
87 /* setup the OMAC for the ciphertext */ 101 /* setup the OMAC for the ciphertext */
88 if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) { 102 if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) {
89 return err; 103 goto __ERR;
90 } 104 }
91 105
92 /* omac [2]_n */ 106 /* omac [2]_n */
93 zeromem(buf, sizeof(buf)); 107 zeromem(buf, MAXBLOCKSIZE);
94 buf[blklen-1] = 2; 108 buf[blklen-1] = 2;
95 if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) { 109 if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) {
96 return err; 110 goto __ERR;
97 } 111 }
98 112
113 err = CRYPT_OK;
114 __ERR:
99 #ifdef CLEAN_STACK 115 #ifdef CLEAN_STACK
100 zeromem(buf, sizeof(buf)); 116 zeromem(buf, MAXBLOCKSIZE);
101 zeromem(&omac, sizeof(omac)); 117 zeromem(omac, sizeof(omac_state));
102 #endif 118 #endif
103 return CRYPT_OK; 119
120 XFREE(omac);
121 XFREE(buf);
122
123 return err;
104 } 124 }
105 125
106 #endif 126 #endif