Mercurial > dropbear
comparison libtomcrypt/src/encauth/eax/eax_init.c @ 1471:6dba84798cd5
Update to libtomcrypt 1.18.1, merged with Dropbear changes
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 09 Feb 2018 21:44:05 +0800 |
parents | f849a5ca2efc |
children |
comparison
equal
deleted
inserted
replaced
1470:8bba51a55704 | 1471:6dba84798cd5 |
---|---|
3 * LibTomCrypt is a library that provides various cryptographic | 3 * LibTomCrypt is a library that provides various cryptographic |
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | |
9 * Tom St Denis, [email protected], http://libtom.org | |
10 */ | 8 */ |
11 | 9 |
12 /** | 10 /** |
13 @file eax_init.c | 11 @file eax_init.c |
14 EAX implementation, initialized EAX state, by Tom St Denis | 12 EAX implementation, initialized EAX state, by Tom St Denis |
15 */ | 13 */ |
16 #include "tomcrypt.h" | 14 #include "tomcrypt.h" |
17 | 15 |
18 #ifdef LTC_EAX_MODE | 16 #ifdef LTC_EAX_MODE |
19 | 17 |
20 /** | 18 /** |
21 Initialized an EAX state | 19 Initialized an EAX state |
22 @param eax [out] The EAX state to initialize | 20 @param eax [out] The EAX state to initialize |
23 @param cipher The index of the desired cipher | 21 @param cipher The index of the desired cipher |
24 @param key The secret key | 22 @param key The secret key |
25 @param keylen The length of the secret key (octets) | 23 @param keylen The length of the secret key (octets) |
27 @param noncelen The length of the nonce (octets) | 25 @param noncelen The length of the nonce (octets) |
28 @param header The header for the EAX state | 26 @param header The header for the EAX state |
29 @param headerlen The header length (octets) | 27 @param headerlen The header length (octets) |
30 @return CRYPT_OK if successful | 28 @return CRYPT_OK if successful |
31 */ | 29 */ |
32 int eax_init(eax_state *eax, int cipher, | 30 int eax_init(eax_state *eax, int cipher, |
33 const unsigned char *key, unsigned long keylen, | 31 const unsigned char *key, unsigned long keylen, |
34 const unsigned char *nonce, unsigned long noncelen, | 32 const unsigned char *nonce, unsigned long noncelen, |
35 const unsigned char *header, unsigned long headerlen) | 33 const unsigned char *header, unsigned long headerlen) |
36 { | 34 { |
37 unsigned char *buf; | 35 unsigned char *buf; |
67 } | 65 } |
68 | 66 |
69 /* N = LTC_OMAC_0K(nonce) */ | 67 /* N = LTC_OMAC_0K(nonce) */ |
70 zeromem(buf, MAXBLOCKSIZE); | 68 zeromem(buf, MAXBLOCKSIZE); |
71 if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) { | 69 if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) { |
72 goto LBL_ERR; | 70 goto LBL_ERR; |
73 } | 71 } |
74 | 72 |
75 /* omac the [0]_n */ | 73 /* omac the [0]_n */ |
76 if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) { | 74 if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) { |
77 goto LBL_ERR; | 75 goto LBL_ERR; |
78 } | 76 } |
79 /* omac the nonce */ | 77 /* omac the nonce */ |
80 if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) { | 78 if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) { |
81 goto LBL_ERR; | 79 goto LBL_ERR; |
82 } | 80 } |
83 /* store result */ | 81 /* store result */ |
84 len = sizeof(eax->N); | 82 len = sizeof(eax->N); |
85 if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) { | 83 if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) { |
86 goto LBL_ERR; | 84 goto LBL_ERR; |
87 } | 85 } |
88 | 86 |
89 /* H = LTC_OMAC_1K(header) */ | 87 /* H = LTC_OMAC_1K(header) */ |
90 zeromem(buf, MAXBLOCKSIZE); | 88 zeromem(buf, MAXBLOCKSIZE); |
91 buf[blklen - 1] = 1; | 89 buf[blklen - 1] = 1; |
92 | 90 |
93 if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) { | 91 if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) { |
94 goto LBL_ERR; | 92 goto LBL_ERR; |
95 } | 93 } |
96 | 94 |
97 /* omac the [1]_n */ | 95 /* omac the [1]_n */ |
98 if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) { | 96 if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) { |
99 goto LBL_ERR; | 97 goto LBL_ERR; |
100 } | 98 } |
101 /* omac the header */ | 99 /* omac the header */ |
102 if (headerlen != 0) { | 100 if (headerlen != 0) { |
103 if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) { | 101 if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) { |
104 goto LBL_ERR; | 102 goto LBL_ERR; |
105 } | 103 } |
106 } | 104 } |
107 | 105 |
108 /* note we don't finish the headeromac, this allows us to add more header later */ | 106 /* note we don't finish the headeromac, this allows us to add more header later */ |
109 | 107 |
110 /* setup the CTR mode */ | 108 /* setup the CTR mode */ |
111 if ((err = ctr_start(cipher, eax->N, key, keylen, 0, CTR_COUNTER_BIG_ENDIAN, &eax->ctr)) != CRYPT_OK) { | 109 if ((err = ctr_start(cipher, eax->N, key, keylen, 0, CTR_COUNTER_BIG_ENDIAN, &eax->ctr)) != CRYPT_OK) { |
112 goto LBL_ERR; | 110 goto LBL_ERR; |
113 } | 111 } |
114 | 112 |
115 /* setup the LTC_OMAC for the ciphertext */ | 113 /* setup the LTC_OMAC for the ciphertext */ |
116 if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) { | 114 if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) { |
117 goto LBL_ERR; | 115 goto LBL_ERR; |
118 } | 116 } |
119 | 117 |
120 /* omac [2]_n */ | 118 /* omac [2]_n */ |
121 zeromem(buf, MAXBLOCKSIZE); | 119 zeromem(buf, MAXBLOCKSIZE); |
122 buf[blklen-1] = 2; | 120 buf[blklen-1] = 2; |
123 if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) { | 121 if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) { |
124 goto LBL_ERR; | 122 goto LBL_ERR; |
125 } | 123 } |
126 | 124 |
127 err = CRYPT_OK; | 125 err = CRYPT_OK; |
128 LBL_ERR: | 126 LBL_ERR: |
129 #ifdef LTC_CLEAN_STACK | 127 #ifdef LTC_CLEAN_STACK |
135 XFREE(buf); | 133 XFREE(buf); |
136 | 134 |
137 return err; | 135 return err; |
138 } | 136 } |
139 | 137 |
140 #endif | 138 #endif |
141 | 139 |
142 /* $Source$ */ | 140 /* ref: $Format:%D$ */ |
143 /* $Revision$ */ | 141 /* git commit: $Format:%H$ */ |
144 /* $Date$ */ | 142 /* commit time: $Format:%ai$ */ |