hmac.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. /* hmac.c
  2. *
  3. * Copyright (C) 2006-2022 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_HMAC
  28. #if defined(HAVE_FIPS) && \
  29. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  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$b")
  34. #pragma const_seg(".fipsB$b")
  35. #endif
  36. #endif
  37. #include <wolfssl/wolfcrypt/hmac.h>
  38. #ifdef WOLF_CRYPTO_CB
  39. #include <wolfssl/wolfcrypt/cryptocb.h>
  40. #endif
  41. #ifdef NO_INLINE
  42. #include <wolfssl/wolfcrypt/misc.h>
  43. #else
  44. #define WOLFSSL_MISC_INCLUDED
  45. #include <wolfcrypt/src/misc.c>
  46. #endif
  47. #ifdef WOLFSSL_KCAPI_HMAC
  48. #include <wolfssl/wolfcrypt/port/kcapi/kcapi_hmac.h>
  49. /* map the _Software calls used by kcapi_hmac.c */
  50. #define wc_HmacSetKey wc_HmacSetKey_Software
  51. #define wc_HmacUpdate wc_HmacUpdate_Software
  52. #define wc_HmacFinal wc_HmacFinal_Software
  53. #endif
  54. /* fips wrapper calls, user can call direct */
  55. /* If building for old FIPS. */
  56. #if defined(HAVE_FIPS) && \
  57. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  58. /* does init */
  59. int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 keySz)
  60. {
  61. if (hmac == NULL || (key == NULL && keySz != 0) ||
  62. !(type == WC_MD5 || type == WC_SHA || type == WC_SHA256 ||
  63. type == WC_SHA384 || type == WC_SHA512)) {
  64. return BAD_FUNC_ARG;
  65. }
  66. return HmacSetKey_fips(hmac, type, key, keySz);
  67. }
  68. int wc_HmacUpdate(Hmac* hmac, const byte* in, word32 sz)
  69. {
  70. if (hmac == NULL || (in == NULL && sz > 0)) {
  71. return BAD_FUNC_ARG;
  72. }
  73. return HmacUpdate_fips(hmac, in, sz);
  74. }
  75. int wc_HmacFinal(Hmac* hmac, byte* out)
  76. {
  77. if (hmac == NULL) {
  78. return BAD_FUNC_ARG;
  79. }
  80. return HmacFinal_fips(hmac, out);
  81. }
  82. int wolfSSL_GetHmacMaxSize(void)
  83. {
  84. return CyaSSL_GetHmacMaxSize();
  85. }
  86. int wc_HmacInit(Hmac* hmac, void* heap, int devId)
  87. {
  88. #ifndef WOLFSSL_KCAPI_HMAC
  89. (void)hmac;
  90. (void)heap;
  91. (void)devId;
  92. return 0;
  93. #else
  94. return HmacInit(hmac, heap, devId);
  95. #endif
  96. }
  97. void wc_HmacFree(Hmac* hmac)
  98. {
  99. #ifndef WOLFSSL_KCAPI_HMAC
  100. (void)hmac;
  101. #else
  102. HmacFree(hmac);
  103. #endif
  104. }
  105. #ifdef HAVE_HKDF
  106. int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
  107. const byte* salt, word32 saltSz,
  108. const byte* info, word32 infoSz,
  109. byte* out, word32 outSz)
  110. {
  111. return HKDF(type, inKey, inKeySz, salt, saltSz,
  112. info, infoSz, out, outSz);
  113. }
  114. #endif /* HAVE_HKDF */
  115. #else /* else build without fips, or for new fips */
  116. int wc_HmacSizeByType(int type)
  117. {
  118. int ret;
  119. if (!(type == WC_MD5 || type == WC_SHA ||
  120. type == WC_SHA224 || type == WC_SHA256 ||
  121. type == WC_SHA384 || type == WC_SHA512 ||
  122. type == WC_SHA3_224 || type == WC_SHA3_256 ||
  123. type == WC_SHA3_384 || type == WC_SHA3_512)) {
  124. return BAD_FUNC_ARG;
  125. }
  126. switch (type) {
  127. #ifndef NO_MD5
  128. case WC_MD5:
  129. ret = WC_MD5_DIGEST_SIZE;
  130. break;
  131. #endif /* !NO_MD5 */
  132. #ifndef NO_SHA
  133. case WC_SHA:
  134. ret = WC_SHA_DIGEST_SIZE;
  135. break;
  136. #endif /* !NO_SHA */
  137. #ifdef WOLFSSL_SHA224
  138. case WC_SHA224:
  139. ret = WC_SHA224_DIGEST_SIZE;
  140. break;
  141. #endif /* WOLFSSL_SHA224 */
  142. #ifndef NO_SHA256
  143. case WC_SHA256:
  144. ret = WC_SHA256_DIGEST_SIZE;
  145. break;
  146. #endif /* !NO_SHA256 */
  147. #ifdef WOLFSSL_SHA384
  148. case WC_SHA384:
  149. ret = WC_SHA384_DIGEST_SIZE;
  150. break;
  151. #endif /* WOLFSSL_SHA384 */
  152. #ifdef WOLFSSL_SHA512
  153. case WC_SHA512:
  154. ret = WC_SHA512_DIGEST_SIZE;
  155. break;
  156. #endif /* WOLFSSL_SHA512 */
  157. #ifdef WOLFSSL_SHA3
  158. case WC_SHA3_224:
  159. ret = WC_SHA3_224_DIGEST_SIZE;
  160. break;
  161. case WC_SHA3_256:
  162. ret = WC_SHA3_256_DIGEST_SIZE;
  163. break;
  164. case WC_SHA3_384:
  165. ret = WC_SHA3_384_DIGEST_SIZE;
  166. break;
  167. case WC_SHA3_512:
  168. ret = WC_SHA3_512_DIGEST_SIZE;
  169. break;
  170. #endif
  171. default:
  172. ret = BAD_FUNC_ARG;
  173. break;
  174. }
  175. return ret;
  176. }
  177. int _InitHmac(Hmac* hmac, int type, void* heap)
  178. {
  179. int ret = 0;
  180. #ifdef WOLF_CRYPTO_CB
  181. int devId = hmac->devId;
  182. #else
  183. int devId = INVALID_DEVID;
  184. #endif
  185. switch (type) {
  186. #ifndef NO_MD5
  187. case WC_MD5:
  188. ret = wc_InitMd5_ex(&hmac->hash.md5, heap, devId);
  189. break;
  190. #endif /* !NO_MD5 */
  191. #ifndef NO_SHA
  192. case WC_SHA:
  193. ret = wc_InitSha_ex(&hmac->hash.sha, heap, devId);
  194. break;
  195. #endif /* !NO_SHA */
  196. #ifdef WOLFSSL_SHA224
  197. case WC_SHA224:
  198. ret = wc_InitSha224_ex(&hmac->hash.sha224, heap, devId);
  199. break;
  200. #endif /* WOLFSSL_SHA224 */
  201. #ifndef NO_SHA256
  202. case WC_SHA256:
  203. ret = wc_InitSha256_ex(&hmac->hash.sha256, heap, devId);
  204. break;
  205. #endif /* !NO_SHA256 */
  206. #ifdef WOLFSSL_SHA384
  207. case WC_SHA384:
  208. ret = wc_InitSha384_ex(&hmac->hash.sha384, heap, devId);
  209. break;
  210. #endif /* WOLFSSL_SHA384 */
  211. #ifdef WOLFSSL_SHA512
  212. case WC_SHA512:
  213. ret = wc_InitSha512_ex(&hmac->hash.sha512, heap, devId);
  214. break;
  215. #endif /* WOLFSSL_SHA512 */
  216. #ifdef WOLFSSL_SHA3
  217. #ifndef WOLFSSL_NOSHA3_224
  218. case WC_SHA3_224:
  219. ret = wc_InitSha3_224(&hmac->hash.sha3, heap, devId);
  220. break;
  221. #endif
  222. #ifndef WOLFSSL_NOSHA3_256
  223. case WC_SHA3_256:
  224. ret = wc_InitSha3_256(&hmac->hash.sha3, heap, devId);
  225. break;
  226. #endif
  227. #ifndef WOLFSSL_NOSHA3_384
  228. case WC_SHA3_384:
  229. ret = wc_InitSha3_384(&hmac->hash.sha3, heap, devId);
  230. break;
  231. #endif
  232. #ifndef WOLFSSL_NOSHA3_512
  233. case WC_SHA3_512:
  234. ret = wc_InitSha3_512(&hmac->hash.sha3, heap, devId);
  235. break;
  236. #endif
  237. #endif
  238. default:
  239. ret = BAD_FUNC_ARG;
  240. break;
  241. }
  242. /* default to NULL heap hint or test value */
  243. #ifdef WOLFSSL_HEAP_TEST
  244. hmac->heap = (void*)WOLFSSL_HEAP_TEST;
  245. #else
  246. hmac->heap = heap;
  247. #endif /* WOLFSSL_HEAP_TEST */
  248. return ret;
  249. }
  250. int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
  251. {
  252. #ifndef WOLFSSL_MAXQ108X
  253. byte* ip;
  254. byte* op;
  255. word32 i, hmac_block_size = 0;
  256. #endif
  257. int ret = 0;
  258. void* heap = NULL;
  259. if (hmac == NULL || (key == NULL && length != 0) ||
  260. !(type == WC_MD5 || type == WC_SHA ||
  261. type == WC_SHA224 || type == WC_SHA256 ||
  262. type == WC_SHA384 || type == WC_SHA512 ||
  263. type == WC_SHA3_224 || type == WC_SHA3_256 ||
  264. type == WC_SHA3_384 || type == WC_SHA3_512)) {
  265. return BAD_FUNC_ARG;
  266. }
  267. #ifndef HAVE_FIPS
  268. /* if set key has already been run then make sure and free existing */
  269. /* This is for async and PIC32MZ situations, and just normally OK,
  270. provided the user calls wc_HmacInit() first. That function is not
  271. available in FIPS builds. In current FIPS builds, the hashes are
  272. not allocating resources. */
  273. if (hmac->macType != WC_HASH_TYPE_NONE) {
  274. wc_HmacFree(hmac);
  275. }
  276. #endif
  277. hmac->innerHashKeyed = 0;
  278. hmac->macType = (byte)type;
  279. ret = _InitHmac(hmac, type, heap);
  280. if (ret != 0)
  281. return ret;
  282. #ifdef HAVE_FIPS
  283. if (length < HMAC_FIPS_MIN_KEY) {
  284. WOLFSSL_ERROR_VERBOSE(HMAC_MIN_KEYLEN_E);
  285. return HMAC_MIN_KEYLEN_E;
  286. }
  287. #endif
  288. #ifdef WOLF_CRYPTO_CB
  289. hmac->keyRaw = key; /* use buffer directly */
  290. hmac->keyLen = length;
  291. #endif
  292. #ifdef WOLFSSL_MAXQ108X
  293. /* For MAXQ108x, nothing left to do. */
  294. return 0;
  295. #else
  296. ip = (byte*)hmac->ipad;
  297. op = (byte*)hmac->opad;
  298. switch (hmac->macType) {
  299. #ifndef NO_MD5
  300. case WC_MD5:
  301. hmac_block_size = WC_MD5_BLOCK_SIZE;
  302. if (length <= WC_MD5_BLOCK_SIZE) {
  303. if (key != NULL) {
  304. XMEMCPY(ip, key, length);
  305. }
  306. }
  307. else {
  308. ret = wc_Md5Update(&hmac->hash.md5, key, length);
  309. if (ret != 0)
  310. break;
  311. ret = wc_Md5Final(&hmac->hash.md5, ip);
  312. if (ret != 0)
  313. break;
  314. length = WC_MD5_DIGEST_SIZE;
  315. }
  316. break;
  317. #endif /* !NO_MD5 */
  318. #ifndef NO_SHA
  319. case WC_SHA:
  320. hmac_block_size = WC_SHA_BLOCK_SIZE;
  321. if (length <= WC_SHA_BLOCK_SIZE) {
  322. if (key != NULL) {
  323. XMEMCPY(ip, key, length);
  324. }
  325. }
  326. else {
  327. ret = wc_ShaUpdate(&hmac->hash.sha, key, length);
  328. if (ret != 0)
  329. break;
  330. ret = wc_ShaFinal(&hmac->hash.sha, ip);
  331. if (ret != 0)
  332. break;
  333. length = WC_SHA_DIGEST_SIZE;
  334. }
  335. break;
  336. #endif /* !NO_SHA */
  337. #ifdef WOLFSSL_SHA224
  338. case WC_SHA224:
  339. hmac_block_size = WC_SHA224_BLOCK_SIZE;
  340. if (length <= WC_SHA224_BLOCK_SIZE) {
  341. if (key != NULL) {
  342. XMEMCPY(ip, key, length);
  343. }
  344. }
  345. else {
  346. ret = wc_Sha224Update(&hmac->hash.sha224, key, length);
  347. if (ret != 0)
  348. break;
  349. ret = wc_Sha224Final(&hmac->hash.sha224, ip);
  350. if (ret != 0)
  351. break;
  352. length = WC_SHA224_DIGEST_SIZE;
  353. }
  354. break;
  355. #endif /* WOLFSSL_SHA224 */
  356. #ifndef NO_SHA256
  357. case WC_SHA256:
  358. hmac_block_size = WC_SHA256_BLOCK_SIZE;
  359. if (length <= WC_SHA256_BLOCK_SIZE) {
  360. if (key != NULL) {
  361. XMEMCPY(ip, key, length);
  362. }
  363. }
  364. else {
  365. ret = wc_Sha256Update(&hmac->hash.sha256, key, length);
  366. if (ret != 0)
  367. break;
  368. ret = wc_Sha256Final(&hmac->hash.sha256, ip);
  369. if (ret != 0)
  370. break;
  371. length = WC_SHA256_DIGEST_SIZE;
  372. }
  373. break;
  374. #endif /* !NO_SHA256 */
  375. #ifdef WOLFSSL_SHA384
  376. case WC_SHA384:
  377. hmac_block_size = WC_SHA384_BLOCK_SIZE;
  378. if (length <= WC_SHA384_BLOCK_SIZE) {
  379. if (key != NULL) {
  380. XMEMCPY(ip, key, length);
  381. }
  382. }
  383. else {
  384. ret = wc_Sha384Update(&hmac->hash.sha384, key, length);
  385. if (ret != 0)
  386. break;
  387. ret = wc_Sha384Final(&hmac->hash.sha384, ip);
  388. if (ret != 0)
  389. break;
  390. length = WC_SHA384_DIGEST_SIZE;
  391. }
  392. break;
  393. #endif /* WOLFSSL_SHA384 */
  394. #ifdef WOLFSSL_SHA512
  395. case WC_SHA512:
  396. hmac_block_size = WC_SHA512_BLOCK_SIZE;
  397. if (length <= WC_SHA512_BLOCK_SIZE) {
  398. if (key != NULL) {
  399. XMEMCPY(ip, key, length);
  400. }
  401. }
  402. else {
  403. ret = wc_Sha512Update(&hmac->hash.sha512, key, length);
  404. if (ret != 0)
  405. break;
  406. ret = wc_Sha512Final(&hmac->hash.sha512, ip);
  407. if (ret != 0)
  408. break;
  409. length = WC_SHA512_DIGEST_SIZE;
  410. }
  411. break;
  412. #endif /* WOLFSSL_SHA512 */
  413. #ifdef WOLFSSL_SHA3
  414. #ifndef WOLFSSL_NOSHA3_224
  415. case WC_SHA3_224:
  416. hmac_block_size = WC_SHA3_224_BLOCK_SIZE;
  417. if (length <= WC_SHA3_224_BLOCK_SIZE) {
  418. if (key != NULL) {
  419. XMEMCPY(ip, key, length);
  420. }
  421. }
  422. else {
  423. ret = wc_Sha3_224_Update(&hmac->hash.sha3, key, length);
  424. if (ret != 0)
  425. break;
  426. ret = wc_Sha3_224_Final(&hmac->hash.sha3, ip);
  427. if (ret != 0)
  428. break;
  429. length = WC_SHA3_224_DIGEST_SIZE;
  430. }
  431. break;
  432. #endif
  433. #ifndef WOLFSSL_NOSHA3_256
  434. case WC_SHA3_256:
  435. hmac_block_size = WC_SHA3_256_BLOCK_SIZE;
  436. if (length <= WC_SHA3_256_BLOCK_SIZE) {
  437. if (key != NULL) {
  438. XMEMCPY(ip, key, length);
  439. }
  440. }
  441. else {
  442. ret = wc_Sha3_256_Update(&hmac->hash.sha3, key, length);
  443. if (ret != 0)
  444. break;
  445. ret = wc_Sha3_256_Final(&hmac->hash.sha3, ip);
  446. if (ret != 0)
  447. break;
  448. length = WC_SHA3_256_DIGEST_SIZE;
  449. }
  450. break;
  451. #endif
  452. #ifndef WOLFSSL_NOSHA3_384
  453. case WC_SHA3_384:
  454. hmac_block_size = WC_SHA3_384_BLOCK_SIZE;
  455. if (length <= WC_SHA3_384_BLOCK_SIZE) {
  456. if (key != NULL) {
  457. XMEMCPY(ip, key, length);
  458. }
  459. }
  460. else {
  461. ret = wc_Sha3_384_Update(&hmac->hash.sha3, key, length);
  462. if (ret != 0)
  463. break;
  464. ret = wc_Sha3_384_Final(&hmac->hash.sha3, ip);
  465. if (ret != 0)
  466. break;
  467. length = WC_SHA3_384_DIGEST_SIZE;
  468. }
  469. break;
  470. #endif
  471. #ifndef WOLFSSL_NOSHA3_512
  472. case WC_SHA3_512:
  473. hmac_block_size = WC_SHA3_512_BLOCK_SIZE;
  474. if (length <= WC_SHA3_512_BLOCK_SIZE) {
  475. if (key != NULL) {
  476. XMEMCPY(ip, key, length);
  477. }
  478. }
  479. else {
  480. ret = wc_Sha3_512_Update(&hmac->hash.sha3, key, length);
  481. if (ret != 0)
  482. break;
  483. ret = wc_Sha3_512_Final(&hmac->hash.sha3, ip);
  484. if (ret != 0)
  485. break;
  486. length = WC_SHA3_512_DIGEST_SIZE;
  487. }
  488. break;
  489. #endif
  490. #endif /* WOLFSSL_SHA3 */
  491. default:
  492. return BAD_FUNC_ARG;
  493. }
  494. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  495. if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
  496. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  497. #ifdef HAVE_INTEL_QA
  498. if (IntelQaHmacGetType(hmac->macType, NULL) == 0)
  499. #endif
  500. {
  501. if (length > hmac_block_size)
  502. length = hmac_block_size;
  503. /* update key length */
  504. hmac->keyLen = (word16)length;
  505. return ret;
  506. }
  507. /* no need to pad below */
  508. #endif
  509. }
  510. #endif
  511. if (ret == 0) {
  512. if (length < hmac_block_size)
  513. XMEMSET(ip + length, 0, hmac_block_size - length);
  514. for(i = 0; i < hmac_block_size; i++) {
  515. op[i] = ip[i] ^ OPAD;
  516. ip[i] ^= IPAD;
  517. }
  518. }
  519. return ret;
  520. #endif /* WOLFSSL_MAXQ108X */
  521. }
  522. static int HmacKeyInnerHash(Hmac* hmac)
  523. {
  524. int ret = 0;
  525. switch (hmac->macType) {
  526. #ifndef NO_MD5
  527. case WC_MD5:
  528. ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->ipad,
  529. WC_MD5_BLOCK_SIZE);
  530. break;
  531. #endif /* !NO_MD5 */
  532. #ifndef NO_SHA
  533. case WC_SHA:
  534. ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->ipad,
  535. WC_SHA_BLOCK_SIZE);
  536. break;
  537. #endif /* !NO_SHA */
  538. #ifdef WOLFSSL_SHA224
  539. case WC_SHA224:
  540. ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->ipad,
  541. WC_SHA224_BLOCK_SIZE);
  542. break;
  543. #endif /* WOLFSSL_SHA224 */
  544. #ifndef NO_SHA256
  545. case WC_SHA256:
  546. ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->ipad,
  547. WC_SHA256_BLOCK_SIZE);
  548. break;
  549. #endif /* !NO_SHA256 */
  550. #ifdef WOLFSSL_SHA384
  551. case WC_SHA384:
  552. ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->ipad,
  553. WC_SHA384_BLOCK_SIZE);
  554. break;
  555. #endif /* WOLFSSL_SHA384 */
  556. #ifdef WOLFSSL_SHA512
  557. case WC_SHA512:
  558. ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->ipad,
  559. WC_SHA512_BLOCK_SIZE);
  560. break;
  561. #endif /* WOLFSSL_SHA512 */
  562. #ifdef WOLFSSL_SHA3
  563. #ifndef WOLFSSL_NOSHA3_224
  564. case WC_SHA3_224:
  565. ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  566. WC_SHA3_224_BLOCK_SIZE);
  567. break;
  568. #endif
  569. #ifndef WOLFSSL_NOSHA3_256
  570. case WC_SHA3_256:
  571. ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  572. WC_SHA3_256_BLOCK_SIZE);
  573. break;
  574. #endif
  575. #ifndef WOLFSSL_NOSHA3_384
  576. case WC_SHA3_384:
  577. ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  578. WC_SHA3_384_BLOCK_SIZE);
  579. break;
  580. #endif
  581. #ifndef WOLFSSL_NOSHA3_512
  582. case WC_SHA3_512:
  583. ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  584. WC_SHA3_512_BLOCK_SIZE);
  585. break;
  586. #endif
  587. #endif /* WOLFSSL_SHA3 */
  588. default:
  589. break;
  590. }
  591. if (ret == 0)
  592. hmac->innerHashKeyed = WC_HMAC_INNER_HASH_KEYED_SW;
  593. return ret;
  594. }
  595. int wc_HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
  596. {
  597. int ret = 0;
  598. if (hmac == NULL || (msg == NULL && length > 0)) {
  599. return BAD_FUNC_ARG;
  600. }
  601. #ifdef WOLF_CRYPTO_CB
  602. if (hmac->devId != INVALID_DEVID) {
  603. ret = wc_CryptoCb_Hmac(hmac, hmac->macType, msg, length, NULL);
  604. if (ret != CRYPTOCB_UNAVAILABLE)
  605. return ret;
  606. /* fall-through when unavailable */
  607. ret = 0; /* reset error code */
  608. }
  609. #endif
  610. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  611. if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
  612. #if defined(HAVE_CAVIUM)
  613. return NitroxHmacUpdate(hmac, msg, length);
  614. #elif defined(HAVE_INTEL_QA)
  615. if (IntelQaHmacGetType(hmac->macType, NULL) == 0) {
  616. return IntelQaHmac(&hmac->asyncDev, hmac->macType,
  617. (byte*)hmac->ipad, hmac->keyLen, NULL, msg, length);
  618. }
  619. #endif
  620. }
  621. #endif /* WOLFSSL_ASYNC_CRYPT */
  622. if (!hmac->innerHashKeyed) {
  623. ret = HmacKeyInnerHash(hmac);
  624. if (ret != 0)
  625. return ret;
  626. }
  627. switch (hmac->macType) {
  628. #ifndef NO_MD5
  629. case WC_MD5:
  630. ret = wc_Md5Update(&hmac->hash.md5, msg, length);
  631. break;
  632. #endif /* !NO_MD5 */
  633. #ifndef NO_SHA
  634. case WC_SHA:
  635. ret = wc_ShaUpdate(&hmac->hash.sha, msg, length);
  636. break;
  637. #endif /* !NO_SHA */
  638. #ifdef WOLFSSL_SHA224
  639. case WC_SHA224:
  640. ret = wc_Sha224Update(&hmac->hash.sha224, msg, length);
  641. break;
  642. #endif /* WOLFSSL_SHA224 */
  643. #ifndef NO_SHA256
  644. case WC_SHA256:
  645. ret = wc_Sha256Update(&hmac->hash.sha256, msg, length);
  646. break;
  647. #endif /* !NO_SHA256 */
  648. #ifdef WOLFSSL_SHA384
  649. case WC_SHA384:
  650. ret = wc_Sha384Update(&hmac->hash.sha384, msg, length);
  651. break;
  652. #endif /* WOLFSSL_SHA384 */
  653. #ifdef WOLFSSL_SHA512
  654. case WC_SHA512:
  655. ret = wc_Sha512Update(&hmac->hash.sha512, msg, length);
  656. break;
  657. #endif /* WOLFSSL_SHA512 */
  658. #ifdef WOLFSSL_SHA3
  659. #ifndef WOLFSSL_NOSHA3_224
  660. case WC_SHA3_224:
  661. ret = wc_Sha3_224_Update(&hmac->hash.sha3, msg, length);
  662. break;
  663. #endif
  664. #ifndef WOLFSSL_NOSHA3_256
  665. case WC_SHA3_256:
  666. ret = wc_Sha3_256_Update(&hmac->hash.sha3, msg, length);
  667. break;
  668. #endif
  669. #ifndef WOLFSSL_NOSHA3_384
  670. case WC_SHA3_384:
  671. ret = wc_Sha3_384_Update(&hmac->hash.sha3, msg, length);
  672. break;
  673. #endif
  674. #ifndef WOLFSSL_NOSHA3_512
  675. case WC_SHA3_512:
  676. ret = wc_Sha3_512_Update(&hmac->hash.sha3, msg, length);
  677. break;
  678. #endif
  679. #endif /* WOLFSSL_SHA3 */
  680. default:
  681. break;
  682. }
  683. return ret;
  684. }
  685. int wc_HmacFinal(Hmac* hmac, byte* hash)
  686. {
  687. int ret;
  688. if (hmac == NULL || hash == NULL) {
  689. return BAD_FUNC_ARG;
  690. }
  691. #ifdef WOLF_CRYPTO_CB
  692. if (hmac->devId != INVALID_DEVID) {
  693. ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, hash);
  694. if (ret != CRYPTOCB_UNAVAILABLE)
  695. return ret;
  696. /* fall-through when unavailable */
  697. }
  698. #endif
  699. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  700. if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
  701. int hashLen = wc_HmacSizeByType(hmac->macType);
  702. if (hashLen <= 0)
  703. return hashLen;
  704. #if defined(HAVE_CAVIUM)
  705. return NitroxHmacFinal(hmac, hash, hashLen);
  706. #elif defined(HAVE_INTEL_QA)
  707. if (IntelQaHmacGetType(hmac->macType, NULL) == 0) {
  708. return IntelQaHmac(&hmac->asyncDev, hmac->macType,
  709. (byte*)hmac->ipad, hmac->keyLen, hash, NULL, hashLen);
  710. }
  711. #endif
  712. }
  713. #endif /* WOLFSSL_ASYNC_CRYPT */
  714. if (!hmac->innerHashKeyed) {
  715. ret = HmacKeyInnerHash(hmac);
  716. if (ret != 0)
  717. return ret;
  718. }
  719. switch (hmac->macType) {
  720. #ifndef NO_MD5
  721. case WC_MD5:
  722. ret = wc_Md5Final(&hmac->hash.md5, (byte*)hmac->innerHash);
  723. if (ret != 0)
  724. break;
  725. ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->opad,
  726. WC_MD5_BLOCK_SIZE);
  727. if (ret != 0)
  728. break;
  729. ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->innerHash,
  730. WC_MD5_DIGEST_SIZE);
  731. if (ret != 0)
  732. break;
  733. ret = wc_Md5Final(&hmac->hash.md5, hash);
  734. break;
  735. #endif /* !NO_MD5 */
  736. #ifndef NO_SHA
  737. case WC_SHA:
  738. ret = wc_ShaFinal(&hmac->hash.sha, (byte*)hmac->innerHash);
  739. if (ret != 0)
  740. break;
  741. ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->opad,
  742. WC_SHA_BLOCK_SIZE);
  743. if (ret != 0)
  744. break;
  745. ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->innerHash,
  746. WC_SHA_DIGEST_SIZE);
  747. if (ret != 0)
  748. break;
  749. ret = wc_ShaFinal(&hmac->hash.sha, hash);
  750. break;
  751. #endif /* !NO_SHA */
  752. #ifdef WOLFSSL_SHA224
  753. case WC_SHA224:
  754. ret = wc_Sha224Final(&hmac->hash.sha224, (byte*)hmac->innerHash);
  755. if (ret != 0)
  756. break;
  757. ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->opad,
  758. WC_SHA224_BLOCK_SIZE);
  759. if (ret != 0)
  760. break;
  761. ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->innerHash,
  762. WC_SHA224_DIGEST_SIZE);
  763. if (ret != 0)
  764. break;
  765. ret = wc_Sha224Final(&hmac->hash.sha224, hash);
  766. if (ret != 0)
  767. break;
  768. break;
  769. #endif /* WOLFSSL_SHA224 */
  770. #ifndef NO_SHA256
  771. case WC_SHA256:
  772. ret = wc_Sha256Final(&hmac->hash.sha256, (byte*)hmac->innerHash);
  773. if (ret != 0)
  774. break;
  775. ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->opad,
  776. WC_SHA256_BLOCK_SIZE);
  777. if (ret != 0)
  778. break;
  779. ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->innerHash,
  780. WC_SHA256_DIGEST_SIZE);
  781. if (ret != 0)
  782. break;
  783. ret = wc_Sha256Final(&hmac->hash.sha256, hash);
  784. break;
  785. #endif /* !NO_SHA256 */
  786. #ifdef WOLFSSL_SHA384
  787. case WC_SHA384:
  788. ret = wc_Sha384Final(&hmac->hash.sha384, (byte*)hmac->innerHash);
  789. if (ret != 0)
  790. break;
  791. ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->opad,
  792. WC_SHA384_BLOCK_SIZE);
  793. if (ret != 0)
  794. break;
  795. ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->innerHash,
  796. WC_SHA384_DIGEST_SIZE);
  797. if (ret != 0)
  798. break;
  799. ret = wc_Sha384Final(&hmac->hash.sha384, hash);
  800. break;
  801. #endif /* WOLFSSL_SHA384 */
  802. #ifdef WOLFSSL_SHA512
  803. case WC_SHA512:
  804. ret = wc_Sha512Final(&hmac->hash.sha512, (byte*)hmac->innerHash);
  805. if (ret != 0)
  806. break;
  807. ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->opad,
  808. WC_SHA512_BLOCK_SIZE);
  809. if (ret != 0)
  810. break;
  811. ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->innerHash,
  812. WC_SHA512_DIGEST_SIZE);
  813. if (ret != 0)
  814. break;
  815. ret = wc_Sha512Final(&hmac->hash.sha512, hash);
  816. break;
  817. #endif /* WOLFSSL_SHA512 */
  818. #ifdef WOLFSSL_SHA3
  819. #ifndef WOLFSSL_NOSHA3_224
  820. case WC_SHA3_224:
  821. ret = wc_Sha3_224_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  822. if (ret != 0)
  823. break;
  824. ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  825. WC_SHA3_224_BLOCK_SIZE);
  826. if (ret != 0)
  827. break;
  828. ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  829. WC_SHA3_224_DIGEST_SIZE);
  830. if (ret != 0)
  831. break;
  832. ret = wc_Sha3_224_Final(&hmac->hash.sha3, hash);
  833. break;
  834. #endif
  835. #ifndef WOLFSSL_NOSHA3_256
  836. case WC_SHA3_256:
  837. ret = wc_Sha3_256_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  838. if (ret != 0)
  839. break;
  840. ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  841. WC_SHA3_256_BLOCK_SIZE);
  842. if (ret != 0)
  843. break;
  844. ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  845. WC_SHA3_256_DIGEST_SIZE);
  846. if (ret != 0)
  847. break;
  848. ret = wc_Sha3_256_Final(&hmac->hash.sha3, hash);
  849. break;
  850. #endif
  851. #ifndef WOLFSSL_NOSHA3_384
  852. case WC_SHA3_384:
  853. ret = wc_Sha3_384_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  854. if (ret != 0)
  855. break;
  856. ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  857. WC_SHA3_384_BLOCK_SIZE);
  858. if (ret != 0)
  859. break;
  860. ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  861. WC_SHA3_384_DIGEST_SIZE);
  862. if (ret != 0)
  863. break;
  864. ret = wc_Sha3_384_Final(&hmac->hash.sha3, hash);
  865. break;
  866. #endif
  867. #ifndef WOLFSSL_NOSHA3_512
  868. case WC_SHA3_512:
  869. ret = wc_Sha3_512_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  870. if (ret != 0)
  871. break;
  872. ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  873. WC_SHA3_512_BLOCK_SIZE);
  874. if (ret != 0)
  875. break;
  876. ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  877. WC_SHA3_512_DIGEST_SIZE);
  878. if (ret != 0)
  879. break;
  880. ret = wc_Sha3_512_Final(&hmac->hash.sha3, hash);
  881. break;
  882. #endif
  883. #endif /* WOLFSSL_SHA3 */
  884. default:
  885. ret = BAD_FUNC_ARG;
  886. break;
  887. }
  888. if (ret == 0) {
  889. hmac->innerHashKeyed = 0;
  890. }
  891. return ret;
  892. }
  893. #ifdef WOLFSSL_KCAPI_HMAC
  894. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hmac.c */
  895. /* unmap the _Software calls used by kcapi_hmac.c */
  896. #undef wc_HmacSetKey
  897. #undef wc_HmacUpdate
  898. #undef wc_HmacFinal
  899. #else
  900. /* Initialize Hmac for use with async device */
  901. int wc_HmacInit(Hmac* hmac, void* heap, int devId)
  902. {
  903. int ret = 0;
  904. if (hmac == NULL)
  905. return BAD_FUNC_ARG;
  906. XMEMSET(hmac, 0, sizeof(Hmac));
  907. hmac->macType = WC_HASH_TYPE_NONE;
  908. hmac->heap = heap;
  909. #ifdef WOLF_CRYPTO_CB
  910. hmac->devId = devId;
  911. hmac->devCtx = NULL;
  912. #endif
  913. #if defined(WOLFSSL_DEVCRYPTO_HMAC)
  914. hmac->ctx.cfd = -1;
  915. #endif
  916. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  917. ret = wolfAsync_DevCtxInit(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC,
  918. hmac->heap, devId);
  919. #else
  920. (void)devId;
  921. #endif /* WOLFSSL_ASYNC_CRYPT */
  922. return ret;
  923. }
  924. #ifdef WOLF_PRIVATE_KEY_ID
  925. int wc_HmacInit_Id(Hmac* hmac, unsigned char* id, int len, void* heap,
  926. int devId)
  927. {
  928. int ret = 0;
  929. if (hmac == NULL)
  930. ret = BAD_FUNC_ARG;
  931. if (ret == 0 && (len < 0 || len > HMAC_MAX_ID_LEN))
  932. ret = BUFFER_E;
  933. if (ret == 0)
  934. ret = wc_HmacInit(hmac, heap, devId);
  935. if (ret == 0) {
  936. XMEMCPY(hmac->id, id, len);
  937. hmac->idLen = len;
  938. }
  939. return ret;
  940. }
  941. int wc_HmacInit_Label(Hmac* hmac, const char* label, void* heap, int devId)
  942. {
  943. int ret = 0;
  944. int labelLen = 0;
  945. if (hmac == NULL || label == NULL)
  946. ret = BAD_FUNC_ARG;
  947. if (ret == 0) {
  948. labelLen = (int)XSTRLEN(label);
  949. if (labelLen == 0 || labelLen > HMAC_MAX_LABEL_LEN)
  950. ret = BUFFER_E;
  951. }
  952. if (ret == 0)
  953. ret = wc_HmacInit(hmac, heap, devId);
  954. if (ret == 0) {
  955. XMEMCPY(hmac->label, label, labelLen);
  956. hmac->labelLen = labelLen;
  957. }
  958. return ret;
  959. }
  960. #endif /* WOLF_PRIVATE_KEY_ID */
  961. /* Free Hmac from use with async device */
  962. void wc_HmacFree(Hmac* hmac)
  963. {
  964. if (hmac == NULL)
  965. return;
  966. #ifdef WOLF_CRYPTO_CB
  967. /* handle cleanup case where final is not called */
  968. if (hmac->devId != INVALID_DEVID && hmac->devCtx != NULL) {
  969. int ret;
  970. byte finalHash[WC_HMAC_BLOCK_SIZE];
  971. ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, finalHash);
  972. (void)ret; /* must ignore return code here */
  973. (void)finalHash;
  974. }
  975. #endif
  976. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  977. wolfAsync_DevCtxFree(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC);
  978. #endif /* WOLFSSL_ASYNC_CRYPT */
  979. switch (hmac->macType) {
  980. #ifndef NO_MD5
  981. case WC_MD5:
  982. wc_Md5Free(&hmac->hash.md5);
  983. break;
  984. #endif /* !NO_MD5 */
  985. #ifndef NO_SHA
  986. case WC_SHA:
  987. wc_ShaFree(&hmac->hash.sha);
  988. break;
  989. #endif /* !NO_SHA */
  990. #ifdef WOLFSSL_SHA224
  991. case WC_SHA224:
  992. wc_Sha224Free(&hmac->hash.sha224);
  993. break;
  994. #endif /* WOLFSSL_SHA224 */
  995. #ifndef NO_SHA256
  996. case WC_SHA256:
  997. wc_Sha256Free(&hmac->hash.sha256);
  998. break;
  999. #endif /* !NO_SHA256 */
  1000. #ifdef WOLFSSL_SHA384
  1001. case WC_SHA384:
  1002. wc_Sha384Free(&hmac->hash.sha384);
  1003. break;
  1004. #endif /* WOLFSSL_SHA384 */
  1005. #ifdef WOLFSSL_SHA512
  1006. case WC_SHA512:
  1007. wc_Sha512Free(&hmac->hash.sha512);
  1008. break;
  1009. #endif /* WOLFSSL_SHA512 */
  1010. #ifdef WOLFSSL_SHA3
  1011. #ifndef WOLFSSL_NOSHA3_224
  1012. case WC_SHA3_224:
  1013. wc_Sha3_224_Free(&hmac->hash.sha3);
  1014. break;
  1015. #endif
  1016. #ifndef WOLFSSL_NOSHA3_256
  1017. case WC_SHA3_256:
  1018. wc_Sha3_256_Free(&hmac->hash.sha3);
  1019. break;
  1020. #endif
  1021. #ifndef WOLFSSL_NOSHA3_384
  1022. case WC_SHA3_384:
  1023. wc_Sha3_384_Free(&hmac->hash.sha3);
  1024. break;
  1025. #endif
  1026. #ifndef WOLFSSL_NOSHA3_512
  1027. case WC_SHA3_512:
  1028. wc_Sha3_512_Free(&hmac->hash.sha3);
  1029. break;
  1030. #endif
  1031. #endif /* WOLFSSL_SHA3 */
  1032. default:
  1033. break;
  1034. }
  1035. }
  1036. #endif /* WOLFSSL_KCAPI_HMAC */
  1037. int wolfSSL_GetHmacMaxSize(void)
  1038. {
  1039. return WC_MAX_DIGEST_SIZE;
  1040. }
  1041. #ifdef HAVE_HKDF
  1042. /* HMAC-KDF-Extract.
  1043. * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
  1044. *
  1045. * type The hash algorithm type.
  1046. * salt The optional salt value.
  1047. * saltSz The size of the salt.
  1048. * inKey The input keying material.
  1049. * inKeySz The size of the input keying material.
  1050. * out The pseudorandom key with the length that of the hash.
  1051. * returns 0 on success, otherwise failure.
  1052. */
  1053. int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
  1054. const byte* inKey, word32 inKeySz, byte* out)
  1055. {
  1056. byte tmp[WC_MAX_DIGEST_SIZE]; /* localSalt helper */
  1057. #ifdef WOLFSSL_SMALL_STACK
  1058. Hmac* myHmac;
  1059. #else
  1060. Hmac myHmac[1];
  1061. #endif
  1062. int ret;
  1063. const byte* localSalt; /* either points to user input or tmp */
  1064. int hashSz;
  1065. ret = wc_HmacSizeByType(type);
  1066. if (ret < 0) {
  1067. return ret;
  1068. }
  1069. #ifdef WOLFSSL_SMALL_STACK
  1070. myHmac = (Hmac*)XMALLOC(sizeof(Hmac), NULL, DYNAMIC_TYPE_HMAC);
  1071. if (myHmac == NULL) {
  1072. return MEMORY_E;
  1073. }
  1074. #endif
  1075. hashSz = ret;
  1076. localSalt = salt;
  1077. if (localSalt == NULL) {
  1078. XMEMSET(tmp, 0, hashSz);
  1079. localSalt = tmp;
  1080. saltSz = hashSz;
  1081. }
  1082. ret = wc_HmacInit(myHmac, NULL, INVALID_DEVID);
  1083. if (ret == 0) {
  1084. ret = wc_HmacSetKey(myHmac, type, localSalt, saltSz);
  1085. if (ret == 0)
  1086. ret = wc_HmacUpdate(myHmac, inKey, inKeySz);
  1087. if (ret == 0)
  1088. ret = wc_HmacFinal(myHmac, out);
  1089. wc_HmacFree(myHmac);
  1090. }
  1091. #ifdef WOLFSSL_SMALL_STACK
  1092. XFREE(myHmac, NULL, DYNAMIC_TYPE_HMAC);
  1093. #endif
  1094. return ret;
  1095. }
  1096. /* HMAC-KDF-Expand.
  1097. * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
  1098. *
  1099. * type The hash algorithm type.
  1100. * inKey The input key.
  1101. * inKeySz The size of the input key.
  1102. * info The application specific information.
  1103. * infoSz The size of the application specific information.
  1104. * out The output keying material.
  1105. * returns 0 on success, otherwise failure.
  1106. */
  1107. int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
  1108. const byte* info, word32 infoSz, byte* out, word32 outSz)
  1109. {
  1110. byte tmp[WC_MAX_DIGEST_SIZE];
  1111. #ifdef WOLFSSL_SMALL_STACK
  1112. Hmac* myHmac;
  1113. #else
  1114. Hmac myHmac[1];
  1115. #endif
  1116. int ret = 0;
  1117. word32 outIdx = 0;
  1118. word32 hashSz = wc_HmacSizeByType(type);
  1119. byte n = 0x1;
  1120. /* RFC 5869 states that the length of output keying material in
  1121. * octets must be L <= 255*HashLen or N = ceil(L/HashLen) */
  1122. if (out == NULL || ((outSz/hashSz) + ((outSz % hashSz) != 0)) > 255) {
  1123. return BAD_FUNC_ARG;
  1124. }
  1125. #ifdef WOLFSSL_SMALL_STACK
  1126. myHmac = (Hmac*)XMALLOC(sizeof(Hmac), NULL, DYNAMIC_TYPE_HMAC);
  1127. if (myHmac == NULL) {
  1128. return MEMORY_E;
  1129. }
  1130. #endif
  1131. ret = wc_HmacInit(myHmac, NULL, INVALID_DEVID);
  1132. if (ret != 0) {
  1133. #ifdef WOLFSSL_SMALL_STACK
  1134. XFREE(myHmac, NULL, DYNAMIC_TYPE_HMAC);
  1135. #endif
  1136. return ret;
  1137. }
  1138. while (outIdx < outSz) {
  1139. int tmpSz = (n == 1) ? 0 : hashSz;
  1140. word32 left = outSz - outIdx;
  1141. ret = wc_HmacSetKey(myHmac, type, inKey, inKeySz);
  1142. if (ret != 0)
  1143. break;
  1144. ret = wc_HmacUpdate(myHmac, tmp, tmpSz);
  1145. if (ret != 0)
  1146. break;
  1147. ret = wc_HmacUpdate(myHmac, info, infoSz);
  1148. if (ret != 0)
  1149. break;
  1150. ret = wc_HmacUpdate(myHmac, &n, 1);
  1151. if (ret != 0)
  1152. break;
  1153. ret = wc_HmacFinal(myHmac, tmp);
  1154. if (ret != 0)
  1155. break;
  1156. left = min(left, hashSz);
  1157. XMEMCPY(out+outIdx, tmp, left);
  1158. outIdx += hashSz;
  1159. n++;
  1160. }
  1161. wc_HmacFree(myHmac);
  1162. #ifdef WOLFSSL_SMALL_STACK
  1163. XFREE(myHmac, NULL, DYNAMIC_TYPE_HMAC);
  1164. #endif
  1165. return ret;
  1166. }
  1167. /* HMAC-KDF.
  1168. * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
  1169. *
  1170. * type The hash algorithm type.
  1171. * inKey The input keying material.
  1172. * inKeySz The size of the input keying material.
  1173. * salt The optional salt value.
  1174. * saltSz The size of the salt.
  1175. * info The application specific information.
  1176. * infoSz The size of the application specific information.
  1177. * out The output keying material.
  1178. * returns 0 on success, otherwise failure.
  1179. */
  1180. int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
  1181. const byte* salt, word32 saltSz,
  1182. const byte* info, word32 infoSz,
  1183. byte* out, word32 outSz)
  1184. {
  1185. byte prk[WC_MAX_DIGEST_SIZE];
  1186. int hashSz = wc_HmacSizeByType(type);
  1187. int ret;
  1188. if (hashSz < 0)
  1189. return BAD_FUNC_ARG;
  1190. ret = wc_HKDF_Extract(type, salt, saltSz, inKey, inKeySz, prk);
  1191. if (ret != 0)
  1192. return ret;
  1193. return wc_HKDF_Expand(type, prk, hashSz, info, infoSz, out, outSz);
  1194. }
  1195. #endif /* HAVE_HKDF */
  1196. #endif /* HAVE_FIPS */
  1197. #endif /* NO_HMAC */