hmac.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. byte* ip;
  253. byte* op;
  254. word32 i, hmac_block_size = 0;
  255. int ret = 0;
  256. void* heap = NULL;
  257. if (hmac == NULL || (key == NULL && length != 0) ||
  258. !(type == WC_MD5 || type == WC_SHA ||
  259. type == WC_SHA224 || type == WC_SHA256 ||
  260. type == WC_SHA384 || type == WC_SHA512 ||
  261. type == WC_SHA3_224 || type == WC_SHA3_256 ||
  262. type == WC_SHA3_384 || type == WC_SHA3_512)) {
  263. return BAD_FUNC_ARG;
  264. }
  265. #ifndef HAVE_FIPS
  266. /* if set key has already been run then make sure and free existing */
  267. /* This is for async and PIC32MZ situations, and just normally OK,
  268. provided the user calls wc_HmacInit() first. That function is not
  269. available in FIPS builds. In current FIPS builds, the hashes are
  270. not allocating resources. */
  271. if (hmac->macType != WC_HASH_TYPE_NONE) {
  272. wc_HmacFree(hmac);
  273. }
  274. #endif
  275. hmac->innerHashKeyed = 0;
  276. hmac->macType = (byte)type;
  277. ret = _InitHmac(hmac, type, heap);
  278. if (ret != 0)
  279. return ret;
  280. #ifdef HAVE_FIPS
  281. if (length < HMAC_FIPS_MIN_KEY) {
  282. WOLFSSL_ERROR_VERBOSE(HMAC_MIN_KEYLEN_E);
  283. return HMAC_MIN_KEYLEN_E;
  284. }
  285. #endif
  286. #ifdef WOLF_CRYPTO_CB
  287. hmac->keyRaw = key; /* use buffer directly */
  288. hmac->keyLen = length;
  289. #endif
  290. ip = (byte*)hmac->ipad;
  291. op = (byte*)hmac->opad;
  292. switch (hmac->macType) {
  293. #ifndef NO_MD5
  294. case WC_MD5:
  295. hmac_block_size = WC_MD5_BLOCK_SIZE;
  296. if (length <= WC_MD5_BLOCK_SIZE) {
  297. if (key != NULL) {
  298. XMEMCPY(ip, key, length);
  299. }
  300. }
  301. else {
  302. ret = wc_Md5Update(&hmac->hash.md5, key, length);
  303. if (ret != 0)
  304. break;
  305. ret = wc_Md5Final(&hmac->hash.md5, ip);
  306. if (ret != 0)
  307. break;
  308. length = WC_MD5_DIGEST_SIZE;
  309. }
  310. break;
  311. #endif /* !NO_MD5 */
  312. #ifndef NO_SHA
  313. case WC_SHA:
  314. hmac_block_size = WC_SHA_BLOCK_SIZE;
  315. if (length <= WC_SHA_BLOCK_SIZE) {
  316. if (key != NULL) {
  317. XMEMCPY(ip, key, length);
  318. }
  319. }
  320. else {
  321. ret = wc_ShaUpdate(&hmac->hash.sha, key, length);
  322. if (ret != 0)
  323. break;
  324. ret = wc_ShaFinal(&hmac->hash.sha, ip);
  325. if (ret != 0)
  326. break;
  327. length = WC_SHA_DIGEST_SIZE;
  328. }
  329. break;
  330. #endif /* !NO_SHA */
  331. #ifdef WOLFSSL_SHA224
  332. case WC_SHA224:
  333. hmac_block_size = WC_SHA224_BLOCK_SIZE;
  334. if (length <= WC_SHA224_BLOCK_SIZE) {
  335. if (key != NULL) {
  336. XMEMCPY(ip, key, length);
  337. }
  338. }
  339. else {
  340. ret = wc_Sha224Update(&hmac->hash.sha224, key, length);
  341. if (ret != 0)
  342. break;
  343. ret = wc_Sha224Final(&hmac->hash.sha224, ip);
  344. if (ret != 0)
  345. break;
  346. length = WC_SHA224_DIGEST_SIZE;
  347. }
  348. break;
  349. #endif /* WOLFSSL_SHA224 */
  350. #ifndef NO_SHA256
  351. case WC_SHA256:
  352. hmac_block_size = WC_SHA256_BLOCK_SIZE;
  353. if (length <= WC_SHA256_BLOCK_SIZE) {
  354. if (key != NULL) {
  355. XMEMCPY(ip, key, length);
  356. }
  357. }
  358. else {
  359. ret = wc_Sha256Update(&hmac->hash.sha256, key, length);
  360. if (ret != 0)
  361. break;
  362. ret = wc_Sha256Final(&hmac->hash.sha256, ip);
  363. if (ret != 0)
  364. break;
  365. length = WC_SHA256_DIGEST_SIZE;
  366. }
  367. break;
  368. #endif /* !NO_SHA256 */
  369. #ifdef WOLFSSL_SHA384
  370. case WC_SHA384:
  371. hmac_block_size = WC_SHA384_BLOCK_SIZE;
  372. if (length <= WC_SHA384_BLOCK_SIZE) {
  373. if (key != NULL) {
  374. XMEMCPY(ip, key, length);
  375. }
  376. }
  377. else {
  378. ret = wc_Sha384Update(&hmac->hash.sha384, key, length);
  379. if (ret != 0)
  380. break;
  381. ret = wc_Sha384Final(&hmac->hash.sha384, ip);
  382. if (ret != 0)
  383. break;
  384. length = WC_SHA384_DIGEST_SIZE;
  385. }
  386. break;
  387. #endif /* WOLFSSL_SHA384 */
  388. #ifdef WOLFSSL_SHA512
  389. case WC_SHA512:
  390. hmac_block_size = WC_SHA512_BLOCK_SIZE;
  391. if (length <= WC_SHA512_BLOCK_SIZE) {
  392. if (key != NULL) {
  393. XMEMCPY(ip, key, length);
  394. }
  395. }
  396. else {
  397. ret = wc_Sha512Update(&hmac->hash.sha512, key, length);
  398. if (ret != 0)
  399. break;
  400. ret = wc_Sha512Final(&hmac->hash.sha512, ip);
  401. if (ret != 0)
  402. break;
  403. length = WC_SHA512_DIGEST_SIZE;
  404. }
  405. break;
  406. #endif /* WOLFSSL_SHA512 */
  407. #ifdef WOLFSSL_SHA3
  408. #ifndef WOLFSSL_NOSHA3_224
  409. case WC_SHA3_224:
  410. hmac_block_size = WC_SHA3_224_BLOCK_SIZE;
  411. if (length <= WC_SHA3_224_BLOCK_SIZE) {
  412. if (key != NULL) {
  413. XMEMCPY(ip, key, length);
  414. }
  415. }
  416. else {
  417. ret = wc_Sha3_224_Update(&hmac->hash.sha3, key, length);
  418. if (ret != 0)
  419. break;
  420. ret = wc_Sha3_224_Final(&hmac->hash.sha3, ip);
  421. if (ret != 0)
  422. break;
  423. length = WC_SHA3_224_DIGEST_SIZE;
  424. }
  425. break;
  426. #endif
  427. #ifndef WOLFSSL_NOSHA3_256
  428. case WC_SHA3_256:
  429. hmac_block_size = WC_SHA3_256_BLOCK_SIZE;
  430. if (length <= WC_SHA3_256_BLOCK_SIZE) {
  431. if (key != NULL) {
  432. XMEMCPY(ip, key, length);
  433. }
  434. }
  435. else {
  436. ret = wc_Sha3_256_Update(&hmac->hash.sha3, key, length);
  437. if (ret != 0)
  438. break;
  439. ret = wc_Sha3_256_Final(&hmac->hash.sha3, ip);
  440. if (ret != 0)
  441. break;
  442. length = WC_SHA3_256_DIGEST_SIZE;
  443. }
  444. break;
  445. #endif
  446. #ifndef WOLFSSL_NOSHA3_384
  447. case WC_SHA3_384:
  448. hmac_block_size = WC_SHA3_384_BLOCK_SIZE;
  449. if (length <= WC_SHA3_384_BLOCK_SIZE) {
  450. if (key != NULL) {
  451. XMEMCPY(ip, key, length);
  452. }
  453. }
  454. else {
  455. ret = wc_Sha3_384_Update(&hmac->hash.sha3, key, length);
  456. if (ret != 0)
  457. break;
  458. ret = wc_Sha3_384_Final(&hmac->hash.sha3, ip);
  459. if (ret != 0)
  460. break;
  461. length = WC_SHA3_384_DIGEST_SIZE;
  462. }
  463. break;
  464. #endif
  465. #ifndef WOLFSSL_NOSHA3_512
  466. case WC_SHA3_512:
  467. hmac_block_size = WC_SHA3_512_BLOCK_SIZE;
  468. if (length <= WC_SHA3_512_BLOCK_SIZE) {
  469. if (key != NULL) {
  470. XMEMCPY(ip, key, length);
  471. }
  472. }
  473. else {
  474. ret = wc_Sha3_512_Update(&hmac->hash.sha3, key, length);
  475. if (ret != 0)
  476. break;
  477. ret = wc_Sha3_512_Final(&hmac->hash.sha3, ip);
  478. if (ret != 0)
  479. break;
  480. length = WC_SHA3_512_DIGEST_SIZE;
  481. }
  482. break;
  483. #endif
  484. #endif /* WOLFSSL_SHA3 */
  485. default:
  486. return BAD_FUNC_ARG;
  487. }
  488. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  489. if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
  490. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  491. #ifdef HAVE_INTEL_QA
  492. if (IntelQaHmacGetType(hmac->macType, NULL) == 0)
  493. #endif
  494. {
  495. if (length > hmac_block_size)
  496. length = hmac_block_size;
  497. /* update key length */
  498. hmac->keyLen = (word16)length;
  499. return ret;
  500. }
  501. /* no need to pad below */
  502. #endif
  503. }
  504. #endif
  505. if (ret == 0) {
  506. if (length < hmac_block_size)
  507. XMEMSET(ip + length, 0, hmac_block_size - length);
  508. for(i = 0; i < hmac_block_size; i++) {
  509. op[i] = ip[i] ^ OPAD;
  510. ip[i] ^= IPAD;
  511. }
  512. }
  513. return ret;
  514. }
  515. static int HmacKeyInnerHash(Hmac* hmac)
  516. {
  517. int ret = 0;
  518. switch (hmac->macType) {
  519. #ifndef NO_MD5
  520. case WC_MD5:
  521. ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->ipad,
  522. WC_MD5_BLOCK_SIZE);
  523. break;
  524. #endif /* !NO_MD5 */
  525. #ifndef NO_SHA
  526. case WC_SHA:
  527. ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->ipad,
  528. WC_SHA_BLOCK_SIZE);
  529. break;
  530. #endif /* !NO_SHA */
  531. #ifdef WOLFSSL_SHA224
  532. case WC_SHA224:
  533. ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->ipad,
  534. WC_SHA224_BLOCK_SIZE);
  535. break;
  536. #endif /* WOLFSSL_SHA224 */
  537. #ifndef NO_SHA256
  538. case WC_SHA256:
  539. ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->ipad,
  540. WC_SHA256_BLOCK_SIZE);
  541. break;
  542. #endif /* !NO_SHA256 */
  543. #ifdef WOLFSSL_SHA384
  544. case WC_SHA384:
  545. ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->ipad,
  546. WC_SHA384_BLOCK_SIZE);
  547. break;
  548. #endif /* WOLFSSL_SHA384 */
  549. #ifdef WOLFSSL_SHA512
  550. case WC_SHA512:
  551. ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->ipad,
  552. WC_SHA512_BLOCK_SIZE);
  553. break;
  554. #endif /* WOLFSSL_SHA512 */
  555. #ifdef WOLFSSL_SHA3
  556. #ifndef WOLFSSL_NOSHA3_224
  557. case WC_SHA3_224:
  558. ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  559. WC_SHA3_224_BLOCK_SIZE);
  560. break;
  561. #endif
  562. #ifndef WOLFSSL_NOSHA3_256
  563. case WC_SHA3_256:
  564. ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  565. WC_SHA3_256_BLOCK_SIZE);
  566. break;
  567. #endif
  568. #ifndef WOLFSSL_NOSHA3_384
  569. case WC_SHA3_384:
  570. ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  571. WC_SHA3_384_BLOCK_SIZE);
  572. break;
  573. #endif
  574. #ifndef WOLFSSL_NOSHA3_512
  575. case WC_SHA3_512:
  576. ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  577. WC_SHA3_512_BLOCK_SIZE);
  578. break;
  579. #endif
  580. #endif /* WOLFSSL_SHA3 */
  581. default:
  582. break;
  583. }
  584. if (ret == 0)
  585. hmac->innerHashKeyed = WC_HMAC_INNER_HASH_KEYED_SW;
  586. return ret;
  587. }
  588. int wc_HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
  589. {
  590. int ret = 0;
  591. if (hmac == NULL || (msg == NULL && length > 0)) {
  592. return BAD_FUNC_ARG;
  593. }
  594. #ifdef WOLF_CRYPTO_CB
  595. if (hmac->devId != INVALID_DEVID) {
  596. ret = wc_CryptoCb_Hmac(hmac, hmac->macType, msg, length, NULL);
  597. if (ret != CRYPTOCB_UNAVAILABLE)
  598. return ret;
  599. /* fall-through when unavailable */
  600. ret = 0; /* reset error code */
  601. }
  602. #endif
  603. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  604. if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
  605. #if defined(HAVE_CAVIUM)
  606. return NitroxHmacUpdate(hmac, msg, length);
  607. #elif defined(HAVE_INTEL_QA)
  608. if (IntelQaHmacGetType(hmac->macType, NULL) == 0) {
  609. return IntelQaHmac(&hmac->asyncDev, hmac->macType,
  610. (byte*)hmac->ipad, hmac->keyLen, NULL, msg, length);
  611. }
  612. #endif
  613. }
  614. #endif /* WOLFSSL_ASYNC_CRYPT */
  615. if (!hmac->innerHashKeyed) {
  616. ret = HmacKeyInnerHash(hmac);
  617. if (ret != 0)
  618. return ret;
  619. }
  620. switch (hmac->macType) {
  621. #ifndef NO_MD5
  622. case WC_MD5:
  623. ret = wc_Md5Update(&hmac->hash.md5, msg, length);
  624. break;
  625. #endif /* !NO_MD5 */
  626. #ifndef NO_SHA
  627. case WC_SHA:
  628. ret = wc_ShaUpdate(&hmac->hash.sha, msg, length);
  629. break;
  630. #endif /* !NO_SHA */
  631. #ifdef WOLFSSL_SHA224
  632. case WC_SHA224:
  633. ret = wc_Sha224Update(&hmac->hash.sha224, msg, length);
  634. break;
  635. #endif /* WOLFSSL_SHA224 */
  636. #ifndef NO_SHA256
  637. case WC_SHA256:
  638. ret = wc_Sha256Update(&hmac->hash.sha256, msg, length);
  639. break;
  640. #endif /* !NO_SHA256 */
  641. #ifdef WOLFSSL_SHA384
  642. case WC_SHA384:
  643. ret = wc_Sha384Update(&hmac->hash.sha384, msg, length);
  644. break;
  645. #endif /* WOLFSSL_SHA384 */
  646. #ifdef WOLFSSL_SHA512
  647. case WC_SHA512:
  648. ret = wc_Sha512Update(&hmac->hash.sha512, msg, length);
  649. break;
  650. #endif /* WOLFSSL_SHA512 */
  651. #ifdef WOLFSSL_SHA3
  652. #ifndef WOLFSSL_NOSHA3_224
  653. case WC_SHA3_224:
  654. ret = wc_Sha3_224_Update(&hmac->hash.sha3, msg, length);
  655. break;
  656. #endif
  657. #ifndef WOLFSSL_NOSHA3_256
  658. case WC_SHA3_256:
  659. ret = wc_Sha3_256_Update(&hmac->hash.sha3, msg, length);
  660. break;
  661. #endif
  662. #ifndef WOLFSSL_NOSHA3_384
  663. case WC_SHA3_384:
  664. ret = wc_Sha3_384_Update(&hmac->hash.sha3, msg, length);
  665. break;
  666. #endif
  667. #ifndef WOLFSSL_NOSHA3_512
  668. case WC_SHA3_512:
  669. ret = wc_Sha3_512_Update(&hmac->hash.sha3, msg, length);
  670. break;
  671. #endif
  672. #endif /* WOLFSSL_SHA3 */
  673. default:
  674. break;
  675. }
  676. return ret;
  677. }
  678. int wc_HmacFinal(Hmac* hmac, byte* hash)
  679. {
  680. int ret;
  681. if (hmac == NULL || hash == NULL) {
  682. return BAD_FUNC_ARG;
  683. }
  684. #ifdef WOLF_CRYPTO_CB
  685. if (hmac->devId != INVALID_DEVID) {
  686. ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, hash);
  687. if (ret != CRYPTOCB_UNAVAILABLE)
  688. return ret;
  689. /* fall-through when unavailable */
  690. }
  691. #endif
  692. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  693. if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
  694. int hashLen = wc_HmacSizeByType(hmac->macType);
  695. if (hashLen <= 0)
  696. return hashLen;
  697. #if defined(HAVE_CAVIUM)
  698. return NitroxHmacFinal(hmac, hash, hashLen);
  699. #elif defined(HAVE_INTEL_QA)
  700. if (IntelQaHmacGetType(hmac->macType, NULL) == 0) {
  701. return IntelQaHmac(&hmac->asyncDev, hmac->macType,
  702. (byte*)hmac->ipad, hmac->keyLen, hash, NULL, hashLen);
  703. }
  704. #endif
  705. }
  706. #endif /* WOLFSSL_ASYNC_CRYPT */
  707. if (!hmac->innerHashKeyed) {
  708. ret = HmacKeyInnerHash(hmac);
  709. if (ret != 0)
  710. return ret;
  711. }
  712. switch (hmac->macType) {
  713. #ifndef NO_MD5
  714. case WC_MD5:
  715. ret = wc_Md5Final(&hmac->hash.md5, (byte*)hmac->innerHash);
  716. if (ret != 0)
  717. break;
  718. ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->opad,
  719. WC_MD5_BLOCK_SIZE);
  720. if (ret != 0)
  721. break;
  722. ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->innerHash,
  723. WC_MD5_DIGEST_SIZE);
  724. if (ret != 0)
  725. break;
  726. ret = wc_Md5Final(&hmac->hash.md5, hash);
  727. break;
  728. #endif /* !NO_MD5 */
  729. #ifndef NO_SHA
  730. case WC_SHA:
  731. ret = wc_ShaFinal(&hmac->hash.sha, (byte*)hmac->innerHash);
  732. if (ret != 0)
  733. break;
  734. ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->opad,
  735. WC_SHA_BLOCK_SIZE);
  736. if (ret != 0)
  737. break;
  738. ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->innerHash,
  739. WC_SHA_DIGEST_SIZE);
  740. if (ret != 0)
  741. break;
  742. ret = wc_ShaFinal(&hmac->hash.sha, hash);
  743. break;
  744. #endif /* !NO_SHA */
  745. #ifdef WOLFSSL_SHA224
  746. case WC_SHA224:
  747. ret = wc_Sha224Final(&hmac->hash.sha224, (byte*)hmac->innerHash);
  748. if (ret != 0)
  749. break;
  750. ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->opad,
  751. WC_SHA224_BLOCK_SIZE);
  752. if (ret != 0)
  753. break;
  754. ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->innerHash,
  755. WC_SHA224_DIGEST_SIZE);
  756. if (ret != 0)
  757. break;
  758. ret = wc_Sha224Final(&hmac->hash.sha224, hash);
  759. if (ret != 0)
  760. break;
  761. break;
  762. #endif /* WOLFSSL_SHA224 */
  763. #ifndef NO_SHA256
  764. case WC_SHA256:
  765. ret = wc_Sha256Final(&hmac->hash.sha256, (byte*)hmac->innerHash);
  766. if (ret != 0)
  767. break;
  768. ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->opad,
  769. WC_SHA256_BLOCK_SIZE);
  770. if (ret != 0)
  771. break;
  772. ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->innerHash,
  773. WC_SHA256_DIGEST_SIZE);
  774. if (ret != 0)
  775. break;
  776. ret = wc_Sha256Final(&hmac->hash.sha256, hash);
  777. break;
  778. #endif /* !NO_SHA256 */
  779. #ifdef WOLFSSL_SHA384
  780. case WC_SHA384:
  781. ret = wc_Sha384Final(&hmac->hash.sha384, (byte*)hmac->innerHash);
  782. if (ret != 0)
  783. break;
  784. ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->opad,
  785. WC_SHA384_BLOCK_SIZE);
  786. if (ret != 0)
  787. break;
  788. ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->innerHash,
  789. WC_SHA384_DIGEST_SIZE);
  790. if (ret != 0)
  791. break;
  792. ret = wc_Sha384Final(&hmac->hash.sha384, hash);
  793. break;
  794. #endif /* WOLFSSL_SHA384 */
  795. #ifdef WOLFSSL_SHA512
  796. case WC_SHA512:
  797. ret = wc_Sha512Final(&hmac->hash.sha512, (byte*)hmac->innerHash);
  798. if (ret != 0)
  799. break;
  800. ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->opad,
  801. WC_SHA512_BLOCK_SIZE);
  802. if (ret != 0)
  803. break;
  804. ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->innerHash,
  805. WC_SHA512_DIGEST_SIZE);
  806. if (ret != 0)
  807. break;
  808. ret = wc_Sha512Final(&hmac->hash.sha512, hash);
  809. break;
  810. #endif /* WOLFSSL_SHA512 */
  811. #ifdef WOLFSSL_SHA3
  812. #ifndef WOLFSSL_NOSHA3_224
  813. case WC_SHA3_224:
  814. ret = wc_Sha3_224_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  815. if (ret != 0)
  816. break;
  817. ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  818. WC_SHA3_224_BLOCK_SIZE);
  819. if (ret != 0)
  820. break;
  821. ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  822. WC_SHA3_224_DIGEST_SIZE);
  823. if (ret != 0)
  824. break;
  825. ret = wc_Sha3_224_Final(&hmac->hash.sha3, hash);
  826. break;
  827. #endif
  828. #ifndef WOLFSSL_NOSHA3_256
  829. case WC_SHA3_256:
  830. ret = wc_Sha3_256_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  831. if (ret != 0)
  832. break;
  833. ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  834. WC_SHA3_256_BLOCK_SIZE);
  835. if (ret != 0)
  836. break;
  837. ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  838. WC_SHA3_256_DIGEST_SIZE);
  839. if (ret != 0)
  840. break;
  841. ret = wc_Sha3_256_Final(&hmac->hash.sha3, hash);
  842. break;
  843. #endif
  844. #ifndef WOLFSSL_NOSHA3_384
  845. case WC_SHA3_384:
  846. ret = wc_Sha3_384_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  847. if (ret != 0)
  848. break;
  849. ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  850. WC_SHA3_384_BLOCK_SIZE);
  851. if (ret != 0)
  852. break;
  853. ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  854. WC_SHA3_384_DIGEST_SIZE);
  855. if (ret != 0)
  856. break;
  857. ret = wc_Sha3_384_Final(&hmac->hash.sha3, hash);
  858. break;
  859. #endif
  860. #ifndef WOLFSSL_NOSHA3_512
  861. case WC_SHA3_512:
  862. ret = wc_Sha3_512_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  863. if (ret != 0)
  864. break;
  865. ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  866. WC_SHA3_512_BLOCK_SIZE);
  867. if (ret != 0)
  868. break;
  869. ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  870. WC_SHA3_512_DIGEST_SIZE);
  871. if (ret != 0)
  872. break;
  873. ret = wc_Sha3_512_Final(&hmac->hash.sha3, hash);
  874. break;
  875. #endif
  876. #endif /* WOLFSSL_SHA3 */
  877. default:
  878. ret = BAD_FUNC_ARG;
  879. break;
  880. }
  881. if (ret == 0) {
  882. hmac->innerHashKeyed = 0;
  883. }
  884. return ret;
  885. }
  886. #ifdef WOLFSSL_KCAPI_HMAC
  887. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hmac.c */
  888. /* unmap the _Software calls used by kcapi_hmac.c */
  889. #undef wc_HmacSetKey
  890. #undef wc_HmacUpdate
  891. #undef wc_HmacFinal
  892. #else
  893. /* Initialize Hmac for use with async device */
  894. int wc_HmacInit(Hmac* hmac, void* heap, int devId)
  895. {
  896. int ret = 0;
  897. if (hmac == NULL)
  898. return BAD_FUNC_ARG;
  899. XMEMSET(hmac, 0, sizeof(Hmac));
  900. hmac->macType = WC_HASH_TYPE_NONE;
  901. hmac->heap = heap;
  902. #ifdef WOLF_CRYPTO_CB
  903. hmac->devId = devId;
  904. hmac->devCtx = NULL;
  905. #endif
  906. #if defined(WOLFSSL_DEVCRYPTO_HMAC)
  907. hmac->ctx.cfd = -1;
  908. #endif
  909. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  910. ret = wolfAsync_DevCtxInit(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC,
  911. hmac->heap, devId);
  912. #else
  913. (void)devId;
  914. #endif /* WOLFSSL_ASYNC_CRYPT */
  915. return ret;
  916. }
  917. #ifdef WOLF_PRIVATE_KEY_ID
  918. int wc_HmacInit_Id(Hmac* hmac, unsigned char* id, int len, void* heap,
  919. int devId)
  920. {
  921. int ret = 0;
  922. if (hmac == NULL)
  923. ret = BAD_FUNC_ARG;
  924. if (ret == 0 && (len < 0 || len > HMAC_MAX_ID_LEN))
  925. ret = BUFFER_E;
  926. if (ret == 0)
  927. ret = wc_HmacInit(hmac, heap, devId);
  928. if (ret == 0) {
  929. XMEMCPY(hmac->id, id, len);
  930. hmac->idLen = len;
  931. }
  932. return ret;
  933. }
  934. int wc_HmacInit_Label(Hmac* hmac, const char* label, void* heap, int devId)
  935. {
  936. int ret = 0;
  937. int labelLen = 0;
  938. if (hmac == NULL || label == NULL)
  939. ret = BAD_FUNC_ARG;
  940. if (ret == 0) {
  941. labelLen = (int)XSTRLEN(label);
  942. if (labelLen == 0 || labelLen > HMAC_MAX_LABEL_LEN)
  943. ret = BUFFER_E;
  944. }
  945. if (ret == 0)
  946. ret = wc_HmacInit(hmac, heap, devId);
  947. if (ret == 0) {
  948. XMEMCPY(hmac->label, label, labelLen);
  949. hmac->labelLen = labelLen;
  950. }
  951. return ret;
  952. }
  953. #endif /* WOLF_PRIVATE_KEY_ID */
  954. /* Free Hmac from use with async device */
  955. void wc_HmacFree(Hmac* hmac)
  956. {
  957. if (hmac == NULL)
  958. return;
  959. #ifdef WOLF_CRYPTO_CB
  960. /* handle cleanup case where final is not called */
  961. if (hmac->devId != INVALID_DEVID && hmac->devCtx != NULL) {
  962. int ret;
  963. byte finalHash[WC_HMAC_BLOCK_SIZE];
  964. ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, finalHash);
  965. (void)ret; /* must ignore return code here */
  966. (void)finalHash;
  967. }
  968. #endif
  969. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  970. wolfAsync_DevCtxFree(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC);
  971. #endif /* WOLFSSL_ASYNC_CRYPT */
  972. switch (hmac->macType) {
  973. #ifndef NO_MD5
  974. case WC_MD5:
  975. wc_Md5Free(&hmac->hash.md5);
  976. break;
  977. #endif /* !NO_MD5 */
  978. #ifndef NO_SHA
  979. case WC_SHA:
  980. wc_ShaFree(&hmac->hash.sha);
  981. break;
  982. #endif /* !NO_SHA */
  983. #ifdef WOLFSSL_SHA224
  984. case WC_SHA224:
  985. wc_Sha224Free(&hmac->hash.sha224);
  986. break;
  987. #endif /* WOLFSSL_SHA224 */
  988. #ifndef NO_SHA256
  989. case WC_SHA256:
  990. wc_Sha256Free(&hmac->hash.sha256);
  991. break;
  992. #endif /* !NO_SHA256 */
  993. #ifdef WOLFSSL_SHA384
  994. case WC_SHA384:
  995. wc_Sha384Free(&hmac->hash.sha384);
  996. break;
  997. #endif /* WOLFSSL_SHA384 */
  998. #ifdef WOLFSSL_SHA512
  999. case WC_SHA512:
  1000. wc_Sha512Free(&hmac->hash.sha512);
  1001. break;
  1002. #endif /* WOLFSSL_SHA512 */
  1003. #ifdef WOLFSSL_SHA3
  1004. #ifndef WOLFSSL_NOSHA3_224
  1005. case WC_SHA3_224:
  1006. wc_Sha3_224_Free(&hmac->hash.sha3);
  1007. break;
  1008. #endif
  1009. #ifndef WOLFSSL_NOSHA3_256
  1010. case WC_SHA3_256:
  1011. wc_Sha3_256_Free(&hmac->hash.sha3);
  1012. break;
  1013. #endif
  1014. #ifndef WOLFSSL_NOSHA3_384
  1015. case WC_SHA3_384:
  1016. wc_Sha3_384_Free(&hmac->hash.sha3);
  1017. break;
  1018. #endif
  1019. #ifndef WOLFSSL_NOSHA3_512
  1020. case WC_SHA3_512:
  1021. wc_Sha3_512_Free(&hmac->hash.sha3);
  1022. break;
  1023. #endif
  1024. #endif /* WOLFSSL_SHA3 */
  1025. default:
  1026. break;
  1027. }
  1028. }
  1029. #endif /* WOLFSSL_KCAPI_HMAC */
  1030. int wolfSSL_GetHmacMaxSize(void)
  1031. {
  1032. return WC_MAX_DIGEST_SIZE;
  1033. }
  1034. #ifdef HAVE_HKDF
  1035. /* HMAC-KDF-Extract.
  1036. * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
  1037. *
  1038. * type The hash algorithm type.
  1039. * salt The optional salt value.
  1040. * saltSz The size of the salt.
  1041. * inKey The input keying material.
  1042. * inKeySz The size of the input keying material.
  1043. * out The pseudorandom key with the length that of the hash.
  1044. * returns 0 on success, otherwise failure.
  1045. */
  1046. int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
  1047. const byte* inKey, word32 inKeySz, byte* out)
  1048. {
  1049. byte tmp[WC_MAX_DIGEST_SIZE]; /* localSalt helper */
  1050. Hmac myHmac;
  1051. int ret;
  1052. const byte* localSalt; /* either points to user input or tmp */
  1053. int hashSz;
  1054. ret = wc_HmacSizeByType(type);
  1055. if (ret < 0)
  1056. return ret;
  1057. hashSz = ret;
  1058. localSalt = salt;
  1059. if (localSalt == NULL) {
  1060. XMEMSET(tmp, 0, hashSz);
  1061. localSalt = tmp;
  1062. saltSz = hashSz;
  1063. }
  1064. ret = wc_HmacInit(&myHmac, NULL, INVALID_DEVID);
  1065. if (ret == 0) {
  1066. ret = wc_HmacSetKey(&myHmac, type, localSalt, saltSz);
  1067. if (ret == 0)
  1068. ret = wc_HmacUpdate(&myHmac, inKey, inKeySz);
  1069. if (ret == 0)
  1070. ret = wc_HmacFinal(&myHmac, out);
  1071. wc_HmacFree(&myHmac);
  1072. }
  1073. return ret;
  1074. }
  1075. /* HMAC-KDF-Expand.
  1076. * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
  1077. *
  1078. * type The hash algorithm type.
  1079. * inKey The input key.
  1080. * inKeySz The size of the input key.
  1081. * info The application specific information.
  1082. * infoSz The size of the application specific information.
  1083. * out The output keying material.
  1084. * returns 0 on success, otherwise failure.
  1085. */
  1086. int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
  1087. const byte* info, word32 infoSz, byte* out, word32 outSz)
  1088. {
  1089. byte tmp[WC_MAX_DIGEST_SIZE];
  1090. Hmac myHmac;
  1091. int ret = 0;
  1092. word32 outIdx = 0;
  1093. word32 hashSz = wc_HmacSizeByType(type);
  1094. byte n = 0x1;
  1095. /* RFC 5869 states that the length of output keying material in
  1096. octets must be L <= 255*HashLen or N = ceil(L/HashLen) */
  1097. if (out == NULL || ((outSz/hashSz) + ((outSz % hashSz) != 0)) > 255)
  1098. return BAD_FUNC_ARG;
  1099. ret = wc_HmacInit(&myHmac, NULL, INVALID_DEVID);
  1100. if (ret != 0)
  1101. return ret;
  1102. while (outIdx < outSz) {
  1103. int tmpSz = (n == 1) ? 0 : hashSz;
  1104. word32 left = outSz - outIdx;
  1105. ret = wc_HmacSetKey(&myHmac, type, inKey, inKeySz);
  1106. if (ret != 0)
  1107. break;
  1108. ret = wc_HmacUpdate(&myHmac, tmp, tmpSz);
  1109. if (ret != 0)
  1110. break;
  1111. ret = wc_HmacUpdate(&myHmac, info, infoSz);
  1112. if (ret != 0)
  1113. break;
  1114. ret = wc_HmacUpdate(&myHmac, &n, 1);
  1115. if (ret != 0)
  1116. break;
  1117. ret = wc_HmacFinal(&myHmac, tmp);
  1118. if (ret != 0)
  1119. break;
  1120. left = min(left, hashSz);
  1121. XMEMCPY(out+outIdx, tmp, left);
  1122. outIdx += hashSz;
  1123. n++;
  1124. }
  1125. wc_HmacFree(&myHmac);
  1126. return ret;
  1127. }
  1128. /* HMAC-KDF.
  1129. * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
  1130. *
  1131. * type The hash algorithm type.
  1132. * inKey The input keying material.
  1133. * inKeySz The size of the input keying material.
  1134. * salt The optional salt value.
  1135. * saltSz The size of the salt.
  1136. * info The application specific information.
  1137. * infoSz The size of the application specific information.
  1138. * out The output keying material.
  1139. * returns 0 on success, otherwise failure.
  1140. */
  1141. int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
  1142. const byte* salt, word32 saltSz,
  1143. const byte* info, word32 infoSz,
  1144. byte* out, word32 outSz)
  1145. {
  1146. byte prk[WC_MAX_DIGEST_SIZE];
  1147. int hashSz = wc_HmacSizeByType(type);
  1148. int ret;
  1149. if (hashSz < 0)
  1150. return BAD_FUNC_ARG;
  1151. ret = wc_HKDF_Extract(type, salt, saltSz, inKey, inKeySz, prk);
  1152. if (ret != 0)
  1153. return ret;
  1154. return wc_HKDF_Expand(type, prk, hashSz, info, infoSz, out, outSz);
  1155. }
  1156. #endif /* HAVE_HKDF */
  1157. #endif /* HAVE_FIPS */
  1158. #endif /* NO_HMAC */