Skip to content

Commit 87b1c04

Browse files
committed
SRTP-KDF: use two bytes of index
One byte of index creates up to 4096 bytes for a key. Increase output size to match specification.
1 parent c807903 commit 87b1c04

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

wolfcrypt/src/kdf.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,8 +896,7 @@ static void wc_srtp_kdf_first_block(const byte* salt, word32 saltSz, int kdrIdx,
896896
block[i] = 0;
897897
}
898898
XMEMCPY(block + WC_SRTP_MAX_SALT - saltSz, salt, saltSz);
899-
block[WC_SRTP_MAX_SALT] = 0;
900-
/* block[15] is counter. */
899+
/* block[14-15] are counter. */
901900

902901
/* When kdrIdx is -1, don't XOR in index. */
903902
if (kdrIdx >= 0) {
@@ -947,6 +946,7 @@ static int wc_srtp_kdf_derive_key(byte* block, int idxSz, byte label,
947946
block[WC_SRTP_MAX_SALT - idxSz - 1] ^= label;
948947
for (i = 0; (ret == 0) && (i < blocks); i++) {
949948
/* Set counter. */
949+
block[14] = (byte)(i >> 8);
950950
block[15] = (byte)i;
951951
/* Encrypt block into key buffer. */
952952
ret = wc_AesEcbEncrypt(aes, key, block, WC_AES_BLOCK_SIZE);
@@ -959,6 +959,7 @@ static int wc_srtp_kdf_derive_key(byte* block, int idxSz, byte label,
959959
if ((ret == 0) && (keySz > 0)) {
960960
byte enc[WC_AES_BLOCK_SIZE];
961961
/* Set counter. */
962+
block[14] = (byte)(i >> 8);
962963
block[15] = (byte)i;
963964
/* Encrypt block into temporary. */
964965
ret = wc_AesEcbEncrypt(aes, enc, block, WC_AES_BLOCK_SIZE);

wolfcrypt/test/test.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32034,6 +32034,18 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
3203432034
unsigned char keyE[32];
3203532035
unsigned char keyA[20];
3203632036
unsigned char keyS[14];
32037+
#ifndef BENCH_EMBEDDED
32038+
WC_DECLARE_VAR(keyELong, byte, 5000, HEAP_HINT);
32039+
WC_DECLARE_VAR(keyALong, byte, 5000, HEAP_HINT);
32040+
WC_DECLARE_VAR(keySLong, byte, 5000, HEAP_HINT);
32041+
#endif
32042+
32043+
#ifndef BENCH_EMBEDDED
32044+
WC_ALLOC_VAR(keyELong, byte, 5000, HEAP_HINT);
32045+
WC_ALLOC_VAR(keyALong, byte, 5000, HEAP_HINT);
32046+
WC_ALLOC_VAR(keySLong, byte, 5000, HEAP_HINT);
32047+
#endif
32048+
3203732049
WOLFSSL_ENTER("srtpkdf_test");
3203832050

3203932051
for (i = 0; (ret == 0) && (i < SRTP_TV_CNT); i++) {
@@ -32284,6 +32296,30 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
3228432296
return WC_TEST_RET_ENC_NC;
3228532297
}
3228632298

32299+
#ifndef BENCH_EMBEDDED
32300+
/* Check that long messages can be created. */
32301+
ret = wc_SRTP_KDF(tv[0].key, tv[0].keySz, tv[0].salt, tv[0].saltSz,
32302+
tv[0].kdfIdx, tv[0].index_c, keyELong, (word32)sizeof(keyELong),
32303+
keyALong, (word32)sizeof(keyALong), keySLong, (word32)sizeof(keySLong));
32304+
if (ret != 0)
32305+
return WC_TEST_RET_ENC_EC(ret);
32306+
32307+
/* Check that two bytes of counter are being used. */
32308+
if (XMEMCMP(keyELong, keyELong + 4096, sizeof(keyELong) - 4096) == 0) {
32309+
return WC_TEST_RET_ENC_NC;
32310+
}
32311+
if (XMEMCMP(keyELong, keyALong + 4096, sizeof(keyALong) - 4096) == 0) {
32312+
return WC_TEST_RET_ENC_NC;
32313+
}
32314+
if (XMEMCMP(keyELong, keySLong + 4096, sizeof(keySLong) - 4096) == 0) {
32315+
return WC_TEST_RET_ENC_NC;
32316+
}
32317+
32318+
WC_FREE_VAR(keyELong, HEAP_HINT);
32319+
WC_FREE_VAR(keyALong, HEAP_HINT);
32320+
WC_FREE_VAR(keySLong, HEAP_HINT);
32321+
#endif
32322+
3228732323
return 0;
3228832324
}
3228932325
#endif

0 commit comments

Comments
 (0)