blob: 4b47a44a6de31944d4bdd2b4938d1a881c3c7863 [file] [log] [blame]
Cullen Jennings235513a2005-09-21 22:51:36 +00001/*
2 * crypto_kernel.c
3 *
4 * header for the cryptographic kernel
5 *
6 * David A. McGrew
7 * Cisco Systems, Inc.
8 */
9/*
10 *
jfigus7882dd92013-08-02 16:08:23 -040011 * Copyright(c) 2001-2006,2013 Cisco Systems, Inc.
Cullen Jennings235513a2005-09-21 22:51:36 +000012 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
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.
25 *
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.
29 *
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.
42 *
43 */
44
45
Teerapap Changwichukarn6cffe242014-09-24 11:24:07 +080046#ifdef HAVE_CONFIG_H
47 #include <config.h>
48#endif
49
Cullen Jennings235513a2005-09-21 22:51:36 +000050#include "alloc.h"
51
52#include "crypto_kernel.h"
53
54/* the debug module for the crypto_kernel */
55
56debug_module_t mod_crypto_kernel = {
57 0, /* debugging is off by default */
58 "crypto kernel" /* printable name for module */
59};
60
61/*
62 * other debug modules that can be included in the kernel
63 */
64
jfigus8f669722014-11-19 15:20:03 -050065extern debug_module_t srtp_mod_auth;
jfigus9a840432014-11-19 15:48:21 -050066extern debug_module_t srtp_mod_cipher;
Cullen Jennings235513a2005-09-21 22:51:36 +000067extern debug_module_t mod_stat;
68extern debug_module_t mod_alloc;
69
70/*
71 * cipher types that can be included in the kernel
72 */
73
jfigus9de994f2014-11-20 09:17:19 -050074extern srtp_cipher_type_t srtp_null_cipher;
jfigus9a840432014-11-19 15:48:21 -050075extern srtp_cipher_type_t srtp_aes_icm;
jfigusa3127b82014-11-19 14:46:52 -050076#ifdef OPENSSL
jfigus9a840432014-11-19 15:48:21 -050077extern srtp_cipher_type_t srtp_aes_gcm_128_openssl;
78extern srtp_cipher_type_t srtp_aes_gcm_256_openssl;
jfigus0d3a2682013-04-02 15:42:37 -040079#endif
Cullen Jennings235513a2005-09-21 22:51:36 +000080
81
82/*
83 * auth func types that can be included in the kernel
84 */
85
jfigus9de994f2014-11-20 09:17:19 -050086extern srtp_auth_type_t srtp_null_auth;
jfigus8f669722014-11-19 15:20:03 -050087extern srtp_auth_type_t hmac;
Cullen Jennings235513a2005-09-21 22:51:36 +000088
89/* crypto_kernel is a global variable, the only one of its datatype */
90
91crypto_kernel_t
92crypto_kernel = {
93 crypto_kernel_state_insecure, /* start off in insecure state */
94 NULL, /* no cipher types yet */
95 NULL, /* no auth types yet */
96 NULL /* no debug modules yet */
97};
98
David McGrewb0ad0702006-03-17 20:51:24 +000099#define MAX_RNG_TRIALS 25
David McGrew026b47c2006-03-17 18:22:41 +0000100
jfigus857009c2014-11-05 11:17:43 -0500101srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000102crypto_kernel_init() {
jfigus857009c2014-11-05 11:17:43 -0500103 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000104
David McGrew98beafa2005-10-07 12:59:57 +0000105 /* check the security state */
106 if (crypto_kernel.state == crypto_kernel_state_secure) {
107
108 /*
109 * we're already in the secure state, but we've been asked to
110 * re-initialize, so we just re-run the self-tests and then return
111 */
112 return crypto_kernel_status();
113 }
114
Cullen Jennings235513a2005-09-21 22:51:36 +0000115 /* initialize error reporting system */
116 status = err_reporting_init("crypto");
117 if (status)
118 return status;
119
120 /* load debug modules */
121 status = crypto_kernel_load_debug_module(&mod_crypto_kernel);
122 if (status)
123 return status;
jfigus8f669722014-11-19 15:20:03 -0500124 status = crypto_kernel_load_debug_module(&srtp_mod_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +0000125 if (status)
126 return status;
jfigus9a840432014-11-19 15:48:21 -0500127 status = crypto_kernel_load_debug_module(&srtp_mod_cipher);
Cullen Jennings235513a2005-09-21 22:51:36 +0000128 if (status)
129 return status;
130 status = crypto_kernel_load_debug_module(&mod_stat);
131 if (status)
132 return status;
133 status = crypto_kernel_load_debug_module(&mod_alloc);
134 if (status)
135 return status;
136
137 /* initialize random number generator */
David McGrewb0ad0702006-03-17 20:51:24 +0000138 status = rand_source_init();
139 if (status)
140 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000141
142 /* run FIPS-140 statistical tests on rand_source */
David McGrewb0ad0702006-03-17 20:51:24 +0000143 status = stat_test_rand_source_with_repetition(rand_source_get_octet_string, MAX_RNG_TRIALS);
Cullen Jennings235513a2005-09-21 22:51:36 +0000144 if (status)
145 return status;
146
Cullen Jennings235513a2005-09-21 22:51:36 +0000147 /* load cipher types */
jfigus9de994f2014-11-20 09:17:19 -0500148 status = crypto_kernel_load_cipher_type(&srtp_null_cipher, NULL_CIPHER);
Cullen Jennings235513a2005-09-21 22:51:36 +0000149 if (status)
150 return status;
jfigus5a2b2d02014-11-19 14:34:20 -0500151 status = crypto_kernel_load_cipher_type(&srtp_aes_icm, AES_ICM);
Cullen Jennings235513a2005-09-21 22:51:36 +0000152 if (status)
153 return status;
jfigusa3127b82014-11-19 14:46:52 -0500154#ifdef OPENSSL
jfigus5a2b2d02014-11-19 14:34:20 -0500155 status = crypto_kernel_load_cipher_type(&srtp_aes_gcm_128_openssl, AES_128_GCM);
jfigus7882dd92013-08-02 16:08:23 -0400156 if (status) {
157 return status;
158 }
jfigus5a2b2d02014-11-19 14:34:20 -0500159 status = crypto_kernel_load_cipher_type(&srtp_aes_gcm_256_openssl, AES_256_GCM);
jfigus7882dd92013-08-02 16:08:23 -0400160 if (status) {
161 return status;
162 }
jfigus0d3a2682013-04-02 15:42:37 -0400163#endif
Cullen Jennings235513a2005-09-21 22:51:36 +0000164
165 /* load auth func types */
jfigus9de994f2014-11-20 09:17:19 -0500166 status = crypto_kernel_load_auth_type(&srtp_null_auth, NULL_AUTH);
Cullen Jennings235513a2005-09-21 22:51:36 +0000167 if (status)
168 return status;
169 status = crypto_kernel_load_auth_type(&hmac, HMAC_SHA1);
170 if (status)
171 return status;
172
173 /* change state to secure */
174 crypto_kernel.state = crypto_kernel_state_secure;
175
jfigus857009c2014-11-05 11:17:43 -0500176 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000177}
178
jfigus857009c2014-11-05 11:17:43 -0500179srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000180crypto_kernel_status() {
jfigus857009c2014-11-05 11:17:43 -0500181 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000182 kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list;
183 kernel_auth_type_t *atype = crypto_kernel.auth_type_list;
184 kernel_debug_module_t *dm = crypto_kernel.debug_module_list;
185
186 /* run FIPS-140 statistical tests on rand_source */
187 printf("testing rand_source...");
David McGrewb0ad0702006-03-17 20:51:24 +0000188 status = stat_test_rand_source_with_repetition(rand_source_get_octet_string, MAX_RNG_TRIALS);
Cullen Jennings235513a2005-09-21 22:51:36 +0000189 if (status) {
190 printf("failed\n");
191 crypto_kernel.state = crypto_kernel_state_insecure;
192 return status;
193 }
194 printf("passed\n");
195
196 /* for each cipher type, describe and test */
197 while(ctype != NULL) {
198 printf("cipher: %s\n", ctype->cipher_type->description);
Cullen Jennings235513a2005-09-21 22:51:36 +0000199 printf(" self-test: ");
jfigus9a840432014-11-19 15:48:21 -0500200 status = srtp_cipher_type_self_test(ctype->cipher_type);
Cullen Jennings235513a2005-09-21 22:51:36 +0000201 if (status) {
202 printf("failed with error code %d\n", status);
203 exit(status);
204 }
205 printf("passed\n");
206 ctype = ctype->next;
207 }
208
209 /* for each auth type, describe and test */
210 while(atype != NULL) {
211 printf("auth func: %s\n", atype->auth_type->description);
Cullen Jennings235513a2005-09-21 22:51:36 +0000212 printf(" self-test: ");
jfigus8f669722014-11-19 15:20:03 -0500213 status = srtp_auth_type_self_test(atype->auth_type);
Cullen Jennings235513a2005-09-21 22:51:36 +0000214 if (status) {
215 printf("failed with error code %d\n", status);
216 exit(status);
217 }
218 printf("passed\n");
219 atype = atype->next;
220 }
221
222 /* describe each debug module */
223 printf("debug modules loaded:\n");
224 while (dm != NULL) {
225 printf(" %s ", dm->mod->name);
226 if (dm->mod->on)
227 printf("(on)\n");
228 else
229 printf("(off)\n");
230 dm = dm->next;
231 }
232
jfigus857009c2014-11-05 11:17:43 -0500233 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000234}
235
jfigus857009c2014-11-05 11:17:43 -0500236srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000237crypto_kernel_list_debug_modules() {
238 kernel_debug_module_t *dm = crypto_kernel.debug_module_list;
239
240 /* describe each debug module */
241 printf("debug modules loaded:\n");
242 while (dm != NULL) {
243 printf(" %s ", dm->mod->name);
244 if (dm->mod->on)
245 printf("(on)\n");
246 else
247 printf("(off)\n");
248 dm = dm->next;
249 }
250
jfigus857009c2014-11-05 11:17:43 -0500251 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000252}
253
jfigus857009c2014-11-05 11:17:43 -0500254srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000255crypto_kernel_shutdown() {
jfigus857009c2014-11-05 11:17:43 -0500256 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000257
258 /*
259 * free dynamic memory used in crypto_kernel at present
260 */
261
262 /* walk down cipher type list, freeing memory */
David McGrew026b47c2006-03-17 18:22:41 +0000263 while (crypto_kernel.cipher_type_list != NULL) {
264 kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list;
265 crypto_kernel.cipher_type_list = ctype->next;
Cullen Jennings235513a2005-09-21 22:51:36 +0000266 debug_print(mod_crypto_kernel,
267 "freeing memory for cipher %s",
268 ctype->cipher_type->description);
jfigused755f52014-11-19 14:57:19 -0500269 srtp_crypto_free(ctype);
Cullen Jennings235513a2005-09-21 22:51:36 +0000270 }
271
David McGrew026b47c2006-03-17 18:22:41 +0000272 /* walk down authetication module list, freeing memory */
273 while (crypto_kernel.auth_type_list != NULL) {
274 kernel_auth_type_t *atype = crypto_kernel.auth_type_list;
275 crypto_kernel.auth_type_list = atype->next;
276 debug_print(mod_crypto_kernel,
277 "freeing memory for authentication %s",
278 atype->auth_type->description);
jfigused755f52014-11-19 14:57:19 -0500279 srtp_crypto_free(atype);
David McGrew026b47c2006-03-17 18:22:41 +0000280 }
281
282 /* walk down debug module list, freeing memory */
283 while (crypto_kernel.debug_module_list != NULL) {
284 kernel_debug_module_t *kdm = crypto_kernel.debug_module_list;
285 crypto_kernel.debug_module_list = kdm->next;
286 debug_print(mod_crypto_kernel,
287 "freeing memory for debug module %s",
288 kdm->mod->name);
jfigused755f52014-11-19 14:57:19 -0500289 srtp_crypto_free(kdm);
David McGrew026b47c2006-03-17 18:22:41 +0000290 }
291
292 /* de-initialize random number generator */ status = rand_source_deinit();
Cullen Jennings235513a2005-09-21 22:51:36 +0000293 if (status)
294 return status;
295
296 /* return to insecure state */
297 crypto_kernel.state = crypto_kernel_state_insecure;
298
jfigus857009c2014-11-05 11:17:43 -0500299 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000300}
301
jfigus857009c2014-11-05 11:17:43 -0500302static inline srtp_err_status_t
jfigus9a840432014-11-19 15:48:21 -0500303crypto_kernel_do_load_cipher_type(srtp_cipher_type_t *new_ct, srtp_cipher_type_id_t id,
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000304 int replace) {
David McGrewd8783aa2006-07-11 22:10:31 +0000305 kernel_cipher_type_t *ctype, *new_ctype;
jfigus857009c2014-11-05 11:17:43 -0500306 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000307
308 /* defensive coding */
309 if (new_ct == NULL)
jfigus857009c2014-11-05 11:17:43 -0500310 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +0000311
Jonathan Lennoxc79d2e22010-05-20 22:09:11 +0000312 if (new_ct->id != id)
jfigus857009c2014-11-05 11:17:43 -0500313 return srtp_err_status_bad_param;
Jonathan Lennoxc79d2e22010-05-20 22:09:11 +0000314
Cullen Jennings235513a2005-09-21 22:51:36 +0000315 /* check cipher type by running self-test */
jfigus9a840432014-11-19 15:48:21 -0500316 status = srtp_cipher_type_self_test(new_ct);
Cullen Jennings235513a2005-09-21 22:51:36 +0000317 if (status) {
318 return status;
319 }
320
321 /* walk down list, checking if this type is in the list already */
322 ctype = crypto_kernel.cipher_type_list;
323 while (ctype != NULL) {
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000324 if (id == ctype->id) {
325 if (!replace)
jfigus857009c2014-11-05 11:17:43 -0500326 return srtp_err_status_bad_param;
jfigus9a840432014-11-19 15:48:21 -0500327 status = srtp_cipher_type_test(new_ct, ctype->cipher_type->test_data);
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000328 if (status)
329 return status;
330 new_ctype = ctype;
331 break;
332 }
333 else if (new_ct == ctype->cipher_type)
jfigus857009c2014-11-05 11:17:43 -0500334 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +0000335 ctype = ctype->next;
336 }
337
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000338 /* if not found, put new_ct at the head of the list */
339 if (ctype == NULL) {
Cullen Jennings235513a2005-09-21 22:51:36 +0000340 /* allocate memory */
jfigused755f52014-11-19 14:57:19 -0500341 new_ctype = (kernel_cipher_type_t *) srtp_crypto_alloc(sizeof(kernel_cipher_type_t));
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000342 if (new_ctype == NULL)
jfigus857009c2014-11-05 11:17:43 -0500343 return srtp_err_status_alloc_fail;
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000344 new_ctype->next = crypto_kernel.cipher_type_list;
345
346 /* set head of list to new cipher type */
347 crypto_kernel.cipher_type_list = new_ctype;
348 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000349
350 /* set fields */
David McGrewd8783aa2006-07-11 22:10:31 +0000351 new_ctype->cipher_type = new_ct;
352 new_ctype->id = id;
Cullen Jennings235513a2005-09-21 22:51:36 +0000353
354 /* load debug module, if there is one present */
355 if (new_ct->debug != NULL)
356 crypto_kernel_load_debug_module(new_ct->debug);
357 /* we could check for errors here */
358
jfigus857009c2014-11-05 11:17:43 -0500359 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000360}
361
jfigus857009c2014-11-05 11:17:43 -0500362srtp_err_status_t
jfigus9a840432014-11-19 15:48:21 -0500363crypto_kernel_load_cipher_type(srtp_cipher_type_t *new_ct, srtp_cipher_type_id_t id) {
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000364 return crypto_kernel_do_load_cipher_type(new_ct, id, 0);
365}
366
jfigus857009c2014-11-05 11:17:43 -0500367srtp_err_status_t
jfigus9a840432014-11-19 15:48:21 -0500368crypto_kernel_replace_cipher_type(srtp_cipher_type_t *new_ct, srtp_cipher_type_id_t id) {
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000369 return crypto_kernel_do_load_cipher_type(new_ct, id, 1);
370}
371
jfigus857009c2014-11-05 11:17:43 -0500372srtp_err_status_t
jfigus8f669722014-11-19 15:20:03 -0500373crypto_kernel_do_load_auth_type(srtp_auth_type_t *new_at, srtp_auth_type_id_t id,
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000374 int replace) {
David McGrewd8783aa2006-07-11 22:10:31 +0000375 kernel_auth_type_t *atype, *new_atype;
jfigus857009c2014-11-05 11:17:43 -0500376 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000377
378 /* defensive coding */
379 if (new_at == NULL)
jfigus857009c2014-11-05 11:17:43 -0500380 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +0000381
Jonathan Lennoxc79d2e22010-05-20 22:09:11 +0000382 if (new_at->id != id)
jfigus857009c2014-11-05 11:17:43 -0500383 return srtp_err_status_bad_param;
Jonathan Lennoxc79d2e22010-05-20 22:09:11 +0000384
Cullen Jennings235513a2005-09-21 22:51:36 +0000385 /* check auth type by running self-test */
jfigus8f669722014-11-19 15:20:03 -0500386 status = srtp_auth_type_self_test(new_at);
Cullen Jennings235513a2005-09-21 22:51:36 +0000387 if (status) {
388 return status;
389 }
390
391 /* walk down list, checking if this type is in the list already */
392 atype = crypto_kernel.auth_type_list;
393 while (atype != NULL) {
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000394 if (id == atype->id) {
395 if (!replace)
jfigus857009c2014-11-05 11:17:43 -0500396 return srtp_err_status_bad_param;
jfigus8f669722014-11-19 15:20:03 -0500397 status = srtp_auth_type_test(new_at, atype->auth_type->test_data);
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000398 if (status)
399 return status;
400 new_atype = atype;
401 break;
402 }
403 else if (new_at == atype->auth_type)
jfigus857009c2014-11-05 11:17:43 -0500404 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +0000405 atype = atype->next;
406 }
407
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000408 /* if not found, put new_at at the head of the list */
409 if (atype == NULL) {
410 /* allocate memory */
jfigused755f52014-11-19 14:57:19 -0500411 new_atype = (kernel_auth_type_t *)srtp_crypto_alloc(sizeof(kernel_auth_type_t));
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000412 if (new_atype == NULL)
jfigus857009c2014-11-05 11:17:43 -0500413 return srtp_err_status_alloc_fail;
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000414
415 new_atype->next = crypto_kernel.auth_type_list;
416 /* set head of list to new auth type */
417 crypto_kernel.auth_type_list = new_atype;
418 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000419
420 /* set fields */
David McGrewd8783aa2006-07-11 22:10:31 +0000421 new_atype->auth_type = new_at;
422 new_atype->id = id;
Cullen Jennings235513a2005-09-21 22:51:36 +0000423
424 /* load debug module, if there is one present */
425 if (new_at->debug != NULL)
426 crypto_kernel_load_debug_module(new_at->debug);
427 /* we could check for errors here */
428
jfigus857009c2014-11-05 11:17:43 -0500429 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000430
431}
432
jfigus857009c2014-11-05 11:17:43 -0500433srtp_err_status_t
jfigus8f669722014-11-19 15:20:03 -0500434crypto_kernel_load_auth_type(srtp_auth_type_t *new_at, srtp_auth_type_id_t id) {
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000435 return crypto_kernel_do_load_auth_type(new_at, id, 0);
436}
437
jfigus857009c2014-11-05 11:17:43 -0500438srtp_err_status_t
jfigus8f669722014-11-19 15:20:03 -0500439crypto_kernel_replace_auth_type(srtp_auth_type_t *new_at, srtp_auth_type_id_t id) {
Jonathan Lennoxdc4d8842010-05-20 23:53:15 +0000440 return crypto_kernel_do_load_auth_type(new_at, id, 1);
441}
442
Cullen Jennings235513a2005-09-21 22:51:36 +0000443
jfigus9a840432014-11-19 15:48:21 -0500444srtp_cipher_type_t *
jfigus857009c2014-11-05 11:17:43 -0500445crypto_kernel_get_cipher_type(srtp_cipher_type_id_t id) {
Cullen Jennings235513a2005-09-21 22:51:36 +0000446 kernel_cipher_type_t *ctype;
447
448 /* walk down list, looking for id */
449 ctype = crypto_kernel.cipher_type_list;
450 while (ctype != NULL) {
451 if (id == ctype->id)
452 return ctype->cipher_type;
453 ctype = ctype->next;
454 }
455
456 /* haven't found the right one, indicate failure by returning NULL */
457 return NULL;
458}
459
460
jfigus857009c2014-11-05 11:17:43 -0500461srtp_err_status_t
462crypto_kernel_alloc_cipher(srtp_cipher_type_id_t id,
jfigus9a840432014-11-19 15:48:21 -0500463 srtp_cipher_pointer_t *cp,
jfigusc13c1002014-05-08 13:34:53 -0400464 int key_len,
465 int tag_len) {
jfigus9a840432014-11-19 15:48:21 -0500466 srtp_cipher_type_t *ct;
Cullen Jennings235513a2005-09-21 22:51:36 +0000467
468 /*
469 * if the crypto_kernel is not yet initialized, we refuse to allocate
470 * any ciphers - this is a bit extra-paranoid
471 */
472 if (crypto_kernel.state != crypto_kernel_state_secure)
jfigus857009c2014-11-05 11:17:43 -0500473 return srtp_err_status_init_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000474
475 ct = crypto_kernel_get_cipher_type(id);
476 if (!ct)
jfigus857009c2014-11-05 11:17:43 -0500477 return srtp_err_status_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000478
jfigusc13c1002014-05-08 13:34:53 -0400479 return ((ct)->alloc(cp, key_len, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +0000480}
481
482
483
jfigus8f669722014-11-19 15:20:03 -0500484srtp_auth_type_t *
jfigus857009c2014-11-05 11:17:43 -0500485crypto_kernel_get_auth_type(srtp_auth_type_id_t id) {
Cullen Jennings235513a2005-09-21 22:51:36 +0000486 kernel_auth_type_t *atype;
487
488 /* walk down list, looking for id */
489 atype = crypto_kernel.auth_type_list;
490 while (atype != NULL) {
491 if (id == atype->id)
492 return atype->auth_type;
493 atype = atype->next;
494 }
495
496 /* haven't found the right one, indicate failure by returning NULL */
497 return NULL;
498}
499
jfigus857009c2014-11-05 11:17:43 -0500500srtp_err_status_t
501crypto_kernel_alloc_auth(srtp_auth_type_id_t id,
Cullen Jennings235513a2005-09-21 22:51:36 +0000502 auth_pointer_t *ap,
503 int key_len,
504 int tag_len) {
jfigus8f669722014-11-19 15:20:03 -0500505 srtp_auth_type_t *at;
Cullen Jennings235513a2005-09-21 22:51:36 +0000506
507 /*
508 * if the crypto_kernel is not yet initialized, we refuse to allocate
509 * any auth functions - this is a bit extra-paranoid
510 */
511 if (crypto_kernel.state != crypto_kernel_state_secure)
jfigus857009c2014-11-05 11:17:43 -0500512 return srtp_err_status_init_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000513
514 at = crypto_kernel_get_auth_type(id);
515 if (!at)
jfigus857009c2014-11-05 11:17:43 -0500516 return srtp_err_status_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000517
518 return ((at)->alloc(ap, key_len, tag_len));
519}
520
jfigus857009c2014-11-05 11:17:43 -0500521srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000522crypto_kernel_load_debug_module(debug_module_t *new_dm) {
523 kernel_debug_module_t *kdm, *new;
524
525 /* defensive coding */
526 if (new_dm == NULL)
jfigus857009c2014-11-05 11:17:43 -0500527 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +0000528
529 /* walk down list, checking if this type is in the list already */
530 kdm = crypto_kernel.debug_module_list;
531 while (kdm != NULL) {
532 if (strncmp(new_dm->name, kdm->mod->name, 64) == 0)
jfigus857009c2014-11-05 11:17:43 -0500533 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +0000534 kdm = kdm->next;
535 }
536
537 /* put new_dm at the head of the list */
538 /* allocate memory */
jfigused755f52014-11-19 14:57:19 -0500539 new = (kernel_debug_module_t *)srtp_crypto_alloc(sizeof(kernel_debug_module_t));
Cullen Jennings235513a2005-09-21 22:51:36 +0000540 if (new == NULL)
jfigus857009c2014-11-05 11:17:43 -0500541 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000542
543 /* set fields */
544 new->mod = new_dm;
545 new->next = crypto_kernel.debug_module_list;
546
547 /* set head of list to new cipher type */
548 crypto_kernel.debug_module_list = new;
549
jfigus857009c2014-11-05 11:17:43 -0500550 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000551}
552
jfigus857009c2014-11-05 11:17:43 -0500553srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000554crypto_kernel_set_debug_module(char *name, int on) {
555 kernel_debug_module_t *kdm;
556
557 /* walk down list, checking if this type is in the list already */
558 kdm = crypto_kernel.debug_module_list;
559 while (kdm != NULL) {
560 if (strncmp(name, kdm->mod->name, 64) == 0) {
561 kdm->mod->on = on;
jfigus857009c2014-11-05 11:17:43 -0500562 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000563 }
564 kdm = kdm->next;
565 }
566
jfigus857009c2014-11-05 11:17:43 -0500567 return srtp_err_status_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000568}