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 = (v128_t *) iv; |
276 | 291 |
277 debug_print(mod_aes_icm, | 292 debug_print(mod_aes_icm, |
278 "setting iv: %s", v128_hex_string(nonce)); | 293 "setting iv: %s", v128_hex_string(nonce)); |
279 | 294 |
280 v128_xor(&c->counter, &c->offset, nonce); | 295 v128_xor(&c->counter, &c->offset, nonce); |
281 | 296 |
282 debug_print(mod_aes_icm, | 297 debug_print(mod_aes_icm, |
283 "set_counter: %s", v128_hex_string(&c->counter)); | 298 "set_counter: %s", v128_hex_string(&c->counter)); |
284 | 299 |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 aes_icm_output(aes_icm_ctx_t *c, uint8_t *buffer, int num_octets_to_output) { | 472 aes_icm_output(aes_icm_ctx_t *c, uint8_t *buffer, int num_octets_to_output) { |
458 unsigned int len = num_octets_to_output; | 473 unsigned int len = num_octets_to_output; |
459 | 474 |
460 /* zeroize the buffer */ | 475 /* zeroize the buffer */ |
461 octet_string_set_to_zero(buffer, num_octets_to_output); | 476 octet_string_set_to_zero(buffer, num_octets_to_output); |
462 | 477 |
463 /* exor keystream into buffer */ | 478 /* exor keystream into buffer */ |
464 return aes_icm_encrypt(c, buffer, &len); | 479 return aes_icm_encrypt(c, buffer, &len); |
465 } | 480 } |
466 | 481 |
| 482 uint16_t |
| 483 aes_icm_bytes_encrypted(aes_icm_ctx_t *c) { |
| 484 return htons(c->counter.v16[7]); |
| 485 } |
467 | 486 |
468 char | 487 char |
469 aes_icm_description[] = "aes integer counter mode"; | 488 aes_icm_description[] = "aes integer counter mode"; |
470 | 489 |
471 uint8_t aes_icm_test_case_0_key[30] = { | 490 uint8_t aes_icm_test_case_0_key[30] = { |
472 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, | 491 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, |
473 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c, | 492 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c, |
474 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, | 493 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
475 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd | 494 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd |
476 }; | 495 }; |
(...skipping 18 matching lines...) Expand all Loading... |
495 }; | 514 }; |
496 | 515 |
497 cipher_test_case_t aes_icm_test_case_0 = { | 516 cipher_test_case_t aes_icm_test_case_0 = { |
498 30, /* octets in key */ | 517 30, /* octets in key */ |
499 aes_icm_test_case_0_key, /* key */ | 518 aes_icm_test_case_0_key, /* key */ |
500 aes_icm_test_case_0_nonce, /* packet index */ | 519 aes_icm_test_case_0_nonce, /* packet index */ |
501 32, /* octets in plaintext */ | 520 32, /* octets in plaintext */ |
502 aes_icm_test_case_0_plaintext, /* plaintext */ | 521 aes_icm_test_case_0_plaintext, /* plaintext */ |
503 32, /* octets in ciphertext */ | 522 32, /* octets in ciphertext */ |
504 aes_icm_test_case_0_ciphertext, /* ciphertext */ | 523 aes_icm_test_case_0_ciphertext, /* ciphertext */ |
| 524 0, |
| 525 NULL, |
| 526 0, |
505 NULL /* pointer to next testcase */ | 527 NULL /* pointer to next testcase */ |
506 }; | 528 }; |
507 | 529 |
508 uint8_t aes_icm_test_case_1_key[46] = { | 530 uint8_t aes_icm_test_case_1_key[46] = { |
509 0x57, 0xf8, 0x2f, 0xe3, 0x61, 0x3f, 0xd1, 0x70, | 531 0x57, 0xf8, 0x2f, 0xe3, 0x61, 0x3f, 0xd1, 0x70, |
510 0xa8, 0x5e, 0xc9, 0x3c, 0x40, 0xb1, 0xf0, 0x92, | 532 0xa8, 0x5e, 0xc9, 0x3c, 0x40, 0xb1, 0xf0, 0x92, |
511 0x2e, 0xc4, 0xcb, 0x0d, 0xc0, 0x25, 0xb5, 0x82, | 533 0x2e, 0xc4, 0xcb, 0x0d, 0xc0, 0x25, 0xb5, 0x82, |
512 0x72, 0x14, 0x7c, 0xc4, 0x38, 0x94, 0x4a, 0x98, | 534 0x72, 0x14, 0x7c, 0xc4, 0x38, 0x94, 0x4a, 0x98, |
513 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, | 535 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
514 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd | 536 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd |
(...skipping 19 matching lines...) Expand all Loading... |
534 }; | 556 }; |
535 | 557 |
536 cipher_test_case_t aes_icm_test_case_1 = { | 558 cipher_test_case_t aes_icm_test_case_1 = { |
537 46, /* octets in key */ | 559 46, /* octets in key */ |
538 aes_icm_test_case_1_key, /* key */ | 560 aes_icm_test_case_1_key, /* key */ |
539 aes_icm_test_case_1_nonce, /* packet index */ | 561 aes_icm_test_case_1_nonce, /* packet index */ |
540 32, /* octets in plaintext */ | 562 32, /* octets in plaintext */ |
541 aes_icm_test_case_1_plaintext, /* plaintext */ | 563 aes_icm_test_case_1_plaintext, /* plaintext */ |
542 32, /* octets in ciphertext */ | 564 32, /* octets in ciphertext */ |
543 aes_icm_test_case_1_ciphertext, /* ciphertext */ | 565 aes_icm_test_case_1_ciphertext, /* ciphertext */ |
| 566 0, |
| 567 NULL, |
| 568 0, |
544 &aes_icm_test_case_0 /* pointer to next testcase */ | 569 &aes_icm_test_case_0 /* pointer to next testcase */ |
545 }; | 570 }; |
546 | 571 |
547 | 572 |
548 | 573 |
549 /* | 574 /* |
550 * note: the encrypt function is identical to the decrypt function | 575 * note: the encrypt function is identical to the decrypt function |
551 */ | 576 */ |
552 | 577 |
553 cipher_type_t aes_icm = { | 578 cipher_type_t aes_icm = { |
554 (cipher_alloc_func_t) aes_icm_alloc, | 579 (cipher_alloc_func_t) aes_icm_alloc, |
555 (cipher_dealloc_func_t) aes_icm_dealloc, | 580 (cipher_dealloc_func_t) aes_icm_dealloc, |
556 (cipher_init_func_t) aes_icm_context_init, | 581 (cipher_init_func_t) aes_icm_context_init, |
| 582 (cipher_set_aad_func_t) 0, |
557 (cipher_encrypt_func_t) aes_icm_encrypt, | 583 (cipher_encrypt_func_t) aes_icm_encrypt, |
558 (cipher_decrypt_func_t) aes_icm_encrypt, | 584 (cipher_decrypt_func_t) aes_icm_encrypt, |
559 (cipher_set_iv_func_t) aes_icm_set_iv, | 585 (cipher_set_iv_func_t) aes_icm_set_iv, |
| 586 (cipher_get_tag_func_t) 0, |
560 (char *) aes_icm_description, | 587 (char *) aes_icm_description, |
561 (int) 0, /* instance count */ | 588 (int) 0, /* instance count */ |
562 (cipher_test_case_t *) &aes_icm_test_case_1, | 589 (cipher_test_case_t *) &aes_icm_test_case_1, |
563 (debug_module_t *) &mod_aes_icm, | 590 (debug_module_t *) &mod_aes_icm, |
564 (cipher_type_id_t) AES_ICM | 591 (cipher_type_id_t) AES_ICM |
565 }; | 592 }; |
566 | 593 |
OLD | NEW |