Skip to content

Commit cfe645c

Browse files
authored
Merge pull request #7455 from kaleb-himes/SRTCP-48bit-indices
Srtcp 48bit indices
2 parents eeadb2a + 4b1edc7 commit cfe645c

3 files changed

Lines changed: 119 additions & 7 deletions

File tree

wolfcrypt/src/kdf.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,9 +1099,9 @@ int wc_SRTP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
10991099
* @return MEMORY_E on dynamic memory allocation failure.
11001100
* @return 0 on success.
11011101
*/
1102-
int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
1102+
int wc_SRTCP_KDF_ex(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
11031103
int kdrIdx, const byte* index, byte* key1, word32 key1Sz, byte* key2,
1104-
word32 key2Sz, byte* key3, word32 key3Sz)
1104+
word32 key2Sz, byte* key3, word32 key3Sz, int idxLenIndicator)
11051105
{
11061106
int ret = 0;
11071107
byte block[AES_BLOCK_SIZE];
@@ -1111,6 +1111,15 @@ int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
11111111
Aes aes[1];
11121112
#endif
11131113
int aes_inited = 0;
1114+
int idxLen;
1115+
1116+
if (idxLenIndicator == WC_SRTCP_32BIT_IDX) {
1117+
idxLen = WC_SRTCP_INDEX_LEN;
1118+
} else if (idxLenIndicator == WC_SRTCP_48BIT_IDX) {
1119+
idxLen = WC_SRTP_INDEX_LEN;
1120+
} else {
1121+
return BAD_FUNC_ARG; /* bad or invalid idxLenIndicator */
1122+
}
11141123

11151124
/* Validate parameters. */
11161125
if ((key == NULL) || (keySz > AES_256_KEY_SIZE) || (salt == NULL) ||
@@ -1142,23 +1151,22 @@ int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
11421151

11431152
/* Calculate first block that can be used in each derivation. */
11441153
if (ret == 0) {
1145-
wc_srtp_kdf_first_block(salt, saltSz, kdrIdx, index, WC_SRTCP_INDEX_LEN,
1146-
block);
1154+
wc_srtp_kdf_first_block(salt, saltSz, kdrIdx, index, idxLen, block);
11471155
}
11481156

11491157
/* Calculate first key if required. */
11501158
if ((ret == 0) && (key1 != NULL)) {
1151-
ret = wc_srtp_kdf_derive_key(block, WC_SRTCP_INDEX_LEN,
1159+
ret = wc_srtp_kdf_derive_key(block, idxLen,
11521160
WC_SRTCP_LABEL_ENCRYPTION, key1, key1Sz, aes);
11531161
}
11541162
/* Calculate second key if required. */
11551163
if ((ret == 0) && (key2 != NULL)) {
1156-
ret = wc_srtp_kdf_derive_key(block, WC_SRTCP_INDEX_LEN,
1164+
ret = wc_srtp_kdf_derive_key(block, idxLen,
11571165
WC_SRTCP_LABEL_MSG_AUTH, key2, key2Sz, aes);
11581166
}
11591167
/* Calculate third key if required. */
11601168
if ((ret == 0) && (key3 != NULL)) {
1161-
ret = wc_srtp_kdf_derive_key(block, WC_SRTCP_INDEX_LEN,
1169+
ret = wc_srtp_kdf_derive_key(block, idxLen,
11621170
WC_SRTCP_LABEL_SALT, key3, key3Sz, aes);
11631171
}
11641172

@@ -1170,6 +1178,15 @@ int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
11701178
return ret;
11711179
}
11721180

1181+
int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
1182+
int kdrIdx, const byte* index, byte* key1, word32 key1Sz, byte* key2,
1183+
word32 key2Sz, byte* key3, word32 key3Sz)
1184+
{
1185+
/* The default 32-bit IDX expected by many implementations */
1186+
return wc_SRTCP_KDF_ex(key, keySz, salt, saltSz, kdrIdx, index,
1187+
key1, key1Sz, key2, key2Sz, key3, key3Sz,
1188+
WC_SRTCP_32BIT_IDX);
1189+
}
11731190
/* Derive key with label using SRTP KDF algorithm.
11741191
*
11751192
* SP 800-135 (RFC 3711).

wolfcrypt/test/test.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26375,6 +26375,61 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
2637526375
0xe1, 0x29, 0x4f, 0x61, 0x30, 0x3c, 0x4d, 0x46,
2637626376
0x5f, 0x5c, 0x81, 0x3c, 0x38, 0xb6
2637726377
};
26378+
26379+
/* SRTCP w/ 48-bit idx - KDR 0 (-1) */
26380+
WOLFSSL_SMALL_STACK_STATIC const byte mk48_1[] = {
26381+
0xFF, 0xB6, 0xCB, 0x09, 0x71, 0x3F, 0x63, 0x4D,
26382+
0x7F, 0x42, 0xED, 0xA8, 0x12, 0x81, 0x50, 0xE6
26383+
};
26384+
WOLFSSL_SMALL_STACK_STATIC const byte ms48_1[] = {
26385+
0x1F, 0x04, 0x76, 0xC8, 0x7F, 0x58, 0x23, 0xEF,
26386+
0xD3, 0x57, 0xB2, 0xBD, 0xF1, 0x32
26387+
};
26388+
WOLFSSL_SMALL_STACK_STATIC const byte srtcp48idx_1[] = {
26389+
0x00, 0x00, 0x08, 0x56, 0xBC, 0x39
26390+
};
26391+
WOLFSSL_SMALL_STACK_STATIC const byte srtcpKe_48_1[] = {
26392+
0xD2, 0xC3, 0xF3, 0x49, 0x00, 0x1A, 0x18, 0x0F,
26393+
0xB6, 0x05, 0x5A, 0x5A, 0x67, 0x8E, 0xE5, 0xB2
26394+
};
26395+
WOLFSSL_SMALL_STACK_STATIC const byte srtcpKa_48_1[] = {
26396+
0x8D, 0x54, 0xBE, 0xB5, 0x7B, 0x7F, 0x7A, 0xAB,
26397+
0xF5, 0x46, 0xCE, 0x5B, 0x45, 0x69, 0x4A, 0x75,
26398+
0x81, 0x2A, 0xE2, 0xCB
26399+
};
26400+
WOLFSSL_SMALL_STACK_STATIC const byte srtcpKs_48_1[] = {
26401+
0x76, 0x3C, 0x97, 0x6A, 0x45, 0x31, 0xA7, 0x79,
26402+
0x3C, 0x28, 0x4A, 0xA6, 0x82, 0x03
26403+
};
26404+
26405+
/* SRTCP w/ 48-bit idx - KDR 19 */
26406+
WOLFSSL_SMALL_STACK_STATIC const byte mk48_2[] = {
26407+
0xBD, 0x1D, 0x71, 0x6B, 0xDA, 0x28, 0xE3, 0xFC,
26408+
0xA5, 0xA0, 0x66, 0x3F, 0x2E, 0x34, 0xA8, 0x58
26409+
};
26410+
WOLFSSL_SMALL_STACK_STATIC const byte ms48_2[] = {
26411+
0x79, 0x06, 0xE5, 0xAB, 0x5C, 0x2B, 0x1B, 0x69,
26412+
0xFA, 0xEE, 0xD2, 0x29, 0x57, 0x3C
26413+
};
26414+
WOLFSSL_SMALL_STACK_STATIC const byte srtcp48idx_2[] = {
26415+
0x00, 0x00, 0x59, 0xD0, 0xC2, 0xE8
26416+
};
26417+
WOLFSSL_SMALL_STACK_STATIC const byte srtcpKe_48_2[] = {
26418+
0xB9, 0xD7, 0xAD, 0xD8, 0x90, 0x94, 0xC2, 0x92,
26419+
0xA5, 0x04, 0x87, 0xC4, 0x8C, 0xEF, 0xE2, 0xA3
26420+
};
26421+
WOLFSSL_SMALL_STACK_STATIC const byte srtcpKa_48_2[] = {
26422+
0x07, 0xD5, 0xC4, 0xD2, 0x06, 0xFB, 0x63, 0x15,
26423+
0xC2, 0x9C, 0x7F, 0x55, 0xD1, 0x16, 0x5C, 0xB5,
26424+
0xB7, 0x44, 0x54, 0xBD
26425+
};
26426+
WOLFSSL_SMALL_STACK_STATIC const byte srtcpKs_48_2[] = {
26427+
0x0C, 0x5E, 0x53, 0xC1, 0xD0, 0x75, 0xAD, 0x65,
26428+
0xBF, 0x51, 0x74, 0x50, 0x89, 0xD7
26429+
};
26430+
int kdr_48_1 = -1;
26431+
int kdr_48_2 = 19;
26432+
2637826433
#define SRTP_TV_CNT 4
2637926434
Srtp_Kdf_Tv tv[SRTP_TV_CNT] = {
2638026435
{ key_0, (word32)sizeof(key_0), salt_0, (word32)sizeof(salt_0), -1,
@@ -26614,6 +26669,37 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srtpkdf_test(void)
2661426669
return WC_TEST_RET_ENC_NC;
2661526670
}
2661626671

26672+
/* SRTCP w/ 48-bit IDX, 128-bit key test */
26673+
if (i == 0) {
26674+
ret = wc_SRTCP_KDF_ex(mk48_1, (word32)sizeof(mk48_1),
26675+
ms48_1, (word32)sizeof(ms48_1),
26676+
kdr_48_1, srtcp48idx_1, keyE, tv[i].keSz,
26677+
keyA, tv[i].kaSz, keyS, tv[i].ksSz,
26678+
WC_SRTCP_48BIT_IDX);
26679+
if (ret != 0)
26680+
return WC_TEST_RET_ENC_EC(ret);
26681+
if (XMEMCMP(keyE, srtcpKe_48_1, tv[i].keSz) != 0)
26682+
return WC_TEST_RET_ENC_NC;
26683+
if (XMEMCMP(keyA, srtcpKa_48_1, tv[i].kaSz) != 0)
26684+
return WC_TEST_RET_ENC_NC;
26685+
if (XMEMCMP(keyS, srtcpKs_48_1, tv[i].ksSz) != 0)
26686+
return WC_TEST_RET_ENC_NC;
26687+
26688+
ret = wc_SRTCP_KDF_ex(mk48_2, (word32)sizeof(mk48_2),
26689+
ms48_2, (word32)sizeof(ms48_2),
26690+
kdr_48_2, srtcp48idx_2, keyE, tv[i].keSz,
26691+
keyA, tv[i].kaSz, keyS, tv[i].ksSz,
26692+
WC_SRTCP_48BIT_IDX);
26693+
if (ret != 0)
26694+
return WC_TEST_RET_ENC_EC(ret);
26695+
if (XMEMCMP(keyE, srtcpKe_48_2, tv[i].keSz) != 0)
26696+
return WC_TEST_RET_ENC_NC;
26697+
if (XMEMCMP(keyA, srtcpKa_48_2, tv[i].kaSz) != 0)
26698+
return WC_TEST_RET_ENC_NC;
26699+
if (XMEMCMP(keyS, srtcpKs_48_2, tv[i].ksSz) != 0)
26700+
return WC_TEST_RET_ENC_NC;
26701+
}
26702+
2661726703
return 0;
2661826704
}
2661926705
#endif

wolfssl/wolfcrypt/kdf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ WOLFSSL_API int wc_SSH_KDF(byte hashId, byte keyId,
137137
/* Length of index for SRTCP KDF. */
138138
#define WC_SRTCP_INDEX_LEN 4
139139

140+
/* Indicators */
141+
enum {
142+
WC_SRTCP_32BIT_IDX = 0,
143+
WC_SRTCP_48BIT_IDX = 1,
144+
};
145+
140146
/* Maximum length of salt that can be used with SRTP/SRTCP. */
141147
#define WC_SRTP_MAX_SALT 14
142148

@@ -146,6 +152,9 @@ WOLFSSL_API int wc_SRTP_KDF(const byte* key, word32 keySz, const byte* salt,
146152
WOLFSSL_API int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt,
147153
word32 saltSz, int kdrIdx, const byte* index, byte* key1, word32 key1Sz,
148154
byte* key2, word32 key2Sz, byte* key3, word32 key3Sz);
155+
WOLFSSL_API int wc_SRTCP_KDF_ex(const byte* key, word32 keySz, const byte* salt,
156+
word32 saltSz, int kdrIdx, const byte* index, byte* key1, word32 key1Sz,
157+
byte* key2, word32 key2Sz, byte* key3, word32 key3Sz, int idxLenIndicator);
149158
WOLFSSL_API int wc_SRTP_KDF_label(const byte* key, word32 keySz,
150159
const byte* salt, word32 saltSz, int kdrIdx, const byte* index, byte label,
151160
byte* outKey, word32 outKeySz);

0 commit comments

Comments
 (0)