Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(973)

Side by Side Diff: srtp/crypto/cipher/aes_cbc.c

Issue 889083003: Update libsrtp to upstream 1.5.0 (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/libsrtp@master
Patch Set: Updated to libsrtp 1.5.1 Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « srtp/crypto/cipher/aes.c ('k') | srtp/crypto/cipher/aes_gcm_ossl.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
69 /* allocate memory a cipher of type aes_cbc */ 72 /* allocate memory a cipher of type aes_cbc */
70 tmp = (sizeof(aes_cbc_ctx_t) + sizeof(cipher_t)); 73 tmp = (sizeof(aes_cbc_ctx_t) + sizeof(cipher_t));
71 pointer = (uint8_t*)crypto_alloc(tmp); 74 pointer = (uint8_t*)crypto_alloc(tmp);
72 if (pointer == NULL) 75 if (pointer == NULL)
73 return err_status_alloc_fail; 76 return err_status_alloc_fail;
74 77
75 /* set pointers */ 78 /* set pointers */
76 *c = (cipher_t *)pointer; 79 *c = (cipher_t *)pointer;
80 (*c)->algorithm = AES_CBC;
77 (*c)->type = &aes_cbc; 81 (*c)->type = &aes_cbc;
78 (*c)->state = pointer + sizeof(cipher_t); 82 (*c)->state = pointer + sizeof(cipher_t);
79 83
80 /* increment ref_count */ 84 /* increment ref_count */
81 aes_cbc.ref_count++; 85 aes_cbc.ref_count++;
82 86
83 /* set key size */ 87 /* set key size */
84 (*c)->key_len = key_len; 88 (*c)->key_len = key_len;
85 89
86 return err_status_ok; 90 return err_status_ok;
(...skipping 10 matching lines...) Expand all
97 /* free memory */ 101 /* free memory */
98 crypto_free(c); 102 crypto_free(c);
99 103
100 /* decrement ref_count */ 104 /* decrement ref_count */
101 aes_cbc.ref_count--; 105 aes_cbc.ref_count--;
102 106
103 return err_status_ok; 107 return err_status_ok;
104 } 108 }
105 109
106 err_status_t 110 err_status_t
107 aes_cbc_context_init(aes_cbc_ctx_t *c, const uint8_t *key, int key_len, 111 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 112
111 debug_print(mod_aes_cbc, 113 debug_print(mod_aes_cbc,
112 "key: %s", octet_string_hex_string(key, key_len)); 114 "key: %s", octet_string_hex_string(key, key_len));
113 115
114 /* expand key for the appropriate direction */ 116 /*
115 switch (dir) { 117 * Save the key until we have the IV later. We don't
116 case (direction_encrypt): 118 * know the direction until the IV is set.
117 status = aes_expand_encryption_key(key, key_len, &c->expanded_key); 119 */
118 if (status) 120 c->key_len = (key_len <= 32 ? key_len : 32);
119 return status; 121 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 122
131 return err_status_ok; 123 return err_status_ok;
132 } 124 }
133 125
134 126
135 err_status_t 127 err_status_t
136 aes_cbc_set_iv(aes_cbc_ctx_t *c, void *iv) { 128 aes_cbc_set_iv(aes_cbc_ctx_t *c, void *iv, int direction) {
129 err_status_t status;
137 int i; 130 int i;
138 /* v128_t *input = iv; */ 131 /* v128_t *input = iv; */
139 uint8_t *input = (uint8_t*) iv; 132 uint8_t *input = (uint8_t*) iv;
140 133
141 /* set state and 'previous' block to iv */ 134 /* set state and 'previous' block to iv */
142 for (i=0; i < 16; i++) 135 for (i=0; i < 16; i++)
143 c->previous.v8[i] = c->state.v8[i] = input[i]; 136 c->previous.v8[i] = c->state.v8[i] = input[i];
144 137
145 debug_print(mod_aes_cbc, "setting iv: %s", v128_hex_string(&c->state)); 138 debug_print(mod_aes_cbc, "setting iv: %s", v128_hex_string(&c->state));
146 139
140 /* expand key for the appropriate direction */
141 switch (direction) {
142 case (direction_encrypt):
143 status = aes_expand_encryption_key(c->key, c->key_len, &c->expanded_key);
144 memset(c->key, 0, 32);
145 if (status)
146 return status;
147 break;
148 case (direction_decrypt):
149 status = aes_expand_decryption_key(c->key, c->key_len, &c->expanded_key);
150 memset(c->key, 0, 32);
151 if (status)
152 return status;
153 break;
154 default:
155 return err_status_bad_param;
156 }
157
147 return err_status_ok; 158 return err_status_ok;
148 } 159 }
149 160
150 err_status_t 161 err_status_t
151 aes_cbc_encrypt(aes_cbc_ctx_t *c, 162 aes_cbc_encrypt(aes_cbc_ctx_t *c,
152 unsigned char *data, 163 unsigned char *data,
153 unsigned int *bytes_in_data) { 164 unsigned int *bytes_in_data) {
154 int i; 165 int i;
155 unsigned char *input = data; /* pointer to data being read */ 166 unsigned char *input = data; /* pointer to data being read */
156 unsigned char *output = data; /* pointer to data being written */ 167 unsigned char *output = data; /* pointer to data being written */
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 379
369 380
370 cipher_test_case_t aes_cbc_test_case_0 = { 381 cipher_test_case_t aes_cbc_test_case_0 = {
371 16, /* octets in key */ 382 16, /* octets in key */
372 aes_cbc_test_case_0_key, /* key */ 383 aes_cbc_test_case_0_key, /* key */
373 aes_cbc_test_case_0_iv, /* initialization vector */ 384 aes_cbc_test_case_0_iv, /* initialization vector */
374 16, /* octets in plaintext */ 385 16, /* octets in plaintext */
375 aes_cbc_test_case_0_plaintext, /* plaintext */ 386 aes_cbc_test_case_0_plaintext, /* plaintext */
376 32, /* octets in ciphertext */ 387 32, /* octets in ciphertext */
377 aes_cbc_test_case_0_ciphertext, /* ciphertext */ 388 aes_cbc_test_case_0_ciphertext, /* ciphertext */
389 0,
390 NULL,
391 0,
378 NULL /* pointer to next testcase */ 392 NULL /* pointer to next testcase */
379 }; 393 };
380 394
381 395
382 /* 396 /*
383 * this test case is taken directly from Appendix F.2 of NIST Special 397 * this test case is taken directly from Appendix F.2 of NIST Special
384 * Publication SP 800-38A 398 * Publication SP 800-38A
385 */ 399 */
386 400
387 uint8_t aes_cbc_test_case_1_key[16] = { 401 uint8_t aes_cbc_test_case_1_key[16] = {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 }; 433 };
420 434
421 cipher_test_case_t aes_cbc_test_case_1 = { 435 cipher_test_case_t aes_cbc_test_case_1 = {
422 16, /* octets in key */ 436 16, /* octets in key */
423 aes_cbc_test_case_1_key, /* key */ 437 aes_cbc_test_case_1_key, /* key */
424 aes_cbc_test_case_1_iv, /* initialization vector */ 438 aes_cbc_test_case_1_iv, /* initialization vector */
425 64, /* octets in plaintext */ 439 64, /* octets in plaintext */
426 aes_cbc_test_case_1_plaintext, /* plaintext */ 440 aes_cbc_test_case_1_plaintext, /* plaintext */
427 80, /* octets in ciphertext */ 441 80, /* octets in ciphertext */
428 aes_cbc_test_case_1_ciphertext, /* ciphertext */ 442 aes_cbc_test_case_1_ciphertext, /* ciphertext */
443 0,
444 NULL,
445 0,
429 &aes_cbc_test_case_0 /* pointer to next testcase */ 446 &aes_cbc_test_case_0 /* pointer to next testcase */
430 }; 447 };
431 448
432 /* 449 /*
433 * Test case 2 is like test case 0, but for 256-bit keys. (FIPS 197 450 * Test case 2 is like test case 0, but for 256-bit keys. (FIPS 197
434 * appendix C.3). 451 * appendix C.3).
435 */ 452 */
436 453
437 454
438 uint8_t aes_cbc_test_case_2_key[32] = { 455 uint8_t aes_cbc_test_case_2_key[32] = {
(...skipping 21 matching lines...) Expand all
460 }; 477 };
461 478
462 cipher_test_case_t aes_cbc_test_case_2 = { 479 cipher_test_case_t aes_cbc_test_case_2 = {
463 32, /* octets in key */ 480 32, /* octets in key */
464 aes_cbc_test_case_2_key, /* key */ 481 aes_cbc_test_case_2_key, /* key */
465 aes_cbc_test_case_2_iv, /* initialization vector */ 482 aes_cbc_test_case_2_iv, /* initialization vector */
466 16, /* octets in plaintext */ 483 16, /* octets in plaintext */
467 aes_cbc_test_case_2_plaintext, /* plaintext */ 484 aes_cbc_test_case_2_plaintext, /* plaintext */
468 32, /* octets in ciphertext */ 485 32, /* octets in ciphertext */
469 aes_cbc_test_case_2_ciphertext, /* ciphertext */ 486 aes_cbc_test_case_2_ciphertext, /* ciphertext */
487 0,
488 NULL,
489 0,
470 &aes_cbc_test_case_1 /* pointer to next testcase */ 490 &aes_cbc_test_case_1 /* pointer to next testcase */
471 }; 491 };
472 492
473 493
474 /* 494 /*
475 * this test case is taken directly from Appendix F.2 of NIST Special 495 * this test case is taken directly from Appendix F.2 of NIST Special
476 * Publication SP 800-38A 496 * Publication SP 800-38A
477 */ 497 */
478 498
479 uint8_t aes_cbc_test_case_3_key[32] = { 499 uint8_t aes_cbc_test_case_3_key[32] = {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 }; 533 };
514 534
515 cipher_test_case_t aes_cbc_test_case_3 = { 535 cipher_test_case_t aes_cbc_test_case_3 = {
516 32, /* octets in key */ 536 32, /* octets in key */
517 aes_cbc_test_case_3_key, /* key */ 537 aes_cbc_test_case_3_key, /* key */
518 aes_cbc_test_case_3_iv, /* initialization vector */ 538 aes_cbc_test_case_3_iv, /* initialization vector */
519 64, /* octets in plaintext */ 539 64, /* octets in plaintext */
520 aes_cbc_test_case_3_plaintext, /* plaintext */ 540 aes_cbc_test_case_3_plaintext, /* plaintext */
521 80, /* octets in ciphertext */ 541 80, /* octets in ciphertext */
522 aes_cbc_test_case_3_ciphertext, /* ciphertext */ 542 aes_cbc_test_case_3_ciphertext, /* ciphertext */
543 0,
544 NULL,
545 0,
523 &aes_cbc_test_case_2 /* pointer to next testcase */ 546 &aes_cbc_test_case_2 /* pointer to next testcase */
524 }; 547 };
525 548
526 cipher_type_t aes_cbc = { 549 cipher_type_t aes_cbc = {
527 (cipher_alloc_func_t) aes_cbc_alloc, 550 (cipher_alloc_func_t) aes_cbc_alloc,
528 (cipher_dealloc_func_t) aes_cbc_dealloc, 551 (cipher_dealloc_func_t) aes_cbc_dealloc,
529 (cipher_init_func_t) aes_cbc_context_init, 552 (cipher_init_func_t) aes_cbc_context_init,
553 (cipher_set_aad_func_t) 0,
530 (cipher_encrypt_func_t) aes_cbc_nist_encrypt, 554 (cipher_encrypt_func_t) aes_cbc_nist_encrypt,
531 (cipher_decrypt_func_t) aes_cbc_nist_decrypt, 555 (cipher_decrypt_func_t) aes_cbc_nist_decrypt,
532 (cipher_set_iv_func_t) aes_cbc_set_iv, 556 (cipher_set_iv_func_t) aes_cbc_set_iv,
557 (cipher_get_tag_func_t) 0,
533 (char *) aes_cbc_description, 558 (char *) aes_cbc_description,
534 (int) 0, /* instance count */ 559 (int) 0, /* instance count */
535 (cipher_test_case_t *) &aes_cbc_test_case_3, 560 (cipher_test_case_t *) &aes_cbc_test_case_3,
536 (debug_module_t *) &mod_aes_cbc, 561 (debug_module_t *) &mod_aes_cbc,
537 (cipher_type_id_t) AES_CBC 562 (cipher_type_id_t) AES_CBC
538 }; 563 };
539 564
540 565
OLDNEW
« no previous file with comments | « srtp/crypto/cipher/aes.c ('k') | srtp/crypto/cipher/aes_gcm_ossl.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698