| OLD | NEW |
| 1 /* | 1 /* |
| 2 * crypto_kernel.c | 2 * crypto_kernel.c |
| 3 * | 3 * |
| 4 * header for the cryptographic kernel | 4 * header for the cryptographic kernel |
| 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 * Copyright(c) 2001-2006 Cisco Systems, Inc. | 11 * Copyright(c) 2001-2006,2013 Cisco Systems, Inc. |
| 12 * All rights reserved. | 12 * All rights reserved. |
| 13 * | 13 * |
| 14 * Redistribution and use in source and binary forms, with or without | 14 * Redistribution and use in source and binary forms, with or without |
| 15 * modification, are permitted provided that the following conditions | 15 * modification, are permitted provided that the following conditions |
| 16 * are met: | 16 * are met: |
| 17 * | 17 * |
| 18 * Redistributions of source code must retain the above copyright | 18 * Redistributions of source code must retain the above copyright |
| 19 * notice, this list of conditions and the following disclaimer. | 19 * notice, this list of conditions and the following disclaimer. |
| 20 * | 20 * |
| 21 * Redistributions in binary form must reproduce the above | 21 * Redistributions in binary form must reproduce the above |
| (...skipping 14 matching lines...) Expand all Loading... |
| 36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 41 * OF THE POSSIBILITY OF SUCH DAMAGE. | 41 * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 42 * | 42 * |
| 43 */ | 43 */ |
| 44 | 44 |
| 45 | 45 |
| 46 #ifdef HAVE_CONFIG_H |
| 47 #include <config.h> |
| 48 #endif |
| 49 |
| 46 #include "alloc.h" | 50 #include "alloc.h" |
| 47 | 51 |
| 48 #include "crypto_kernel.h" | 52 #include "crypto_kernel.h" |
| 49 | 53 |
| 50 /* the debug module for the crypto_kernel */ | 54 /* the debug module for the crypto_kernel */ |
| 51 | 55 |
| 52 debug_module_t mod_crypto_kernel = { | 56 debug_module_t mod_crypto_kernel = { |
| 53 0, /* debugging is off by default */ | 57 0, /* debugging is off by default */ |
| 54 "crypto kernel" /* printable name for module */ | 58 "crypto kernel" /* printable name for module */ |
| 55 }; | 59 }; |
| 56 | 60 |
| 57 /* | 61 /* |
| 58 * other debug modules that can be included in the kernel | 62 * other debug modules that can be included in the kernel |
| 59 */ | 63 */ |
| 60 | 64 |
| 61 extern debug_module_t mod_auth; | 65 extern debug_module_t mod_auth; |
| 62 extern debug_module_t mod_cipher; | 66 extern debug_module_t mod_cipher; |
| 63 extern debug_module_t mod_stat; | 67 extern debug_module_t mod_stat; |
| 64 extern debug_module_t mod_alloc; | 68 extern debug_module_t mod_alloc; |
| 65 | 69 |
| 66 /* | 70 /* |
| 67 * cipher types that can be included in the kernel | 71 * cipher types that can be included in the kernel |
| 68 */ | 72 */ |
| 69 | 73 |
| 70 extern cipher_type_t null_cipher; | 74 extern cipher_type_t null_cipher; |
| 71 extern cipher_type_t aes_icm; | 75 extern cipher_type_t aes_icm; |
| 76 #ifndef OPENSSL |
| 72 extern cipher_type_t aes_cbc; | 77 extern cipher_type_t aes_cbc; |
| 78 #else |
| 79 extern cipher_type_t aes_gcm_128_openssl; |
| 80 extern cipher_type_t aes_gcm_256_openssl; |
| 81 #endif |
| 73 | 82 |
| 74 | 83 |
| 75 /* | 84 /* |
| 76 * auth func types that can be included in the kernel | 85 * auth func types that can be included in the kernel |
| 77 */ | 86 */ |
| 78 | 87 |
| 79 extern auth_type_t null_auth; | 88 extern auth_type_t null_auth; |
| 80 extern auth_type_t hmac; | 89 extern auth_type_t hmac; |
| 81 | 90 |
| 82 /* crypto_kernel is a global variable, the only one of its datatype */ | 91 /* crypto_kernel is a global variable, the only one of its datatype */ |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 /* initialize random number generator */ | 139 /* initialize random number generator */ |
| 131 status = rand_source_init(); | 140 status = rand_source_init(); |
| 132 if (status) | 141 if (status) |
| 133 return status; | 142 return status; |
| 134 | 143 |
| 135 /* run FIPS-140 statistical tests on rand_source */ | 144 /* run FIPS-140 statistical tests on rand_source */ |
| 136 status = stat_test_rand_source_with_repetition(rand_source_get_octet_string, M
AX_RNG_TRIALS); | 145 status = stat_test_rand_source_with_repetition(rand_source_get_octet_string, M
AX_RNG_TRIALS); |
| 137 if (status) | 146 if (status) |
| 138 return status; | 147 return status; |
| 139 | 148 |
| 149 #ifndef OPENSSL |
| 140 /* initialize pseudorandom number generator */ | 150 /* initialize pseudorandom number generator */ |
| 141 status = ctr_prng_init(rand_source_get_octet_string); | 151 status = ctr_prng_init(rand_source_get_octet_string); |
| 142 if (status) | 152 if (status) |
| 143 return status; | 153 return status; |
| 144 | 154 |
| 145 /* run FIPS-140 statistical tests on ctr_prng */ | 155 /* run FIPS-140 statistical tests on ctr_prng */ |
| 146 status = stat_test_rand_source_with_repetition(ctr_prng_get_octet_string, MAX_
RNG_TRIALS); | 156 status = stat_test_rand_source_with_repetition(ctr_prng_get_octet_string, MAX_
RNG_TRIALS); |
| 147 if (status) | 157 if (status) |
| 148 return status; | 158 return status; |
| 159 #endif |
| 149 | 160 |
| 150 /* load cipher types */ | 161 /* load cipher types */ |
| 151 status = crypto_kernel_load_cipher_type(&null_cipher, NULL_CIPHER); | 162 status = crypto_kernel_load_cipher_type(&null_cipher, NULL_CIPHER); |
| 152 if (status) | 163 if (status) |
| 153 return status; | 164 return status; |
| 154 status = crypto_kernel_load_cipher_type(&aes_icm, AES_ICM); | 165 status = crypto_kernel_load_cipher_type(&aes_icm, AES_ICM); |
| 155 if (status) | 166 if (status) |
| 156 return status; | 167 return status; |
| 168 #ifndef OPENSSL |
| 157 status = crypto_kernel_load_cipher_type(&aes_cbc, AES_CBC); | 169 status = crypto_kernel_load_cipher_type(&aes_cbc, AES_CBC); |
| 158 if (status) | 170 if (status) |
| 159 return status; | 171 return status; |
| 172 #else |
| 173 status = crypto_kernel_load_cipher_type(&aes_gcm_128_openssl, AES_128_GCM); |
| 174 if (status) { |
| 175 return status; |
| 176 } |
| 177 status = crypto_kernel_load_cipher_type(&aes_gcm_256_openssl, AES_256_GCM); |
| 178 if (status) { |
| 179 return status; |
| 180 } |
| 181 #endif |
| 160 | 182 |
| 161 /* load auth func types */ | 183 /* load auth func types */ |
| 162 status = crypto_kernel_load_auth_type(&null_auth, NULL_AUTH); | 184 status = crypto_kernel_load_auth_type(&null_auth, NULL_AUTH); |
| 163 if (status) | 185 if (status) |
| 164 return status; | 186 return status; |
| 165 status = crypto_kernel_load_auth_type(&hmac, HMAC_SHA1); | 187 status = crypto_kernel_load_auth_type(&hmac, HMAC_SHA1); |
| 166 if (status) | 188 if (status) |
| 167 return status; | 189 return status; |
| 168 | 190 |
| 169 /* change state to secure */ | 191 /* change state to secure */ |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 /* de-initialize random number generator */ status = rand_source_deinit(); | 312 /* de-initialize random number generator */ status = rand_source_deinit(); |
| 291 if (status) | 313 if (status) |
| 292 return status; | 314 return status; |
| 293 | 315 |
| 294 /* return to insecure state */ | 316 /* return to insecure state */ |
| 295 crypto_kernel.state = crypto_kernel_state_insecure; | 317 crypto_kernel.state = crypto_kernel_state_insecure; |
| 296 | 318 |
| 297 return err_status_ok; | 319 return err_status_ok; |
| 298 } | 320 } |
| 299 | 321 |
| 300 static INLINE err_status_t | 322 static inline err_status_t |
| 301 crypto_kernel_do_load_cipher_type(cipher_type_t *new_ct, cipher_type_id_t id, | 323 crypto_kernel_do_load_cipher_type(cipher_type_t *new_ct, cipher_type_id_t id, |
| 302 int replace) { | 324 int replace) { |
| 303 kernel_cipher_type_t *ctype, *new_ctype; | 325 kernel_cipher_type_t *ctype, *new_ctype; |
| 304 err_status_t status; | 326 err_status_t status; |
| 305 | 327 |
| 306 /* defensive coding */ | 328 /* defensive coding */ |
| 307 if (new_ct == NULL) | 329 if (new_ct == NULL) |
| 308 return err_status_bad_param; | 330 return err_status_bad_param; |
| 309 | 331 |
| 310 if (new_ct->id != id) | 332 if (new_ct->id != id) |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 } | 474 } |
| 453 | 475 |
| 454 /* haven't found the right one, indicate failure by returning NULL */ | 476 /* haven't found the right one, indicate failure by returning NULL */ |
| 455 return NULL; | 477 return NULL; |
| 456 } | 478 } |
| 457 | 479 |
| 458 | 480 |
| 459 err_status_t | 481 err_status_t |
| 460 crypto_kernel_alloc_cipher(cipher_type_id_t id, | 482 crypto_kernel_alloc_cipher(cipher_type_id_t id, |
| 461 cipher_pointer_t *cp, | 483 cipher_pointer_t *cp, |
| 462 » » » int key_len) { | 484 » » » int key_len, |
| 485 » » » int tag_len) { |
| 463 cipher_type_t *ct; | 486 cipher_type_t *ct; |
| 464 | 487 |
| 465 /* | 488 /* |
| 466 * if the crypto_kernel is not yet initialized, we refuse to allocate | 489 * if the crypto_kernel is not yet initialized, we refuse to allocate |
| 467 * any ciphers - this is a bit extra-paranoid | 490 * any ciphers - this is a bit extra-paranoid |
| 468 */ | 491 */ |
| 469 if (crypto_kernel.state != crypto_kernel_state_secure) | 492 if (crypto_kernel.state != crypto_kernel_state_secure) |
| 470 return err_status_init_fail; | 493 return err_status_init_fail; |
| 471 | 494 |
| 472 ct = crypto_kernel_get_cipher_type(id); | 495 ct = crypto_kernel_get_cipher_type(id); |
| 473 if (!ct) | 496 if (!ct) |
| 474 return err_status_fail; | 497 return err_status_fail; |
| 475 | 498 |
| 476 return ((ct)->alloc(cp, key_len)); | 499 return ((ct)->alloc(cp, key_len, tag_len)); |
| 477 } | 500 } |
| 478 | 501 |
| 479 | 502 |
| 480 | 503 |
| 481 auth_type_t * | 504 auth_type_t * |
| 482 crypto_kernel_get_auth_type(auth_type_id_t id) { | 505 crypto_kernel_get_auth_type(auth_type_id_t id) { |
| 483 kernel_auth_type_t *atype; | 506 kernel_auth_type_t *atype; |
| 484 | 507 |
| 485 /* walk down list, looking for id */ | 508 /* walk down list, looking for id */ |
| 486 atype = crypto_kernel.auth_type_list; | 509 atype = crypto_kernel.auth_type_list; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 } | 583 } |
| 561 kdm = kdm->next; | 584 kdm = kdm->next; |
| 562 } | 585 } |
| 563 | 586 |
| 564 return err_status_fail; | 587 return err_status_fail; |
| 565 } | 588 } |
| 566 | 589 |
| 567 err_status_t | 590 err_status_t |
| 568 crypto_get_random(unsigned char *buffer, unsigned int length) { | 591 crypto_get_random(unsigned char *buffer, unsigned int length) { |
| 569 if (crypto_kernel.state == crypto_kernel_state_secure) | 592 if (crypto_kernel.state == crypto_kernel_state_secure) |
| 593 #ifdef OPENSSL |
| 594 return rand_source_get_octet_string(buffer, length); |
| 595 #else |
| 570 return ctr_prng_get_octet_string(buffer, length); | 596 return ctr_prng_get_octet_string(buffer, length); |
| 597 #endif |
| 571 else | 598 else |
| 572 return err_status_fail; | 599 return err_status_fail; |
| 573 } | 600 } |
| OLD | NEW |