Coverage Report

Created: 2026-04-12 07:08

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
0
#define KDF_SRTP_AUTH_KEY_LEN 20
25
0
#define KDF_SRTP_SALT_KEY_LEN 14
26
0
#define KDF_SRTCP_AUTH_KEY_LEN KDF_SRTP_AUTH_KEY_LEN
27
0
#define KDF_SRTCP_SALT_KEY_LEN KDF_SRTP_SALT_KEY_LEN
28
0
#define KDF_SRTP_SALT_LEN 14
29
#define KDF_SRTP_KDR_LEN 6
30
0
#define KDF_SRTP_IDX_LEN 6
31
0
#define KDF_SRTCP_IDX_LEN 4
32
0
#define KDF_SRTP_IV_LEN 16
33
0
#define KDF_SRTP_MAX_KDR 24
34
0
#define KDF_SRTP_MAX_LABEL 7
35
0
#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
0
{
71
0
    KDF_SRTPKDF *ctx;
72
73
0
    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
0
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL)
83
0
        ctx->provctx = provctx;
84
0
    return ctx;
85
0
}
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
0
{
115
0
    KDF_SRTPKDF *ctx = (KDF_SRTPKDF *)vctx;
116
117
0
    if (ctx != NULL) {
118
0
        kdf_srtpkdf_reset(ctx);
119
0
        OPENSSL_free(ctx);
120
0
    }
121
0
}
122
123
static void kdf_srtpkdf_reset(void *vctx)
124
0
{
125
0
    KDF_SRTPKDF *ctx = (KDF_SRTPKDF *)vctx;
126
0
    void *provctx = ctx->provctx;
127
128
0
    ossl_prov_cipher_reset(&ctx->cipher);
129
0
    OPENSSL_clear_free(ctx->key, ctx->key_len);
130
0
    OPENSSL_clear_free(ctx->index, ctx->index_len);
131
0
    OPENSSL_clear_free(ctx->salt, ctx->salt_len);
132
0
    memset(ctx, 0, sizeof(*ctx));
133
0
    ctx->provctx = provctx;
134
0
}
135
136
static int srtpkdf_set_membuf(unsigned char **dst, size_t *dst_len,
137
    const OSSL_PARAM *p)
138
0
{
139
0
    OPENSSL_clear_free(*dst, *dst_len);
140
0
    *dst = NULL;
141
0
    *dst_len = 0;
142
0
    return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
143
0
}
144
145
static int is_power_of_two(uint32_t x, uint32_t *n)
146
0
{
147
    /* Check if we've been given an exact power of two */
148
0
    if (x == 0 || (x & (x - 1)) != 0) {
149
0
        *n = 0;
150
0
        return 0;
151
0
    }
152
    /* Count the number of trailing bits in the passed value */
153
0
#ifdef __GNUC__
154
0
    *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
0
    return 1;
166
0
}
167
168
static int kdf_srtpkdf_check_key(KDF_SRTPKDF *ctx)
169
0
{
170
0
    const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&ctx->cipher);
171
172
0
    if (cipher != NULL) {
173
0
        if (ctx->key == NULL) {
174
0
            ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
175
0
            return 0;
176
0
        }
177
0
        if (ctx->key_len != (size_t)EVP_CIPHER_get_key_length(cipher)) {
178
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
179
0
            return 0;
180
0
        }
181
0
    }
182
0
    return 1;
183
0
}
184
185
static int kdf_srtpkdf_derive(void *vctx, unsigned char *key, size_t keylen,
186
    const OSSL_PARAM params[])
187
0
{
188
0
    KDF_SRTPKDF *ctx = (KDF_SRTPKDF *)vctx;
189
0
    const EVP_CIPHER *cipher;
190
0
    OSSL_LIB_CTX *libctx;
191
192
0
    if (!ossl_prov_is_running() || !kdf_srtpkdf_set_ctx_params(ctx, params))
193
0
        return 0;
194
195
0
    libctx = PROV_LIBCTX_OF(ctx->provctx);
196
197
0
    cipher = ossl_prov_cipher_cipher(&ctx->cipher);
198
0
    if (cipher == NULL) {
199
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
200
0
        return 0;
201
0
    }
202
0
    if (!kdf_srtpkdf_check_key(ctx))
203
0
        return 0;
204
0
    if (ctx->salt == NULL) {
205
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
206
0
        return 0;
207
0
    }
208
0
    return SRTPKDF(libctx, cipher, ctx->key, ctx->salt,
209
0
        ctx->index, ctx->index_len, ctx->kdr, ctx->kdr_n, ctx->label,
210
0
        key, keylen);
211
0
}
212
213
static int kdf_srtpkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
214
0
{
215
0
    struct srtp_set_ctx_params_st p;
216
0
    KDF_SRTPKDF *ctx = vctx;
217
0
    OSSL_LIB_CTX *libctx;
218
219
0
    if (params == NULL)
220
0
        return 1;
221
222
0
    if (ctx == NULL || !srtp_set_ctx_params_decoder(params, &p))
223
0
        return 0;
224
225
0
    libctx = PROV_LIBCTX_OF(ctx->provctx);
226
227
0
    if (p.cipher != NULL) {
228
0
        const EVP_CIPHER *cipher = NULL;
229
230
0
        if (!ossl_prov_cipher_load(&ctx->cipher, p.cipher, p.propq, libctx))
231
0
            return 0;
232
0
        cipher = ossl_prov_cipher_cipher(&ctx->cipher);
233
0
        if (cipher == NULL)
234
0
            return 0;
235
0
        if (!EVP_CIPHER_is_a(cipher, "AES-128-CTR")
236
0
            && !EVP_CIPHER_is_a(cipher, "AES-192-CTR")
237
0
            && !EVP_CIPHER_is_a(cipher, "AES-256-CTR")) {
238
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CIPHER);
239
0
            return 0;
240
0
        }
241
0
    }
242
0
    if (p.key != NULL) {
243
0
        if (!srtpkdf_set_membuf(&ctx->key, &ctx->key_len, p.key))
244
0
            return 0;
245
0
        if (!kdf_srtpkdf_check_key(ctx))
246
0
            return 0;
247
0
    }
248
0
    if (p.salt != NULL) {
249
0
        if (!srtpkdf_set_membuf(&ctx->salt, &ctx->salt_len, p.salt))
250
0
            return 0;
251
0
        if (ctx->salt_len < KDF_SRTP_SALT_LEN) {
252
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
253
0
            return 0;
254
0
        }
255
0
    }
256
0
    if (p.kdr != NULL) {
257
0
        if (!OSSL_PARAM_get_uint32(p.kdr, &ctx->kdr))
258
0
            return 0;
259
0
        if (ctx->kdr > 0) {
260
0
            uint32_t n = 0;
261
262
0
            if (!is_power_of_two(ctx->kdr, &n)
263
0
                || n > KDF_SRTP_MAX_KDR) {
264
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KDR);
265
0
                return 0;
266
0
            }
267
0
            ctx->kdr_n = n;
268
0
        }
269
0
    }
270
271
0
    if (p.label != NULL) {
272
0
        if (!OSSL_PARAM_get_uint32(p.label, &ctx->label))
273
0
            return 0;
274
0
        if (ctx->label > KDF_SRTP_MAX_LABEL) {
275
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_LABEL);
276
0
            return 0;
277
0
        }
278
0
    }
279
0
    if (p.index != NULL) {
280
0
        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
0
    }
287
288
0
    return 1;
289
0
}
290
291
static const OSSL_PARAM *kdf_srtpkdf_settable_ctx_params(ossl_unused void *ctx,
292
    ossl_unused void *p_ctx)
293
0
{
294
0
    return srtp_set_ctx_params_list;
295
0
}
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
0
{
339
0
    static const bool strp_table[] = {
340
0
        true, /* 0 */
341
0
        true, /* 1 */
342
0
        true, /* 2 */
343
0
        false, /* 3 */
344
0
        false, /* 4 */
345
0
        false, /* 5 */
346
0
        true, /* 6 */
347
0
        true, /* 7 */
348
0
    };
349
0
    return strp_table[label];
350
0
}
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
0
{
396
0
    EVP_CIPHER_CTX *ctx = NULL;
397
0
    int outl, i, index_len = 0, o_len = 0, salt_len = 0;
398
0
    uint8_t buf[EVP_MAX_KEY_LENGTH];
399
0
    uint8_t iv[KDF_SRTP_IV_LEN];
400
0
    uint8_t local_salt[KDF_SRTP_MAX_SALT_LEN];
401
0
    uint8_t master_salt[KDF_SRTP_MAX_SALT_LEN];
402
0
    BIGNUM *bn_index = NULL, *bn_salt = NULL;
403
0
    int ret, iv_len = KDF_SRTP_IV_LEN, rv = 0;
404
405
0
    if (obuffer == NULL || keylen > INT_MAX)
406
0
        return rv;
407
    /* get label-specific lengths */
408
0
    switch (label) {
409
0
    case 0:
410
0
    case 3:
411
0
    case 6:
412
0
        o_len = EVP_CIPHER_key_length(cipher);
413
0
        break;
414
0
    case 1:
415
0
        o_len = KDF_SRTP_AUTH_KEY_LEN;
416
0
        break;
417
0
    case 4:
418
0
        o_len = KDF_SRTCP_AUTH_KEY_LEN;
419
0
        break;
420
0
    case 2:
421
0
    case 7:
422
0
        o_len = KDF_SRTP_SALT_KEY_LEN;
423
0
        break;
424
0
    case 5:
425
0
        o_len = KDF_SRTCP_SALT_KEY_LEN;
426
0
        break;
427
0
    default:
428
0
        return rv;
429
0
    }
430
0
    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
0
    salt_len = KDF_SRTP_SALT_LEN;
435
0
    memset(iv, 0, KDF_SRTP_IV_LEN);
436
0
    memset(master_salt, 0, sizeof(master_salt));
437
0
    memcpy(master_salt, msalt, salt_len);
438
439
    /* gather some bignums for some math */
440
0
    bn_index = BN_new();
441
0
    bn_salt = BN_new();
442
0
    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
0
    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
0
    if (index != NULL && idxlen > 0 && kdr > 0) {
451
0
        if ((int)idxlen < index_len) {
452
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INDEX_LENGTH);
453
0
            goto err;
454
0
        }
455
0
        if (!BN_bin2bn(index, index_len, bn_index))
456
0
            goto err;
457
458
0
        ret = BN_rshift(bn_salt, bn_index, kdr_n);
459
0
        if (!ret)
460
0
            goto err;
461
0
        iv_len = BN_bn2bin(bn_salt, iv);
462
0
        for (i = 1; i <= iv_len; i++)
463
0
            master_salt[salt_len - i] ^= iv[iv_len - i];
464
0
    }
465
466
    /* take the munged up salt from above and add the label */
467
0
    memset(local_salt, 0, KDF_SRTP_MAX_SALT_LEN);
468
0
    memcpy(local_salt, master_salt, salt_len);
469
0
    local_salt[((KDF_SRTP_SALT_LEN - 1) - index_len)] ^= label;
470
471
    /* perform the AES encryption on the master key and derived salt */
472
0
    memset(buf, 0, o_len);
473
0
    if (!(ctx = EVP_CIPHER_CTX_new())
474
0
        || (EVP_EncryptInit_ex(ctx, cipher, NULL, mkey, local_salt) <= 0)
475
0
        || (EVP_CIPHER_CTX_set_padding(ctx, 0) <= 0)
476
0
        || (EVP_EncryptUpdate(ctx, (unsigned char *)obuffer, &outl, buf, o_len) <= 0)
477
0
        || (EVP_EncryptFinal_ex(ctx, (unsigned char *)obuffer, &outl) <= 0))
478
0
        goto err;
479
480
0
    rv = 1;
481
0
err:
482
0
    EVP_CIPHER_CTX_free(ctx);
483
0
    OPENSSL_cleanse(iv, KDF_SRTP_IV_LEN);
484
0
    OPENSSL_cleanse(local_salt, KDF_SRTP_MAX_SALT_LEN);
485
0
    OPENSSL_cleanse(master_salt, KDF_SRTP_IV_LEN);
486
0
    BN_clear_free(bn_index);
487
0
    BN_clear_free(bn_salt);
488
0
    return rv;
489
0
}