Skip to content

Commit fd21462

Browse files
committed
Merge remote-tracking branch 'origin/main' into policy-api
2 parents f343b25 + 41ff079 commit fd21462

6 files changed

Lines changed: 281 additions & 4 deletions

File tree

crypto/cipher/aes_icm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ static srtp_err_status_t srtp_aes_icm_context_init(void *cv, const uint8_t *key)
217217
status =
218218
srtp_aes_expand_encryption_key(key, base_key_len, &c->expanded_key);
219219
if (status) {
220+
octet_string_set_to_zero(&c->expanded_key, sizeof(c->expanded_key));
220221
v128_set_to_zero(&c->counter);
221222
v128_set_to_zero(&c->offset);
222223
return status;
@@ -316,7 +317,7 @@ static srtp_err_status_t srtp_aes_icm_encrypt(void *cv,
316317
/* check that there's enough segment left*/
317318
size_t bytes_of_new_keystream = bytes_to_encr - c->bytes_in_buffer;
318319
size_t blocks_of_new_keystream = (bytes_of_new_keystream + 15) >> 4;
319-
if ((blocks_of_new_keystream + htons(c->counter.v16[7])) > 0xffff) {
320+
if (blocks_of_new_keystream > (size_t)0xffff - htons(c->counter.v16[7])) {
320321
return srtp_err_status_terminus;
321322
}
322323

srtp/srtp.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,10 @@ static srtp_err_status_t srtp_get_session_keys_for_packet(
19361936
size_t tag_len,
19371937
srtp_session_keys_t **session_keys)
19381938
{
1939+
if (stream->num_master_keys == 0 || stream->session_keys == NULL) {
1940+
return srtp_err_status_no_ctx;
1941+
}
1942+
19391943
if (!stream->use_mki) {
19401944
*session_keys = &stream->session_keys[0];
19411945
return srtp_err_status_ok;
@@ -1974,6 +1978,10 @@ static srtp_err_status_t srtp_get_session_keys_for_rtp_packet(
19741978
{
19751979
size_t tag_len = 0;
19761980

1981+
if (stream->num_master_keys == 0 || stream->session_keys == NULL) {
1982+
return srtp_err_status_no_ctx;
1983+
}
1984+
19771985
// Determine the authentication tag size
19781986
if (stream->session_keys[0].rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
19791987
stream->session_keys[0].rtp_cipher->algorithm == SRTP_AES_GCM_256) {
@@ -1994,6 +2002,10 @@ static srtp_err_status_t srtp_get_session_keys_for_rtcp_packet(
19942002
{
19952003
size_t tag_len = 0;
19962004

2005+
if (stream->num_master_keys == 0 || stream->session_keys == NULL) {
2006+
return srtp_err_status_no_ctx;
2007+
}
2008+
19972009
// Determine the authentication tag size
19982010
if (stream->session_keys[0].rtcp_cipher->algorithm == SRTP_AES_GCM_128 ||
19992011
stream->session_keys[0].rtcp_cipher->algorithm == SRTP_AES_GCM_256) {
@@ -2305,7 +2317,8 @@ static srtp_err_status_t srtp_unprotect_aead(srtp_ctx_t *ctx,
23052317
return srtp_err_status_cryptex_err;
23062318
}
23072319

2308-
if (enc_start > srtp_len - tag_len - stream->mki_size) {
2320+
if (tag_len + stream->mki_size > srtp_len ||
2321+
enc_start > srtp_len - tag_len - stream->mki_size) {
23092322
return srtp_err_status_parse_err;
23102323
}
23112324

@@ -2942,7 +2955,8 @@ srtp_err_status_t srtp_unprotect(srtp_t ctx,
29422955
return status;
29432956
}
29442957

2945-
if (enc_start > srtp_len - tag_len - stream->mki_size) {
2958+
if (tag_len + stream->mki_size > srtp_len ||
2959+
enc_start > srtp_len - tag_len - stream->mki_size) {
29462960
return srtp_err_status_parse_err;
29472961
}
29482962
enc_octet_len = srtp_len - enc_start - stream->mki_size - tag_len;

test/rdbx_driver.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161

6262
srtp_err_status_t test_replay_dbx(size_t num_trials, size_t ws);
6363

64+
srtp_err_status_t test_replay_dbx_boundaries(size_t ws);
65+
6466
double rdbx_check_adds_per_second(size_t num_trials, size_t ws);
6567

6668
void usage(char *prog_name)
@@ -321,6 +323,65 @@ srtp_err_status_t test_replay_dbx(size_t num_trials, size_t ws)
321323

322324
srtp_rdbx_dealloc(&rdbx);
323325

326+
status = test_replay_dbx_boundaries(ws);
327+
if (status) {
328+
return status;
329+
}
330+
331+
return srtp_err_status_ok;
332+
}
333+
334+
srtp_err_status_t test_replay_dbx_boundaries(size_t ws)
335+
{
336+
srtp_rdbx_t rdbx;
337+
ssize_t oldest_delta = -((ssize_t)ws - 1);
338+
339+
if (srtp_rdbx_init(&rdbx, ws) != srtp_err_status_ok) {
340+
printf("replay_init failed\n");
341+
return srtp_err_status_init_fail;
342+
}
343+
344+
if (srtp_rdbx_add_index(&rdbx, 0) != srtp_err_status_ok) {
345+
printf("rdbx_add_index failed at delta 0\n");
346+
return srtp_err_status_algo_fail;
347+
}
348+
if (srtp_rdbx_check(&rdbx, oldest_delta) != srtp_err_status_ok) {
349+
printf("rdbx_check failed at oldest in-window delta %zd\n",
350+
oldest_delta);
351+
return srtp_err_status_algo_fail;
352+
}
353+
if (srtp_rdbx_add_index(&rdbx, oldest_delta) != srtp_err_status_ok) {
354+
printf("rdbx_add_index failed at oldest in-window delta %zd\n",
355+
oldest_delta);
356+
return srtp_err_status_algo_fail;
357+
}
358+
if (srtp_rdbx_check(&rdbx, oldest_delta) != srtp_err_status_replay_fail) {
359+
printf("rdbx_check failed to reject oldest in-window delta %zd\n",
360+
oldest_delta);
361+
return srtp_err_status_algo_fail;
362+
}
363+
if (srtp_rdbx_check(&rdbx, -((ssize_t)ws)) != srtp_err_status_replay_old) {
364+
printf("rdbx_check failed to reject out-of-window delta %zd\n",
365+
-((ssize_t)ws));
366+
return srtp_err_status_algo_fail;
367+
}
368+
if (srtp_rdbx_add_index(&rdbx, (ssize_t)ws) != srtp_err_status_ok) {
369+
printf("rdbx_add_index failed at window-size delta %zu\n", ws);
370+
return srtp_err_status_algo_fail;
371+
}
372+
if (rdbx.index != ws) {
373+
printf("rdbx index was %llu, expected %zu\n",
374+
(unsigned long long)rdbx.index, ws);
375+
return srtp_err_status_algo_fail;
376+
}
377+
if (srtp_rdbx_check(&rdbx, -((ssize_t)ws)) != srtp_err_status_replay_old) {
378+
printf("rdbx_check failed to age out window-size delta %zd\n",
379+
-((ssize_t)ws));
380+
return srtp_err_status_algo_fail;
381+
}
382+
383+
srtp_rdbx_dealloc(&rdbx);
384+
324385
return srtp_err_status_ok;
325386
}
326387

test/replay_driver.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ size_t num_trials = 1 << 16;
6363

6464
srtp_err_status_t test_rdb_db(void);
6565

66+
srtp_err_status_t test_rdb_boundaries(void);
67+
6668
double rdb_check_adds_per_second(void);
6769

6870
int main(void)
@@ -247,6 +249,75 @@ srtp_err_status_t test_rdb_db(void)
247249
return srtp_err_status_fail;
248250
}
249251

252+
err = test_rdb_boundaries();
253+
if (err) {
254+
return err;
255+
}
256+
257+
return srtp_err_status_ok;
258+
}
259+
260+
srtp_err_status_t test_rdb_boundaries(void)
261+
{
262+
srtp_rdb_t rdb;
263+
264+
if (srtp_rdb_init(&rdb) != srtp_err_status_ok) {
265+
printf("rdb_init failed\n");
266+
return srtp_err_status_fail;
267+
}
268+
269+
if (srtp_rdb_add_index(&rdb, 0) != srtp_err_status_ok) {
270+
printf("rdb_add_index failed at index 0\n");
271+
return srtp_err_status_fail;
272+
}
273+
if (srtp_rdb_check(&rdb, 127) != srtp_err_status_ok) {
274+
printf("rdb_check failed at index 127\n");
275+
return srtp_err_status_fail;
276+
}
277+
if (srtp_rdb_add_index(&rdb, 127) != srtp_err_status_ok) {
278+
printf("rdb_add_index failed at index 127\n");
279+
return srtp_err_status_fail;
280+
}
281+
if (srtp_rdb_check(&rdb, 127) != srtp_err_status_replay_fail) {
282+
printf("rdb_check failed to reject index 127\n");
283+
return srtp_err_status_fail;
284+
}
285+
if (srtp_rdb_check(&rdb, 128) != srtp_err_status_ok) {
286+
printf("rdb_check failed at index 128\n");
287+
return srtp_err_status_fail;
288+
}
289+
if (srtp_rdb_add_index(&rdb, 128) != srtp_err_status_ok) {
290+
printf("rdb_add_index failed at index 128\n");
291+
return srtp_err_status_fail;
292+
}
293+
if (rdb.window_start != 1) {
294+
printf("rdb window_start was %u, expected 1\n", rdb.window_start);
295+
return srtp_err_status_fail;
296+
}
297+
if (srtp_rdb_check(&rdb, 0) != srtp_err_status_replay_old) {
298+
printf("rdb_check failed to age out index 0\n");
299+
return srtp_err_status_fail;
300+
}
301+
302+
if (srtp_rdb_init(&rdb) != srtp_err_status_ok) {
303+
printf("rdb_init failed\n");
304+
return srtp_err_status_fail;
305+
}
306+
307+
rdb.window_start = 0x7fffffff - 127;
308+
if (srtp_rdb_add_index(&rdb, 0x7fffffff) != srtp_err_status_ok) {
309+
printf("rdb_add_index failed at 31-bit boundary\n");
310+
return srtp_err_status_fail;
311+
}
312+
if (srtp_rdb_check(&rdb, 0x7fffffff) != srtp_err_status_replay_fail) {
313+
printf("rdb_check failed to retain 31-bit boundary packet\n");
314+
return srtp_err_status_fail;
315+
}
316+
if (srtp_rdb_check(&rdb, 0x7fffffff - 128) != srtp_err_status_replay_old) {
317+
printf("rdb_check failed to age packets before 31-bit boundary\n");
318+
return srtp_err_status_fail;
319+
}
320+
250321
return srtp_err_status_ok;
251322
}
252323

test/roc_driver.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464

6565
srtp_err_status_t roc_test(size_t num_trials);
6666

67+
srtp_err_status_t roc_boundary_test(void);
68+
6769
int main(void)
6870
{
6971
srtp_err_status_t status;
@@ -78,6 +80,11 @@ int main(void)
7880
printf("failed\n");
7981
exit(status);
8082
}
83+
status = roc_boundary_test();
84+
if (status) {
85+
printf("failed\n");
86+
exit(status);
87+
}
8188
printf("passed\n");
8289
return 0;
8390
}
@@ -173,3 +180,26 @@ srtp_err_status_t roc_test(size_t num_trials)
173180

174181
return srtp_err_status_ok;
175182
}
183+
184+
srtp_err_status_t roc_boundary_test(void)
185+
{
186+
srtp_xtd_seq_num_t local;
187+
srtp_xtd_seq_num_t est;
188+
ssize_t delta;
189+
190+
local = (((uint64_t)1) << 16) | 0x0010;
191+
delta = srtp_index_guess(&local, &est, 0x9001);
192+
if (est != 0x9001 || delta != -28687) {
193+
printf("index_guess failed low-seq boundary test\n");
194+
return srtp_err_status_algo_fail;
195+
}
196+
197+
local = (((uint64_t)1) << 16) | 0x8001;
198+
delta = srtp_index_guess(&local, &est, 0x0000);
199+
if (est != ((((uint64_t)2) << 16)) || delta != 32767) {
200+
printf("index_guess failed high-seq boundary test\n");
201+
return srtp_err_status_algo_fail;
202+
}
203+
204+
return srtp_err_status_ok;
205+
}

0 commit comments

Comments
 (0)