sha3.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. /* sha3.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/settings.h>
  25. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  26. !defined(WOLFSSL_AFALG_XILINX_SHA3)
  27. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  28. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  29. #define FIPS_NO_WRAPPERS
  30. #ifdef USE_WINDOWS_API
  31. #pragma code_seg(".fipsA$l")
  32. #pragma const_seg(".fipsB$l")
  33. #endif
  34. #endif
  35. #include <wolfssl/wolfcrypt/sha3.h>
  36. #include <wolfssl/wolfcrypt/error-crypt.h>
  37. #include <wolfssl/wolfcrypt/hash.h>
  38. #ifdef NO_INLINE
  39. #include <wolfssl/wolfcrypt/misc.h>
  40. #else
  41. #define WOLFSSL_MISC_INCLUDED
  42. #include <wolfcrypt/src/misc.c>
  43. #endif
  44. #if !defined(WOLFSSL_ARMASM) || !defined(WOLFSSL_ARMASM_CRYPTO_SHA3)
  45. #ifdef WOLFSSL_SHA3_SMALL
  46. /* Rotate a 64-bit value left.
  47. *
  48. * a Number to rotate left.
  49. * r Number od bits to rotate left.
  50. * returns the rotated number.
  51. */
  52. #define ROTL64(a, n) (((a)<<(n))|((a)>>(64-(n))))
  53. /* An array of values to XOR for block operation. */
  54. static const word64 hash_keccak_r[24] =
  55. {
  56. 0x0000000000000001UL, 0x0000000000008082UL,
  57. 0x800000000000808aUL, 0x8000000080008000UL,
  58. 0x000000000000808bUL, 0x0000000080000001UL,
  59. 0x8000000080008081UL, 0x8000000000008009UL,
  60. 0x000000000000008aUL, 0x0000000000000088UL,
  61. 0x0000000080008009UL, 0x000000008000000aUL,
  62. 0x000000008000808bUL, 0x800000000000008bUL,
  63. 0x8000000000008089UL, 0x8000000000008003UL,
  64. 0x8000000000008002UL, 0x8000000000000080UL,
  65. 0x000000000000800aUL, 0x800000008000000aUL,
  66. 0x8000000080008081UL, 0x8000000000008080UL,
  67. 0x0000000080000001UL, 0x8000000080008008UL
  68. };
  69. /* Indices used in swap and rotate operation. */
  70. #define K_I_0 10
  71. #define K_I_1 7
  72. #define K_I_2 11
  73. #define K_I_3 17
  74. #define K_I_4 18
  75. #define K_I_5 3
  76. #define K_I_6 5
  77. #define K_I_7 16
  78. #define K_I_8 8
  79. #define K_I_9 21
  80. #define K_I_10 24
  81. #define K_I_11 4
  82. #define K_I_12 15
  83. #define K_I_13 23
  84. #define K_I_14 19
  85. #define K_I_15 13
  86. #define K_I_16 12
  87. #define K_I_17 2
  88. #define K_I_18 20
  89. #define K_I_19 14
  90. #define K_I_20 22
  91. #define K_I_21 9
  92. #define K_I_22 6
  93. #define K_I_23 1
  94. /* Number of bits to rotate in swap and rotate operation. */
  95. #define K_R_0 1
  96. #define K_R_1 3
  97. #define K_R_2 6
  98. #define K_R_3 10
  99. #define K_R_4 15
  100. #define K_R_5 21
  101. #define K_R_6 28
  102. #define K_R_7 36
  103. #define K_R_8 45
  104. #define K_R_9 55
  105. #define K_R_10 2
  106. #define K_R_11 14
  107. #define K_R_12 27
  108. #define K_R_13 41
  109. #define K_R_14 56
  110. #define K_R_15 8
  111. #define K_R_16 25
  112. #define K_R_17 43
  113. #define K_R_18 62
  114. #define K_R_19 18
  115. #define K_R_20 39
  116. #define K_R_21 61
  117. #define K_R_22 20
  118. #define K_R_23 44
  119. /* Swap and rotate left operation.
  120. *
  121. * s The state.
  122. * t1 Temporary value.
  123. * t2 Second temporary value.
  124. * i The index of the loop.
  125. */
  126. #define SWAP_ROTL(s, t1, t2, i) \
  127. do { \
  128. t2 = s[K_I_##i]; s[K_I_##i] = ROTL64(t1, K_R_##i); \
  129. } \
  130. while (0)
  131. /* Mix the XOR of the column's values into each number by column.
  132. *
  133. * s The state.
  134. * b Temporary array of XORed column values.
  135. * x The index of the column.
  136. * t Temporary variable.
  137. */
  138. #define COL_MIX(s, b, x, t) \
  139. do { \
  140. for (x = 0; x < 5; x++) \
  141. b[x] = s[x + 0] ^ s[x + 5] ^ s[x + 10] ^ s[x + 15] ^ s[x + 20]; \
  142. for (x = 0; x < 5; x++) { \
  143. t = b[(x + 4) % 5] ^ ROTL64(b[(x + 1) % 5], 1); \
  144. s[x + 0] ^= t; \
  145. s[x + 5] ^= t; \
  146. s[x + 10] ^= t; \
  147. s[x + 15] ^= t; \
  148. s[x + 20] ^= t; \
  149. } \
  150. } \
  151. while (0)
  152. #ifdef SHA3_BY_SPEC
  153. /* Mix the row values.
  154. * BMI1 has ANDN instruction ((~a) & b) - Haswell and above.
  155. *
  156. * s The state.
  157. * b Temporary array of XORed row values.
  158. * y The index of the row to work on.
  159. * x The index of the column.
  160. * t0 Temporary variable.
  161. * t1 Temporary variable.
  162. */
  163. #define ROW_MIX(s, b, y, x, t0, t1) \
  164. do { \
  165. for (y = 0; y < 5; y++) { \
  166. for (x = 0; x < 5; x++) \
  167. b[x] = s[y * 5 + x]; \
  168. for (x = 0; x < 5; x++) \
  169. s[y * 5 + x] = b[x] ^ (~b[(x + 1) % 5] & b[(x + 2) % 5]); \
  170. } \
  171. } \
  172. while (0)
  173. #else
  174. /* Mix the row values.
  175. * a ^ (~b & c) == a ^ (c & (b ^ c)) == (a ^ b) ^ (b | c)
  176. *
  177. * s The state.
  178. * b Temporary array of XORed row values.
  179. * y The index of the row to work on.
  180. * x The index of the column.
  181. * t0 Temporary variable.
  182. * t1 Temporary variable.
  183. */
  184. #define ROW_MIX(s, b, y, x, t12, t34) \
  185. do { \
  186. for (y = 0; y < 5; y++) { \
  187. for (x = 0; x < 5; x++) \
  188. b[x] = s[y * 5 + x]; \
  189. t12 = (b[1] ^ b[2]); t34 = (b[3] ^ b[4]); \
  190. s[y * 5 + 0] = b[0] ^ (b[2] & t12); \
  191. s[y * 5 + 1] = t12 ^ (b[2] | b[3]); \
  192. s[y * 5 + 2] = b[2] ^ (b[4] & t34); \
  193. s[y * 5 + 3] = t34 ^ (b[4] | b[0]); \
  194. s[y * 5 + 4] = b[4] ^ (b[1] & (b[0] ^ b[1])); \
  195. } \
  196. } \
  197. while (0)
  198. #endif /* SHA3_BY_SPEC */
  199. /* The block operation performed on the state.
  200. *
  201. * s The state.
  202. */
  203. static void BlockSha3(word64 *s)
  204. {
  205. byte i, x, y;
  206. word64 t0, t1;
  207. word64 b[5];
  208. for (i = 0; i < 24; i++)
  209. {
  210. COL_MIX(s, b, x, t0);
  211. t0 = s[1];
  212. SWAP_ROTL(s, t0, t1, 0);
  213. SWAP_ROTL(s, t1, t0, 1);
  214. SWAP_ROTL(s, t0, t1, 2);
  215. SWAP_ROTL(s, t1, t0, 3);
  216. SWAP_ROTL(s, t0, t1, 4);
  217. SWAP_ROTL(s, t1, t0, 5);
  218. SWAP_ROTL(s, t0, t1, 6);
  219. SWAP_ROTL(s, t1, t0, 7);
  220. SWAP_ROTL(s, t0, t1, 8);
  221. SWAP_ROTL(s, t1, t0, 9);
  222. SWAP_ROTL(s, t0, t1, 10);
  223. SWAP_ROTL(s, t1, t0, 11);
  224. SWAP_ROTL(s, t0, t1, 12);
  225. SWAP_ROTL(s, t1, t0, 13);
  226. SWAP_ROTL(s, t0, t1, 14);
  227. SWAP_ROTL(s, t1, t0, 15);
  228. SWAP_ROTL(s, t0, t1, 16);
  229. SWAP_ROTL(s, t1, t0, 17);
  230. SWAP_ROTL(s, t0, t1, 18);
  231. SWAP_ROTL(s, t1, t0, 19);
  232. SWAP_ROTL(s, t0, t1, 20);
  233. SWAP_ROTL(s, t1, t0, 21);
  234. SWAP_ROTL(s, t0, t1, 22);
  235. SWAP_ROTL(s, t1, t0, 23);
  236. ROW_MIX(s, b, y, x, t0, t1);
  237. s[0] ^= hash_keccak_r[i];
  238. }
  239. }
  240. #else
  241. /* Rotate a 64-bit value left.
  242. *
  243. * a Number to rotate left.
  244. * r Number od bits to rotate left.
  245. * returns the rotated number.
  246. */
  247. #define ROTL64(a, n) (((a)<<(n))|((a)>>(64-(n))))
  248. /* An array of values to XOR for block operation. */
  249. static const word64 hash_keccak_r[24] =
  250. {
  251. W64LIT(0x0000000000000001), W64LIT(0x0000000000008082),
  252. W64LIT(0x800000000000808a), W64LIT(0x8000000080008000),
  253. W64LIT(0x000000000000808b), W64LIT(0x0000000080000001),
  254. W64LIT(0x8000000080008081), W64LIT(0x8000000000008009),
  255. W64LIT(0x000000000000008a), W64LIT(0x0000000000000088),
  256. W64LIT(0x0000000080008009), W64LIT(0x000000008000000a),
  257. W64LIT(0x000000008000808b), W64LIT(0x800000000000008b),
  258. W64LIT(0x8000000000008089), W64LIT(0x8000000000008003),
  259. W64LIT(0x8000000000008002), W64LIT(0x8000000000000080),
  260. W64LIT(0x000000000000800a), W64LIT(0x800000008000000a),
  261. W64LIT(0x8000000080008081), W64LIT(0x8000000000008080),
  262. W64LIT(0x0000000080000001), W64LIT(0x8000000080008008)
  263. };
  264. /* Indices used in swap and rotate operation. */
  265. #define KI_0 6
  266. #define KI_1 12
  267. #define KI_2 18
  268. #define KI_3 24
  269. #define KI_4 3
  270. #define KI_5 9
  271. #define KI_6 10
  272. #define KI_7 16
  273. #define KI_8 22
  274. #define KI_9 1
  275. #define KI_10 7
  276. #define KI_11 13
  277. #define KI_12 19
  278. #define KI_13 20
  279. #define KI_14 4
  280. #define KI_15 5
  281. #define KI_16 11
  282. #define KI_17 17
  283. #define KI_18 23
  284. #define KI_19 2
  285. #define KI_20 8
  286. #define KI_21 14
  287. #define KI_22 15
  288. #define KI_23 21
  289. /* Number of bits to rotate in swap and rotate operation. */
  290. #define KR_0 44
  291. #define KR_1 43
  292. #define KR_2 21
  293. #define KR_3 14
  294. #define KR_4 28
  295. #define KR_5 20
  296. #define KR_6 3
  297. #define KR_7 45
  298. #define KR_8 61
  299. #define KR_9 1
  300. #define KR_10 6
  301. #define KR_11 25
  302. #define KR_12 8
  303. #define KR_13 18
  304. #define KR_14 27
  305. #define KR_15 36
  306. #define KR_16 10
  307. #define KR_17 15
  308. #define KR_18 56
  309. #define KR_19 62
  310. #define KR_20 55
  311. #define KR_21 39
  312. #define KR_22 41
  313. #define KR_23 2
  314. /* Mix the XOR of the column's values into each number by column.
  315. *
  316. * s The state.
  317. * b Temporary array of XORed column values.
  318. * x The index of the column.
  319. * t Temporary variable.
  320. */
  321. #define COL_MIX(s, b, x, t) \
  322. do { \
  323. (b)[0] = (s)[0] ^ (s)[5] ^ (s)[10] ^ (s)[15] ^ (s)[20]; \
  324. (b)[1] = (s)[1] ^ (s)[6] ^ (s)[11] ^ (s)[16] ^ (s)[21]; \
  325. (b)[2] = (s)[2] ^ (s)[7] ^ (s)[12] ^ (s)[17] ^ (s)[22]; \
  326. (b)[3] = (s)[3] ^ (s)[8] ^ (s)[13] ^ (s)[18] ^ (s)[23]; \
  327. (b)[4] = (s)[4] ^ (s)[9] ^ (s)[14] ^ (s)[19] ^ (s)[24]; \
  328. (t) = (b)[(0 + 4) % 5] ^ ROTL64((b)[(0 + 1) % 5], 1); \
  329. (s)[ 0] ^= (t); (s)[ 5] ^= (t); (s)[10] ^= (t); (s)[15] ^= (t); (s)[20] ^= (t); \
  330. (t) = (b)[(1 + 4) % 5] ^ ROTL64((b)[(1 + 1) % 5], 1); \
  331. (s)[ 1] ^= (t); (s)[ 6] ^= (t); (s)[11] ^= (t); (s)[16] ^= (t); (s)[21] ^= (t); \
  332. (t) = (b)[(2 + 4) % 5] ^ ROTL64((b)[(2 + 1) % 5], 1); \
  333. (s)[ 2] ^= (t); (s)[ 7] ^= (t); (s)[12] ^= (t); (s)[17] ^= (t); (s)[22] ^= (t); \
  334. (t) = (b)[(3 + 4) % 5] ^ ROTL64((b)[(3 + 1) % 5], 1); \
  335. (s)[ 3] ^= (t); (s)[ 8] ^= (t); (s)[13] ^= (t); (s)[18] ^= (t); (s)[23] ^= (t); \
  336. (t) = (b)[(4 + 4) % 5] ^ ROTL64((b)[(4 + 1) % 5], 1); \
  337. (s)[ 4] ^= (t); (s)[ 9] ^= (t); (s)[14] ^= (t); (s)[19] ^= (t); (s)[24] ^= (t); \
  338. } \
  339. while (0)
  340. #define S(s1, i) ROTL64((s1)[KI_##i], KR_##i)
  341. #ifdef SHA3_BY_SPEC
  342. /* Mix the row values.
  343. * BMI1 has ANDN instruction ((~a) & b) - Haswell and above.
  344. *
  345. * s2 The new state.
  346. * s1 The current state.
  347. * b Temporary array of XORed row values.
  348. * t0 Temporary variable. (Unused)
  349. * t1 Temporary variable. (Unused)
  350. */
  351. #define ROW_MIX(s2, s1, b, t0, t1) \
  352. do { \
  353. (b)[0] = (s1)[0]; \
  354. (b)[1] = S((s1), 0); \
  355. (b)[2] = S((s1), 1); \
  356. (b)[3] = S((s1), 2); \
  357. (b)[4] = S((s1), 3); \
  358. (s2)[0] = (b)[0] ^ (~(b)[1] & (b)[2]); \
  359. (s2)[1] = (b)[1] ^ (~(b)[2] & (b)[3]); \
  360. (s2)[2] = (b)[2] ^ (~(b)[3] & (b)[4]); \
  361. (s2)[3] = (b)[3] ^ (~(b)[4] & (b)[0]); \
  362. (s2)[4] = (b)[4] ^ (~(b)[0] & (b)[1]); \
  363. (b)[0] = S((s1), 4); \
  364. (b)[1] = S((s1), 5); \
  365. (b)[2] = S((s1), 6); \
  366. (b)[3] = S((s1), 7); \
  367. (b)[4] = S((s1), 8); \
  368. (s2)[5] = (b)[0] ^ (~(b)[1] & (b)[2]); \
  369. (s2)[6] = (b)[1] ^ (~(b)[2] & (b)[3]); \
  370. (s2)[7] = (b)[2] ^ (~(b)[3] & (b)[4]); \
  371. (s2)[8] = (b)[3] ^ (~(b)[4] & (b)[0]); \
  372. (s2)[9] = (b)[4] ^ (~(b)[0] & (b)[1]); \
  373. (b)[0] = S((s1), 9); \
  374. (b)[1] = S((s1), 10); \
  375. (b)[2] = S((s1), 11); \
  376. (b)[3] = S((s1), 12); \
  377. (b)[4] = S((s1), 13); \
  378. (s2)[10] = (b)[0] ^ (~(b)[1] & (b)[2]); \
  379. (s2)[11] = (b)[1] ^ (~(b)[2] & (b)[3]); \
  380. (s2)[12] = (b)[2] ^ (~(b)[3] & (b)[4]); \
  381. (s2)[13] = (b)[3] ^ (~(b)[4] & (b)[0]); \
  382. (s2)[14] = (b)[4] ^ (~(b)[0] & (b)[1]); \
  383. (b)[0] = S((s1), 14); \
  384. (b)[1] = S((s1), 15); \
  385. (b)[2] = S((s1), 16); \
  386. (b)[3] = S((s1), 17); \
  387. (b)[4] = S((s1), 18); \
  388. (s2)[15] = (b)[0] ^ (~(b)[1] & (b)[2]); \
  389. (s2)[16] = (b)[1] ^ (~(b)[2] & (b)[3]); \
  390. (s2)[17] = (b)[2] ^ (~(b)[3] & (b)[4]); \
  391. (s2)[18] = (b)[3] ^ (~(b)[4] & (b)[0]); \
  392. (s2)[19] = (b)[4] ^ (~(b)[0] & (b)[1]); \
  393. (b)[0] = S((s1), 19); \
  394. (b)[1] = S((s1), 20); \
  395. (b)[2] = S((s1), 21); \
  396. (b)[3] = S((s1), 22); \
  397. (b)[4] = S((s1), 23); \
  398. (s2)[20] = (b)[0] ^ (~(b)[1] & (b)[2]); \
  399. (s2)[21] = (b)[1] ^ (~(b)[2] & (b)[3]); \
  400. (s2)[22] = (b)[2] ^ (~(b)[3] & (b)[4]); \
  401. (s2)[23] = (b)[3] ^ (~(b)[4] & (b)[0]); \
  402. (s2)[24] = (b)[4] ^ (~(b)[0] & (b)[1]); \
  403. } \
  404. while (0)
  405. #else
  406. /* Mix the row values.
  407. * a ^ (~b & c) == a ^ (c & (b ^ c)) == (a ^ b) ^ (b | c)
  408. *
  409. * s2 The new state.
  410. * s1 The current state.
  411. * b Temporary array of XORed row values.
  412. * t12 Temporary variable.
  413. * t34 Temporary variable.
  414. */
  415. #define ROW_MIX(s2, s1, b, t12, t34) \
  416. do { \
  417. (b)[0] = (s1)[0]; \
  418. (b)[1] = S((s1), 0); \
  419. (b)[2] = S((s1), 1); \
  420. (b)[3] = S((s1), 2); \
  421. (b)[4] = S((s1), 3); \
  422. (t12) = ((b)[1] ^ (b)[2]); (t34) = ((b)[3] ^ (b)[4]); \
  423. (s2)[0] = (b)[0] ^ ((b)[2] & (t12)); \
  424. (s2)[1] = (t12) ^ ((b)[2] | (b)[3]); \
  425. (s2)[2] = (b)[2] ^ ((b)[4] & (t34)); \
  426. (s2)[3] = (t34) ^ ((b)[4] | (b)[0]); \
  427. (s2)[4] = (b)[4] ^ ((b)[1] & ((b)[0] ^ (b)[1])); \
  428. (b)[0] = S((s1), 4); \
  429. (b)[1] = S((s1), 5); \
  430. (b)[2] = S((s1), 6); \
  431. (b)[3] = S((s1), 7); \
  432. (b)[4] = S((s1), 8); \
  433. (t12) = ((b)[1] ^ (b)[2]); (t34) = ((b)[3] ^ (b)[4]); \
  434. (s2)[5] = (b)[0] ^ ((b)[2] & (t12)); \
  435. (s2)[6] = (t12) ^ ((b)[2] | (b)[3]); \
  436. (s2)[7] = (b)[2] ^ ((b)[4] & (t34)); \
  437. (s2)[8] = (t34) ^ ((b)[4] | (b)[0]); \
  438. (s2)[9] = (b)[4] ^ ((b)[1] & ((b)[0] ^ (b)[1])); \
  439. (b)[0] = S((s1), 9); \
  440. (b)[1] = S((s1), 10); \
  441. (b)[2] = S((s1), 11); \
  442. (b)[3] = S((s1), 12); \
  443. (b)[4] = S((s1), 13); \
  444. (t12) = ((b)[1] ^ (b)[2]); (t34) = ((b)[3] ^ (b)[4]); \
  445. (s2)[10] = (b)[0] ^ ((b)[2] & (t12)); \
  446. (s2)[11] = (t12) ^ ((b)[2] | (b)[3]); \
  447. (s2)[12] = (b)[2] ^ ((b)[4] & (t34)); \
  448. (s2)[13] = (t34) ^ ((b)[4] | (b)[0]); \
  449. (s2)[14] = (b)[4] ^ ((b)[1] & ((b)[0] ^ (b)[1])); \
  450. (b)[0] = S((s1), 14); \
  451. (b)[1] = S((s1), 15); \
  452. (b)[2] = S((s1), 16); \
  453. (b)[3] = S((s1), 17); \
  454. (b)[4] = S((s1), 18); \
  455. (t12) = ((b)[1] ^ (b)[2]); (t34) = ((b)[3] ^ (b)[4]); \
  456. (s2)[15] = (b)[0] ^ ((b)[2] & (t12)); \
  457. (s2)[16] = (t12) ^ ((b)[2] | (b)[3]); \
  458. (s2)[17] = (b)[2] ^ ((b)[4] & (t34)); \
  459. (s2)[18] = (t34) ^ ((b)[4] | (b)[0]); \
  460. (s2)[19] = (b)[4] ^ ((b)[1] & ((b)[0] ^ (b)[1])); \
  461. (b)[0] = S((s1), 19); \
  462. (b)[1] = S((s1), 20); \
  463. (b)[2] = S((s1), 21); \
  464. (b)[3] = S((s1), 22); \
  465. (b)[4] = S((s1), 23); \
  466. (t12) = ((b)[1] ^ (b)[2]); (t34) = ((b)[3] ^ (b)[4]); \
  467. (s2)[20] = (b)[0] ^ ((b)[2] & (t12)); \
  468. (s2)[21] = (t12) ^ ((b)[2] | (b)[3]); \
  469. (s2)[22] = (b)[2] ^ ((b)[4] & (t34)); \
  470. (s2)[23] = (t34) ^ ((b)[4] | (b)[0]); \
  471. (s2)[24] = (b)[4] ^ ((b)[1] & ((b)[0] ^ (b)[1])); \
  472. } \
  473. while (0)
  474. #endif /* SHA3_BY_SPEC */
  475. /* The block operation performed on the state.
  476. *
  477. * s The state.
  478. */
  479. static void BlockSha3(word64 *s)
  480. {
  481. word64 n[25];
  482. word64 b[5];
  483. word64 t0;
  484. #ifndef SHA3_BY_SPEC
  485. word64 t1;
  486. #endif
  487. byte i;
  488. for (i = 0; i < 24; i += 2)
  489. {
  490. COL_MIX(s, b, x, t0);
  491. ROW_MIX(n, s, b, t0, t1);
  492. n[0] ^= hash_keccak_r[i];
  493. COL_MIX(n, b, x, t0);
  494. ROW_MIX(s, n, b, t0, t1);
  495. s[0] ^= hash_keccak_r[i+1];
  496. }
  497. }
  498. #endif /* WOLFSSL_SHA3_SMALL */
  499. #endif /* !WOLFSSL_ARMASM */
  500. static WC_INLINE word64 Load64Unaligned(const unsigned char *a)
  501. {
  502. return ((word64)a[0] << 0) |
  503. ((word64)a[1] << 8) |
  504. ((word64)a[2] << 16) |
  505. ((word64)a[3] << 24) |
  506. ((word64)a[4] << 32) |
  507. ((word64)a[5] << 40) |
  508. ((word64)a[6] << 48) |
  509. ((word64)a[7] << 56);
  510. }
  511. /* Convert the array of bytes, in little-endian order, to a 64-bit integer.
  512. *
  513. * a Array of bytes.
  514. * returns a 64-bit integer.
  515. */
  516. static word64 Load64BitBigEndian(const byte* a)
  517. {
  518. #if defined(BIG_ENDIAN_ORDER) || (WOLFSSL_GENERAL_ALIGNMENT == 1)
  519. word64 n = 0;
  520. int i;
  521. for (i = 0; i < 8; i++)
  522. n |= (word64)a[i] << (8 * i);
  523. return n;
  524. #elif ((WOLFSSL_GENERAL_ALIGNMENT > 0) && (WOLFSSL_GENERAL_ALIGNMENT == 4))
  525. word64 n;
  526. n = *(word32*) a;
  527. n |= ((word64)*(word32*)(a + 4)) << 32;
  528. return n;
  529. #elif ((WOLFSSL_GENERAL_ALIGNMENT > 0) && (WOLFSSL_GENERAL_ALIGNMENT == 2))
  530. word64 n;
  531. n = *(word16*) a;
  532. n |= ((word64)*(word16*)(a + 2)) << 16;
  533. n |= ((word64)*(word16*)(a + 4)) << 32;
  534. n |= ((word64)*(word16*)(a + 6)) << 48;
  535. return n;
  536. #else
  537. return *(const word64*)a;
  538. #endif
  539. }
  540. /* Initialize the state for a SHA3-224 hash operation.
  541. *
  542. * sha3 wc_Sha3 object holding state.
  543. * returns 0 on success.
  544. */
  545. static int InitSha3(wc_Sha3* sha3)
  546. {
  547. int i;
  548. for (i = 0; i < 25; i++)
  549. sha3->s[i] = 0;
  550. sha3->i = 0;
  551. #ifdef WOLFSSL_HASH_FLAGS
  552. sha3->flags = 0;
  553. #endif
  554. return 0;
  555. }
  556. /* Update the SHA-3 hash state with message data.
  557. *
  558. * sha3 wc_Sha3 object holding state.
  559. * data Message data to be hashed.
  560. * len Length of the message data.
  561. * p Number of 64-bit numbers in a block of data to process.
  562. * returns 0 on success.
  563. */
  564. static int Sha3Update(wc_Sha3* sha3, const byte* data, word32 len, byte p)
  565. {
  566. word32 i;
  567. byte l;
  568. byte *t;
  569. if (sha3->i > 0) {
  570. l = p * 8 - sha3->i;
  571. if (l > len) {
  572. l = (byte)len;
  573. }
  574. t = &sha3->t[sha3->i];
  575. for (i = 0; i < l; i++)
  576. t[i] = data[i];
  577. data += i;
  578. len -= i;
  579. sha3->i += (byte) i;
  580. if (sha3->i == p * 8) {
  581. for (i = 0; i < p; i++)
  582. sha3->s[i] ^= Load64BitBigEndian(sha3->t + 8 * i);
  583. BlockSha3(sha3->s);
  584. sha3->i = 0;
  585. }
  586. }
  587. while (len >= ((word32)(p * 8))) {
  588. for (i = 0; i < p; i++)
  589. sha3->s[i] ^= Load64Unaligned(data + 8 * i);
  590. BlockSha3(sha3->s);
  591. len -= p * 8;
  592. data += p * 8;
  593. }
  594. for (i = 0; i < len; i++)
  595. sha3->t[i] = data[i];
  596. sha3->i += (byte) i;
  597. return 0;
  598. }
  599. /* Calculate the SHA-3 hash based on all the message data seen.
  600. *
  601. * sha3 wc_Sha3 object holding state.
  602. * hash Buffer to hold the hash result.
  603. * p Number of 64-bit numbers in a block of data to process.
  604. * len Number of bytes in output.
  605. * returns 0 on success.
  606. */
  607. static int Sha3Final(wc_Sha3* sha3, byte padChar, byte* hash, byte p, word32 l)
  608. {
  609. word32 rate = p * 8;
  610. word32 j;
  611. word32 i;
  612. sha3->t[rate - 1] = 0x00;
  613. #ifdef WOLFSSL_HASH_FLAGS
  614. if (p == WC_SHA3_256_COUNT && sha3->flags & WC_HASH_SHA3_KECCAK256)
  615. padChar = 0x01;
  616. #endif
  617. sha3->t[sha3->i ] = padChar;
  618. sha3->t[rate - 1] |= 0x80;
  619. for (i=sha3->i + 1; i < rate - 1; i++)
  620. sha3->t[i] = 0;
  621. for (i = 0; i < p; i++)
  622. sha3->s[i] ^= Load64BitBigEndian(sha3->t + 8 * i);
  623. for (j = 0; l - j >= rate; j += rate) {
  624. BlockSha3(sha3->s);
  625. #if defined(BIG_ENDIAN_ORDER)
  626. ByteReverseWords64((word64*)(hash + j), sha3->s, rate);
  627. #else
  628. XMEMCPY(hash + j, sha3->s, rate);
  629. #endif
  630. }
  631. if (j != l) {
  632. BlockSha3(sha3->s);
  633. #if defined(BIG_ENDIAN_ORDER)
  634. ByteReverseWords64(sha3->s, sha3->s, rate);
  635. #endif
  636. XMEMCPY(hash + j, sha3->s, l - j);
  637. }
  638. return 0;
  639. }
  640. /* Initialize the state for a SHA-3 hash operation.
  641. *
  642. * sha3 wc_Sha3 object holding state.
  643. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  644. * devId Device identifier for asynchronous operation.
  645. * returns 0 on success.
  646. */
  647. static int wc_InitSha3(wc_Sha3* sha3, void* heap, int devId)
  648. {
  649. int ret = 0;
  650. if (sha3 == NULL)
  651. return BAD_FUNC_ARG;
  652. sha3->heap = heap;
  653. ret = InitSha3(sha3);
  654. if (ret != 0)
  655. return ret;
  656. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  657. ret = wolfAsync_DevCtxInit(&sha3->asyncDev,
  658. WOLFSSL_ASYNC_MARKER_SHA3, sha3->heap, devId);
  659. #else
  660. (void)devId;
  661. #endif /* WOLFSSL_ASYNC_CRYPT */
  662. return ret;
  663. }
  664. /* Update the SHA-3 hash state with message data.
  665. *
  666. * sha3 wc_Sha3 object holding state.
  667. * data Message data to be hashed.
  668. * len Length of the message data.
  669. * p Number of 64-bit numbers in a block of data to process.
  670. * returns 0 on success.
  671. */
  672. static int wc_Sha3Update(wc_Sha3* sha3, const byte* data, word32 len, byte p)
  673. {
  674. int ret;
  675. if (sha3 == NULL || (data == NULL && len > 0)) {
  676. return BAD_FUNC_ARG;
  677. }
  678. if (data == NULL && len == 0) {
  679. /* valid, but do nothing */
  680. return 0;
  681. }
  682. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  683. if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) {
  684. #if defined(HAVE_INTEL_QA) && defined(QAT_V2)
  685. /* QAT only supports SHA3_256 */
  686. if (p == WC_SHA3_256_COUNT) {
  687. ret = IntelQaSymSha3(&sha3->asyncDev, NULL, data, len);
  688. if (ret != NOT_COMPILED_IN)
  689. return ret;
  690. /* fall-through when unavailable */
  691. }
  692. #endif
  693. }
  694. #endif /* WOLFSSL_ASYNC_CRYPT */
  695. ret = Sha3Update(sha3, data, len, p);
  696. return ret;
  697. }
  698. /* Calculate the SHA-3 hash based on all the message data seen.
  699. *
  700. * sha3 wc_Sha3 object holding state.
  701. * hash Buffer to hold the hash result.
  702. * p Number of 64-bit numbers in a block of data to process.
  703. * len Number of bytes in output.
  704. * returns 0 on success.
  705. */
  706. static int wc_Sha3Final(wc_Sha3* sha3, byte* hash, byte p, byte len)
  707. {
  708. int ret;
  709. if (sha3 == NULL || hash == NULL) {
  710. return BAD_FUNC_ARG;
  711. }
  712. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  713. if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) {
  714. #if defined(HAVE_INTEL_QA) && defined(QAT_V2)
  715. /* QAT only supports SHA3_256 */
  716. /* QAT SHA-3 only supported on v2 (8970 or later cards) */
  717. if (len == WC_SHA3_256_DIGEST_SIZE) {
  718. ret = IntelQaSymSha3(&sha3->asyncDev, hash, NULL, len);
  719. if (ret != NOT_COMPILED_IN)
  720. return ret;
  721. /* fall-through when unavailable */
  722. }
  723. #endif
  724. }
  725. #endif /* WOLFSSL_ASYNC_CRYPT */
  726. ret = Sha3Final(sha3, 0x06, hash, p, (word32)len);
  727. if (ret != 0)
  728. return ret;
  729. return InitSha3(sha3); /* reset state */
  730. }
  731. /* Dispose of any dynamically allocated data from the SHA3-384 operation.
  732. * (Required for async ops.)
  733. *
  734. * sha3 wc_Sha3 object holding state.
  735. * returns 0 on success.
  736. */
  737. static void wc_Sha3Free(wc_Sha3* sha3)
  738. {
  739. (void)sha3;
  740. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  741. if (sha3 == NULL)
  742. return;
  743. wolfAsync_DevCtxFree(&sha3->asyncDev, WOLFSSL_ASYNC_MARKER_SHA3);
  744. #endif /* WOLFSSL_ASYNC_CRYPT */
  745. }
  746. /* Copy the state of the SHA3 operation.
  747. *
  748. * src wc_Sha3 object holding state top copy.
  749. * dst wc_Sha3 object to copy into.
  750. * returns 0 on success.
  751. */
  752. static int wc_Sha3Copy(wc_Sha3* src, wc_Sha3* dst)
  753. {
  754. int ret = 0;
  755. if (src == NULL || dst == NULL)
  756. return BAD_FUNC_ARG;
  757. XMEMCPY(dst, src, sizeof(wc_Sha3));
  758. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  759. ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
  760. #endif
  761. #ifdef WOLFSSL_HASH_FLAGS
  762. dst->flags |= WC_HASH_FLAG_ISCOPY;
  763. #endif
  764. return ret;
  765. }
  766. /* Calculate the SHA3-224 hash based on all the message data so far.
  767. * More message data can be added, after this operation, using the current
  768. * state.
  769. *
  770. * sha3 wc_Sha3 object holding state.
  771. * hash Buffer to hold the hash result. Must be at least 28 bytes.
  772. * p Number of 64-bit numbers in a block of data to process.
  773. * len Number of bytes in output.
  774. * returns 0 on success.
  775. */
  776. static int wc_Sha3GetHash(wc_Sha3* sha3, byte* hash, byte p, byte len)
  777. {
  778. int ret;
  779. wc_Sha3 tmpSha3;
  780. if (sha3 == NULL || hash == NULL)
  781. return BAD_FUNC_ARG;
  782. ret = wc_Sha3Copy(sha3, &tmpSha3);
  783. if (ret == 0) {
  784. ret = wc_Sha3Final(&tmpSha3, hash, p, len);
  785. }
  786. return ret;
  787. }
  788. /* Initialize the state for a SHA3-224 hash operation.
  789. *
  790. * sha3 wc_Sha3 object holding state.
  791. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  792. * devId Device identifier for asynchronous operation.
  793. * returns 0 on success.
  794. */
  795. int wc_InitSha3_224(wc_Sha3* sha3, void* heap, int devId)
  796. {
  797. return wc_InitSha3(sha3, heap, devId);
  798. }
  799. /* Update the SHA3-224 hash state with message data.
  800. *
  801. * sha3 wc_Sha3 object holding state.
  802. * data Message data to be hashed.
  803. * len Length of the message data.
  804. * returns 0 on success.
  805. */
  806. int wc_Sha3_224_Update(wc_Sha3* sha3, const byte* data, word32 len)
  807. {
  808. return wc_Sha3Update(sha3, data, len, WC_SHA3_224_COUNT);
  809. }
  810. /* Calculate the SHA3-224 hash based on all the message data seen.
  811. * The state is initialized ready for a new message to hash.
  812. *
  813. * sha3 wc_Sha3 object holding state.
  814. * hash Buffer to hold the hash result. Must be at least 28 bytes.
  815. * returns 0 on success.
  816. */
  817. int wc_Sha3_224_Final(wc_Sha3* sha3, byte* hash)
  818. {
  819. return wc_Sha3Final(sha3, hash, WC_SHA3_224_COUNT, WC_SHA3_224_DIGEST_SIZE);
  820. }
  821. /* Dispose of any dynamically allocated data from the SHA3-224 operation.
  822. * (Required for async ops.)
  823. *
  824. * sha3 wc_Sha3 object holding state.
  825. * returns 0 on success.
  826. */
  827. void wc_Sha3_224_Free(wc_Sha3* sha3)
  828. {
  829. wc_Sha3Free(sha3);
  830. }
  831. /* Calculate the SHA3-224 hash based on all the message data so far.
  832. * More message data can be added, after this operation, using the current
  833. * state.
  834. *
  835. * sha3 wc_Sha3 object holding state.
  836. * hash Buffer to hold the hash result. Must be at least 28 bytes.
  837. * returns 0 on success.
  838. */
  839. int wc_Sha3_224_GetHash(wc_Sha3* sha3, byte* hash)
  840. {
  841. return wc_Sha3GetHash(sha3, hash, WC_SHA3_224_COUNT, WC_SHA3_224_DIGEST_SIZE);
  842. }
  843. /* Copy the state of the SHA3-224 operation.
  844. *
  845. * src wc_Sha3 object holding state top copy.
  846. * dst wc_Sha3 object to copy into.
  847. * returns 0 on success.
  848. */
  849. int wc_Sha3_224_Copy(wc_Sha3* src, wc_Sha3* dst)
  850. {
  851. return wc_Sha3Copy(src, dst);
  852. }
  853. /* Initialize the state for a SHA3-256 hash operation.
  854. *
  855. * sha3 wc_Sha3 object holding state.
  856. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  857. * devId Device identifier for asynchronous operation.
  858. * returns 0 on success.
  859. */
  860. int wc_InitSha3_256(wc_Sha3* sha3, void* heap, int devId)
  861. {
  862. return wc_InitSha3(sha3, heap, devId);
  863. }
  864. /* Update the SHA3-256 hash state with message data.
  865. *
  866. * sha3 wc_Sha3 object holding state.
  867. * data Message data to be hashed.
  868. * len Length of the message data.
  869. * returns 0 on success.
  870. */
  871. int wc_Sha3_256_Update(wc_Sha3* sha3, const byte* data, word32 len)
  872. {
  873. return wc_Sha3Update(sha3, data, len, WC_SHA3_256_COUNT);
  874. }
  875. /* Calculate the SHA3-256 hash based on all the message data seen.
  876. * The state is initialized ready for a new message to hash.
  877. *
  878. * sha3 wc_Sha3 object holding state.
  879. * hash Buffer to hold the hash result. Must be at least 32 bytes.
  880. * returns 0 on success.
  881. */
  882. int wc_Sha3_256_Final(wc_Sha3* sha3, byte* hash)
  883. {
  884. return wc_Sha3Final(sha3, hash, WC_SHA3_256_COUNT, WC_SHA3_256_DIGEST_SIZE);
  885. }
  886. /* Dispose of any dynamically allocated data from the SHA3-256 operation.
  887. * (Required for async ops.)
  888. *
  889. * sha3 wc_Sha3 object holding state.
  890. * returns 0 on success.
  891. */
  892. void wc_Sha3_256_Free(wc_Sha3* sha3)
  893. {
  894. wc_Sha3Free(sha3);
  895. }
  896. /* Calculate the SHA3-256 hash based on all the message data so far.
  897. * More message data can be added, after this operation, using the current
  898. * state.
  899. *
  900. * sha3 wc_Sha3 object holding state.
  901. * hash Buffer to hold the hash result. Must be at least 32 bytes.
  902. * returns 0 on success.
  903. */
  904. int wc_Sha3_256_GetHash(wc_Sha3* sha3, byte* hash)
  905. {
  906. return wc_Sha3GetHash(sha3, hash, WC_SHA3_256_COUNT, WC_SHA3_256_DIGEST_SIZE);
  907. }
  908. /* Copy the state of the SHA3-256 operation.
  909. *
  910. * src wc_Sha3 object holding state top copy.
  911. * dst wc_Sha3 object to copy into.
  912. * returns 0 on success.
  913. */
  914. int wc_Sha3_256_Copy(wc_Sha3* src, wc_Sha3* dst)
  915. {
  916. return wc_Sha3Copy(src, dst);
  917. }
  918. /* Initialize the state for a SHA3-384 hash operation.
  919. *
  920. * sha3 wc_Sha3 object holding state.
  921. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  922. * devId Device identifier for asynchronous operation.
  923. * returns 0 on success.
  924. */
  925. int wc_InitSha3_384(wc_Sha3* sha3, void* heap, int devId)
  926. {
  927. return wc_InitSha3(sha3, heap, devId);
  928. }
  929. /* Update the SHA3-384 hash state with message data.
  930. *
  931. * sha3 wc_Sha3 object holding state.
  932. * data Message data to be hashed.
  933. * len Length of the message data.
  934. * returns 0 on success.
  935. */
  936. int wc_Sha3_384_Update(wc_Sha3* sha3, const byte* data, word32 len)
  937. {
  938. return wc_Sha3Update(sha3, data, len, WC_SHA3_384_COUNT);
  939. }
  940. /* Calculate the SHA3-384 hash based on all the message data seen.
  941. * The state is initialized ready for a new message to hash.
  942. *
  943. * sha3 wc_Sha3 object holding state.
  944. * hash Buffer to hold the hash result. Must be at least 48 bytes.
  945. * returns 0 on success.
  946. */
  947. int wc_Sha3_384_Final(wc_Sha3* sha3, byte* hash)
  948. {
  949. return wc_Sha3Final(sha3, hash, WC_SHA3_384_COUNT, WC_SHA3_384_DIGEST_SIZE);
  950. }
  951. /* Dispose of any dynamically allocated data from the SHA3-384 operation.
  952. * (Required for async ops.)
  953. *
  954. * sha3 wc_Sha3 object holding state.
  955. * returns 0 on success.
  956. */
  957. void wc_Sha3_384_Free(wc_Sha3* sha3)
  958. {
  959. wc_Sha3Free(sha3);
  960. }
  961. /* Calculate the SHA3-384 hash based on all the message data so far.
  962. * More message data can be added, after this operation, using the current
  963. * state.
  964. *
  965. * sha3 wc_Sha3 object holding state.
  966. * hash Buffer to hold the hash result. Must be at least 48 bytes.
  967. * returns 0 on success.
  968. */
  969. int wc_Sha3_384_GetHash(wc_Sha3* sha3, byte* hash)
  970. {
  971. return wc_Sha3GetHash(sha3, hash, WC_SHA3_384_COUNT, WC_SHA3_384_DIGEST_SIZE);
  972. }
  973. /* Copy the state of the SHA3-384 operation.
  974. *
  975. * src wc_Sha3 object holding state top copy.
  976. * dst wc_Sha3 object to copy into.
  977. * returns 0 on success.
  978. */
  979. int wc_Sha3_384_Copy(wc_Sha3* src, wc_Sha3* dst)
  980. {
  981. return wc_Sha3Copy(src, dst);
  982. }
  983. /* Initialize the state for a SHA3-512 hash operation.
  984. *
  985. * sha3 wc_Sha3 object holding state.
  986. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  987. * devId Device identifier for asynchronous operation.
  988. * returns 0 on success.
  989. */
  990. int wc_InitSha3_512(wc_Sha3* sha3, void* heap, int devId)
  991. {
  992. return wc_InitSha3(sha3, heap, devId);
  993. }
  994. /* Update the SHA3-512 hash state with message data.
  995. *
  996. * sha3 wc_Sha3 object holding state.
  997. * data Message data to be hashed.
  998. * len Length of the message data.
  999. * returns 0 on success.
  1000. */
  1001. int wc_Sha3_512_Update(wc_Sha3* sha3, const byte* data, word32 len)
  1002. {
  1003. return wc_Sha3Update(sha3, data, len, WC_SHA3_512_COUNT);
  1004. }
  1005. /* Calculate the SHA3-512 hash based on all the message data seen.
  1006. * The state is initialized ready for a new message to hash.
  1007. *
  1008. * sha3 wc_Sha3 object holding state.
  1009. * hash Buffer to hold the hash result. Must be at least 64 bytes.
  1010. * returns 0 on success.
  1011. */
  1012. int wc_Sha3_512_Final(wc_Sha3* sha3, byte* hash)
  1013. {
  1014. return wc_Sha3Final(sha3, hash, WC_SHA3_512_COUNT, WC_SHA3_512_DIGEST_SIZE);
  1015. }
  1016. /* Dispose of any dynamically allocated data from the SHA3-512 operation.
  1017. * (Required for async ops.)
  1018. *
  1019. * sha3 wc_Sha3 object holding state.
  1020. * returns 0 on success.
  1021. */
  1022. void wc_Sha3_512_Free(wc_Sha3* sha3)
  1023. {
  1024. wc_Sha3Free(sha3);
  1025. }
  1026. /* Calculate the SHA3-512 hash based on all the message data so far.
  1027. * More message data can be added, after this operation, using the current
  1028. * state.
  1029. *
  1030. * sha3 wc_Sha3 object holding state.
  1031. * hash Buffer to hold the hash result. Must be at least 64 bytes.
  1032. * returns 0 on success.
  1033. */
  1034. int wc_Sha3_512_GetHash(wc_Sha3* sha3, byte* hash)
  1035. {
  1036. return wc_Sha3GetHash(sha3, hash, WC_SHA3_512_COUNT, WC_SHA3_512_DIGEST_SIZE);
  1037. }
  1038. /* Copy the state of the SHA3-512 operation.
  1039. *
  1040. * src wc_Sha3 object holding state top copy.
  1041. * dst wc_Sha3 object to copy into.
  1042. * returns 0 on success.
  1043. */
  1044. int wc_Sha3_512_Copy(wc_Sha3* src, wc_Sha3* dst)
  1045. {
  1046. return wc_Sha3Copy(src, dst);
  1047. }
  1048. #ifdef WOLFSSL_HASH_FLAGS
  1049. int wc_Sha3_SetFlags(wc_Sha3* sha3, word32 flags)
  1050. {
  1051. if (sha3) {
  1052. sha3->flags = flags;
  1053. }
  1054. return 0;
  1055. }
  1056. int wc_Sha3_GetFlags(wc_Sha3* sha3, word32* flags)
  1057. {
  1058. if (sha3 && flags) {
  1059. *flags = sha3->flags;
  1060. }
  1061. return 0;
  1062. }
  1063. #endif
  1064. #ifdef WOLFSSL_SHAKE256
  1065. /* Initialize the state for a Shake256 hash operation.
  1066. *
  1067. * shake wc_Shake object holding state.
  1068. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  1069. * devId Device identifier for asynchronous operation.
  1070. * returns 0 on success.
  1071. */
  1072. int wc_InitShake256(wc_Shake* shake, void* heap, int devId)
  1073. {
  1074. return wc_InitSha3(shake, heap, devId);
  1075. }
  1076. /* Update the SHAKE256 hash state with message data.
  1077. *
  1078. * shake wc_Shake object holding state.
  1079. * data Message data to be hashed.
  1080. * len Length of the message data.
  1081. * returns 0 on success.
  1082. */
  1083. int wc_Shake256_Update(wc_Shake* shake, const byte* data, word32 len)
  1084. {
  1085. if (shake == NULL || (data == NULL && len > 0)) {
  1086. return BAD_FUNC_ARG;
  1087. }
  1088. if (data == NULL && len == 0) {
  1089. /* valid, but do nothing */
  1090. return 0;
  1091. }
  1092. return Sha3Update(shake, data, len, WC_SHA3_256_COUNT);
  1093. }
  1094. /* Calculate the SHAKE256 hash based on all the message data seen.
  1095. * The state is initialized ready for a new message to hash.
  1096. *
  1097. * shake wc_Shake object holding state.
  1098. * hash Buffer to hold the hash result. Must be at least 64 bytes.
  1099. * returns 0 on success.
  1100. */
  1101. int wc_Shake256_Final(wc_Shake* shake, byte* hash, word32 hashLen)
  1102. {
  1103. int ret;
  1104. if (shake == NULL || hash == NULL) {
  1105. return BAD_FUNC_ARG;
  1106. }
  1107. ret = Sha3Final(shake, 0x1f, hash, WC_SHA3_256_COUNT, hashLen);
  1108. if (ret != 0)
  1109. return ret;
  1110. return InitSha3(shake); /* reset state */
  1111. }
  1112. /* Dispose of any dynamically allocated data from the SHAKE256 operation.
  1113. * (Required for async ops.)
  1114. *
  1115. * shake wc_Shake object holding state.
  1116. * returns 0 on success.
  1117. */
  1118. void wc_Shake256_Free(wc_Shake* shake)
  1119. {
  1120. wc_Sha3Free(shake);
  1121. }
  1122. /* Copy the state of the SHA3-512 operation.
  1123. *
  1124. * src wc_Shake object holding state top copy.
  1125. * dst wc_Shake object to copy into.
  1126. * returns 0 on success.
  1127. */
  1128. int wc_Shake256_Copy(wc_Shake* src, wc_Shake* dst)
  1129. {
  1130. return wc_Sha3Copy(src, dst);
  1131. }
  1132. #endif
  1133. #endif /* WOLFSSL_SHA3 */