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