sha3.c 44 KB

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