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 | * crypto_kernel.c |
michael@0 | 3 | * |
michael@0 | 4 | * header for the cryptographic kernel |
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 | * Copyright(c) 2001-2006 Cisco Systems, Inc. |
michael@0 | 12 | * All rights reserved. |
michael@0 | 13 | * |
michael@0 | 14 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 15 | * modification, are permitted provided that the following conditions |
michael@0 | 16 | * are met: |
michael@0 | 17 | * |
michael@0 | 18 | * Redistributions of source code must retain the above copyright |
michael@0 | 19 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 20 | * |
michael@0 | 21 | * Redistributions in binary form must reproduce the above |
michael@0 | 22 | * copyright notice, this list of conditions and the following |
michael@0 | 23 | * disclaimer in the documentation and/or other materials provided |
michael@0 | 24 | * with the distribution. |
michael@0 | 25 | * |
michael@0 | 26 | * Neither the name of the Cisco Systems, Inc. nor the names of its |
michael@0 | 27 | * contributors may be used to endorse or promote products derived |
michael@0 | 28 | * from this software without specific prior written permission. |
michael@0 | 29 | * |
michael@0 | 30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 31 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 32 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
michael@0 | 33 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
michael@0 | 34 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
michael@0 | 35 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
michael@0 | 36 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
michael@0 | 37 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
michael@0 | 38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
michael@0 | 39 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 40 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
michael@0 | 41 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 42 | * |
michael@0 | 43 | */ |
michael@0 | 44 | |
michael@0 | 45 | |
michael@0 | 46 | #include "alloc.h" |
michael@0 | 47 | |
michael@0 | 48 | #include "crypto_kernel.h" |
michael@0 | 49 | |
michael@0 | 50 | /* the debug module for the crypto_kernel */ |
michael@0 | 51 | |
michael@0 | 52 | debug_module_t mod_crypto_kernel = { |
michael@0 | 53 | 0, /* debugging is off by default */ |
michael@0 | 54 | "crypto kernel" /* printable name for module */ |
michael@0 | 55 | }; |
michael@0 | 56 | |
michael@0 | 57 | /* |
michael@0 | 58 | * other debug modules that can be included in the kernel |
michael@0 | 59 | */ |
michael@0 | 60 | |
michael@0 | 61 | extern debug_module_t mod_auth; |
michael@0 | 62 | extern debug_module_t mod_cipher; |
michael@0 | 63 | extern debug_module_t mod_stat; |
michael@0 | 64 | extern debug_module_t mod_alloc; |
michael@0 | 65 | |
michael@0 | 66 | /* |
michael@0 | 67 | * cipher types that can be included in the kernel |
michael@0 | 68 | */ |
michael@0 | 69 | |
michael@0 | 70 | extern cipher_type_t null_cipher; |
michael@0 | 71 | extern cipher_type_t aes_icm; |
michael@0 | 72 | extern cipher_type_t aes_cbc; |
michael@0 | 73 | |
michael@0 | 74 | |
michael@0 | 75 | /* |
michael@0 | 76 | * auth func types that can be included in the kernel |
michael@0 | 77 | */ |
michael@0 | 78 | |
michael@0 | 79 | extern auth_type_t null_auth; |
michael@0 | 80 | extern auth_type_t hmac; |
michael@0 | 81 | |
michael@0 | 82 | /* crypto_kernel is a global variable, the only one of its datatype */ |
michael@0 | 83 | |
michael@0 | 84 | crypto_kernel_t |
michael@0 | 85 | crypto_kernel = { |
michael@0 | 86 | crypto_kernel_state_insecure, /* start off in insecure state */ |
michael@0 | 87 | NULL, /* no cipher types yet */ |
michael@0 | 88 | NULL, /* no auth types yet */ |
michael@0 | 89 | NULL /* no debug modules yet */ |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | #define MAX_RNG_TRIALS 25 |
michael@0 | 93 | |
michael@0 | 94 | err_status_t |
michael@0 | 95 | crypto_kernel_init() { |
michael@0 | 96 | err_status_t status; |
michael@0 | 97 | |
michael@0 | 98 | /* check the security state */ |
michael@0 | 99 | if (crypto_kernel.state == crypto_kernel_state_secure) { |
michael@0 | 100 | |
michael@0 | 101 | /* |
michael@0 | 102 | * we're already in the secure state, but we've been asked to |
michael@0 | 103 | * re-initialize, so we just re-run the self-tests and then return |
michael@0 | 104 | */ |
michael@0 | 105 | return crypto_kernel_status(); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | /* initialize error reporting system */ |
michael@0 | 109 | status = err_reporting_init("crypto"); |
michael@0 | 110 | if (status) |
michael@0 | 111 | return status; |
michael@0 | 112 | |
michael@0 | 113 | /* load debug modules */ |
michael@0 | 114 | status = crypto_kernel_load_debug_module(&mod_crypto_kernel); |
michael@0 | 115 | if (status) |
michael@0 | 116 | return status; |
michael@0 | 117 | status = crypto_kernel_load_debug_module(&mod_auth); |
michael@0 | 118 | if (status) |
michael@0 | 119 | return status; |
michael@0 | 120 | status = crypto_kernel_load_debug_module(&mod_cipher); |
michael@0 | 121 | if (status) |
michael@0 | 122 | return status; |
michael@0 | 123 | status = crypto_kernel_load_debug_module(&mod_stat); |
michael@0 | 124 | if (status) |
michael@0 | 125 | return status; |
michael@0 | 126 | status = crypto_kernel_load_debug_module(&mod_alloc); |
michael@0 | 127 | if (status) |
michael@0 | 128 | return status; |
michael@0 | 129 | |
michael@0 | 130 | /* initialize random number generator */ |
michael@0 | 131 | status = rand_source_init(); |
michael@0 | 132 | if (status) |
michael@0 | 133 | return status; |
michael@0 | 134 | |
michael@0 | 135 | /* run FIPS-140 statistical tests on rand_source */ |
michael@0 | 136 | status = stat_test_rand_source_with_repetition(rand_source_get_octet_string, MAX_RNG_TRIALS); |
michael@0 | 137 | if (status) |
michael@0 | 138 | return status; |
michael@0 | 139 | |
michael@0 | 140 | /* initialize pseudorandom number generator */ |
michael@0 | 141 | status = ctr_prng_init(rand_source_get_octet_string); |
michael@0 | 142 | if (status) |
michael@0 | 143 | return status; |
michael@0 | 144 | |
michael@0 | 145 | /* run FIPS-140 statistical tests on ctr_prng */ |
michael@0 | 146 | status = stat_test_rand_source_with_repetition(ctr_prng_get_octet_string, MAX_RNG_TRIALS); |
michael@0 | 147 | if (status) |
michael@0 | 148 | return status; |
michael@0 | 149 | |
michael@0 | 150 | /* load cipher types */ |
michael@0 | 151 | status = crypto_kernel_load_cipher_type(&null_cipher, NULL_CIPHER); |
michael@0 | 152 | if (status) |
michael@0 | 153 | return status; |
michael@0 | 154 | status = crypto_kernel_load_cipher_type(&aes_icm, AES_ICM); |
michael@0 | 155 | if (status) |
michael@0 | 156 | return status; |
michael@0 | 157 | status = crypto_kernel_load_cipher_type(&aes_cbc, AES_CBC); |
michael@0 | 158 | if (status) |
michael@0 | 159 | return status; |
michael@0 | 160 | |
michael@0 | 161 | /* load auth func types */ |
michael@0 | 162 | status = crypto_kernel_load_auth_type(&null_auth, NULL_AUTH); |
michael@0 | 163 | if (status) |
michael@0 | 164 | return status; |
michael@0 | 165 | status = crypto_kernel_load_auth_type(&hmac, HMAC_SHA1); |
michael@0 | 166 | if (status) |
michael@0 | 167 | return status; |
michael@0 | 168 | |
michael@0 | 169 | /* change state to secure */ |
michael@0 | 170 | crypto_kernel.state = crypto_kernel_state_secure; |
michael@0 | 171 | |
michael@0 | 172 | return err_status_ok; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | err_status_t |
michael@0 | 176 | crypto_kernel_status() { |
michael@0 | 177 | err_status_t status; |
michael@0 | 178 | kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list; |
michael@0 | 179 | kernel_auth_type_t *atype = crypto_kernel.auth_type_list; |
michael@0 | 180 | kernel_debug_module_t *dm = crypto_kernel.debug_module_list; |
michael@0 | 181 | |
michael@0 | 182 | /* run FIPS-140 statistical tests on rand_source */ |
michael@0 | 183 | printf("testing rand_source..."); |
michael@0 | 184 | status = stat_test_rand_source_with_repetition(rand_source_get_octet_string, MAX_RNG_TRIALS); |
michael@0 | 185 | if (status) { |
michael@0 | 186 | printf("failed\n"); |
michael@0 | 187 | crypto_kernel.state = crypto_kernel_state_insecure; |
michael@0 | 188 | return status; |
michael@0 | 189 | } |
michael@0 | 190 | printf("passed\n"); |
michael@0 | 191 | |
michael@0 | 192 | /* for each cipher type, describe and test */ |
michael@0 | 193 | while(ctype != NULL) { |
michael@0 | 194 | printf("cipher: %s\n", ctype->cipher_type->description); |
michael@0 | 195 | printf(" instance count: %d\n", ctype->cipher_type->ref_count); |
michael@0 | 196 | printf(" self-test: "); |
michael@0 | 197 | status = cipher_type_self_test(ctype->cipher_type); |
michael@0 | 198 | if (status) { |
michael@0 | 199 | printf("failed with error code %d\n", status); |
michael@0 | 200 | exit(status); |
michael@0 | 201 | } |
michael@0 | 202 | printf("passed\n"); |
michael@0 | 203 | ctype = ctype->next; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | /* for each auth type, describe and test */ |
michael@0 | 207 | while(atype != NULL) { |
michael@0 | 208 | printf("auth func: %s\n", atype->auth_type->description); |
michael@0 | 209 | printf(" instance count: %d\n", atype->auth_type->ref_count); |
michael@0 | 210 | printf(" self-test: "); |
michael@0 | 211 | status = auth_type_self_test(atype->auth_type); |
michael@0 | 212 | if (status) { |
michael@0 | 213 | printf("failed with error code %d\n", status); |
michael@0 | 214 | exit(status); |
michael@0 | 215 | } |
michael@0 | 216 | printf("passed\n"); |
michael@0 | 217 | atype = atype->next; |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | /* describe each debug module */ |
michael@0 | 221 | printf("debug modules loaded:\n"); |
michael@0 | 222 | while (dm != NULL) { |
michael@0 | 223 | printf(" %s ", dm->mod->name); |
michael@0 | 224 | if (dm->mod->on) |
michael@0 | 225 | printf("(on)\n"); |
michael@0 | 226 | else |
michael@0 | 227 | printf("(off)\n"); |
michael@0 | 228 | dm = dm->next; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | return err_status_ok; |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | err_status_t |
michael@0 | 235 | crypto_kernel_list_debug_modules() { |
michael@0 | 236 | kernel_debug_module_t *dm = crypto_kernel.debug_module_list; |
michael@0 | 237 | |
michael@0 | 238 | /* describe each debug module */ |
michael@0 | 239 | printf("debug modules loaded:\n"); |
michael@0 | 240 | while (dm != NULL) { |
michael@0 | 241 | printf(" %s ", dm->mod->name); |
michael@0 | 242 | if (dm->mod->on) |
michael@0 | 243 | printf("(on)\n"); |
michael@0 | 244 | else |
michael@0 | 245 | printf("(off)\n"); |
michael@0 | 246 | dm = dm->next; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | return err_status_ok; |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | err_status_t |
michael@0 | 253 | crypto_kernel_shutdown() { |
michael@0 | 254 | err_status_t status; |
michael@0 | 255 | |
michael@0 | 256 | /* |
michael@0 | 257 | * free dynamic memory used in crypto_kernel at present |
michael@0 | 258 | */ |
michael@0 | 259 | |
michael@0 | 260 | /* walk down cipher type list, freeing memory */ |
michael@0 | 261 | while (crypto_kernel.cipher_type_list != NULL) { |
michael@0 | 262 | kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list; |
michael@0 | 263 | crypto_kernel.cipher_type_list = ctype->next; |
michael@0 | 264 | debug_print(mod_crypto_kernel, |
michael@0 | 265 | "freeing memory for cipher %s", |
michael@0 | 266 | ctype->cipher_type->description); |
michael@0 | 267 | crypto_free(ctype); |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | /* walk down authetication module list, freeing memory */ |
michael@0 | 271 | while (crypto_kernel.auth_type_list != NULL) { |
michael@0 | 272 | kernel_auth_type_t *atype = crypto_kernel.auth_type_list; |
michael@0 | 273 | crypto_kernel.auth_type_list = atype->next; |
michael@0 | 274 | debug_print(mod_crypto_kernel, |
michael@0 | 275 | "freeing memory for authentication %s", |
michael@0 | 276 | atype->auth_type->description); |
michael@0 | 277 | crypto_free(atype); |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | /* walk down debug module list, freeing memory */ |
michael@0 | 281 | while (crypto_kernel.debug_module_list != NULL) { |
michael@0 | 282 | kernel_debug_module_t *kdm = crypto_kernel.debug_module_list; |
michael@0 | 283 | crypto_kernel.debug_module_list = kdm->next; |
michael@0 | 284 | debug_print(mod_crypto_kernel, |
michael@0 | 285 | "freeing memory for debug module %s", |
michael@0 | 286 | kdm->mod->name); |
michael@0 | 287 | crypto_free(kdm); |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | /* de-initialize random number generator */ status = rand_source_deinit(); |
michael@0 | 291 | if (status) |
michael@0 | 292 | return status; |
michael@0 | 293 | |
michael@0 | 294 | /* return to insecure state */ |
michael@0 | 295 | crypto_kernel.state = crypto_kernel_state_insecure; |
michael@0 | 296 | |
michael@0 | 297 | return err_status_ok; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | static inline err_status_t |
michael@0 | 301 | crypto_kernel_do_load_cipher_type(cipher_type_t *new_ct, cipher_type_id_t id, |
michael@0 | 302 | int replace) { |
michael@0 | 303 | kernel_cipher_type_t *ctype, *new_ctype; |
michael@0 | 304 | err_status_t status; |
michael@0 | 305 | |
michael@0 | 306 | /* defensive coding */ |
michael@0 | 307 | if (new_ct == NULL) |
michael@0 | 308 | return err_status_bad_param; |
michael@0 | 309 | |
michael@0 | 310 | if (new_ct->id != id) |
michael@0 | 311 | return err_status_bad_param; |
michael@0 | 312 | |
michael@0 | 313 | /* check cipher type by running self-test */ |
michael@0 | 314 | status = cipher_type_self_test(new_ct); |
michael@0 | 315 | if (status) { |
michael@0 | 316 | return status; |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | /* walk down list, checking if this type is in the list already */ |
michael@0 | 320 | ctype = crypto_kernel.cipher_type_list; |
michael@0 | 321 | while (ctype != NULL) { |
michael@0 | 322 | if (id == ctype->id) { |
michael@0 | 323 | if (!replace) |
michael@0 | 324 | return err_status_bad_param; |
michael@0 | 325 | status = cipher_type_test(new_ct, ctype->cipher_type->test_data); |
michael@0 | 326 | if (status) |
michael@0 | 327 | return status; |
michael@0 | 328 | new_ctype = ctype; |
michael@0 | 329 | break; |
michael@0 | 330 | } |
michael@0 | 331 | else if (new_ct == ctype->cipher_type) |
michael@0 | 332 | return err_status_bad_param; |
michael@0 | 333 | ctype = ctype->next; |
michael@0 | 334 | } |
michael@0 | 335 | |
michael@0 | 336 | /* if not found, put new_ct at the head of the list */ |
michael@0 | 337 | if (ctype == NULL) { |
michael@0 | 338 | /* allocate memory */ |
michael@0 | 339 | new_ctype = (kernel_cipher_type_t *) crypto_alloc(sizeof(kernel_cipher_type_t)); |
michael@0 | 340 | if (new_ctype == NULL) |
michael@0 | 341 | return err_status_alloc_fail; |
michael@0 | 342 | new_ctype->next = crypto_kernel.cipher_type_list; |
michael@0 | 343 | |
michael@0 | 344 | /* set head of list to new cipher type */ |
michael@0 | 345 | crypto_kernel.cipher_type_list = new_ctype; |
michael@0 | 346 | } |
michael@0 | 347 | |
michael@0 | 348 | /* set fields */ |
michael@0 | 349 | new_ctype->cipher_type = new_ct; |
michael@0 | 350 | new_ctype->id = id; |
michael@0 | 351 | |
michael@0 | 352 | /* load debug module, if there is one present */ |
michael@0 | 353 | if (new_ct->debug != NULL) |
michael@0 | 354 | crypto_kernel_load_debug_module(new_ct->debug); |
michael@0 | 355 | /* we could check for errors here */ |
michael@0 | 356 | |
michael@0 | 357 | return err_status_ok; |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | err_status_t |
michael@0 | 361 | crypto_kernel_load_cipher_type(cipher_type_t *new_ct, cipher_type_id_t id) { |
michael@0 | 362 | return crypto_kernel_do_load_cipher_type(new_ct, id, 0); |
michael@0 | 363 | } |
michael@0 | 364 | |
michael@0 | 365 | err_status_t |
michael@0 | 366 | crypto_kernel_replace_cipher_type(cipher_type_t *new_ct, cipher_type_id_t id) { |
michael@0 | 367 | return crypto_kernel_do_load_cipher_type(new_ct, id, 1); |
michael@0 | 368 | } |
michael@0 | 369 | |
michael@0 | 370 | err_status_t |
michael@0 | 371 | crypto_kernel_do_load_auth_type(auth_type_t *new_at, auth_type_id_t id, |
michael@0 | 372 | int replace) { |
michael@0 | 373 | kernel_auth_type_t *atype, *new_atype; |
michael@0 | 374 | err_status_t status; |
michael@0 | 375 | |
michael@0 | 376 | /* defensive coding */ |
michael@0 | 377 | if (new_at == NULL) |
michael@0 | 378 | return err_status_bad_param; |
michael@0 | 379 | |
michael@0 | 380 | if (new_at->id != id) |
michael@0 | 381 | return err_status_bad_param; |
michael@0 | 382 | |
michael@0 | 383 | /* check auth type by running self-test */ |
michael@0 | 384 | status = auth_type_self_test(new_at); |
michael@0 | 385 | if (status) { |
michael@0 | 386 | return status; |
michael@0 | 387 | } |
michael@0 | 388 | |
michael@0 | 389 | /* walk down list, checking if this type is in the list already */ |
michael@0 | 390 | atype = crypto_kernel.auth_type_list; |
michael@0 | 391 | while (atype != NULL) { |
michael@0 | 392 | if (id == atype->id) { |
michael@0 | 393 | if (!replace) |
michael@0 | 394 | return err_status_bad_param; |
michael@0 | 395 | status = auth_type_test(new_at, atype->auth_type->test_data); |
michael@0 | 396 | if (status) |
michael@0 | 397 | return status; |
michael@0 | 398 | new_atype = atype; |
michael@0 | 399 | break; |
michael@0 | 400 | } |
michael@0 | 401 | else if (new_at == atype->auth_type) |
michael@0 | 402 | return err_status_bad_param; |
michael@0 | 403 | atype = atype->next; |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | /* if not found, put new_at at the head of the list */ |
michael@0 | 407 | if (atype == NULL) { |
michael@0 | 408 | /* allocate memory */ |
michael@0 | 409 | new_atype = (kernel_auth_type_t *)crypto_alloc(sizeof(kernel_auth_type_t)); |
michael@0 | 410 | if (new_atype == NULL) |
michael@0 | 411 | return err_status_alloc_fail; |
michael@0 | 412 | |
michael@0 | 413 | new_atype->next = crypto_kernel.auth_type_list; |
michael@0 | 414 | /* set head of list to new auth type */ |
michael@0 | 415 | crypto_kernel.auth_type_list = new_atype; |
michael@0 | 416 | } |
michael@0 | 417 | |
michael@0 | 418 | /* set fields */ |
michael@0 | 419 | new_atype->auth_type = new_at; |
michael@0 | 420 | new_atype->id = id; |
michael@0 | 421 | |
michael@0 | 422 | /* load debug module, if there is one present */ |
michael@0 | 423 | if (new_at->debug != NULL) |
michael@0 | 424 | crypto_kernel_load_debug_module(new_at->debug); |
michael@0 | 425 | /* we could check for errors here */ |
michael@0 | 426 | |
michael@0 | 427 | return err_status_ok; |
michael@0 | 428 | |
michael@0 | 429 | } |
michael@0 | 430 | |
michael@0 | 431 | err_status_t |
michael@0 | 432 | crypto_kernel_load_auth_type(auth_type_t *new_at, auth_type_id_t id) { |
michael@0 | 433 | return crypto_kernel_do_load_auth_type(new_at, id, 0); |
michael@0 | 434 | } |
michael@0 | 435 | |
michael@0 | 436 | err_status_t |
michael@0 | 437 | crypto_kernel_replace_auth_type(auth_type_t *new_at, auth_type_id_t id) { |
michael@0 | 438 | return crypto_kernel_do_load_auth_type(new_at, id, 1); |
michael@0 | 439 | } |
michael@0 | 440 | |
michael@0 | 441 | |
michael@0 | 442 | cipher_type_t * |
michael@0 | 443 | crypto_kernel_get_cipher_type(cipher_type_id_t id) { |
michael@0 | 444 | kernel_cipher_type_t *ctype; |
michael@0 | 445 | |
michael@0 | 446 | /* walk down list, looking for id */ |
michael@0 | 447 | ctype = crypto_kernel.cipher_type_list; |
michael@0 | 448 | while (ctype != NULL) { |
michael@0 | 449 | if (id == ctype->id) |
michael@0 | 450 | return ctype->cipher_type; |
michael@0 | 451 | ctype = ctype->next; |
michael@0 | 452 | } |
michael@0 | 453 | |
michael@0 | 454 | /* haven't found the right one, indicate failure by returning NULL */ |
michael@0 | 455 | return NULL; |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | |
michael@0 | 459 | err_status_t |
michael@0 | 460 | crypto_kernel_alloc_cipher(cipher_type_id_t id, |
michael@0 | 461 | cipher_pointer_t *cp, |
michael@0 | 462 | int key_len) { |
michael@0 | 463 | cipher_type_t *ct; |
michael@0 | 464 | |
michael@0 | 465 | /* |
michael@0 | 466 | * if the crypto_kernel is not yet initialized, we refuse to allocate |
michael@0 | 467 | * any ciphers - this is a bit extra-paranoid |
michael@0 | 468 | */ |
michael@0 | 469 | if (crypto_kernel.state != crypto_kernel_state_secure) |
michael@0 | 470 | return err_status_init_fail; |
michael@0 | 471 | |
michael@0 | 472 | ct = crypto_kernel_get_cipher_type(id); |
michael@0 | 473 | if (!ct) |
michael@0 | 474 | return err_status_fail; |
michael@0 | 475 | |
michael@0 | 476 | return ((ct)->alloc(cp, key_len)); |
michael@0 | 477 | } |
michael@0 | 478 | |
michael@0 | 479 | |
michael@0 | 480 | |
michael@0 | 481 | auth_type_t * |
michael@0 | 482 | crypto_kernel_get_auth_type(auth_type_id_t id) { |
michael@0 | 483 | kernel_auth_type_t *atype; |
michael@0 | 484 | |
michael@0 | 485 | /* walk down list, looking for id */ |
michael@0 | 486 | atype = crypto_kernel.auth_type_list; |
michael@0 | 487 | while (atype != NULL) { |
michael@0 | 488 | if (id == atype->id) |
michael@0 | 489 | return atype->auth_type; |
michael@0 | 490 | atype = atype->next; |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | /* haven't found the right one, indicate failure by returning NULL */ |
michael@0 | 494 | return NULL; |
michael@0 | 495 | } |
michael@0 | 496 | |
michael@0 | 497 | err_status_t |
michael@0 | 498 | crypto_kernel_alloc_auth(auth_type_id_t id, |
michael@0 | 499 | auth_pointer_t *ap, |
michael@0 | 500 | int key_len, |
michael@0 | 501 | int tag_len) { |
michael@0 | 502 | auth_type_t *at; |
michael@0 | 503 | |
michael@0 | 504 | /* |
michael@0 | 505 | * if the crypto_kernel is not yet initialized, we refuse to allocate |
michael@0 | 506 | * any auth functions - this is a bit extra-paranoid |
michael@0 | 507 | */ |
michael@0 | 508 | if (crypto_kernel.state != crypto_kernel_state_secure) |
michael@0 | 509 | return err_status_init_fail; |
michael@0 | 510 | |
michael@0 | 511 | at = crypto_kernel_get_auth_type(id); |
michael@0 | 512 | if (!at) |
michael@0 | 513 | return err_status_fail; |
michael@0 | 514 | |
michael@0 | 515 | return ((at)->alloc(ap, key_len, tag_len)); |
michael@0 | 516 | } |
michael@0 | 517 | |
michael@0 | 518 | err_status_t |
michael@0 | 519 | crypto_kernel_load_debug_module(debug_module_t *new_dm) { |
michael@0 | 520 | kernel_debug_module_t *kdm, *new; |
michael@0 | 521 | |
michael@0 | 522 | /* defensive coding */ |
michael@0 | 523 | if (new_dm == NULL) |
michael@0 | 524 | return err_status_bad_param; |
michael@0 | 525 | |
michael@0 | 526 | /* walk down list, checking if this type is in the list already */ |
michael@0 | 527 | kdm = crypto_kernel.debug_module_list; |
michael@0 | 528 | while (kdm != NULL) { |
michael@0 | 529 | if (strncmp(new_dm->name, kdm->mod->name, 64) == 0) |
michael@0 | 530 | return err_status_bad_param; |
michael@0 | 531 | kdm = kdm->next; |
michael@0 | 532 | } |
michael@0 | 533 | |
michael@0 | 534 | /* put new_dm at the head of the list */ |
michael@0 | 535 | /* allocate memory */ |
michael@0 | 536 | new = (kernel_debug_module_t *)crypto_alloc(sizeof(kernel_debug_module_t)); |
michael@0 | 537 | if (new == NULL) |
michael@0 | 538 | return err_status_alloc_fail; |
michael@0 | 539 | |
michael@0 | 540 | /* set fields */ |
michael@0 | 541 | new->mod = new_dm; |
michael@0 | 542 | new->next = crypto_kernel.debug_module_list; |
michael@0 | 543 | |
michael@0 | 544 | /* set head of list to new cipher type */ |
michael@0 | 545 | crypto_kernel.debug_module_list = new; |
michael@0 | 546 | |
michael@0 | 547 | return err_status_ok; |
michael@0 | 548 | } |
michael@0 | 549 | |
michael@0 | 550 | err_status_t |
michael@0 | 551 | crypto_kernel_set_debug_module(char *name, int on) { |
michael@0 | 552 | kernel_debug_module_t *kdm; |
michael@0 | 553 | |
michael@0 | 554 | /* walk down list, checking if this type is in the list already */ |
michael@0 | 555 | kdm = crypto_kernel.debug_module_list; |
michael@0 | 556 | while (kdm != NULL) { |
michael@0 | 557 | if (strncmp(name, kdm->mod->name, 64) == 0) { |
michael@0 | 558 | kdm->mod->on = on; |
michael@0 | 559 | return err_status_ok; |
michael@0 | 560 | } |
michael@0 | 561 | kdm = kdm->next; |
michael@0 | 562 | } |
michael@0 | 563 | |
michael@0 | 564 | return err_status_fail; |
michael@0 | 565 | } |
michael@0 | 566 | |
michael@0 | 567 | err_status_t |
michael@0 | 568 | crypto_get_random(unsigned char *buffer, unsigned int length) { |
michael@0 | 569 | if (crypto_kernel.state == crypto_kernel_state_secure) |
michael@0 | 570 | return ctr_prng_get_octet_string(buffer, length); |
michael@0 | 571 | else |
michael@0 | 572 | return err_status_fail; |
michael@0 | 573 | } |