Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/providers/implementations/kdfs/srtpkdf.c
Line
Count
Source
1
/*
2
 * Copyright 2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdlib.h>
11
#include <stdarg.h>
12
#include <string.h>
13
#include <stdbool.h>
14
#include <openssl/evp.h>
15
#include <openssl/kdf.h>
16
#include <openssl/bn.h>
17
#include <openssl/core_names.h>
18
#include <openssl/proverr.h>
19
#include "internal/cryptlib.h"
20
#include "internal/fips.h"
21
#include "internal/numbers.h"
22
#include "crypto/evp.h"
23
#include "prov/provider_ctx.h"
24
#include "prov/providercommon.h"
25
#include "prov/implementations.h"
26
#include "prov/provider_util.h"
27
#include "providers/implementations/kdfs/srtpkdf.inc"
28
29
4
#define KDF_SRTP_AUTH_KEY_LEN 20
30
5
#define KDF_SRTP_SALT_KEY_LEN 14
31
2
#define KDF_SRTCP_AUTH_KEY_LEN KDF_SRTP_AUTH_KEY_LEN
32
3
#define KDF_SRTCP_SALT_KEY_LEN KDF_SRTP_SALT_KEY_LEN
33
274
#define KDF_SRTP_SALT_LEN 14
34
#define KDF_SRTP_KDR_LEN 6
35
46
#define KDF_SRTP_IDX_LEN 6
36
64
#define KDF_SRTCP_IDX_LEN 4
37
220
#define KDF_SRTP_IV_LEN 16
38
43
#define KDF_SRTP_MAX_KDR 24
39
57
#define KDF_SRTP_MAX_LABEL 7
40
107
#define KDF_SRTP_MAX_SALT_LEN (KDF_SRTP_SALT_LEN + 2)
41
42
/* See RFC 3711, Section 4.3.3 */
43
static OSSL_FUNC_kdf_newctx_fn kdf_srtpkdf_new;
44
static OSSL_FUNC_kdf_dupctx_fn kdf_srtpkdf_dup;
45
static OSSL_FUNC_kdf_freectx_fn kdf_srtpkdf_free;
46
static OSSL_FUNC_kdf_reset_fn kdf_srtpkdf_reset;
47
static OSSL_FUNC_kdf_derive_fn kdf_srtpkdf_derive;
48
static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_srtpkdf_settable_ctx_params;
49
static OSSL_FUNC_kdf_set_ctx_params_fn kdf_srtpkdf_set_ctx_params;
50
static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_srtpkdf_gettable_ctx_params;
51
static OSSL_FUNC_kdf_get_ctx_params_fn kdf_srtpkdf_get_ctx_params;
52
53
static int SRTPKDF(OSSL_LIB_CTX *provctx, const EVP_CIPHER *cipher,
54
    const uint8_t *mkey, const uint8_t *msalt,
55
    const uint8_t *index, size_t index_len,
56
    const uint32_t kdr, const uint32_t kdr_n,
57
    const uint32_t label, uint8_t *obuffer, const size_t keylen);
58
59
typedef struct {
60
    /* Warning: Any changes to this structure may require you to update kdf_srtpkdf_dup */
61
    void *provctx;
62
    PROV_CIPHER cipher;
63
    unsigned char *key;
64
    size_t key_len;
65
    unsigned char *salt;
66
    size_t salt_len;
67
    unsigned char *index;
68
    size_t index_len;
69
    uint32_t kdr;
70
    uint32_t kdr_n; /* 2 ** kdr_n = kdr */
71
    uint32_t label;
72
} KDF_SRTPKDF;
73
74
static void *kdf_srtpkdf_new(void *provctx)
75
74
{
76
74
    KDF_SRTPKDF *ctx;
77
78
74
    if (!ossl_prov_is_running())
79
0
        return NULL;
80
81
#ifdef FIPS_MODULE
82
    if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx),
83
            ST_ID_KDF_SRTPKDF))
84
        return NULL;
85
#endif
86
87
74
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL)
88
74
        ctx->provctx = provctx;
89
74
    return ctx;
90
74
}
91
92
static void *kdf_srtpkdf_dup(void *vsrc)
93
0
{
94
0
    const KDF_SRTPKDF *src = (const KDF_SRTPKDF *)vsrc;
95
0
    KDF_SRTPKDF *dest;
96
97
0
    dest = kdf_srtpkdf_new(src->provctx);
98
0
    if (dest != NULL) {
99
0
        if (!ossl_prov_memdup(src->key, src->key_len,
100
0
                &dest->key, &dest->key_len)
101
0
            || !ossl_prov_memdup(src->salt, src->salt_len,
102
0
                &dest->salt, &dest->salt_len)
103
0
            || !ossl_prov_memdup(src->index, src->index_len,
104
0
                &dest->index, &dest->index_len)
105
0
            || !ossl_prov_cipher_copy(&dest->cipher, &src->cipher))
106
0
            goto err;
107
0
        dest->kdr = src->kdr;
108
0
        dest->kdr_n = src->kdr_n;
109
0
        dest->label = src->label;
110
0
    }
111
0
    return dest;
112
113
0
err:
114
0
    kdf_srtpkdf_free(dest);
115
0
    return NULL;
116
0
}
117
118
static void kdf_srtpkdf_free(void *vctx)
119
74
{
120
74
    KDF_SRTPKDF *ctx = (KDF_SRTPKDF *)vctx;
121
122
74
    if (ctx != NULL) {
123
74
        kdf_srtpkdf_reset(ctx);
124
74
        OPENSSL_free(ctx);
125
74
    }
126
74
}
127
128
static void kdf_srtpkdf_reset(void *vctx)
129
74
{
130
74
    KDF_SRTPKDF *ctx = (KDF_SRTPKDF *)vctx;
131
74
    void *provctx = ctx->provctx;
132
133
74
    ossl_prov_cipher_reset(&ctx->cipher);
134
74
    OPENSSL_clear_free(ctx->key, ctx->key_len);
135
74
    OPENSSL_clear_free(ctx->index, ctx->index_len);
136
74
    OPENSSL_clear_free(ctx->salt, ctx->salt_len);
137
74
    memset(ctx, 0, sizeof(*ctx));
138
74
    ctx->provctx = provctx;
139
74
}
140
141
static int srtpkdf_set_membuf(unsigned char **dst, size_t *dst_len,
142
    const OSSL_PARAM *p)
143
180
{
144
180
    OPENSSL_clear_free(*dst, *dst_len);
145
180
    *dst = NULL;
146
180
    *dst_len = 0;
147
180
    return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
148
180
}
149
150
static int is_power_of_two(uint32_t x, uint32_t *n)
151
45
{
152
    /* Check if we've been given an exact power of two */
153
45
    if (x == 0 || (x & (x - 1)) != 0) {
154
2
        *n = 0;
155
2
        return 0;
156
2
    }
157
    /* Count the number of trailing bits in the passed value */
158
43
#ifdef __GNUC__
159
43
    *n = __builtin_ctz(x);
160
#else
161
    {
162
        uint32_t count = 0;
163
        while ((x & 1) == 0) {
164
            count++;
165
            x >>= 1;
166
        }
167
        *n = count;
168
    }
169
#endif
170
43
    return 1;
171
45
}
172
173
static int kdf_srtpkdf_check_key(KDF_SRTPKDF *ctx)
174
120
{
175
120
    const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&ctx->cipher);
176
177
120
    if (cipher != NULL) {
178
120
        if (ctx->key == NULL) {
179
0
            ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
180
0
            return 0;
181
0
        }
182
120
        if (ctx->key_len != (size_t)EVP_CIPHER_get_key_length(cipher)) {
183
5
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
184
5
            return 0;
185
5
        }
186
120
    }
187
115
    return 1;
188
120
}
189
190
static int kdf_srtpkdf_derive(void *vctx, unsigned char *key, size_t keylen,
191
    const OSSL_PARAM params[])
192
55
{
193
55
    KDF_SRTPKDF *ctx = (KDF_SRTPKDF *)vctx;
194
55
    const EVP_CIPHER *cipher;
195
55
    OSSL_LIB_CTX *libctx;
196
197
55
    if (!ossl_prov_is_running() || !kdf_srtpkdf_set_ctx_params(ctx, params))
198
0
        return 0;
199
200
55
    libctx = PROV_LIBCTX_OF(ctx->provctx);
201
202
55
    cipher = ossl_prov_cipher_cipher(&ctx->cipher);
203
55
    if (cipher == NULL) {
204
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
205
0
        return 0;
206
0
    }
207
55
    if (!kdf_srtpkdf_check_key(ctx))
208
0
        return 0;
209
55
    if (ctx->salt == NULL) {
210
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
211
0
        return 0;
212
0
    }
213
55
    return SRTPKDF(libctx, cipher, ctx->key, ctx->salt,
214
55
        ctx->index, ctx->index_len, ctx->kdr, ctx->kdr_n, ctx->label,
215
55
        key, keylen);
216
55
}
217
218
static int kdf_srtpkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
219
129
{
220
129
    struct srtp_set_ctx_params_st p;
221
129
    KDF_SRTPKDF *ctx = vctx;
222
129
    OSSL_LIB_CTX *libctx;
223
224
129
    if (params == NULL)
225
55
        return 1;
226
227
74
    if (ctx == NULL || !srtp_set_ctx_params_decoder(params, &p))
228
0
        return 0;
229
230
74
    libctx = PROV_LIBCTX_OF(ctx->provctx);
231
232
74
    if (p.cipher != NULL) {
233
74
        const EVP_CIPHER *cipher = NULL;
234
235
74
        if (!ossl_prov_cipher_load(&ctx->cipher, p.cipher, p.propq, libctx))
236
4
            return 0;
237
70
        cipher = ossl_prov_cipher_cipher(&ctx->cipher);
238
70
        if (cipher == NULL)
239
0
            return 0;
240
70
        if (!EVP_CIPHER_is_a(cipher, "AES-128-CTR")
241
10
            && !EVP_CIPHER_is_a(cipher, "AES-192-CTR")
242
8
            && !EVP_CIPHER_is_a(cipher, "AES-256-CTR")) {
243
5
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CIPHER);
244
5
            return 0;
245
5
        }
246
70
    }
247
65
    if (p.key != NULL) {
248
65
        if (!srtpkdf_set_membuf(&ctx->key, &ctx->key_len, p.key))
249
0
            return 0;
250
65
        if (!kdf_srtpkdf_check_key(ctx))
251
5
            return 0;
252
65
    }
253
60
    if (p.salt != NULL) {
254
60
        if (!srtpkdf_set_membuf(&ctx->salt, &ctx->salt_len, p.salt))
255
0
            return 0;
256
60
        if (ctx->salt_len < KDF_SRTP_SALT_LEN) {
257
1
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
258
1
            return 0;
259
1
        }
260
60
    }
261
59
    if (p.kdr != NULL) {
262
59
        if (!OSSL_PARAM_get_uint32(p.kdr, &ctx->kdr))
263
0
            return 0;
264
59
        if (ctx->kdr > 0) {
265
45
            uint32_t n = 0;
266
267
45
            if (!is_power_of_two(ctx->kdr, &n)
268
43
                || n > KDF_SRTP_MAX_KDR) {
269
2
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KDR);
270
2
                return 0;
271
2
            }
272
43
            ctx->kdr_n = n;
273
43
        }
274
59
    }
275
276
57
    if (p.label != NULL) {
277
57
        if (!OSSL_PARAM_get_uint32(p.label, &ctx->label))
278
0
            return 0;
279
57
        if (ctx->label > KDF_SRTP_MAX_LABEL) {
280
2
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_LABEL);
281
2
            return 0;
282
2
        }
283
57
    }
284
55
    if (p.index != NULL) {
285
55
        if (!srtpkdf_set_membuf(&ctx->index, &ctx->index_len, p.index))
286
0
            return 0;
287
        /*
288
         * Defer checking the index until the derive() since it is dependant
289
         * on values of kdr and label.
290
         */
291
55
    }
292
293
55
    return 1;
294
55
}
295
296
static const OSSL_PARAM *kdf_srtpkdf_settable_ctx_params(ossl_unused void *ctx,
297
    ossl_unused void *p_ctx)
298
74
{
299
74
    return srtp_set_ctx_params_list;
300
74
}
301
302
static int kdf_srtpkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
303
0
{
304
0
    struct srtp_get_ctx_params_st p;
305
0
    KDF_SRTPKDF *ctx = vctx;
306
307
0
    if (ctx == NULL || !srtp_get_ctx_params_decoder(params, &p))
308
0
        return 0;
309
310
0
    if (p.size != NULL) {
311
0
        size_t sz = EVP_CIPHER_key_length(ossl_prov_cipher_cipher(&ctx->cipher));
312
313
0
        if (!OSSL_PARAM_set_size_t(p.size, sz))
314
0
            return 0;
315
0
    }
316
0
    return 1;
317
0
}
318
319
static const OSSL_PARAM *kdf_srtpkdf_gettable_ctx_params(ossl_unused void *ctx,
320
    ossl_unused void *p_ctx)
321
0
{
322
0
    return srtp_get_ctx_params_list;
323
0
}
324
325
const OSSL_DISPATCH ossl_kdf_srtpkdf_functions[] = {
326
    { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_srtpkdf_new },
327
    { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_srtpkdf_dup },
328
    { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_srtpkdf_free },
329
    { OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_srtpkdf_reset },
330
    { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_srtpkdf_derive },
331
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
332
        (void (*)(void))kdf_srtpkdf_settable_ctx_params },
333
    { OSSL_FUNC_KDF_SET_CTX_PARAMS,
334
        (void (*)(void))kdf_srtpkdf_set_ctx_params },
335
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
336
        (void (*)(void))kdf_srtpkdf_gettable_ctx_params },
337
    { OSSL_FUNC_KDF_GET_CTX_PARAMS,
338
        (void (*)(void))kdf_srtpkdf_get_ctx_params },
339
    { 0, NULL }
340
};
341
342
static bool is_srtp(uint32_t label)
343
55
{
344
55
    static const bool strp_table[] = {
345
55
        true, /* 0 */
346
55
        true, /* 1 */
347
55
        true, /* 2 */
348
55
        false, /* 3 */
349
55
        false, /* 4 */
350
55
        false, /* 5 */
351
55
        true, /* 6 */
352
55
        true, /* 7 */
353
55
    };
354
55
    return strp_table[label];
355
55
}
356
357
/*
358
 * SRTPKDF - In compliance with SP800-135 and RFC3711, calculate
359
 *           various keys defined by label using a master key,
360
 *           master salt, kdr(if non-zero) and index.
361
 *
362
 * Denote the cryptographic key (encryption key, cipher salt or
363
 * authentication key(HMAC key), etc) to be derived as K. The
364
 * length of K is denoted by L. Below is a description of the KDF.
365
 *
366
 * master_salt: a random non-salt value.
367
 * kdr: the key derivation rate. kdr is a number from the set
368
 *   factor of 2.
369
 * index: a 48-bit value in RTP or a 32-bit value in RTCP.
370
 *   See Sections 3.2.1 and 4.3.2 of RFC 3711 for details.
371
 * A function, DIV, is defined as followed:
372
 *   a and x are non-negative integers.
373
 *   a DIV x =  a | x (a DIV x) is represented as a bit string whose
374
 *   length (in bits) is the same as a.
375
 * label: an 8-bit value represented by two hexadecimal numbers from
376
 *   the set of {0x00,0x01, 0x02, 0x03, 0x04, 0x05}.
377
 *   https://www.ietf.org/archive/id/draft-ietf-avtcore-srtp-encrypted-header-ext-01.html
378
 *   The values 06 and 07 are used.
379
 * key_id = label || (index DIV kdr)
380
 *
381
 * Input:
382
 *   cipher - AES cipher
383
 *   mkey - pointer to master key
384
 *   msalt - pointer to master salt
385
 *   index - pointer to index
386
 *   idxlen - size of the index buffer
387
 *   kdr - key derivation rate
388
 *   kdr_n - power of kdr (2**kdr_n = kdr)
389
 *   label - 8-bit label
390
 *   keylen - size of obuffer
391
 * Output:
392
 *   obuffer - filled with derived key
393
 *   return - 1 on pass, 0 fail
394
 */
395
int SRTPKDF(OSSL_LIB_CTX *provctx, const EVP_CIPHER *cipher,
396
    const uint8_t *mkey, const uint8_t *msalt,
397
    const uint8_t *index, size_t idxlen,
398
    const uint32_t kdr, const uint32_t kdr_n,
399
    const uint32_t label, uint8_t *obuffer, const size_t keylen)
400
55
{
401
55
    EVP_CIPHER_CTX *ctx = NULL;
402
55
    int outl, i, index_len = 0, o_len = 0, salt_len = 0;
403
55
    uint8_t buf[EVP_MAX_KEY_LENGTH];
404
55
    uint8_t iv[KDF_SRTP_IV_LEN];
405
55
    uint8_t local_salt[KDF_SRTP_MAX_SALT_LEN];
406
55
    uint8_t master_salt[KDF_SRTP_MAX_SALT_LEN];
407
55
    BIGNUM *bn_index = NULL, *bn_salt = NULL;
408
55
    int ret, iv_len = KDF_SRTP_IV_LEN, rv = 0;
409
410
55
    if (obuffer == NULL || keylen > INT_MAX)
411
0
        return rv;
412
    /* get label-specific lengths */
413
55
    switch (label) {
414
39
    case 0:
415
43
    case 3:
416
46
    case 6:
417
46
        o_len = EVP_CIPHER_key_length(cipher);
418
46
        break;
419
2
    case 1:
420
2
        o_len = KDF_SRTP_AUTH_KEY_LEN;
421
2
        break;
422
2
    case 4:
423
2
        o_len = KDF_SRTCP_AUTH_KEY_LEN;
424
2
        break;
425
1
    case 2:
426
2
    case 7:
427
2
        o_len = KDF_SRTP_SALT_KEY_LEN;
428
2
        break;
429
3
    case 5:
430
3
        o_len = KDF_SRTCP_SALT_KEY_LEN;
431
3
        break;
432
0
    default:
433
0
        return rv;
434
55
    }
435
55
    if (o_len > (int)keylen)
436
0
        return rv;
437
438
    /* set up a couple of work areas for the final logic on the salt */
439
55
    salt_len = KDF_SRTP_SALT_LEN;
440
55
    memset(iv, 0, KDF_SRTP_IV_LEN);
441
55
    memset(master_salt, 0, sizeof(master_salt));
442
55
    memcpy(master_salt, msalt, salt_len);
443
444
    /* gather some bignums for some math */
445
55
    bn_index = BN_new();
446
55
    bn_salt = BN_new();
447
55
    if ((bn_index == NULL) || (bn_salt == NULL)) {
448
0
        BN_free(bn_index);
449
0
        BN_free(bn_salt);
450
0
        return rv;
451
0
    }
452
453
55
    index_len = is_srtp(label) ? KDF_SRTP_IDX_LEN : KDF_SRTCP_IDX_LEN;
454
    /* if index is NULL or kdr=0, then index and kdr are not in play */
455
55
    if (index != NULL && idxlen > 0 && kdr > 0) {
456
42
        if ((int)idxlen < index_len) {
457
3
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INDEX_LENGTH);
458
3
            goto err;
459
3
        }
460
39
        if (!BN_bin2bn(index, index_len, bn_index))
461
0
            goto err;
462
463
39
        ret = BN_rshift(bn_salt, bn_index, kdr_n);
464
39
        if (!ret)
465
0
            goto err;
466
39
        iv_len = BN_bn2bin(bn_salt, iv);
467
199
        for (i = 1; i <= iv_len; i++)
468
160
            master_salt[salt_len - i] ^= iv[iv_len - i];
469
39
    }
470
471
    /* take the munged up salt from above and add the label */
472
52
    memset(local_salt, 0, KDF_SRTP_MAX_SALT_LEN);
473
52
    memcpy(local_salt, master_salt, salt_len);
474
52
    local_salt[((KDF_SRTP_SALT_LEN - 1) - index_len)] ^= label;
475
476
    /* perform the AES encryption on the master key and derived salt */
477
52
    memset(buf, 0, o_len);
478
52
    if (!(ctx = EVP_CIPHER_CTX_new())
479
52
        || (EVP_EncryptInit_ex(ctx, cipher, NULL, mkey, local_salt) <= 0)
480
52
        || (EVP_CIPHER_CTX_set_padding(ctx, 0) <= 0)
481
52
        || (EVP_EncryptUpdate(ctx, (unsigned char *)obuffer, &outl, buf, o_len) <= 0)
482
52
        || (EVP_EncryptFinal_ex(ctx, (unsigned char *)obuffer, &outl) <= 0))
483
0
        goto err;
484
485
52
    rv = 1;
486
55
err:
487
55
    EVP_CIPHER_CTX_free(ctx);
488
55
    OPENSSL_cleanse(iv, KDF_SRTP_IV_LEN);
489
55
    OPENSSL_cleanse(local_salt, KDF_SRTP_MAX_SALT_LEN);
490
55
    OPENSSL_cleanse(master_salt, KDF_SRTP_IV_LEN);
491
55
    BN_clear_free(bn_index);
492
55
    BN_clear_free(bn_salt);
493
55
    return rv;
494
52
}