kdf.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /* kdf.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/wc_port.h>
  25. #include <wolfssl/wolfcrypt/error-crypt.h>
  26. #include <wolfssl/wolfcrypt/logging.h>
  27. #ifndef NO_KDF
  28. #if defined(HAVE_FIPS) && \
  29. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  30. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  31. #define FIPS_NO_WRAPPERS
  32. #ifdef USE_WINDOWS_API
  33. #pragma code_seg(".fipsA$m")
  34. #pragma const_seg(".fipsB$m")
  35. #endif
  36. #endif
  37. #ifdef NO_INLINE
  38. #include <wolfssl/wolfcrypt/misc.h>
  39. #else
  40. #define WOLFSSL_MISC_INCLUDED
  41. #include <wolfcrypt/src/misc.c>
  42. #endif
  43. #include <wolfssl/wolfcrypt/hmac.h>
  44. #include <wolfssl/wolfcrypt/kdf.h>
  45. #ifdef WC_SRTP_KDF
  46. #include <wolfssl/wolfcrypt/aes.h>
  47. #endif
  48. #if defined(WOLFSSL_HAVE_PRF) && !defined(NO_HMAC)
  49. #ifdef WOLFSSL_SHA512
  50. #define P_HASH_MAX_SIZE WC_SHA512_DIGEST_SIZE
  51. #elif defined(WOLFSSL_SHA384)
  52. #define P_HASH_MAX_SIZE WC_SHA384_DIGEST_SIZE
  53. #else
  54. #define P_HASH_MAX_SIZE WC_SHA256_DIGEST_SIZE
  55. #endif
  56. /* Pseudo Random Function for MD5, SHA-1, SHA-256, SHA-384, or SHA-512 */
  57. int wc_PRF(byte* result, word32 resLen, const byte* secret,
  58. word32 secLen, const byte* seed, word32 seedLen, int hash,
  59. void* heap, int devId)
  60. {
  61. word32 len = P_HASH_MAX_SIZE;
  62. word32 times;
  63. word32 lastLen;
  64. word32 lastTime;
  65. int ret = 0;
  66. #ifdef WOLFSSL_SMALL_STACK
  67. byte* previous;
  68. byte* current;
  69. Hmac* hmac;
  70. #else
  71. byte previous[P_HASH_MAX_SIZE]; /* max size */
  72. byte current[P_HASH_MAX_SIZE]; /* max size */
  73. Hmac hmac[1];
  74. #endif
  75. switch (hash) {
  76. #ifndef NO_MD5
  77. case md5_mac:
  78. hash = WC_MD5;
  79. len = WC_MD5_DIGEST_SIZE;
  80. break;
  81. #endif
  82. #ifndef NO_SHA256
  83. case sha256_mac:
  84. hash = WC_SHA256;
  85. len = WC_SHA256_DIGEST_SIZE;
  86. break;
  87. #endif
  88. #ifdef WOLFSSL_SHA384
  89. case sha384_mac:
  90. hash = WC_SHA384;
  91. len = WC_SHA384_DIGEST_SIZE;
  92. break;
  93. #endif
  94. #ifdef WOLFSSL_SHA512
  95. case sha512_mac:
  96. hash = WC_SHA512;
  97. len = WC_SHA512_DIGEST_SIZE;
  98. break;
  99. #endif
  100. #ifdef WOLFSSL_SM3
  101. case sm3_mac:
  102. hash = WC_SM3;
  103. len = WC_SM3_DIGEST_SIZE;
  104. break;
  105. #endif
  106. #ifndef NO_SHA
  107. case sha_mac:
  108. hash = WC_SHA;
  109. len = WC_SHA_DIGEST_SIZE;
  110. break;
  111. #endif
  112. default:
  113. return HASH_TYPE_E;
  114. }
  115. times = resLen / len;
  116. lastLen = resLen % len;
  117. if (lastLen)
  118. times += 1;
  119. /* times == 0 if resLen == 0, but times == 0 abides clang static analyzer
  120. while resLen == 0 doesn't */
  121. if (times == 0)
  122. return BAD_FUNC_ARG;
  123. lastTime = times - 1;
  124. #ifdef WOLFSSL_SMALL_STACK
  125. previous = (byte*)XMALLOC(P_HASH_MAX_SIZE, heap, DYNAMIC_TYPE_DIGEST);
  126. current = (byte*)XMALLOC(P_HASH_MAX_SIZE, heap, DYNAMIC_TYPE_DIGEST);
  127. hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap, DYNAMIC_TYPE_HMAC);
  128. if (previous == NULL || current == NULL || hmac == NULL) {
  129. if (previous) XFREE(previous, heap, DYNAMIC_TYPE_DIGEST);
  130. if (current) XFREE(current, heap, DYNAMIC_TYPE_DIGEST);
  131. if (hmac) XFREE(hmac, heap, DYNAMIC_TYPE_HMAC);
  132. return MEMORY_E;
  133. }
  134. #endif
  135. #ifdef WOLFSSL_CHECK_MEM_ZERO
  136. XMEMSET(previous, 0xff, P_HASH_MAX_SIZE);
  137. wc_MemZero_Add("wc_PRF previous", previous, P_HASH_MAX_SIZE);
  138. wc_MemZero_Add("wc_PRF current", current, P_HASH_MAX_SIZE);
  139. wc_MemZero_Add("wc_PRF hmac", hmac, sizeof(Hmac));
  140. #endif
  141. ret = wc_HmacInit(hmac, heap, devId);
  142. if (ret == 0) {
  143. ret = wc_HmacSetKey(hmac, hash, secret, secLen);
  144. if (ret == 0)
  145. ret = wc_HmacUpdate(hmac, seed, seedLen); /* A0 = seed */
  146. if (ret == 0)
  147. ret = wc_HmacFinal(hmac, previous); /* A1 */
  148. if (ret == 0) {
  149. word32 i;
  150. word32 idx = 0;
  151. for (i = 0; i < times; i++) {
  152. ret = wc_HmacUpdate(hmac, previous, len);
  153. if (ret != 0)
  154. break;
  155. ret = wc_HmacUpdate(hmac, seed, seedLen);
  156. if (ret != 0)
  157. break;
  158. ret = wc_HmacFinal(hmac, current);
  159. if (ret != 0)
  160. break;
  161. if ((i == lastTime) && lastLen)
  162. XMEMCPY(&result[idx], current,
  163. min(lastLen, P_HASH_MAX_SIZE));
  164. else {
  165. XMEMCPY(&result[idx], current, len);
  166. idx += len;
  167. ret = wc_HmacUpdate(hmac, previous, len);
  168. if (ret != 0)
  169. break;
  170. ret = wc_HmacFinal(hmac, previous);
  171. if (ret != 0)
  172. break;
  173. }
  174. }
  175. }
  176. wc_HmacFree(hmac);
  177. }
  178. ForceZero(previous, P_HASH_MAX_SIZE);
  179. ForceZero(current, P_HASH_MAX_SIZE);
  180. ForceZero(hmac, sizeof(Hmac));
  181. #if defined(WOLFSSL_CHECK_MEM_ZERO)
  182. wc_MemZero_Check(previous, P_HASH_MAX_SIZE);
  183. wc_MemZero_Check(current, P_HASH_MAX_SIZE);
  184. wc_MemZero_Check(hmac, sizeof(Hmac));
  185. #endif
  186. #ifdef WOLFSSL_SMALL_STACK
  187. XFREE(previous, heap, DYNAMIC_TYPE_DIGEST);
  188. XFREE(current, heap, DYNAMIC_TYPE_DIGEST);
  189. XFREE(hmac, heap, DYNAMIC_TYPE_HMAC);
  190. #endif
  191. return ret;
  192. }
  193. #undef P_HASH_MAX_SIZE
  194. /* compute PRF (pseudo random function) using SHA1 and MD5 for TLSv1 */
  195. int wc_PRF_TLSv1(byte* digest, word32 digLen, const byte* secret,
  196. word32 secLen, const byte* label, word32 labLen,
  197. const byte* seed, word32 seedLen, void* heap, int devId)
  198. {
  199. int ret = 0;
  200. word32 half = (secLen + 1) / 2;
  201. const byte* md5_half;
  202. const byte* sha_half;
  203. byte* md5_result;
  204. #ifdef WOLFSSL_SMALL_STACK
  205. byte* sha_result;
  206. byte* labelSeed;
  207. #else
  208. byte sha_result[MAX_PRF_DIG]; /* digLen is real size */
  209. byte labelSeed[MAX_PRF_LABSEED];
  210. #endif
  211. if (half > MAX_PRF_HALF ||
  212. labLen + seedLen > MAX_PRF_LABSEED ||
  213. digLen > MAX_PRF_DIG)
  214. {
  215. return BUFFER_E;
  216. }
  217. #ifdef WOLFSSL_SMALL_STACK
  218. sha_result = (byte*)XMALLOC(MAX_PRF_DIG, heap, DYNAMIC_TYPE_DIGEST);
  219. labelSeed = (byte*)XMALLOC(MAX_PRF_LABSEED, heap, DYNAMIC_TYPE_DIGEST);
  220. if (sha_result == NULL || labelSeed == NULL) {
  221. XFREE(sha_result, heap, DYNAMIC_TYPE_DIGEST);
  222. XFREE(labelSeed, heap, DYNAMIC_TYPE_DIGEST);
  223. return MEMORY_E;
  224. }
  225. #endif
  226. md5_half = secret;
  227. sha_half = secret + half - secLen % 2;
  228. md5_result = digest;
  229. XMEMCPY(labelSeed, label, labLen);
  230. XMEMCPY(labelSeed + labLen, seed, seedLen);
  231. if ((ret = wc_PRF(md5_result, digLen, md5_half, half, labelSeed,
  232. labLen + seedLen, md5_mac, heap, devId)) == 0) {
  233. if ((ret = wc_PRF(sha_result, digLen, sha_half, half, labelSeed,
  234. labLen + seedLen, sha_mac, heap, devId)) == 0) {
  235. #ifdef WOLFSSL_CHECK_MEM_ZERO
  236. wc_MemZero_Add("wc_PRF_TLSv1 sha_result", sha_result, digLen);
  237. #endif
  238. /* calculate XOR for TLSv1 PRF */
  239. /* md5 result is placed directly in digest */
  240. xorbuf(digest, sha_result, digLen);
  241. ForceZero(sha_result, digLen);
  242. }
  243. }
  244. #if defined(WOLFSSL_CHECK_MEM_ZERO)
  245. wc_MemZero_Check(sha_result, MAX_PRF_DIG);
  246. #endif
  247. #ifdef WOLFSSL_SMALL_STACK
  248. XFREE(sha_result, heap, DYNAMIC_TYPE_DIGEST);
  249. XFREE(labelSeed, heap, DYNAMIC_TYPE_DIGEST);
  250. #endif
  251. return ret;
  252. }
  253. /* Wrapper for TLS 1.2 and TLSv1 cases to calculate PRF */
  254. /* In TLS 1.2 case call straight thru to wc_PRF */
  255. int wc_PRF_TLS(byte* digest, word32 digLen, const byte* secret, word32 secLen,
  256. const byte* label, word32 labLen, const byte* seed, word32 seedLen,
  257. int useAtLeastSha256, int hash_type, void* heap, int devId)
  258. {
  259. int ret = 0;
  260. if (useAtLeastSha256) {
  261. #ifdef WOLFSSL_SMALL_STACK
  262. byte* labelSeed;
  263. #else
  264. byte labelSeed[MAX_PRF_LABSEED];
  265. #endif
  266. if (labLen + seedLen > MAX_PRF_LABSEED) {
  267. return BUFFER_E;
  268. }
  269. #ifdef WOLFSSL_SMALL_STACK
  270. labelSeed = (byte*)XMALLOC(MAX_PRF_LABSEED, heap, DYNAMIC_TYPE_DIGEST);
  271. if (labelSeed == NULL) {
  272. return MEMORY_E;
  273. }
  274. #endif
  275. XMEMCPY(labelSeed, label, labLen);
  276. XMEMCPY(labelSeed + labLen, seed, seedLen);
  277. /* If a cipher suite wants an algorithm better than sha256, it
  278. * should use better. */
  279. if (hash_type < sha256_mac || hash_type == blake2b_mac) {
  280. hash_type = sha256_mac;
  281. }
  282. /* compute PRF for MD5, SHA-1, SHA-256, or SHA-384 for TLSv1.2 PRF */
  283. ret = wc_PRF(digest, digLen, secret, secLen, labelSeed,
  284. labLen + seedLen, hash_type, heap, devId);
  285. #ifdef WOLFSSL_SMALL_STACK
  286. XFREE(labelSeed, heap, DYNAMIC_TYPE_DIGEST);
  287. #endif
  288. }
  289. else {
  290. #ifndef NO_OLD_TLS
  291. /* compute TLSv1 PRF (pseudo random function using HMAC) */
  292. ret = wc_PRF_TLSv1(digest, digLen, secret, secLen, label, labLen, seed,
  293. seedLen, heap, devId);
  294. #else
  295. ret = BAD_FUNC_ARG;
  296. #endif
  297. }
  298. return ret;
  299. }
  300. #endif /* WOLFSSL_HAVE_PRF && !NO_HMAC */
  301. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  302. /* Extract data using HMAC, salt and input.
  303. * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF)
  304. *
  305. * prk The generated pseudorandom key.
  306. * salt The salt.
  307. * saltLen The length of the salt.
  308. * ikm The input keying material.
  309. * ikmLen The length of the input keying material.
  310. * digest The type of digest to use.
  311. * returns 0 on success, otherwise failure.
  312. */
  313. int wc_Tls13_HKDF_Extract(byte* prk, const byte* salt, word32 saltLen,
  314. byte* ikm, word32 ikmLen, int digest)
  315. {
  316. int ret;
  317. word32 len = 0;
  318. switch (digest) {
  319. #ifndef NO_SHA256
  320. case WC_SHA256:
  321. len = WC_SHA256_DIGEST_SIZE;
  322. break;
  323. #endif
  324. #ifdef WOLFSSL_SHA384
  325. case WC_SHA384:
  326. len = WC_SHA384_DIGEST_SIZE;
  327. break;
  328. #endif
  329. #ifdef WOLFSSL_TLS13_SHA512
  330. case WC_SHA512:
  331. len = WC_SHA512_DIGEST_SIZE;
  332. break;
  333. #endif
  334. #ifdef WOLFSSL_SM3
  335. case WC_SM3:
  336. len = WC_SM3_DIGEST_SIZE;
  337. break;
  338. #endif
  339. default:
  340. return BAD_FUNC_ARG;
  341. }
  342. /* When length is 0 then use zeroed data of digest length. */
  343. if (ikmLen == 0) {
  344. ikmLen = len;
  345. XMEMSET(ikm, 0, len);
  346. }
  347. #ifdef WOLFSSL_DEBUG_TLS
  348. WOLFSSL_MSG(" Salt");
  349. WOLFSSL_BUFFER(salt, saltLen);
  350. WOLFSSL_MSG(" IKM");
  351. WOLFSSL_BUFFER(ikm, ikmLen);
  352. #endif
  353. ret = wc_HKDF_Extract(digest, salt, saltLen, ikm, ikmLen, prk);
  354. #ifdef WOLFSSL_DEBUG_TLS
  355. WOLFSSL_MSG(" PRK");
  356. WOLFSSL_BUFFER(prk, len);
  357. #endif
  358. return ret;
  359. }
  360. /* Expand data using HMAC, salt and label and info.
  361. * TLS v1.3 defines this function.
  362. *
  363. * okm The generated pseudorandom key - output key material.
  364. * okmLen The length of generated pseudorandom key -
  365. * output key material.
  366. * prk The salt - pseudo-random key.
  367. * prkLen The length of the salt - pseudo-random key.
  368. * protocol The TLS protocol label.
  369. * protocolLen The length of the TLS protocol label.
  370. * info The information to expand.
  371. * infoLen The length of the information.
  372. * digest The type of digest to use.
  373. * returns 0 on success, otherwise failure.
  374. */
  375. int wc_Tls13_HKDF_Expand_Label(byte* okm, word32 okmLen,
  376. const byte* prk, word32 prkLen,
  377. const byte* protocol, word32 protocolLen,
  378. const byte* label, word32 labelLen,
  379. const byte* info, word32 infoLen,
  380. int digest)
  381. {
  382. int ret = 0;
  383. word32 idx = 0;
  384. #ifdef WOLFSSL_SMALL_STACK
  385. byte* data;
  386. #else
  387. byte data[MAX_TLS13_HKDF_LABEL_SZ];
  388. #endif
  389. /* okmLen (2) + protocol|label len (1) + info len(1) + protocollen +
  390. * labellen + infolen */
  391. idx = 4 + protocolLen + labelLen + infoLen;
  392. if (idx > MAX_TLS13_HKDF_LABEL_SZ) {
  393. return BUFFER_E;
  394. }
  395. #ifdef WOLFSSL_SMALL_STACK
  396. data = (byte*)XMALLOC(idx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  397. if (data == NULL) {
  398. return MEMORY_E;
  399. }
  400. #endif
  401. idx = 0;
  402. /* Output length. */
  403. data[idx++] = (byte)(okmLen >> 8);
  404. data[idx++] = (byte)okmLen;
  405. /* Length of protocol | label. */
  406. data[idx++] = (byte)(protocolLen + labelLen);
  407. /* Protocol */
  408. XMEMCPY(&data[idx], protocol, protocolLen);
  409. idx += protocolLen;
  410. /* Label */
  411. XMEMCPY(&data[idx], label, labelLen);
  412. idx += labelLen;
  413. /* Length of hash of messages */
  414. data[idx++] = (byte)infoLen;
  415. /* Hash of messages */
  416. XMEMCPY(&data[idx], info, infoLen);
  417. idx += infoLen;
  418. #ifdef WOLFSSL_CHECK_MEM_ZERO
  419. wc_MemZero_Add("wc_Tls13_HKDF_Expand_Label data", data, idx);
  420. #endif
  421. #ifdef WOLFSSL_DEBUG_TLS
  422. WOLFSSL_MSG(" PRK");
  423. WOLFSSL_BUFFER(prk, prkLen);
  424. WOLFSSL_MSG(" Info");
  425. WOLFSSL_BUFFER(data, idx);
  426. WOLFSSL_MSG_EX(" Digest %d", digest);
  427. #endif
  428. ret = wc_HKDF_Expand(digest, prk, prkLen, data, idx, okm, okmLen);
  429. #ifdef WOLFSSL_DEBUG_TLS
  430. WOLFSSL_MSG(" OKM");
  431. WOLFSSL_BUFFER(okm, okmLen);
  432. #endif
  433. ForceZero(data, idx);
  434. #ifdef WOLFSSL_CHECK_MEM_ZERO
  435. wc_MemZero_Check(data, idx);
  436. #endif
  437. #ifdef WOLFSSL_SMALL_STACK
  438. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  439. #endif
  440. return ret;
  441. }
  442. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \
  443. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  444. /* Expand data using HMAC, salt and label and info.
  445. * TLS v1.3 defines this function.
  446. *
  447. * okm The generated pseudorandom key - output key material.
  448. * okmLen The length of generated pseudorandom key -
  449. * output key material.
  450. * prk The salt - pseudo-random key.
  451. * prkLen The length of the salt - pseudo-random key.
  452. * protocol The TLS protocol label.
  453. * protocolLen The length of the TLS protocol label.
  454. * info The information to expand.
  455. * infoLen The length of the information.
  456. * digest The type of digest to use.
  457. *
  458. * This functions is very similar to wc_Tls13_HKDF_Expand_Label() but it
  459. * allocate memory if the stack space usually used isn't enough.
  460. *
  461. * returns 0 on success, otherwise failure.
  462. */
  463. int wc_Tls13_HKDF_Expand_Label_Alloc(byte* okm, word32 okmLen,
  464. const byte* prk, word32 prkLen, const byte* protocol,
  465. word32 protocolLen, const byte* label, word32 labelLen,
  466. const byte* info, word32 infoLen, int digest, void* heap)
  467. {
  468. int ret = 0;
  469. int idx = 0;
  470. int len;
  471. byte *data;
  472. (void)heap;
  473. /* okmLen (2) + protocol|label len (1) + info len(1) + protocollen +
  474. * labellen + infolen */
  475. len = 4 + protocolLen + labelLen + infoLen;
  476. data = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_TMP_BUFFER);
  477. if (data == NULL)
  478. return BUFFER_E;
  479. /* Output length. */
  480. data[idx++] = (byte)(okmLen >> 8);
  481. data[idx++] = (byte)okmLen;
  482. /* Length of protocol | label. */
  483. data[idx++] = (byte)(protocolLen + labelLen);
  484. /* Protocol */
  485. XMEMCPY(&data[idx], protocol, protocolLen);
  486. idx += protocolLen;
  487. /* Label */
  488. XMEMCPY(&data[idx], label, labelLen);
  489. idx += labelLen;
  490. /* Length of hash of messages */
  491. data[idx++] = (byte)infoLen;
  492. /* Hash of messages */
  493. XMEMCPY(&data[idx], info, infoLen);
  494. idx += infoLen;
  495. #ifdef WOLFSSL_CHECK_MEM_ZERO
  496. wc_MemZero_Add("wc_Tls13_HKDF_Expand_Label data", data, idx);
  497. #endif
  498. #ifdef WOLFSSL_DEBUG_TLS
  499. WOLFSSL_MSG(" PRK");
  500. WOLFSSL_BUFFER(prk, prkLen);
  501. WOLFSSL_MSG(" Info");
  502. WOLFSSL_BUFFER(data, idx);
  503. WOLFSSL_MSG_EX(" Digest %d", digest);
  504. #endif
  505. ret = wc_HKDF_Expand(digest, prk, prkLen, data, idx, okm, okmLen);
  506. #ifdef WOLFSSL_DEBUG_TLS
  507. WOLFSSL_MSG(" OKM");
  508. WOLFSSL_BUFFER(okm, okmLen);
  509. #endif
  510. ForceZero(data, idx);
  511. #ifdef WOLFSSL_CHECK_MEM_ZERO
  512. wc_MemZero_Check(data, len);
  513. #endif
  514. XFREE(data, heap, DYNAMIC_TYPE_TMP_BUFFER);
  515. return ret;
  516. }
  517. #endif
  518. /* defined(WOLFSSL_TICKET_NONCE_MALLOC) && (!defined(HAVE_FIPS) ||
  519. * FIPS_VERSION_GE(5,3)) */
  520. #endif /* HAVE_HKDF && !NO_HMAC */
  521. #ifdef WOLFSSL_WOLFSSH
  522. /* hash union */
  523. typedef union {
  524. #ifndef NO_MD5
  525. wc_Md5 md5;
  526. #endif
  527. #ifndef NO_SHA
  528. wc_Sha sha;
  529. #endif
  530. #ifdef WOLFSSL_SHA224
  531. wc_Sha224 sha224;
  532. #endif
  533. #ifndef NO_SHA256
  534. wc_Sha256 sha256;
  535. #endif
  536. #ifdef WOLFSSL_SHA384
  537. wc_Sha384 sha384;
  538. #endif
  539. #ifdef WOLFSSL_SHA512
  540. wc_Sha512 sha512;
  541. #endif
  542. #ifdef WOLFSSL_SHA3
  543. wc_Sha3 sha3;
  544. #endif
  545. } _hash;
  546. static
  547. int _HashInit(byte hashId, _hash* hash)
  548. {
  549. int ret = BAD_FUNC_ARG;
  550. switch (hashId) {
  551. #ifndef NO_SHA
  552. case WC_SHA:
  553. ret = wc_InitSha(&hash->sha);
  554. break;
  555. #endif /* !NO_SHA */
  556. #ifndef NO_SHA256
  557. case WC_SHA256:
  558. ret = wc_InitSha256(&hash->sha256);
  559. break;
  560. #endif /* !NO_SHA256 */
  561. #ifdef WOLFSSL_SHA384
  562. case WC_SHA384:
  563. ret = wc_InitSha384(&hash->sha384);
  564. break;
  565. #endif /* WOLFSSL_SHA384 */
  566. #ifdef WOLFSSL_SHA512
  567. case WC_SHA512:
  568. ret = wc_InitSha512(&hash->sha512);
  569. break;
  570. #endif /* WOLFSSL_SHA512 */
  571. }
  572. return ret;
  573. }
  574. static
  575. int _HashUpdate(byte hashId, _hash* hash,
  576. const byte* data, word32 dataSz)
  577. {
  578. int ret = BAD_FUNC_ARG;
  579. switch (hashId) {
  580. #ifndef NO_SHA
  581. case WC_SHA:
  582. ret = wc_ShaUpdate(&hash->sha, data, dataSz);
  583. break;
  584. #endif /* !NO_SHA */
  585. #ifndef NO_SHA256
  586. case WC_SHA256:
  587. ret = wc_Sha256Update(&hash->sha256, data, dataSz);
  588. break;
  589. #endif /* !NO_SHA256 */
  590. #ifdef WOLFSSL_SHA384
  591. case WC_SHA384:
  592. ret = wc_Sha384Update(&hash->sha384, data, dataSz);
  593. break;
  594. #endif /* WOLFSSL_SHA384 */
  595. #ifdef WOLFSSL_SHA512
  596. case WC_SHA512:
  597. ret = wc_Sha512Update(&hash->sha512, data, dataSz);
  598. break;
  599. #endif /* WOLFSSL_SHA512 */
  600. }
  601. return ret;
  602. }
  603. static
  604. int _HashFinal(byte hashId, _hash* hash, byte* digest)
  605. {
  606. int ret = BAD_FUNC_ARG;
  607. switch (hashId) {
  608. #ifndef NO_SHA
  609. case WC_SHA:
  610. ret = wc_ShaFinal(&hash->sha, digest);
  611. break;
  612. #endif /* !NO_SHA */
  613. #ifndef NO_SHA256
  614. case WC_SHA256:
  615. ret = wc_Sha256Final(&hash->sha256, digest);
  616. break;
  617. #endif /* !NO_SHA256 */
  618. #ifdef WOLFSSL_SHA384
  619. case WC_SHA384:
  620. ret = wc_Sha384Final(&hash->sha384, digest);
  621. break;
  622. #endif /* WOLFSSL_SHA384 */
  623. #ifdef WOLFSSL_SHA512
  624. case WC_SHA512:
  625. ret = wc_Sha512Final(&hash->sha512, digest);
  626. break;
  627. #endif /* WOLFSSL_SHA512 */
  628. }
  629. return ret;
  630. }
  631. static
  632. void _HashFree(byte hashId, _hash* hash)
  633. {
  634. switch (hashId) {
  635. #ifndef NO_SHA
  636. case WC_SHA:
  637. wc_ShaFree(&hash->sha);
  638. break;
  639. #endif /* !NO_SHA */
  640. #ifndef NO_SHA256
  641. case WC_SHA256:
  642. wc_Sha256Free(&hash->sha256);
  643. break;
  644. #endif /* !NO_SHA256 */
  645. #ifdef WOLFSSL_SHA384
  646. case WC_SHA384:
  647. wc_Sha384Free(&hash->sha384);
  648. break;
  649. #endif /* WOLFSSL_SHA384 */
  650. #ifdef WOLFSSL_SHA512
  651. case WC_SHA512:
  652. wc_Sha512Free(&hash->sha512);
  653. break;
  654. #endif /* WOLFSSL_SHA512 */
  655. }
  656. }
  657. #define LENGTH_SZ 4
  658. int wc_SSH_KDF(byte hashId, byte keyId, byte* key, word32 keySz,
  659. const byte* k, word32 kSz, const byte* h, word32 hSz,
  660. const byte* sessionId, word32 sessionIdSz)
  661. {
  662. word32 blocks, remainder;
  663. _hash hash;
  664. enum wc_HashType enmhashId = (enum wc_HashType)hashId;
  665. byte kPad = 0;
  666. byte pad = 0;
  667. byte kSzFlat[LENGTH_SZ];
  668. word32 digestSz;
  669. int ret;
  670. if (key == NULL || keySz == 0 ||
  671. k == NULL || kSz == 0 ||
  672. h == NULL || hSz == 0 ||
  673. sessionId == NULL || sessionIdSz == 0) {
  674. return BAD_FUNC_ARG;
  675. }
  676. ret = wc_HmacSizeByType(enmhashId);
  677. if (ret <= 0) {
  678. return BAD_FUNC_ARG;
  679. }
  680. digestSz = (word32)ret;
  681. if (k[0] & 0x80) kPad = 1;
  682. c32toa(kSz + kPad, kSzFlat);
  683. blocks = keySz / digestSz;
  684. remainder = keySz % digestSz;
  685. ret = _HashInit(enmhashId, &hash);
  686. if (ret == 0)
  687. ret = _HashUpdate(enmhashId, &hash, kSzFlat, LENGTH_SZ);
  688. if (ret == 0 && kPad)
  689. ret = _HashUpdate(enmhashId, &hash, &pad, 1);
  690. if (ret == 0)
  691. ret = _HashUpdate(enmhashId, &hash, k, kSz);
  692. if (ret == 0)
  693. ret = _HashUpdate(enmhashId, &hash, h, hSz);
  694. if (ret == 0)
  695. ret = _HashUpdate(enmhashId, &hash, &keyId, sizeof(keyId));
  696. if (ret == 0)
  697. ret = _HashUpdate(enmhashId, &hash, sessionId, sessionIdSz);
  698. if (ret == 0) {
  699. if (blocks == 0) {
  700. if (remainder > 0) {
  701. byte lastBlock[WC_MAX_DIGEST_SIZE];
  702. ret = _HashFinal(enmhashId, &hash, lastBlock);
  703. if (ret == 0)
  704. XMEMCPY(key, lastBlock, remainder);
  705. }
  706. }
  707. else {
  708. word32 runningKeySz, curBlock;
  709. runningKeySz = digestSz;
  710. ret = _HashFinal(enmhashId, &hash, key);
  711. for (curBlock = 1; curBlock < blocks; curBlock++) {
  712. ret = _HashInit(enmhashId, &hash);
  713. if (ret != 0) break;
  714. ret = _HashUpdate(enmhashId, &hash, kSzFlat, LENGTH_SZ);
  715. if (ret != 0) break;
  716. if (kPad)
  717. ret = _HashUpdate(enmhashId, &hash, &pad, 1);
  718. if (ret != 0) break;
  719. ret = _HashUpdate(enmhashId, &hash, k, kSz);
  720. if (ret != 0) break;
  721. ret = _HashUpdate(enmhashId, &hash, h, hSz);
  722. if (ret != 0) break;
  723. ret = _HashUpdate(enmhashId, &hash, key, runningKeySz);
  724. if (ret != 0) break;
  725. ret = _HashFinal(enmhashId, &hash, key + runningKeySz);
  726. if (ret != 0) break;
  727. runningKeySz += digestSz;
  728. }
  729. if (remainder > 0) {
  730. byte lastBlock[WC_MAX_DIGEST_SIZE];
  731. if (ret == 0)
  732. ret = _HashInit(enmhashId, &hash);
  733. if (ret == 0)
  734. ret = _HashUpdate(enmhashId, &hash, kSzFlat, LENGTH_SZ);
  735. if (ret == 0 && kPad)
  736. ret = _HashUpdate(enmhashId, &hash, &pad, 1);
  737. if (ret == 0)
  738. ret = _HashUpdate(enmhashId, &hash, k, kSz);
  739. if (ret == 0)
  740. ret = _HashUpdate(enmhashId, &hash, h, hSz);
  741. if (ret == 0)
  742. ret = _HashUpdate(enmhashId, &hash, key, runningKeySz);
  743. if (ret == 0)
  744. ret = _HashFinal(enmhashId, &hash, lastBlock);
  745. if (ret == 0)
  746. XMEMCPY(key + runningKeySz, lastBlock, remainder);
  747. }
  748. }
  749. }
  750. _HashFree(enmhashId, &hash);
  751. return ret;
  752. }
  753. #endif /* WOLFSSL_WOLFSSH */
  754. #ifdef WC_SRTP_KDF
  755. /* Calculate first block to encrypt.
  756. *
  757. * @param [in] salt Random value to XOR in.
  758. * @param [in] saltSz Size of random value in bytes.
  759. * @param [in] kdrIdx Key derivation rate. kdr = 0 when -1, otherwise
  760. * kdr = 2^kdrIdx.
  761. * @param [in] index Index value to XOR in.
  762. * @param [in] indexSz Size of index value in bytes.
  763. * @param [out] block First block to encrypt.
  764. */
  765. static void wc_srtp_kdf_first_block(const byte* salt, word32 saltSz, int kdrIdx,
  766. const byte* index, byte indexSz, unsigned char* block)
  767. {
  768. word32 i;
  769. /* XOR salt into zeroized buffer. */
  770. for (i = 0; i < WC_SRTP_MAX_SALT - saltSz; i++) {
  771. block[i] = 0;
  772. }
  773. XMEMCPY(block + WC_SRTP_MAX_SALT - saltSz, salt, saltSz);
  774. block[WC_SRTP_MAX_SALT] = 0;
  775. /* block[15] is counter. */
  776. /* When kdrIdx is -1, don't XOR in index. */
  777. if (kdrIdx >= 0) {
  778. /* Get the number of bits to shift index by. */
  779. word32 bits = kdrIdx & 0x7;
  780. /* Reduce index size by number of bytes to remove. */
  781. indexSz -= kdrIdx >> 3;
  782. if ((kdrIdx & 0x7) == 0) {
  783. /* Just XOR in as no bit shifting. */
  784. for (i = 0; i < indexSz; i++) {
  785. block[i + WC_SRTP_MAX_SALT - indexSz] ^= index[i];
  786. }
  787. }
  788. else {
  789. /* XOR in as bit shifted index. */
  790. block[WC_SRTP_MAX_SALT - indexSz] ^= index[0] >> bits;
  791. for (i = 1; i < indexSz; i++) {
  792. block[i + WC_SRTP_MAX_SALT - indexSz] ^=
  793. (index[i-1] << (8 - bits)) |
  794. (index[i+0] >> bits );
  795. }
  796. }
  797. }
  798. }
  799. /* Derive a key given the first block.
  800. *
  801. * @param [in, out] block First block to encrypt. Need label XORed in.
  802. * @param [in] indexSz Size of index in bytes to calculate where label is
  803. * XORed into.
  804. * @param [in] label Label byte that differs for each key.
  805. * @param [out] key Derived key.
  806. * @param [in] keySz Size of key to derive in bytes.
  807. * @param [in] aes AES object to encrypt with.
  808. * @return 0 on success.
  809. */
  810. static int wc_srtp_kdf_derive_key(byte* block, byte indexSz, byte label,
  811. byte* key, word32 keySz, Aes* aes)
  812. {
  813. int i;
  814. int ret = 0;
  815. /* Calculate the number of full blocks needed for derived key. */
  816. int blocks = keySz / AES_BLOCK_SIZE;
  817. /* XOR in label. */
  818. block[WC_SRTP_MAX_SALT - indexSz - 1] ^= label;
  819. for (i = 0; (ret == 0) && (i < blocks); i++) {
  820. /* Set counter. */
  821. block[15] = i;
  822. /* Encrypt block into key buffer. */
  823. ret = wc_AesEcbEncrypt(aes, key, block, AES_BLOCK_SIZE);
  824. /* Reposition for more derived key. */
  825. key += AES_BLOCK_SIZE;
  826. /* Reduce the count of key bytes required. */
  827. keySz -= AES_BLOCK_SIZE;
  828. }
  829. /* Do any partial blocks. */
  830. if ((ret == 0) && (keySz > 0)) {
  831. byte enc[AES_BLOCK_SIZE];
  832. /* Set counter. */
  833. block[15] = i;
  834. /* Encrypt block into temporary. */
  835. ret = wc_AesEcbEncrypt(aes, enc, block, AES_BLOCK_SIZE);
  836. if (ret == 0) {
  837. /* Copy into key required amount. */
  838. XMEMCPY(key, enc, keySz);
  839. }
  840. }
  841. /* XOR out label. */
  842. block[WC_SRTP_MAX_SALT - indexSz - 1] ^= label;
  843. return ret;
  844. }
  845. /* Derive keys using SRTP KDF algorithm.
  846. *
  847. * SP 800-135 (RFC 3711).
  848. *
  849. * @param [in] key Key to use with encryption.
  850. * @param [in] keySz Size of key in bytes.
  851. * @param [in] salt Random non-secret value.
  852. * @param [in] saltSz Size of random in bytes.
  853. * @param [in] kdrIdx Key derivation rate. kdr = 0 when -1, otherwise
  854. * kdr = 2^kdrIdx.
  855. * @param [in] index Index value to XOR in.
  856. * @param [out] key1 First key. Label value of 0x00.
  857. * @param [in] key1Sz Size of first key in bytes.
  858. * @param [out] key2 Second key. Label value of 0x01.
  859. * @param [in] key2Sz Size of second key in bytes.
  860. * @param [out] key3 Third key. Label value of 0x02.
  861. * @param [in] key3Sz Size of third key in bytes.
  862. * @return BAD_FUNC_ARG when key or salt is NULL.
  863. * @return BAD_FUNC_ARG when key length is not 16, 24 or 32.
  864. * @return BAD_FUNC_ARG when saltSz is larger than 14.
  865. * @return BAD_FUNC_ARG when kdrIdx is less than -1 or larger than 24.
  866. * @return MEMORY_E on dynamic memory allocation failure.
  867. * @return 0 on success.
  868. */
  869. int wc_SRTP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
  870. int kdrIdx, const byte* index, byte* key1, word32 key1Sz, byte* key2,
  871. word32 key2Sz, byte* key3, word32 key3Sz)
  872. {
  873. int ret = 0;
  874. byte block[AES_BLOCK_SIZE];
  875. #ifdef WOLFSSL_SMALL_STACK
  876. Aes* aes = NULL;
  877. #else
  878. Aes aes[1];
  879. #endif
  880. int aes_inited = 0;
  881. /* Validate parameters. */
  882. if ((key == NULL) || (keySz > AES_256_KEY_SIZE) || (salt == NULL) ||
  883. (saltSz > WC_SRTP_MAX_SALT) || (kdrIdx < -1) || (kdrIdx > 24)) {
  884. ret = BAD_FUNC_ARG;
  885. }
  886. #ifdef WOLFSSL_SMALL_STACK
  887. if (ret == 0) {
  888. aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_CIPHER);
  889. if (aes == NULL) {
  890. ret = MEMORY_E;
  891. }
  892. }
  893. if (aes != NULL)
  894. #endif
  895. {
  896. XMEMSET(aes, 0, sizeof(Aes));
  897. }
  898. /* Setup AES object. */
  899. if (ret == 0) {
  900. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  901. }
  902. if (ret == 0) {
  903. aes_inited = 1;
  904. ret = wc_AesSetKey(aes, key, keySz, NULL, AES_ENCRYPTION);
  905. }
  906. /* Calculate first block that can be used in each derivation. */
  907. if (ret == 0) {
  908. wc_srtp_kdf_first_block(salt, saltSz, kdrIdx, index, WC_SRTP_INDEX_LEN,
  909. block);
  910. }
  911. /* Calculate first key if required. */
  912. if ((ret == 0) && (key1 != NULL)) {
  913. ret = wc_srtp_kdf_derive_key(block, WC_SRTP_INDEX_LEN,
  914. WC_SRTP_LABEL_ENCRYPTION, key1, key1Sz, aes);
  915. }
  916. /* Calculate second key if required. */
  917. if ((ret == 0) && (key2 != NULL)) {
  918. ret = wc_srtp_kdf_derive_key(block, WC_SRTP_INDEX_LEN,
  919. WC_SRTP_LABEL_MSG_AUTH, key2, key2Sz, aes);
  920. }
  921. /* Calculate third key if required. */
  922. if ((ret == 0) && (key3 != NULL)) {
  923. ret = wc_srtp_kdf_derive_key(block, WC_SRTP_INDEX_LEN,
  924. WC_SRTP_LABEL_SALT, key3, key3Sz, aes);
  925. }
  926. if (aes_inited)
  927. wc_AesFree(aes);
  928. #ifdef WOLFSSL_SMALL_STACK
  929. XFREE(aes, NULL, DYNAMIC_TYPE_CIPHER);
  930. #endif
  931. return ret;
  932. }
  933. /* Derive keys using SRTCP KDF algorithm.
  934. *
  935. * SP 800-135 (RFC 3711).
  936. *
  937. * @param [in] key Key to use with encryption.
  938. * @param [in] keySz Size of key in bytes.
  939. * @param [in] salt Random non-secret value.
  940. * @param [in] saltSz Size of random in bytes.
  941. * @param [in] kdrIdx Key derivation rate index. kdr = 0 when -1, otherwise
  942. * kdr = 2^kdrIdx. See wc_SRTP_KDF_kdr_to_idx()
  943. * @param [in] index Index value to XOR in.
  944. * @param [out] key1 First key. Label value of 0x03.
  945. * @param [in] key1Sz Size of first key in bytes.
  946. * @param [out] key2 Second key. Label value of 0x04.
  947. * @param [in] key2Sz Size of second key in bytes.
  948. * @param [out] key3 Third key. Label value of 0x05.
  949. * @param [in] key3Sz Size of third key in bytes.
  950. * @return BAD_FUNC_ARG when key or salt is NULL.
  951. * @return BAD_FUNC_ARG when key length is not 16, 24 or 32.
  952. * @return BAD_FUNC_ARG when saltSz is larger than 14.
  953. * @return BAD_FUNC_ARG when kdrIdx is less than -1 or larger than 24.
  954. * @return MEMORY_E on dynamic memory allocation failure.
  955. * @return 0 on success.
  956. */
  957. int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
  958. int kdrIdx, const byte* index, byte* key1, word32 key1Sz, byte* key2,
  959. word32 key2Sz, byte* key3, word32 key3Sz)
  960. {
  961. int ret = 0;
  962. byte block[AES_BLOCK_SIZE];
  963. #ifdef WOLFSSL_SMALL_STACK
  964. Aes* aes = NULL;
  965. #else
  966. Aes aes[1];
  967. #endif
  968. int aes_inited = 0;
  969. /* Validate parameters. */
  970. if ((key == NULL) || (keySz > AES_256_KEY_SIZE) || (salt == NULL) ||
  971. (saltSz > WC_SRTP_MAX_SALT) || (kdrIdx < -1) || (kdrIdx > 24)) {
  972. ret = BAD_FUNC_ARG;
  973. }
  974. #ifdef WOLFSSL_SMALL_STACK
  975. if (ret == 0) {
  976. aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_CIPHER);
  977. if (aes == NULL) {
  978. ret = MEMORY_E;
  979. }
  980. }
  981. if (aes != NULL)
  982. #endif
  983. {
  984. XMEMSET(aes, 0, sizeof(Aes));
  985. }
  986. /* Setup AES object. */
  987. if (ret == 0) {
  988. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  989. }
  990. if (ret == 0) {
  991. aes_inited = 1;
  992. ret = wc_AesSetKey(aes, key, keySz, NULL, AES_ENCRYPTION);
  993. }
  994. /* Calculate first block that can be used in each derivation. */
  995. if (ret == 0) {
  996. wc_srtp_kdf_first_block(salt, saltSz, kdrIdx, index, WC_SRTCP_INDEX_LEN,
  997. block);
  998. }
  999. /* Calculate first key if required. */
  1000. if ((ret == 0) && (key1 != NULL)) {
  1001. ret = wc_srtp_kdf_derive_key(block, WC_SRTCP_INDEX_LEN,
  1002. WC_SRTCP_LABEL_ENCRYPTION, key1, key1Sz, aes);
  1003. }
  1004. /* Calculate second key if required. */
  1005. if ((ret == 0) && (key2 != NULL)) {
  1006. ret = wc_srtp_kdf_derive_key(block, WC_SRTCP_INDEX_LEN,
  1007. WC_SRTCP_LABEL_MSG_AUTH, key2, key2Sz, aes);
  1008. }
  1009. /* Calculate third key if required. */
  1010. if ((ret == 0) && (key3 != NULL)) {
  1011. ret = wc_srtp_kdf_derive_key(block, WC_SRTCP_INDEX_LEN,
  1012. WC_SRTCP_LABEL_SALT, key3, key3Sz, aes);
  1013. }
  1014. if (aes_inited)
  1015. wc_AesFree(aes);
  1016. #ifdef WOLFSSL_SMALL_STACK
  1017. XFREE(aes, NULL, DYNAMIC_TYPE_CIPHER);
  1018. #endif
  1019. return ret;
  1020. }
  1021. /* Derive key with label using SRTP KDF algorithm.
  1022. *
  1023. * SP 800-135 (RFC 3711).
  1024. *
  1025. * @param [in] key Key to use with encryption.
  1026. * @param [in] keySz Size of key in bytes.
  1027. * @param [in] salt Random non-secret value.
  1028. * @param [in] saltSz Size of random in bytes.
  1029. * @param [in] kdrIdx Key derivation rate index. kdr = 0 when -1, otherwise
  1030. * kdr = 2^kdrIdx. See wc_SRTP_KDF_kdr_to_idx()
  1031. * @param [in] index Index value to XOR in.
  1032. * @param [in] label Label to use when deriving key.
  1033. * @param [out] outKey Derived key.
  1034. * @param [in] outKeySz Size of derived key in bytes.
  1035. * @return BAD_FUNC_ARG when key, salt or outKey is NULL.
  1036. * @return BAD_FUNC_ARG when key length is not 16, 24 or 32.
  1037. * @return BAD_FUNC_ARG when saltSz is larger than 14.
  1038. * @return BAD_FUNC_ARG when kdrIdx is less than -1 or larger than 24.
  1039. * @return MEMORY_E on dynamic memory allocation failure.
  1040. * @return 0 on success.
  1041. */
  1042. int wc_SRTP_KDF_label(const byte* key, word32 keySz, const byte* salt,
  1043. word32 saltSz, int kdrIdx, const byte* index, byte label, byte* outKey,
  1044. word32 outKeySz)
  1045. {
  1046. int ret = 0;
  1047. byte block[AES_BLOCK_SIZE];
  1048. #ifdef WOLFSSL_SMALL_STACK
  1049. Aes* aes = NULL;
  1050. #else
  1051. Aes aes[1];
  1052. #endif
  1053. int aes_inited = 0;
  1054. /* Validate parameters. */
  1055. if ((key == NULL) || (keySz > AES_256_KEY_SIZE) || (salt == NULL) ||
  1056. (saltSz > WC_SRTP_MAX_SALT) || (kdrIdx < -1) || (kdrIdx > 24) ||
  1057. (outKey == NULL)) {
  1058. ret = BAD_FUNC_ARG;
  1059. }
  1060. #ifdef WOLFSSL_SMALL_STACK
  1061. if (ret == 0) {
  1062. aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_CIPHER);
  1063. if (aes == NULL) {
  1064. ret = MEMORY_E;
  1065. }
  1066. }
  1067. if (aes != NULL)
  1068. #endif
  1069. {
  1070. XMEMSET(aes, 0, sizeof(Aes));
  1071. }
  1072. /* Setup AES object. */
  1073. if (ret == 0) {
  1074. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  1075. }
  1076. if (ret == 0) {
  1077. aes_inited = 1;
  1078. ret = wc_AesSetKey(aes, key, keySz, NULL, AES_ENCRYPTION);
  1079. }
  1080. /* Calculate first block that can be used in each derivation. */
  1081. if (ret == 0) {
  1082. wc_srtp_kdf_first_block(salt, saltSz, kdrIdx, index, WC_SRTP_INDEX_LEN,
  1083. block);
  1084. }
  1085. if (ret == 0) {
  1086. /* Calculate key. */
  1087. ret = wc_srtp_kdf_derive_key(block, WC_SRTP_INDEX_LEN, label, outKey,
  1088. outKeySz, aes);
  1089. }
  1090. if (aes_inited)
  1091. wc_AesFree(aes);
  1092. #ifdef WOLFSSL_SMALL_STACK
  1093. XFREE(aes, NULL, DYNAMIC_TYPE_CIPHER);
  1094. #endif
  1095. return ret;
  1096. }
  1097. /* Derive key with label using SRTCP KDF algorithm.
  1098. *
  1099. * SP 800-135 (RFC 3711).
  1100. *
  1101. * @param [in] key Key to use with encryption.
  1102. * @param [in] keySz Size of key in bytes.
  1103. * @param [in] salt Random non-secret value.
  1104. * @param [in] saltSz Size of random in bytes.
  1105. * @param [in] kdrIdx Key derivation rate index. kdr = 0 when -1, otherwise
  1106. * kdr = 2^kdrIdx. See wc_SRTP_KDF_kdr_to_idx()
  1107. * @param [in] index Index value to XOR in.
  1108. * @param [in] label Label to use when deriving key.
  1109. * @param [out] outKey Derived key.
  1110. * @param [in] outKeySz Size of derived key in bytes.
  1111. * @return BAD_FUNC_ARG when key, salt or outKey is NULL.
  1112. * @return BAD_FUNC_ARG when key length is not 16, 24 or 32.
  1113. * @return BAD_FUNC_ARG when saltSz is larger than 14.
  1114. * @return BAD_FUNC_ARG when kdrIdx is less than -1 or larger than 24.
  1115. * @return MEMORY_E on dynamic memory allocation failure.
  1116. * @return 0 on success.
  1117. */
  1118. int wc_SRTCP_KDF_label(const byte* key, word32 keySz, const byte* salt,
  1119. word32 saltSz, int kdrIdx, const byte* index, byte label, byte* outKey,
  1120. word32 outKeySz)
  1121. {
  1122. int ret = 0;
  1123. byte block[AES_BLOCK_SIZE];
  1124. #ifdef WOLFSSL_SMALL_STACK
  1125. Aes* aes = NULL;
  1126. #else
  1127. Aes aes[1];
  1128. #endif
  1129. int aes_inited = 0;
  1130. /* Validate parameters. */
  1131. if ((key == NULL) || (keySz > AES_256_KEY_SIZE) || (salt == NULL) ||
  1132. (saltSz > WC_SRTP_MAX_SALT) || (kdrIdx < -1) || (kdrIdx > 24) ||
  1133. (outKey == NULL)) {
  1134. ret = BAD_FUNC_ARG;
  1135. }
  1136. #ifdef WOLFSSL_SMALL_STACK
  1137. if (ret == 0) {
  1138. aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_CIPHER);
  1139. if (aes == NULL) {
  1140. ret = MEMORY_E;
  1141. }
  1142. }
  1143. if (aes != NULL)
  1144. #endif
  1145. {
  1146. XMEMSET(aes, 0, sizeof(Aes));
  1147. }
  1148. /* Setup AES object. */
  1149. if (ret == 0) {
  1150. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  1151. }
  1152. if (ret == 0) {
  1153. aes_inited = 1;
  1154. ret = wc_AesSetKey(aes, key, keySz, NULL, AES_ENCRYPTION);
  1155. }
  1156. /* Calculate first block that can be used in each derivation. */
  1157. if (ret == 0) {
  1158. wc_srtp_kdf_first_block(salt, saltSz, kdrIdx, index, WC_SRTCP_INDEX_LEN,
  1159. block);
  1160. }
  1161. if (ret == 0) {
  1162. /* Calculate key. */
  1163. ret = wc_srtp_kdf_derive_key(block, WC_SRTCP_INDEX_LEN, label, outKey,
  1164. outKeySz, aes);
  1165. }
  1166. if (aes_inited)
  1167. wc_AesFree(aes);
  1168. #ifdef WOLFSSL_SMALL_STACK
  1169. XFREE(aes, NULL, DYNAMIC_TYPE_CIPHER);
  1170. #endif
  1171. return ret;
  1172. }
  1173. /* Converts a kdr value to an index to use in SRTP/SRTCP KDF API.
  1174. *
  1175. * @param [in] kdr Key derivation rate to convert.
  1176. * @return Key derivation rate as an index.
  1177. */
  1178. int wc_SRTP_KDF_kdr_to_idx(word32 kdr)
  1179. {
  1180. int idx = -1;
  1181. /* Keep shifting value down and incrementing index until top bit is gone. */
  1182. while (kdr != 0) {
  1183. kdr >>= 1;
  1184. idx++;
  1185. }
  1186. /* Index of top bit set. */
  1187. return idx;
  1188. }
  1189. #endif /* WC_SRTP_KDF */
  1190. #endif /* NO_KDF */