sha3.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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 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. byte l;
  598. byte *t;
  599. word32 blocks;
  600. if (sha3->i > 0) {
  601. l = p * 8 - sha3->i;
  602. if (l > len) {
  603. l = (byte)len;
  604. }
  605. t = &sha3->t[sha3->i];
  606. for (i = 0; i < l; i++) {
  607. t[i] = data[i];
  608. }
  609. data += i;
  610. len -= i;
  611. sha3->i += (byte) i;
  612. if (sha3->i == p * 8) {
  613. for (i = 0; i < p; i++) {
  614. sha3->s[i] ^= Load64BitBigEndian(sha3->t + 8 * i);
  615. }
  616. #ifdef USE_INTEL_SPEEDUP
  617. (*sha3_block)(sha3->s);
  618. #else
  619. BlockSha3(sha3->s);
  620. #endif
  621. sha3->i = 0;
  622. }
  623. }
  624. blocks = len / (p * 8);
  625. #ifdef USE_INTEL_SPEEDUP
  626. if ((sha3_block_n != NULL) && (blocks > 0)) {
  627. (*sha3_block_n)(sha3->s, data, blocks, p * 8);
  628. len -= blocks * (p * 8);
  629. data += blocks * (p * 8);
  630. blocks = 0;
  631. }
  632. #endif
  633. for (; blocks > 0; blocks--) {
  634. for (i = 0; i < p; i++) {
  635. sha3->s[i] ^= Load64Unaligned(data + 8 * i);
  636. }
  637. #ifdef USE_INTEL_SPEEDUP
  638. (*sha3_block)(sha3->s);
  639. #else
  640. BlockSha3(sha3->s);
  641. #endif
  642. len -= p * 8;
  643. data += p * 8;
  644. }
  645. XMEMCPY(sha3->t, data, len);
  646. sha3->i += len;
  647. return 0;
  648. }
  649. /* Calculate the SHA-3 hash based on all the message data seen.
  650. *
  651. * sha3 wc_Sha3 object holding state.
  652. * hash Buffer to hold the hash result.
  653. * p Number of 64-bit numbers in a block of data to process.
  654. * len Number of bytes in output.
  655. * returns 0 on success.
  656. */
  657. static int Sha3Final(wc_Sha3* sha3, byte padChar, byte* hash, byte p, word32 l)
  658. {
  659. word32 rate = p * 8;
  660. word32 j;
  661. word32 i;
  662. sha3->t[rate - 1] = 0x00;
  663. #ifdef WOLFSSL_HASH_FLAGS
  664. if ((p == WC_SHA3_256_COUNT) && (sha3->flags & WC_HASH_SHA3_KECCAK256))
  665. padChar = 0x01;
  666. #endif
  667. sha3->t[sha3->i ] = padChar;
  668. sha3->t[rate - 1] |= 0x80;
  669. if (rate - 1 > (word32)sha3->i + 1) {
  670. XMEMSET(sha3->t + sha3->i + 1, 0, rate - 1 - (sha3->i + 1));
  671. }
  672. for (i = 0; i < p; i++) {
  673. sha3->s[i] ^= Load64BitBigEndian(sha3->t + 8 * i);
  674. }
  675. for (j = 0; l - j >= rate; j += rate) {
  676. #ifdef USE_INTEL_SPEEDUP
  677. (*sha3_block)(sha3->s);
  678. #else
  679. BlockSha3(sha3->s);
  680. #endif
  681. #if defined(BIG_ENDIAN_ORDER)
  682. ByteReverseWords64((word64*)(hash + j), sha3->s, rate);
  683. #else
  684. XMEMCPY(hash + j, sha3->s, rate);
  685. #endif
  686. }
  687. if (j != l) {
  688. #ifdef USE_INTEL_SPEEDUP
  689. (*sha3_block)(sha3->s);
  690. #else
  691. BlockSha3(sha3->s);
  692. #endif
  693. #if defined(BIG_ENDIAN_ORDER)
  694. ByteReverseWords64(sha3->s, sha3->s, rate);
  695. #endif
  696. XMEMCPY(hash + j, sha3->s, l - j);
  697. }
  698. return 0;
  699. }
  700. /* Initialize the state for a SHA-3 hash operation.
  701. *
  702. * sha3 wc_Sha3 object holding state.
  703. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  704. * devId Device identifier for asynchronous operation.
  705. * returns 0 on success.
  706. */
  707. static int wc_InitSha3(wc_Sha3* sha3, void* heap, int devId)
  708. {
  709. int ret = 0;
  710. if (sha3 == NULL)
  711. return BAD_FUNC_ARG;
  712. sha3->heap = heap;
  713. ret = InitSha3(sha3);
  714. if (ret != 0)
  715. return ret;
  716. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  717. ret = wolfAsync_DevCtxInit(&sha3->asyncDev,
  718. WOLFSSL_ASYNC_MARKER_SHA3, sha3->heap, devId);
  719. #else
  720. (void)devId;
  721. #endif /* WOLFSSL_ASYNC_CRYPT */
  722. return ret;
  723. }
  724. /* Update the SHA-3 hash state with message data.
  725. *
  726. * sha3 wc_Sha3 object holding state.
  727. * data Message data to be hashed.
  728. * len Length of the message data.
  729. * p Number of 64-bit numbers in a block of data to process.
  730. * returns 0 on success.
  731. */
  732. static int wc_Sha3Update(wc_Sha3* sha3, const byte* data, word32 len, byte p)
  733. {
  734. int ret;
  735. if (sha3 == NULL || (data == NULL && len > 0)) {
  736. return BAD_FUNC_ARG;
  737. }
  738. if (data == NULL && len == 0) {
  739. /* valid, but do nothing */
  740. return 0;
  741. }
  742. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  743. if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) {
  744. #if defined(HAVE_INTEL_QA) && defined(QAT_V2)
  745. /* QAT only supports SHA3_256 */
  746. if (p == WC_SHA3_256_COUNT) {
  747. ret = IntelQaSymSha3(&sha3->asyncDev, NULL, data, len);
  748. if (ret != NOT_COMPILED_IN)
  749. return ret;
  750. /* fall-through when unavailable */
  751. }
  752. #endif
  753. }
  754. #endif /* WOLFSSL_ASYNC_CRYPT */
  755. ret = Sha3Update(sha3, data, len, p);
  756. return ret;
  757. }
  758. /* Calculate the SHA-3 hash based on all the message data seen.
  759. *
  760. * sha3 wc_Sha3 object holding state.
  761. * hash Buffer to hold the hash result.
  762. * p Number of 64-bit numbers in a block of data to process.
  763. * len Number of bytes in output.
  764. * returns 0 on success.
  765. */
  766. static int wc_Sha3Final(wc_Sha3* sha3, byte* hash, byte p, byte len)
  767. {
  768. int ret;
  769. if (sha3 == NULL || hash == NULL) {
  770. return BAD_FUNC_ARG;
  771. }
  772. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  773. if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) {
  774. #if defined(HAVE_INTEL_QA) && defined(QAT_V2)
  775. /* QAT only supports SHA3_256 */
  776. /* QAT SHA-3 only supported on v2 (8970 or later cards) */
  777. if (len == WC_SHA3_256_DIGEST_SIZE) {
  778. ret = IntelQaSymSha3(&sha3->asyncDev, hash, NULL, len);
  779. if (ret != NOT_COMPILED_IN)
  780. return ret;
  781. /* fall-through when unavailable */
  782. }
  783. #endif
  784. }
  785. #endif /* WOLFSSL_ASYNC_CRYPT */
  786. ret = Sha3Final(sha3, 0x06, hash, p, (word32)len);
  787. if (ret != 0)
  788. return ret;
  789. return InitSha3(sha3); /* reset state */
  790. }
  791. /* Dispose of any dynamically allocated data from the SHA3-384 operation.
  792. * (Required for async ops.)
  793. *
  794. * sha3 wc_Sha3 object holding state.
  795. * returns 0 on success.
  796. */
  797. static void wc_Sha3Free(wc_Sha3* sha3)
  798. {
  799. (void)sha3;
  800. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  801. if (sha3 == NULL)
  802. return;
  803. wolfAsync_DevCtxFree(&sha3->asyncDev, WOLFSSL_ASYNC_MARKER_SHA3);
  804. #endif /* WOLFSSL_ASYNC_CRYPT */
  805. }
  806. /* Copy the state of the SHA3 operation.
  807. *
  808. * src wc_Sha3 object holding state top copy.
  809. * dst wc_Sha3 object to copy into.
  810. * returns 0 on success.
  811. */
  812. static int wc_Sha3Copy(wc_Sha3* src, wc_Sha3* dst)
  813. {
  814. int ret = 0;
  815. if (src == NULL || dst == NULL)
  816. return BAD_FUNC_ARG;
  817. XMEMCPY(dst, src, sizeof(wc_Sha3));
  818. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  819. ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
  820. #endif
  821. #ifdef WOLFSSL_HASH_FLAGS
  822. dst->flags |= WC_HASH_FLAG_ISCOPY;
  823. #endif
  824. return ret;
  825. }
  826. /* Calculate the SHA3-224 hash based on all the message data so far.
  827. * More message data can be added, after this operation, using the current
  828. * state.
  829. *
  830. * sha3 wc_Sha3 object holding state.
  831. * hash Buffer to hold the hash result. Must be at least 28 bytes.
  832. * p Number of 64-bit numbers in a block of data to process.
  833. * len Number of bytes in output.
  834. * returns 0 on success.
  835. */
  836. static int wc_Sha3GetHash(wc_Sha3* sha3, byte* hash, byte p, byte len)
  837. {
  838. int ret;
  839. wc_Sha3 tmpSha3;
  840. if (sha3 == NULL || hash == NULL)
  841. return BAD_FUNC_ARG;
  842. ret = wc_Sha3Copy(sha3, &tmpSha3);
  843. if (ret == 0) {
  844. ret = wc_Sha3Final(&tmpSha3, hash, p, len);
  845. }
  846. return ret;
  847. }
  848. /* Initialize the state for a SHA3-224 hash operation.
  849. *
  850. * sha3 wc_Sha3 object holding state.
  851. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  852. * devId Device identifier for asynchronous operation.
  853. * returns 0 on success.
  854. */
  855. int wc_InitSha3_224(wc_Sha3* sha3, void* heap, int devId)
  856. {
  857. return wc_InitSha3(sha3, heap, devId);
  858. }
  859. /* Update the SHA3-224 hash state with message data.
  860. *
  861. * sha3 wc_Sha3 object holding state.
  862. * data Message data to be hashed.
  863. * len Length of the message data.
  864. * returns 0 on success.
  865. */
  866. int wc_Sha3_224_Update(wc_Sha3* sha3, const byte* data, word32 len)
  867. {
  868. return wc_Sha3Update(sha3, data, len, WC_SHA3_224_COUNT);
  869. }
  870. /* Calculate the SHA3-224 hash based on all the message data seen.
  871. * The state is initialized ready for a new message to hash.
  872. *
  873. * sha3 wc_Sha3 object holding state.
  874. * hash Buffer to hold the hash result. Must be at least 28 bytes.
  875. * returns 0 on success.
  876. */
  877. int wc_Sha3_224_Final(wc_Sha3* sha3, byte* hash)
  878. {
  879. return wc_Sha3Final(sha3, hash, WC_SHA3_224_COUNT, WC_SHA3_224_DIGEST_SIZE);
  880. }
  881. /* Dispose of any dynamically allocated data from the SHA3-224 operation.
  882. * (Required for async ops.)
  883. *
  884. * sha3 wc_Sha3 object holding state.
  885. * returns 0 on success.
  886. */
  887. void wc_Sha3_224_Free(wc_Sha3* sha3)
  888. {
  889. wc_Sha3Free(sha3);
  890. }
  891. /* Calculate the SHA3-224 hash based on all the message data so far.
  892. * More message data can be added, after this operation, using the current
  893. * state.
  894. *
  895. * sha3 wc_Sha3 object holding state.
  896. * hash Buffer to hold the hash result. Must be at least 28 bytes.
  897. * returns 0 on success.
  898. */
  899. int wc_Sha3_224_GetHash(wc_Sha3* sha3, byte* hash)
  900. {
  901. return wc_Sha3GetHash(sha3, hash, WC_SHA3_224_COUNT, WC_SHA3_224_DIGEST_SIZE);
  902. }
  903. /* Copy the state of the SHA3-224 operation.
  904. *
  905. * src wc_Sha3 object holding state top copy.
  906. * dst wc_Sha3 object to copy into.
  907. * returns 0 on success.
  908. */
  909. int wc_Sha3_224_Copy(wc_Sha3* src, wc_Sha3* dst)
  910. {
  911. return wc_Sha3Copy(src, dst);
  912. }
  913. /* Initialize the state for a SHA3-256 hash operation.
  914. *
  915. * sha3 wc_Sha3 object holding state.
  916. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  917. * devId Device identifier for asynchronous operation.
  918. * returns 0 on success.
  919. */
  920. int wc_InitSha3_256(wc_Sha3* sha3, void* heap, int devId)
  921. {
  922. return wc_InitSha3(sha3, heap, devId);
  923. }
  924. /* Update the SHA3-256 hash state with message data.
  925. *
  926. * sha3 wc_Sha3 object holding state.
  927. * data Message data to be hashed.
  928. * len Length of the message data.
  929. * returns 0 on success.
  930. */
  931. int wc_Sha3_256_Update(wc_Sha3* sha3, const byte* data, word32 len)
  932. {
  933. return wc_Sha3Update(sha3, data, len, WC_SHA3_256_COUNT);
  934. }
  935. /* Calculate the SHA3-256 hash based on all the message data seen.
  936. * The state is initialized ready for a new message to hash.
  937. *
  938. * sha3 wc_Sha3 object holding state.
  939. * hash Buffer to hold the hash result. Must be at least 32 bytes.
  940. * returns 0 on success.
  941. */
  942. int wc_Sha3_256_Final(wc_Sha3* sha3, byte* hash)
  943. {
  944. return wc_Sha3Final(sha3, hash, WC_SHA3_256_COUNT, WC_SHA3_256_DIGEST_SIZE);
  945. }
  946. /* Dispose of any dynamically allocated data from the SHA3-256 operation.
  947. * (Required for async ops.)
  948. *
  949. * sha3 wc_Sha3 object holding state.
  950. * returns 0 on success.
  951. */
  952. void wc_Sha3_256_Free(wc_Sha3* sha3)
  953. {
  954. wc_Sha3Free(sha3);
  955. }
  956. /* Calculate the SHA3-256 hash based on all the message data so far.
  957. * More message data can be added, after this operation, using the current
  958. * state.
  959. *
  960. * sha3 wc_Sha3 object holding state.
  961. * hash Buffer to hold the hash result. Must be at least 32 bytes.
  962. * returns 0 on success.
  963. */
  964. int wc_Sha3_256_GetHash(wc_Sha3* sha3, byte* hash)
  965. {
  966. return wc_Sha3GetHash(sha3, hash, WC_SHA3_256_COUNT, WC_SHA3_256_DIGEST_SIZE);
  967. }
  968. /* Copy the state of the SHA3-256 operation.
  969. *
  970. * src wc_Sha3 object holding state top copy.
  971. * dst wc_Sha3 object to copy into.
  972. * returns 0 on success.
  973. */
  974. int wc_Sha3_256_Copy(wc_Sha3* src, wc_Sha3* dst)
  975. {
  976. return wc_Sha3Copy(src, dst);
  977. }
  978. /* Initialize the state for a SHA3-384 hash operation.
  979. *
  980. * sha3 wc_Sha3 object holding state.
  981. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  982. * devId Device identifier for asynchronous operation.
  983. * returns 0 on success.
  984. */
  985. int wc_InitSha3_384(wc_Sha3* sha3, void* heap, int devId)
  986. {
  987. return wc_InitSha3(sha3, heap, devId);
  988. }
  989. /* Update the SHA3-384 hash state with message data.
  990. *
  991. * sha3 wc_Sha3 object holding state.
  992. * data Message data to be hashed.
  993. * len Length of the message data.
  994. * returns 0 on success.
  995. */
  996. int wc_Sha3_384_Update(wc_Sha3* sha3, const byte* data, word32 len)
  997. {
  998. return wc_Sha3Update(sha3, data, len, WC_SHA3_384_COUNT);
  999. }
  1000. /* Calculate the SHA3-384 hash based on all the message data seen.
  1001. * The state is initialized ready for a new message to hash.
  1002. *
  1003. * sha3 wc_Sha3 object holding state.
  1004. * hash Buffer to hold the hash result. Must be at least 48 bytes.
  1005. * returns 0 on success.
  1006. */
  1007. int wc_Sha3_384_Final(wc_Sha3* sha3, byte* hash)
  1008. {
  1009. return wc_Sha3Final(sha3, hash, WC_SHA3_384_COUNT, WC_SHA3_384_DIGEST_SIZE);
  1010. }
  1011. /* Dispose of any dynamically allocated data from the SHA3-384 operation.
  1012. * (Required for async ops.)
  1013. *
  1014. * sha3 wc_Sha3 object holding state.
  1015. * returns 0 on success.
  1016. */
  1017. void wc_Sha3_384_Free(wc_Sha3* sha3)
  1018. {
  1019. wc_Sha3Free(sha3);
  1020. }
  1021. /* Calculate the SHA3-384 hash based on all the message data so far.
  1022. * More message data can be added, after this operation, using the current
  1023. * state.
  1024. *
  1025. * sha3 wc_Sha3 object holding state.
  1026. * hash Buffer to hold the hash result. Must be at least 48 bytes.
  1027. * returns 0 on success.
  1028. */
  1029. int wc_Sha3_384_GetHash(wc_Sha3* sha3, byte* hash)
  1030. {
  1031. return wc_Sha3GetHash(sha3, hash, WC_SHA3_384_COUNT, WC_SHA3_384_DIGEST_SIZE);
  1032. }
  1033. /* Copy the state of the SHA3-384 operation.
  1034. *
  1035. * src wc_Sha3 object holding state top copy.
  1036. * dst wc_Sha3 object to copy into.
  1037. * returns 0 on success.
  1038. */
  1039. int wc_Sha3_384_Copy(wc_Sha3* src, wc_Sha3* dst)
  1040. {
  1041. return wc_Sha3Copy(src, dst);
  1042. }
  1043. /* Initialize the state for a SHA3-512 hash operation.
  1044. *
  1045. * sha3 wc_Sha3 object holding state.
  1046. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  1047. * devId Device identifier for asynchronous operation.
  1048. * returns 0 on success.
  1049. */
  1050. int wc_InitSha3_512(wc_Sha3* sha3, void* heap, int devId)
  1051. {
  1052. return wc_InitSha3(sha3, heap, devId);
  1053. }
  1054. /* Update the SHA3-512 hash state with message data.
  1055. *
  1056. * sha3 wc_Sha3 object holding state.
  1057. * data Message data to be hashed.
  1058. * len Length of the message data.
  1059. * returns 0 on success.
  1060. */
  1061. int wc_Sha3_512_Update(wc_Sha3* sha3, const byte* data, word32 len)
  1062. {
  1063. return wc_Sha3Update(sha3, data, len, WC_SHA3_512_COUNT);
  1064. }
  1065. /* Calculate the SHA3-512 hash based on all the message data seen.
  1066. * The state is initialized ready for a new message to hash.
  1067. *
  1068. * sha3 wc_Sha3 object holding state.
  1069. * hash Buffer to hold the hash result. Must be at least 64 bytes.
  1070. * returns 0 on success.
  1071. */
  1072. int wc_Sha3_512_Final(wc_Sha3* sha3, byte* hash)
  1073. {
  1074. return wc_Sha3Final(sha3, hash, WC_SHA3_512_COUNT, WC_SHA3_512_DIGEST_SIZE);
  1075. }
  1076. /* Dispose of any dynamically allocated data from the SHA3-512 operation.
  1077. * (Required for async ops.)
  1078. *
  1079. * sha3 wc_Sha3 object holding state.
  1080. * returns 0 on success.
  1081. */
  1082. void wc_Sha3_512_Free(wc_Sha3* sha3)
  1083. {
  1084. wc_Sha3Free(sha3);
  1085. }
  1086. /* Calculate the SHA3-512 hash based on all the message data so far.
  1087. * More message data can be added, after this operation, using the current
  1088. * state.
  1089. *
  1090. * sha3 wc_Sha3 object holding state.
  1091. * hash Buffer to hold the hash result. Must be at least 64 bytes.
  1092. * returns 0 on success.
  1093. */
  1094. int wc_Sha3_512_GetHash(wc_Sha3* sha3, byte* hash)
  1095. {
  1096. return wc_Sha3GetHash(sha3, hash, WC_SHA3_512_COUNT, WC_SHA3_512_DIGEST_SIZE);
  1097. }
  1098. /* Copy the state of the SHA3-512 operation.
  1099. *
  1100. * src wc_Sha3 object holding state top copy.
  1101. * dst wc_Sha3 object to copy into.
  1102. * returns 0 on success.
  1103. */
  1104. int wc_Sha3_512_Copy(wc_Sha3* src, wc_Sha3* dst)
  1105. {
  1106. return wc_Sha3Copy(src, dst);
  1107. }
  1108. #ifdef WOLFSSL_HASH_FLAGS
  1109. int wc_Sha3_SetFlags(wc_Sha3* sha3, word32 flags)
  1110. {
  1111. if (sha3) {
  1112. sha3->flags = flags;
  1113. }
  1114. return 0;
  1115. }
  1116. int wc_Sha3_GetFlags(wc_Sha3* sha3, word32* flags)
  1117. {
  1118. if (sha3 && flags) {
  1119. *flags = sha3->flags;
  1120. }
  1121. return 0;
  1122. }
  1123. #endif
  1124. #ifdef WOLFSSL_SHAKE128
  1125. /* Initialize the state for a Shake128 hash operation.
  1126. *
  1127. * shake wc_Shake object holding state.
  1128. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  1129. * devId Device identifier for asynchronous operation.
  1130. * returns 0 on success.
  1131. */
  1132. int wc_InitShake128(wc_Shake* shake, void* heap, int devId)
  1133. {
  1134. return wc_InitSha3(shake, heap, devId);
  1135. }
  1136. /* Update the SHAKE128 hash state with message data.
  1137. *
  1138. * shake wc_Shake object holding state.
  1139. * data Message data to be hashed.
  1140. * len Length of the message data.
  1141. * returns 0 on success.
  1142. */
  1143. int wc_Shake128_Update(wc_Shake* shake, const byte* data, word32 len)
  1144. {
  1145. if (shake == NULL || (data == NULL && len > 0)) {
  1146. return BAD_FUNC_ARG;
  1147. }
  1148. if (data == NULL && len == 0) {
  1149. /* valid, but do nothing */
  1150. return 0;
  1151. }
  1152. return Sha3Update(shake, data, len, WC_SHA3_128_COUNT);
  1153. }
  1154. /* Calculate the SHAKE128 hash based on all the message data seen.
  1155. * The state is initialized ready for a new message to hash.
  1156. *
  1157. * shake wc_Shake object holding state.
  1158. * hash Buffer to hold the hash result. Must be at least 64 bytes.
  1159. * returns 0 on success.
  1160. */
  1161. int wc_Shake128_Final(wc_Shake* shake, byte* hash, word32 hashLen)
  1162. {
  1163. int ret;
  1164. if (shake == NULL || hash == NULL) {
  1165. return BAD_FUNC_ARG;
  1166. }
  1167. ret = Sha3Final(shake, 0x1f, hash, WC_SHA3_128_COUNT, hashLen);
  1168. if (ret != 0)
  1169. return ret;
  1170. return InitSha3(shake); /* reset state */
  1171. }
  1172. /* Absorb the data for squeezing.
  1173. *
  1174. * Update and final with data but no output and no reset
  1175. *
  1176. * shake wc_Shake object holding state.
  1177. * data Data to absorb.
  1178. * len Length of d to absorb in bytes.
  1179. * returns 0 on success.
  1180. */
  1181. int wc_Shake128_Absorb(wc_Shake* shake, const byte* data, word32 len)
  1182. {
  1183. int ret;
  1184. byte hash[1];
  1185. ret = Sha3Update(shake, data, len, WC_SHA3_128_COUNT);
  1186. if (ret == 0) {
  1187. ret = Sha3Final(shake, 0x1f, hash, WC_SHA3_128_COUNT, 0);
  1188. }
  1189. /* No partial data. */
  1190. shake->i = 0;
  1191. return ret;
  1192. }
  1193. /* Squeeze the state to produce pseudo-random output.
  1194. *
  1195. * shake wc_Shake object holding state.
  1196. * out Output buffer.
  1197. * blockCnt Number of blocks to write.
  1198. * returns 0 on success.
  1199. */
  1200. int wc_Shake128_SqueezeBlocks(wc_Shake* shake, byte* out, word32 blockCnt)
  1201. {
  1202. for (; (blockCnt > 0); blockCnt--) {
  1203. #ifdef USE_INTEL_SPEEDUP
  1204. (*sha3_block)(shake->s);
  1205. #else
  1206. BlockSha3(shake->s);
  1207. #endif
  1208. #if defined(BIG_ENDIAN_ORDER)
  1209. ByteReverseWords64((word64*)out, shake->s, WC_SHA3_128_COUNT * 8);
  1210. #else
  1211. XMEMCPY(out, shake->s, WC_SHA3_128_COUNT * 8);
  1212. #endif
  1213. out += WC_SHA3_128_COUNT * 8;
  1214. }
  1215. return 0;
  1216. }
  1217. /* Dispose of any dynamically allocated data from the SHAKE128 operation.
  1218. * (Required for async ops.)
  1219. *
  1220. * shake wc_Shake object holding state.
  1221. * returns 0 on success.
  1222. */
  1223. void wc_Shake128_Free(wc_Shake* shake)
  1224. {
  1225. wc_Sha3Free(shake);
  1226. }
  1227. /* Copy the state of the SHA3-512 operation.
  1228. *
  1229. * src wc_Shake object holding state top copy.
  1230. * dst wc_Shake object to copy into.
  1231. * returns 0 on success.
  1232. */
  1233. int wc_Shake128_Copy(wc_Shake* src, wc_Shake* dst)
  1234. {
  1235. return wc_Sha3Copy(src, dst);
  1236. }
  1237. #endif
  1238. #ifdef WOLFSSL_SHAKE256
  1239. /* Initialize the state for a Shake256 hash operation.
  1240. *
  1241. * shake wc_Shake object holding state.
  1242. * heap Heap reference for dynamic memory allocation. (Used in async ops.)
  1243. * devId Device identifier for asynchronous operation.
  1244. * returns 0 on success.
  1245. */
  1246. int wc_InitShake256(wc_Shake* shake, void* heap, int devId)
  1247. {
  1248. return wc_InitSha3(shake, heap, devId);
  1249. }
  1250. /* Update the SHAKE256 hash state with message data.
  1251. *
  1252. * shake wc_Shake object holding state.
  1253. * data Message data to be hashed.
  1254. * len Length of the message data.
  1255. * returns 0 on success.
  1256. */
  1257. int wc_Shake256_Update(wc_Shake* shake, const byte* data, word32 len)
  1258. {
  1259. if (shake == NULL || (data == NULL && len > 0)) {
  1260. return BAD_FUNC_ARG;
  1261. }
  1262. if (data == NULL && len == 0) {
  1263. /* valid, but do nothing */
  1264. return 0;
  1265. }
  1266. return Sha3Update(shake, data, len, WC_SHA3_256_COUNT);
  1267. }
  1268. /* Calculate the SHAKE256 hash based on all the message data seen.
  1269. * The state is initialized ready for a new message to hash.
  1270. *
  1271. * shake wc_Shake object holding state.
  1272. * hash Buffer to hold the hash result. Must be at least 64 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. byte hash[1];
  1299. ret = Sha3Update(shake, data, len, WC_SHA3_256_COUNT);
  1300. if (ret == 0) {
  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 */