hash_md5_sha.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2010 Denys Vlasenko
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. #define STR1(s) #s
  11. #define STR(s) STR1(s)
  12. #define NEED_SHA512 (ENABLE_SHA512SUM || ENABLE_USE_BB_CRYPT_SHA)
  13. #if ENABLE_SHA1_HWACCEL || ENABLE_SHA256_HWACCEL
  14. # if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  15. static void cpuid(unsigned *eax, unsigned *ebx, unsigned *ecx, unsigned *edx)
  16. {
  17. asm ("cpuid"
  18. : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
  19. : "0"(*eax), "1"(*ebx), "2"(*ecx), "3"(*edx)
  20. );
  21. }
  22. static smallint shaNI;
  23. void FAST_FUNC sha1_process_block64_shaNI(sha1_ctx_t *ctx);
  24. void FAST_FUNC sha256_process_block64_shaNI(sha256_ctx_t *ctx);
  25. # if defined(__i386__)
  26. struct ASM_expects_76_shaNI { char t[1 - 2*(offsetof(sha256_ctx_t, hash) != 76)]; };
  27. # endif
  28. # if defined(__x86_64__)
  29. struct ASM_expects_80_shaNI { char t[1 - 2*(offsetof(sha256_ctx_t, hash) != 80)]; };
  30. # endif
  31. # endif
  32. #endif
  33. /* gcc 4.2.1 optimizes rotr64 better with inline than with macro
  34. * (for rotX32, there is no difference). Why? My guess is that
  35. * macro requires clever common subexpression elimination heuristics
  36. * in gcc, while inline basically forces it to happen.
  37. */
  38. //#define rotl32(x,n) (((x) << (n)) | ((x) >> (32 - (n))))
  39. static ALWAYS_INLINE uint32_t rotl32(uint32_t x, unsigned n)
  40. {
  41. return (x << n) | (x >> (32 - n));
  42. }
  43. //#define rotr32(x,n) (((x) >> (n)) | ((x) << (32 - (n))))
  44. static ALWAYS_INLINE uint32_t rotr32(uint32_t x, unsigned n)
  45. {
  46. return (x >> n) | (x << (32 - n));
  47. }
  48. /* rotr64 in needed for sha512 only: */
  49. //#define rotr64(x,n) (((x) >> (n)) | ((x) << (64 - (n))))
  50. static ALWAYS_INLINE uint64_t rotr64(uint64_t x, unsigned n)
  51. {
  52. return (x >> n) | (x << (64 - n));
  53. }
  54. /* rotl64 only used for sha3 currently */
  55. static ALWAYS_INLINE uint64_t rotl64(uint64_t x, unsigned n)
  56. {
  57. return (x << n) | (x >> (64 - n));
  58. }
  59. /* Process the remaining bytes in the buffer */
  60. static void FAST_FUNC common64_end(md5_ctx_t *ctx, int swap_needed)
  61. {
  62. unsigned bufpos = ctx->total64 & 63;
  63. /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */
  64. ctx->wbuffer[bufpos++] = 0x80;
  65. /* This loop iterates either once or twice, no more, no less */
  66. while (1) {
  67. unsigned remaining = 64 - bufpos;
  68. memset(ctx->wbuffer + bufpos, 0, remaining);
  69. /* Do we have enough space for the length count? */
  70. if (remaining >= 8) {
  71. /* Store the 64-bit counter of bits in the buffer */
  72. uint64_t t = ctx->total64 << 3;
  73. if (swap_needed)
  74. t = bb_bswap_64(t);
  75. /* wbuffer is suitably aligned for this */
  76. *(bb__aliased_uint64_t *) (&ctx->wbuffer[64 - 8]) = t;
  77. }
  78. ctx->process_block(ctx);
  79. if (remaining >= 8)
  80. break;
  81. bufpos = 0;
  82. }
  83. }
  84. /*
  85. * Compute MD5 checksum of strings according to the
  86. * definition of MD5 in RFC 1321 from April 1992.
  87. *
  88. * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
  89. *
  90. * Copyright (C) 1995-1999 Free Software Foundation, Inc.
  91. * Copyright (C) 2001 Manuel Novoa III
  92. * Copyright (C) 2003 Glenn L. McGrath
  93. * Copyright (C) 2003 Erik Andersen
  94. *
  95. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  96. */
  97. /* 0: fastest, 3: smallest */
  98. #if CONFIG_MD5_SMALL < 0
  99. # define MD5_SMALL 0
  100. #elif CONFIG_MD5_SMALL > 3
  101. # define MD5_SMALL 3
  102. #else
  103. # define MD5_SMALL CONFIG_MD5_SMALL
  104. #endif
  105. /* These are the four functions used in the four steps of the MD5 algorithm
  106. * and defined in the RFC 1321. The first function is a little bit optimized
  107. * (as found in Colin Plumbs public domain implementation).
  108. * #define FF(b, c, d) ((b & c) | (~b & d))
  109. */
  110. #undef FF
  111. #undef FG
  112. #undef FH
  113. #undef FI
  114. #define FF(b, c, d) (d ^ (b & (c ^ d)))
  115. #define FG(b, c, d) FF(d, b, c)
  116. #define FH(b, c, d) (b ^ c ^ d)
  117. #define FI(b, c, d) (c ^ (b | ~d))
  118. /* Hash a single block, 64 bytes long and 4-byte aligned */
  119. static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx)
  120. {
  121. #if MD5_SMALL > 0
  122. /* Before we start, one word to the strange constants.
  123. They are defined in RFC 1321 as
  124. T[i] = (int)(2^32 * fabs(sin(i))), i=1..64
  125. */
  126. static const uint32_t C_array[] ALIGN4 = {
  127. /* round 1 */
  128. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  129. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  130. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  131. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  132. /* round 2 */
  133. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  134. 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
  135. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  136. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  137. /* round 3 */
  138. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  139. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  140. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
  141. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  142. /* round 4 */
  143. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  144. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  145. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  146. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
  147. };
  148. static const char P_array[] ALIGN1 = {
  149. # if MD5_SMALL > 1
  150. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
  151. # endif
  152. 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
  153. 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
  154. 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */
  155. };
  156. #endif
  157. uint32_t *words = (void*) ctx->wbuffer;
  158. uint32_t A = ctx->hash[0];
  159. uint32_t B = ctx->hash[1];
  160. uint32_t C = ctx->hash[2];
  161. uint32_t D = ctx->hash[3];
  162. #if MD5_SMALL >= 2 /* 2 or 3 */
  163. static const char S_array[] ALIGN1 = {
  164. 7, 12, 17, 22,
  165. 5, 9, 14, 20,
  166. 4, 11, 16, 23,
  167. 6, 10, 15, 21
  168. };
  169. const uint32_t *pc;
  170. const char *pp;
  171. const char *ps;
  172. int i;
  173. uint32_t temp;
  174. if (BB_BIG_ENDIAN)
  175. for (i = 0; i < 16; i++)
  176. words[i] = SWAP_LE32(words[i]);
  177. # if MD5_SMALL == 3
  178. pc = C_array;
  179. pp = P_array;
  180. ps = S_array - 4;
  181. for (i = 0; i < 64; i++) {
  182. if ((i & 0x0f) == 0)
  183. ps += 4;
  184. temp = A;
  185. switch (i >> 4) {
  186. case 0:
  187. temp += FF(B, C, D);
  188. break;
  189. case 1:
  190. temp += FG(B, C, D);
  191. break;
  192. case 2:
  193. temp += FH(B, C, D);
  194. break;
  195. default: /* case 3 */
  196. temp += FI(B, C, D);
  197. }
  198. temp += words[(int) (*pp++)] + *pc++;
  199. temp = rotl32(temp, ps[i & 3]);
  200. temp += B;
  201. A = D;
  202. D = C;
  203. C = B;
  204. B = temp;
  205. }
  206. # else /* MD5_SMALL == 2 */
  207. pc = C_array;
  208. pp = P_array;
  209. ps = S_array;
  210. for (i = 0; i < 16; i++) {
  211. temp = A + FF(B, C, D) + words[(int) (*pp++)] + *pc++;
  212. temp = rotl32(temp, ps[i & 3]);
  213. temp += B;
  214. A = D;
  215. D = C;
  216. C = B;
  217. B = temp;
  218. }
  219. ps += 4;
  220. for (i = 0; i < 16; i++) {
  221. temp = A + FG(B, C, D) + words[(int) (*pp++)] + *pc++;
  222. temp = rotl32(temp, ps[i & 3]);
  223. temp += B;
  224. A = D;
  225. D = C;
  226. C = B;
  227. B = temp;
  228. }
  229. ps += 4;
  230. for (i = 0; i < 16; i++) {
  231. temp = A + FH(B, C, D) + words[(int) (*pp++)] + *pc++;
  232. temp = rotl32(temp, ps[i & 3]);
  233. temp += B;
  234. A = D;
  235. D = C;
  236. C = B;
  237. B = temp;
  238. }
  239. ps += 4;
  240. for (i = 0; i < 16; i++) {
  241. temp = A + FI(B, C, D) + words[(int) (*pp++)] + *pc++;
  242. temp = rotl32(temp, ps[i & 3]);
  243. temp += B;
  244. A = D;
  245. D = C;
  246. C = B;
  247. B = temp;
  248. }
  249. # endif
  250. /* Add checksum to the starting values */
  251. ctx->hash[0] += A;
  252. ctx->hash[1] += B;
  253. ctx->hash[2] += C;
  254. ctx->hash[3] += D;
  255. #else /* MD5_SMALL == 0 or 1 */
  256. # if MD5_SMALL == 1
  257. const uint32_t *pc;
  258. const char *pp;
  259. int i;
  260. # endif
  261. /* First round: using the given function, the context and a constant
  262. the next context is computed. Because the algorithm's processing
  263. unit is a 32-bit word and it is determined to work on words in
  264. little endian byte order we perhaps have to change the byte order
  265. before the computation. To reduce the work for the next steps
  266. we save swapped words in WORDS array. */
  267. # undef OP
  268. # define OP(a, b, c, d, s, T) \
  269. do { \
  270. a += FF(b, c, d) + (*words IF_BIG_ENDIAN(= SWAP_LE32(*words))) + T; \
  271. words++; \
  272. a = rotl32(a, s); \
  273. a += b; \
  274. } while (0)
  275. /* Round 1 */
  276. # if MD5_SMALL == 1
  277. pc = C_array;
  278. for (i = 0; i < 4; i++) {
  279. OP(A, B, C, D, 7, *pc++);
  280. OP(D, A, B, C, 12, *pc++);
  281. OP(C, D, A, B, 17, *pc++);
  282. OP(B, C, D, A, 22, *pc++);
  283. }
  284. # else
  285. OP(A, B, C, D, 7, 0xd76aa478);
  286. OP(D, A, B, C, 12, 0xe8c7b756);
  287. OP(C, D, A, B, 17, 0x242070db);
  288. OP(B, C, D, A, 22, 0xc1bdceee);
  289. OP(A, B, C, D, 7, 0xf57c0faf);
  290. OP(D, A, B, C, 12, 0x4787c62a);
  291. OP(C, D, A, B, 17, 0xa8304613);
  292. OP(B, C, D, A, 22, 0xfd469501);
  293. OP(A, B, C, D, 7, 0x698098d8);
  294. OP(D, A, B, C, 12, 0x8b44f7af);
  295. OP(C, D, A, B, 17, 0xffff5bb1);
  296. OP(B, C, D, A, 22, 0x895cd7be);
  297. OP(A, B, C, D, 7, 0x6b901122);
  298. OP(D, A, B, C, 12, 0xfd987193);
  299. OP(C, D, A, B, 17, 0xa679438e);
  300. OP(B, C, D, A, 22, 0x49b40821);
  301. # endif
  302. words -= 16;
  303. /* For the second to fourth round we have the possibly swapped words
  304. in WORDS. Redefine the macro to take an additional first
  305. argument specifying the function to use. */
  306. # undef OP
  307. # define OP(f, a, b, c, d, k, s, T) \
  308. do { \
  309. a += f(b, c, d) + words[k] + T; \
  310. a = rotl32(a, s); \
  311. a += b; \
  312. } while (0)
  313. /* Round 2 */
  314. # if MD5_SMALL == 1
  315. pp = P_array;
  316. for (i = 0; i < 4; i++) {
  317. OP(FG, A, B, C, D, (int) (*pp++), 5, *pc++);
  318. OP(FG, D, A, B, C, (int) (*pp++), 9, *pc++);
  319. OP(FG, C, D, A, B, (int) (*pp++), 14, *pc++);
  320. OP(FG, B, C, D, A, (int) (*pp++), 20, *pc++);
  321. }
  322. # else
  323. OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
  324. OP(FG, D, A, B, C, 6, 9, 0xc040b340);
  325. OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
  326. OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
  327. OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
  328. OP(FG, D, A, B, C, 10, 9, 0x02441453);
  329. OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
  330. OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
  331. OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
  332. OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
  333. OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
  334. OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
  335. OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
  336. OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
  337. OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
  338. OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
  339. # endif
  340. /* Round 3 */
  341. # if MD5_SMALL == 1
  342. for (i = 0; i < 4; i++) {
  343. OP(FH, A, B, C, D, (int) (*pp++), 4, *pc++);
  344. OP(FH, D, A, B, C, (int) (*pp++), 11, *pc++);
  345. OP(FH, C, D, A, B, (int) (*pp++), 16, *pc++);
  346. OP(FH, B, C, D, A, (int) (*pp++), 23, *pc++);
  347. }
  348. # else
  349. OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
  350. OP(FH, D, A, B, C, 8, 11, 0x8771f681);
  351. OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
  352. OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
  353. OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
  354. OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
  355. OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
  356. OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
  357. OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
  358. OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
  359. OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
  360. OP(FH, B, C, D, A, 6, 23, 0x04881d05);
  361. OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
  362. OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
  363. OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
  364. OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
  365. # endif
  366. /* Round 4 */
  367. # if MD5_SMALL == 1
  368. for (i = 0; i < 4; i++) {
  369. OP(FI, A, B, C, D, (int) (*pp++), 6, *pc++);
  370. OP(FI, D, A, B, C, (int) (*pp++), 10, *pc++);
  371. OP(FI, C, D, A, B, (int) (*pp++), 15, *pc++);
  372. OP(FI, B, C, D, A, (int) (*pp++), 21, *pc++);
  373. }
  374. # else
  375. OP(FI, A, B, C, D, 0, 6, 0xf4292244);
  376. OP(FI, D, A, B, C, 7, 10, 0x432aff97);
  377. OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
  378. OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
  379. OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
  380. OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
  381. OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
  382. OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
  383. OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
  384. OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
  385. OP(FI, C, D, A, B, 6, 15, 0xa3014314);
  386. OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
  387. OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
  388. OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
  389. OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
  390. OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
  391. # endif
  392. /* Add checksum to the starting values */
  393. ctx->hash[0] += A;
  394. ctx->hash[1] += B;
  395. ctx->hash[2] += C;
  396. ctx->hash[3] += D;
  397. #endif
  398. }
  399. #undef OP
  400. #undef FF
  401. #undef FG
  402. #undef FH
  403. #undef FI
  404. /* Initialize structure containing state of computation.
  405. * (RFC 1321, 3.3: Step 3)
  406. */
  407. void FAST_FUNC md5_begin(md5_ctx_t *ctx)
  408. {
  409. ctx->hash[0] = 0x67452301;
  410. ctx->hash[1] = 0xefcdab89;
  411. ctx->hash[2] = 0x98badcfe;
  412. ctx->hash[3] = 0x10325476;
  413. ctx->total64 = 0;
  414. ctx->process_block = md5_process_block64;
  415. }
  416. /* Used also for sha1 and sha256 */
  417. void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
  418. {
  419. unsigned bufpos = ctx->total64 & 63;
  420. ctx->total64 += len;
  421. while (1) {
  422. unsigned remaining = 64 - bufpos;
  423. if (remaining > len)
  424. remaining = len;
  425. /* Copy data into aligned buffer */
  426. memcpy(ctx->wbuffer + bufpos, buffer, remaining);
  427. len -= remaining;
  428. buffer = (const char *)buffer + remaining;
  429. bufpos += remaining;
  430. /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
  431. bufpos -= 64;
  432. if (bufpos != 0)
  433. break;
  434. /* Buffer is filled up, process it */
  435. ctx->process_block(ctx);
  436. /*bufpos = 0; - already is */
  437. }
  438. }
  439. /* Process the remaining bytes in the buffer and put result from CTX
  440. * in first 16 bytes following RESBUF. The result is always in little
  441. * endian byte order, so that a byte-wise output yields to the wanted
  442. * ASCII representation of the message digest.
  443. */
  444. unsigned FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf)
  445. {
  446. /* MD5 stores total in LE, need to swap on BE arches: */
  447. common64_end(ctx, /*swap_needed:*/ BB_BIG_ENDIAN);
  448. /* The MD5 result is in little endian byte order */
  449. if (BB_BIG_ENDIAN) {
  450. ctx->hash[0] = SWAP_LE32(ctx->hash[0]);
  451. ctx->hash[1] = SWAP_LE32(ctx->hash[1]);
  452. ctx->hash[2] = SWAP_LE32(ctx->hash[2]);
  453. ctx->hash[3] = SWAP_LE32(ctx->hash[3]);
  454. }
  455. memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * 4);
  456. return sizeof(ctx->hash[0]) * 4;
  457. }
  458. /*
  459. * SHA1 part is:
  460. * Copyright 2007 Rob Landley <rob@landley.net>
  461. *
  462. * Based on the public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
  463. * from http://www.mirrors.wiretapped.net/security/cryptography/hashes/sha1/
  464. *
  465. * Licensed under GPLv2, see file LICENSE in this source tree.
  466. *
  467. * ---------------------------------------------------------------------------
  468. *
  469. * SHA256 and SHA512 parts are:
  470. * Released into the Public Domain by Ulrich Drepper <drepper@redhat.com>.
  471. * Shrank by Denys Vlasenko.
  472. *
  473. * ---------------------------------------------------------------------------
  474. *
  475. * The best way to test random blocksizes is to go to coreutils/md5_sha1_sum.c
  476. * and replace "4096" with something like "2000 + time(NULL) % 2097",
  477. * then rebuild and compare "shaNNNsum bigfile" results.
  478. */
  479. #if CONFIG_SHA1_SMALL == 0
  480. # if defined(__GNUC__) && defined(__i386__)
  481. static void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx UNUSED_PARAM)
  482. {
  483. BUILD_BUG_ON(offsetof(sha1_ctx_t, hash) != 76);
  484. asm(
  485. "\n\
  486. pushl %ebp # \n\
  487. pushl %edi # \n\
  488. pushl %esi # \n\
  489. pushl %ebx # \n\
  490. pushl %eax \n\
  491. movl $15, %edi \n\
  492. 1: \n\
  493. movl (%eax,%edi,4), %esi \n\
  494. bswap %esi \n\
  495. pushl %esi \n\
  496. decl %edi \n\
  497. jns 1b \n\
  498. movl 80(%eax), %ebx # b = ctx->hash[1] \n\
  499. movl 84(%eax), %ecx # c = ctx->hash[2] \n\
  500. movl 88(%eax), %edx # d = ctx->hash[3] \n\
  501. movl 92(%eax), %ebp # e = ctx->hash[4] \n\
  502. movl 76(%eax), %eax # a = ctx->hash[0] \n\
  503. #Register and stack use: \n\
  504. # eax..edx: a..d \n\
  505. # ebp: e \n\
  506. # esi,edi: temps \n\
  507. # 4*n(%esp): W[n] \n\
  508. "
  509. #define RD1As(a,b,c,d,e, n, RCONST) \
  510. "\n\
  511. ##movl 4*"n"(%esp), %esi # n=0, W[0] already in %esi \n\
  512. movl "c", %edi # c \n\
  513. xorl "d", %edi # ^d \n\
  514. andl "b", %edi # &b \n\
  515. xorl "d", %edi # (((c ^ d) & b) ^ d) \n\
  516. leal "RCONST"("e",%esi), "e" # e += RCONST + W[n] \n\
  517. addl %edi, "e" # e += (((c ^ d) & b) ^ d) \n\
  518. movl "a", %esi # \n\
  519. roll $5, %esi # rotl32(a,5) \n\
  520. addl %esi, "e" # e += rotl32(a,5) \n\
  521. rorl $2, "b" # b = rotl32(b,30) \n\
  522. "
  523. #define RD1Bs(a,b,c,d,e, n, RCONST) \
  524. "\n\
  525. movl 4*"n"(%esp), %esi # W[n] \n\
  526. movl "c", %edi # c \n\
  527. xorl "d", %edi # ^d \n\
  528. andl "b", %edi # &b \n\
  529. xorl "d", %edi # (((c ^ d) & b) ^ d) \n\
  530. leal "RCONST"("e",%esi), "e" # e += RCONST + W[n] \n\
  531. addl %edi, "e" # e += (((c ^ d) & b) ^ d) \n\
  532. movl "a", %esi # \n\
  533. roll $5, %esi # rotl32(a,5) \n\
  534. addl %esi, "e" # e += rotl32(a,5) \n\
  535. rorl $2, "b" # b = rotl32(b,30) \n\
  536. "
  537. #define RD1Cs(a,b,c,d,e, n13,n8,n2,n, RCONST) \
  538. "\n\
  539. movl 4*"n13"(%esp), %esi # W[(n+13) & 15] \n\
  540. xorl 4*"n8"(%esp), %esi # ^W[(n+8) & 15] \n\
  541. xorl 4*"n2"(%esp), %esi # ^W[(n+2) & 15] \n\
  542. xorl 4*"n"(%esp), %esi # ^W[n & 15] \n\
  543. roll %esi # \n\
  544. movl %esi, 4*"n"(%esp) # store to W[n & 15] \n\
  545. movl "c", %edi # c \n\
  546. xorl "d", %edi # ^d \n\
  547. andl "b", %edi # &b \n\
  548. xorl "d", %edi # (((c ^ d) & b) ^ d) \n\
  549. leal "RCONST"("e",%esi), "e" # e += RCONST + mixed_W \n\
  550. addl %edi, "e" # e += (((c ^ d) & b) ^ d) \n\
  551. movl "a", %esi # \n\
  552. roll $5, %esi # rotl32(a,5) \n\
  553. addl %esi, "e" # e += rotl32(a,5) \n\
  554. rorl $2, "b" # b = rotl32(b,30) \n\
  555. "
  556. #define RD1A(a,b,c,d,e, n) RD1As("%e"STR(a),"%e"STR(b),"%e"STR(c),"%e"STR(d),"%e"STR(e), STR((n)), STR(RCONST))
  557. #define RD1B(a,b,c,d,e, n) RD1Bs("%e"STR(a),"%e"STR(b),"%e"STR(c),"%e"STR(d),"%e"STR(e), STR((n)), STR(RCONST))
  558. #define RD1C(a,b,c,d,e, n) RD1Cs("%e"STR(a),"%e"STR(b),"%e"STR(c),"%e"STR(d),"%e"STR(e), STR(((n+13)&15)), STR(((n+8)&15)), STR(((n+2)&15)), STR(((n)&15)), STR(RCONST))
  559. #undef RCONST
  560. #define RCONST 0x5A827999
  561. RD1A(ax,bx,cx,dx,bp, 0) RD1B(bp,ax,bx,cx,dx, 1) RD1B(dx,bp,ax,bx,cx, 2) RD1B(cx,dx,bp,ax,bx, 3) RD1B(bx,cx,dx,bp,ax, 4)
  562. RD1B(ax,bx,cx,dx,bp, 5) RD1B(bp,ax,bx,cx,dx, 6) RD1B(dx,bp,ax,bx,cx, 7) RD1B(cx,dx,bp,ax,bx, 8) RD1B(bx,cx,dx,bp,ax, 9)
  563. RD1B(ax,bx,cx,dx,bp,10) RD1B(bp,ax,bx,cx,dx,11) RD1B(dx,bp,ax,bx,cx,12) RD1B(cx,dx,bp,ax,bx,13) RD1B(bx,cx,dx,bp,ax,14)
  564. RD1B(ax,bx,cx,dx,bp,15) RD1C(bp,ax,bx,cx,dx,16) RD1C(dx,bp,ax,bx,cx,17) RD1C(cx,dx,bp,ax,bx,18) RD1C(bx,cx,dx,bp,ax,19)
  565. #define RD2s(a,b,c,d,e, n13,n8,n2,n, RCONST) \
  566. "\n\
  567. movl 4*"n13"(%esp), %esi # W[(n+13) & 15] \n\
  568. xorl 4*"n8"(%esp), %esi # ^W[(n+8) & 15] \n\
  569. xorl 4*"n2"(%esp), %esi # ^W[(n+2) & 15] \n\
  570. xorl 4*"n"(%esp), %esi # ^W[n & 15] \n\
  571. roll %esi # \n\
  572. movl %esi, 4*"n"(%esp) # store to W[n & 15] \n\
  573. movl "c", %edi # c \n\
  574. xorl "d", %edi # ^d \n\
  575. xorl "b", %edi # ^b \n\
  576. leal "RCONST"("e",%esi), "e" # e += RCONST + mixed_W \n\
  577. addl %edi, "e" # e += (c ^ d ^ b) \n\
  578. movl "a", %esi # \n\
  579. roll $5, %esi # rotl32(a,5) \n\
  580. addl %esi, "e" # e += rotl32(a,5) \n\
  581. rorl $2, "b" # b = rotl32(b,30) \n\
  582. "
  583. #define RD2(a,b,c,d,e, n) RD2s("%e"STR(a),"%e"STR(b),"%e"STR(c),"%e"STR(d),"%e"STR(e), STR(((20+n+13)&15)), STR(((20+n+8)&15)), STR(((20+n+2)&15)), STR(((20+n)&15)), STR(RCONST))
  584. #undef RCONST
  585. #define RCONST 0x6ED9EBA1
  586. RD2(ax,bx,cx,dx,bp, 0) RD2(bp,ax,bx,cx,dx, 1) RD2(dx,bp,ax,bx,cx, 2) RD2(cx,dx,bp,ax,bx, 3) RD2(bx,cx,dx,bp,ax, 4)
  587. RD2(ax,bx,cx,dx,bp, 5) RD2(bp,ax,bx,cx,dx, 6) RD2(dx,bp,ax,bx,cx, 7) RD2(cx,dx,bp,ax,bx, 8) RD2(bx,cx,dx,bp,ax, 9)
  588. RD2(ax,bx,cx,dx,bp,10) RD2(bp,ax,bx,cx,dx,11) RD2(dx,bp,ax,bx,cx,12) RD2(cx,dx,bp,ax,bx,13) RD2(bx,cx,dx,bp,ax,14)
  589. RD2(ax,bx,cx,dx,bp,15) RD2(bp,ax,bx,cx,dx,16) RD2(dx,bp,ax,bx,cx,17) RD2(cx,dx,bp,ax,bx,18) RD2(bx,cx,dx,bp,ax,19)
  590. #define RD3s(a,b,c,d,e, n13,n8,n2,n, RCONST) \
  591. "\n\
  592. movl "b", %edi # di: b \n\
  593. movl "b", %esi # si: b \n\
  594. orl "c", %edi # di: b | c \n\
  595. andl "c", %esi # si: b & c \n\
  596. andl "d", %edi # di: (b | c) & d \n\
  597. orl %esi, %edi # ((b | c) & d) | (b & c) \n\
  598. movl 4*"n13"(%esp), %esi # W[(n+13) & 15] \n\
  599. xorl 4*"n8"(%esp), %esi # ^W[(n+8) & 15] \n\
  600. xorl 4*"n2"(%esp), %esi # ^W[(n+2) & 15] \n\
  601. xorl 4*"n"(%esp), %esi # ^W[n & 15] \n\
  602. roll %esi # \n\
  603. movl %esi, 4*"n"(%esp) # store to W[n & 15] \n\
  604. addl %edi, "e" # += ((b | c) & d) | (b & c)\n\
  605. leal "RCONST"("e",%esi), "e" # e += RCONST + mixed_W \n\
  606. movl "a", %esi # \n\
  607. roll $5, %esi # rotl32(a,5) \n\
  608. addl %esi, "e" # e += rotl32(a,5) \n\
  609. rorl $2, "b" # b = rotl32(b,30) \n\
  610. "
  611. #define RD3(a,b,c,d,e, n) RD3s("%e"STR(a),"%e"STR(b),"%e"STR(c),"%e"STR(d),"%e"STR(e), STR(((40+n+13)&15)), STR(((40+n+8)&15)), STR(((40+n+2)&15)), STR(((40+n)&15)), STR(RCONST))
  612. #undef RCONST
  613. #define RCONST 0x8F1BBCDC
  614. RD3(ax,bx,cx,dx,bp, 0) RD3(bp,ax,bx,cx,dx, 1) RD3(dx,bp,ax,bx,cx, 2) RD3(cx,dx,bp,ax,bx, 3) RD3(bx,cx,dx,bp,ax, 4)
  615. RD3(ax,bx,cx,dx,bp, 5) RD3(bp,ax,bx,cx,dx, 6) RD3(dx,bp,ax,bx,cx, 7) RD3(cx,dx,bp,ax,bx, 8) RD3(bx,cx,dx,bp,ax, 9)
  616. RD3(ax,bx,cx,dx,bp,10) RD3(bp,ax,bx,cx,dx,11) RD3(dx,bp,ax,bx,cx,12) RD3(cx,dx,bp,ax,bx,13) RD3(bx,cx,dx,bp,ax,14)
  617. RD3(ax,bx,cx,dx,bp,15) RD3(bp,ax,bx,cx,dx,16) RD3(dx,bp,ax,bx,cx,17) RD3(cx,dx,bp,ax,bx,18) RD3(bx,cx,dx,bp,ax,19)
  618. #define RD4As(a,b,c,d,e, n13,n8,n2,n, RCONST) \
  619. "\n\
  620. movl 4*"n13"(%esp), %esi # W[(n+13) & 15] \n\
  621. xorl 4*"n8"(%esp), %esi # ^W[(n+8) & 15] \n\
  622. xorl 4*"n2"(%esp), %esi # ^W[(n+2) & 15] \n\
  623. xorl 4*"n"(%esp), %esi # ^W[n & 15] \n\
  624. roll %esi # \n\
  625. movl %esi, 4*"n"(%esp) # store to W[n & 15] \n\
  626. movl "c", %edi # c \n\
  627. xorl "d", %edi # ^d \n\
  628. xorl "b", %edi # ^b \n\
  629. leal "RCONST"("e",%esi), "e" # e += RCONST + mixed_W \n\
  630. addl %edi, "e" # e += (c ^ d ^ b) \n\
  631. movl "a", %esi # \n\
  632. roll $5, %esi # rotl32(a,5) \n\
  633. addl %esi, "e" # e += rotl32(a,5) \n\
  634. rorl $2, "b" # b = rotl32(b,30) \n\
  635. "
  636. #define RD4Bs(a,b,c,d,e, n13,n8,n2,n, RCONST) \
  637. "\n\
  638. movl 4*"n13"(%esp), %esi # W[(n+13) & 15] \n\
  639. xorl 4*"n8"(%esp), %esi # ^W[(n+8) & 15] \n\
  640. xorl 4*"n2"(%esp), %esi # ^W[(n+2) & 15] \n\
  641. xorl 4*"n"(%esp), %esi # ^W[n & 15] \n\
  642. roll %esi # \n\
  643. ##movl %esi, 4*"n"(%esp) # store to W[n & 15] elided \n\
  644. movl "c", %edi # c \n\
  645. xorl "d", %edi # ^d \n\
  646. xorl "b", %edi # ^b \n\
  647. leal "RCONST"("e",%esi), "e" # e += RCONST + mixed_W \n\
  648. addl %edi, "e" # e += (c ^ d ^ b) \n\
  649. movl "a", %esi # \n\
  650. roll $5, %esi # rotl32(a,5) \n\
  651. addl %esi, "e" # e += rotl32(a,5) \n\
  652. rorl $2, "b" # b = rotl32(b,30) \n\
  653. "
  654. #define RD4A(a,b,c,d,e, n) RD4As("%e"STR(a),"%e"STR(b),"%e"STR(c),"%e"STR(d),"%e"STR(e), STR(((60+n+13)&15)), STR(((60+n+8)&15)), STR(((60+n+2)&15)), STR(((60+n)&15)), STR(RCONST))
  655. #define RD4B(a,b,c,d,e, n) RD4Bs("%e"STR(a),"%e"STR(b),"%e"STR(c),"%e"STR(d),"%e"STR(e), STR(((60+n+13)&15)), STR(((60+n+8)&15)), STR(((60+n+2)&15)), STR(((60+n)&15)), STR(RCONST))
  656. #undef RCONST
  657. #define RCONST 0xCA62C1D6
  658. RD4A(ax,bx,cx,dx,bp, 0) RD4A(bp,ax,bx,cx,dx, 1) RD4A(dx,bp,ax,bx,cx, 2) RD4A(cx,dx,bp,ax,bx, 3) RD4A(bx,cx,dx,bp,ax, 4)
  659. RD4A(ax,bx,cx,dx,bp, 5) RD4A(bp,ax,bx,cx,dx, 6) RD4A(dx,bp,ax,bx,cx, 7) RD4A(cx,dx,bp,ax,bx, 8) RD4A(bx,cx,dx,bp,ax, 9)
  660. RD4A(ax,bx,cx,dx,bp,10) RD4A(bp,ax,bx,cx,dx,11) RD4A(dx,bp,ax,bx,cx,12) RD4A(cx,dx,bp,ax,bx,13) RD4A(bx,cx,dx,bp,ax,14)
  661. RD4A(ax,bx,cx,dx,bp,15) RD4A(bp,ax,bx,cx,dx,16) RD4B(dx,bp,ax,bx,cx,17) RD4B(cx,dx,bp,ax,bx,18) RD4B(bx,cx,dx,bp,ax,19)
  662. "\n\
  663. movl 4*16(%esp), %esi # \n\
  664. addl $4*(16+1), %esp # \n\
  665. addl %eax, 76(%esi) # ctx->hash[0] += a \n\
  666. addl %ebx, 80(%esi) # ctx->hash[1] += b \n\
  667. addl %ecx, 84(%esi) # ctx->hash[2] += c \n\
  668. addl %edx, 88(%esi) # ctx->hash[3] += d \n\
  669. addl %ebp, 92(%esi) # ctx->hash[4] += e \n\
  670. popl %ebx # \n\
  671. popl %esi # \n\
  672. popl %edi # \n\
  673. popl %ebp # \n\
  674. "
  675. ); /* asm */
  676. #undef RCONST
  677. }
  678. # elif defined(__GNUC__) && defined(__x86_64__)
  679. /* in hash_md5_sha_x86-64.S */
  680. struct ASM_expects_80 { char t[1 - 2*(offsetof(sha1_ctx_t, hash) != 80)]; };
  681. void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx);
  682. # else
  683. /* Fast, fully-unrolled SHA1. +3800 bytes of code on x86.
  684. * It seems further speedup can be achieved by handling more than
  685. * 64 bytes per one function call (coreutils does that).
  686. */
  687. static void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx)
  688. {
  689. static const uint32_t rconsts[] ALIGN4 = {
  690. 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6
  691. };
  692. uint32_t W[16];
  693. uint32_t a, b, c, d, e;
  694. a = ctx->hash[0];
  695. b = ctx->hash[1];
  696. c = ctx->hash[2];
  697. d = ctx->hash[3];
  698. e = ctx->hash[4];
  699. /* From kernel source comments:
  700. * """
  701. * If you have 32 registers or more, the compiler can (and should)
  702. * try to change the array[] accesses into registers. However, on
  703. * machines with less than ~25 registers, that won't really work,
  704. * and at least gcc will make an unholy mess of it.
  705. *
  706. * So to avoid that mess which just slows things down, we force
  707. * the stores to memory to actually happen (we might be better off
  708. * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
  709. * suggested by Artur Skawina - that will also make gcc unable to
  710. * try to do the silly "optimize away loads" part because it won't
  711. * see what the value will be).
  712. * """
  713. */
  714. #if defined(__GNUC__) && defined(__i386__)
  715. # define DO_NOT_TRY_PROPAGATING(m) asm("":"+m"(m))
  716. #else
  717. # define DO_NOT_TRY_PROPAGATING(m) ((void)0)
  718. #endif
  719. #undef OP
  720. #define OP(A,B,C,D,E, n) \
  721. do { \
  722. uint32_t work = EXPR(B, C, D); \
  723. if (n <= 15) \
  724. work += W[n & 15] = SWAP_BE32(((uint32_t*)ctx->wbuffer)[n]); \
  725. if (n >= 16) \
  726. work += W[n & 15] = rotl32(W[(n+13) & 15] ^ W[(n+8) & 15] ^ W[(n+2) & 15] ^ W[n & 15], 1); \
  727. DO_NOT_TRY_PROPAGATING(W[n & 15]); \
  728. E += work + rotl32(A, 5) + rconsts[n / 20]; \
  729. B = rotl32(B, 30); \
  730. } while (0)
  731. #define OP20(n) \
  732. OP(a,b,c,d,e, (n+ 0)); OP(e,a,b,c,d, (n+ 1)); OP(d,e,a,b,c, (n+ 2)); OP(c,d,e,a,b, (n+ 3)); OP(b,c,d,e,a, (n+ 4)); \
  733. OP(a,b,c,d,e, (n+ 5)); OP(e,a,b,c,d, (n+ 6)); OP(d,e,a,b,c, (n+ 7)); OP(c,d,e,a,b, (n+ 8)); OP(b,c,d,e,a, (n+ 9)); \
  734. OP(a,b,c,d,e, (n+10)); OP(e,a,b,c,d, (n+11)); OP(d,e,a,b,c, (n+12)); OP(c,d,e,a,b, (n+13)); OP(b,c,d,e,a, (n+14)); \
  735. OP(a,b,c,d,e, (n+15)); OP(e,a,b,c,d, (n+16)); OP(d,e,a,b,c, (n+17)); OP(c,d,e,a,b, (n+18)); OP(b,c,d,e,a, (n+19))
  736. /* 4 rounds of 20 operations each */
  737. #define EXPR(b,c,d) (((c ^ d) & b) ^ d)
  738. OP20(0);
  739. #undef EXPR
  740. #define EXPR(b,c,d) (c ^ d ^ b)
  741. OP20(20);
  742. #undef EXPR
  743. #define EXPR(b,c,d) (((b | c) & d) | (b & c))
  744. OP20(40);
  745. #undef EXPR
  746. #define EXPR(b,c,d) (c ^ d ^ b)
  747. OP20(60);
  748. #undef EXPR
  749. #undef OP
  750. #undef OP20
  751. ctx->hash[0] += a;
  752. ctx->hash[1] += b;
  753. ctx->hash[2] += c;
  754. ctx->hash[3] += d;
  755. ctx->hash[4] += e;
  756. }
  757. # endif
  758. #elif CONFIG_SHA1_SMALL == 1
  759. /* Middle-sized version, +300 bytes of code on x86. */
  760. static void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx)
  761. {
  762. static const uint32_t rconsts[] ALIGN4 = {
  763. 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6
  764. };
  765. int j;
  766. int n;
  767. uint32_t W[16+16];
  768. uint32_t a, b, c, d, e;
  769. a = ctx->hash[0];
  770. b = ctx->hash[1];
  771. c = ctx->hash[2];
  772. d = ctx->hash[3];
  773. e = ctx->hash[4];
  774. /* 1st round of 20 operations */
  775. n = 0;
  776. do {
  777. uint32_t work = ((c ^ d) & b) ^ d;
  778. W[n] = W[n+16] = SWAP_BE32(((uint32_t*)ctx->wbuffer)[n]);
  779. work += W[n];
  780. work += e + rotl32(a, 5) + rconsts[0];
  781. /* Rotate by one for next time */
  782. e = d;
  783. d = c;
  784. c = rotl32(b, 30);
  785. b = a;
  786. a = work;
  787. n = (n + 1) & 15;
  788. } while (n != 0);
  789. do {
  790. uint32_t work = ((c ^ d) & b) ^ d;
  791. W[n] = W[n+16] = rotl32(W[n+13] ^ W[n+8] ^ W[n+2] ^ W[n], 1);
  792. work += W[n];
  793. work += e + rotl32(a, 5) + rconsts[0];
  794. e = d;
  795. d = c;
  796. c = rotl32(b, 30);
  797. b = a;
  798. a = work;
  799. n = (n + 1) /* & 15*/;
  800. } while (n != 4);
  801. /* 2nd round of 20 operations */
  802. j = 19;
  803. do {
  804. uint32_t work = c ^ d ^ b;
  805. W[n] = W[n+16] = rotl32(W[n+13] ^ W[n+8] ^ W[n+2] ^ W[n], 1);
  806. work += W[n];
  807. work += e + rotl32(a, 5) + rconsts[1];
  808. e = d;
  809. d = c;
  810. c = rotl32(b, 30);
  811. b = a;
  812. a = work;
  813. n = (n + 1) & 15;
  814. } while (--j >= 0);
  815. /* 3rd round */
  816. j = 19;
  817. do {
  818. uint32_t work = ((b | c) & d) | (b & c);
  819. W[n] = W[n+16] = rotl32(W[n+13] ^ W[n+8] ^ W[n+2] ^ W[n], 1);
  820. work += W[n];
  821. work += e + rotl32(a, 5) + rconsts[2];
  822. e = d;
  823. d = c;
  824. c = rotl32(b, 30);
  825. b = a;
  826. a = work;
  827. n = (n + 1) & 15;
  828. } while (--j >= 0);
  829. /* 4th round */
  830. j = 19;
  831. do {
  832. uint32_t work = c ^ d ^ b;
  833. W[n] = W[n+16] = rotl32(W[n+13] ^ W[n+8] ^ W[n+2] ^ W[n], 1);
  834. work += W[n];
  835. work += e + rotl32(a, 5) + rconsts[3];
  836. e = d;
  837. d = c;
  838. c = rotl32(b, 30);
  839. b = a;
  840. a = work;
  841. n = (n + 1) & 15;
  842. } while (--j >= 0);
  843. ctx->hash[0] += a;
  844. ctx->hash[1] += b;
  845. ctx->hash[2] += c;
  846. ctx->hash[3] += d;
  847. ctx->hash[4] += e;
  848. }
  849. #else
  850. /* Compact version, almost twice as slow as fully unrolled */
  851. static void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx)
  852. {
  853. static const uint32_t rconsts[] ALIGN4 = {
  854. 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6
  855. };
  856. int i, j;
  857. int n;
  858. uint32_t W[16+16];
  859. uint32_t a, b, c, d, e;
  860. /* On-stack work buffer frees up one register in the main loop
  861. * which otherwise will be needed to hold ctx pointer.
  862. *
  863. * The compiler is not smart enough to realize it, though. :(
  864. * If __attribute__((optimize("2"))) is added to the function,
  865. * only then gcc-9.3.1 spills "ctx" to stack and uses the freed
  866. * register (making code 6 bytes smaller, not just faster).
  867. */
  868. for (i = 0; i < 16; i++)
  869. W[i] = W[i+16] = SWAP_BE32(((uint32_t*)ctx->wbuffer)[i]);
  870. a = ctx->hash[0];
  871. b = ctx->hash[1];
  872. c = ctx->hash[2];
  873. d = ctx->hash[3];
  874. e = ctx->hash[4];
  875. /* 4 rounds of 20 operations each */
  876. n = 0;
  877. for (i = 0; i < 4; i++) {
  878. j = 19;
  879. do {
  880. uint32_t work;
  881. work = c ^ d;
  882. if (i == 0) {
  883. work = (work & b) ^ d;
  884. if (j <= 3)
  885. goto ge16;
  886. } else {
  887. if (i == 2)
  888. work = ((b | c) & d) | (b & c);
  889. else /* i = 1 or 3 */
  890. work ^= b;
  891. ge16:
  892. W[n] = W[n+16] = rotl32(W[n+13] ^ W[n+8] ^ W[n+2] ^ W[n], 1);
  893. }
  894. work += W[n];
  895. work += e + rotl32(a, 5) + rconsts[i];
  896. /* Rotate by one for next time */
  897. e = d;
  898. d = c;
  899. c = rotl32(b, 30);
  900. b = a;
  901. a = work;
  902. n = (n + 1) & 15;
  903. } while (--j >= 0);
  904. }
  905. ctx->hash[0] += a;
  906. ctx->hash[1] += b;
  907. ctx->hash[2] += c;
  908. ctx->hash[3] += d;
  909. ctx->hash[4] += e;
  910. }
  911. #endif
  912. /* Constants for SHA512 from FIPS 180-2:4.2.3.
  913. * SHA256 constants from FIPS 180-2:4.2.2
  914. * are the most significant half of first 64 elements
  915. * of the same array.
  916. */
  917. #undef K
  918. #if NEED_SHA512
  919. typedef uint64_t sha_K_int;
  920. # define K(v) v
  921. #else
  922. typedef uint32_t sha_K_int;
  923. # define K(v) (uint32_t)(v >> 32)
  924. #endif
  925. static const sha_K_int sha_K[] ALIGN8 = {
  926. K(0x428a2f98d728ae22ULL), K(0x7137449123ef65cdULL),
  927. K(0xb5c0fbcfec4d3b2fULL), K(0xe9b5dba58189dbbcULL),
  928. K(0x3956c25bf348b538ULL), K(0x59f111f1b605d019ULL),
  929. K(0x923f82a4af194f9bULL), K(0xab1c5ed5da6d8118ULL),
  930. K(0xd807aa98a3030242ULL), K(0x12835b0145706fbeULL),
  931. K(0x243185be4ee4b28cULL), K(0x550c7dc3d5ffb4e2ULL),
  932. K(0x72be5d74f27b896fULL), K(0x80deb1fe3b1696b1ULL),
  933. K(0x9bdc06a725c71235ULL), K(0xc19bf174cf692694ULL),
  934. K(0xe49b69c19ef14ad2ULL), K(0xefbe4786384f25e3ULL),
  935. K(0x0fc19dc68b8cd5b5ULL), K(0x240ca1cc77ac9c65ULL),
  936. K(0x2de92c6f592b0275ULL), K(0x4a7484aa6ea6e483ULL),
  937. K(0x5cb0a9dcbd41fbd4ULL), K(0x76f988da831153b5ULL),
  938. K(0x983e5152ee66dfabULL), K(0xa831c66d2db43210ULL),
  939. K(0xb00327c898fb213fULL), K(0xbf597fc7beef0ee4ULL),
  940. K(0xc6e00bf33da88fc2ULL), K(0xd5a79147930aa725ULL),
  941. K(0x06ca6351e003826fULL), K(0x142929670a0e6e70ULL),
  942. K(0x27b70a8546d22ffcULL), K(0x2e1b21385c26c926ULL),
  943. K(0x4d2c6dfc5ac42aedULL), K(0x53380d139d95b3dfULL),
  944. K(0x650a73548baf63deULL), K(0x766a0abb3c77b2a8ULL),
  945. K(0x81c2c92e47edaee6ULL), K(0x92722c851482353bULL),
  946. K(0xa2bfe8a14cf10364ULL), K(0xa81a664bbc423001ULL),
  947. K(0xc24b8b70d0f89791ULL), K(0xc76c51a30654be30ULL),
  948. K(0xd192e819d6ef5218ULL), K(0xd69906245565a910ULL),
  949. K(0xf40e35855771202aULL), K(0x106aa07032bbd1b8ULL),
  950. K(0x19a4c116b8d2d0c8ULL), K(0x1e376c085141ab53ULL),
  951. K(0x2748774cdf8eeb99ULL), K(0x34b0bcb5e19b48a8ULL),
  952. K(0x391c0cb3c5c95a63ULL), K(0x4ed8aa4ae3418acbULL),
  953. K(0x5b9cca4f7763e373ULL), K(0x682e6ff3d6b2b8a3ULL),
  954. K(0x748f82ee5defb2fcULL), K(0x78a5636f43172f60ULL),
  955. K(0x84c87814a1f0ab72ULL), K(0x8cc702081a6439ecULL),
  956. K(0x90befffa23631e28ULL), K(0xa4506cebde82bde9ULL),
  957. K(0xbef9a3f7b2c67915ULL), K(0xc67178f2e372532bULL),
  958. #if NEED_SHA512 /* [64]+ are used for sha512 only */
  959. K(0xca273eceea26619cULL), K(0xd186b8c721c0c207ULL),
  960. K(0xeada7dd6cde0eb1eULL), K(0xf57d4f7fee6ed178ULL),
  961. K(0x06f067aa72176fbaULL), K(0x0a637dc5a2c898a6ULL),
  962. K(0x113f9804bef90daeULL), K(0x1b710b35131c471bULL),
  963. K(0x28db77f523047d84ULL), K(0x32caab7b40c72493ULL),
  964. K(0x3c9ebe0a15c9bebcULL), K(0x431d67c49c100d4cULL),
  965. K(0x4cc5d4becb3e42b6ULL), K(0x597f299cfc657e2aULL),
  966. K(0x5fcb6fab3ad6faecULL), K(0x6c44198c4a475817ULL),
  967. #endif
  968. };
  969. #undef K
  970. #undef Ch
  971. #undef Maj
  972. #undef S0
  973. #undef S1
  974. #undef R0
  975. #undef R1
  976. static void FAST_FUNC sha256_process_block64(sha256_ctx_t *ctx)
  977. {
  978. unsigned t;
  979. uint32_t W[64], a, b, c, d, e, f, g, h;
  980. const uint32_t *words = (uint32_t*) ctx->wbuffer;
  981. /* Operators defined in FIPS 180-2:4.1.2. */
  982. #define Ch(x, y, z) ((x & y) ^ (~x & z))
  983. #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
  984. #define S0(x) (rotr32(x, 2) ^ rotr32(x, 13) ^ rotr32(x, 22))
  985. #define S1(x) (rotr32(x, 6) ^ rotr32(x, 11) ^ rotr32(x, 25))
  986. #define R0(x) (rotr32(x, 7) ^ rotr32(x, 18) ^ (x >> 3))
  987. #define R1(x) (rotr32(x, 17) ^ rotr32(x, 19) ^ (x >> 10))
  988. /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */
  989. for (t = 0; t < 16; ++t)
  990. W[t] = SWAP_BE32(words[t]);
  991. for (/*t = 16*/; t < 64; ++t)
  992. W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16];
  993. a = ctx->hash[0];
  994. b = ctx->hash[1];
  995. c = ctx->hash[2];
  996. d = ctx->hash[3];
  997. e = ctx->hash[4];
  998. f = ctx->hash[5];
  999. g = ctx->hash[6];
  1000. h = ctx->hash[7];
  1001. /* The actual computation according to FIPS 180-2:6.2.2 step 3. */
  1002. for (t = 0; t < 64; ++t) {
  1003. /* Need to fetch upper half of sha_K[t]
  1004. * (I hope compiler is clever enough to just fetch
  1005. * upper half)
  1006. */
  1007. uint32_t K_t = NEED_SHA512 ? (sha_K[t] >> 32) : sha_K[t];
  1008. uint32_t T1 = h + S1(e) + Ch(e, f, g) + K_t + W[t];
  1009. uint32_t T2 = S0(a) + Maj(a, b, c);
  1010. h = g;
  1011. g = f;
  1012. f = e;
  1013. e = d + T1;
  1014. d = c;
  1015. c = b;
  1016. b = a;
  1017. a = T1 + T2;
  1018. }
  1019. #undef Ch
  1020. #undef Maj
  1021. #undef S0
  1022. #undef S1
  1023. #undef R0
  1024. #undef R1
  1025. /* Add the starting values of the context according to FIPS 180-2:6.2.2
  1026. step 4. */
  1027. ctx->hash[0] += a;
  1028. ctx->hash[1] += b;
  1029. ctx->hash[2] += c;
  1030. ctx->hash[3] += d;
  1031. ctx->hash[4] += e;
  1032. ctx->hash[5] += f;
  1033. ctx->hash[6] += g;
  1034. ctx->hash[7] += h;
  1035. }
  1036. #if NEED_SHA512
  1037. static void FAST_FUNC sha512_process_block128(sha512_ctx_t *ctx)
  1038. {
  1039. unsigned t;
  1040. uint64_t W[80];
  1041. /* On i386, having assignments here (not later as sha256 does)
  1042. * produces 99 bytes smaller code with gcc 4.3.1
  1043. */
  1044. uint64_t a = ctx->hash[0];
  1045. uint64_t b = ctx->hash[1];
  1046. uint64_t c = ctx->hash[2];
  1047. uint64_t d = ctx->hash[3];
  1048. uint64_t e = ctx->hash[4];
  1049. uint64_t f = ctx->hash[5];
  1050. uint64_t g = ctx->hash[6];
  1051. uint64_t h = ctx->hash[7];
  1052. const uint64_t *words = (uint64_t*) ctx->wbuffer;
  1053. /* Operators defined in FIPS 180-2:4.1.2. */
  1054. #define Ch(x, y, z) ((x & y) ^ (~x & z))
  1055. #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
  1056. #define S0(x) (rotr64(x, 28) ^ rotr64(x, 34) ^ rotr64(x, 39))
  1057. #define S1(x) (rotr64(x, 14) ^ rotr64(x, 18) ^ rotr64(x, 41))
  1058. #define R0(x) (rotr64(x, 1) ^ rotr64(x, 8) ^ (x >> 7))
  1059. #define R1(x) (rotr64(x, 19) ^ rotr64(x, 61) ^ (x >> 6))
  1060. /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */
  1061. for (t = 0; t < 16; ++t)
  1062. W[t] = SWAP_BE64(words[t]);
  1063. for (/*t = 16*/; t < 80; ++t)
  1064. W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16];
  1065. /* The actual computation according to FIPS 180-2:6.3.2 step 3. */
  1066. for (t = 0; t < 80; ++t) {
  1067. uint64_t T1 = h + S1(e) + Ch(e, f, g) + sha_K[t] + W[t];
  1068. uint64_t T2 = S0(a) + Maj(a, b, c);
  1069. h = g;
  1070. g = f;
  1071. f = e;
  1072. e = d + T1;
  1073. d = c;
  1074. c = b;
  1075. b = a;
  1076. a = T1 + T2;
  1077. }
  1078. #undef Ch
  1079. #undef Maj
  1080. #undef S0
  1081. #undef S1
  1082. #undef R0
  1083. #undef R1
  1084. /* Add the starting values of the context according to FIPS 180-2:6.3.2
  1085. step 4. */
  1086. ctx->hash[0] += a;
  1087. ctx->hash[1] += b;
  1088. ctx->hash[2] += c;
  1089. ctx->hash[3] += d;
  1090. ctx->hash[4] += e;
  1091. ctx->hash[5] += f;
  1092. ctx->hash[6] += g;
  1093. ctx->hash[7] += h;
  1094. }
  1095. #endif /* NEED_SHA512 */
  1096. void FAST_FUNC sha1_begin(sha1_ctx_t *ctx)
  1097. {
  1098. ctx->hash[0] = 0x67452301;
  1099. ctx->hash[1] = 0xefcdab89;
  1100. ctx->hash[2] = 0x98badcfe;
  1101. ctx->hash[3] = 0x10325476;
  1102. ctx->hash[4] = 0xc3d2e1f0;
  1103. ctx->total64 = 0;
  1104. ctx->process_block = sha1_process_block64;
  1105. #if ENABLE_SHA1_HWACCEL
  1106. # if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  1107. {
  1108. if (!shaNI) {
  1109. unsigned eax = 7, ebx = ebx, ecx = 0, edx = edx;
  1110. cpuid(&eax, &ebx, &ecx, &edx);
  1111. shaNI = ((ebx >> 28) & 2) - 1; /* bit 29 -> 1 or -1 */
  1112. }
  1113. if (shaNI > 0)
  1114. ctx->process_block = sha1_process_block64_shaNI;
  1115. }
  1116. # endif
  1117. #endif
  1118. }
  1119. static const uint32_t init256[] ALIGN4 = {
  1120. 0,
  1121. 0,
  1122. 0x6a09e667,
  1123. 0xbb67ae85,
  1124. 0x3c6ef372,
  1125. 0xa54ff53a,
  1126. 0x510e527f,
  1127. 0x9b05688c,
  1128. 0x1f83d9ab,
  1129. 0x5be0cd19,
  1130. };
  1131. #if NEED_SHA512
  1132. static const uint32_t init512_lo[] ALIGN4 = {
  1133. 0,
  1134. 0,
  1135. 0xf3bcc908,
  1136. 0x84caa73b,
  1137. 0xfe94f82b,
  1138. 0x5f1d36f1,
  1139. 0xade682d1,
  1140. 0x2b3e6c1f,
  1141. 0xfb41bd6b,
  1142. 0x137e2179,
  1143. };
  1144. #endif /* NEED_SHA512 */
  1145. // Note: SHA-384 is identical to SHA-512, except that initial hash values are
  1146. // 0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
  1147. // 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4,
  1148. // and the output is constructed by omitting last two 64-bit words of it.
  1149. /* Initialize structure containing state of computation.
  1150. (FIPS 180-2:5.3.2) */
  1151. void FAST_FUNC sha256_begin(sha256_ctx_t *ctx)
  1152. {
  1153. memcpy(&ctx->total64, init256, sizeof(init256));
  1154. /*ctx->total64 = 0; - done by prepending two 32-bit zeros to init256 */
  1155. ctx->process_block = sha256_process_block64;
  1156. #if ENABLE_SHA256_HWACCEL
  1157. # if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  1158. {
  1159. if (!shaNI) {
  1160. unsigned eax = 7, ebx = ebx, ecx = 0, edx = edx;
  1161. cpuid(&eax, &ebx, &ecx, &edx);
  1162. shaNI = ((ebx >> 28) & 2) - 1; /* bit 29 -> 1 or -1 */
  1163. }
  1164. if (shaNI > 0)
  1165. ctx->process_block = sha256_process_block64_shaNI;
  1166. }
  1167. # endif
  1168. #endif
  1169. }
  1170. #if NEED_SHA512
  1171. /* Initialize structure containing state of computation.
  1172. (FIPS 180-2:5.3.3) */
  1173. void FAST_FUNC sha512_begin(sha512_ctx_t *ctx)
  1174. {
  1175. int i;
  1176. /* Two extra iterations zero out ctx->total64[2] */
  1177. uint64_t *tp = ctx->total64;
  1178. for (i = 0; i < 8 + 2; i++)
  1179. tp[i] = ((uint64_t)(init256[i]) << 32) + init512_lo[i];
  1180. /*ctx->total64[0] = ctx->total64[1] = 0; - already done */
  1181. }
  1182. void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len)
  1183. {
  1184. unsigned bufpos = ctx->total64[0] & 127;
  1185. unsigned remaining;
  1186. /* First increment the byte count. FIPS 180-2 specifies the possible
  1187. length of the file up to 2^128 _bits_.
  1188. We compute the number of _bytes_ and convert to bits later. */
  1189. ctx->total64[0] += len;
  1190. if (ctx->total64[0] < len)
  1191. ctx->total64[1]++;
  1192. while (1) {
  1193. remaining = 128 - bufpos;
  1194. if (remaining > len)
  1195. remaining = len;
  1196. /* Copy data into aligned buffer */
  1197. memcpy(ctx->wbuffer + bufpos, buffer, remaining);
  1198. len -= remaining;
  1199. buffer = (const char *)buffer + remaining;
  1200. bufpos += remaining;
  1201. /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
  1202. bufpos -= 128;
  1203. if (bufpos != 0)
  1204. break;
  1205. /* Buffer is filled up, process it */
  1206. sha512_process_block128(ctx);
  1207. /*bufpos = 0; - already is */
  1208. }
  1209. }
  1210. #endif /* NEED_SHA512 */
  1211. /* Used also for sha256 */
  1212. unsigned FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf)
  1213. {
  1214. unsigned hash_size;
  1215. /* SHA stores total in BE, need to swap on LE arches: */
  1216. common64_end(ctx, /*swap_needed:*/ BB_LITTLE_ENDIAN);
  1217. hash_size = (ctx->process_block == sha1_process_block64) ? 5 : 8;
  1218. /* This way we do not impose alignment constraints on resbuf: */
  1219. if (BB_LITTLE_ENDIAN) {
  1220. unsigned i;
  1221. for (i = 0; i < hash_size; ++i)
  1222. ctx->hash[i] = SWAP_BE32(ctx->hash[i]);
  1223. }
  1224. hash_size *= sizeof(ctx->hash[0]);
  1225. memcpy(resbuf, ctx->hash, hash_size);
  1226. return hash_size;
  1227. }
  1228. #if NEED_SHA512
  1229. unsigned FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
  1230. {
  1231. unsigned bufpos = ctx->total64[0] & 127;
  1232. /* Pad the buffer to the next 128-byte boundary with 0x80,0,0,0... */
  1233. ctx->wbuffer[bufpos++] = 0x80;
  1234. while (1) {
  1235. unsigned remaining = 128 - bufpos;
  1236. memset(ctx->wbuffer + bufpos, 0, remaining);
  1237. if (remaining >= 16) {
  1238. /* Store the 128-bit counter of bits in the buffer in BE format */
  1239. uint64_t t;
  1240. t = ctx->total64[0] << 3;
  1241. t = SWAP_BE64(t);
  1242. *(bb__aliased_uint64_t *) (&ctx->wbuffer[128 - 8]) = t;
  1243. t = (ctx->total64[1] << 3) | (ctx->total64[0] >> 61);
  1244. t = SWAP_BE64(t);
  1245. *(bb__aliased_uint64_t *) (&ctx->wbuffer[128 - 16]) = t;
  1246. }
  1247. sha512_process_block128(ctx);
  1248. if (remaining >= 16)
  1249. break;
  1250. bufpos = 0;
  1251. }
  1252. if (BB_LITTLE_ENDIAN) {
  1253. unsigned i;
  1254. for (i = 0; i < ARRAY_SIZE(ctx->hash); ++i)
  1255. ctx->hash[i] = SWAP_BE64(ctx->hash[i]);
  1256. }
  1257. memcpy(resbuf, ctx->hash, sizeof(ctx->hash));
  1258. return sizeof(ctx->hash);
  1259. }
  1260. #endif /* NEED_SHA512 */
  1261. /*
  1262. * The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
  1263. * Michael Peeters and Gilles Van Assche. For more information, feedback or
  1264. * questions, please refer to our website: http://keccak.noekeon.org/
  1265. *
  1266. * Implementation by Ronny Van Keer,
  1267. * hereby denoted as "the implementer".
  1268. *
  1269. * To the extent possible under law, the implementer has waived all copyright
  1270. * and related or neighboring rights to the source code in this file.
  1271. * http://creativecommons.org/publicdomain/zero/1.0/
  1272. *
  1273. * Busybox modifications (C) Lauri Kasanen, under the GPLv2.
  1274. */
  1275. #if CONFIG_SHA3_SMALL < 0
  1276. # define SHA3_SMALL 0
  1277. #elif CONFIG_SHA3_SMALL > 1
  1278. # define SHA3_SMALL 1
  1279. #else
  1280. # define SHA3_SMALL CONFIG_SHA3_SMALL
  1281. #endif
  1282. #define OPTIMIZE_SHA3_FOR_32 0
  1283. /*
  1284. * SHA3 can be optimized for 32-bit CPUs with bit-slicing:
  1285. * every 64-bit word of state[] can be split into two 32-bit words
  1286. * by even/odd bits. In this form, all rotations of sha3 round
  1287. * are 32-bit - and there are lots of them.
  1288. * However, it requires either splitting/combining state words
  1289. * before/after sha3 round (code does this now)
  1290. * or shuffling bits before xor'ing them into state and in sha3_end.
  1291. * Without shuffling, bit-slicing results in -130 bytes of code
  1292. * and marginal speedup (but of course it gives wrong result).
  1293. * With shuffling it works, but +260 code bytes, and slower.
  1294. * Disabled for now:
  1295. */
  1296. #if 0 /* LONG_MAX == 0x7fffffff */
  1297. # undef OPTIMIZE_SHA3_FOR_32
  1298. # define OPTIMIZE_SHA3_FOR_32 1
  1299. #endif
  1300. #if OPTIMIZE_SHA3_FOR_32
  1301. /* This splits every 64-bit word into a pair of 32-bit words,
  1302. * even bits go into first word, odd bits go to second one.
  1303. * The conversion is done in-place.
  1304. */
  1305. static void split_halves(uint64_t *state)
  1306. {
  1307. /* Credit: Henry S. Warren, Hacker's Delight, Addison-Wesley, 2002 */
  1308. uint32_t *s32 = (uint32_t*)state;
  1309. uint32_t t, x0, x1;
  1310. int i;
  1311. for (i = 24; i >= 0; --i) {
  1312. x0 = s32[0];
  1313. t = (x0 ^ (x0 >> 1)) & 0x22222222; x0 = x0 ^ t ^ (t << 1);
  1314. t = (x0 ^ (x0 >> 2)) & 0x0C0C0C0C; x0 = x0 ^ t ^ (t << 2);
  1315. t = (x0 ^ (x0 >> 4)) & 0x00F000F0; x0 = x0 ^ t ^ (t << 4);
  1316. t = (x0 ^ (x0 >> 8)) & 0x0000FF00; x0 = x0 ^ t ^ (t << 8);
  1317. x1 = s32[1];
  1318. t = (x1 ^ (x1 >> 1)) & 0x22222222; x1 = x1 ^ t ^ (t << 1);
  1319. t = (x1 ^ (x1 >> 2)) & 0x0C0C0C0C; x1 = x1 ^ t ^ (t << 2);
  1320. t = (x1 ^ (x1 >> 4)) & 0x00F000F0; x1 = x1 ^ t ^ (t << 4);
  1321. t = (x1 ^ (x1 >> 8)) & 0x0000FF00; x1 = x1 ^ t ^ (t << 8);
  1322. *s32++ = (x0 & 0x0000FFFF) | (x1 << 16);
  1323. *s32++ = (x0 >> 16) | (x1 & 0xFFFF0000);
  1324. }
  1325. }
  1326. /* The reverse operation */
  1327. static void combine_halves(uint64_t *state)
  1328. {
  1329. uint32_t *s32 = (uint32_t*)state;
  1330. uint32_t t, x0, x1;
  1331. int i;
  1332. for (i = 24; i >= 0; --i) {
  1333. x0 = s32[0];
  1334. x1 = s32[1];
  1335. t = (x0 & 0x0000FFFF) | (x1 << 16);
  1336. x1 = (x0 >> 16) | (x1 & 0xFFFF0000);
  1337. x0 = t;
  1338. t = (x0 ^ (x0 >> 8)) & 0x0000FF00; x0 = x0 ^ t ^ (t << 8);
  1339. t = (x0 ^ (x0 >> 4)) & 0x00F000F0; x0 = x0 ^ t ^ (t << 4);
  1340. t = (x0 ^ (x0 >> 2)) & 0x0C0C0C0C; x0 = x0 ^ t ^ (t << 2);
  1341. t = (x0 ^ (x0 >> 1)) & 0x22222222; x0 = x0 ^ t ^ (t << 1);
  1342. *s32++ = x0;
  1343. t = (x1 ^ (x1 >> 8)) & 0x0000FF00; x1 = x1 ^ t ^ (t << 8);
  1344. t = (x1 ^ (x1 >> 4)) & 0x00F000F0; x1 = x1 ^ t ^ (t << 4);
  1345. t = (x1 ^ (x1 >> 2)) & 0x0C0C0C0C; x1 = x1 ^ t ^ (t << 2);
  1346. t = (x1 ^ (x1 >> 1)) & 0x22222222; x1 = x1 ^ t ^ (t << 1);
  1347. *s32++ = x1;
  1348. }
  1349. }
  1350. #endif
  1351. /*
  1352. * In the crypto literature this function is usually called Keccak-f().
  1353. */
  1354. static void sha3_process_block72(uint64_t *state)
  1355. {
  1356. enum { NROUNDS = 24 };
  1357. #if OPTIMIZE_SHA3_FOR_32
  1358. /*
  1359. static const uint32_t IOTA_CONST_0[NROUNDS] ALIGN4 = {
  1360. 0x00000001UL,
  1361. 0x00000000UL,
  1362. 0x00000000UL,
  1363. 0x00000000UL,
  1364. 0x00000001UL,
  1365. 0x00000001UL,
  1366. 0x00000001UL,
  1367. 0x00000001UL,
  1368. 0x00000000UL,
  1369. 0x00000000UL,
  1370. 0x00000001UL,
  1371. 0x00000000UL,
  1372. 0x00000001UL,
  1373. 0x00000001UL,
  1374. 0x00000001UL,
  1375. 0x00000001UL,
  1376. 0x00000000UL,
  1377. 0x00000000UL,
  1378. 0x00000000UL,
  1379. 0x00000000UL,
  1380. 0x00000001UL,
  1381. 0x00000000UL,
  1382. 0x00000001UL,
  1383. 0x00000000UL,
  1384. };
  1385. ** bits are in lsb: 0101 0000 1111 0100 1111 0001
  1386. */
  1387. uint32_t IOTA_CONST_0bits = (uint32_t)(0x0050f4f1);
  1388. static const uint32_t IOTA_CONST_1[NROUNDS] ALIGN4 = {
  1389. 0x00000000UL,
  1390. 0x00000089UL,
  1391. 0x8000008bUL,
  1392. 0x80008080UL,
  1393. 0x0000008bUL,
  1394. 0x00008000UL,
  1395. 0x80008088UL,
  1396. 0x80000082UL,
  1397. 0x0000000bUL,
  1398. 0x0000000aUL,
  1399. 0x00008082UL,
  1400. 0x00008003UL,
  1401. 0x0000808bUL,
  1402. 0x8000000bUL,
  1403. 0x8000008aUL,
  1404. 0x80000081UL,
  1405. 0x80000081UL,
  1406. 0x80000008UL,
  1407. 0x00000083UL,
  1408. 0x80008003UL,
  1409. 0x80008088UL,
  1410. 0x80000088UL,
  1411. 0x00008000UL,
  1412. 0x80008082UL,
  1413. };
  1414. uint32_t *const s32 = (uint32_t*)state;
  1415. unsigned round;
  1416. split_halves(state);
  1417. for (round = 0; round < NROUNDS; round++) {
  1418. unsigned x;
  1419. /* Theta */
  1420. {
  1421. uint32_t BC[20];
  1422. for (x = 0; x < 10; ++x) {
  1423. BC[x+10] = BC[x] = s32[x]^s32[x+10]^s32[x+20]^s32[x+30]^s32[x+40];
  1424. }
  1425. for (x = 0; x < 10; x += 2) {
  1426. uint32_t ta, tb;
  1427. ta = BC[x+8] ^ rotl32(BC[x+3], 1);
  1428. tb = BC[x+9] ^ BC[x+2];
  1429. s32[x+0] ^= ta;
  1430. s32[x+1] ^= tb;
  1431. s32[x+10] ^= ta;
  1432. s32[x+11] ^= tb;
  1433. s32[x+20] ^= ta;
  1434. s32[x+21] ^= tb;
  1435. s32[x+30] ^= ta;
  1436. s32[x+31] ^= tb;
  1437. s32[x+40] ^= ta;
  1438. s32[x+41] ^= tb;
  1439. }
  1440. }
  1441. /* RhoPi */
  1442. {
  1443. uint32_t t0a,t0b, t1a,t1b;
  1444. t1a = s32[1*2+0];
  1445. t1b = s32[1*2+1];
  1446. #define RhoPi(PI_LANE, ROT_CONST) \
  1447. t0a = s32[PI_LANE*2+0];\
  1448. t0b = s32[PI_LANE*2+1];\
  1449. if (ROT_CONST & 1) {\
  1450. s32[PI_LANE*2+0] = rotl32(t1b, ROT_CONST/2+1);\
  1451. s32[PI_LANE*2+1] = ROT_CONST == 1 ? t1a : rotl32(t1a, ROT_CONST/2+0);\
  1452. } else {\
  1453. s32[PI_LANE*2+0] = rotl32(t1a, ROT_CONST/2);\
  1454. s32[PI_LANE*2+1] = rotl32(t1b, ROT_CONST/2);\
  1455. }\
  1456. t1a = t0a; t1b = t0b;
  1457. RhoPi(10, 1)
  1458. RhoPi( 7, 3)
  1459. RhoPi(11, 6)
  1460. RhoPi(17,10)
  1461. RhoPi(18,15)
  1462. RhoPi( 3,21)
  1463. RhoPi( 5,28)
  1464. RhoPi(16,36)
  1465. RhoPi( 8,45)
  1466. RhoPi(21,55)
  1467. RhoPi(24, 2)
  1468. RhoPi( 4,14)
  1469. RhoPi(15,27)
  1470. RhoPi(23,41)
  1471. RhoPi(19,56)
  1472. RhoPi(13, 8)
  1473. RhoPi(12,25)
  1474. RhoPi( 2,43)
  1475. RhoPi(20,62)
  1476. RhoPi(14,18)
  1477. RhoPi(22,39)
  1478. RhoPi( 9,61)
  1479. RhoPi( 6,20)
  1480. RhoPi( 1,44)
  1481. #undef RhoPi
  1482. }
  1483. /* Chi */
  1484. for (x = 0; x <= 40;) {
  1485. uint32_t BC0, BC1, BC2, BC3, BC4;
  1486. BC0 = s32[x + 0*2];
  1487. BC1 = s32[x + 1*2];
  1488. BC2 = s32[x + 2*2];
  1489. s32[x + 0*2] = BC0 ^ ((~BC1) & BC2);
  1490. BC3 = s32[x + 3*2];
  1491. s32[x + 1*2] = BC1 ^ ((~BC2) & BC3);
  1492. BC4 = s32[x + 4*2];
  1493. s32[x + 2*2] = BC2 ^ ((~BC3) & BC4);
  1494. s32[x + 3*2] = BC3 ^ ((~BC4) & BC0);
  1495. s32[x + 4*2] = BC4 ^ ((~BC0) & BC1);
  1496. x++;
  1497. BC0 = s32[x + 0*2];
  1498. BC1 = s32[x + 1*2];
  1499. BC2 = s32[x + 2*2];
  1500. s32[x + 0*2] = BC0 ^ ((~BC1) & BC2);
  1501. BC3 = s32[x + 3*2];
  1502. s32[x + 1*2] = BC1 ^ ((~BC2) & BC3);
  1503. BC4 = s32[x + 4*2];
  1504. s32[x + 2*2] = BC2 ^ ((~BC3) & BC4);
  1505. s32[x + 3*2] = BC3 ^ ((~BC4) & BC0);
  1506. s32[x + 4*2] = BC4 ^ ((~BC0) & BC1);
  1507. x += 9;
  1508. }
  1509. /* Iota */
  1510. s32[0] ^= IOTA_CONST_0bits & 1;
  1511. IOTA_CONST_0bits >>= 1;
  1512. s32[1] ^= IOTA_CONST_1[round];
  1513. }
  1514. combine_halves(state);
  1515. #else
  1516. /* Native 64-bit algorithm */
  1517. static const uint16_t IOTA_CONST[NROUNDS] ALIGN2 = {
  1518. /* Elements should be 64-bit, but top half is always zero
  1519. * or 0x80000000. We encode 63rd bits in a separate word below.
  1520. * Same is true for 31th bits, which lets us use 16-bit table
  1521. * instead of 64-bit. The speed penalty is lost in the noise.
  1522. */
  1523. 0x0001,
  1524. 0x8082,
  1525. 0x808a,
  1526. 0x8000,
  1527. 0x808b,
  1528. 0x0001,
  1529. 0x8081,
  1530. 0x8009,
  1531. 0x008a,
  1532. 0x0088,
  1533. 0x8009,
  1534. 0x000a,
  1535. 0x808b,
  1536. 0x008b,
  1537. 0x8089,
  1538. 0x8003,
  1539. 0x8002,
  1540. 0x0080,
  1541. 0x800a,
  1542. 0x000a,
  1543. 0x8081,
  1544. 0x8080,
  1545. 0x0001,
  1546. 0x8008,
  1547. };
  1548. /* bit for CONST[0] is in msb: 0011 0011 0000 0111 1101 1101 */
  1549. const uint32_t IOTA_CONST_bit63 = (uint32_t)(0x3307dd00);
  1550. /* bit for CONST[0] is in msb: 0001 0110 0011 1000 0001 1011 */
  1551. const uint32_t IOTA_CONST_bit31 = (uint32_t)(0x16381b00);
  1552. static const uint8_t ROT_CONST[24] ALIGN1 = {
  1553. 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,
  1554. 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44,
  1555. };
  1556. static const uint8_t PI_LANE[24] ALIGN1 = {
  1557. 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
  1558. 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1,
  1559. };
  1560. /*static const uint8_t MOD5[10] ALIGN1 = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, };*/
  1561. unsigned x;
  1562. unsigned round;
  1563. if (BB_BIG_ENDIAN) {
  1564. for (x = 0; x < 25; x++) {
  1565. state[x] = SWAP_LE64(state[x]);
  1566. }
  1567. }
  1568. for (round = 0; round < NROUNDS; ++round) {
  1569. /* Theta */
  1570. {
  1571. uint64_t BC[10];
  1572. for (x = 0; x < 5; ++x) {
  1573. BC[x + 5] = BC[x] = state[x]
  1574. ^ state[x + 5] ^ state[x + 10]
  1575. ^ state[x + 15] ^ state[x + 20];
  1576. }
  1577. /* Using 2x5 vector above eliminates the need to use
  1578. * BC[MOD5[x+N]] trick below to fetch BC[(x+N) % 5],
  1579. * and the code is a bit _smaller_.
  1580. */
  1581. for (x = 0; x < 5; ++x) {
  1582. uint64_t temp = BC[x + 4] ^ rotl64(BC[x + 1], 1);
  1583. state[x] ^= temp;
  1584. state[x + 5] ^= temp;
  1585. state[x + 10] ^= temp;
  1586. state[x + 15] ^= temp;
  1587. state[x + 20] ^= temp;
  1588. }
  1589. }
  1590. /* Rho Pi */
  1591. if (SHA3_SMALL) {
  1592. uint64_t t1 = state[1];
  1593. for (x = 0; x < 24; ++x) {
  1594. uint64_t t0 = state[PI_LANE[x]];
  1595. state[PI_LANE[x]] = rotl64(t1, ROT_CONST[x]);
  1596. t1 = t0;
  1597. }
  1598. } else {
  1599. /* Especially large benefit for 32-bit arch (75% faster):
  1600. * 64-bit rotations by non-constant usually are SLOW on those.
  1601. * We resort to unrolling here.
  1602. * This optimizes out PI_LANE[] and ROT_CONST[],
  1603. * but generates 300-500 more bytes of code.
  1604. */
  1605. uint64_t t0;
  1606. uint64_t t1 = state[1];
  1607. #define RhoPi_twice(x) \
  1608. t0 = state[PI_LANE[x ]]; \
  1609. state[PI_LANE[x ]] = rotl64(t1, ROT_CONST[x ]); \
  1610. t1 = state[PI_LANE[x+1]]; \
  1611. state[PI_LANE[x+1]] = rotl64(t0, ROT_CONST[x+1]);
  1612. RhoPi_twice(0); RhoPi_twice(2);
  1613. RhoPi_twice(4); RhoPi_twice(6);
  1614. RhoPi_twice(8); RhoPi_twice(10);
  1615. RhoPi_twice(12); RhoPi_twice(14);
  1616. RhoPi_twice(16); RhoPi_twice(18);
  1617. RhoPi_twice(20); RhoPi_twice(22);
  1618. #undef RhoPi_twice
  1619. }
  1620. /* Chi */
  1621. # if LONG_MAX > 0x7fffffff
  1622. for (x = 0; x <= 20; x += 5) {
  1623. uint64_t BC0, BC1, BC2, BC3, BC4;
  1624. BC0 = state[x + 0];
  1625. BC1 = state[x + 1];
  1626. BC2 = state[x + 2];
  1627. state[x + 0] = BC0 ^ ((~BC1) & BC2);
  1628. BC3 = state[x + 3];
  1629. state[x + 1] = BC1 ^ ((~BC2) & BC3);
  1630. BC4 = state[x + 4];
  1631. state[x + 2] = BC2 ^ ((~BC3) & BC4);
  1632. state[x + 3] = BC3 ^ ((~BC4) & BC0);
  1633. state[x + 4] = BC4 ^ ((~BC0) & BC1);
  1634. }
  1635. # else
  1636. /* Reduced register pressure version
  1637. * for register-starved 32-bit arches
  1638. * (i386: -95 bytes, and it is _faster_)
  1639. */
  1640. for (x = 0; x <= 40;) {
  1641. uint32_t BC0, BC1, BC2, BC3, BC4;
  1642. uint32_t *const s32 = (uint32_t*)state;
  1643. # if SHA3_SMALL
  1644. do_half:
  1645. # endif
  1646. BC0 = s32[x + 0*2];
  1647. BC1 = s32[x + 1*2];
  1648. BC2 = s32[x + 2*2];
  1649. s32[x + 0*2] = BC0 ^ ((~BC1) & BC2);
  1650. BC3 = s32[x + 3*2];
  1651. s32[x + 1*2] = BC1 ^ ((~BC2) & BC3);
  1652. BC4 = s32[x + 4*2];
  1653. s32[x + 2*2] = BC2 ^ ((~BC3) & BC4);
  1654. s32[x + 3*2] = BC3 ^ ((~BC4) & BC0);
  1655. s32[x + 4*2] = BC4 ^ ((~BC0) & BC1);
  1656. x++;
  1657. # if SHA3_SMALL
  1658. if (x & 1)
  1659. goto do_half;
  1660. x += 8;
  1661. # else
  1662. BC0 = s32[x + 0*2];
  1663. BC1 = s32[x + 1*2];
  1664. BC2 = s32[x + 2*2];
  1665. s32[x + 0*2] = BC0 ^ ((~BC1) & BC2);
  1666. BC3 = s32[x + 3*2];
  1667. s32[x + 1*2] = BC1 ^ ((~BC2) & BC3);
  1668. BC4 = s32[x + 4*2];
  1669. s32[x + 2*2] = BC2 ^ ((~BC3) & BC4);
  1670. s32[x + 3*2] = BC3 ^ ((~BC4) & BC0);
  1671. s32[x + 4*2] = BC4 ^ ((~BC0) & BC1);
  1672. x += 9;
  1673. # endif
  1674. }
  1675. # endif /* long is 32-bit */
  1676. /* Iota */
  1677. state[0] ^= IOTA_CONST[round]
  1678. | (uint32_t)((IOTA_CONST_bit31 << round) & 0x80000000)
  1679. | (uint64_t)((IOTA_CONST_bit63 << round) & 0x80000000) << 32;
  1680. }
  1681. if (BB_BIG_ENDIAN) {
  1682. for (x = 0; x < 25; x++) {
  1683. state[x] = SWAP_LE64(state[x]);
  1684. }
  1685. }
  1686. #endif
  1687. }
  1688. void FAST_FUNC sha3_begin(sha3_ctx_t *ctx)
  1689. {
  1690. memset(ctx, 0, sizeof(*ctx));
  1691. /* SHA3-512, user can override */
  1692. ctx->input_block_bytes = (1600 - 512*2) / 8; /* 72 bytes */
  1693. }
  1694. void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len)
  1695. {
  1696. #if SHA3_SMALL
  1697. const uint8_t *data = buffer;
  1698. unsigned bufpos = ctx->bytes_queued;
  1699. while (1) {
  1700. unsigned remaining = ctx->input_block_bytes - bufpos;
  1701. if (remaining > len)
  1702. remaining = len;
  1703. len -= remaining;
  1704. /* XOR data into buffer */
  1705. while (remaining != 0) {
  1706. uint8_t *buf = (uint8_t*)ctx->state;
  1707. buf[bufpos] ^= *data++;
  1708. bufpos++;
  1709. remaining--;
  1710. }
  1711. /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
  1712. bufpos -= ctx->input_block_bytes;
  1713. if (bufpos != 0)
  1714. break;
  1715. /* Buffer is filled up, process it */
  1716. sha3_process_block72(ctx->state);
  1717. /*bufpos = 0; - already is */
  1718. }
  1719. ctx->bytes_queued = bufpos + ctx->input_block_bytes;
  1720. #else
  1721. /* +50 bytes code size, but a bit faster because of long-sized XORs */
  1722. const uint8_t *data = buffer;
  1723. unsigned bufpos = ctx->bytes_queued;
  1724. unsigned iblk_bytes = ctx->input_block_bytes;
  1725. /* If already data in queue, continue queuing first */
  1726. if (bufpos != 0) {
  1727. while (len != 0) {
  1728. uint8_t *buf = (uint8_t*)ctx->state;
  1729. buf[bufpos] ^= *data++;
  1730. len--;
  1731. bufpos++;
  1732. if (bufpos == iblk_bytes) {
  1733. bufpos = 0;
  1734. goto do_block;
  1735. }
  1736. }
  1737. }
  1738. /* Absorb complete blocks */
  1739. while (len >= iblk_bytes) {
  1740. /* XOR data onto beginning of state[].
  1741. * We try to be efficient - operate one word at a time, not byte.
  1742. * Careful wrt unaligned access: can't just use "*(long*)data"!
  1743. */
  1744. unsigned count = iblk_bytes / sizeof(long);
  1745. long *buf = (long*)ctx->state;
  1746. do {
  1747. long v;
  1748. move_from_unaligned_long(v, (long*)data);
  1749. *buf++ ^= v;
  1750. data += sizeof(long);
  1751. } while (--count);
  1752. len -= iblk_bytes;
  1753. do_block:
  1754. sha3_process_block72(ctx->state);
  1755. }
  1756. /* Queue remaining data bytes */
  1757. while (len != 0) {
  1758. uint8_t *buf = (uint8_t*)ctx->state;
  1759. buf[bufpos] ^= *data++;
  1760. bufpos++;
  1761. len--;
  1762. }
  1763. ctx->bytes_queued = bufpos;
  1764. #endif
  1765. }
  1766. unsigned FAST_FUNC sha3_end(sha3_ctx_t *ctx, void *resbuf)
  1767. {
  1768. /* Padding */
  1769. uint8_t *buf = (uint8_t*)ctx->state;
  1770. /*
  1771. * Keccak block padding is: add 1 bit after last bit of input,
  1772. * then add zero bits until the end of block, and add the last 1 bit
  1773. * (the last bit in the block) - the "10*1" pattern.
  1774. * SHA3 standard appends additional two bits, 01, before that padding:
  1775. *
  1776. * SHA3-224(M) = KECCAK[448](M||01, 224)
  1777. * SHA3-256(M) = KECCAK[512](M||01, 256)
  1778. * SHA3-384(M) = KECCAK[768](M||01, 384)
  1779. * SHA3-512(M) = KECCAK[1024](M||01, 512)
  1780. * (M is the input, || is bit concatenation)
  1781. *
  1782. * The 6 below contains 01 "SHA3" bits and the first 1 "Keccak" bit:
  1783. */
  1784. buf[ctx->bytes_queued] ^= 6; /* bit pattern 00000110 */
  1785. buf[ctx->input_block_bytes - 1] ^= 0x80;
  1786. sha3_process_block72(ctx->state);
  1787. /* Output */
  1788. memcpy(resbuf, ctx->state, 64);
  1789. return 64;
  1790. }