4
* header for the cryptographic kernel
11
* Copyright(c) 2001-2006 Cisco Systems, Inc.
12
* All rights reserved.
14
* Redistribution and use in source and binary forms, with or without
15
* modification, are permitted provided that the following conditions
18
* Redistributions of source code must retain the above copyright
19
* notice, this list of conditions and the following disclaimer.
21
* Redistributions in binary form must reproduce the above
22
* copyright notice, this list of conditions and the following
23
* disclaimer in the documentation and/or other materials provided
24
* with the distribution.
26
* Neither the name of the Cisco Systems, Inc. nor the names of its
27
* contributors may be used to endorse or promote products derived
28
* from this software without specific prior written permission.
30
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41
* OF THE POSSIBILITY OF SUCH DAMAGE.
48
#include "crypto_kernel.h"
50
/* the debug module for the crypto_kernel */
52
debug_module_t mod_crypto_kernel = {
53
0, /* debugging is off by default */
54
"crypto kernel" /* printable name for module */
58
* other debug modules that can be included in the kernel
61
extern debug_module_t mod_auth;
62
extern debug_module_t mod_cipher;
63
extern debug_module_t mod_stat;
64
extern debug_module_t mod_alloc;
67
* cipher types that can be included in the kernel
70
extern cipher_type_t null_cipher;
71
extern cipher_type_t aes_icm;
72
extern cipher_type_t aes_cbc;
76
* auth func types that can be included in the kernel
79
extern auth_type_t null_auth;
80
extern auth_type_t hmac;
82
/* crypto_kernel is a global variable, the only one of its datatype */
86
crypto_kernel_state_insecure, /* start off in insecure state */
87
NULL, /* no cipher types yet */
88
NULL, /* no auth types yet */
89
NULL /* no debug modules yet */
92
#define MAX_RNG_TRIALS 25
95
crypto_kernel_init() {
98
/* check the security state */
99
if (crypto_kernel.state == crypto_kernel_state_secure) {
102
* we're already in the secure state, but we've been asked to
103
* re-initialize, so we just re-run the self-tests and then return
105
return crypto_kernel_status();
108
/* initialize error reporting system */
109
status = err_reporting_init("crypto");
113
/* load debug modules */
114
status = crypto_kernel_load_debug_module(&mod_crypto_kernel);
117
status = crypto_kernel_load_debug_module(&mod_auth);
120
status = crypto_kernel_load_debug_module(&mod_cipher);
123
status = crypto_kernel_load_debug_module(&mod_stat);
126
status = crypto_kernel_load_debug_module(&mod_alloc);
130
/* initialize random number generator */
131
status = rand_source_init();
135
/* run FIPS-140 statistical tests on rand_source */
136
status = stat_test_rand_source_with_repetition(rand_source_get_octet_string, MAX_RNG_TRIALS);
140
/* initialize pseudorandom number generator */
141
status = ctr_prng_init(rand_source_get_octet_string);
145
/* run FIPS-140 statistical tests on ctr_prng */
146
status = stat_test_rand_source_with_repetition(ctr_prng_get_octet_string, MAX_RNG_TRIALS);
150
/* load cipher types */
151
status = crypto_kernel_load_cipher_type(&null_cipher, NULL_CIPHER);
154
status = crypto_kernel_load_cipher_type(&aes_icm, AES_128_ICM);
157
status = crypto_kernel_load_cipher_type(&aes_cbc, AES_128_CBC);
161
/* load auth func types */
162
status = crypto_kernel_load_auth_type(&null_auth, NULL_AUTH);
165
status = crypto_kernel_load_auth_type(&hmac, HMAC_SHA1);
169
/* change state to secure */
170
crypto_kernel.state = crypto_kernel_state_secure;
172
return err_status_ok;
176
crypto_kernel_status() {
178
kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list;
179
kernel_auth_type_t *atype = crypto_kernel.auth_type_list;
180
kernel_debug_module_t *dm = crypto_kernel.debug_module_list;
182
/* run FIPS-140 statistical tests on rand_source */
183
printf("testing rand_source...");
184
status = stat_test_rand_source_with_repetition(rand_source_get_octet_string, MAX_RNG_TRIALS);
187
crypto_kernel.state = crypto_kernel_state_insecure;
192
/* for each cipher type, describe and test */
193
while(ctype != NULL) {
194
printf("cipher: %s\n", ctype->cipher_type->description);
195
printf(" instance count: %d\n", ctype->cipher_type->ref_count);
196
printf(" self-test: ");
197
status = cipher_type_self_test(ctype->cipher_type);
199
printf("failed with error code %d\n", status);
206
/* for each auth type, describe and test */
207
while(atype != NULL) {
208
printf("auth func: %s\n", atype->auth_type->description);
209
printf(" instance count: %d\n", atype->auth_type->ref_count);
210
printf(" self-test: ");
211
status = auth_type_self_test(atype->auth_type);
213
printf("failed with error code %d\n", status);
220
/* describe each debug module */
221
printf("debug modules loaded:\n");
223
printf(" %s ", dm->mod->name);
231
return err_status_ok;
235
crypto_kernel_list_debug_modules() {
236
kernel_debug_module_t *dm = crypto_kernel.debug_module_list;
238
/* describe each debug module */
239
printf("debug modules loaded:\n");
241
printf(" %s ", dm->mod->name);
249
return err_status_ok;
253
crypto_kernel_shutdown() {
257
* free dynamic memory used in crypto_kernel at present
260
/* walk down cipher type list, freeing memory */
261
while (crypto_kernel.cipher_type_list != NULL) {
262
kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list;
263
crypto_kernel.cipher_type_list = ctype->next;
264
debug_print(mod_crypto_kernel,
265
"freeing memory for cipher %s",
266
ctype->cipher_type->description);
270
/* walk down authetication module list, freeing memory */
271
while (crypto_kernel.auth_type_list != NULL) {
272
kernel_auth_type_t *atype = crypto_kernel.auth_type_list;
273
crypto_kernel.auth_type_list = atype->next;
274
debug_print(mod_crypto_kernel,
275
"freeing memory for authentication %s",
276
atype->auth_type->description);
280
/* walk down debug module list, freeing memory */
281
while (crypto_kernel.debug_module_list != NULL) {
282
kernel_debug_module_t *kdm = crypto_kernel.debug_module_list;
283
crypto_kernel.debug_module_list = kdm->next;
284
debug_print(mod_crypto_kernel,
285
"freeing memory for debug module %s",
290
/* de-initialize random number generator */ status = rand_source_deinit();
294
/* return to insecure state */
295
crypto_kernel.state = crypto_kernel_state_insecure;
297
return err_status_ok;
301
crypto_kernel_load_cipher_type(cipher_type_t *new_ct, cipher_type_id_t id) {
302
kernel_cipher_type_t *ctype, *new_ctype;
305
/* defensive coding */
307
return err_status_bad_param;
309
/* check cipher type by running self-test */
310
status = cipher_type_self_test(new_ct);
315
/* walk down list, checking if this type is in the list already */
316
ctype = crypto_kernel.cipher_type_list;
317
while (ctype != NULL) {
318
if ((new_ct == ctype->cipher_type) || (id == ctype->id))
319
return err_status_bad_param;
323
/* put new_ct at the head of the list */
324
/* allocate memory */
325
new_ctype = (kernel_cipher_type_t *) crypto_alloc(sizeof(kernel_cipher_type_t));
326
if (new_ctype == NULL)
327
return err_status_alloc_fail;
330
new_ctype->cipher_type = new_ct;
332
new_ctype->next = crypto_kernel.cipher_type_list;
334
/* set head of list to new cipher type */
335
crypto_kernel.cipher_type_list = new_ctype;
337
/* load debug module, if there is one present */
338
if (new_ct->debug != NULL)
339
crypto_kernel_load_debug_module(new_ct->debug);
340
/* we could check for errors here */
342
return err_status_ok;
346
crypto_kernel_load_auth_type(auth_type_t *new_at, auth_type_id_t id) {
347
kernel_auth_type_t *atype, *new_atype;
350
/* defensive coding */
352
return err_status_bad_param;
354
/* check auth type by running self-test */
355
status = auth_type_self_test(new_at);
360
/* walk down list, checking if this type is in the list already */
361
atype = crypto_kernel.auth_type_list;
362
while (atype != NULL) {
363
if ((new_at == atype->auth_type) || (id == atype->id))
364
return err_status_bad_param;
368
/* put new_at at the head of the list */
369
/* allocate memory */
370
new_atype = (kernel_auth_type_t *)crypto_alloc(sizeof(kernel_auth_type_t));
371
if (new_atype == NULL)
372
return err_status_alloc_fail;
375
new_atype->auth_type = new_at;
377
new_atype->next = crypto_kernel.auth_type_list;
379
/* set head of list to new auth type */
380
crypto_kernel.auth_type_list = new_atype;
382
/* load debug module, if there is one present */
383
if (new_at->debug != NULL)
384
crypto_kernel_load_debug_module(new_at->debug);
385
/* we could check for errors here */
387
return err_status_ok;
393
crypto_kernel_get_cipher_type(cipher_type_id_t id) {
394
kernel_cipher_type_t *ctype;
396
/* walk down list, looking for id */
397
ctype = crypto_kernel.cipher_type_list;
398
while (ctype != NULL) {
400
return ctype->cipher_type;
404
/* haven't found the right one, indicate failure by returning NULL */
410
crypto_kernel_alloc_cipher(cipher_type_id_t id,
411
cipher_pointer_t *cp,
416
* if the crypto_kernel is not yet initialized, we refuse to allocate
417
* any ciphers - this is a bit extra-paranoid
419
if (crypto_kernel.state != crypto_kernel_state_secure)
420
return err_status_init_fail;
422
ct = crypto_kernel_get_cipher_type(id);
424
return err_status_fail;
426
return ((ct)->alloc(cp, key_len));
432
crypto_kernel_get_auth_type(auth_type_id_t id) {
433
kernel_auth_type_t *atype;
435
/* walk down list, looking for id */
436
atype = crypto_kernel.auth_type_list;
437
while (atype != NULL) {
439
return atype->auth_type;
443
/* haven't found the right one, indicate failure by returning NULL */
448
crypto_kernel_alloc_auth(auth_type_id_t id,
455
* if the crypto_kernel is not yet initialized, we refuse to allocate
456
* any auth functions - this is a bit extra-paranoid
458
if (crypto_kernel.state != crypto_kernel_state_secure)
459
return err_status_init_fail;
461
at = crypto_kernel_get_auth_type(id);
463
return err_status_fail;
465
return ((at)->alloc(ap, key_len, tag_len));
469
crypto_kernel_load_debug_module(debug_module_t *new_dm) {
470
kernel_debug_module_t *kdm, *new;
472
/* defensive coding */
474
return err_status_bad_param;
476
/* walk down list, checking if this type is in the list already */
477
kdm = crypto_kernel.debug_module_list;
478
while (kdm != NULL) {
479
if (strncmp(new_dm->name, kdm->mod->name, 64) == 0)
480
return err_status_bad_param;
484
/* put new_dm at the head of the list */
485
/* allocate memory */
486
new = (kernel_debug_module_t *)crypto_alloc(sizeof(kernel_debug_module_t));
488
return err_status_alloc_fail;
492
new->next = crypto_kernel.debug_module_list;
494
/* set head of list to new cipher type */
495
crypto_kernel.debug_module_list = new;
497
return err_status_ok;
501
crypto_kernel_set_debug_module(char *name, int on) {
502
kernel_debug_module_t *kdm;
504
/* walk down list, checking if this type is in the list already */
505
kdm = crypto_kernel.debug_module_list;
506
while (kdm != NULL) {
507
if (strncmp(name, kdm->mod->name, 64) == 0) {
509
return err_status_ok;
514
return err_status_fail;
518
crypto_get_random(unsigned char *buffer, unsigned int length) {
519
if (crypto_kernel.state == crypto_kernel_state_secure)
520
return ctr_prng_get_octet_string(buffer, length);
522
return err_status_fail;