Coverage Report

Created: 2025-08-26 06:53

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