Skip to content

Commit b1d3529

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 b1d3529

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31783,6 +31783,8 @@ typedef struct Srtp_Kdf_Tv {
3178331783
word32 ksSz;
3178431784
} Srtp_Kdf_Tv;
3178531785

31786+
#define SRTP_KDF_LONG_KEY 5000
31787+
3178631788
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
3178731789
{
3178831790
wc_test_ret_t ret = 0;
@@ -32034,6 +32036,18 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
3203432036
unsigned char keyE[32];
3203532037
unsigned char keyA[20];
3203632038
unsigned char keyS[14];
32039+
#ifndef BENCH_EMBEDDED
32040+
WC_DECLARE_VAR(keyELong, byte, SRTP_KDF_LONG_KEY, HEAP_HINT);
32041+
WC_DECLARE_VAR(keyALong, byte, SRTP_KDF_LONG_KEY, HEAP_HINT);
32042+
WC_DECLARE_VAR(keySLong, byte, SRTP_KDF_LONG_KEY, HEAP_HINT);
32043+
#endif
32044+
32045+
#ifndef BENCH_EMBEDDED
32046+
WC_ALLOC_VAR(keyELong, byte, SRTP_KDF_LONG_KEY, HEAP_HINT);
32047+
WC_ALLOC_VAR(keyALong, byte, SRTP_KDF_LONG_KEY, HEAP_HINT);
32048+
WC_ALLOC_VAR(keySLong, byte, SRTP_KDF_LONG_KEY, HEAP_HINT);
32049+
#endif
32050+
3203732051
WOLFSSL_ENTER("srtpkdf_test");
3203832052

3203932053
for (i = 0; (ret == 0) && (i < SRTP_TV_CNT); i++) {
@@ -32284,6 +32298,30 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
3228432298
return WC_TEST_RET_ENC_NC;
3228532299
}
3228632300

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

0 commit comments

Comments
 (0)