Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * aes_cbc.c |
michael@0 | 3 | * |
michael@0 | 4 | * AES Cipher Block Chaining Mode |
michael@0 | 5 | * |
michael@0 | 6 | * David A. McGrew |
michael@0 | 7 | * Cisco Systems, Inc. |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | /* |
michael@0 | 11 | * |
michael@0 | 12 | * Copyright (c) 2001-2006, Cisco Systems, Inc. |
michael@0 | 13 | * All rights reserved. |
michael@0 | 14 | * |
michael@0 | 15 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 16 | * modification, are permitted provided that the following conditions |
michael@0 | 17 | * are met: |
michael@0 | 18 | * |
michael@0 | 19 | * Redistributions of source code must retain the above copyright |
michael@0 | 20 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 21 | * |
michael@0 | 22 | * Redistributions in binary form must reproduce the above |
michael@0 | 23 | * copyright notice, this list of conditions and the following |
michael@0 | 24 | * disclaimer in the documentation and/or other materials provided |
michael@0 | 25 | * with the distribution. |
michael@0 | 26 | * |
michael@0 | 27 | * Neither the name of the Cisco Systems, Inc. nor the names of its |
michael@0 | 28 | * contributors may be used to endorse or promote products derived |
michael@0 | 29 | * from this software without specific prior written permission. |
michael@0 | 30 | * |
michael@0 | 31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 32 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 33 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
michael@0 | 34 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
michael@0 | 35 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
michael@0 | 36 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
michael@0 | 37 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
michael@0 | 38 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
michael@0 | 39 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
michael@0 | 40 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 41 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
michael@0 | 42 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 43 | * |
michael@0 | 44 | */ |
michael@0 | 45 | |
michael@0 | 46 | |
michael@0 | 47 | #include "aes_cbc.h" |
michael@0 | 48 | #include "alloc.h" |
michael@0 | 49 | |
michael@0 | 50 | debug_module_t mod_aes_cbc = { |
michael@0 | 51 | 0, /* debugging is off by default */ |
michael@0 | 52 | "aes cbc" /* printable module name */ |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | |
michael@0 | 56 | |
michael@0 | 57 | err_status_t |
michael@0 | 58 | aes_cbc_alloc(cipher_t **c, int key_len) { |
michael@0 | 59 | extern cipher_type_t aes_cbc; |
michael@0 | 60 | uint8_t *pointer; |
michael@0 | 61 | int tmp; |
michael@0 | 62 | |
michael@0 | 63 | debug_print(mod_aes_cbc, |
michael@0 | 64 | "allocating cipher with key length %d", key_len); |
michael@0 | 65 | |
michael@0 | 66 | if (key_len != 16 && key_len != 24 && key_len != 32) |
michael@0 | 67 | return err_status_bad_param; |
michael@0 | 68 | |
michael@0 | 69 | /* allocate memory a cipher of type aes_cbc */ |
michael@0 | 70 | tmp = (sizeof(aes_cbc_ctx_t) + sizeof(cipher_t)); |
michael@0 | 71 | pointer = (uint8_t*)crypto_alloc(tmp); |
michael@0 | 72 | if (pointer == NULL) |
michael@0 | 73 | return err_status_alloc_fail; |
michael@0 | 74 | |
michael@0 | 75 | /* set pointers */ |
michael@0 | 76 | *c = (cipher_t *)pointer; |
michael@0 | 77 | (*c)->type = &aes_cbc; |
michael@0 | 78 | (*c)->state = pointer + sizeof(cipher_t); |
michael@0 | 79 | |
michael@0 | 80 | /* increment ref_count */ |
michael@0 | 81 | aes_cbc.ref_count++; |
michael@0 | 82 | |
michael@0 | 83 | /* set key size */ |
michael@0 | 84 | (*c)->key_len = key_len; |
michael@0 | 85 | |
michael@0 | 86 | return err_status_ok; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | err_status_t |
michael@0 | 90 | aes_cbc_dealloc(cipher_t *c) { |
michael@0 | 91 | extern cipher_type_t aes_cbc; |
michael@0 | 92 | |
michael@0 | 93 | /* zeroize entire state*/ |
michael@0 | 94 | octet_string_set_to_zero((uint8_t *)c, |
michael@0 | 95 | sizeof(aes_cbc_ctx_t) + sizeof(cipher_t)); |
michael@0 | 96 | |
michael@0 | 97 | /* free memory */ |
michael@0 | 98 | crypto_free(c); |
michael@0 | 99 | |
michael@0 | 100 | /* decrement ref_count */ |
michael@0 | 101 | aes_cbc.ref_count--; |
michael@0 | 102 | |
michael@0 | 103 | return err_status_ok; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | err_status_t |
michael@0 | 107 | aes_cbc_context_init(aes_cbc_ctx_t *c, const uint8_t *key, int key_len, |
michael@0 | 108 | cipher_direction_t dir) { |
michael@0 | 109 | err_status_t status; |
michael@0 | 110 | |
michael@0 | 111 | debug_print(mod_aes_cbc, |
michael@0 | 112 | "key: %s", octet_string_hex_string(key, key_len)); |
michael@0 | 113 | |
michael@0 | 114 | /* expand key for the appropriate direction */ |
michael@0 | 115 | switch (dir) { |
michael@0 | 116 | case (direction_encrypt): |
michael@0 | 117 | status = aes_expand_encryption_key(key, key_len, &c->expanded_key); |
michael@0 | 118 | if (status) |
michael@0 | 119 | return status; |
michael@0 | 120 | break; |
michael@0 | 121 | case (direction_decrypt): |
michael@0 | 122 | status = aes_expand_decryption_key(key, key_len, &c->expanded_key); |
michael@0 | 123 | if (status) |
michael@0 | 124 | return status; |
michael@0 | 125 | break; |
michael@0 | 126 | default: |
michael@0 | 127 | return err_status_bad_param; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | |
michael@0 | 131 | return err_status_ok; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | |
michael@0 | 135 | err_status_t |
michael@0 | 136 | aes_cbc_set_iv(aes_cbc_ctx_t *c, void *iv) { |
michael@0 | 137 | int i; |
michael@0 | 138 | /* v128_t *input = iv; */ |
michael@0 | 139 | uint8_t *input = (uint8_t*) iv; |
michael@0 | 140 | |
michael@0 | 141 | /* set state and 'previous' block to iv */ |
michael@0 | 142 | for (i=0; i < 16; i++) |
michael@0 | 143 | c->previous.v8[i] = c->state.v8[i] = input[i]; |
michael@0 | 144 | |
michael@0 | 145 | debug_print(mod_aes_cbc, "setting iv: %s", v128_hex_string(&c->state)); |
michael@0 | 146 | |
michael@0 | 147 | return err_status_ok; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | err_status_t |
michael@0 | 151 | aes_cbc_encrypt(aes_cbc_ctx_t *c, |
michael@0 | 152 | unsigned char *data, |
michael@0 | 153 | unsigned int *bytes_in_data) { |
michael@0 | 154 | int i; |
michael@0 | 155 | unsigned char *input = data; /* pointer to data being read */ |
michael@0 | 156 | unsigned char *output = data; /* pointer to data being written */ |
michael@0 | 157 | int bytes_to_encr = *bytes_in_data; |
michael@0 | 158 | |
michael@0 | 159 | /* |
michael@0 | 160 | * verify that we're 16-octet aligned |
michael@0 | 161 | */ |
michael@0 | 162 | if (*bytes_in_data & 0xf) |
michael@0 | 163 | return err_status_bad_param; |
michael@0 | 164 | |
michael@0 | 165 | /* |
michael@0 | 166 | * note that we assume that the initialization vector has already |
michael@0 | 167 | * been set, e.g. by calling aes_cbc_set_iv() |
michael@0 | 168 | */ |
michael@0 | 169 | debug_print(mod_aes_cbc, "iv: %s", |
michael@0 | 170 | v128_hex_string(&c->state)); |
michael@0 | 171 | |
michael@0 | 172 | /* |
michael@0 | 173 | * loop over plaintext blocks, exoring state into plaintext then |
michael@0 | 174 | * encrypting and writing to output |
michael@0 | 175 | */ |
michael@0 | 176 | while (bytes_to_encr > 0) { |
michael@0 | 177 | |
michael@0 | 178 | /* exor plaintext into state */ |
michael@0 | 179 | for (i=0; i < 16; i++) |
michael@0 | 180 | c->state.v8[i] ^= *input++; |
michael@0 | 181 | |
michael@0 | 182 | debug_print(mod_aes_cbc, "inblock: %s", |
michael@0 | 183 | v128_hex_string(&c->state)); |
michael@0 | 184 | |
michael@0 | 185 | aes_encrypt(&c->state, &c->expanded_key); |
michael@0 | 186 | |
michael@0 | 187 | debug_print(mod_aes_cbc, "outblock: %s", |
michael@0 | 188 | v128_hex_string(&c->state)); |
michael@0 | 189 | |
michael@0 | 190 | /* copy ciphertext to output */ |
michael@0 | 191 | for (i=0; i < 16; i++) |
michael@0 | 192 | *output++ = c->state.v8[i]; |
michael@0 | 193 | |
michael@0 | 194 | bytes_to_encr -= 16; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | return err_status_ok; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | err_status_t |
michael@0 | 201 | aes_cbc_decrypt(aes_cbc_ctx_t *c, |
michael@0 | 202 | unsigned char *data, |
michael@0 | 203 | unsigned int *bytes_in_data) { |
michael@0 | 204 | int i; |
michael@0 | 205 | v128_t state, previous; |
michael@0 | 206 | unsigned char *input = data; /* pointer to data being read */ |
michael@0 | 207 | unsigned char *output = data; /* pointer to data being written */ |
michael@0 | 208 | int bytes_to_encr = *bytes_in_data; |
michael@0 | 209 | uint8_t tmp; |
michael@0 | 210 | |
michael@0 | 211 | /* |
michael@0 | 212 | * verify that we're 16-octet aligned |
michael@0 | 213 | */ |
michael@0 | 214 | if (*bytes_in_data & 0x0f) |
michael@0 | 215 | return err_status_bad_param; |
michael@0 | 216 | |
michael@0 | 217 | /* set 'previous' block to iv*/ |
michael@0 | 218 | for (i=0; i < 16; i++) { |
michael@0 | 219 | previous.v8[i] = c->previous.v8[i]; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | debug_print(mod_aes_cbc, "iv: %s", |
michael@0 | 223 | v128_hex_string(&previous)); |
michael@0 | 224 | |
michael@0 | 225 | /* |
michael@0 | 226 | * loop over ciphertext blocks, decrypting then exoring with state |
michael@0 | 227 | * then writing plaintext to output |
michael@0 | 228 | */ |
michael@0 | 229 | while (bytes_to_encr > 0) { |
michael@0 | 230 | |
michael@0 | 231 | /* set state to ciphertext input block */ |
michael@0 | 232 | for (i=0; i < 16; i++) { |
michael@0 | 233 | state.v8[i] = *input++; |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | debug_print(mod_aes_cbc, "inblock: %s", |
michael@0 | 237 | v128_hex_string(&state)); |
michael@0 | 238 | |
michael@0 | 239 | /* decrypt state */ |
michael@0 | 240 | aes_decrypt(&state, &c->expanded_key); |
michael@0 | 241 | |
michael@0 | 242 | debug_print(mod_aes_cbc, "outblock: %s", |
michael@0 | 243 | v128_hex_string(&state)); |
michael@0 | 244 | |
michael@0 | 245 | /* |
michael@0 | 246 | * exor previous ciphertext block out of plaintext, and write new |
michael@0 | 247 | * plaintext block to output, while copying old ciphertext block |
michael@0 | 248 | * to the 'previous' block |
michael@0 | 249 | */ |
michael@0 | 250 | for (i=0; i < 16; i++) { |
michael@0 | 251 | tmp = *output; |
michael@0 | 252 | *output++ = state.v8[i] ^ previous.v8[i]; |
michael@0 | 253 | previous.v8[i] = tmp; |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | bytes_to_encr -= 16; |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | return err_status_ok; |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | |
michael@0 | 263 | err_status_t |
michael@0 | 264 | aes_cbc_nist_encrypt(aes_cbc_ctx_t *c, |
michael@0 | 265 | unsigned char *data, |
michael@0 | 266 | unsigned int *bytes_in_data) { |
michael@0 | 267 | int i; |
michael@0 | 268 | unsigned char *pad_start; |
michael@0 | 269 | int num_pad_bytes; |
michael@0 | 270 | err_status_t status; |
michael@0 | 271 | |
michael@0 | 272 | /* |
michael@0 | 273 | * determine the number of padding bytes that we need to add - |
michael@0 | 274 | * this value is always between 1 and 16, inclusive. |
michael@0 | 275 | */ |
michael@0 | 276 | num_pad_bytes = 16 - (*bytes_in_data & 0xf); |
michael@0 | 277 | pad_start = data; |
michael@0 | 278 | pad_start += *bytes_in_data; |
michael@0 | 279 | *pad_start++ = 0xa0; |
michael@0 | 280 | for (i=0; i < num_pad_bytes; i++) |
michael@0 | 281 | *pad_start++ = 0x00; |
michael@0 | 282 | |
michael@0 | 283 | /* |
michael@0 | 284 | * increment the data size |
michael@0 | 285 | */ |
michael@0 | 286 | *bytes_in_data += num_pad_bytes; |
michael@0 | 287 | |
michael@0 | 288 | /* |
michael@0 | 289 | * now cbc encrypt the padded data |
michael@0 | 290 | */ |
michael@0 | 291 | status = aes_cbc_encrypt(c, data, bytes_in_data); |
michael@0 | 292 | if (status) |
michael@0 | 293 | return status; |
michael@0 | 294 | |
michael@0 | 295 | return err_status_ok; |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | |
michael@0 | 299 | err_status_t |
michael@0 | 300 | aes_cbc_nist_decrypt(aes_cbc_ctx_t *c, |
michael@0 | 301 | unsigned char *data, |
michael@0 | 302 | unsigned int *bytes_in_data) { |
michael@0 | 303 | unsigned char *pad_end; |
michael@0 | 304 | int num_pad_bytes; |
michael@0 | 305 | err_status_t status; |
michael@0 | 306 | |
michael@0 | 307 | /* |
michael@0 | 308 | * cbc decrypt the padded data |
michael@0 | 309 | */ |
michael@0 | 310 | status = aes_cbc_decrypt(c, data, bytes_in_data); |
michael@0 | 311 | if (status) |
michael@0 | 312 | return status; |
michael@0 | 313 | |
michael@0 | 314 | /* |
michael@0 | 315 | * determine the number of padding bytes in the decrypted plaintext |
michael@0 | 316 | * - this value is always between 1 and 16, inclusive. |
michael@0 | 317 | */ |
michael@0 | 318 | num_pad_bytes = 1; |
michael@0 | 319 | pad_end = data + (*bytes_in_data - 1); |
michael@0 | 320 | while (*pad_end != 0xa0) { /* note: should check padding correctness */ |
michael@0 | 321 | pad_end--; |
michael@0 | 322 | num_pad_bytes++; |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | /* decrement data size */ |
michael@0 | 326 | *bytes_in_data -= num_pad_bytes; |
michael@0 | 327 | |
michael@0 | 328 | return err_status_ok; |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | |
michael@0 | 332 | char |
michael@0 | 333 | aes_cbc_description[] = "aes cipher block chaining (cbc) mode"; |
michael@0 | 334 | |
michael@0 | 335 | /* |
michael@0 | 336 | * Test case 0 is derived from FIPS 197 Appendix C; it uses an |
michael@0 | 337 | * all-zero IV, so that the first block encryption matches the test |
michael@0 | 338 | * case in that appendix. This property provides a check of the base |
michael@0 | 339 | * AES encryption and decryption algorithms; if CBC fails on some |
michael@0 | 340 | * particular platform, then you should print out AES intermediate |
michael@0 | 341 | * data and compare with the detailed info provided in that appendix. |
michael@0 | 342 | * |
michael@0 | 343 | */ |
michael@0 | 344 | |
michael@0 | 345 | |
michael@0 | 346 | uint8_t aes_cbc_test_case_0_key[16] = { |
michael@0 | 347 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
michael@0 | 348 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
michael@0 | 349 | }; |
michael@0 | 350 | |
michael@0 | 351 | uint8_t aes_cbc_test_case_0_plaintext[64] = { |
michael@0 | 352 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
michael@0 | 353 | 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff |
michael@0 | 354 | }; |
michael@0 | 355 | |
michael@0 | 356 | uint8_t aes_cbc_test_case_0_ciphertext[80] = { |
michael@0 | 357 | 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, |
michael@0 | 358 | 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a, |
michael@0 | 359 | 0x03, 0x35, 0xed, 0x27, 0x67, 0xf2, 0x6d, 0xf1, |
michael@0 | 360 | 0x64, 0x83, 0x2e, 0x23, 0x44, 0x38, 0x70, 0x8b |
michael@0 | 361 | |
michael@0 | 362 | }; |
michael@0 | 363 | |
michael@0 | 364 | uint8_t aes_cbc_test_case_0_iv[16] = { |
michael@0 | 365 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
michael@0 | 366 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
michael@0 | 367 | }; |
michael@0 | 368 | |
michael@0 | 369 | |
michael@0 | 370 | cipher_test_case_t aes_cbc_test_case_0 = { |
michael@0 | 371 | 16, /* octets in key */ |
michael@0 | 372 | aes_cbc_test_case_0_key, /* key */ |
michael@0 | 373 | aes_cbc_test_case_0_iv, /* initialization vector */ |
michael@0 | 374 | 16, /* octets in plaintext */ |
michael@0 | 375 | aes_cbc_test_case_0_plaintext, /* plaintext */ |
michael@0 | 376 | 32, /* octets in ciphertext */ |
michael@0 | 377 | aes_cbc_test_case_0_ciphertext, /* ciphertext */ |
michael@0 | 378 | NULL /* pointer to next testcase */ |
michael@0 | 379 | }; |
michael@0 | 380 | |
michael@0 | 381 | |
michael@0 | 382 | /* |
michael@0 | 383 | * this test case is taken directly from Appendix F.2 of NIST Special |
michael@0 | 384 | * Publication SP 800-38A |
michael@0 | 385 | */ |
michael@0 | 386 | |
michael@0 | 387 | uint8_t aes_cbc_test_case_1_key[16] = { |
michael@0 | 388 | 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, |
michael@0 | 389 | 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c, |
michael@0 | 390 | }; |
michael@0 | 391 | |
michael@0 | 392 | uint8_t aes_cbc_test_case_1_plaintext[64] = { |
michael@0 | 393 | 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, |
michael@0 | 394 | 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, |
michael@0 | 395 | 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, |
michael@0 | 396 | 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, |
michael@0 | 397 | 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, |
michael@0 | 398 | 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, |
michael@0 | 399 | 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, |
michael@0 | 400 | 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 |
michael@0 | 401 | }; |
michael@0 | 402 | |
michael@0 | 403 | uint8_t aes_cbc_test_case_1_ciphertext[80] = { |
michael@0 | 404 | 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, |
michael@0 | 405 | 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d, |
michael@0 | 406 | 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, |
michael@0 | 407 | 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2, |
michael@0 | 408 | 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, |
michael@0 | 409 | 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16, |
michael@0 | 410 | 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, |
michael@0 | 411 | 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7, |
michael@0 | 412 | 0x39, 0x34, 0x07, 0x03, 0x36, 0xd0, 0x77, 0x99, |
michael@0 | 413 | 0xe0, 0xc4, 0x2f, 0xdd, 0xa8, 0xdf, 0x4c, 0xa3 |
michael@0 | 414 | }; |
michael@0 | 415 | |
michael@0 | 416 | uint8_t aes_cbc_test_case_1_iv[16] = { |
michael@0 | 417 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
michael@0 | 418 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
michael@0 | 419 | }; |
michael@0 | 420 | |
michael@0 | 421 | cipher_test_case_t aes_cbc_test_case_1 = { |
michael@0 | 422 | 16, /* octets in key */ |
michael@0 | 423 | aes_cbc_test_case_1_key, /* key */ |
michael@0 | 424 | aes_cbc_test_case_1_iv, /* initialization vector */ |
michael@0 | 425 | 64, /* octets in plaintext */ |
michael@0 | 426 | aes_cbc_test_case_1_plaintext, /* plaintext */ |
michael@0 | 427 | 80, /* octets in ciphertext */ |
michael@0 | 428 | aes_cbc_test_case_1_ciphertext, /* ciphertext */ |
michael@0 | 429 | &aes_cbc_test_case_0 /* pointer to next testcase */ |
michael@0 | 430 | }; |
michael@0 | 431 | |
michael@0 | 432 | /* |
michael@0 | 433 | * Test case 2 is like test case 0, but for 256-bit keys. (FIPS 197 |
michael@0 | 434 | * appendix C.3). |
michael@0 | 435 | */ |
michael@0 | 436 | |
michael@0 | 437 | |
michael@0 | 438 | uint8_t aes_cbc_test_case_2_key[32] = { |
michael@0 | 439 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
michael@0 | 440 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
michael@0 | 441 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
michael@0 | 442 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f |
michael@0 | 443 | }; |
michael@0 | 444 | |
michael@0 | 445 | uint8_t aes_cbc_test_case_2_plaintext[64] = { |
michael@0 | 446 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
michael@0 | 447 | 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff |
michael@0 | 448 | }; |
michael@0 | 449 | |
michael@0 | 450 | uint8_t aes_cbc_test_case_2_ciphertext[80] = { |
michael@0 | 451 | 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, |
michael@0 | 452 | 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89, |
michael@0 | 453 | 0x72, 0x72, 0x6e, 0xe7, 0x71, 0x39, 0xbf, 0x11, |
michael@0 | 454 | 0xe5, 0x40, 0xe2, 0x7c, 0x54, 0x65, 0x1d, 0xee |
michael@0 | 455 | }; |
michael@0 | 456 | |
michael@0 | 457 | uint8_t aes_cbc_test_case_2_iv[16] = { |
michael@0 | 458 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
michael@0 | 459 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
michael@0 | 460 | }; |
michael@0 | 461 | |
michael@0 | 462 | cipher_test_case_t aes_cbc_test_case_2 = { |
michael@0 | 463 | 32, /* octets in key */ |
michael@0 | 464 | aes_cbc_test_case_2_key, /* key */ |
michael@0 | 465 | aes_cbc_test_case_2_iv, /* initialization vector */ |
michael@0 | 466 | 16, /* octets in plaintext */ |
michael@0 | 467 | aes_cbc_test_case_2_plaintext, /* plaintext */ |
michael@0 | 468 | 32, /* octets in ciphertext */ |
michael@0 | 469 | aes_cbc_test_case_2_ciphertext, /* ciphertext */ |
michael@0 | 470 | &aes_cbc_test_case_1 /* pointer to next testcase */ |
michael@0 | 471 | }; |
michael@0 | 472 | |
michael@0 | 473 | |
michael@0 | 474 | /* |
michael@0 | 475 | * this test case is taken directly from Appendix F.2 of NIST Special |
michael@0 | 476 | * Publication SP 800-38A |
michael@0 | 477 | */ |
michael@0 | 478 | |
michael@0 | 479 | uint8_t aes_cbc_test_case_3_key[32] = { |
michael@0 | 480 | 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, |
michael@0 | 481 | 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, |
michael@0 | 482 | 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, |
michael@0 | 483 | 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 |
michael@0 | 484 | }; |
michael@0 | 485 | |
michael@0 | 486 | uint8_t aes_cbc_test_case_3_plaintext[64] = { |
michael@0 | 487 | 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, |
michael@0 | 488 | 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, |
michael@0 | 489 | 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, |
michael@0 | 490 | 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, |
michael@0 | 491 | 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, |
michael@0 | 492 | 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, |
michael@0 | 493 | 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, |
michael@0 | 494 | 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 |
michael@0 | 495 | }; |
michael@0 | 496 | |
michael@0 | 497 | uint8_t aes_cbc_test_case_3_ciphertext[80] = { |
michael@0 | 498 | 0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, |
michael@0 | 499 | 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6, |
michael@0 | 500 | 0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, |
michael@0 | 501 | 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d, |
michael@0 | 502 | 0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf, |
michael@0 | 503 | 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61, |
michael@0 | 504 | 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, |
michael@0 | 505 | 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b, |
michael@0 | 506 | 0xfb, 0x98, 0x20, 0x2c, 0x45, 0xb2, 0xe4, 0xa0, |
michael@0 | 507 | 0x63, 0xc4, 0x68, 0xba, 0x84, 0x39, 0x16, 0x5a |
michael@0 | 508 | }; |
michael@0 | 509 | |
michael@0 | 510 | uint8_t aes_cbc_test_case_3_iv[16] = { |
michael@0 | 511 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
michael@0 | 512 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
michael@0 | 513 | }; |
michael@0 | 514 | |
michael@0 | 515 | cipher_test_case_t aes_cbc_test_case_3 = { |
michael@0 | 516 | 32, /* octets in key */ |
michael@0 | 517 | aes_cbc_test_case_3_key, /* key */ |
michael@0 | 518 | aes_cbc_test_case_3_iv, /* initialization vector */ |
michael@0 | 519 | 64, /* octets in plaintext */ |
michael@0 | 520 | aes_cbc_test_case_3_plaintext, /* plaintext */ |
michael@0 | 521 | 80, /* octets in ciphertext */ |
michael@0 | 522 | aes_cbc_test_case_3_ciphertext, /* ciphertext */ |
michael@0 | 523 | &aes_cbc_test_case_2 /* pointer to next testcase */ |
michael@0 | 524 | }; |
michael@0 | 525 | |
michael@0 | 526 | cipher_type_t aes_cbc = { |
michael@0 | 527 | (cipher_alloc_func_t) aes_cbc_alloc, |
michael@0 | 528 | (cipher_dealloc_func_t) aes_cbc_dealloc, |
michael@0 | 529 | (cipher_init_func_t) aes_cbc_context_init, |
michael@0 | 530 | (cipher_encrypt_func_t) aes_cbc_nist_encrypt, |
michael@0 | 531 | (cipher_decrypt_func_t) aes_cbc_nist_decrypt, |
michael@0 | 532 | (cipher_set_iv_func_t) aes_cbc_set_iv, |
michael@0 | 533 | (char *) aes_cbc_description, |
michael@0 | 534 | (int) 0, /* instance count */ |
michael@0 | 535 | (cipher_test_case_t *) &aes_cbc_test_case_3, |
michael@0 | 536 | (debug_module_t *) &mod_aes_cbc, |
michael@0 | 537 | (cipher_type_id_t) AES_CBC |
michael@0 | 538 | }; |
michael@0 | 539 | |
michael@0 | 540 |