sha1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Based on shasum from http://www.netsw.org/crypto/hash/
  4. * Majorly hacked up to use Dr Brian Gladman's sha1 code
  5. *
  6. * Copyright (C) 2002 Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
  7. * Copyright (C) 2003 Glenn L. McGrath
  8. * Copyright (C) 2003 Erik Andersen
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  11. *
  12. * ---------------------------------------------------------------------------
  13. * Issue Date: 10/11/2002
  14. *
  15. * This is a byte oriented version of SHA1 that operates on arrays of bytes
  16. * stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor
  17. *
  18. * ---------------------------------------------------------------------------
  19. *
  20. * SHA256 and SHA512 parts are:
  21. * Released into the Public Domain by Ulrich Drepper <drepper@redhat.com>.
  22. * Shrank by Denys Vlasenko.
  23. *
  24. * ---------------------------------------------------------------------------
  25. *
  26. * The best way to test random blocksizes is to go to coreutils/md5_sha1_sum.c
  27. * and replace "4096" with something like "2000 + time(NULL) % 2097",
  28. * then rebuild and compare "shaNNNsum bigfile" results.
  29. */
  30. #include "libbb.h"
  31. #define rotl32(x,n) (((x) << (n)) | ((x) >> (32 - (n))))
  32. #define rotr32(x,n) (((x) >> (n)) | ((x) << (32 - (n))))
  33. /* for sha512: */
  34. #define rotr64(x,n) (((x) >> (n)) | ((x) << (64 - (n))))
  35. #if BB_LITTLE_ENDIAN
  36. static inline uint64_t hton64(uint64_t v)
  37. {
  38. return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32);
  39. }
  40. #else
  41. #define hton64(v) (v)
  42. #endif
  43. #define ntoh64(v) hton64(v)
  44. /* To check alignment gcc has an appropriate operator. Other
  45. compilers don't. */
  46. #if defined(__GNUC__) && __GNUC__ >= 2
  47. # define UNALIGNED_P(p,type) (((uintptr_t) p) % __alignof__(type) != 0)
  48. #else
  49. # define UNALIGNED_P(p,type) (((uintptr_t) p) % sizeof(type) != 0)
  50. #endif
  51. static void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx)
  52. {
  53. unsigned t;
  54. uint32_t W[80], a, b, c, d, e;
  55. const uint32_t *words = (uint32_t*) ctx->wbuffer;
  56. for (t = 0; t < 16; ++t) {
  57. W[t] = ntohl(*words);
  58. words++;
  59. }
  60. for (/*t = 16*/; t < 80; ++t) {
  61. uint32_t T = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
  62. W[t] = rotl32(T, 1);
  63. }
  64. a = ctx->hash[0];
  65. b = ctx->hash[1];
  66. c = ctx->hash[2];
  67. d = ctx->hash[3];
  68. e = ctx->hash[4];
  69. /* Reverse byte order in 32-bit words */
  70. #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  71. #define parity(x,y,z) ((x) ^ (y) ^ (z))
  72. #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
  73. /* A normal version as set out in the FIPS. This version uses */
  74. /* partial loop unrolling and is optimised for the Pentium 4 */
  75. #define rnd(f,k) \
  76. do { \
  77. uint32_t T = a; \
  78. a = rotl32(a, 5) + f(b, c, d) + e + k + W[t]; \
  79. e = d; \
  80. d = c; \
  81. c = rotl32(b, 30); \
  82. b = T; \
  83. } while (0)
  84. for (t = 0; t < 20; ++t)
  85. rnd(ch, 0x5a827999);
  86. for (/*t = 20*/; t < 40; ++t)
  87. rnd(parity, 0x6ed9eba1);
  88. for (/*t = 40*/; t < 60; ++t)
  89. rnd(maj, 0x8f1bbcdc);
  90. for (/*t = 60*/; t < 80; ++t)
  91. rnd(parity, 0xca62c1d6);
  92. #undef ch
  93. #undef parity
  94. #undef maj
  95. #undef rnd
  96. ctx->hash[0] += a;
  97. ctx->hash[1] += b;
  98. ctx->hash[2] += c;
  99. ctx->hash[3] += d;
  100. ctx->hash[4] += e;
  101. }
  102. /* Constants for SHA512 from FIPS 180-2:4.2.3.
  103. * SHA256 constants from FIPS 180-2:4.2.2
  104. * are the most significant half of first 64 elements
  105. * of the same array.
  106. */
  107. static const uint64_t sha_K[80] = {
  108. 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
  109. 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
  110. 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
  111. 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
  112. 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
  113. 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
  114. 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
  115. 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
  116. 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
  117. 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
  118. 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
  119. 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
  120. 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
  121. 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
  122. 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
  123. 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
  124. 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
  125. 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
  126. 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
  127. 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
  128. 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
  129. 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
  130. 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
  131. 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
  132. 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
  133. 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
  134. 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
  135. 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
  136. 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
  137. 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
  138. 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
  139. 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
  140. 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, /* [64]+ are used for sha512 only */
  141. 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
  142. 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
  143. 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
  144. 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
  145. 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
  146. 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
  147. 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
  148. };
  149. static void FAST_FUNC sha256_process_block64(sha256_ctx_t *ctx)
  150. {
  151. unsigned t;
  152. uint32_t W[64], a, b, c, d, e, f, g, h;
  153. const uint32_t *words = (uint32_t*) ctx->wbuffer;
  154. /* Operators defined in FIPS 180-2:4.1.2. */
  155. #define Ch(x, y, z) ((x & y) ^ (~x & z))
  156. #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
  157. #define S0(x) (rotr32(x, 2) ^ rotr32(x, 13) ^ rotr32(x, 22))
  158. #define S1(x) (rotr32(x, 6) ^ rotr32(x, 11) ^ rotr32(x, 25))
  159. #define R0(x) (rotr32(x, 7) ^ rotr32(x, 18) ^ (x >> 3))
  160. #define R1(x) (rotr32(x, 17) ^ rotr32(x, 19) ^ (x >> 10))
  161. /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */
  162. for (t = 0; t < 16; ++t) {
  163. W[t] = ntohl(*words);
  164. words++;
  165. }
  166. for (/*t = 16*/; t < 64; ++t)
  167. W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16];
  168. a = ctx->hash[0];
  169. b = ctx->hash[1];
  170. c = ctx->hash[2];
  171. d = ctx->hash[3];
  172. e = ctx->hash[4];
  173. f = ctx->hash[5];
  174. g = ctx->hash[6];
  175. h = ctx->hash[7];
  176. /* The actual computation according to FIPS 180-2:6.2.2 step 3. */
  177. for (t = 0; t < 64; ++t) {
  178. /* Need to fetch upper half of sha_K[t]
  179. * (I hope compiler is clever enough to just fetch
  180. * upper half)
  181. */
  182. uint32_t K_t = sha_K[t] >> 32;
  183. uint32_t T1 = h + S1(e) + Ch(e, f, g) + K_t + W[t];
  184. uint32_t T2 = S0(a) + Maj(a, b, c);
  185. h = g;
  186. g = f;
  187. f = e;
  188. e = d + T1;
  189. d = c;
  190. c = b;
  191. b = a;
  192. a = T1 + T2;
  193. }
  194. #undef Ch
  195. #undef Maj
  196. #undef S0
  197. #undef S1
  198. #undef R0
  199. #undef R1
  200. /* Add the starting values of the context according to FIPS 180-2:6.2.2
  201. step 4. */
  202. ctx->hash[0] += a;
  203. ctx->hash[1] += b;
  204. ctx->hash[2] += c;
  205. ctx->hash[3] += d;
  206. ctx->hash[4] += e;
  207. ctx->hash[5] += f;
  208. ctx->hash[6] += g;
  209. ctx->hash[7] += h;
  210. }
  211. static void FAST_FUNC sha512_process_block128(sha512_ctx_t *ctx)
  212. {
  213. unsigned t;
  214. uint64_t W[80];
  215. /* On i386, having assignments here (not later as sha256 does)
  216. * produces 99 bytes smaller code with gcc 4.3.1
  217. */
  218. uint64_t a = ctx->hash[0];
  219. uint64_t b = ctx->hash[1];
  220. uint64_t c = ctx->hash[2];
  221. uint64_t d = ctx->hash[3];
  222. uint64_t e = ctx->hash[4];
  223. uint64_t f = ctx->hash[5];
  224. uint64_t g = ctx->hash[6];
  225. uint64_t h = ctx->hash[7];
  226. const uint64_t *words = (uint64_t*) ctx->wbuffer;
  227. /* Operators defined in FIPS 180-2:4.1.2. */
  228. #define Ch(x, y, z) ((x & y) ^ (~x & z))
  229. #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
  230. #define S0(x) (rotr64(x, 28) ^ rotr64(x, 34) ^ rotr64(x, 39))
  231. #define S1(x) (rotr64(x, 14) ^ rotr64(x, 18) ^ rotr64(x, 41))
  232. #define R0(x) (rotr64(x, 1) ^ rotr64(x, 8) ^ (x >> 7))
  233. #define R1(x) (rotr64(x, 19) ^ rotr64(x, 61) ^ (x >> 6))
  234. /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */
  235. for (t = 0; t < 16; ++t) {
  236. W[t] = ntoh64(*words);
  237. words++;
  238. }
  239. for (/*t = 16*/; t < 80; ++t)
  240. W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16];
  241. /* The actual computation according to FIPS 180-2:6.3.2 step 3. */
  242. for (t = 0; t < 80; ++t) {
  243. uint64_t T1 = h + S1(e) + Ch(e, f, g) + sha_K[t] + W[t];
  244. uint64_t T2 = S0(a) + Maj(a, b, c);
  245. h = g;
  246. g = f;
  247. f = e;
  248. e = d + T1;
  249. d = c;
  250. c = b;
  251. b = a;
  252. a = T1 + T2;
  253. }
  254. #undef Ch
  255. #undef Maj
  256. #undef S0
  257. #undef S1
  258. #undef R0
  259. #undef R1
  260. /* Add the starting values of the context according to FIPS 180-2:6.3.2
  261. step 4. */
  262. ctx->hash[0] += a;
  263. ctx->hash[1] += b;
  264. ctx->hash[2] += c;
  265. ctx->hash[3] += d;
  266. ctx->hash[4] += e;
  267. ctx->hash[5] += f;
  268. ctx->hash[6] += g;
  269. ctx->hash[7] += h;
  270. }
  271. void FAST_FUNC sha1_begin(sha1_ctx_t *ctx)
  272. {
  273. ctx->hash[0] = 0x67452301;
  274. ctx->hash[1] = 0xefcdab89;
  275. ctx->hash[2] = 0x98badcfe;
  276. ctx->hash[3] = 0x10325476;
  277. ctx->hash[4] = 0xc3d2e1f0;
  278. ctx->total64 = 0;
  279. ctx->process_block = sha1_process_block64;
  280. }
  281. static const uint32_t init256[] = {
  282. 0x6a09e667,
  283. 0xbb67ae85,
  284. 0x3c6ef372,
  285. 0xa54ff53a,
  286. 0x510e527f,
  287. 0x9b05688c,
  288. 0x1f83d9ab,
  289. 0x5be0cd19
  290. };
  291. static const uint32_t init512_lo[] = {
  292. 0xf3bcc908,
  293. 0x84caa73b,
  294. 0xfe94f82b,
  295. 0x5f1d36f1,
  296. 0xade682d1,
  297. 0x2b3e6c1f,
  298. 0xfb41bd6b,
  299. 0x137e2179
  300. };
  301. /* Initialize structure containing state of computation.
  302. (FIPS 180-2:5.3.2) */
  303. void FAST_FUNC sha256_begin(sha256_ctx_t *ctx)
  304. {
  305. memcpy(ctx->hash, init256, sizeof(init256));
  306. ctx->total64 = 0;
  307. ctx->process_block = sha256_process_block64;
  308. }
  309. /* Initialize structure containing state of computation.
  310. (FIPS 180-2:5.3.3) */
  311. void FAST_FUNC sha512_begin(sha512_ctx_t *ctx)
  312. {
  313. int i;
  314. for (i = 0; i < 8; i++)
  315. ctx->hash[i] = ((uint64_t)(init256[i]) << 32) + init512_lo[i];
  316. ctx->total64[0] = ctx->total64[1] = 0;
  317. }
  318. /* Used also for sha256 */
  319. void FAST_FUNC sha1_hash(const void *buffer, size_t len, sha1_ctx_t *ctx)
  320. {
  321. unsigned in_buf = ctx->total64 & 63;
  322. unsigned add = 64 - in_buf;
  323. ctx->total64 += len;
  324. while (len >= add) { /* transfer whole blocks while possible */
  325. memcpy(ctx->wbuffer + in_buf, buffer, add);
  326. buffer = (const char *)buffer + add;
  327. len -= add;
  328. add = 64;
  329. in_buf = 0;
  330. ctx->process_block(ctx);
  331. }
  332. memcpy(ctx->wbuffer + in_buf, buffer, len);
  333. }
  334. void FAST_FUNC sha512_hash(const void *buffer, size_t len, sha512_ctx_t *ctx)
  335. {
  336. unsigned in_buf = ctx->total64[0] & 127;
  337. unsigned add = 128 - in_buf;
  338. /* First increment the byte count. FIPS 180-2 specifies the possible
  339. length of the file up to 2^128 _bits_.
  340. We compute the number of _bytes_ and convert to bits later. */
  341. ctx->total64[0] += len;
  342. if (ctx->total64[0] < len)
  343. ctx->total64[1]++;
  344. while (len >= add) { /* transfer whole blocks while possible */
  345. memcpy(ctx->wbuffer + in_buf, buffer, add);
  346. buffer = (const char *)buffer + add;
  347. len -= add;
  348. add = 128;
  349. in_buf = 0;
  350. sha512_process_block128(ctx);
  351. }
  352. memcpy(ctx->wbuffer + in_buf, buffer, len);
  353. }
  354. /* Used also for sha256 */
  355. void FAST_FUNC sha1_end(void *resbuf, sha1_ctx_t *ctx)
  356. {
  357. unsigned i, pad, in_buf;
  358. in_buf = ctx->total64 & 63;
  359. /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */
  360. ctx->wbuffer[in_buf++] = 0x80;
  361. /* This loop iterates either once or twice, no more, no less */
  362. while (1) {
  363. pad = 64 - in_buf;
  364. memset(ctx->wbuffer + in_buf, 0, pad);
  365. in_buf = 0;
  366. /* Do we have enough space for the length count? */
  367. if (pad >= 8) {
  368. /* Store the 64-bit counter of bits in the buffer in BE format */
  369. uint64_t t = ctx->total64 << 3;
  370. t = hton64(t);
  371. /* wbuffer is suitably aligned for this */
  372. *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t;
  373. }
  374. ctx->process_block(ctx);
  375. if (pad >= 8)
  376. break;
  377. }
  378. in_buf = (ctx->process_block == sha1_process_block64) ? 5 : 8;
  379. /* This way we do not impose alignment constraints on resbuf: */
  380. #if BB_LITTLE_ENDIAN
  381. for (i = 0; i < in_buf; ++i)
  382. ctx->hash[i] = htonl(ctx->hash[i]);
  383. #endif
  384. memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * in_buf);
  385. }
  386. void FAST_FUNC sha512_end(void *resbuf, sha512_ctx_t *ctx)
  387. {
  388. unsigned i, pad, in_buf;
  389. in_buf = ctx->total64[0] & 127;
  390. /* Pad the buffer to the next 128-byte boundary with 0x80,0,0,0...
  391. * (FIPS 180-2:5.1.2)
  392. */
  393. ctx->wbuffer[in_buf++] = 0x80;
  394. while (1) {
  395. pad = 128 - in_buf;
  396. memset(ctx->wbuffer + in_buf, 0, pad);
  397. in_buf = 0;
  398. if (pad >= 16) {
  399. /* Store the 128-bit counter of bits in the buffer in BE format */
  400. uint64_t t;
  401. t = ctx->total64[0] << 3;
  402. t = hton64(t);
  403. *(uint64_t *) (&ctx->wbuffer[128 - 8]) = t;
  404. t = (ctx->total64[1] << 3) | (ctx->total64[0] >> 61);
  405. t = hton64(t);
  406. *(uint64_t *) (&ctx->wbuffer[128 - 16]) = t;
  407. }
  408. sha512_process_block128(ctx);
  409. if (pad >= 16)
  410. break;
  411. }
  412. #if BB_LITTLE_ENDIAN
  413. for (i = 0; i < ARRAY_SIZE(ctx->hash); ++i)
  414. ctx->hash[i] = hton64(ctx->hash[i]);
  415. #endif
  416. memcpy(resbuf, ctx->hash, sizeof(ctx->hash));
  417. }