OLD | NEW |
1 /* | 1 /* |
2 * aes_cbc.c | 2 * aes_cbc.c |
3 * | 3 * |
4 * AES Cipher Block Chaining Mode | 4 * AES Cipher Block Chaining 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 /* |
(...skipping 25 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 #include "aes_cbc.h" | 50 #include "aes_cbc.h" |
48 #include "alloc.h" | 51 #include "alloc.h" |
49 | 52 |
50 debug_module_t mod_aes_cbc = { | 53 debug_module_t mod_aes_cbc = { |
51 0, /* debugging is off by default */ | 54 0, /* debugging is off by default */ |
52 "aes cbc" /* printable module name */ | 55 "aes cbc" /* printable module name */ |
53 }; | 56 }; |
54 | 57 |
55 | 58 |
56 | 59 |
57 err_status_t | 60 err_status_t |
58 aes_cbc_alloc(cipher_t **c, int key_len) { | 61 aes_cbc_alloc(cipher_t **c, int key_len, int tlen) { |
59 extern cipher_type_t aes_cbc; | 62 extern cipher_type_t aes_cbc; |
60 uint8_t *pointer; | 63 uint8_t *pointer; |
61 int tmp; | 64 int tmp; |
62 | 65 |
63 debug_print(mod_aes_cbc, | 66 debug_print(mod_aes_cbc, |
64 "allocating cipher with key length %d", key_len); | 67 "allocating cipher with key length %d", key_len); |
65 | 68 |
66 if (key_len != 16 && key_len != 24 && key_len != 32) | 69 if (key_len != 16 && key_len != 24 && key_len != 32) |
67 return err_status_bad_param; | 70 return err_status_bad_param; |
68 | 71 |
(...skipping 28 matching lines...) Expand all Loading... |
97 /* free memory */ | 100 /* free memory */ |
98 crypto_free(c); | 101 crypto_free(c); |
99 | 102 |
100 /* decrement ref_count */ | 103 /* decrement ref_count */ |
101 aes_cbc.ref_count--; | 104 aes_cbc.ref_count--; |
102 | 105 |
103 return err_status_ok; | 106 return err_status_ok; |
104 } | 107 } |
105 | 108 |
106 err_status_t | 109 err_status_t |
107 aes_cbc_context_init(aes_cbc_ctx_t *c, const uint8_t *key, int key_len, | 110 aes_cbc_context_init(aes_cbc_ctx_t *c, const uint8_t *key, int key_len) { |
108 » » cipher_direction_t dir) { | |
109 err_status_t status; | |
110 | 111 |
111 debug_print(mod_aes_cbc, | 112 debug_print(mod_aes_cbc, |
112 "key: %s", octet_string_hex_string(key, key_len)); | 113 "key: %s", octet_string_hex_string(key, key_len)); |
113 | 114 |
114 /* expand key for the appropriate direction */ | 115 /* |
115 switch (dir) { | 116 * Save the key until we have the IV later. We don't |
116 case (direction_encrypt): | 117 * know the direction until the IV is set. |
117 status = aes_expand_encryption_key(key, key_len, &c->expanded_key); | 118 */ |
118 if (status) | 119 c->key_len = (key_len <= 32 ? key_len : 32); |
119 return status; | 120 memcpy(c->key, key, c->key_len); |
120 break; | |
121 case (direction_decrypt): | |
122 status = aes_expand_decryption_key(key, key_len, &c->expanded_key); | |
123 if (status) | |
124 return status; | |
125 break; | |
126 default: | |
127 return err_status_bad_param; | |
128 } | |
129 | |
130 | 121 |
131 return err_status_ok; | 122 return err_status_ok; |
132 } | 123 } |
133 | 124 |
134 | 125 |
135 err_status_t | 126 err_status_t |
136 aes_cbc_set_iv(aes_cbc_ctx_t *c, void *iv) { | 127 aes_cbc_set_iv(aes_cbc_ctx_t *c, void *iv, int direction) { |
| 128 err_status_t status; |
137 int i; | 129 int i; |
138 /* v128_t *input = iv; */ | 130 /* v128_t *input = iv; */ |
139 uint8_t *input = (uint8_t*) iv; | 131 uint8_t *input = (uint8_t*) iv; |
140 | 132 |
141 /* set state and 'previous' block to iv */ | 133 /* set state and 'previous' block to iv */ |
142 for (i=0; i < 16; i++) | 134 for (i=0; i < 16; i++) |
143 c->previous.v8[i] = c->state.v8[i] = input[i]; | 135 c->previous.v8[i] = c->state.v8[i] = input[i]; |
144 | 136 |
145 debug_print(mod_aes_cbc, "setting iv: %s", v128_hex_string(&c->state)); | 137 debug_print(mod_aes_cbc, "setting iv: %s", v128_hex_string(&c->state)); |
146 | 138 |
| 139 /* expand key for the appropriate direction */ |
| 140 switch (direction) { |
| 141 case (direction_encrypt): |
| 142 status = aes_expand_encryption_key(c->key, c->key_len, &c->expanded_key); |
| 143 memset(c->key, 0, 32); |
| 144 if (status) |
| 145 return status; |
| 146 break; |
| 147 case (direction_decrypt): |
| 148 status = aes_expand_decryption_key(c->key, c->key_len, &c->expanded_key); |
| 149 memset(c->key, 0, 32); |
| 150 if (status) |
| 151 return status; |
| 152 break; |
| 153 default: |
| 154 return err_status_bad_param; |
| 155 } |
| 156 |
147 return err_status_ok; | 157 return err_status_ok; |
148 } | 158 } |
149 | 159 |
150 err_status_t | 160 err_status_t |
151 aes_cbc_encrypt(aes_cbc_ctx_t *c, | 161 aes_cbc_encrypt(aes_cbc_ctx_t *c, |
152 unsigned char *data, | 162 unsigned char *data, |
153 unsigned int *bytes_in_data) { | 163 unsigned int *bytes_in_data) { |
154 int i; | 164 int i; |
155 unsigned char *input = data; /* pointer to data being read */ | 165 unsigned char *input = data; /* pointer to data being read */ |
156 unsigned char *output = data; /* pointer to data being written */ | 166 unsigned char *output = data; /* pointer to data being written */ |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 | 378 |
369 | 379 |
370 cipher_test_case_t aes_cbc_test_case_0 = { | 380 cipher_test_case_t aes_cbc_test_case_0 = { |
371 16, /* octets in key */ | 381 16, /* octets in key */ |
372 aes_cbc_test_case_0_key, /* key */ | 382 aes_cbc_test_case_0_key, /* key */ |
373 aes_cbc_test_case_0_iv, /* initialization vector */ | 383 aes_cbc_test_case_0_iv, /* initialization vector */ |
374 16, /* octets in plaintext */ | 384 16, /* octets in plaintext */ |
375 aes_cbc_test_case_0_plaintext, /* plaintext */ | 385 aes_cbc_test_case_0_plaintext, /* plaintext */ |
376 32, /* octets in ciphertext */ | 386 32, /* octets in ciphertext */ |
377 aes_cbc_test_case_0_ciphertext, /* ciphertext */ | 387 aes_cbc_test_case_0_ciphertext, /* ciphertext */ |
| 388 0, |
| 389 NULL, |
| 390 0, |
378 NULL /* pointer to next testcase */ | 391 NULL /* pointer to next testcase */ |
379 }; | 392 }; |
380 | 393 |
381 | 394 |
382 /* | 395 /* |
383 * this test case is taken directly from Appendix F.2 of NIST Special | 396 * this test case is taken directly from Appendix F.2 of NIST Special |
384 * Publication SP 800-38A | 397 * Publication SP 800-38A |
385 */ | 398 */ |
386 | 399 |
387 uint8_t aes_cbc_test_case_1_key[16] = { | 400 uint8_t aes_cbc_test_case_1_key[16] = { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 }; | 432 }; |
420 | 433 |
421 cipher_test_case_t aes_cbc_test_case_1 = { | 434 cipher_test_case_t aes_cbc_test_case_1 = { |
422 16, /* octets in key */ | 435 16, /* octets in key */ |
423 aes_cbc_test_case_1_key, /* key */ | 436 aes_cbc_test_case_1_key, /* key */ |
424 aes_cbc_test_case_1_iv, /* initialization vector */ | 437 aes_cbc_test_case_1_iv, /* initialization vector */ |
425 64, /* octets in plaintext */ | 438 64, /* octets in plaintext */ |
426 aes_cbc_test_case_1_plaintext, /* plaintext */ | 439 aes_cbc_test_case_1_plaintext, /* plaintext */ |
427 80, /* octets in ciphertext */ | 440 80, /* octets in ciphertext */ |
428 aes_cbc_test_case_1_ciphertext, /* ciphertext */ | 441 aes_cbc_test_case_1_ciphertext, /* ciphertext */ |
| 442 0, |
| 443 NULL, |
| 444 0, |
429 &aes_cbc_test_case_0 /* pointer to next testcase */ | 445 &aes_cbc_test_case_0 /* pointer to next testcase */ |
430 }; | 446 }; |
431 | 447 |
432 /* | 448 /* |
433 * Test case 2 is like test case 0, but for 256-bit keys. (FIPS 197 | 449 * Test case 2 is like test case 0, but for 256-bit keys. (FIPS 197 |
434 * appendix C.3). | 450 * appendix C.3). |
435 */ | 451 */ |
436 | 452 |
437 | 453 |
438 uint8_t aes_cbc_test_case_2_key[32] = { | 454 uint8_t aes_cbc_test_case_2_key[32] = { |
(...skipping 21 matching lines...) Expand all Loading... |
460 }; | 476 }; |
461 | 477 |
462 cipher_test_case_t aes_cbc_test_case_2 = { | 478 cipher_test_case_t aes_cbc_test_case_2 = { |
463 32, /* octets in key */ | 479 32, /* octets in key */ |
464 aes_cbc_test_case_2_key, /* key */ | 480 aes_cbc_test_case_2_key, /* key */ |
465 aes_cbc_test_case_2_iv, /* initialization vector */ | 481 aes_cbc_test_case_2_iv, /* initialization vector */ |
466 16, /* octets in plaintext */ | 482 16, /* octets in plaintext */ |
467 aes_cbc_test_case_2_plaintext, /* plaintext */ | 483 aes_cbc_test_case_2_plaintext, /* plaintext */ |
468 32, /* octets in ciphertext */ | 484 32, /* octets in ciphertext */ |
469 aes_cbc_test_case_2_ciphertext, /* ciphertext */ | 485 aes_cbc_test_case_2_ciphertext, /* ciphertext */ |
| 486 0, |
| 487 NULL, |
| 488 0, |
470 &aes_cbc_test_case_1 /* pointer to next testcase */ | 489 &aes_cbc_test_case_1 /* pointer to next testcase */ |
471 }; | 490 }; |
472 | 491 |
473 | 492 |
474 /* | 493 /* |
475 * this test case is taken directly from Appendix F.2 of NIST Special | 494 * this test case is taken directly from Appendix F.2 of NIST Special |
476 * Publication SP 800-38A | 495 * Publication SP 800-38A |
477 */ | 496 */ |
478 | 497 |
479 uint8_t aes_cbc_test_case_3_key[32] = { | 498 uint8_t aes_cbc_test_case_3_key[32] = { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 }; | 532 }; |
514 | 533 |
515 cipher_test_case_t aes_cbc_test_case_3 = { | 534 cipher_test_case_t aes_cbc_test_case_3 = { |
516 32, /* octets in key */ | 535 32, /* octets in key */ |
517 aes_cbc_test_case_3_key, /* key */ | 536 aes_cbc_test_case_3_key, /* key */ |
518 aes_cbc_test_case_3_iv, /* initialization vector */ | 537 aes_cbc_test_case_3_iv, /* initialization vector */ |
519 64, /* octets in plaintext */ | 538 64, /* octets in plaintext */ |
520 aes_cbc_test_case_3_plaintext, /* plaintext */ | 539 aes_cbc_test_case_3_plaintext, /* plaintext */ |
521 80, /* octets in ciphertext */ | 540 80, /* octets in ciphertext */ |
522 aes_cbc_test_case_3_ciphertext, /* ciphertext */ | 541 aes_cbc_test_case_3_ciphertext, /* ciphertext */ |
| 542 0, |
| 543 NULL, |
| 544 0, |
523 &aes_cbc_test_case_2 /* pointer to next testcase */ | 545 &aes_cbc_test_case_2 /* pointer to next testcase */ |
524 }; | 546 }; |
525 | 547 |
526 cipher_type_t aes_cbc = { | 548 cipher_type_t aes_cbc = { |
527 (cipher_alloc_func_t) aes_cbc_alloc, | 549 (cipher_alloc_func_t) aes_cbc_alloc, |
528 (cipher_dealloc_func_t) aes_cbc_dealloc, | 550 (cipher_dealloc_func_t) aes_cbc_dealloc, |
529 (cipher_init_func_t) aes_cbc_context_init, | 551 (cipher_init_func_t) aes_cbc_context_init, |
| 552 (cipher_set_aad_func_t) 0, |
530 (cipher_encrypt_func_t) aes_cbc_nist_encrypt, | 553 (cipher_encrypt_func_t) aes_cbc_nist_encrypt, |
531 (cipher_decrypt_func_t) aes_cbc_nist_decrypt, | 554 (cipher_decrypt_func_t) aes_cbc_nist_decrypt, |
532 (cipher_set_iv_func_t) aes_cbc_set_iv, | 555 (cipher_set_iv_func_t) aes_cbc_set_iv, |
| 556 (cipher_get_tag_func_t) 0, |
533 (char *) aes_cbc_description, | 557 (char *) aes_cbc_description, |
534 (int) 0, /* instance count */ | 558 (int) 0, /* instance count */ |
535 (cipher_test_case_t *) &aes_cbc_test_case_3, | 559 (cipher_test_case_t *) &aes_cbc_test_case_3, |
536 (debug_module_t *) &mod_aes_cbc, | 560 (debug_module_t *) &mod_aes_cbc, |
537 (cipher_type_id_t) AES_CBC | 561 (cipher_type_id_t) AES_CBC |
538 }; | 562 }; |
539 | 563 |
540 | 564 |
OLD | NEW |