Mercurial > dropbear
comparison libtomcrypt/src/hashes/chc/chc.c @ 1511:5916af64acd4 fuzz
merge from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 17 Feb 2018 19:29:51 +0800 |
parents | 6dba84798cd5 |
children |
comparison
equal
deleted
inserted
replaced
1457:32f990cc96b1 | 1511:5916af64acd4 |
---|---|
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 #include "tomcrypt.h" | 10 #include "tomcrypt.h" |
13 | 11 |
14 /** | 12 /** |
33 &chc_test, | 31 &chc_test, |
34 NULL | 32 NULL |
35 }; | 33 }; |
36 | 34 |
37 /** | 35 /** |
38 Initialize the CHC state with a given cipher | 36 Initialize the CHC state with a given cipher |
39 @param cipher The index of the cipher you wish to bind | 37 @param cipher The index of the cipher you wish to bind |
40 @return CRYPT_OK if successful | 38 @return CRYPT_OK if successful |
41 */ | 39 */ |
42 int chc_register(int cipher) | 40 int chc_register(int cipher) |
43 { | 41 { |
44 int err, kl, idx; | 42 int err, kl, idx; |
68 if ((err = hash_is_valid(idx = find_hash("chc_hash"))) != CRYPT_OK) { | 66 if ((err = hash_is_valid(idx = find_hash("chc_hash"))) != CRYPT_OK) { |
69 return err; | 67 return err; |
70 } | 68 } |
71 | 69 |
72 /* store into descriptor */ | 70 /* store into descriptor */ |
73 hash_descriptor[idx].hashsize = | 71 hash_descriptor[idx].hashsize = |
74 hash_descriptor[idx].blocksize = cipher_descriptor[cipher].block_length; | 72 hash_descriptor[idx].blocksize = cipher_descriptor[cipher].block_length; |
75 | 73 |
76 /* store the idx and block size */ | 74 /* store the idx and block size */ |
77 cipher_idx = cipher; | 75 cipher_idx = cipher; |
78 cipher_blocksize = cipher_descriptor[cipher].block_length; | 76 cipher_blocksize = cipher_descriptor[cipher].block_length; |
87 int chc_init(hash_state *md) | 85 int chc_init(hash_state *md) |
88 { | 86 { |
89 symmetric_key *key; | 87 symmetric_key *key; |
90 unsigned char buf[MAXBLOCKSIZE]; | 88 unsigned char buf[MAXBLOCKSIZE]; |
91 int err; | 89 int err; |
92 | 90 |
93 LTC_ARGCHK(md != NULL); | 91 LTC_ARGCHK(md != NULL); |
94 | 92 |
95 /* is the cipher valid? */ | 93 /* is the cipher valid? */ |
96 if ((err = cipher_is_valid(cipher_idx)) != CRYPT_OK) { | 94 if ((err = cipher_is_valid(cipher_idx)) != CRYPT_OK) { |
97 return err; | 95 return err; |
103 | 101 |
104 if ((key = XMALLOC(sizeof(*key))) == NULL) { | 102 if ((key = XMALLOC(sizeof(*key))) == NULL) { |
105 return CRYPT_MEM; | 103 return CRYPT_MEM; |
106 } | 104 } |
107 | 105 |
108 /* zero key and what not */ | 106 /* zero key and what not */ |
109 zeromem(buf, cipher_blocksize); | 107 zeromem(buf, cipher_blocksize); |
110 if ((err = cipher_descriptor[cipher_idx].setup(buf, cipher_blocksize, 0, key)) != CRYPT_OK) { | 108 if ((err = cipher_descriptor[cipher_idx].setup(buf, cipher_blocksize, 0, key)) != CRYPT_OK) { |
111 XFREE(key); | 109 XFREE(key); |
112 return err; | 110 return err; |
113 } | 111 } |
121 zeromem(md->chc.buf, sizeof(md->chc.buf)); | 119 zeromem(md->chc.buf, sizeof(md->chc.buf)); |
122 XFREE(key); | 120 XFREE(key); |
123 return CRYPT_OK; | 121 return CRYPT_OK; |
124 } | 122 } |
125 | 123 |
126 /* | 124 /* |
127 key <= state | 125 key <= state |
128 T0,T1 <= block | 126 T0,T1 <= block |
129 T0 <= encrypt T0 | 127 T0 <= encrypt T0 |
130 state <= state xor T0 xor T1 | 128 state <= state xor T0 xor T1 |
131 */ | 129 */ |
145 XMEMCPY(T[1], buf, cipher_blocksize); | 143 XMEMCPY(T[1], buf, cipher_blocksize); |
146 cipher_descriptor[cipher_idx].ecb_encrypt(buf, T[0], key); | 144 cipher_descriptor[cipher_idx].ecb_encrypt(buf, T[0], key); |
147 for (x = 0; x < cipher_blocksize; x++) { | 145 for (x = 0; x < cipher_blocksize; x++) { |
148 md->chc.state[x] ^= T[0][x] ^ T[1][x]; | 146 md->chc.state[x] ^= T[0][x] ^ T[1][x]; |
149 } | 147 } |
150 XFREE(key); | |
151 #ifdef LTC_CLEAN_STACK | 148 #ifdef LTC_CLEAN_STACK |
152 zeromem(T, sizeof(T)); | 149 zeromem(T, sizeof(T)); |
153 zeromem(&key, sizeof(key)); | 150 zeromem(key, sizeof(*key)); |
154 #endif | 151 #endif |
152 XFREE(key); | |
155 return CRYPT_OK; | 153 return CRYPT_OK; |
156 } | 154 } |
157 | 155 |
158 /* function for processing blocks */ | 156 /** |
159 int _chc_process(hash_state * md, const unsigned char *buf, unsigned long len); | 157 Function for processing blocks |
160 HASH_PROCESS(_chc_process, chc_compress, chc, (unsigned long)cipher_blocksize) | 158 @param md The hash state |
159 @param buf The data to hash | |
160 @param len The length of the data (octets) | |
161 @return CRYPT_OK if successful | |
162 */ | |
163 static int _chc_process(hash_state * md, const unsigned char *buf, unsigned long len); | |
164 static HASH_PROCESS(_chc_process, chc_compress, chc, (unsigned long)cipher_blocksize) | |
161 | 165 |
162 /** | 166 /** |
163 Process a block of memory though the hash | 167 Process a block of memory though the hash |
164 @param md The hash state | 168 @param md The hash state |
165 @param in The data to hash | 169 @param in The data to hash |
246 } | 250 } |
247 | 251 |
248 /** | 252 /** |
249 Self-test the hash | 253 Self-test the hash |
250 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled | 254 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled |
251 */ | 255 */ |
252 int chc_test(void) | 256 int chc_test(void) |
253 { | 257 { |
258 #ifndef LTC_TEST | |
259 return CRYPT_NOP; | |
260 #else | |
254 static const struct { | 261 static const struct { |
255 unsigned char *msg, | 262 unsigned char *msg, |
256 md[MAXBLOCKSIZE]; | 263 hash[MAXBLOCKSIZE]; |
257 int len; | 264 int len; |
258 } tests[] = { | 265 } tests[] = { |
259 { | 266 { |
260 (unsigned char *)"hello world", | 267 (unsigned char *)"hello world", |
261 { 0xcf, 0x57, 0x9d, 0xc3, 0x0a, 0x0e, 0xea, 0x61, | 268 { 0xcf, 0x57, 0x9d, 0xc3, 0x0a, 0x0e, 0xea, 0x61, |
262 0x0d, 0x54, 0x47, 0xc4, 0x3c, 0x06, 0xf5, 0x4e }, | 269 0x0d, 0x54, 0x47, 0xc4, 0x3c, 0x06, 0xf5, 0x4e }, |
263 16 | 270 16 |
264 } | 271 } |
265 }; | 272 }; |
266 int x, oldhashidx, idx; | 273 int i, oldhashidx, idx; |
267 unsigned char out[MAXBLOCKSIZE]; | 274 unsigned char tmp[MAXBLOCKSIZE]; |
268 hash_state md; | 275 hash_state md; |
269 | 276 |
270 /* AES can be under rijndael or aes... try to find it */ | 277 /* AES can be under rijndael or aes... try to find it */ |
271 if ((idx = find_cipher("aes")) == -1) { | 278 if ((idx = find_cipher("aes")) == -1) { |
272 if ((idx = find_cipher("rijndael")) == -1) { | 279 if ((idx = find_cipher("rijndael")) == -1) { |
274 } | 281 } |
275 } | 282 } |
276 oldhashidx = cipher_idx; | 283 oldhashidx = cipher_idx; |
277 chc_register(idx); | 284 chc_register(idx); |
278 | 285 |
279 for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { | 286 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { |
280 chc_init(&md); | 287 chc_init(&md); |
281 chc_process(&md, tests[x].msg, strlen((char *)tests[x].msg)); | 288 chc_process(&md, tests[i].msg, strlen((char *)tests[i].msg)); |
282 chc_done(&md, out); | 289 chc_done(&md, tmp); |
283 if (XMEMCMP(out, tests[x].md, tests[x].len)) { | 290 if (compare_testvector(tmp, tests[i].len, tests[i].hash, tests[i].len, "CHC", i)) { |
284 return CRYPT_FAIL_TESTVECTOR; | 291 return CRYPT_FAIL_TESTVECTOR; |
285 } | 292 } |
286 } | 293 } |
287 if (oldhashidx != UNDEFED_HASH) { | 294 if (oldhashidx != UNDEFED_HASH) { |
288 chc_register(oldhashidx); | 295 chc_register(oldhashidx); |
289 } | 296 } |
290 | 297 |
291 return CRYPT_OK; | 298 return CRYPT_OK; |
292 } | |
293 | |
294 #endif | 299 #endif |
295 | 300 } |
296 /* $Source$ */ | 301 |
297 /* $Revision$ */ | 302 #endif |
298 /* $Date$ */ | 303 |
304 /* ref: $Format:%D$ */ | |
305 /* git commit: $Format:%H$ */ | |
306 /* commit time: $Format:%ai$ */ |