Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: crypto/kernel/crypto_kernel.c

Issue 2344973002: Update libsrtp to version 2.0 (Closed)
Patch Set: Add '.' back to include_dirs Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « crypto/kernel/alloc.c ('k') | crypto/kernel/err.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * crypto_kernel.c
3 *
4 * header for the cryptographic kernel
5 *
6 * David A. McGrew
7 * Cisco Systems, Inc.
8 */
9 /*
10 *
11 * Copyright(c) 2001-2006,2013 Cisco Systems, Inc.
12 * 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
46 #ifdef HAVE_CONFIG_H
47 #include <config.h>
48 #endif
49
50 #include "alloc.h"
51
52 #include "crypto_kernel.h"
53
54 /* the debug module for the crypto_kernel */
55
56 srtp_debug_module_t srtp_mod_crypto_kernel = {
57 0, /* debugging is off by default */
58 "crypto kernel" /* printable name for module */
59 };
60
61 /*
62 * other generic debug modules that can be included in the kernel
63 */
64
65 extern srtp_debug_module_t srtp_mod_auth;
66 extern srtp_debug_module_t srtp_mod_cipher;
67 extern srtp_debug_module_t mod_stat;
68 extern srtp_debug_module_t mod_alloc;
69
70 /*
71 * cipher types that can be included in the kernel
72 */
73
74 extern srtp_cipher_type_t srtp_null_cipher;
75 extern srtp_cipher_type_t srtp_aes_icm;
76 #ifdef OPENSSL
77 extern srtp_cipher_type_t srtp_aes_icm_192;
78 extern srtp_cipher_type_t srtp_aes_icm_256;
79 extern srtp_cipher_type_t srtp_aes_gcm_128_openssl;
80 extern srtp_cipher_type_t srtp_aes_gcm_256_openssl;
81 #endif
82
83 /* debug modules for cipher types */
84 extern srtp_debug_module_t srtp_mod_aes_icm;
85 #ifdef OPENSSL
86 extern srtp_debug_module_t srtp_mod_aes_gcm;
87 #endif
88
89 /*
90 * auth func types that can be included in the kernel
91 */
92
93 extern srtp_auth_type_t srtp_null_auth;
94 extern srtp_auth_type_t srtp_hmac;
95
96 /* debug modules for auth types */
97 extern srtp_debug_module_t srtp_mod_hmac;
98
99 /* crypto_kernel is a global variable, the only one of its datatype */
100
101 srtp_crypto_kernel_t crypto_kernel = {
102 srtp_crypto_kernel_state_insecure, /* start off in insecure state */
103 NULL, /* no cipher types yet */
104 NULL, /* no auth types yet */
105 NULL /* no debug modules yet */
106 };
107
108 #define MAX_RNG_TRIALS 25
109
110 srtp_err_status_t srtp_crypto_kernel_init ()
111 {
112 srtp_err_status_t status;
113
114 /* check the security state */
115 if (crypto_kernel.state == srtp_crypto_kernel_state_secure) {
116
117 /*
118 * we're already in the secure state, but we've been asked to
119 * re-initialize, so we just re-run the self-tests and then return
120 */
121 return srtp_crypto_kernel_status();
122 }
123
124 /* initialize error reporting system */
125 status = srtp_err_reporting_init("crypto");
126 if (status) {
127 return status;
128 }
129
130 /* load debug modules */
131 status = srtp_crypto_kernel_load_debug_module(&srtp_mod_crypto_kernel);
132 if (status) {
133 return status;
134 }
135 status = srtp_crypto_kernel_load_debug_module(&srtp_mod_auth);
136 if (status) {
137 return status;
138 }
139 status = srtp_crypto_kernel_load_debug_module(&srtp_mod_cipher);
140 if (status) {
141 return status;
142 }
143 status = srtp_crypto_kernel_load_debug_module(&mod_stat);
144 if (status) {
145 return status;
146 }
147 status = srtp_crypto_kernel_load_debug_module(&mod_alloc);
148 if (status) {
149 return status;
150 }
151
152 /* load cipher types */
153 status = srtp_crypto_kernel_load_cipher_type(&srtp_null_cipher, SRTP_NULL_CI PHER);
154 if (status) {
155 return status;
156 }
157 status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_icm, SRTP_AES_ICM);
158 if (status) {
159 return status;
160 }
161 status = srtp_crypto_kernel_load_debug_module(&srtp_mod_aes_icm);
162 if (status) {
163 return status;
164 }
165 #ifdef OPENSSL
166 status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_icm_192, SRTP_AES_192 _ICM);
167 if (status) {
168 return status;
169 }
170 status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_icm_256, SRTP_AES_256 _ICM);
171 if (status) {
172 return status;
173 }
174 status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_gcm_128_openssl, SRTP _AES_128_GCM);
175 if (status) {
176 return status;
177 }
178 status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_gcm_256_openssl, SRTP _AES_256_GCM);
179 if (status) {
180 return status;
181 }
182 status = srtp_crypto_kernel_load_debug_module(&srtp_mod_aes_gcm);
183 if (status) {
184 return status;
185 }
186 #endif
187
188 /* load auth func types */
189 status = srtp_crypto_kernel_load_auth_type(&srtp_null_auth, SRTP_NULL_AUTH);
190 if (status) {
191 return status;
192 }
193 status = srtp_crypto_kernel_load_auth_type(&srtp_hmac, SRTP_HMAC_SHA1);
194 if (status) {
195 return status;
196 }
197 status = srtp_crypto_kernel_load_debug_module(&srtp_mod_hmac);
198 if (status) {
199 return status;
200 }
201
202 /* change state to secure */
203 crypto_kernel.state = srtp_crypto_kernel_state_secure;
204
205 return srtp_err_status_ok;
206 }
207
208 srtp_err_status_t srtp_crypto_kernel_status ()
209 {
210 srtp_err_status_t status;
211 srtp_kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list;
212 srtp_kernel_auth_type_t *atype = crypto_kernel.auth_type_list;
213 srtp_kernel_debug_module_t *dm = crypto_kernel.debug_module_list;
214
215 /* for each cipher type, describe and test */
216 while (ctype != NULL) {
217 printf("cipher: %s\n", ctype->cipher_type->description);
218 printf(" self-test: ");
219 status = srtp_cipher_type_self_test(ctype->cipher_type);
220 if (status) {
221 printf("failed with error code %d\n", status);
222 exit(status);
223 }
224 printf("passed\n");
225 ctype = ctype->next;
226 }
227
228 /* for each auth type, describe and test */
229 while (atype != NULL) {
230 printf("auth func: %s\n", atype->auth_type->description);
231 printf(" self-test: ");
232 status = srtp_auth_type_self_test(atype->auth_type);
233 if (status) {
234 printf("failed with error code %d\n", status);
235 exit(status);
236 }
237 printf("passed\n");
238 atype = atype->next;
239 }
240
241 /* describe each debug module */
242 printf("debug modules loaded:\n");
243 while (dm != NULL) {
244 printf(" %s ", dm->mod->name);
245 if (dm->mod->on) {
246 printf("(on)\n");
247 } else{
248 printf("(off)\n");
249 }
250 dm = dm->next;
251 }
252
253 return srtp_err_status_ok;
254 }
255
256 srtp_err_status_t srtp_crypto_kernel_list_debug_modules ()
257 {
258 srtp_kernel_debug_module_t *dm = crypto_kernel.debug_module_list;
259
260 /* describe each debug module */
261 printf("debug modules loaded:\n");
262 while (dm != NULL) {
263 printf(" %s ", dm->mod->name);
264 if (dm->mod->on) {
265 printf("(on)\n");
266 } else{
267 printf("(off)\n");
268 }
269 dm = dm->next;
270 }
271
272 return srtp_err_status_ok;
273 }
274
275 srtp_err_status_t srtp_crypto_kernel_shutdown ()
276 {
277 /*
278 * free dynamic memory used in crypto_kernel at present
279 */
280
281 /* walk down cipher type list, freeing memory */
282 while (crypto_kernel.cipher_type_list != NULL) {
283 srtp_kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list;
284 crypto_kernel.cipher_type_list = ctype->next;
285 debug_print(srtp_mod_crypto_kernel,
286 "freeing memory for cipher %s",
287 ctype->cipher_type->description);
288 srtp_crypto_free(ctype);
289 }
290
291 /* walk down authetication module list, freeing memory */
292 while (crypto_kernel.auth_type_list != NULL) {
293 srtp_kernel_auth_type_t *atype = crypto_kernel.auth_type_list;
294 crypto_kernel.auth_type_list = atype->next;
295 debug_print(srtp_mod_crypto_kernel,
296 "freeing memory for authentication %s",
297 atype->auth_type->description);
298 srtp_crypto_free(atype);
299 }
300
301 /* walk down debug module list, freeing memory */
302 while (crypto_kernel.debug_module_list != NULL) {
303 srtp_kernel_debug_module_t *kdm = crypto_kernel.debug_module_list;
304 crypto_kernel.debug_module_list = kdm->next;
305 debug_print(srtp_mod_crypto_kernel,
306 "freeing memory for debug module %s",
307 kdm->mod->name);
308 srtp_crypto_free(kdm);
309 }
310
311 /* return to insecure state */
312 crypto_kernel.state = srtp_crypto_kernel_state_insecure;
313
314 return srtp_err_status_ok;
315 }
316
317 static inline srtp_err_status_t srtp_crypto_kernel_do_load_cipher_type (const sr tp_cipher_type_t *new_ct, srtp_cipher_type_id_t id, int replace)
318 {
319 srtp_kernel_cipher_type_t *ctype, *new_ctype;
320 srtp_err_status_t status;
321
322 /* defensive coding */
323 if (new_ct == NULL) {
324 return srtp_err_status_bad_param;
325 }
326
327 if (new_ct->id != id) {
328 return srtp_err_status_bad_param;
329 }
330
331 /* check cipher type by running self-test */
332 status = srtp_cipher_type_self_test(new_ct);
333 if (status) {
334 return status;
335 }
336
337 /* walk down list, checking if this type is in the list already */
338 ctype = crypto_kernel.cipher_type_list;
339 while (ctype != NULL) {
340 if (id == ctype->id) {
341 if (!replace) {
342 return srtp_err_status_bad_param;
343 }
344 status = srtp_cipher_type_test(new_ct, ctype->cipher_type->test_data );
345 if (status) {
346 return status;
347 }
348 new_ctype = ctype;
349 break;
350 }else if (new_ct == ctype->cipher_type) {
351 return srtp_err_status_bad_param;
352 }
353 ctype = ctype->next;
354 }
355
356 /* if not found, put new_ct at the head of the list */
357 if (ctype == NULL) {
358 /* allocate memory */
359 new_ctype = (srtp_kernel_cipher_type_t*)srtp_crypto_alloc(sizeof(srtp_ke rnel_cipher_type_t));
360 if (new_ctype == NULL) {
361 return srtp_err_status_alloc_fail;
362 }
363 new_ctype->next = crypto_kernel.cipher_type_list;
364
365 /* set head of list to new cipher type */
366 crypto_kernel.cipher_type_list = new_ctype;
367 }
368
369 /* set fields */
370 new_ctype->cipher_type = new_ct;
371 new_ctype->id = id;
372
373 return srtp_err_status_ok;
374 }
375
376 srtp_err_status_t srtp_crypto_kernel_load_cipher_type (const srtp_cipher_type_t *new_ct, srtp_cipher_type_id_t id)
377 {
378 return srtp_crypto_kernel_do_load_cipher_type(new_ct, id, 0);
379 }
380
381 srtp_err_status_t srtp_replace_cipher_type (const srtp_cipher_type_t *new_ct, sr tp_cipher_type_id_t id)
382 {
383 return srtp_crypto_kernel_do_load_cipher_type(new_ct, id, 1);
384 }
385
386 srtp_err_status_t srtp_crypto_kernel_do_load_auth_type (const srtp_auth_type_t * new_at, srtp_auth_type_id_t id, int replace)
387 {
388 srtp_kernel_auth_type_t *atype, *new_atype;
389 srtp_err_status_t status;
390
391 /* defensive coding */
392 if (new_at == NULL) {
393 return srtp_err_status_bad_param;
394 }
395
396 if (new_at->id != id) {
397 return srtp_err_status_bad_param;
398 }
399
400 /* check auth type by running self-test */
401 status = srtp_auth_type_self_test(new_at);
402 if (status) {
403 return status;
404 }
405
406 /* walk down list, checking if this type is in the list already */
407 atype = crypto_kernel.auth_type_list;
408 while (atype != NULL) {
409 if (id == atype->id) {
410 if (!replace) {
411 return srtp_err_status_bad_param;
412 }
413 status = srtp_auth_type_test(new_at, atype->auth_type->test_data);
414 if (status) {
415 return status;
416 }
417 new_atype = atype;
418 break;
419 }else if (new_at == atype->auth_type) {
420 return srtp_err_status_bad_param;
421 }
422 atype = atype->next;
423 }
424
425 /* if not found, put new_at at the head of the list */
426 if (atype == NULL) {
427 /* allocate memory */
428 new_atype = (srtp_kernel_auth_type_t*)srtp_crypto_alloc(sizeof(srtp_kern el_auth_type_t));
429 if (new_atype == NULL) {
430 return srtp_err_status_alloc_fail;
431 }
432
433 new_atype->next = crypto_kernel.auth_type_list;
434 /* set head of list to new auth type */
435 crypto_kernel.auth_type_list = new_atype;
436 }
437
438 /* set fields */
439 new_atype->auth_type = new_at;
440 new_atype->id = id;
441
442 return srtp_err_status_ok;
443
444 }
445
446 srtp_err_status_t srtp_crypto_kernel_load_auth_type (const srtp_auth_type_t *new _at, srtp_auth_type_id_t id)
447 {
448 return srtp_crypto_kernel_do_load_auth_type(new_at, id, 0);
449 }
450
451 srtp_err_status_t srtp_replace_auth_type (const srtp_auth_type_t *new_at, srtp_a uth_type_id_t id)
452 {
453 return srtp_crypto_kernel_do_load_auth_type(new_at, id, 1);
454 }
455
456
457 const srtp_cipher_type_t * srtp_crypto_kernel_get_cipher_type (srtp_cipher_type_ id_t id)
458 {
459 srtp_kernel_cipher_type_t *ctype;
460
461 /* walk down list, looking for id */
462 ctype = crypto_kernel.cipher_type_list;
463 while (ctype != NULL) {
464 if (id == ctype->id) {
465 return ctype->cipher_type;
466 }
467 ctype = ctype->next;
468 }
469
470 /* haven't found the right one, indicate failure by returning NULL */
471 return NULL;
472 }
473
474
475 srtp_err_status_t srtp_crypto_kernel_alloc_cipher (srtp_cipher_type_id_t id, srt p_cipher_pointer_t *cp, int key_len, int tag_len)
476 {
477 const srtp_cipher_type_t *ct;
478
479 /*
480 * if the crypto_kernel is not yet initialized, we refuse to allocate
481 * any ciphers - this is a bit extra-paranoid
482 */
483 if (crypto_kernel.state != srtp_crypto_kernel_state_secure) {
484 return srtp_err_status_init_fail;
485 }
486
487 ct = srtp_crypto_kernel_get_cipher_type(id);
488 if (!ct) {
489 return srtp_err_status_fail;
490 }
491
492 return ((ct)->alloc(cp, key_len, tag_len));
493 }
494
495
496
497 const srtp_auth_type_t * srtp_crypto_kernel_get_auth_type (srtp_auth_type_id_t i d)
498 {
499 srtp_kernel_auth_type_t *atype;
500
501 /* walk down list, looking for id */
502 atype = crypto_kernel.auth_type_list;
503 while (atype != NULL) {
504 if (id == atype->id) {
505 return atype->auth_type;
506 }
507 atype = atype->next;
508 }
509
510 /* haven't found the right one, indicate failure by returning NULL */
511 return NULL;
512 }
513
514 srtp_err_status_t srtp_crypto_kernel_alloc_auth (srtp_auth_type_id_t id, srtp_au th_pointer_t *ap, int key_len, int tag_len)
515 {
516 const srtp_auth_type_t *at;
517
518 /*
519 * if the crypto_kernel is not yet initialized, we refuse to allocate
520 * any auth functions - this is a bit extra-paranoid
521 */
522 if (crypto_kernel.state != srtp_crypto_kernel_state_secure) {
523 return srtp_err_status_init_fail;
524 }
525
526 at = srtp_crypto_kernel_get_auth_type(id);
527 if (!at) {
528 return srtp_err_status_fail;
529 }
530
531 return ((at)->alloc(ap, key_len, tag_len));
532 }
533
534 srtp_err_status_t srtp_crypto_kernel_load_debug_module (srtp_debug_module_t *new _dm)
535 {
536 srtp_kernel_debug_module_t *kdm, *new;
537
538 /* defensive coding */
539 if (new_dm == NULL) {
540 return srtp_err_status_bad_param;
541 }
542
543 /* walk down list, checking if this type is in the list already */
544 kdm = crypto_kernel.debug_module_list;
545 while (kdm != NULL) {
546 if (strncmp(new_dm->name, kdm->mod->name, 64) == 0) {
547 return srtp_err_status_bad_param;
548 }
549 kdm = kdm->next;
550 }
551
552 /* put new_dm at the head of the list */
553 /* allocate memory */
554 new = (srtp_kernel_debug_module_t*)srtp_crypto_alloc(sizeof(srtp_kernel_debu g_module_t));
555 if (new == NULL) {
556 return srtp_err_status_alloc_fail;
557 }
558
559 /* set fields */
560 new->mod = new_dm;
561 new->next = crypto_kernel.debug_module_list;
562
563 /* set head of list to new cipher type */
564 crypto_kernel.debug_module_list = new;
565
566 return srtp_err_status_ok;
567 }
568
569 srtp_err_status_t srtp_crypto_kernel_set_debug_module (char *name, int on)
570 {
571 srtp_kernel_debug_module_t *kdm;
572
573 /* walk down list, checking if this type is in the list already */
574 kdm = crypto_kernel.debug_module_list;
575 while (kdm != NULL) {
576 if (strncmp(name, kdm->mod->name, 64) == 0) {
577 kdm->mod->on = on;
578 return srtp_err_status_ok;
579 }
580 kdm = kdm->next;
581 }
582
583 return srtp_err_status_fail;
584 }
OLDNEW
« no previous file with comments | « crypto/kernel/alloc.c ('k') | crypto/kernel/err.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698