| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * cipher.c | |
| 3 * | |
| 4 * cipher meta-functions | |
| 5 * | |
| 6 * David A. McGrew | |
| 7 * Cisco Systems, Inc. | |
| 8 * | |
| 9 */ | |
| 10 | |
| 11 /* | |
| 12 * | |
| 13 * Copyright (c) 2001-2006,2013 Cisco Systems, Inc. | |
| 14 * All rights reserved. | |
| 15 * | |
| 16 * Redistribution and use in source and binary forms, with or without | |
| 17 * modification, are permitted provided that the following conditions | |
| 18 * are met: | |
| 19 * | |
| 20 * Redistributions of source code must retain the above copyright | |
| 21 * notice, this list of conditions and the following disclaimer. | |
| 22 * | |
| 23 * Redistributions in binary form must reproduce the above | |
| 24 * copyright notice, this list of conditions and the following | |
| 25 * disclaimer in the documentation and/or other materials provided | |
| 26 * with the distribution. | |
| 27 * | |
| 28 * Neither the name of the Cisco Systems, Inc. nor the names of its | |
| 29 * contributors may be used to endorse or promote products derived | |
| 30 * from this software without specific prior written permission. | |
| 31 * | |
| 32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 35 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 36 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 37 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 39 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 43 * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 44 * | |
| 45 */ | |
| 46 | |
| 47 #ifdef HAVE_CONFIG_H | |
| 48 #include <config.h> | |
| 49 #endif | |
| 50 | |
| 51 #include "cipher.h" | |
| 52 #include "crypto_types.h" | |
| 53 #include "rand_source.h" /* used in invertibiltiy tests */ | |
| 54 #include "alloc.h" /* for crypto_alloc(), crypto_free() */ | |
| 55 | |
| 56 debug_module_t mod_cipher = { | |
| 57 0, /* debugging is off by default */ | |
| 58 "cipher" /* printable module name */ | |
| 59 }; | |
| 60 | |
| 61 err_status_t | |
| 62 cipher_output(cipher_t *c, uint8_t *buffer, int num_octets_to_output) { | |
| 63 | |
| 64 /* zeroize the buffer */ | |
| 65 octet_string_set_to_zero(buffer, num_octets_to_output); | |
| 66 | |
| 67 /* exor keystream into buffer */ | |
| 68 return cipher_encrypt(c, buffer, (unsigned int *) &num_octets_to_output); | |
| 69 } | |
| 70 | |
| 71 /* some bookkeeping functions */ | |
| 72 | |
| 73 int | |
| 74 cipher_get_key_length(const cipher_t *c) { | |
| 75 return c->key_len; | |
| 76 } | |
| 77 | |
| 78 /* | |
| 79 * cipher_type_test(ct, test_data) tests a cipher of type ct against | |
| 80 * test cases provided in a list test_data of values of key, salt, iv, | |
| 81 * plaintext, and ciphertext that is known to be good | |
| 82 */ | |
| 83 | |
| 84 #define SELF_TEST_BUF_OCTETS 128 | |
| 85 #define NUM_RAND_TESTS 128 | |
| 86 #define MAX_KEY_LEN 64 | |
| 87 | |
| 88 err_status_t | |
| 89 cipher_type_test(const cipher_type_t *ct, const cipher_test_case_t *test_data) { | |
| 90 const cipher_test_case_t *test_case = test_data; | |
| 91 cipher_t *c; | |
| 92 err_status_t status; | |
| 93 uint8_t buffer[SELF_TEST_BUF_OCTETS]; | |
| 94 uint8_t buffer2[SELF_TEST_BUF_OCTETS]; | |
| 95 int tag_len; | |
| 96 unsigned int len; | |
| 97 int i, j, case_num = 0; | |
| 98 | |
| 99 debug_print(mod_cipher, "running self-test for cipher %s", | |
| 100 ct->description); | |
| 101 | |
| 102 /* | |
| 103 * check to make sure that we have at least one test case, and | |
| 104 * return an error if we don't - we need to be paranoid here | |
| 105 */ | |
| 106 if (test_case == NULL) | |
| 107 return err_status_cant_check; | |
| 108 | |
| 109 /* | |
| 110 * loop over all test cases, perform known-answer tests of both the | |
| 111 * encryption and decryption functions | |
| 112 */ | |
| 113 while (test_case != NULL) { | |
| 114 /* allocate cipher */ | |
| 115 status = cipher_type_alloc(ct, &c, test_case->key_length_octets, test_case->
tag_length_octets); | |
| 116 if (status) | |
| 117 return status; | |
| 118 | |
| 119 /* | |
| 120 * test the encrypt function | |
| 121 */ | |
| 122 debug_print(mod_cipher, "testing encryption", NULL); | |
| 123 | |
| 124 /* initialize cipher */ | |
| 125 status = cipher_init(c, test_case->key); | |
| 126 if (status) { | |
| 127 cipher_dealloc(c); | |
| 128 return status; | |
| 129 } | |
| 130 | |
| 131 /* copy plaintext into test buffer */ | |
| 132 if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) { | |
| 133 cipher_dealloc(c); | |
| 134 return err_status_bad_param; | |
| 135 } | |
| 136 for (i=0; i < test_case->plaintext_length_octets; i++) | |
| 137 buffer[i] = test_case->plaintext[i]; | |
| 138 | |
| 139 debug_print(mod_cipher, "plaintext: %s", | |
| 140 octet_string_hex_string(buffer, | |
| 141 test_case->plaintext_length_octets)); | |
| 142 | |
| 143 /* set the initialization vector */ | |
| 144 status = cipher_set_iv(c, test_case->idx, direction_encrypt); | |
| 145 if (status) { | |
| 146 cipher_dealloc(c); | |
| 147 return status; | |
| 148 } | |
| 149 | |
| 150 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { | |
| 151 debug_print(mod_cipher, "IV: %s", | |
| 152 octet_string_hex_string(test_case->idx, 12)); | |
| 153 | |
| 154 /* | |
| 155 * Set the AAD | |
| 156 */ | |
| 157 status = cipher_set_aad(c, test_case->aad, | |
| 158 test_case->aad_length_octets); | |
| 159 if (status) { | |
| 160 cipher_dealloc(c); | |
| 161 return status; | |
| 162 } | |
| 163 debug_print(mod_cipher, "AAD: %s", | |
| 164 octet_string_hex_string(test_case->aad, | |
| 165 test_case->aad_length_octets)); | |
| 166 } | |
| 167 | |
| 168 /* encrypt */ | |
| 169 len = test_case->plaintext_length_octets; | |
| 170 status = cipher_encrypt(c, buffer, &len); | |
| 171 if (status) { | |
| 172 cipher_dealloc(c); | |
| 173 return status; | |
| 174 } | |
| 175 | |
| 176 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { | |
| 177 /* | |
| 178 * Get the GCM tag | |
| 179 */ | |
| 180 status = cipher_get_tag(c, buffer + len, &tag_len); | |
| 181 if (status) { | |
| 182 cipher_dealloc(c); | |
| 183 return status; | |
| 184 } | |
| 185 len += tag_len; | |
| 186 } | |
| 187 | |
| 188 debug_print(mod_cipher, "ciphertext: %s", | |
| 189 octet_string_hex_string(buffer, | |
| 190 test_case->ciphertext_length_octets)); | |
| 191 | |
| 192 /* compare the resulting ciphertext with that in the test case */ | |
| 193 if (len != test_case->ciphertext_length_octets) | |
| 194 return err_status_algo_fail; | |
| 195 status = err_status_ok; | |
| 196 for (i=0; i < test_case->ciphertext_length_octets; i++) | |
| 197 if (buffer[i] != test_case->ciphertext[i]) { | |
| 198 status = err_status_algo_fail; | |
| 199 debug_print(mod_cipher, "test case %d failed", case_num); | |
| 200 debug_print(mod_cipher, "(failure at byte %d)", i); | |
| 201 break; | |
| 202 } | |
| 203 if (status) { | |
| 204 | |
| 205 debug_print(mod_cipher, "c computed: %s", | |
| 206 octet_string_hex_string(buffer, | |
| 207 2*test_case->plaintext_length_octets)); | |
| 208 debug_print(mod_cipher, "c expected: %s", | |
| 209 octet_string_hex_string(test_case->ciphertext, | |
| 210 2*test_case->plaintext_length_octets)); | |
| 211 | |
| 212 cipher_dealloc(c); | |
| 213 return err_status_algo_fail; | |
| 214 } | |
| 215 | |
| 216 /* | |
| 217 * test the decrypt function | |
| 218 */ | |
| 219 debug_print(mod_cipher, "testing decryption", NULL); | |
| 220 | |
| 221 /* re-initialize cipher for decryption */ | |
| 222 status = cipher_init(c, test_case->key); | |
| 223 if (status) { | |
| 224 cipher_dealloc(c); | |
| 225 return status; | |
| 226 } | |
| 227 | |
| 228 /* copy ciphertext into test buffer */ | |
| 229 if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) { | |
| 230 cipher_dealloc(c); | |
| 231 return err_status_bad_param; | |
| 232 } | |
| 233 for (i=0; i < test_case->ciphertext_length_octets; i++) | |
| 234 buffer[i] = test_case->ciphertext[i]; | |
| 235 | |
| 236 debug_print(mod_cipher, "ciphertext: %s", | |
| 237 octet_string_hex_string(buffer, | |
| 238 test_case->plaintext_length_octets)); | |
| 239 | |
| 240 /* set the initialization vector */ | |
| 241 status = cipher_set_iv(c, test_case->idx, direction_decrypt); | |
| 242 if (status) { | |
| 243 cipher_dealloc(c); | |
| 244 return status; | |
| 245 } | |
| 246 | |
| 247 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { | |
| 248 /* | |
| 249 * Set the AAD | |
| 250 */ | |
| 251 status = cipher_set_aad(c, test_case->aad, | |
| 252 test_case->aad_length_octets); | |
| 253 if (status) { | |
| 254 cipher_dealloc(c); | |
| 255 return status; | |
| 256 } | |
| 257 debug_print(mod_cipher, "AAD: %s", | |
| 258 octet_string_hex_string(test_case->aad, | |
| 259 test_case->aad_length_octets)); | |
| 260 } | |
| 261 | |
| 262 /* decrypt */ | |
| 263 len = test_case->ciphertext_length_octets; | |
| 264 status = cipher_decrypt(c, buffer, &len); | |
| 265 if (status) { | |
| 266 cipher_dealloc(c); | |
| 267 return status; | |
| 268 } | |
| 269 | |
| 270 debug_print(mod_cipher, "plaintext: %s", | |
| 271 octet_string_hex_string(buffer, | |
| 272 test_case->plaintext_length_octets)); | |
| 273 | |
| 274 /* compare the resulting plaintext with that in the test case */ | |
| 275 if (len != test_case->plaintext_length_octets) | |
| 276 return err_status_algo_fail; | |
| 277 status = err_status_ok; | |
| 278 for (i=0; i < test_case->plaintext_length_octets; i++) | |
| 279 if (buffer[i] != test_case->plaintext[i]) { | |
| 280 status = err_status_algo_fail; | |
| 281 debug_print(mod_cipher, "test case %d failed", case_num); | |
| 282 debug_print(mod_cipher, "(failure at byte %d)", i); | |
| 283 } | |
| 284 if (status) { | |
| 285 | |
| 286 debug_print(mod_cipher, "p computed: %s", | |
| 287 octet_string_hex_string(buffer, | |
| 288 2*test_case->plaintext_length_octets)); | |
| 289 debug_print(mod_cipher, "p expected: %s", | |
| 290 octet_string_hex_string(test_case->plaintext, | |
| 291 2*test_case->plaintext_length_octets)); | |
| 292 | |
| 293 cipher_dealloc(c); | |
| 294 return err_status_algo_fail; | |
| 295 } | |
| 296 | |
| 297 /* deallocate the cipher */ | |
| 298 status = cipher_dealloc(c); | |
| 299 if (status) | |
| 300 return status; | |
| 301 | |
| 302 /* | |
| 303 * the cipher passed the test case, so move on to the next test | |
| 304 * case in the list; if NULL, we'l proceed to the next test | |
| 305 */ | |
| 306 test_case = test_case->next_test_case; | |
| 307 ++case_num; | |
| 308 } | |
| 309 | |
| 310 /* now run some random invertibility tests */ | |
| 311 | |
| 312 /* allocate cipher, using paramaters from the first test case */ | |
| 313 test_case = test_data; | |
| 314 status = cipher_type_alloc(ct, &c, test_case->key_length_octets, test_case->ta
g_length_octets); | |
| 315 if (status) | |
| 316 return status; | |
| 317 | |
| 318 rand_source_init(); | |
| 319 | |
| 320 for (j=0; j < NUM_RAND_TESTS; j++) { | |
| 321 unsigned length; | |
| 322 int plaintext_len; | |
| 323 uint8_t key[MAX_KEY_LEN]; | |
| 324 uint8_t iv[MAX_KEY_LEN]; | |
| 325 | |
| 326 /* choose a length at random (leaving room for IV and padding) */ | |
| 327 length = rand() % (SELF_TEST_BUF_OCTETS - 64); | |
| 328 debug_print(mod_cipher, "random plaintext length %d\n", length); | |
| 329 status = rand_source_get_octet_string(buffer, length); | |
| 330 if (status) return status; | |
| 331 | |
| 332 debug_print(mod_cipher, "plaintext: %s", | |
| 333 octet_string_hex_string(buffer, length)); | |
| 334 | |
| 335 /* copy plaintext into second buffer */ | |
| 336 for (i=0; (unsigned int)i < length; i++) | |
| 337 buffer2[i] = buffer[i]; | |
| 338 | |
| 339 /* choose a key at random */ | |
| 340 if (test_case->key_length_octets > MAX_KEY_LEN) | |
| 341 return err_status_cant_check; | |
| 342 status = rand_source_get_octet_string(key, test_case->key_length_octets); | |
| 343 if (status) return status; | |
| 344 | |
| 345 /* chose a random initialization vector */ | |
| 346 status = rand_source_get_octet_string(iv, MAX_KEY_LEN); | |
| 347 if (status) return status; | |
| 348 | |
| 349 /* initialize cipher */ | |
| 350 status = cipher_init(c, key); | |
| 351 if (status) { | |
| 352 cipher_dealloc(c); | |
| 353 return status; | |
| 354 } | |
| 355 | |
| 356 /* set initialization vector */ | |
| 357 status = cipher_set_iv(c, test_case->idx, direction_encrypt); | |
| 358 if (status) { | |
| 359 cipher_dealloc(c); | |
| 360 return status; | |
| 361 } | |
| 362 | |
| 363 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { | |
| 364 /* | |
| 365 * Set the AAD | |
| 366 */ | |
| 367 status = cipher_set_aad(c, test_case->aad, | |
| 368 test_case->aad_length_octets); | |
| 369 if (status) { | |
| 370 cipher_dealloc(c); | |
| 371 return status; | |
| 372 } | |
| 373 debug_print(mod_cipher, "AAD: %s", | |
| 374 octet_string_hex_string(test_case->aad, | |
| 375 test_case->aad_length_octets)); | |
| 376 } | |
| 377 | |
| 378 /* encrypt buffer with cipher */ | |
| 379 plaintext_len = length; | |
| 380 status = cipher_encrypt(c, buffer, &length); | |
| 381 if (status) { | |
| 382 cipher_dealloc(c); | |
| 383 return status; | |
| 384 } | |
| 385 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { | |
| 386 /* | |
| 387 * Get the GCM tag | |
| 388 */ | |
| 389 status = cipher_get_tag(c, buffer + length, &tag_len); | |
| 390 if (status) { | |
| 391 cipher_dealloc(c); | |
| 392 return status; | |
| 393 } | |
| 394 length += tag_len; | |
| 395 } | |
| 396 debug_print(mod_cipher, "ciphertext: %s", | |
| 397 octet_string_hex_string(buffer, length)); | |
| 398 | |
| 399 /* | |
| 400 * re-initialize cipher for decryption, re-set the iv, then | |
| 401 * decrypt the ciphertext | |
| 402 */ | |
| 403 status = cipher_init(c, key); | |
| 404 if (status) { | |
| 405 cipher_dealloc(c); | |
| 406 return status; | |
| 407 } | |
| 408 status = cipher_set_iv(c, test_case->idx, direction_decrypt); | |
| 409 if (status) { | |
| 410 cipher_dealloc(c); | |
| 411 return status; | |
| 412 } | |
| 413 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { | |
| 414 /* | |
| 415 * Set the AAD | |
| 416 */ | |
| 417 status = cipher_set_aad(c, test_case->aad, | |
| 418 test_case->aad_length_octets); | |
| 419 if (status) { | |
| 420 cipher_dealloc(c); | |
| 421 return status; | |
| 422 } | |
| 423 debug_print(mod_cipher, "AAD: %s", | |
| 424 octet_string_hex_string(test_case->aad, | |
| 425 test_case->aad_length_octets)); | |
| 426 } | |
| 427 status = cipher_decrypt(c, buffer, &length); | |
| 428 if (status) { | |
| 429 cipher_dealloc(c); | |
| 430 return status; | |
| 431 } | |
| 432 | |
| 433 debug_print(mod_cipher, "plaintext[2]: %s", | |
| 434 octet_string_hex_string(buffer, length)); | |
| 435 | |
| 436 /* compare the resulting plaintext with the original one */ | |
| 437 if (length != plaintext_len) { | |
| 438 return err_status_algo_fail; | |
| 439 } | |
| 440 status = err_status_ok; | |
| 441 for (i=0; i < plaintext_len; i++) | |
| 442 if (buffer[i] != buffer2[i]) { | |
| 443 status = err_status_algo_fail; | |
| 444 debug_print(mod_cipher, "random test case %d failed", case_num); | |
| 445 debug_print(mod_cipher, "(failure at byte %d)", i); | |
| 446 } | |
| 447 if (status) { | |
| 448 cipher_dealloc(c); | |
| 449 return err_status_algo_fail; | |
| 450 } | |
| 451 | |
| 452 } | |
| 453 | |
| 454 status = cipher_dealloc(c); | |
| 455 if (status) | |
| 456 return status; | |
| 457 | |
| 458 return err_status_ok; | |
| 459 } | |
| 460 | |
| 461 | |
| 462 /* | |
| 463 * cipher_type_self_test(ct) performs cipher_type_test on ct's internal | |
| 464 * list of test data. | |
| 465 */ | |
| 466 | |
| 467 err_status_t | |
| 468 cipher_type_self_test(const cipher_type_t *ct) { | |
| 469 return cipher_type_test(ct, ct->test_data); | |
| 470 } | |
| 471 | |
| 472 /* | |
| 473 * cipher_bits_per_second(c, l, t) computes (an estimate of) the | |
| 474 * number of bits that a cipher implementation can encrypt in a second | |
| 475 * | |
| 476 * c is a cipher (which MUST be allocated and initialized already), l | |
| 477 * is the length in octets of the test data to be encrypted, and t is | |
| 478 * the number of trials | |
| 479 * | |
| 480 * if an error is encountered, the value 0 is returned | |
| 481 */ | |
| 482 | |
| 483 uint64_t | |
| 484 cipher_bits_per_second(cipher_t *c, int octets_in_buffer, int num_trials) { | |
| 485 int i; | |
| 486 v128_t nonce; | |
| 487 clock_t timer; | |
| 488 unsigned char *enc_buf; | |
| 489 unsigned int len = octets_in_buffer; | |
| 490 | |
| 491 enc_buf = (unsigned char*) crypto_alloc(octets_in_buffer); | |
| 492 if (enc_buf == NULL) | |
| 493 return 0; /* indicate bad parameters by returning null */ | |
| 494 | |
| 495 /* time repeated trials */ | |
| 496 v128_set_to_zero(&nonce); | |
| 497 timer = clock(); | |
| 498 for(i=0; i < num_trials; i++, nonce.v32[3] = i) { | |
| 499 cipher_set_iv(c, &nonce, direction_encrypt); | |
| 500 cipher_encrypt(c, enc_buf, &len); | |
| 501 } | |
| 502 timer = clock() - timer; | |
| 503 | |
| 504 crypto_free(enc_buf); | |
| 505 | |
| 506 if (timer == 0) { | |
| 507 /* Too fast! */ | |
| 508 return 0; | |
| 509 } | |
| 510 | |
| 511 return (uint64_t)CLOCKS_PER_SEC * num_trials * 8 * octets_in_buffer / timer; | |
| 512 } | |
| OLD | NEW |