hash_md5_sha.c 56 KB

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