OLD | NEW |
1 /* | 1 /* |
2 * aes_icm.c | 2 * aes_icm.c |
3 * | 3 * |
4 * AES Integer Counter Mode | 4 * AES Integer Counter Mode |
5 * | 5 * |
6 * David A. McGrew | 6 * David A. McGrew |
7 * Cisco Systems, Inc. | 7 * Cisco Systems, Inc. |
8 */ | 8 */ |
9 | 9 |
10 /* | 10 /* |
11 * | 11 * |
12 * Copyright (c) 2001-2006, Cisco Systems, Inc. | 12 * Copyright (c) 2001-2006,2013 Cisco Systems, Inc. |
13 * All rights reserved. | 13 * All rights reserved. |
14 * | 14 * |
15 * Redistribution and use in source and binary forms, with or without | 15 * Redistribution and use in source and binary forms, with or without |
16 * modification, are permitted provided that the following conditions | 16 * modification, are permitted provided that the following conditions |
17 * are met: | 17 * are met: |
18 * | 18 * |
19 * Redistributions of source code must retain the above copyright | 19 * Redistributions of source code must retain the above copyright |
20 * notice, this list of conditions and the following disclaimer. | 20 * notice, this list of conditions and the following disclaimer. |
21 * | 21 * |
22 * Redistributions in binary form must reproduce the above | 22 * Redistributions in binary form must reproduce the above |
(...skipping 13 matching lines...) Expand all Loading... |
36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | 41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
42 * OF THE POSSIBILITY OF SUCH DAMAGE. | 42 * OF THE POSSIBILITY OF SUCH DAMAGE. |
43 * | 43 * |
44 */ | 44 */ |
45 | 45 |
| 46 #ifdef HAVE_CONFIG_H |
| 47 #include <config.h> |
| 48 #endif |
46 | 49 |
47 #define ALIGN_32 0 | 50 #define ALIGN_32 0 |
48 | 51 |
49 #include "aes_icm.h" | 52 #include "aes_icm.h" |
50 #include "alloc.h" | 53 #include "alloc.h" |
51 | 54 |
52 | 55 |
53 debug_module_t mod_aes_icm = { | 56 debug_module_t mod_aes_icm = { |
54 0, /* debugging is off by default */ | 57 0, /* debugging is off by default */ |
55 "aes icm" /* printable module name */ | 58 "aes icm" /* printable module name */ |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 return err_status_bad_param; | 114 return err_status_bad_param; |
112 | 115 |
113 /* allocate memory a cipher of type aes_icm */ | 116 /* allocate memory a cipher of type aes_icm */ |
114 tmp = (sizeof(aes_icm_ctx_t) + sizeof(cipher_t)); | 117 tmp = (sizeof(aes_icm_ctx_t) + sizeof(cipher_t)); |
115 pointer = (uint8_t*)crypto_alloc(tmp); | 118 pointer = (uint8_t*)crypto_alloc(tmp); |
116 if (pointer == NULL) | 119 if (pointer == NULL) |
117 return err_status_alloc_fail; | 120 return err_status_alloc_fail; |
118 | 121 |
119 /* set pointers */ | 122 /* set pointers */ |
120 *c = (cipher_t *)pointer; | 123 *c = (cipher_t *)pointer; |
| 124 switch (key_len) { |
| 125 case 46: |
| 126 (*c)->algorithm = AES_256_ICM; |
| 127 break; |
| 128 case 38: |
| 129 (*c)->algorithm = AES_192_ICM; |
| 130 break; |
| 131 default: |
| 132 (*c)->algorithm = AES_128_ICM; |
| 133 break; |
| 134 } |
121 (*c)->type = &aes_icm; | 135 (*c)->type = &aes_icm; |
122 (*c)->state = pointer + sizeof(cipher_t); | 136 (*c)->state = pointer + sizeof(cipher_t); |
123 | 137 |
124 /* increment ref_count */ | 138 /* increment ref_count */ |
125 aes_icm.ref_count++; | 139 aes_icm.ref_count++; |
126 | 140 |
127 /* set key size */ | 141 /* set key size */ |
128 (*c)->key_len = key_len; | 142 (*c)->key_len = key_len; |
129 | 143 |
130 return err_status_ok; | 144 return err_status_ok; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 err_status_t status; | 181 err_status_t status; |
168 int base_key_len, copy_len; | 182 int base_key_len, copy_len; |
169 | 183 |
170 if (key_len > 16 && key_len < 30) /* Ismacryp */ | 184 if (key_len > 16 && key_len < 30) /* Ismacryp */ |
171 base_key_len = 16; | 185 base_key_len = 16; |
172 else if (key_len == 30 || key_len == 38 || key_len == 46) | 186 else if (key_len == 30 || key_len == 38 || key_len == 46) |
173 base_key_len = key_len - 14; | 187 base_key_len = key_len - 14; |
174 else | 188 else |
175 return err_status_bad_param; | 189 return err_status_bad_param; |
176 | 190 |
177 /* | 191 /* |
178 * set counter and initial values to 'offset' value, being careful not to | 192 * set counter and initial values to 'offset' value, being careful not to |
179 * go past the end of the key buffer. | 193 * go past the end of the key buffer |
180 */ | 194 */ |
181 v128_set_to_zero(&c->counter); | 195 v128_set_to_zero(&c->counter); |
182 v128_set_to_zero(&c->offset); | 196 v128_set_to_zero(&c->offset); |
183 | 197 |
184 /* force last two octets of the offset to be left zero | |
185 * (for srtp compatibility) */ | |
186 copy_len = key_len - base_key_len; | 198 copy_len = key_len - base_key_len; |
187 | 199 /* force last two octets of the offset to be left zero (for srtp compatibility
) */ |
| 200 if (copy_len > 14) |
| 201 copy_len = 14; |
| 202 |
188 memcpy(&c->counter, key + base_key_len, copy_len); | 203 memcpy(&c->counter, key + base_key_len, copy_len); |
189 memcpy(&c->offset, key + base_key_len, copy_len); | 204 memcpy(&c->offset, key + base_key_len, copy_len); |
190 | 205 |
191 debug_print(mod_aes_icm, | 206 debug_print(mod_aes_icm, |
192 "key: %s", octet_string_hex_string(key, base_key_len)); | 207 "key: %s", octet_string_hex_string(key, base_key_len)); |
193 debug_print(mod_aes_icm, | 208 debug_print(mod_aes_icm, |
194 "offset: %s", v128_hex_string(&c->offset)); | 209 "offset: %s", v128_hex_string(&c->offset)); |
195 | 210 |
196 /* expand key */ | 211 /* expand key */ |
197 status = aes_expand_encryption_key(key, base_key_len, &c->expanded_key); | 212 status = aes_expand_encryption_key(key, base_key_len, &c->expanded_key); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 | 279 |
265 return err_status_ok; | 280 return err_status_ok; |
266 } | 281 } |
267 | 282 |
268 /* | 283 /* |
269 * aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with | 284 * aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with |
270 * the offset | 285 * the offset |
271 */ | 286 */ |
272 | 287 |
273 err_status_t | 288 err_status_t |
274 aes_icm_set_iv(aes_icm_ctx_t *c, void *iv) { | 289 aes_icm_set_iv(aes_icm_ctx_t *c, void *iv, int direction) { |
275 v128_t *nonce = (v128_t *) iv; | 290 v128_t nonce; |
| 291 |
| 292 /* set nonce (for alignment) */ |
| 293 v128_copy_octet_string(&nonce, iv); |
276 | 294 |
277 debug_print(mod_aes_icm, | 295 debug_print(mod_aes_icm, |
278 » "setting iv: %s", v128_hex_string(nonce)); | 296 » "setting iv: %s", v128_hex_string(&nonce)); |
279 | 297 |
280 v128_xor(&c->counter, &c->offset, nonce); | 298 v128_xor(&c->counter, &c->offset, &nonce); |
281 | 299 |
282 debug_print(mod_aes_icm, | 300 debug_print(mod_aes_icm, |
283 "set_counter: %s", v128_hex_string(&c->counter)); | 301 "set_counter: %s", v128_hex_string(&c->counter)); |
284 | 302 |
285 /* indicate that the keystream_buffer is empty */ | 303 /* indicate that the keystream_buffer is empty */ |
286 c->bytes_in_buffer = 0; | 304 c->bytes_in_buffer = 0; |
287 | 305 |
288 return err_status_ok; | 306 return err_status_ok; |
289 } | 307 } |
290 | 308 |
291 | 309 |
292 | 310 |
293 /* | 311 /* |
294 * aes_icm_advance(...) refills the keystream_buffer and | 312 * aes_icm_advance(...) refills the keystream_buffer and |
295 * advances the block index of the sicm_context forward by one | 313 * advances the block index of the sicm_context forward by one |
296 * | 314 * |
297 * this is an internal, hopefully inlined function | 315 * this is an internal, hopefully inlined function |
298 */ | 316 */ |
299 | 317 |
300 static INLINE void | 318 static inline void |
301 aes_icm_advance_ismacryp(aes_icm_ctx_t *c, uint8_t forIsmacryp) { | 319 aes_icm_advance_ismacryp(aes_icm_ctx_t *c, uint8_t forIsmacryp) { |
302 /* fill buffer with new keystream */ | 320 /* fill buffer with new keystream */ |
303 v128_copy(&c->keystream_buffer, &c->counter); | 321 v128_copy(&c->keystream_buffer, &c->counter); |
304 aes_encrypt(&c->keystream_buffer, &c->expanded_key); | 322 aes_encrypt(&c->keystream_buffer, &c->expanded_key); |
305 c->bytes_in_buffer = sizeof(v128_t); | 323 c->bytes_in_buffer = sizeof(v128_t); |
306 | 324 |
307 debug_print(mod_aes_icm, "counter: %s", | 325 debug_print(mod_aes_icm, "counter: %s", |
308 v128_hex_string(&c->counter)); | 326 v128_hex_string(&c->counter)); |
309 debug_print(mod_aes_icm, "ciphertext: %s", | 327 debug_print(mod_aes_icm, "ciphertext: %s", |
310 v128_hex_string(&c->keystream_buffer)); | 328 v128_hex_string(&c->keystream_buffer)); |
311 | 329 |
312 /* clock counter forward */ | 330 /* clock counter forward */ |
313 | 331 |
314 if (forIsmacryp) { | 332 if (forIsmacryp) { |
315 uint32_t temp; | 333 uint32_t temp; |
316 //alex's clock counter forward | 334 //alex's clock counter forward |
317 temp = ntohl(c->counter.v32[3]); | 335 temp = ntohl(c->counter.v32[3]); |
318 c->counter.v32[3] = htonl(++temp); | 336 ++temp; |
| 337 c->counter.v32[3] = htonl(temp); |
319 } else { | 338 } else { |
320 if (!++(c->counter.v8[15])) | 339 if (!++(c->counter.v8[15])) |
321 ++(c->counter.v8[14]); | 340 ++(c->counter.v8[14]); |
322 } | 341 } |
323 } | 342 } |
324 | 343 |
325 static INLINE void aes_icm_advance(aes_icm_ctx_t *c) { | |
326 aes_icm_advance_ismacryp(c, 0); | |
327 } | |
328 | |
329 | |
330 /*e | 344 /*e |
331 * icm_encrypt deals with the following cases: | 345 * icm_encrypt deals with the following cases: |
332 * | 346 * |
333 * bytes_to_encr < bytes_in_buffer | 347 * bytes_to_encr < bytes_in_buffer |
334 * - add keystream into data | 348 * - add keystream into data |
335 * | 349 * |
336 * bytes_to_encr > bytes_in_buffer | 350 * bytes_to_encr > bytes_in_buffer |
337 * - add keystream into data until keystream_buffer is depleted | 351 * - add keystream into data until keystream_buffer is depleted |
338 * - loop over blocks, filling keystream_buffer and then | 352 * - loop over blocks, filling keystream_buffer and then |
339 * adding keystream into data | 353 * adding keystream into data |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 | 461 |
448 return err_status_ok; | 462 return err_status_ok; |
449 } | 463 } |
450 | 464 |
451 err_status_t | 465 err_status_t |
452 aes_icm_encrypt(aes_icm_ctx_t *c, unsigned char *buf, unsigned int *enc_len) { | 466 aes_icm_encrypt(aes_icm_ctx_t *c, unsigned char *buf, unsigned int *enc_len) { |
453 return aes_icm_encrypt_ismacryp(c, buf, enc_len, 0); | 467 return aes_icm_encrypt_ismacryp(c, buf, enc_len, 0); |
454 } | 468 } |
455 | 469 |
456 err_status_t | 470 err_status_t |
457 aes_icm_output(aes_icm_ctx_t *c, uint8_t *buffer, int num_octets_to_output) { | 471 aes_icm_output(aes_icm_ctx_t *c, uint8_t *buffer, unsigned int num_octets_to_out
put) { |
458 unsigned int len = num_octets_to_output; | 472 unsigned int len = num_octets_to_output; |
459 | 473 |
460 /* zeroize the buffer */ | 474 /* zeroize the buffer */ |
461 octet_string_set_to_zero(buffer, num_octets_to_output); | 475 octet_string_set_to_zero(buffer, num_octets_to_output); |
462 | 476 |
463 /* exor keystream into buffer */ | 477 /* exor keystream into buffer */ |
464 return aes_icm_encrypt(c, buffer, &len); | 478 return aes_icm_encrypt(c, buffer, &len); |
465 } | 479 } |
466 | 480 |
| 481 uint16_t |
| 482 aes_icm_bytes_encrypted(aes_icm_ctx_t *c) { |
| 483 return htons(c->counter.v16[7]); |
| 484 } |
467 | 485 |
468 char | 486 char |
469 aes_icm_description[] = "aes integer counter mode"; | 487 aes_icm_description[] = "aes integer counter mode"; |
470 | 488 |
471 uint8_t aes_icm_test_case_0_key[30] = { | 489 uint8_t aes_icm_test_case_0_key[30] = { |
472 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, | 490 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, |
473 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c, | 491 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c, |
474 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, | 492 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
475 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd | 493 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd |
476 }; | 494 }; |
(...skipping 18 matching lines...) Expand all Loading... |
495 }; | 513 }; |
496 | 514 |
497 cipher_test_case_t aes_icm_test_case_0 = { | 515 cipher_test_case_t aes_icm_test_case_0 = { |
498 30, /* octets in key */ | 516 30, /* octets in key */ |
499 aes_icm_test_case_0_key, /* key */ | 517 aes_icm_test_case_0_key, /* key */ |
500 aes_icm_test_case_0_nonce, /* packet index */ | 518 aes_icm_test_case_0_nonce, /* packet index */ |
501 32, /* octets in plaintext */ | 519 32, /* octets in plaintext */ |
502 aes_icm_test_case_0_plaintext, /* plaintext */ | 520 aes_icm_test_case_0_plaintext, /* plaintext */ |
503 32, /* octets in ciphertext */ | 521 32, /* octets in ciphertext */ |
504 aes_icm_test_case_0_ciphertext, /* ciphertext */ | 522 aes_icm_test_case_0_ciphertext, /* ciphertext */ |
| 523 0, |
| 524 NULL, |
| 525 0, |
505 NULL /* pointer to next testcase */ | 526 NULL /* pointer to next testcase */ |
506 }; | 527 }; |
507 | 528 |
508 uint8_t aes_icm_test_case_1_key[46] = { | 529 uint8_t aes_icm_test_case_1_key[46] = { |
509 0x57, 0xf8, 0x2f, 0xe3, 0x61, 0x3f, 0xd1, 0x70, | 530 0x57, 0xf8, 0x2f, 0xe3, 0x61, 0x3f, 0xd1, 0x70, |
510 0xa8, 0x5e, 0xc9, 0x3c, 0x40, 0xb1, 0xf0, 0x92, | 531 0xa8, 0x5e, 0xc9, 0x3c, 0x40, 0xb1, 0xf0, 0x92, |
511 0x2e, 0xc4, 0xcb, 0x0d, 0xc0, 0x25, 0xb5, 0x82, | 532 0x2e, 0xc4, 0xcb, 0x0d, 0xc0, 0x25, 0xb5, 0x82, |
512 0x72, 0x14, 0x7c, 0xc4, 0x38, 0x94, 0x4a, 0x98, | 533 0x72, 0x14, 0x7c, 0xc4, 0x38, 0x94, 0x4a, 0x98, |
513 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, | 534 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
514 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd | 535 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd |
(...skipping 19 matching lines...) Expand all Loading... |
534 }; | 555 }; |
535 | 556 |
536 cipher_test_case_t aes_icm_test_case_1 = { | 557 cipher_test_case_t aes_icm_test_case_1 = { |
537 46, /* octets in key */ | 558 46, /* octets in key */ |
538 aes_icm_test_case_1_key, /* key */ | 559 aes_icm_test_case_1_key, /* key */ |
539 aes_icm_test_case_1_nonce, /* packet index */ | 560 aes_icm_test_case_1_nonce, /* packet index */ |
540 32, /* octets in plaintext */ | 561 32, /* octets in plaintext */ |
541 aes_icm_test_case_1_plaintext, /* plaintext */ | 562 aes_icm_test_case_1_plaintext, /* plaintext */ |
542 32, /* octets in ciphertext */ | 563 32, /* octets in ciphertext */ |
543 aes_icm_test_case_1_ciphertext, /* ciphertext */ | 564 aes_icm_test_case_1_ciphertext, /* ciphertext */ |
| 565 0, |
| 566 NULL, |
| 567 0, |
544 &aes_icm_test_case_0 /* pointer to next testcase */ | 568 &aes_icm_test_case_0 /* pointer to next testcase */ |
545 }; | 569 }; |
546 | 570 |
547 | 571 |
548 | 572 |
549 /* | 573 /* |
550 * note: the encrypt function is identical to the decrypt function | 574 * note: the encrypt function is identical to the decrypt function |
551 */ | 575 */ |
552 | 576 |
553 cipher_type_t aes_icm = { | 577 cipher_type_t aes_icm = { |
554 (cipher_alloc_func_t) aes_icm_alloc, | 578 (cipher_alloc_func_t) aes_icm_alloc, |
555 (cipher_dealloc_func_t) aes_icm_dealloc, | 579 (cipher_dealloc_func_t) aes_icm_dealloc, |
556 (cipher_init_func_t) aes_icm_context_init, | 580 (cipher_init_func_t) aes_icm_context_init, |
| 581 (cipher_set_aad_func_t) 0, |
557 (cipher_encrypt_func_t) aes_icm_encrypt, | 582 (cipher_encrypt_func_t) aes_icm_encrypt, |
558 (cipher_decrypt_func_t) aes_icm_encrypt, | 583 (cipher_decrypt_func_t) aes_icm_encrypt, |
559 (cipher_set_iv_func_t) aes_icm_set_iv, | 584 (cipher_set_iv_func_t) aes_icm_set_iv, |
| 585 (cipher_get_tag_func_t) 0, |
560 (char *) aes_icm_description, | 586 (char *) aes_icm_description, |
561 (int) 0, /* instance count */ | 587 (int) 0, /* instance count */ |
562 (cipher_test_case_t *) &aes_icm_test_case_1, | 588 (cipher_test_case_t *) &aes_icm_test_case_1, |
563 (debug_module_t *) &mod_aes_icm, | 589 (debug_module_t *) &mod_aes_icm, |
564 (cipher_type_id_t) AES_ICM | 590 (cipher_type_id_t) AES_ICM |
565 }; | 591 }; |
566 | 592 |
OLD | NEW |