hash_md5_sha.c 39 KB

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