Coverage Report

Created: 2026-12-01 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsrtp/crypto/cipher/cipher.c
Line
Count
Source
1
/*
2
 * cipher.c
3
 *
4
 * cipher meta-functions
5
 *
6
 * David A. McGrew
7
 * Cisco Systems, Inc.
8
 *
9
 */
10
11
/*
12
 *
13
 * Copyright (c) 2001-2017 Cisco Systems, Inc.
14
 * All rights reserved.
15
 *
16
 * Redistribution and use in source and binary forms, with or without
17
 * modification, are permitted provided that the following conditions
18
 * are met:
19
 *
20
 *   Redistributions of source code must retain the above copyright
21
 *   notice, this list of conditions and the following disclaimer.
22
 *
23
 *   Redistributions in binary form must reproduce the above
24
 *   copyright notice, this list of conditions and the following
25
 *   disclaimer in the documentation and/or other materials provided
26
 *   with the distribution.
27
 *
28
 *   Neither the name of the Cisco Systems, Inc. nor the names of its
29
 *   contributors may be used to endorse or promote products derived
30
 *   from this software without specific prior written permission.
31
 *
32
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
36
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43
 * OF THE POSSIBILITY OF SUCH DAMAGE.
44
 *
45
 */
46
47
#ifdef HAVE_CONFIG_H
48
#include <config.h>
49
#endif
50
51
#include "cipher.h"
52
#include "cipher_priv.h"
53
#include "crypto_types.h"
54
#include "err.h"   /* for srtp_debug */
55
#include "alloc.h" /* for crypto_alloc(), crypto_free()  */
56
57
#include <stdlib.h>
58
59
srtp_debug_module_t srtp_mod_cipher = {
60
    false,   /* debugging is off by default */
61
    "cipher" /* printable module name       */
62
};
63
64
srtp_err_status_t srtp_cipher_type_alloc(const srtp_cipher_type_t *ct,
65
                                         srtp_cipher_t **c,
66
                                         size_t key_len,
67
                                         size_t tlen)
68
1.54k
{
69
1.54k
    if (!ct || !ct->alloc) {
70
0
        return (srtp_err_status_bad_param);
71
0
    }
72
1.54k
    return ((ct)->alloc((c), (key_len), (tlen)));
73
1.54k
}
74
75
srtp_err_status_t srtp_cipher_dealloc(srtp_cipher_t *c)
76
78.6k
{
77
78.6k
    if (!c || !c->type) {
78
0
        return (srtp_err_status_bad_param);
79
0
    }
80
78.6k
    return (((c)->type)->dealloc(c));
81
78.6k
}
82
83
srtp_err_status_t srtp_cipher_init(srtp_cipher_t *c, const uint8_t *key)
84
78.6k
{
85
78.6k
    srtp_err_status_t status;
86
87
78.6k
    if (!c || !c->type || !c->state || c->initialized) {
88
0
        return (srtp_err_status_bad_param);
89
0
    }
90
91
    /*
92
     * The attempt is consumed before the backend runs. A failed init can
93
     * leave backend state behind, so the caller deallocates instead of
94
     * retrying.
95
     */
96
78.6k
    c->initialized = true;
97
78.6k
    status = c->type->init(c->state, key);
98
78.6k
    return status;
99
78.6k
}
100
101
srtp_err_status_t srtp_cipher_set_iv(srtp_cipher_t *c,
102
                                     uint8_t *iv,
103
                                     srtp_cipher_direction_t direction)
104
420k
{
105
420k
    if (!c || !c->type || !c->state) {
106
0
        return (srtp_err_status_bad_param);
107
0
    }
108
109
420k
    return (((c)->type)->set_iv(((c)->state), iv, direction));
110
420k
}
111
112
srtp_err_status_t srtp_cipher_output(srtp_cipher_t *c,
113
                                     uint8_t *buffer,
114
                                     size_t *num_octets_to_output)
115
110k
{
116
    /* zeroize the buffer */
117
110k
    octet_string_set_to_zero(buffer, *num_octets_to_output);
118
119
    /* exor keystream into buffer */
120
110k
    return (((c)->type)->encrypt(((c)->state), buffer, *num_octets_to_output,
121
110k
                                 buffer, num_octets_to_output));
122
110k
}
123
124
srtp_err_status_t srtp_cipher_encrypt(srtp_cipher_t *c,
125
                                      const uint8_t *src,
126
                                      size_t src_len,
127
                                      uint8_t *dst,
128
                                      size_t *dst_len)
129
354k
{
130
354k
    if (!c || !c->type || !c->state) {
131
0
        return (srtp_err_status_bad_param);
132
0
    }
133
134
354k
    return (((c)->type)->encrypt(((c)->state), src, src_len, dst, dst_len));
135
354k
}
136
137
srtp_err_status_t srtp_cipher_decrypt(srtp_cipher_t *c,
138
                                      const uint8_t *src,
139
                                      size_t src_len,
140
                                      uint8_t *dst,
141
                                      size_t *dst_len)
142
777
{
143
777
    if (!c || !c->type || !c->state) {
144
0
        return (srtp_err_status_bad_param);
145
0
    }
146
147
777
    return (((c)->type)->decrypt(((c)->state), src, src_len, dst, dst_len));
148
777
}
149
150
srtp_err_status_t srtp_cipher_set_aad(srtp_cipher_t *c,
151
                                      const uint8_t *aad,
152
                                      size_t aad_len)
153
0
{
154
0
    if (!c || !c->type || !c->state) {
155
0
        return (srtp_err_status_bad_param);
156
0
    }
157
0
    if (!((c)->type)->set_aad) {
158
0
        return (srtp_err_status_no_such_op);
159
0
    }
160
161
0
    return (((c)->type)->set_aad(((c)->state), aad, aad_len));
162
0
}
163
164
/* some bookkeeping functions */
165
166
size_t srtp_cipher_get_key_length(const srtp_cipher_t *c)
167
38.5k
{
168
38.5k
    return c->key_len;
169
38.5k
}
170
171
/*
172
 * A trivial platform independent random source.
173
 * For use in test only.
174
 */
175
void srtp_cipher_rand_for_tests(uint8_t *dest, size_t len)
176
3.07k
{
177
    /* Generic C-library (rand()) version */
178
    /* This is a random source of last resort */
179
98.7k
    while (len) {
180
95.6k
        int val = rand();
181
        /* rand() returns 0-32767 (ugh) */
182
        /* Is this a good enough way to get random bytes?
183
           It is if it passes FIPS-140... */
184
95.6k
        *dest++ = val & 0xff;
185
95.6k
        len--;
186
95.6k
    }
187
3.07k
}
188
189
/*
190
 * A trivial platform independent 32 bit random number.
191
 * For use in test only.
192
 */
193
uint32_t srtp_cipher_rand_u32_for_tests(void)
194
768
{
195
768
    uint32_t r;
196
768
    srtp_cipher_rand_for_tests((uint8_t *)&r, sizeof(r));
197
768
    return r;
198
768
}
199
200
780
#define SELF_TEST_BUF_OCTETS 128
201
774
#define NUM_RAND_TESTS 128
202
774
#define MAX_KEY_LEN 64
203
/*
204
 * srtp_cipher_type_test(ct, test_data) tests a cipher of type ct against
205
 * test cases provided in a list test_data of values of key, salt, iv,
206
 * plaintext, and ciphertext that is known to be good
207
 */
208
srtp_err_status_t srtp_cipher_type_test(
209
    const srtp_cipher_type_t *ct,
210
    const srtp_cipher_test_case_t *test_data)
211
6
{
212
6
    const srtp_cipher_test_case_t *test_case = test_data;
213
6
    srtp_cipher_t *c;
214
6
    srtp_err_status_t status;
215
6
    uint8_t buffer[SELF_TEST_BUF_OCTETS];
216
6
    uint8_t buffer2[SELF_TEST_BUF_OCTETS];
217
6
    size_t len;
218
6
    size_t case_num = 0;
219
220
6
    debug_print(srtp_mod_cipher, "running self-test for cipher %s",
221
6
                ct->description);
222
223
    /*
224
     * check to make sure that we have at least one test case, and
225
     * return an error if we don't - we need to be paranoid here
226
     */
227
6
    if (test_case == NULL) {
228
0
        return srtp_err_status_cant_check;
229
0
    }
230
231
    /*
232
     * loop over all test cases, perform known-answer tests of both the
233
     * encryption and decryption functions
234
     */
235
12
    while (test_case != NULL) {
236
        /* allocate cipher */
237
6
        status = srtp_cipher_type_alloc(ct, &c, test_case->key_length_octets,
238
6
                                        test_case->tag_length_octets);
239
6
        if (status) {
240
0
            return status;
241
0
        }
242
243
        /*
244
         * test the encrypt function
245
         */
246
6
        debug_print0(srtp_mod_cipher, "testing encryption");
247
248
        /* initialize cipher */
249
6
        status = srtp_cipher_init(c, test_case->key);
250
6
        if (status) {
251
0
            srtp_cipher_dealloc(c);
252
0
            return status;
253
0
        }
254
255
        /* copy plaintext into test buffer */
256
6
        if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) {
257
0
            srtp_cipher_dealloc(c);
258
0
            return srtp_err_status_bad_param;
259
0
        }
260
134
        for (size_t k = 0; k < test_case->plaintext_length_octets; k++) {
261
128
            buffer[k] = test_case->plaintext[k];
262
128
        }
263
264
6
        debug_print(srtp_mod_cipher, "plaintext:    %s",
265
6
                    srtp_octet_string_hex_string(
266
6
                        buffer, test_case->plaintext_length_octets));
267
268
        /* set the initialization vector */
269
6
        status = srtp_cipher_set_iv(c, test_case->idx, srtp_direction_encrypt);
270
6
        if (status) {
271
0
            srtp_cipher_dealloc(c);
272
0
            return status;
273
0
        }
274
275
6
        if (c->algorithm == SRTP_AES_GCM_128 ||
276
6
            c->algorithm == SRTP_AES_GCM_256) {
277
0
            debug_print(srtp_mod_cipher, "IV:    %s",
278
0
                        srtp_octet_string_hex_string(test_case->idx, 12));
279
280
            /*
281
             * Set the AAD
282
             */
283
0
            status = srtp_cipher_set_aad(c, test_case->aad,
284
0
                                         test_case->aad_length_octets);
285
0
            if (status) {
286
0
                srtp_cipher_dealloc(c);
287
0
                return status;
288
0
            }
289
0
            debug_print(srtp_mod_cipher, "AAD:    %s",
290
0
                        srtp_octet_string_hex_string(
291
0
                            test_case->aad, test_case->aad_length_octets));
292
0
        }
293
294
        /* encrypt */
295
6
        len = sizeof(buffer);
296
6
        status = srtp_cipher_encrypt(
297
6
            c, buffer, test_case->plaintext_length_octets, buffer, &len);
298
6
        if (status) {
299
0
            srtp_cipher_dealloc(c);
300
0
            return status;
301
0
        }
302
303
6
        debug_print(srtp_mod_cipher, "ciphertext:   %s",
304
6
                    srtp_octet_string_hex_string(
305
6
                        buffer, test_case->ciphertext_length_octets));
306
307
        /* compare the resulting ciphertext with that in the test case */
308
6
        if (len != test_case->ciphertext_length_octets) {
309
0
            srtp_cipher_dealloc(c);
310
0
            return srtp_err_status_algo_fail;
311
0
        }
312
6
        status = srtp_err_status_ok;
313
134
        for (size_t k = 0; k < test_case->ciphertext_length_octets; k++) {
314
128
            if (buffer[k] != test_case->ciphertext[k]) {
315
0
                status = srtp_err_status_algo_fail;
316
0
                debug_print(srtp_mod_cipher, "test case %zu failed", case_num);
317
0
                debug_print(srtp_mod_cipher, "(failure at byte %zu)", k);
318
0
                break;
319
0
            }
320
128
        }
321
6
        if (status) {
322
0
            debug_print(srtp_mod_cipher, "c computed: %s",
323
0
                        srtp_octet_string_hex_string(
324
0
                            buffer, 2 * test_case->plaintext_length_octets));
325
0
            debug_print(srtp_mod_cipher, "c expected: %s",
326
0
                        srtp_octet_string_hex_string(
327
0
                            test_case->ciphertext,
328
0
                            2 * test_case->plaintext_length_octets));
329
330
0
            srtp_cipher_dealloc(c);
331
0
            return srtp_err_status_algo_fail;
332
0
        }
333
334
        /*
335
         * test the decrypt function on a fresh cipher
336
         */
337
6
        status = srtp_cipher_dealloc(c);
338
6
        if (status) {
339
0
            return status;
340
0
        }
341
6
        status = srtp_cipher_type_alloc(ct, &c, test_case->key_length_octets,
342
6
                                        test_case->tag_length_octets);
343
6
        if (status) {
344
0
            return status;
345
0
        }
346
347
6
        debug_print0(srtp_mod_cipher, "testing decryption");
348
349
6
        status = srtp_cipher_init(c, test_case->key);
350
6
        if (status) {
351
0
            srtp_cipher_dealloc(c);
352
0
            return status;
353
0
        }
354
355
        /* copy ciphertext into test buffer */
356
6
        if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) {
357
0
            srtp_cipher_dealloc(c);
358
0
            return srtp_err_status_bad_param;
359
0
        }
360
134
        for (size_t k = 0; k < test_case->ciphertext_length_octets; k++) {
361
128
            buffer[k] = test_case->ciphertext[k];
362
128
        }
363
364
6
        debug_print(srtp_mod_cipher, "ciphertext:    %s",
365
6
                    srtp_octet_string_hex_string(
366
6
                        buffer, test_case->plaintext_length_octets));
367
368
        /* set the initialization vector */
369
6
        status = srtp_cipher_set_iv(c, test_case->idx, srtp_direction_decrypt);
370
6
        if (status) {
371
0
            srtp_cipher_dealloc(c);
372
0
            return status;
373
0
        }
374
375
6
        if (c->algorithm == SRTP_AES_GCM_128 ||
376
6
            c->algorithm == SRTP_AES_GCM_256) {
377
            /*
378
             * Set the AAD
379
             */
380
0
            status = srtp_cipher_set_aad(c, test_case->aad,
381
0
                                         test_case->aad_length_octets);
382
0
            if (status) {
383
0
                srtp_cipher_dealloc(c);
384
0
                return status;
385
0
            }
386
0
            debug_print(srtp_mod_cipher, "AAD:    %s",
387
0
                        srtp_octet_string_hex_string(
388
0
                            test_case->aad, test_case->aad_length_octets));
389
0
        }
390
391
        /* decrypt */
392
6
        len = sizeof(buffer);
393
6
        status = srtp_cipher_decrypt(
394
6
            c, buffer, test_case->ciphertext_length_octets, buffer, &len);
395
6
        if (status) {
396
0
            srtp_cipher_dealloc(c);
397
0
            return status;
398
0
        }
399
400
6
        debug_print(srtp_mod_cipher, "plaintext:     %s",
401
6
                    srtp_octet_string_hex_string(
402
6
                        buffer, test_case->plaintext_length_octets));
403
404
        /* compare the resulting plaintext with that in the test case */
405
6
        if (len != test_case->plaintext_length_octets) {
406
0
            srtp_cipher_dealloc(c);
407
0
            return srtp_err_status_algo_fail;
408
0
        }
409
6
        status = srtp_err_status_ok;
410
134
        for (size_t k = 0; k < test_case->plaintext_length_octets; k++) {
411
128
            if (buffer[k] != test_case->plaintext[k]) {
412
0
                status = srtp_err_status_algo_fail;
413
0
                debug_print(srtp_mod_cipher, "test case %zu failed", case_num);
414
0
                debug_print(srtp_mod_cipher, "(failure at byte %zu)", k);
415
0
            }
416
128
        }
417
6
        if (status) {
418
0
            debug_print(srtp_mod_cipher, "p computed: %s",
419
0
                        srtp_octet_string_hex_string(
420
0
                            buffer, 2 * test_case->plaintext_length_octets));
421
0
            debug_print(srtp_mod_cipher, "p expected: %s",
422
0
                        srtp_octet_string_hex_string(
423
0
                            test_case->plaintext,
424
0
                            2 * test_case->plaintext_length_octets));
425
426
0
            srtp_cipher_dealloc(c);
427
0
            return srtp_err_status_algo_fail;
428
0
        }
429
430
        /* deallocate the cipher */
431
6
        status = srtp_cipher_dealloc(c);
432
6
        if (status) {
433
0
            return status;
434
0
        }
435
436
        /*
437
         * the cipher passed the test case, so move on to the next test
438
         * case in the list; if NULL, we'l proceed to the next test
439
         */
440
6
        test_case = test_case->next_test_case;
441
6
        ++case_num;
442
6
    }
443
444
    /* now run some random invertibility tests */
445
446
6
    test_case = test_data;
447
6
    if (test_case->key_length_octets > MAX_KEY_LEN) {
448
0
        return srtp_err_status_cant_check;
449
0
    }
450
451
774
    for (size_t j = 0; j < NUM_RAND_TESTS; j++) {
452
768
        srtp_cipher_t *enc = NULL;
453
768
        srtp_cipher_t *dec = NULL;
454
768
        size_t plaintext_len;
455
768
        size_t encrypted_len;
456
768
        size_t decrypted_len;
457
768
        uint8_t key[MAX_KEY_LEN];
458
768
        uint8_t iv[MAX_KEY_LEN];
459
460
        /* choose a length at random (leaving room for IV and padding) */
461
768
        plaintext_len =
462
768
            srtp_cipher_rand_u32_for_tests() % (SELF_TEST_BUF_OCTETS - 64);
463
768
        debug_print(srtp_mod_cipher, "random plaintext length %zu\n",
464
768
                    plaintext_len);
465
768
        srtp_cipher_rand_for_tests(buffer, plaintext_len);
466
467
768
        debug_print(srtp_mod_cipher, "plaintext:    %s",
468
768
                    srtp_octet_string_hex_string(buffer, plaintext_len));
469
470
        /* copy plaintext into second buffer */
471
24.7k
        for (size_t i = 0; i < plaintext_len; i++) {
472
23.9k
            buffer2[i] = buffer[i];
473
23.9k
        }
474
475
        /* choose a key at random */
476
768
        srtp_cipher_rand_for_tests(key, test_case->key_length_octets);
477
478
        /* chose a random initialization vector */
479
768
        srtp_cipher_rand_for_tests(iv, MAX_KEY_LEN);
480
481
768
        status = srtp_cipher_type_alloc(ct, &enc, test_case->key_length_octets,
482
768
                                        test_case->tag_length_octets);
483
768
        if (status) {
484
0
            return status;
485
0
        }
486
487
        /* initialize cipher */
488
768
        status = srtp_cipher_init(enc, key);
489
768
        if (status) {
490
0
            srtp_cipher_dealloc(enc);
491
0
            return status;
492
0
        }
493
494
        /* set initialization vector */
495
768
        status =
496
768
            srtp_cipher_set_iv(enc, test_case->idx, srtp_direction_encrypt);
497
768
        if (status) {
498
0
            srtp_cipher_dealloc(enc);
499
0
            return status;
500
0
        }
501
502
768
        if (enc->algorithm == SRTP_AES_GCM_128 ||
503
768
            enc->algorithm == SRTP_AES_GCM_256) {
504
            /*
505
             * Set the AAD
506
             */
507
0
            status = srtp_cipher_set_aad(enc, test_case->aad,
508
0
                                         test_case->aad_length_octets);
509
0
            if (status) {
510
0
                srtp_cipher_dealloc(enc);
511
0
                return status;
512
0
            }
513
0
            debug_print(srtp_mod_cipher, "AAD:    %s",
514
0
                        srtp_octet_string_hex_string(
515
0
                            test_case->aad, test_case->aad_length_octets));
516
0
        }
517
518
        /* encrypt buffer with cipher */
519
768
        encrypted_len = sizeof(buffer);
520
768
        status = srtp_cipher_encrypt(enc, buffer, plaintext_len, buffer,
521
768
                                     &encrypted_len);
522
768
        if (status) {
523
0
            srtp_cipher_dealloc(enc);
524
0
            return status;
525
0
        }
526
527
768
        status = srtp_cipher_dealloc(enc);
528
768
        if (status) {
529
0
            return status;
530
0
        }
531
532
768
        debug_print(srtp_mod_cipher, "ciphertext:   %s",
533
768
                    srtp_octet_string_hex_string(buffer, encrypted_len));
534
535
        /*
536
         * decrypt with a second cipher initialized with the same key
537
         */
538
768
        status = srtp_cipher_type_alloc(ct, &dec, test_case->key_length_octets,
539
768
                                        test_case->tag_length_octets);
540
768
        if (status) {
541
0
            return status;
542
0
        }
543
768
        status = srtp_cipher_init(dec, key);
544
768
        if (status) {
545
0
            srtp_cipher_dealloc(dec);
546
0
            return status;
547
0
        }
548
768
        status = srtp_cipher_set_iv(dec, (uint8_t *)test_case->idx,
549
768
                                    srtp_direction_decrypt);
550
768
        if (status) {
551
0
            srtp_cipher_dealloc(dec);
552
0
            return status;
553
0
        }
554
768
        if (dec->algorithm == SRTP_AES_GCM_128 ||
555
768
            dec->algorithm == SRTP_AES_GCM_256) {
556
            /*
557
             * Set the AAD
558
             */
559
0
            status = srtp_cipher_set_aad(dec, test_case->aad,
560
0
                                         test_case->aad_length_octets);
561
0
            if (status) {
562
0
                srtp_cipher_dealloc(dec);
563
0
                return status;
564
0
            }
565
0
            debug_print(srtp_mod_cipher, "AAD:    %s",
566
0
                        srtp_octet_string_hex_string(
567
0
                            test_case->aad, test_case->aad_length_octets));
568
0
        }
569
768
        decrypted_len = sizeof(buffer);
570
768
        status = srtp_cipher_decrypt(dec, buffer, encrypted_len, buffer,
571
768
                                     &decrypted_len);
572
768
        if (status) {
573
0
            srtp_cipher_dealloc(dec);
574
0
            return status;
575
0
        }
576
577
768
        debug_print(srtp_mod_cipher, "plaintext[2]: %s",
578
768
                    srtp_octet_string_hex_string(buffer, decrypted_len));
579
580
        /* compare the resulting plaintext with the original one */
581
768
        if (decrypted_len != plaintext_len) {
582
0
            srtp_cipher_dealloc(dec);
583
0
            return srtp_err_status_algo_fail;
584
0
        }
585
768
        status = srtp_err_status_ok;
586
24.7k
        for (size_t k = 0; k < plaintext_len; k++) {
587
23.9k
            if (buffer[k] != buffer2[k]) {
588
0
                status = srtp_err_status_algo_fail;
589
0
                debug_print(srtp_mod_cipher, "random test case %zu failed",
590
0
                            case_num);
591
0
                debug_print(srtp_mod_cipher, "(failure at byte %zu)", k);
592
0
            }
593
23.9k
        }
594
768
        if (status) {
595
0
            srtp_cipher_dealloc(dec);
596
0
            return srtp_err_status_algo_fail;
597
0
        }
598
599
768
        status = srtp_cipher_dealloc(dec);
600
768
        if (status) {
601
0
            return status;
602
0
        }
603
768
    }
604
605
6
    return srtp_err_status_ok;
606
6
}
607
608
/*
609
 * srtp_cipher_type_self_test(ct) performs srtp_cipher_type_test on ct's
610
 * internal list of test data.
611
 */
612
srtp_err_status_t srtp_cipher_type_self_test(const srtp_cipher_type_t *ct)
613
6
{
614
6
    return srtp_cipher_type_test(ct, ct->test_data);
615
6
}
616
617
/*
618
 * cipher_bits_per_second(c, l, t) computes (an estimate of) the
619
 * number of bits that a cipher implementation can encrypt in a second
620
 *
621
 * c is a cipher (which MUST be allocated and initialized already), l
622
 * is the length in octets of the test data to be encrypted, and t is
623
 * the number of trials
624
 *
625
 * if an error is encountered, the value 0 is returned
626
 */
627
uint64_t srtp_cipher_bits_per_second(srtp_cipher_t *c,
628
                                     size_t octets_in_buffer,
629
                                     size_t num_trials)
630
0
{
631
0
    v128_t nonce;
632
0
    clock_t timer;
633
0
    uint8_t *enc_buf;
634
0
    size_t len = octets_in_buffer;
635
0
    size_t out_len;
636
0
    size_t tag_len = SRTP_MAX_TAG_LEN;
637
0
    uint8_t aad[4] = { 0, 0, 0, 0 };
638
0
    size_t aad_len = 4;
639
640
0
    enc_buf = (uint8_t *)srtp_crypto_alloc(octets_in_buffer + tag_len);
641
0
    if (enc_buf == NULL) {
642
0
        return 0; /* indicate bad parameters by returning null */
643
0
    }
644
    /* time repeated trials */
645
0
    v128_set_to_zero(&nonce);
646
0
    timer = clock();
647
0
    for (size_t i = 0; i < num_trials; i++, nonce.v32[3] = (uint32_t)i) {
648
        // Set IV
649
0
        if (srtp_cipher_set_iv(c, (uint8_t *)&nonce, srtp_direction_encrypt) !=
650
0
            srtp_err_status_ok) {
651
0
            srtp_crypto_free(enc_buf);
652
0
            return 0;
653
0
        }
654
655
        // Set (empty) AAD if supported by the cipher
656
0
        if (c->type->set_aad) {
657
0
            if (srtp_cipher_set_aad(c, aad, aad_len) != srtp_err_status_ok) {
658
0
                srtp_crypto_free(enc_buf);
659
0
                return 0;
660
0
            }
661
0
        }
662
663
        // Encrypt the buffer
664
0
        out_len = octets_in_buffer + tag_len;
665
0
        if (srtp_cipher_encrypt(c, enc_buf, len, enc_buf, &out_len) !=
666
0
            srtp_err_status_ok) {
667
0
            srtp_crypto_free(enc_buf);
668
0
            return 0;
669
0
        }
670
0
    }
671
0
    timer = clock() - timer;
672
673
0
    srtp_crypto_free(enc_buf);
674
675
0
    if (timer == 0) {
676
        /* Too fast! */
677
0
        return 0;
678
0
    }
679
680
0
    return (uint64_t)CLOCKS_PER_SEC * num_trials * 8 * octets_in_buffer / timer;
681
0
}