Skip to content

Commit 8921a72

Browse files
Merge pull request #6888 from SparkiDev/srtp_kdf
SRTP/SRTCP KDF: add implementation
2 parents a6de9cd + 8c3e1db commit 8921a72

9 files changed

Lines changed: 951 additions & 5 deletions

File tree

configure.ac

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,6 +3547,22 @@ then
35473547
AM_CFLAGS="$AM_CFLAGS -DHAVE_X963_KDF"
35483548
fi
35493549

3550+
# SRTP-KDF
3551+
AC_ARG_ENABLE([srtp-kdf],
3552+
[AS_HELP_STRING([--enable-srtp-kdf],[Enable SRTP-KDF support (default: disabled)])],
3553+
[ ENABLED_SRTP_KDF=$enableval ],
3554+
[ ENABLED_SRTP_KDF=no ]
3555+
)
3556+
if test "$ENABLED_SRTP" = "yes"
3557+
then
3558+
ENABLED_SRTP_KDF="yes"
3559+
fi
3560+
if test "$ENABLED_SRTP_KDF" = "yes"
3561+
then
3562+
AM_CFLAGS="$AM_CFLAGS -DWC_SRTP_KDF -DHAVE_AES_ECB -DWOLFSSL_AES_DIRECT"
3563+
fi
3564+
3565+
35503566
# DSA
35513567
AC_ARG_ENABLE([dsa],
35523568
[AS_HELP_STRING([--enable-dsa],[Enable DSA (default: disabled)])],
@@ -9583,6 +9599,7 @@ echo " * wolfCrypt Only: $ENABLED_CRYPTONLY"
95839599
echo " * HKDF: $ENABLED_HKDF"
95849600
echo " * HPKE: $ENABLED_HPKE"
95859601
echo " * X9.63 KDF: $ENABLED_X963KDF"
9602+
echo " * SRTP-KDF: $ENABLED_SRTP_KDF"
95869603
echo " * PSK: $ENABLED_PSK"
95879604
echo " * Poly1305: $ENABLED_POLY1305"
95889605
echo " * LEANPSK: $ENABLED_LEANPSK"

doc/dox_comments/header_files/doxygen_groups.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
\defgroup RSA Algorithms - RSA
207207
\defgroup SHA Algorithms - SHA 128/224/256/384/512
208208
\defgroup SipHash Algorithm - SipHash
209+
\defgroup SrtpKdf Algorithm - SRTP KDF
209210
\defgroup SRP Algorithms - SRP
210211
211212
\defgroup ASN ASN.1

doc/dox_comments/header_files/doxygen_pages.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<li>\ref RSA</li>
5858
<li>\ref SHA</li>
5959
<li>\ref SipHash</li>
60+
<li>\ref SrtpKdf</li>
6061
<li>\ref SRP</li>
6162
</ul>
6263
*/
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
2+
/*!
3+
\ingroup SrtpKdf
4+
5+
\brief This function derives keys using SRTP KDF algorithm.
6+
7+
\return 0 Returned upon successful key derviation.
8+
\return BAD_FUNC_ARG Returned when key or salt is NULL
9+
\return BAD_FUNC_ARG Returned when key length is not 16, 24 or 32.
10+
\return BAD_FUNC_ARG Returned when saltSz is larger than 14.
11+
\return BAD_FUNC_ARG Returned when kdrIdx is less than -1 or larger than 24.
12+
\return MEMORY_E on dynamic memory allocation failure.
13+
14+
\param [in] key Key to use with encryption.
15+
\param [in] keySz Size of key in bytes.
16+
\param [in] salt Random non-secret value.
17+
\param [in] saltSz Size of random in bytes.
18+
\param [in] kdrIdx Key derivation rate. kdr = 0 when -1, otherwise kdr = 2^kdrIdx.
19+
\param [in] index Index value to XOR in.
20+
\param [out] key1 First key. Label value of 0x00.
21+
\param [in] key1Sz Size of first key in bytes.
22+
\param [out] key2 Second key. Label value of 0x01.
23+
\param [in] key2Sz Size of second key in bytes.
24+
\param [out] key3 Third key. Label value of 0x02.
25+
\param [in] key3Sz Size of third key in bytes.
26+
27+
28+
_Example_
29+
\code
30+
unsigned char key[16] = { ... };
31+
unsigned char salt[14] = { ... };
32+
unsigned char index[6] = { ... };
33+
unsigned char keyE[16];
34+
unsigned char keyA[20];
35+
unsigned char keyS[14];
36+
int kdrIdx = 0; // Use all of index
37+
int ret;
38+
39+
ret = wc_SRTP_KDF(key, sizeof(key), salt, sizeof(salt), kdrIdx, index,
40+
keyE, sizeof(keyE), keyA, sizeof(keyA), keyS, sizeof(keyS));
41+
if (ret != 0) {
42+
WOLFSSL_MSG("wc_SRTP_KDF failed");
43+
}
44+
\endcode
45+
46+
\sa wc_SRTCP_KDF
47+
\sa wc_SRTP_KDF_kdr_to_idx
48+
*/
49+
int wc_SRTP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
50+
int kdrIdx, const byte* index, byte* key1, word32 key1Sz, byte* key2,
51+
word32 key2Sz, byte* key3, word32 key3Sz);
52+
53+
/*!
54+
\ingroup SrtpKdf
55+
56+
\brief This function derives keys using SRTCP KDF algorithm.
57+
58+
\return 0 Returned upon successful key derviation.
59+
\return BAD_FUNC_ARG Returned when key or salt is NULL
60+
\return BAD_FUNC_ARG Returned when key length is not 16, 24 or 32.
61+
\return BAD_FUNC_ARG Returned when saltSz is larger than 14.
62+
\return BAD_FUNC_ARG Returned when kdrIdx is less than -1 or larger than 24.
63+
\return MEMORY_E on dynamic memory allocation failure.
64+
65+
\param [in] key Key to use with encryption.
66+
\param [in] keySz Size of key in bytes.
67+
\param [in] salt Random non-secret value.
68+
\param [in] saltSz Size of random in bytes.
69+
\param [in] kdrIdx Key derivation rate. kdr = 0 when -1, otherwise kdr = 2^kdrIdx.
70+
\param [in] index Index value to XOR in.
71+
\param [out] key1 First key. Label value of 0x00.
72+
\param [in] key1Sz Size of first key in bytes.
73+
\param [out] key2 Second key. Label value of 0x01.
74+
\param [in] key2Sz Size of second key in bytes.
75+
\param [out] key3 Third key. Label value of 0x02.
76+
\param [in] key3Sz Size of third key in bytes.
77+
78+
79+
_Example_
80+
\code
81+
unsigned char key[16] = { ... };
82+
unsigned char salt[14] = { ... };
83+
unsigned char index[4] = { ... };
84+
unsigned char keyE[16];
85+
unsigned char keyA[20];
86+
unsigned char keyS[14];
87+
int kdrIdx = 0; // Use all of index
88+
int ret;
89+
90+
ret = wc_SRTCP_KDF(key, sizeof(key), salt, sizeof(salt), kdrIdx, index,
91+
keyE, sizeof(keyE), keyA, sizeof(keyA), keyS, sizeof(keyS));
92+
if (ret != 0) {
93+
WOLFSSL_MSG("wc_SRTP_KDF failed");
94+
}
95+
\endcode
96+
97+
\sa wc_SRTP_KDF
98+
\sa wc_SRTP_KDF_kdr_to_idx
99+
*/
100+
int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
101+
int kdrIdx, const byte* index, byte* key1, word32 key1Sz, byte* key2,
102+
word32 key2Sz, byte* key3, word32 key3Sz);
103+
104+
/*!
105+
\ingroup SrtpKdf
106+
107+
\brief This function converts a kdr value to an index to use in SRTP/SRTCP KDF API.
108+
109+
\return Key derivation rate as an index.
110+
111+
\param [in] kdr Key derivation rate to convert.
112+
113+
_Example_
114+
\code
115+
word32 kdr = 0x00000010;
116+
int kdrIdx;
117+
int ret;
118+
119+
kdrIdx = wc_SRTP_KDF_kdr_to_idx(kdr);
120+
\endcode
121+
122+
\sa wc_SRTP_KDF
123+
\sa wc_SRTCP_KDF
124+
*/
125+
int wc_SRTP_KDF_kdr_to_idx(word32 kdr);
126+

wolfcrypt/benchmark/benchmark.c

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
#ifdef WOLFSSL_SIPHASH
128128
#include <wolfssl/wolfcrypt/siphash.h>
129129
#endif
130+
#include <wolfssl/wolfcrypt/kdf.h>
130131
#ifndef NO_PWDBASED
131132
#include <wolfssl/wolfcrypt/pwdbased.h>
132133
#endif
@@ -534,6 +535,9 @@
534535
#define BENCH_PBKDF2 0x00000100
535536
#define BENCH_SIPHASH 0x00000200
536537

538+
/* KDF algorithms */
539+
#define BENCH_SRTP_KDF 0x00000001
540+
537541
/* Asymmetric algorithms. */
538542
#define BENCH_RSA_KEYGEN 0x00000001
539543
#define BENCH_RSA 0x00000002
@@ -619,6 +623,8 @@ static word32 bench_cipher_algs = 0;
619623
static word32 bench_digest_algs = 0;
620624
/* MAC algorithms to benchmark. */
621625
static word32 bench_mac_algs = 0;
626+
/* KDF algorithms to benchmark. */
627+
static word32 bench_kdf_algs = 0;
622628
/* Asymmetric algorithms to benchmark. */
623629
static word32 bench_asym_algs = 0;
624630
/* Post-Quantum Asymmetric algorithms to benchmark. */
@@ -797,9 +803,18 @@ static const bench_alg bench_mac_opt[] = {
797803
#ifndef NO_PWDBASED
798804
{ "-pbkdf2", BENCH_PBKDF2 },
799805
#endif
806+
#endif
800807
#ifdef WOLFSSL_SIPHASH
801808
{ "-siphash", BENCH_SIPHASH },
802809
#endif
810+
{ NULL, 0 }
811+
};
812+
813+
/* All recognized KDF algorithm choosing command line options. */
814+
static const bench_alg bench_kdf_opt[] = {
815+
{ "-kdf", 0xffffffff },
816+
#ifdef WC_SRTP_KDF
817+
{ "-srtp-kdf", BENCH_SRTP_KDF },
803818
#endif
804819
{ NULL, 0 }
805820
};
@@ -1646,6 +1661,7 @@ static void benchmark_static_init(int force)
16461661
bench_cipher_algs = 0;
16471662
bench_digest_algs = 0;
16481663
bench_mac_algs = 0;
1664+
bench_kdf_algs = 0;
16491665
bench_asym_algs = 0;
16501666
bench_pq_asym_algs = 0;
16511667
bench_other_algs = 0;
@@ -2785,12 +2801,18 @@ static void* benchmarks_do(void* args)
27852801
bench_pbkdf2();
27862802
}
27872803
#endif
2788-
#ifdef WOLFSSL_SIPHASH
2789-
if (bench_all || (bench_mac_algs & BENCH_SIPHASH)) {
2790-
bench_siphash();
2791-
}
2792-
#endif
27932804
#endif /* NO_HMAC */
2805+
#ifdef WOLFSSL_SIPHASH
2806+
if (bench_all || (bench_mac_algs & BENCH_SIPHASH)) {
2807+
bench_siphash();
2808+
}
2809+
#endif
2810+
2811+
#ifdef WC_SRTP_KDF
2812+
if (bench_all || (bench_kdf_algs & BENCH_SRTP_KDF)) {
2813+
bench_srtpkdf();
2814+
}
2815+
#endif
27942816

27952817
#ifdef HAVE_SCRYPT
27962818
if (bench_all || (bench_other_algs & BENCH_SCRYPT))
@@ -6721,6 +6743,68 @@ void bench_siphash(void)
67216743
}
67226744
#endif
67236745

6746+
#ifdef WC_SRTP_KDF
6747+
void bench_srtpkdf(void)
6748+
{
6749+
double start;
6750+
int count;
6751+
int ret = 0;
6752+
byte keyE[32];
6753+
byte keyA[20];
6754+
byte keyS[14];
6755+
const byte *key = bench_key_buf;
6756+
const byte salt[14] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6757+
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e };
6758+
const byte index[6] = { 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA };
6759+
int kdrIdx = 0;
6760+
int i;
6761+
6762+
bench_stats_start(&count, &start);
6763+
do {
6764+
for (i = 0; i < numBlocks; i++) {
6765+
ret = wc_SRTP_KDF(key, AES_128_KEY_SIZE, salt, sizeof(salt),
6766+
kdrIdx, index, keyE, AES_128_KEY_SIZE, keyA, sizeof(keyA),
6767+
keyS, sizeof(keyS));
6768+
}
6769+
count += i;
6770+
} while (bench_stats_check(start));
6771+
bench_stats_asym_finish("KDF", 128, "SRTP", 0, count, start, ret);
6772+
6773+
bench_stats_start(&count, &start);
6774+
do {
6775+
for (i = 0; i < numBlocks; i++) {
6776+
ret = wc_SRTP_KDF(key, AES_256_KEY_SIZE, salt, sizeof(salt),
6777+
kdrIdx, index, keyE, AES_256_KEY_SIZE, keyA, sizeof(keyA),
6778+
keyS, sizeof(keyS));
6779+
}
6780+
count += i;
6781+
} while (bench_stats_check(start));
6782+
bench_stats_asym_finish("KDF", 256, "SRTP", 0, count, start, ret);
6783+
6784+
bench_stats_start(&count, &start);
6785+
do {
6786+
for (i = 0; i < numBlocks; i++) {
6787+
ret = wc_SRTCP_KDF(key, AES_128_KEY_SIZE, salt, sizeof(salt),
6788+
kdrIdx, index, keyE, AES_128_KEY_SIZE, keyA, sizeof(keyA),
6789+
keyS, sizeof(keyS));
6790+
}
6791+
count += i;
6792+
} while (bench_stats_check(start));
6793+
bench_stats_asym_finish("KDF", 128, "SRTCP", 0, count, start, ret);
6794+
6795+
bench_stats_start(&count, &start);
6796+
do {
6797+
for (i = 0; i < numBlocks; i++) {
6798+
ret = wc_SRTCP_KDF(key, AES_256_KEY_SIZE, salt, sizeof(salt),
6799+
kdrIdx, index, keyE, AES_256_KEY_SIZE, keyA, sizeof(keyA),
6800+
keyS, sizeof(keyS));
6801+
}
6802+
count += i;
6803+
} while (bench_stats_check(start));
6804+
bench_stats_asym_finish("KDF", 256, "SRTCP", 0, count, start, ret);
6805+
}
6806+
#endif
6807+
67246808
#ifndef NO_RSA
67256809

67266810
#if defined(WOLFSSL_KEY_GEN)
@@ -10661,6 +10745,8 @@ static void Usage(void)
1066110745
print_alg(bench_digest_opt[i].str, &line);
1066210746
for (i=0; bench_mac_opt[i].str != NULL; i++)
1066310747
print_alg(bench_mac_opt[i].str, &line);
10748+
for (i=0; bench_kdf_opt[i].str != NULL; i++)
10749+
print_alg(bench_kdf_opt[i].str, &line);
1066410750
for (i=0; bench_asym_opt[i].str != NULL; i++)
1066510751
print_alg(bench_asym_opt[i].str, &line);
1066610752
for (i=0; bench_other_opt[i].str != NULL; i++)
@@ -10895,6 +10981,14 @@ int wolfcrypt_benchmark_main(int argc, char** argv)
1089510981
optMatched = 1;
1089610982
}
1089710983
}
10984+
/* Known KDF algorithms */
10985+
for (i=0; !optMatched && bench_kdf_opt[i].str != NULL; i++) {
10986+
if (string_matches(argv[1], bench_kdf_opt[i].str)) {
10987+
bench_kdf_algs |= bench_kdf_opt[i].val;
10988+
bench_all = 0;
10989+
optMatched = 1;
10990+
}
10991+
}
1089810992
/* Known asymmetric algorithms */
1089910993
for (i=0; !optMatched && bench_asym_opt[i].str != NULL; i++) {
1090010994
if (string_matches(argv[1], bench_asym_opt[i].str)) {

wolfcrypt/benchmark/benchmark.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ void bench_hmac_sha256(int useDeviceID);
9595
void bench_hmac_sha384(int useDeviceID);
9696
void bench_hmac_sha512(int useDeviceID);
9797
void bench_siphash(void);
98+
void bench_srtpkdf(void);
9899
void bench_rsaKeyGen(int useDeviceID);
99100
void bench_rsaKeyGen_size(int useDeviceID, word32 keySz);
100101
void bench_rsa(int useDeviceID);

0 commit comments

Comments
 (0)