keccak1600.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/e_os2.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len,
  13. size_t r);
  14. void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r);
  15. #if !defined(KECCAK1600_ASM) || !defined(SELFTEST)
  16. /*
  17. * Choose some sensible defaults
  18. */
  19. #if !defined(KECCAK_REF) && !defined(KECCAK_1X) && !defined(KECCAK_1X_ALT) && \
  20. !defined(KECCAK_2X) && !defined(KECCAK_INPLACE)
  21. # define KECCAK_2X /* default to KECCAK_2X variant */
  22. #endif
  23. #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  24. (defined(__x86_64) && !defined(__BMI__)) || defined(_M_X64) || \
  25. defined(__mips) || defined(__riscv) || defined(__s390__) || \
  26. defined(__EMSCRIPTEN__)
  27. /*
  28. * These don't have "and with complement" instruction, so minimize amount
  29. * of "not"-s. Implemented only in the [default] KECCAK_2X variant.
  30. */
  31. # define KECCAK_COMPLEMENTING_TRANSFORM
  32. #endif
  33. #if defined(__x86_64__) || defined(__aarch64__) || \
  34. defined(__mips64) || defined(__ia64) || \
  35. (defined(__VMS) && !defined(__vax))
  36. /*
  37. * These are available even in ILP32 flavours, but even then they are
  38. * capable of performing 64-bit operations as efficiently as in *P64.
  39. * Since it's not given that we can use sizeof(void *), just shunt it.
  40. */
  41. # define BIT_INTERLEAVE (0)
  42. #else
  43. # define BIT_INTERLEAVE (sizeof(void *) < 8)
  44. #endif
  45. #define ROL32(a, offset) (((a) << (offset)) | ((a) >> ((32 - (offset)) & 31)))
  46. static uint64_t ROL64(uint64_t val, int offset)
  47. {
  48. if (offset == 0) {
  49. return val;
  50. } else if (!BIT_INTERLEAVE) {
  51. return (val << offset) | (val >> (64-offset));
  52. } else {
  53. uint32_t hi = (uint32_t)(val >> 32), lo = (uint32_t)val;
  54. if (offset & 1) {
  55. uint32_t tmp = hi;
  56. offset >>= 1;
  57. hi = ROL32(lo, offset);
  58. lo = ROL32(tmp, offset + 1);
  59. } else {
  60. offset >>= 1;
  61. lo = ROL32(lo, offset);
  62. hi = ROL32(hi, offset);
  63. }
  64. return ((uint64_t)hi << 32) | lo;
  65. }
  66. }
  67. static const unsigned char rhotates[5][5] = {
  68. { 0, 1, 62, 28, 27 },
  69. { 36, 44, 6, 55, 20 },
  70. { 3, 10, 43, 25, 39 },
  71. { 41, 45, 15, 21, 8 },
  72. { 18, 2, 61, 56, 14 }
  73. };
  74. static const uint64_t iotas[] = {
  75. BIT_INTERLEAVE ? 0x0000000000000001ULL : 0x0000000000000001ULL,
  76. BIT_INTERLEAVE ? 0x0000008900000000ULL : 0x0000000000008082ULL,
  77. BIT_INTERLEAVE ? 0x8000008b00000000ULL : 0x800000000000808aULL,
  78. BIT_INTERLEAVE ? 0x8000808000000000ULL : 0x8000000080008000ULL,
  79. BIT_INTERLEAVE ? 0x0000008b00000001ULL : 0x000000000000808bULL,
  80. BIT_INTERLEAVE ? 0x0000800000000001ULL : 0x0000000080000001ULL,
  81. BIT_INTERLEAVE ? 0x8000808800000001ULL : 0x8000000080008081ULL,
  82. BIT_INTERLEAVE ? 0x8000008200000001ULL : 0x8000000000008009ULL,
  83. BIT_INTERLEAVE ? 0x0000000b00000000ULL : 0x000000000000008aULL,
  84. BIT_INTERLEAVE ? 0x0000000a00000000ULL : 0x0000000000000088ULL,
  85. BIT_INTERLEAVE ? 0x0000808200000001ULL : 0x0000000080008009ULL,
  86. BIT_INTERLEAVE ? 0x0000800300000000ULL : 0x000000008000000aULL,
  87. BIT_INTERLEAVE ? 0x0000808b00000001ULL : 0x000000008000808bULL,
  88. BIT_INTERLEAVE ? 0x8000000b00000001ULL : 0x800000000000008bULL,
  89. BIT_INTERLEAVE ? 0x8000008a00000001ULL : 0x8000000000008089ULL,
  90. BIT_INTERLEAVE ? 0x8000008100000001ULL : 0x8000000000008003ULL,
  91. BIT_INTERLEAVE ? 0x8000008100000000ULL : 0x8000000000008002ULL,
  92. BIT_INTERLEAVE ? 0x8000000800000000ULL : 0x8000000000000080ULL,
  93. BIT_INTERLEAVE ? 0x0000008300000000ULL : 0x000000000000800aULL,
  94. BIT_INTERLEAVE ? 0x8000800300000000ULL : 0x800000008000000aULL,
  95. BIT_INTERLEAVE ? 0x8000808800000001ULL : 0x8000000080008081ULL,
  96. BIT_INTERLEAVE ? 0x8000008800000000ULL : 0x8000000000008080ULL,
  97. BIT_INTERLEAVE ? 0x0000800000000001ULL : 0x0000000080000001ULL,
  98. BIT_INTERLEAVE ? 0x8000808200000000ULL : 0x8000000080008008ULL
  99. };
  100. #if defined(KECCAK_REF)
  101. /*
  102. * This is straightforward or "maximum clarity" implementation aiming
  103. * to resemble section 3.2 of the FIPS PUB 202 "SHA-3 Standard:
  104. * Permutation-Based Hash and Extendible-Output Functions" as much as
  105. * possible. With one caveat. Because of the way C stores matrices,
  106. * references to A[x,y] in the specification are presented as A[y][x].
  107. * Implementation unrolls inner x-loops so that modulo 5 operations are
  108. * explicitly pre-computed.
  109. */
  110. static void Theta(uint64_t A[5][5])
  111. {
  112. uint64_t C[5], D[5];
  113. size_t y;
  114. C[0] = A[0][0];
  115. C[1] = A[0][1];
  116. C[2] = A[0][2];
  117. C[3] = A[0][3];
  118. C[4] = A[0][4];
  119. for (y = 1; y < 5; y++) {
  120. C[0] ^= A[y][0];
  121. C[1] ^= A[y][1];
  122. C[2] ^= A[y][2];
  123. C[3] ^= A[y][3];
  124. C[4] ^= A[y][4];
  125. }
  126. D[0] = ROL64(C[1], 1) ^ C[4];
  127. D[1] = ROL64(C[2], 1) ^ C[0];
  128. D[2] = ROL64(C[3], 1) ^ C[1];
  129. D[3] = ROL64(C[4], 1) ^ C[2];
  130. D[4] = ROL64(C[0], 1) ^ C[3];
  131. for (y = 0; y < 5; y++) {
  132. A[y][0] ^= D[0];
  133. A[y][1] ^= D[1];
  134. A[y][2] ^= D[2];
  135. A[y][3] ^= D[3];
  136. A[y][4] ^= D[4];
  137. }
  138. }
  139. static void Rho(uint64_t A[5][5])
  140. {
  141. size_t y;
  142. for (y = 0; y < 5; y++) {
  143. A[y][0] = ROL64(A[y][0], rhotates[y][0]);
  144. A[y][1] = ROL64(A[y][1], rhotates[y][1]);
  145. A[y][2] = ROL64(A[y][2], rhotates[y][2]);
  146. A[y][3] = ROL64(A[y][3], rhotates[y][3]);
  147. A[y][4] = ROL64(A[y][4], rhotates[y][4]);
  148. }
  149. }
  150. static void Pi(uint64_t A[5][5])
  151. {
  152. uint64_t T[5][5];
  153. /*
  154. * T = A
  155. * A[y][x] = T[x][(3*y+x)%5]
  156. */
  157. memcpy(T, A, sizeof(T));
  158. A[0][0] = T[0][0];
  159. A[0][1] = T[1][1];
  160. A[0][2] = T[2][2];
  161. A[0][3] = T[3][3];
  162. A[0][4] = T[4][4];
  163. A[1][0] = T[0][3];
  164. A[1][1] = T[1][4];
  165. A[1][2] = T[2][0];
  166. A[1][3] = T[3][1];
  167. A[1][4] = T[4][2];
  168. A[2][0] = T[0][1];
  169. A[2][1] = T[1][2];
  170. A[2][2] = T[2][3];
  171. A[2][3] = T[3][4];
  172. A[2][4] = T[4][0];
  173. A[3][0] = T[0][4];
  174. A[3][1] = T[1][0];
  175. A[3][2] = T[2][1];
  176. A[3][3] = T[3][2];
  177. A[3][4] = T[4][3];
  178. A[4][0] = T[0][2];
  179. A[4][1] = T[1][3];
  180. A[4][2] = T[2][4];
  181. A[4][3] = T[3][0];
  182. A[4][4] = T[4][1];
  183. }
  184. static void Chi(uint64_t A[5][5])
  185. {
  186. uint64_t C[5];
  187. size_t y;
  188. for (y = 0; y < 5; y++) {
  189. C[0] = A[y][0] ^ (~A[y][1] & A[y][2]);
  190. C[1] = A[y][1] ^ (~A[y][2] & A[y][3]);
  191. C[2] = A[y][2] ^ (~A[y][3] & A[y][4]);
  192. C[3] = A[y][3] ^ (~A[y][4] & A[y][0]);
  193. C[4] = A[y][4] ^ (~A[y][0] & A[y][1]);
  194. A[y][0] = C[0];
  195. A[y][1] = C[1];
  196. A[y][2] = C[2];
  197. A[y][3] = C[3];
  198. A[y][4] = C[4];
  199. }
  200. }
  201. static void Iota(uint64_t A[5][5], size_t i)
  202. {
  203. assert(i < (sizeof(iotas) / sizeof(iotas[0])));
  204. A[0][0] ^= iotas[i];
  205. }
  206. static void KeccakF1600(uint64_t A[5][5])
  207. {
  208. size_t i;
  209. for (i = 0; i < 24; i++) {
  210. Theta(A);
  211. Rho(A);
  212. Pi(A);
  213. Chi(A);
  214. Iota(A, i);
  215. }
  216. }
  217. #elif defined(KECCAK_1X)
  218. /*
  219. * This implementation is optimization of above code featuring unroll
  220. * of even y-loops, their fusion and code motion. It also minimizes
  221. * temporary storage. Compiler would normally do all these things for
  222. * you, purpose of manual optimization is to provide "unobscured"
  223. * reference for assembly implementation [in case this approach is
  224. * chosen for implementation on some platform]. In the nutshell it's
  225. * equivalent of "plane-per-plane processing" approach discussed in
  226. * section 2.4 of "Keccak implementation overview".
  227. */
  228. static void Round(uint64_t A[5][5], size_t i)
  229. {
  230. uint64_t C[5], E[2]; /* registers */
  231. uint64_t D[5], T[2][5]; /* memory */
  232. assert(i < (sizeof(iotas) / sizeof(iotas[0])));
  233. C[0] = A[0][0] ^ A[1][0] ^ A[2][0] ^ A[3][0] ^ A[4][0];
  234. C[1] = A[0][1] ^ A[1][1] ^ A[2][1] ^ A[3][1] ^ A[4][1];
  235. C[2] = A[0][2] ^ A[1][2] ^ A[2][2] ^ A[3][2] ^ A[4][2];
  236. C[3] = A[0][3] ^ A[1][3] ^ A[2][3] ^ A[3][3] ^ A[4][3];
  237. C[4] = A[0][4] ^ A[1][4] ^ A[2][4] ^ A[3][4] ^ A[4][4];
  238. #if defined(__arm__)
  239. D[1] = E[0] = ROL64(C[2], 1) ^ C[0];
  240. D[4] = E[1] = ROL64(C[0], 1) ^ C[3];
  241. D[0] = C[0] = ROL64(C[1], 1) ^ C[4];
  242. D[2] = C[1] = ROL64(C[3], 1) ^ C[1];
  243. D[3] = C[2] = ROL64(C[4], 1) ^ C[2];
  244. T[0][0] = A[3][0] ^ C[0]; /* borrow T[0][0] */
  245. T[0][1] = A[0][1] ^ E[0]; /* D[1] */
  246. T[0][2] = A[0][2] ^ C[1]; /* D[2] */
  247. T[0][3] = A[0][3] ^ C[2]; /* D[3] */
  248. T[0][4] = A[0][4] ^ E[1]; /* D[4] */
  249. C[3] = ROL64(A[3][3] ^ C[2], rhotates[3][3]); /* D[3] */
  250. C[4] = ROL64(A[4][4] ^ E[1], rhotates[4][4]); /* D[4] */
  251. C[0] = A[0][0] ^ C[0]; /* rotate by 0 */ /* D[0] */
  252. C[2] = ROL64(A[2][2] ^ C[1], rhotates[2][2]); /* D[2] */
  253. C[1] = ROL64(A[1][1] ^ E[0], rhotates[1][1]); /* D[1] */
  254. #else
  255. D[0] = ROL64(C[1], 1) ^ C[4];
  256. D[1] = ROL64(C[2], 1) ^ C[0];
  257. D[2] = ROL64(C[3], 1) ^ C[1];
  258. D[3] = ROL64(C[4], 1) ^ C[2];
  259. D[4] = ROL64(C[0], 1) ^ C[3];
  260. T[0][0] = A[3][0] ^ D[0]; /* borrow T[0][0] */
  261. T[0][1] = A[0][1] ^ D[1];
  262. T[0][2] = A[0][2] ^ D[2];
  263. T[0][3] = A[0][3] ^ D[3];
  264. T[0][4] = A[0][4] ^ D[4];
  265. C[0] = A[0][0] ^ D[0]; /* rotate by 0 */
  266. C[1] = ROL64(A[1][1] ^ D[1], rhotates[1][1]);
  267. C[2] = ROL64(A[2][2] ^ D[2], rhotates[2][2]);
  268. C[3] = ROL64(A[3][3] ^ D[3], rhotates[3][3]);
  269. C[4] = ROL64(A[4][4] ^ D[4], rhotates[4][4]);
  270. #endif
  271. A[0][0] = C[0] ^ (~C[1] & C[2]) ^ iotas[i];
  272. A[0][1] = C[1] ^ (~C[2] & C[3]);
  273. A[0][2] = C[2] ^ (~C[3] & C[4]);
  274. A[0][3] = C[3] ^ (~C[4] & C[0]);
  275. A[0][4] = C[4] ^ (~C[0] & C[1]);
  276. T[1][0] = A[1][0] ^ (C[3] = D[0]);
  277. T[1][1] = A[2][1] ^ (C[4] = D[1]); /* borrow T[1][1] */
  278. T[1][2] = A[1][2] ^ (E[0] = D[2]);
  279. T[1][3] = A[1][3] ^ (E[1] = D[3]);
  280. T[1][4] = A[2][4] ^ (C[2] = D[4]); /* borrow T[1][4] */
  281. C[0] = ROL64(T[0][3], rhotates[0][3]);
  282. C[1] = ROL64(A[1][4] ^ C[2], rhotates[1][4]); /* D[4] */
  283. C[2] = ROL64(A[2][0] ^ C[3], rhotates[2][0]); /* D[0] */
  284. C[3] = ROL64(A[3][1] ^ C[4], rhotates[3][1]); /* D[1] */
  285. C[4] = ROL64(A[4][2] ^ E[0], rhotates[4][2]); /* D[2] */
  286. A[1][0] = C[0] ^ (~C[1] & C[2]);
  287. A[1][1] = C[1] ^ (~C[2] & C[3]);
  288. A[1][2] = C[2] ^ (~C[3] & C[4]);
  289. A[1][3] = C[3] ^ (~C[4] & C[0]);
  290. A[1][4] = C[4] ^ (~C[0] & C[1]);
  291. C[0] = ROL64(T[0][1], rhotates[0][1]);
  292. C[1] = ROL64(T[1][2], rhotates[1][2]);
  293. C[2] = ROL64(A[2][3] ^ D[3], rhotates[2][3]);
  294. C[3] = ROL64(A[3][4] ^ D[4], rhotates[3][4]);
  295. C[4] = ROL64(A[4][0] ^ D[0], rhotates[4][0]);
  296. A[2][0] = C[0] ^ (~C[1] & C[2]);
  297. A[2][1] = C[1] ^ (~C[2] & C[3]);
  298. A[2][2] = C[2] ^ (~C[3] & C[4]);
  299. A[2][3] = C[3] ^ (~C[4] & C[0]);
  300. A[2][4] = C[4] ^ (~C[0] & C[1]);
  301. C[0] = ROL64(T[0][4], rhotates[0][4]);
  302. C[1] = ROL64(T[1][0], rhotates[1][0]);
  303. C[2] = ROL64(T[1][1], rhotates[2][1]); /* originally A[2][1] */
  304. C[3] = ROL64(A[3][2] ^ D[2], rhotates[3][2]);
  305. C[4] = ROL64(A[4][3] ^ D[3], rhotates[4][3]);
  306. A[3][0] = C[0] ^ (~C[1] & C[2]);
  307. A[3][1] = C[1] ^ (~C[2] & C[3]);
  308. A[3][2] = C[2] ^ (~C[3] & C[4]);
  309. A[3][3] = C[3] ^ (~C[4] & C[0]);
  310. A[3][4] = C[4] ^ (~C[0] & C[1]);
  311. C[0] = ROL64(T[0][2], rhotates[0][2]);
  312. C[1] = ROL64(T[1][3], rhotates[1][3]);
  313. C[2] = ROL64(T[1][4], rhotates[2][4]); /* originally A[2][4] */
  314. C[3] = ROL64(T[0][0], rhotates[3][0]); /* originally A[3][0] */
  315. C[4] = ROL64(A[4][1] ^ D[1], rhotates[4][1]);
  316. A[4][0] = C[0] ^ (~C[1] & C[2]);
  317. A[4][1] = C[1] ^ (~C[2] & C[3]);
  318. A[4][2] = C[2] ^ (~C[3] & C[4]);
  319. A[4][3] = C[3] ^ (~C[4] & C[0]);
  320. A[4][4] = C[4] ^ (~C[0] & C[1]);
  321. }
  322. static void KeccakF1600(uint64_t A[5][5])
  323. {
  324. size_t i;
  325. for (i = 0; i < 24; i++) {
  326. Round(A, i);
  327. }
  328. }
  329. #elif defined(KECCAK_1X_ALT)
  330. /*
  331. * This is variant of above KECCAK_1X that reduces requirement for
  332. * temporary storage even further, but at cost of more updates to A[][].
  333. * It's less suitable if A[][] is memory bound, but better if it's
  334. * register bound.
  335. */
  336. static void Round(uint64_t A[5][5], size_t i)
  337. {
  338. uint64_t C[5], D[5];
  339. assert(i < (sizeof(iotas) / sizeof(iotas[0])));
  340. C[0] = A[0][0] ^ A[1][0] ^ A[2][0] ^ A[3][0] ^ A[4][0];
  341. C[1] = A[0][1] ^ A[1][1] ^ A[2][1] ^ A[3][1] ^ A[4][1];
  342. C[2] = A[0][2] ^ A[1][2] ^ A[2][2] ^ A[3][2] ^ A[4][2];
  343. C[3] = A[0][3] ^ A[1][3] ^ A[2][3] ^ A[3][3] ^ A[4][3];
  344. C[4] = A[0][4] ^ A[1][4] ^ A[2][4] ^ A[3][4] ^ A[4][4];
  345. D[1] = C[0] ^ ROL64(C[2], 1);
  346. D[2] = C[1] ^ ROL64(C[3], 1);
  347. D[3] = C[2] ^= ROL64(C[4], 1);
  348. D[4] = C[3] ^= ROL64(C[0], 1);
  349. D[0] = C[4] ^= ROL64(C[1], 1);
  350. A[0][1] ^= D[1];
  351. A[1][1] ^= D[1];
  352. A[2][1] ^= D[1];
  353. A[3][1] ^= D[1];
  354. A[4][1] ^= D[1];
  355. A[0][2] ^= D[2];
  356. A[1][2] ^= D[2];
  357. A[2][2] ^= D[2];
  358. A[3][2] ^= D[2];
  359. A[4][2] ^= D[2];
  360. A[0][3] ^= C[2];
  361. A[1][3] ^= C[2];
  362. A[2][3] ^= C[2];
  363. A[3][3] ^= C[2];
  364. A[4][3] ^= C[2];
  365. A[0][4] ^= C[3];
  366. A[1][4] ^= C[3];
  367. A[2][4] ^= C[3];
  368. A[3][4] ^= C[3];
  369. A[4][4] ^= C[3];
  370. A[0][0] ^= C[4];
  371. A[1][0] ^= C[4];
  372. A[2][0] ^= C[4];
  373. A[3][0] ^= C[4];
  374. A[4][0] ^= C[4];
  375. C[1] = A[0][1];
  376. C[2] = A[0][2];
  377. C[3] = A[0][3];
  378. C[4] = A[0][4];
  379. A[0][1] = ROL64(A[1][1], rhotates[1][1]);
  380. A[0][2] = ROL64(A[2][2], rhotates[2][2]);
  381. A[0][3] = ROL64(A[3][3], rhotates[3][3]);
  382. A[0][4] = ROL64(A[4][4], rhotates[4][4]);
  383. A[1][1] = ROL64(A[1][4], rhotates[1][4]);
  384. A[2][2] = ROL64(A[2][3], rhotates[2][3]);
  385. A[3][3] = ROL64(A[3][2], rhotates[3][2]);
  386. A[4][4] = ROL64(A[4][1], rhotates[4][1]);
  387. A[1][4] = ROL64(A[4][2], rhotates[4][2]);
  388. A[2][3] = ROL64(A[3][4], rhotates[3][4]);
  389. A[3][2] = ROL64(A[2][1], rhotates[2][1]);
  390. A[4][1] = ROL64(A[1][3], rhotates[1][3]);
  391. A[4][2] = ROL64(A[2][4], rhotates[2][4]);
  392. A[3][4] = ROL64(A[4][3], rhotates[4][3]);
  393. A[2][1] = ROL64(A[1][2], rhotates[1][2]);
  394. A[1][3] = ROL64(A[3][1], rhotates[3][1]);
  395. A[2][4] = ROL64(A[4][0], rhotates[4][0]);
  396. A[4][3] = ROL64(A[3][0], rhotates[3][0]);
  397. A[1][2] = ROL64(A[2][0], rhotates[2][0]);
  398. A[3][1] = ROL64(A[1][0], rhotates[1][0]);
  399. A[1][0] = ROL64(C[3], rhotates[0][3]);
  400. A[2][0] = ROL64(C[1], rhotates[0][1]);
  401. A[3][0] = ROL64(C[4], rhotates[0][4]);
  402. A[4][0] = ROL64(C[2], rhotates[0][2]);
  403. C[0] = A[0][0];
  404. C[1] = A[1][0];
  405. D[0] = A[0][1];
  406. D[1] = A[1][1];
  407. A[0][0] ^= (~A[0][1] & A[0][2]);
  408. A[1][0] ^= (~A[1][1] & A[1][2]);
  409. A[0][1] ^= (~A[0][2] & A[0][3]);
  410. A[1][1] ^= (~A[1][2] & A[1][3]);
  411. A[0][2] ^= (~A[0][3] & A[0][4]);
  412. A[1][2] ^= (~A[1][3] & A[1][4]);
  413. A[0][3] ^= (~A[0][4] & C[0]);
  414. A[1][3] ^= (~A[1][4] & C[1]);
  415. A[0][4] ^= (~C[0] & D[0]);
  416. A[1][4] ^= (~C[1] & D[1]);
  417. C[2] = A[2][0];
  418. C[3] = A[3][0];
  419. D[2] = A[2][1];
  420. D[3] = A[3][1];
  421. A[2][0] ^= (~A[2][1] & A[2][2]);
  422. A[3][0] ^= (~A[3][1] & A[3][2]);
  423. A[2][1] ^= (~A[2][2] & A[2][3]);
  424. A[3][1] ^= (~A[3][2] & A[3][3]);
  425. A[2][2] ^= (~A[2][3] & A[2][4]);
  426. A[3][2] ^= (~A[3][3] & A[3][4]);
  427. A[2][3] ^= (~A[2][4] & C[2]);
  428. A[3][3] ^= (~A[3][4] & C[3]);
  429. A[2][4] ^= (~C[2] & D[2]);
  430. A[3][4] ^= (~C[3] & D[3]);
  431. C[4] = A[4][0];
  432. D[4] = A[4][1];
  433. A[4][0] ^= (~A[4][1] & A[4][2]);
  434. A[4][1] ^= (~A[4][2] & A[4][3]);
  435. A[4][2] ^= (~A[4][3] & A[4][4]);
  436. A[4][3] ^= (~A[4][4] & C[4]);
  437. A[4][4] ^= (~C[4] & D[4]);
  438. A[0][0] ^= iotas[i];
  439. }
  440. static void KeccakF1600(uint64_t A[5][5])
  441. {
  442. size_t i;
  443. for (i = 0; i < 24; i++) {
  444. Round(A, i);
  445. }
  446. }
  447. #elif defined(KECCAK_2X)
  448. /*
  449. * This implementation is variant of KECCAK_1X above with outer-most
  450. * round loop unrolled twice. This allows to take temporary storage
  451. * out of round procedure and simplify references to it by alternating
  452. * it with actual data (see round loop below). Originally it was meant
  453. * rather as reference for an assembly implementation, but it seems to
  454. * play best with compilers [as well as provide best instruction per
  455. * processed byte ratio at minimal round unroll factor]...
  456. */
  457. static void Round(uint64_t R[5][5], uint64_t A[5][5], size_t i)
  458. {
  459. uint64_t C[5], D[5];
  460. assert(i < (sizeof(iotas) / sizeof(iotas[0])));
  461. C[0] = A[0][0] ^ A[1][0] ^ A[2][0] ^ A[3][0] ^ A[4][0];
  462. C[1] = A[0][1] ^ A[1][1] ^ A[2][1] ^ A[3][1] ^ A[4][1];
  463. C[2] = A[0][2] ^ A[1][2] ^ A[2][2] ^ A[3][2] ^ A[4][2];
  464. C[3] = A[0][3] ^ A[1][3] ^ A[2][3] ^ A[3][3] ^ A[4][3];
  465. C[4] = A[0][4] ^ A[1][4] ^ A[2][4] ^ A[3][4] ^ A[4][4];
  466. D[0] = ROL64(C[1], 1) ^ C[4];
  467. D[1] = ROL64(C[2], 1) ^ C[0];
  468. D[2] = ROL64(C[3], 1) ^ C[1];
  469. D[3] = ROL64(C[4], 1) ^ C[2];
  470. D[4] = ROL64(C[0], 1) ^ C[3];
  471. C[0] = A[0][0] ^ D[0]; /* rotate by 0 */
  472. C[1] = ROL64(A[1][1] ^ D[1], rhotates[1][1]);
  473. C[2] = ROL64(A[2][2] ^ D[2], rhotates[2][2]);
  474. C[3] = ROL64(A[3][3] ^ D[3], rhotates[3][3]);
  475. C[4] = ROL64(A[4][4] ^ D[4], rhotates[4][4]);
  476. #ifdef KECCAK_COMPLEMENTING_TRANSFORM
  477. R[0][0] = C[0] ^ ( C[1] | C[2]) ^ iotas[i];
  478. R[0][1] = C[1] ^ (~C[2] | C[3]);
  479. R[0][2] = C[2] ^ ( C[3] & C[4]);
  480. R[0][3] = C[3] ^ ( C[4] | C[0]);
  481. R[0][4] = C[4] ^ ( C[0] & C[1]);
  482. #else
  483. R[0][0] = C[0] ^ (~C[1] & C[2]) ^ iotas[i];
  484. R[0][1] = C[1] ^ (~C[2] & C[3]);
  485. R[0][2] = C[2] ^ (~C[3] & C[4]);
  486. R[0][3] = C[3] ^ (~C[4] & C[0]);
  487. R[0][4] = C[4] ^ (~C[0] & C[1]);
  488. #endif
  489. C[0] = ROL64(A[0][3] ^ D[3], rhotates[0][3]);
  490. C[1] = ROL64(A[1][4] ^ D[4], rhotates[1][4]);
  491. C[2] = ROL64(A[2][0] ^ D[0], rhotates[2][0]);
  492. C[3] = ROL64(A[3][1] ^ D[1], rhotates[3][1]);
  493. C[4] = ROL64(A[4][2] ^ D[2], rhotates[4][2]);
  494. #ifdef KECCAK_COMPLEMENTING_TRANSFORM
  495. R[1][0] = C[0] ^ (C[1] | C[2]);
  496. R[1][1] = C[1] ^ (C[2] & C[3]);
  497. R[1][2] = C[2] ^ (C[3] | ~C[4]);
  498. R[1][3] = C[3] ^ (C[4] | C[0]);
  499. R[1][4] = C[4] ^ (C[0] & C[1]);
  500. #else
  501. R[1][0] = C[0] ^ (~C[1] & C[2]);
  502. R[1][1] = C[1] ^ (~C[2] & C[3]);
  503. R[1][2] = C[2] ^ (~C[3] & C[4]);
  504. R[1][3] = C[3] ^ (~C[4] & C[0]);
  505. R[1][4] = C[4] ^ (~C[0] & C[1]);
  506. #endif
  507. C[0] = ROL64(A[0][1] ^ D[1], rhotates[0][1]);
  508. C[1] = ROL64(A[1][2] ^ D[2], rhotates[1][2]);
  509. C[2] = ROL64(A[2][3] ^ D[3], rhotates[2][3]);
  510. C[3] = ROL64(A[3][4] ^ D[4], rhotates[3][4]);
  511. C[4] = ROL64(A[4][0] ^ D[0], rhotates[4][0]);
  512. #ifdef KECCAK_COMPLEMENTING_TRANSFORM
  513. R[2][0] = C[0] ^ ( C[1] | C[2]);
  514. R[2][1] = C[1] ^ ( C[2] & C[3]);
  515. R[2][2] = C[2] ^ (~C[3] & C[4]);
  516. R[2][3] = ~C[3] ^ ( C[4] | C[0]);
  517. R[2][4] = C[4] ^ ( C[0] & C[1]);
  518. #else
  519. R[2][0] = C[0] ^ (~C[1] & C[2]);
  520. R[2][1] = C[1] ^ (~C[2] & C[3]);
  521. R[2][2] = C[2] ^ (~C[3] & C[4]);
  522. R[2][3] = C[3] ^ (~C[4] & C[0]);
  523. R[2][4] = C[4] ^ (~C[0] & C[1]);
  524. #endif
  525. C[0] = ROL64(A[0][4] ^ D[4], rhotates[0][4]);
  526. C[1] = ROL64(A[1][0] ^ D[0], rhotates[1][0]);
  527. C[2] = ROL64(A[2][1] ^ D[1], rhotates[2][1]);
  528. C[3] = ROL64(A[3][2] ^ D[2], rhotates[3][2]);
  529. C[4] = ROL64(A[4][3] ^ D[3], rhotates[4][3]);
  530. #ifdef KECCAK_COMPLEMENTING_TRANSFORM
  531. R[3][0] = C[0] ^ ( C[1] & C[2]);
  532. R[3][1] = C[1] ^ ( C[2] | C[3]);
  533. R[3][2] = C[2] ^ (~C[3] | C[4]);
  534. R[3][3] = ~C[3] ^ ( C[4] & C[0]);
  535. R[3][4] = C[4] ^ ( C[0] | C[1]);
  536. #else
  537. R[3][0] = C[0] ^ (~C[1] & C[2]);
  538. R[3][1] = C[1] ^ (~C[2] & C[3]);
  539. R[3][2] = C[2] ^ (~C[3] & C[4]);
  540. R[3][3] = C[3] ^ (~C[4] & C[0]);
  541. R[3][4] = C[4] ^ (~C[0] & C[1]);
  542. #endif
  543. C[0] = ROL64(A[0][2] ^ D[2], rhotates[0][2]);
  544. C[1] = ROL64(A[1][3] ^ D[3], rhotates[1][3]);
  545. C[2] = ROL64(A[2][4] ^ D[4], rhotates[2][4]);
  546. C[3] = ROL64(A[3][0] ^ D[0], rhotates[3][0]);
  547. C[4] = ROL64(A[4][1] ^ D[1], rhotates[4][1]);
  548. #ifdef KECCAK_COMPLEMENTING_TRANSFORM
  549. R[4][0] = C[0] ^ (~C[1] & C[2]);
  550. R[4][1] = ~C[1] ^ ( C[2] | C[3]);
  551. R[4][2] = C[2] ^ ( C[3] & C[4]);
  552. R[4][3] = C[3] ^ ( C[4] | C[0]);
  553. R[4][4] = C[4] ^ ( C[0] & C[1]);
  554. #else
  555. R[4][0] = C[0] ^ (~C[1] & C[2]);
  556. R[4][1] = C[1] ^ (~C[2] & C[3]);
  557. R[4][2] = C[2] ^ (~C[3] & C[4]);
  558. R[4][3] = C[3] ^ (~C[4] & C[0]);
  559. R[4][4] = C[4] ^ (~C[0] & C[1]);
  560. #endif
  561. }
  562. static void KeccakF1600(uint64_t A[5][5])
  563. {
  564. uint64_t T[5][5];
  565. size_t i;
  566. #ifdef KECCAK_COMPLEMENTING_TRANSFORM
  567. A[0][1] = ~A[0][1];
  568. A[0][2] = ~A[0][2];
  569. A[1][3] = ~A[1][3];
  570. A[2][2] = ~A[2][2];
  571. A[3][2] = ~A[3][2];
  572. A[4][0] = ~A[4][0];
  573. #endif
  574. for (i = 0; i < 24; i += 2) {
  575. Round(T, A, i);
  576. Round(A, T, i + 1);
  577. }
  578. #ifdef KECCAK_COMPLEMENTING_TRANSFORM
  579. A[0][1] = ~A[0][1];
  580. A[0][2] = ~A[0][2];
  581. A[1][3] = ~A[1][3];
  582. A[2][2] = ~A[2][2];
  583. A[3][2] = ~A[3][2];
  584. A[4][0] = ~A[4][0];
  585. #endif
  586. }
  587. #else /* define KECCAK_INPLACE to compile this code path */
  588. /*
  589. * This implementation is KECCAK_1X from above combined 4 times with
  590. * a twist that allows to omit temporary storage and perform in-place
  591. * processing. It's discussed in section 2.5 of "Keccak implementation
  592. * overview". It's likely to be best suited for processors with large
  593. * register bank... On the other hand processor with large register
  594. * bank can as well use KECCAK_1X_ALT, it would be as fast but much
  595. * more compact...
  596. */
  597. static void FourRounds(uint64_t A[5][5], size_t i)
  598. {
  599. uint64_t B[5], C[5], D[5];
  600. assert(i <= (sizeof(iotas) / sizeof(iotas[0]) - 4));
  601. /* Round 4*n */
  602. C[0] = A[0][0] ^ A[1][0] ^ A[2][0] ^ A[3][0] ^ A[4][0];
  603. C[1] = A[0][1] ^ A[1][1] ^ A[2][1] ^ A[3][1] ^ A[4][1];
  604. C[2] = A[0][2] ^ A[1][2] ^ A[2][2] ^ A[3][2] ^ A[4][2];
  605. C[3] = A[0][3] ^ A[1][3] ^ A[2][3] ^ A[3][3] ^ A[4][3];
  606. C[4] = A[0][4] ^ A[1][4] ^ A[2][4] ^ A[3][4] ^ A[4][4];
  607. D[0] = ROL64(C[1], 1) ^ C[4];
  608. D[1] = ROL64(C[2], 1) ^ C[0];
  609. D[2] = ROL64(C[3], 1) ^ C[1];
  610. D[3] = ROL64(C[4], 1) ^ C[2];
  611. D[4] = ROL64(C[0], 1) ^ C[3];
  612. B[0] = A[0][0] ^ D[0]; /* rotate by 0 */
  613. B[1] = ROL64(A[1][1] ^ D[1], rhotates[1][1]);
  614. B[2] = ROL64(A[2][2] ^ D[2], rhotates[2][2]);
  615. B[3] = ROL64(A[3][3] ^ D[3], rhotates[3][3]);
  616. B[4] = ROL64(A[4][4] ^ D[4], rhotates[4][4]);
  617. C[0] = A[0][0] = B[0] ^ (~B[1] & B[2]) ^ iotas[i];
  618. C[1] = A[1][1] = B[1] ^ (~B[2] & B[3]);
  619. C[2] = A[2][2] = B[2] ^ (~B[3] & B[4]);
  620. C[3] = A[3][3] = B[3] ^ (~B[4] & B[0]);
  621. C[4] = A[4][4] = B[4] ^ (~B[0] & B[1]);
  622. B[0] = ROL64(A[0][3] ^ D[3], rhotates[0][3]);
  623. B[1] = ROL64(A[1][4] ^ D[4], rhotates[1][4]);
  624. B[2] = ROL64(A[2][0] ^ D[0], rhotates[2][0]);
  625. B[3] = ROL64(A[3][1] ^ D[1], rhotates[3][1]);
  626. B[4] = ROL64(A[4][2] ^ D[2], rhotates[4][2]);
  627. C[0] ^= A[2][0] = B[0] ^ (~B[1] & B[2]);
  628. C[1] ^= A[3][1] = B[1] ^ (~B[2] & B[3]);
  629. C[2] ^= A[4][2] = B[2] ^ (~B[3] & B[4]);
  630. C[3] ^= A[0][3] = B[3] ^ (~B[4] & B[0]);
  631. C[4] ^= A[1][4] = B[4] ^ (~B[0] & B[1]);
  632. B[0] = ROL64(A[0][1] ^ D[1], rhotates[0][1]);
  633. B[1] = ROL64(A[1][2] ^ D[2], rhotates[1][2]);
  634. B[2] = ROL64(A[2][3] ^ D[3], rhotates[2][3]);
  635. B[3] = ROL64(A[3][4] ^ D[4], rhotates[3][4]);
  636. B[4] = ROL64(A[4][0] ^ D[0], rhotates[4][0]);
  637. C[0] ^= A[4][0] = B[0] ^ (~B[1] & B[2]);
  638. C[1] ^= A[0][1] = B[1] ^ (~B[2] & B[3]);
  639. C[2] ^= A[1][2] = B[2] ^ (~B[3] & B[4]);
  640. C[3] ^= A[2][3] = B[3] ^ (~B[4] & B[0]);
  641. C[4] ^= A[3][4] = B[4] ^ (~B[0] & B[1]);
  642. B[0] = ROL64(A[0][4] ^ D[4], rhotates[0][4]);
  643. B[1] = ROL64(A[1][0] ^ D[0], rhotates[1][0]);
  644. B[2] = ROL64(A[2][1] ^ D[1], rhotates[2][1]);
  645. B[3] = ROL64(A[3][2] ^ D[2], rhotates[3][2]);
  646. B[4] = ROL64(A[4][3] ^ D[3], rhotates[4][3]);
  647. C[0] ^= A[1][0] = B[0] ^ (~B[1] & B[2]);
  648. C[1] ^= A[2][1] = B[1] ^ (~B[2] & B[3]);
  649. C[2] ^= A[3][2] = B[2] ^ (~B[3] & B[4]);
  650. C[3] ^= A[4][3] = B[3] ^ (~B[4] & B[0]);
  651. C[4] ^= A[0][4] = B[4] ^ (~B[0] & B[1]);
  652. B[0] = ROL64(A[0][2] ^ D[2], rhotates[0][2]);
  653. B[1] = ROL64(A[1][3] ^ D[3], rhotates[1][3]);
  654. B[2] = ROL64(A[2][4] ^ D[4], rhotates[2][4]);
  655. B[3] = ROL64(A[3][0] ^ D[0], rhotates[3][0]);
  656. B[4] = ROL64(A[4][1] ^ D[1], rhotates[4][1]);
  657. C[0] ^= A[3][0] = B[0] ^ (~B[1] & B[2]);
  658. C[1] ^= A[4][1] = B[1] ^ (~B[2] & B[3]);
  659. C[2] ^= A[0][2] = B[2] ^ (~B[3] & B[4]);
  660. C[3] ^= A[1][3] = B[3] ^ (~B[4] & B[0]);
  661. C[4] ^= A[2][4] = B[4] ^ (~B[0] & B[1]);
  662. /* Round 4*n+1 */
  663. D[0] = ROL64(C[1], 1) ^ C[4];
  664. D[1] = ROL64(C[2], 1) ^ C[0];
  665. D[2] = ROL64(C[3], 1) ^ C[1];
  666. D[3] = ROL64(C[4], 1) ^ C[2];
  667. D[4] = ROL64(C[0], 1) ^ C[3];
  668. B[0] = A[0][0] ^ D[0]; /* rotate by 0 */
  669. B[1] = ROL64(A[3][1] ^ D[1], rhotates[1][1]);
  670. B[2] = ROL64(A[1][2] ^ D[2], rhotates[2][2]);
  671. B[3] = ROL64(A[4][3] ^ D[3], rhotates[3][3]);
  672. B[4] = ROL64(A[2][4] ^ D[4], rhotates[4][4]);
  673. C[0] = A[0][0] = B[0] ^ (~B[1] & B[2]) ^ iotas[i + 1];
  674. C[1] = A[3][1] = B[1] ^ (~B[2] & B[3]);
  675. C[2] = A[1][2] = B[2] ^ (~B[3] & B[4]);
  676. C[3] = A[4][3] = B[3] ^ (~B[4] & B[0]);
  677. C[4] = A[2][4] = B[4] ^ (~B[0] & B[1]);
  678. B[0] = ROL64(A[3][3] ^ D[3], rhotates[0][3]);
  679. B[1] = ROL64(A[1][4] ^ D[4], rhotates[1][4]);
  680. B[2] = ROL64(A[4][0] ^ D[0], rhotates[2][0]);
  681. B[3] = ROL64(A[2][1] ^ D[1], rhotates[3][1]);
  682. B[4] = ROL64(A[0][2] ^ D[2], rhotates[4][2]);
  683. C[0] ^= A[4][0] = B[0] ^ (~B[1] & B[2]);
  684. C[1] ^= A[2][1] = B[1] ^ (~B[2] & B[3]);
  685. C[2] ^= A[0][2] = B[2] ^ (~B[3] & B[4]);
  686. C[3] ^= A[3][3] = B[3] ^ (~B[4] & B[0]);
  687. C[4] ^= A[1][4] = B[4] ^ (~B[0] & B[1]);
  688. B[0] = ROL64(A[1][1] ^ D[1], rhotates[0][1]);
  689. B[1] = ROL64(A[4][2] ^ D[2], rhotates[1][2]);
  690. B[2] = ROL64(A[2][3] ^ D[3], rhotates[2][3]);
  691. B[3] = ROL64(A[0][4] ^ D[4], rhotates[3][4]);
  692. B[4] = ROL64(A[3][0] ^ D[0], rhotates[4][0]);
  693. C[0] ^= A[3][0] = B[0] ^ (~B[1] & B[2]);
  694. C[1] ^= A[1][1] = B[1] ^ (~B[2] & B[3]);
  695. C[2] ^= A[4][2] = B[2] ^ (~B[3] & B[4]);
  696. C[3] ^= A[2][3] = B[3] ^ (~B[4] & B[0]);
  697. C[4] ^= A[0][4] = B[4] ^ (~B[0] & B[1]);
  698. B[0] = ROL64(A[4][4] ^ D[4], rhotates[0][4]);
  699. B[1] = ROL64(A[2][0] ^ D[0], rhotates[1][0]);
  700. B[2] = ROL64(A[0][1] ^ D[1], rhotates[2][1]);
  701. B[3] = ROL64(A[3][2] ^ D[2], rhotates[3][2]);
  702. B[4] = ROL64(A[1][3] ^ D[3], rhotates[4][3]);
  703. C[0] ^= A[2][0] = B[0] ^ (~B[1] & B[2]);
  704. C[1] ^= A[0][1] = B[1] ^ (~B[2] & B[3]);
  705. C[2] ^= A[3][2] = B[2] ^ (~B[3] & B[4]);
  706. C[3] ^= A[1][3] = B[3] ^ (~B[4] & B[0]);
  707. C[4] ^= A[4][4] = B[4] ^ (~B[0] & B[1]);
  708. B[0] = ROL64(A[2][2] ^ D[2], rhotates[0][2]);
  709. B[1] = ROL64(A[0][3] ^ D[3], rhotates[1][3]);
  710. B[2] = ROL64(A[3][4] ^ D[4], rhotates[2][4]);
  711. B[3] = ROL64(A[1][0] ^ D[0], rhotates[3][0]);
  712. B[4] = ROL64(A[4][1] ^ D[1], rhotates[4][1]);
  713. C[0] ^= A[1][0] = B[0] ^ (~B[1] & B[2]);
  714. C[1] ^= A[4][1] = B[1] ^ (~B[2] & B[3]);
  715. C[2] ^= A[2][2] = B[2] ^ (~B[3] & B[4]);
  716. C[3] ^= A[0][3] = B[3] ^ (~B[4] & B[0]);
  717. C[4] ^= A[3][4] = B[4] ^ (~B[0] & B[1]);
  718. /* Round 4*n+2 */
  719. D[0] = ROL64(C[1], 1) ^ C[4];
  720. D[1] = ROL64(C[2], 1) ^ C[0];
  721. D[2] = ROL64(C[3], 1) ^ C[1];
  722. D[3] = ROL64(C[4], 1) ^ C[2];
  723. D[4] = ROL64(C[0], 1) ^ C[3];
  724. B[0] = A[0][0] ^ D[0]; /* rotate by 0 */
  725. B[1] = ROL64(A[2][1] ^ D[1], rhotates[1][1]);
  726. B[2] = ROL64(A[4][2] ^ D[2], rhotates[2][2]);
  727. B[3] = ROL64(A[1][3] ^ D[3], rhotates[3][3]);
  728. B[4] = ROL64(A[3][4] ^ D[4], rhotates[4][4]);
  729. C[0] = A[0][0] = B[0] ^ (~B[1] & B[2]) ^ iotas[i + 2];
  730. C[1] = A[2][1] = B[1] ^ (~B[2] & B[3]);
  731. C[2] = A[4][2] = B[2] ^ (~B[3] & B[4]);
  732. C[3] = A[1][3] = B[3] ^ (~B[4] & B[0]);
  733. C[4] = A[3][4] = B[4] ^ (~B[0] & B[1]);
  734. B[0] = ROL64(A[4][3] ^ D[3], rhotates[0][3]);
  735. B[1] = ROL64(A[1][4] ^ D[4], rhotates[1][4]);
  736. B[2] = ROL64(A[3][0] ^ D[0], rhotates[2][0]);
  737. B[3] = ROL64(A[0][1] ^ D[1], rhotates[3][1]);
  738. B[4] = ROL64(A[2][2] ^ D[2], rhotates[4][2]);
  739. C[0] ^= A[3][0] = B[0] ^ (~B[1] & B[2]);
  740. C[1] ^= A[0][1] = B[1] ^ (~B[2] & B[3]);
  741. C[2] ^= A[2][2] = B[2] ^ (~B[3] & B[4]);
  742. C[3] ^= A[4][3] = B[3] ^ (~B[4] & B[0]);
  743. C[4] ^= A[1][4] = B[4] ^ (~B[0] & B[1]);
  744. B[0] = ROL64(A[3][1] ^ D[1], rhotates[0][1]);
  745. B[1] = ROL64(A[0][2] ^ D[2], rhotates[1][2]);
  746. B[2] = ROL64(A[2][3] ^ D[3], rhotates[2][3]);
  747. B[3] = ROL64(A[4][4] ^ D[4], rhotates[3][4]);
  748. B[4] = ROL64(A[1][0] ^ D[0], rhotates[4][0]);
  749. C[0] ^= A[1][0] = B[0] ^ (~B[1] & B[2]);
  750. C[1] ^= A[3][1] = B[1] ^ (~B[2] & B[3]);
  751. C[2] ^= A[0][2] = B[2] ^ (~B[3] & B[4]);
  752. C[3] ^= A[2][3] = B[3] ^ (~B[4] & B[0]);
  753. C[4] ^= A[4][4] = B[4] ^ (~B[0] & B[1]);
  754. B[0] = ROL64(A[2][4] ^ D[4], rhotates[0][4]);
  755. B[1] = ROL64(A[4][0] ^ D[0], rhotates[1][0]);
  756. B[2] = ROL64(A[1][1] ^ D[1], rhotates[2][1]);
  757. B[3] = ROL64(A[3][2] ^ D[2], rhotates[3][2]);
  758. B[4] = ROL64(A[0][3] ^ D[3], rhotates[4][3]);
  759. C[0] ^= A[4][0] = B[0] ^ (~B[1] & B[2]);
  760. C[1] ^= A[1][1] = B[1] ^ (~B[2] & B[3]);
  761. C[2] ^= A[3][2] = B[2] ^ (~B[3] & B[4]);
  762. C[3] ^= A[0][3] = B[3] ^ (~B[4] & B[0]);
  763. C[4] ^= A[2][4] = B[4] ^ (~B[0] & B[1]);
  764. B[0] = ROL64(A[1][2] ^ D[2], rhotates[0][2]);
  765. B[1] = ROL64(A[3][3] ^ D[3], rhotates[1][3]);
  766. B[2] = ROL64(A[0][4] ^ D[4], rhotates[2][4]);
  767. B[3] = ROL64(A[2][0] ^ D[0], rhotates[3][0]);
  768. B[4] = ROL64(A[4][1] ^ D[1], rhotates[4][1]);
  769. C[0] ^= A[2][0] = B[0] ^ (~B[1] & B[2]);
  770. C[1] ^= A[4][1] = B[1] ^ (~B[2] & B[3]);
  771. C[2] ^= A[1][2] = B[2] ^ (~B[3] & B[4]);
  772. C[3] ^= A[3][3] = B[3] ^ (~B[4] & B[0]);
  773. C[4] ^= A[0][4] = B[4] ^ (~B[0] & B[1]);
  774. /* Round 4*n+3 */
  775. D[0] = ROL64(C[1], 1) ^ C[4];
  776. D[1] = ROL64(C[2], 1) ^ C[0];
  777. D[2] = ROL64(C[3], 1) ^ C[1];
  778. D[3] = ROL64(C[4], 1) ^ C[2];
  779. D[4] = ROL64(C[0], 1) ^ C[3];
  780. B[0] = A[0][0] ^ D[0]; /* rotate by 0 */
  781. B[1] = ROL64(A[0][1] ^ D[1], rhotates[1][1]);
  782. B[2] = ROL64(A[0][2] ^ D[2], rhotates[2][2]);
  783. B[3] = ROL64(A[0][3] ^ D[3], rhotates[3][3]);
  784. B[4] = ROL64(A[0][4] ^ D[4], rhotates[4][4]);
  785. /* C[0] = */ A[0][0] = B[0] ^ (~B[1] & B[2]) ^ iotas[i + 3];
  786. /* C[1] = */ A[0][1] = B[1] ^ (~B[2] & B[3]);
  787. /* C[2] = */ A[0][2] = B[2] ^ (~B[3] & B[4]);
  788. /* C[3] = */ A[0][3] = B[3] ^ (~B[4] & B[0]);
  789. /* C[4] = */ A[0][4] = B[4] ^ (~B[0] & B[1]);
  790. B[0] = ROL64(A[1][3] ^ D[3], rhotates[0][3]);
  791. B[1] = ROL64(A[1][4] ^ D[4], rhotates[1][4]);
  792. B[2] = ROL64(A[1][0] ^ D[0], rhotates[2][0]);
  793. B[3] = ROL64(A[1][1] ^ D[1], rhotates[3][1]);
  794. B[4] = ROL64(A[1][2] ^ D[2], rhotates[4][2]);
  795. /* C[0] ^= */ A[1][0] = B[0] ^ (~B[1] & B[2]);
  796. /* C[1] ^= */ A[1][1] = B[1] ^ (~B[2] & B[3]);
  797. /* C[2] ^= */ A[1][2] = B[2] ^ (~B[3] & B[4]);
  798. /* C[3] ^= */ A[1][3] = B[3] ^ (~B[4] & B[0]);
  799. /* C[4] ^= */ A[1][4] = B[4] ^ (~B[0] & B[1]);
  800. B[0] = ROL64(A[2][1] ^ D[1], rhotates[0][1]);
  801. B[1] = ROL64(A[2][2] ^ D[2], rhotates[1][2]);
  802. B[2] = ROL64(A[2][3] ^ D[3], rhotates[2][3]);
  803. B[3] = ROL64(A[2][4] ^ D[4], rhotates[3][4]);
  804. B[4] = ROL64(A[2][0] ^ D[0], rhotates[4][0]);
  805. /* C[0] ^= */ A[2][0] = B[0] ^ (~B[1] & B[2]);
  806. /* C[1] ^= */ A[2][1] = B[1] ^ (~B[2] & B[3]);
  807. /* C[2] ^= */ A[2][2] = B[2] ^ (~B[3] & B[4]);
  808. /* C[3] ^= */ A[2][3] = B[3] ^ (~B[4] & B[0]);
  809. /* C[4] ^= */ A[2][4] = B[4] ^ (~B[0] & B[1]);
  810. B[0] = ROL64(A[3][4] ^ D[4], rhotates[0][4]);
  811. B[1] = ROL64(A[3][0] ^ D[0], rhotates[1][0]);
  812. B[2] = ROL64(A[3][1] ^ D[1], rhotates[2][1]);
  813. B[3] = ROL64(A[3][2] ^ D[2], rhotates[3][2]);
  814. B[4] = ROL64(A[3][3] ^ D[3], rhotates[4][3]);
  815. /* C[0] ^= */ A[3][0] = B[0] ^ (~B[1] & B[2]);
  816. /* C[1] ^= */ A[3][1] = B[1] ^ (~B[2] & B[3]);
  817. /* C[2] ^= */ A[3][2] = B[2] ^ (~B[3] & B[4]);
  818. /* C[3] ^= */ A[3][3] = B[3] ^ (~B[4] & B[0]);
  819. /* C[4] ^= */ A[3][4] = B[4] ^ (~B[0] & B[1]);
  820. B[0] = ROL64(A[4][2] ^ D[2], rhotates[0][2]);
  821. B[1] = ROL64(A[4][3] ^ D[3], rhotates[1][3]);
  822. B[2] = ROL64(A[4][4] ^ D[4], rhotates[2][4]);
  823. B[3] = ROL64(A[4][0] ^ D[0], rhotates[3][0]);
  824. B[4] = ROL64(A[4][1] ^ D[1], rhotates[4][1]);
  825. /* C[0] ^= */ A[4][0] = B[0] ^ (~B[1] & B[2]);
  826. /* C[1] ^= */ A[4][1] = B[1] ^ (~B[2] & B[3]);
  827. /* C[2] ^= */ A[4][2] = B[2] ^ (~B[3] & B[4]);
  828. /* C[3] ^= */ A[4][3] = B[3] ^ (~B[4] & B[0]);
  829. /* C[4] ^= */ A[4][4] = B[4] ^ (~B[0] & B[1]);
  830. }
  831. static void KeccakF1600(uint64_t A[5][5])
  832. {
  833. size_t i;
  834. for (i = 0; i < 24; i += 4) {
  835. FourRounds(A, i);
  836. }
  837. }
  838. #endif
  839. static uint64_t BitInterleave(uint64_t Ai)
  840. {
  841. if (BIT_INTERLEAVE) {
  842. uint32_t hi = (uint32_t)(Ai >> 32), lo = (uint32_t)Ai;
  843. uint32_t t0, t1;
  844. t0 = lo & 0x55555555;
  845. t0 |= t0 >> 1; t0 &= 0x33333333;
  846. t0 |= t0 >> 2; t0 &= 0x0f0f0f0f;
  847. t0 |= t0 >> 4; t0 &= 0x00ff00ff;
  848. t0 |= t0 >> 8; t0 &= 0x0000ffff;
  849. t1 = hi & 0x55555555;
  850. t1 |= t1 >> 1; t1 &= 0x33333333;
  851. t1 |= t1 >> 2; t1 &= 0x0f0f0f0f;
  852. t1 |= t1 >> 4; t1 &= 0x00ff00ff;
  853. t1 |= t1 >> 8; t1 <<= 16;
  854. lo &= 0xaaaaaaaa;
  855. lo |= lo << 1; lo &= 0xcccccccc;
  856. lo |= lo << 2; lo &= 0xf0f0f0f0;
  857. lo |= lo << 4; lo &= 0xff00ff00;
  858. lo |= lo << 8; lo >>= 16;
  859. hi &= 0xaaaaaaaa;
  860. hi |= hi << 1; hi &= 0xcccccccc;
  861. hi |= hi << 2; hi &= 0xf0f0f0f0;
  862. hi |= hi << 4; hi &= 0xff00ff00;
  863. hi |= hi << 8; hi &= 0xffff0000;
  864. Ai = ((uint64_t)(hi | lo) << 32) | (t1 | t0);
  865. }
  866. return Ai;
  867. }
  868. static uint64_t BitDeinterleave(uint64_t Ai)
  869. {
  870. if (BIT_INTERLEAVE) {
  871. uint32_t hi = (uint32_t)(Ai >> 32), lo = (uint32_t)Ai;
  872. uint32_t t0, t1;
  873. t0 = lo & 0x0000ffff;
  874. t0 |= t0 << 8; t0 &= 0x00ff00ff;
  875. t0 |= t0 << 4; t0 &= 0x0f0f0f0f;
  876. t0 |= t0 << 2; t0 &= 0x33333333;
  877. t0 |= t0 << 1; t0 &= 0x55555555;
  878. t1 = hi << 16;
  879. t1 |= t1 >> 8; t1 &= 0xff00ff00;
  880. t1 |= t1 >> 4; t1 &= 0xf0f0f0f0;
  881. t1 |= t1 >> 2; t1 &= 0xcccccccc;
  882. t1 |= t1 >> 1; t1 &= 0xaaaaaaaa;
  883. lo >>= 16;
  884. lo |= lo << 8; lo &= 0x00ff00ff;
  885. lo |= lo << 4; lo &= 0x0f0f0f0f;
  886. lo |= lo << 2; lo &= 0x33333333;
  887. lo |= lo << 1; lo &= 0x55555555;
  888. hi &= 0xffff0000;
  889. hi |= hi >> 8; hi &= 0xff00ff00;
  890. hi |= hi >> 4; hi &= 0xf0f0f0f0;
  891. hi |= hi >> 2; hi &= 0xcccccccc;
  892. hi |= hi >> 1; hi &= 0xaaaaaaaa;
  893. Ai = ((uint64_t)(hi | lo) << 32) | (t1 | t0);
  894. }
  895. return Ai;
  896. }
  897. /*
  898. * SHA3_absorb can be called multiple times, but at each invocation
  899. * largest multiple of |r| out of |len| bytes are processed. Then
  900. * remaining amount of bytes is returned. This is done to spare caller
  901. * trouble of calculating the largest multiple of |r|. |r| can be viewed
  902. * as blocksize. It is commonly (1600 - 256*n)/8, e.g. 168, 136, 104,
  903. * 72, but can also be (1600 - 448)/8 = 144. All this means that message
  904. * padding and intermediate sub-block buffering, byte- or bitwise, is
  905. * caller's responsibility.
  906. */
  907. size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len,
  908. size_t r)
  909. {
  910. uint64_t *A_flat = (uint64_t *)A;
  911. size_t i, w = r / 8;
  912. assert(r < (25 * sizeof(A[0][0])) && (r % 8) == 0);
  913. while (len >= r) {
  914. for (i = 0; i < w; i++) {
  915. uint64_t Ai = (uint64_t)inp[0] | (uint64_t)inp[1] << 8 |
  916. (uint64_t)inp[2] << 16 | (uint64_t)inp[3] << 24 |
  917. (uint64_t)inp[4] << 32 | (uint64_t)inp[5] << 40 |
  918. (uint64_t)inp[6] << 48 | (uint64_t)inp[7] << 56;
  919. inp += 8;
  920. A_flat[i] ^= BitInterleave(Ai);
  921. }
  922. KeccakF1600(A);
  923. len -= r;
  924. }
  925. return len;
  926. }
  927. /*
  928. * sha3_squeeze is called once at the end to generate |out| hash value
  929. * of |len| bytes.
  930. */
  931. void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r)
  932. {
  933. uint64_t *A_flat = (uint64_t *)A;
  934. size_t i, w = r / 8;
  935. assert(r < (25 * sizeof(A[0][0])) && (r % 8) == 0);
  936. while (len != 0) {
  937. for (i = 0; i < w && len != 0; i++) {
  938. uint64_t Ai = BitDeinterleave(A_flat[i]);
  939. if (len < 8) {
  940. for (i = 0; i < len; i++) {
  941. *out++ = (unsigned char)Ai;
  942. Ai >>= 8;
  943. }
  944. return;
  945. }
  946. out[0] = (unsigned char)(Ai);
  947. out[1] = (unsigned char)(Ai >> 8);
  948. out[2] = (unsigned char)(Ai >> 16);
  949. out[3] = (unsigned char)(Ai >> 24);
  950. out[4] = (unsigned char)(Ai >> 32);
  951. out[5] = (unsigned char)(Ai >> 40);
  952. out[6] = (unsigned char)(Ai >> 48);
  953. out[7] = (unsigned char)(Ai >> 56);
  954. out += 8;
  955. len -= 8;
  956. }
  957. if (len)
  958. KeccakF1600(A);
  959. }
  960. }
  961. #endif
  962. #ifdef SELFTEST
  963. /*
  964. * Post-padding one-shot implementations would look as following:
  965. *
  966. * SHA3_224 SHA3_sponge(inp, len, out, 224/8, (1600-448)/8);
  967. * SHA3_256 SHA3_sponge(inp, len, out, 256/8, (1600-512)/8);
  968. * SHA3_384 SHA3_sponge(inp, len, out, 384/8, (1600-768)/8);
  969. * SHA3_512 SHA3_sponge(inp, len, out, 512/8, (1600-1024)/8);
  970. * SHAKE_128 SHA3_sponge(inp, len, out, d, (1600-256)/8);
  971. * SHAKE_256 SHA3_sponge(inp, len, out, d, (1600-512)/8);
  972. */
  973. void SHA3_sponge(const unsigned char *inp, size_t len,
  974. unsigned char *out, size_t d, size_t r)
  975. {
  976. uint64_t A[5][5];
  977. memset(A, 0, sizeof(A));
  978. SHA3_absorb(A, inp, len, r);
  979. SHA3_squeeze(A, out, d, r);
  980. }
  981. # include <stdio.h>
  982. int main()
  983. {
  984. /*
  985. * This is 5-bit SHAKE128 test from http://csrc.nist.gov/groups/ST/toolkit/examples.html#aHashing
  986. */
  987. unsigned char test[168] = { '\xf3', '\x3' };
  988. unsigned char out[512];
  989. size_t i;
  990. static const unsigned char result[512] = {
  991. 0x2E, 0x0A, 0xBF, 0xBA, 0x83, 0xE6, 0x72, 0x0B,
  992. 0xFB, 0xC2, 0x25, 0xFF, 0x6B, 0x7A, 0xB9, 0xFF,
  993. 0xCE, 0x58, 0xBA, 0x02, 0x7E, 0xE3, 0xD8, 0x98,
  994. 0x76, 0x4F, 0xEF, 0x28, 0x7D, 0xDE, 0xCC, 0xCA,
  995. 0x3E, 0x6E, 0x59, 0x98, 0x41, 0x1E, 0x7D, 0xDB,
  996. 0x32, 0xF6, 0x75, 0x38, 0xF5, 0x00, 0xB1, 0x8C,
  997. 0x8C, 0x97, 0xC4, 0x52, 0xC3, 0x70, 0xEA, 0x2C,
  998. 0xF0, 0xAF, 0xCA, 0x3E, 0x05, 0xDE, 0x7E, 0x4D,
  999. 0xE2, 0x7F, 0xA4, 0x41, 0xA9, 0xCB, 0x34, 0xFD,
  1000. 0x17, 0xC9, 0x78, 0xB4, 0x2D, 0x5B, 0x7E, 0x7F,
  1001. 0x9A, 0xB1, 0x8F, 0xFE, 0xFF, 0xC3, 0xC5, 0xAC,
  1002. 0x2F, 0x3A, 0x45, 0x5E, 0xEB, 0xFD, 0xC7, 0x6C,
  1003. 0xEA, 0xEB, 0x0A, 0x2C, 0xCA, 0x22, 0xEE, 0xF6,
  1004. 0xE6, 0x37, 0xF4, 0xCA, 0xBE, 0x5C, 0x51, 0xDE,
  1005. 0xD2, 0xE3, 0xFA, 0xD8, 0xB9, 0x52, 0x70, 0xA3,
  1006. 0x21, 0x84, 0x56, 0x64, 0xF1, 0x07, 0xD1, 0x64,
  1007. 0x96, 0xBB, 0x7A, 0xBF, 0xBE, 0x75, 0x04, 0xB6,
  1008. 0xED, 0xE2, 0xE8, 0x9E, 0x4B, 0x99, 0x6F, 0xB5,
  1009. 0x8E, 0xFD, 0xC4, 0x18, 0x1F, 0x91, 0x63, 0x38,
  1010. 0x1C, 0xBE, 0x7B, 0xC0, 0x06, 0xA7, 0xA2, 0x05,
  1011. 0x98, 0x9C, 0x52, 0x6C, 0xD1, 0xBD, 0x68, 0x98,
  1012. 0x36, 0x93, 0xB4, 0xBD, 0xC5, 0x37, 0x28, 0xB2,
  1013. 0x41, 0xC1, 0xCF, 0xF4, 0x2B, 0xB6, 0x11, 0x50,
  1014. 0x2C, 0x35, 0x20, 0x5C, 0xAB, 0xB2, 0x88, 0x75,
  1015. 0x56, 0x55, 0xD6, 0x20, 0xC6, 0x79, 0x94, 0xF0,
  1016. 0x64, 0x51, 0x18, 0x7F, 0x6F, 0xD1, 0x7E, 0x04,
  1017. 0x66, 0x82, 0xBA, 0x12, 0x86, 0x06, 0x3F, 0xF8,
  1018. 0x8F, 0xE2, 0x50, 0x8D, 0x1F, 0xCA, 0xF9, 0x03,
  1019. 0x5A, 0x12, 0x31, 0xAD, 0x41, 0x50, 0xA9, 0xC9,
  1020. 0xB2, 0x4C, 0x9B, 0x2D, 0x66, 0xB2, 0xAD, 0x1B,
  1021. 0xDE, 0x0B, 0xD0, 0xBB, 0xCB, 0x8B, 0xE0, 0x5B,
  1022. 0x83, 0x52, 0x29, 0xEF, 0x79, 0x19, 0x73, 0x73,
  1023. 0x23, 0x42, 0x44, 0x01, 0xE1, 0xD8, 0x37, 0xB6,
  1024. 0x6E, 0xB4, 0xE6, 0x30, 0xFF, 0x1D, 0xE7, 0x0C,
  1025. 0xB3, 0x17, 0xC2, 0xBA, 0xCB, 0x08, 0x00, 0x1D,
  1026. 0x34, 0x77, 0xB7, 0xA7, 0x0A, 0x57, 0x6D, 0x20,
  1027. 0x86, 0x90, 0x33, 0x58, 0x9D, 0x85, 0xA0, 0x1D,
  1028. 0xDB, 0x2B, 0x66, 0x46, 0xC0, 0x43, 0xB5, 0x9F,
  1029. 0xC0, 0x11, 0x31, 0x1D, 0xA6, 0x66, 0xFA, 0x5A,
  1030. 0xD1, 0xD6, 0x38, 0x7F, 0xA9, 0xBC, 0x40, 0x15,
  1031. 0xA3, 0x8A, 0x51, 0xD1, 0xDA, 0x1E, 0xA6, 0x1D,
  1032. 0x64, 0x8D, 0xC8, 0xE3, 0x9A, 0x88, 0xB9, 0xD6,
  1033. 0x22, 0xBD, 0xE2, 0x07, 0xFD, 0xAB, 0xC6, 0xF2,
  1034. 0x82, 0x7A, 0x88, 0x0C, 0x33, 0x0B, 0xBF, 0x6D,
  1035. 0xF7, 0x33, 0x77, 0x4B, 0x65, 0x3E, 0x57, 0x30,
  1036. 0x5D, 0x78, 0xDC, 0xE1, 0x12, 0xF1, 0x0A, 0x2C,
  1037. 0x71, 0xF4, 0xCD, 0xAD, 0x92, 0xED, 0x11, 0x3E,
  1038. 0x1C, 0xEA, 0x63, 0xB9, 0x19, 0x25, 0xED, 0x28,
  1039. 0x19, 0x1E, 0x6D, 0xBB, 0xB5, 0xAA, 0x5A, 0x2A,
  1040. 0xFD, 0xA5, 0x1F, 0xC0, 0x5A, 0x3A, 0xF5, 0x25,
  1041. 0x8B, 0x87, 0x66, 0x52, 0x43, 0x55, 0x0F, 0x28,
  1042. 0x94, 0x8A, 0xE2, 0xB8, 0xBE, 0xB6, 0xBC, 0x9C,
  1043. 0x77, 0x0B, 0x35, 0xF0, 0x67, 0xEA, 0xA6, 0x41,
  1044. 0xEF, 0xE6, 0x5B, 0x1A, 0x44, 0x90, 0x9D, 0x1B,
  1045. 0x14, 0x9F, 0x97, 0xEE, 0xA6, 0x01, 0x39, 0x1C,
  1046. 0x60, 0x9E, 0xC8, 0x1D, 0x19, 0x30, 0xF5, 0x7C,
  1047. 0x18, 0xA4, 0xE0, 0xFA, 0xB4, 0x91, 0xD1, 0xCA,
  1048. 0xDF, 0xD5, 0x04, 0x83, 0x44, 0x9E, 0xDC, 0x0F,
  1049. 0x07, 0xFF, 0xB2, 0x4D, 0x2C, 0x6F, 0x9A, 0x9A,
  1050. 0x3B, 0xFF, 0x39, 0xAE, 0x3D, 0x57, 0xF5, 0x60,
  1051. 0x65, 0x4D, 0x7D, 0x75, 0xC9, 0x08, 0xAB, 0xE6,
  1052. 0x25, 0x64, 0x75, 0x3E, 0xAC, 0x39, 0xD7, 0x50,
  1053. 0x3D, 0xA6, 0xD3, 0x7C, 0x2E, 0x32, 0xE1, 0xAF,
  1054. 0x3B, 0x8A, 0xEC, 0x8A, 0xE3, 0x06, 0x9C, 0xD9
  1055. };
  1056. test[167] = '\x80';
  1057. SHA3_sponge(test, sizeof(test), out, sizeof(out), sizeof(test));
  1058. /*
  1059. * Rationale behind keeping output [formatted as below] is that
  1060. * one should be able to redirect it to a file, then copy-n-paste
  1061. * final "output val" from official example to another file, and
  1062. * compare the two with diff(1).
  1063. */
  1064. for (i = 0; i < sizeof(out);) {
  1065. printf("%02X", out[i]);
  1066. printf(++i % 16 && i != sizeof(out) ? " " : "\n");
  1067. }
  1068. if (memcmp(out, result, sizeof(out))) {
  1069. fprintf(stderr, "failure\n");
  1070. return 1;
  1071. } else {
  1072. fprintf(stderr, "success\n");
  1073. return 0;
  1074. }
  1075. }
  1076. #endif