hmac.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /* hmac.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_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 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 = (word16)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. word32 i;
  513. if (length < hmac_block_size)
  514. XMEMSET(ip + length, 0, hmac_block_size - length);
  515. for(i = 0; i < hmac_block_size; i++) {
  516. op[i] = (byte)(ip[i] ^ OPAD);
  517. ip[i] ^= IPAD;
  518. }
  519. }
  520. return ret;
  521. #endif /* WOLFSSL_MAXQ108X */
  522. }
  523. static int HmacKeyInnerHash(Hmac* hmac)
  524. {
  525. int ret = 0;
  526. switch (hmac->macType) {
  527. #ifndef NO_MD5
  528. case WC_MD5:
  529. ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->ipad,
  530. WC_MD5_BLOCK_SIZE);
  531. break;
  532. #endif /* !NO_MD5 */
  533. #ifndef NO_SHA
  534. case WC_SHA:
  535. ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->ipad,
  536. WC_SHA_BLOCK_SIZE);
  537. break;
  538. #endif /* !NO_SHA */
  539. #ifdef WOLFSSL_SHA224
  540. case WC_SHA224:
  541. ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->ipad,
  542. WC_SHA224_BLOCK_SIZE);
  543. break;
  544. #endif /* WOLFSSL_SHA224 */
  545. #ifndef NO_SHA256
  546. case WC_SHA256:
  547. ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->ipad,
  548. WC_SHA256_BLOCK_SIZE);
  549. break;
  550. #endif /* !NO_SHA256 */
  551. #ifdef WOLFSSL_SHA384
  552. case WC_SHA384:
  553. ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->ipad,
  554. WC_SHA384_BLOCK_SIZE);
  555. break;
  556. #endif /* WOLFSSL_SHA384 */
  557. #ifdef WOLFSSL_SHA512
  558. case WC_SHA512:
  559. ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->ipad,
  560. WC_SHA512_BLOCK_SIZE);
  561. break;
  562. #endif /* WOLFSSL_SHA512 */
  563. #ifdef WOLFSSL_SHA3
  564. #ifndef WOLFSSL_NOSHA3_224
  565. case WC_SHA3_224:
  566. ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  567. WC_SHA3_224_BLOCK_SIZE);
  568. break;
  569. #endif
  570. #ifndef WOLFSSL_NOSHA3_256
  571. case WC_SHA3_256:
  572. ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  573. WC_SHA3_256_BLOCK_SIZE);
  574. break;
  575. #endif
  576. #ifndef WOLFSSL_NOSHA3_384
  577. case WC_SHA3_384:
  578. ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  579. WC_SHA3_384_BLOCK_SIZE);
  580. break;
  581. #endif
  582. #ifndef WOLFSSL_NOSHA3_512
  583. case WC_SHA3_512:
  584. ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
  585. WC_SHA3_512_BLOCK_SIZE);
  586. break;
  587. #endif
  588. #endif /* WOLFSSL_SHA3 */
  589. default:
  590. break;
  591. }
  592. if (ret == 0)
  593. hmac->innerHashKeyed = WC_HMAC_INNER_HASH_KEYED_SW;
  594. return ret;
  595. }
  596. int wc_HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
  597. {
  598. int ret = 0;
  599. if (hmac == NULL || (msg == NULL && length > 0)) {
  600. return BAD_FUNC_ARG;
  601. }
  602. #ifdef WOLF_CRYPTO_CB
  603. if (hmac->devId != INVALID_DEVID) {
  604. ret = wc_CryptoCb_Hmac(hmac, hmac->macType, msg, length, NULL);
  605. if (ret != CRYPTOCB_UNAVAILABLE)
  606. return ret;
  607. /* fall-through when unavailable */
  608. ret = 0; /* reset error code */
  609. }
  610. #endif
  611. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  612. if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
  613. #if defined(HAVE_CAVIUM)
  614. return NitroxHmacUpdate(hmac, msg, length);
  615. #elif defined(HAVE_INTEL_QA)
  616. if (IntelQaHmacGetType(hmac->macType, NULL) == 0) {
  617. return IntelQaHmac(&hmac->asyncDev, hmac->macType,
  618. (byte*)hmac->ipad, hmac->keyLen, NULL, msg, length);
  619. }
  620. #endif
  621. }
  622. #endif /* WOLFSSL_ASYNC_CRYPT */
  623. if (!hmac->innerHashKeyed) {
  624. ret = HmacKeyInnerHash(hmac);
  625. if (ret != 0)
  626. return ret;
  627. }
  628. switch (hmac->macType) {
  629. #ifndef NO_MD5
  630. case WC_MD5:
  631. ret = wc_Md5Update(&hmac->hash.md5, msg, length);
  632. break;
  633. #endif /* !NO_MD5 */
  634. #ifndef NO_SHA
  635. case WC_SHA:
  636. ret = wc_ShaUpdate(&hmac->hash.sha, msg, length);
  637. break;
  638. #endif /* !NO_SHA */
  639. #ifdef WOLFSSL_SHA224
  640. case WC_SHA224:
  641. ret = wc_Sha224Update(&hmac->hash.sha224, msg, length);
  642. break;
  643. #endif /* WOLFSSL_SHA224 */
  644. #ifndef NO_SHA256
  645. case WC_SHA256:
  646. ret = wc_Sha256Update(&hmac->hash.sha256, msg, length);
  647. break;
  648. #endif /* !NO_SHA256 */
  649. #ifdef WOLFSSL_SHA384
  650. case WC_SHA384:
  651. ret = wc_Sha384Update(&hmac->hash.sha384, msg, length);
  652. break;
  653. #endif /* WOLFSSL_SHA384 */
  654. #ifdef WOLFSSL_SHA512
  655. case WC_SHA512:
  656. ret = wc_Sha512Update(&hmac->hash.sha512, msg, length);
  657. break;
  658. #endif /* WOLFSSL_SHA512 */
  659. #ifdef WOLFSSL_SHA3
  660. #ifndef WOLFSSL_NOSHA3_224
  661. case WC_SHA3_224:
  662. ret = wc_Sha3_224_Update(&hmac->hash.sha3, msg, length);
  663. break;
  664. #endif
  665. #ifndef WOLFSSL_NOSHA3_256
  666. case WC_SHA3_256:
  667. ret = wc_Sha3_256_Update(&hmac->hash.sha3, msg, length);
  668. break;
  669. #endif
  670. #ifndef WOLFSSL_NOSHA3_384
  671. case WC_SHA3_384:
  672. ret = wc_Sha3_384_Update(&hmac->hash.sha3, msg, length);
  673. break;
  674. #endif
  675. #ifndef WOLFSSL_NOSHA3_512
  676. case WC_SHA3_512:
  677. ret = wc_Sha3_512_Update(&hmac->hash.sha3, msg, length);
  678. break;
  679. #endif
  680. #endif /* WOLFSSL_SHA3 */
  681. default:
  682. break;
  683. }
  684. return ret;
  685. }
  686. int wc_HmacFinal(Hmac* hmac, byte* hash)
  687. {
  688. int ret;
  689. if (hmac == NULL || hash == NULL) {
  690. return BAD_FUNC_ARG;
  691. }
  692. #ifdef WOLF_CRYPTO_CB
  693. if (hmac->devId != INVALID_DEVID) {
  694. ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, hash);
  695. if (ret != CRYPTOCB_UNAVAILABLE)
  696. return ret;
  697. /* fall-through when unavailable */
  698. }
  699. #endif
  700. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  701. if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
  702. int hashLen = wc_HmacSizeByType(hmac->macType);
  703. if (hashLen <= 0)
  704. return hashLen;
  705. #if defined(HAVE_CAVIUM)
  706. return NitroxHmacFinal(hmac, hash, hashLen);
  707. #elif defined(HAVE_INTEL_QA)
  708. if (IntelQaHmacGetType(hmac->macType, NULL) == 0) {
  709. return IntelQaHmac(&hmac->asyncDev, hmac->macType,
  710. (byte*)hmac->ipad, hmac->keyLen, hash, NULL, hashLen);
  711. }
  712. #endif
  713. }
  714. #endif /* WOLFSSL_ASYNC_CRYPT */
  715. if (!hmac->innerHashKeyed) {
  716. ret = HmacKeyInnerHash(hmac);
  717. if (ret != 0)
  718. return ret;
  719. }
  720. switch (hmac->macType) {
  721. #ifndef NO_MD5
  722. case WC_MD5:
  723. ret = wc_Md5Final(&hmac->hash.md5, (byte*)hmac->innerHash);
  724. if (ret != 0)
  725. break;
  726. ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->opad,
  727. WC_MD5_BLOCK_SIZE);
  728. if (ret != 0)
  729. break;
  730. ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->innerHash,
  731. WC_MD5_DIGEST_SIZE);
  732. if (ret != 0)
  733. break;
  734. ret = wc_Md5Final(&hmac->hash.md5, hash);
  735. break;
  736. #endif /* !NO_MD5 */
  737. #ifndef NO_SHA
  738. case WC_SHA:
  739. ret = wc_ShaFinal(&hmac->hash.sha, (byte*)hmac->innerHash);
  740. if (ret != 0)
  741. break;
  742. ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->opad,
  743. WC_SHA_BLOCK_SIZE);
  744. if (ret != 0)
  745. break;
  746. ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->innerHash,
  747. WC_SHA_DIGEST_SIZE);
  748. if (ret != 0)
  749. break;
  750. ret = wc_ShaFinal(&hmac->hash.sha, hash);
  751. break;
  752. #endif /* !NO_SHA */
  753. #ifdef WOLFSSL_SHA224
  754. case WC_SHA224:
  755. ret = wc_Sha224Final(&hmac->hash.sha224, (byte*)hmac->innerHash);
  756. if (ret != 0)
  757. break;
  758. ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->opad,
  759. WC_SHA224_BLOCK_SIZE);
  760. if (ret != 0)
  761. break;
  762. ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->innerHash,
  763. WC_SHA224_DIGEST_SIZE);
  764. if (ret != 0)
  765. break;
  766. ret = wc_Sha224Final(&hmac->hash.sha224, hash);
  767. if (ret != 0)
  768. break;
  769. break;
  770. #endif /* WOLFSSL_SHA224 */
  771. #ifndef NO_SHA256
  772. case WC_SHA256:
  773. ret = wc_Sha256Final(&hmac->hash.sha256, (byte*)hmac->innerHash);
  774. if (ret != 0)
  775. break;
  776. ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->opad,
  777. WC_SHA256_BLOCK_SIZE);
  778. if (ret != 0)
  779. break;
  780. ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->innerHash,
  781. WC_SHA256_DIGEST_SIZE);
  782. if (ret != 0)
  783. break;
  784. ret = wc_Sha256Final(&hmac->hash.sha256, hash);
  785. break;
  786. #endif /* !NO_SHA256 */
  787. #ifdef WOLFSSL_SHA384
  788. case WC_SHA384:
  789. ret = wc_Sha384Final(&hmac->hash.sha384, (byte*)hmac->innerHash);
  790. if (ret != 0)
  791. break;
  792. ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->opad,
  793. WC_SHA384_BLOCK_SIZE);
  794. if (ret != 0)
  795. break;
  796. ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->innerHash,
  797. WC_SHA384_DIGEST_SIZE);
  798. if (ret != 0)
  799. break;
  800. ret = wc_Sha384Final(&hmac->hash.sha384, hash);
  801. break;
  802. #endif /* WOLFSSL_SHA384 */
  803. #ifdef WOLFSSL_SHA512
  804. case WC_SHA512:
  805. ret = wc_Sha512Final(&hmac->hash.sha512, (byte*)hmac->innerHash);
  806. if (ret != 0)
  807. break;
  808. ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->opad,
  809. WC_SHA512_BLOCK_SIZE);
  810. if (ret != 0)
  811. break;
  812. ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->innerHash,
  813. WC_SHA512_DIGEST_SIZE);
  814. if (ret != 0)
  815. break;
  816. ret = wc_Sha512Final(&hmac->hash.sha512, hash);
  817. break;
  818. #endif /* WOLFSSL_SHA512 */
  819. #ifdef WOLFSSL_SHA3
  820. #ifndef WOLFSSL_NOSHA3_224
  821. case WC_SHA3_224:
  822. ret = wc_Sha3_224_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  823. if (ret != 0)
  824. break;
  825. ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  826. WC_SHA3_224_BLOCK_SIZE);
  827. if (ret != 0)
  828. break;
  829. ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  830. WC_SHA3_224_DIGEST_SIZE);
  831. if (ret != 0)
  832. break;
  833. ret = wc_Sha3_224_Final(&hmac->hash.sha3, hash);
  834. break;
  835. #endif
  836. #ifndef WOLFSSL_NOSHA3_256
  837. case WC_SHA3_256:
  838. ret = wc_Sha3_256_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  839. if (ret != 0)
  840. break;
  841. ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  842. WC_SHA3_256_BLOCK_SIZE);
  843. if (ret != 0)
  844. break;
  845. ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  846. WC_SHA3_256_DIGEST_SIZE);
  847. if (ret != 0)
  848. break;
  849. ret = wc_Sha3_256_Final(&hmac->hash.sha3, hash);
  850. break;
  851. #endif
  852. #ifndef WOLFSSL_NOSHA3_384
  853. case WC_SHA3_384:
  854. ret = wc_Sha3_384_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  855. if (ret != 0)
  856. break;
  857. ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  858. WC_SHA3_384_BLOCK_SIZE);
  859. if (ret != 0)
  860. break;
  861. ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  862. WC_SHA3_384_DIGEST_SIZE);
  863. if (ret != 0)
  864. break;
  865. ret = wc_Sha3_384_Final(&hmac->hash.sha3, hash);
  866. break;
  867. #endif
  868. #ifndef WOLFSSL_NOSHA3_512
  869. case WC_SHA3_512:
  870. ret = wc_Sha3_512_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
  871. if (ret != 0)
  872. break;
  873. ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->opad,
  874. WC_SHA3_512_BLOCK_SIZE);
  875. if (ret != 0)
  876. break;
  877. ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
  878. WC_SHA3_512_DIGEST_SIZE);
  879. if (ret != 0)
  880. break;
  881. ret = wc_Sha3_512_Final(&hmac->hash.sha3, hash);
  882. break;
  883. #endif
  884. #endif /* WOLFSSL_SHA3 */
  885. default:
  886. ret = BAD_FUNC_ARG;
  887. break;
  888. }
  889. if (ret == 0) {
  890. hmac->innerHashKeyed = 0;
  891. }
  892. return ret;
  893. }
  894. #ifdef WOLFSSL_KCAPI_HMAC
  895. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hmac.c */
  896. /* unmap the _Software calls used by kcapi_hmac.c */
  897. #undef wc_HmacSetKey
  898. #undef wc_HmacUpdate
  899. #undef wc_HmacFinal
  900. #else
  901. /* Initialize Hmac for use with async device */
  902. int wc_HmacInit(Hmac* hmac, void* heap, int devId)
  903. {
  904. int ret = 0;
  905. if (hmac == NULL)
  906. return BAD_FUNC_ARG;
  907. XMEMSET(hmac, 0, sizeof(Hmac));
  908. hmac->macType = WC_HASH_TYPE_NONE;
  909. hmac->heap = heap;
  910. #ifdef WOLF_CRYPTO_CB
  911. hmac->devId = devId;
  912. hmac->devCtx = NULL;
  913. #endif
  914. #if defined(WOLFSSL_DEVCRYPTO_HMAC)
  915. hmac->ctx.cfd = -1;
  916. #endif
  917. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  918. ret = wolfAsync_DevCtxInit(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC,
  919. hmac->heap, devId);
  920. #else
  921. (void)devId;
  922. #endif /* WOLFSSL_ASYNC_CRYPT */
  923. return ret;
  924. }
  925. #ifdef WOLF_PRIVATE_KEY_ID
  926. int wc_HmacInit_Id(Hmac* hmac, unsigned char* id, int len, void* heap,
  927. int devId)
  928. {
  929. int ret = 0;
  930. if (hmac == NULL)
  931. ret = BAD_FUNC_ARG;
  932. if (ret == 0 && (len < 0 || len > HMAC_MAX_ID_LEN))
  933. ret = BUFFER_E;
  934. if (ret == 0)
  935. ret = wc_HmacInit(hmac, heap, devId);
  936. if (ret == 0) {
  937. XMEMCPY(hmac->id, id, (size_t)len);
  938. hmac->idLen = len;
  939. }
  940. return ret;
  941. }
  942. int wc_HmacInit_Label(Hmac* hmac, const char* label, void* heap, int devId)
  943. {
  944. int ret = 0;
  945. int labelLen = 0;
  946. if (hmac == NULL || label == NULL)
  947. ret = BAD_FUNC_ARG;
  948. if (ret == 0) {
  949. labelLen = (int)XSTRLEN(label);
  950. if (labelLen == 0 || labelLen > HMAC_MAX_LABEL_LEN)
  951. ret = BUFFER_E;
  952. }
  953. if (ret == 0)
  954. ret = wc_HmacInit(hmac, heap, devId);
  955. if (ret == 0) {
  956. XMEMCPY(hmac->label, label, (size_t)labelLen);
  957. hmac->labelLen = labelLen;
  958. }
  959. return ret;
  960. }
  961. #endif /* WOLF_PRIVATE_KEY_ID */
  962. /* Free Hmac from use with async device */
  963. void wc_HmacFree(Hmac* hmac)
  964. {
  965. if (hmac == NULL)
  966. return;
  967. #ifdef WOLF_CRYPTO_CB
  968. /* handle cleanup case where final is not called */
  969. if (hmac->devId != INVALID_DEVID && hmac->devCtx != NULL) {
  970. int ret;
  971. byte finalHash[WC_HMAC_BLOCK_SIZE];
  972. ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, finalHash);
  973. (void)ret; /* must ignore return code here */
  974. (void)finalHash;
  975. }
  976. #endif
  977. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  978. wolfAsync_DevCtxFree(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC);
  979. #endif /* WOLFSSL_ASYNC_CRYPT */
  980. switch (hmac->macType) {
  981. #ifndef NO_MD5
  982. case WC_MD5:
  983. wc_Md5Free(&hmac->hash.md5);
  984. break;
  985. #endif /* !NO_MD5 */
  986. #ifndef NO_SHA
  987. case WC_SHA:
  988. wc_ShaFree(&hmac->hash.sha);
  989. break;
  990. #endif /* !NO_SHA */
  991. #ifdef WOLFSSL_SHA224
  992. case WC_SHA224:
  993. wc_Sha224Free(&hmac->hash.sha224);
  994. break;
  995. #endif /* WOLFSSL_SHA224 */
  996. #ifndef NO_SHA256
  997. case WC_SHA256:
  998. wc_Sha256Free(&hmac->hash.sha256);
  999. break;
  1000. #endif /* !NO_SHA256 */
  1001. #ifdef WOLFSSL_SHA384
  1002. case WC_SHA384:
  1003. wc_Sha384Free(&hmac->hash.sha384);
  1004. break;
  1005. #endif /* WOLFSSL_SHA384 */
  1006. #ifdef WOLFSSL_SHA512
  1007. case WC_SHA512:
  1008. wc_Sha512Free(&hmac->hash.sha512);
  1009. break;
  1010. #endif /* WOLFSSL_SHA512 */
  1011. #ifdef WOLFSSL_SHA3
  1012. #ifndef WOLFSSL_NOSHA3_224
  1013. case WC_SHA3_224:
  1014. wc_Sha3_224_Free(&hmac->hash.sha3);
  1015. break;
  1016. #endif
  1017. #ifndef WOLFSSL_NOSHA3_256
  1018. case WC_SHA3_256:
  1019. wc_Sha3_256_Free(&hmac->hash.sha3);
  1020. break;
  1021. #endif
  1022. #ifndef WOLFSSL_NOSHA3_384
  1023. case WC_SHA3_384:
  1024. wc_Sha3_384_Free(&hmac->hash.sha3);
  1025. break;
  1026. #endif
  1027. #ifndef WOLFSSL_NOSHA3_512
  1028. case WC_SHA3_512:
  1029. wc_Sha3_512_Free(&hmac->hash.sha3);
  1030. break;
  1031. #endif
  1032. #endif /* WOLFSSL_SHA3 */
  1033. default:
  1034. break;
  1035. }
  1036. }
  1037. #endif /* WOLFSSL_KCAPI_HMAC */
  1038. int wolfSSL_GetHmacMaxSize(void)
  1039. {
  1040. return WC_MAX_DIGEST_SIZE;
  1041. }
  1042. #ifdef HAVE_HKDF
  1043. /* HMAC-KDF-Extract.
  1044. * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
  1045. *
  1046. * type The hash algorithm type.
  1047. * salt The optional salt value.
  1048. * saltSz The size of the salt.
  1049. * inKey The input keying material.
  1050. * inKeySz The size of the input keying material.
  1051. * out The pseudorandom key with the length that of the hash.
  1052. * returns 0 on success, otherwise failure.
  1053. */
  1054. int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
  1055. const byte* inKey, word32 inKeySz, byte* out)
  1056. {
  1057. byte tmp[WC_MAX_DIGEST_SIZE]; /* localSalt helper */
  1058. #ifdef WOLFSSL_SMALL_STACK
  1059. Hmac* myHmac;
  1060. #else
  1061. Hmac myHmac[1];
  1062. #endif
  1063. int ret;
  1064. const byte* localSalt; /* either points to user input or tmp */
  1065. word32 hashSz;
  1066. ret = wc_HmacSizeByType(type);
  1067. if (ret < 0) {
  1068. return ret;
  1069. }
  1070. #ifdef WOLFSSL_SMALL_STACK
  1071. myHmac = (Hmac*)XMALLOC(sizeof(Hmac), NULL, DYNAMIC_TYPE_HMAC);
  1072. if (myHmac == NULL) {
  1073. return MEMORY_E;
  1074. }
  1075. #endif
  1076. hashSz = (word32)ret;
  1077. localSalt = salt;
  1078. if (localSalt == NULL) {
  1079. XMEMSET(tmp, 0, hashSz);
  1080. localSalt = tmp;
  1081. saltSz = hashSz;
  1082. }
  1083. ret = wc_HmacInit(myHmac, NULL, INVALID_DEVID);
  1084. if (ret == 0) {
  1085. ret = wc_HmacSetKey(myHmac, type, localSalt, saltSz);
  1086. if (ret == 0)
  1087. ret = wc_HmacUpdate(myHmac, inKey, inKeySz);
  1088. if (ret == 0)
  1089. ret = wc_HmacFinal(myHmac, out);
  1090. wc_HmacFree(myHmac);
  1091. }
  1092. #ifdef WOLFSSL_SMALL_STACK
  1093. XFREE(myHmac, NULL, DYNAMIC_TYPE_HMAC);
  1094. #endif
  1095. return ret;
  1096. }
  1097. /* HMAC-KDF-Expand.
  1098. * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
  1099. *
  1100. * type The hash algorithm type.
  1101. * inKey The input key.
  1102. * inKeySz The size of the input key.
  1103. * info The application specific information.
  1104. * infoSz The size of the application specific information.
  1105. * out The output keying material.
  1106. * returns 0 on success, otherwise failure.
  1107. */
  1108. int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
  1109. const byte* info, word32 infoSz, byte* out, word32 outSz)
  1110. {
  1111. byte tmp[WC_MAX_DIGEST_SIZE];
  1112. #ifdef WOLFSSL_SMALL_STACK
  1113. Hmac* myHmac;
  1114. #else
  1115. Hmac myHmac[1];
  1116. #endif
  1117. int ret = 0;
  1118. word32 outIdx = 0;
  1119. word32 hashSz;
  1120. byte n = 0x1;
  1121. ret = wc_HmacSizeByType(type);
  1122. if (ret < 0) {
  1123. return ret;
  1124. }
  1125. hashSz = (word32)ret;
  1126. /* RFC 5869 states that the length of output keying material in
  1127. * octets must be L <= 255*HashLen or N = ceil(L/HashLen) */
  1128. if (out == NULL || ((outSz/hashSz) + ((outSz % hashSz) != 0)) > 255) {
  1129. return BAD_FUNC_ARG;
  1130. }
  1131. #ifdef WOLFSSL_SMALL_STACK
  1132. myHmac = (Hmac*)XMALLOC(sizeof(Hmac), NULL, DYNAMIC_TYPE_HMAC);
  1133. if (myHmac == NULL) {
  1134. return MEMORY_E;
  1135. }
  1136. #endif
  1137. ret = wc_HmacInit(myHmac, NULL, INVALID_DEVID);
  1138. if (ret != 0) {
  1139. #ifdef WOLFSSL_SMALL_STACK
  1140. XFREE(myHmac, NULL, DYNAMIC_TYPE_HMAC);
  1141. #endif
  1142. return ret;
  1143. }
  1144. XMEMSET(tmp, 0, WC_MAX_DIGEST_SIZE);
  1145. while (outIdx < outSz) {
  1146. word32 tmpSz = (n == 1) ? 0 : hashSz;
  1147. word32 left = outSz - outIdx;
  1148. ret = wc_HmacSetKey(myHmac, type, inKey, inKeySz);
  1149. if (ret != 0)
  1150. break;
  1151. ret = wc_HmacUpdate(myHmac, tmp, tmpSz);
  1152. if (ret != 0)
  1153. break;
  1154. ret = wc_HmacUpdate(myHmac, info, infoSz);
  1155. if (ret != 0)
  1156. break;
  1157. ret = wc_HmacUpdate(myHmac, &n, 1);
  1158. if (ret != 0)
  1159. break;
  1160. ret = wc_HmacFinal(myHmac, tmp);
  1161. if (ret != 0)
  1162. break;
  1163. left = min(left, hashSz);
  1164. XMEMCPY(out+outIdx, tmp, left);
  1165. outIdx += hashSz;
  1166. n++;
  1167. }
  1168. wc_HmacFree(myHmac);
  1169. #ifdef WOLFSSL_SMALL_STACK
  1170. XFREE(myHmac, NULL, DYNAMIC_TYPE_HMAC);
  1171. #endif
  1172. return ret;
  1173. }
  1174. /* HMAC-KDF.
  1175. * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
  1176. *
  1177. * type The hash algorithm type.
  1178. * inKey The input keying material.
  1179. * inKeySz The size of the input keying material.
  1180. * salt The optional salt value.
  1181. * saltSz The size of the salt.
  1182. * info The application specific information.
  1183. * infoSz The size of the application specific information.
  1184. * out The output keying material.
  1185. * returns 0 on success, otherwise failure.
  1186. */
  1187. int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
  1188. const byte* salt, word32 saltSz,
  1189. const byte* info, word32 infoSz,
  1190. byte* out, word32 outSz)
  1191. {
  1192. byte prk[WC_MAX_DIGEST_SIZE];
  1193. word32 hashSz;
  1194. int ret;
  1195. ret = wc_HmacSizeByType(type);
  1196. if (ret < 0) {
  1197. return ret;
  1198. }
  1199. hashSz = (word32)ret;
  1200. ret = wc_HKDF_Extract(type, salt, saltSz, inKey, inKeySz, prk);
  1201. if (ret != 0)
  1202. return ret;
  1203. return wc_HKDF_Expand(type, prk, hashSz, info, infoSz, out, outSz);
  1204. }
  1205. #endif /* HAVE_HKDF */
  1206. #endif /* HAVE_FIPS */
  1207. #endif /* NO_HMAC */