Coverage Report

Created: 2026-04-09 06:50

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