2
0

mkhash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. *
  16. * -- MD5 code:
  17. *
  18. * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
  19. * MD5 Message-Digest Algorithm (RFC 1321).
  20. *
  21. * Homepage:
  22. * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
  23. *
  24. * Author:
  25. * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
  26. *
  27. * This software was written by Alexander Peslyak in 2001. No copyright is
  28. * claimed, and the software is hereby placed in the public domain.
  29. * In case this attempt to disclaim copyright and place the software in the
  30. * public domain is deemed null and void, then the software is
  31. * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
  32. * general public under the following terms:
  33. *
  34. * Redistribution and use in source and binary forms, with or without
  35. * modification, are permitted.
  36. *
  37. * There's ABSOLUTELY NO WARRANTY, express or implied.
  38. *
  39. * (This is a heavily cut-down "BSD license".)
  40. *
  41. * This differs from Colin Plumb's older public domain implementation in that
  42. * no exactly 32-bit integer data type is required (any 32-bit or wider
  43. * unsigned integer data type will do), there's no compile-time endianness
  44. * configuration, and the function prototypes match OpenSSL's. No code from
  45. * Colin Plumb's implementation has been reused; this comment merely compares
  46. * the properties of the two independent implementations.
  47. *
  48. * The primary goals of this implementation are portability and ease of use.
  49. * It is meant to be fast, but not as fast as possible. Some known
  50. * optimizations are not included to reduce source code size and avoid
  51. * compile-time configuration.
  52. *
  53. * -- SHA256 Code:
  54. *
  55. * Copyright 2005 Colin Percival
  56. * All rights reserved.
  57. *
  58. * Redistribution and use in source and binary forms, with or without
  59. * modification, are permitted provided that the following conditions
  60. * are met:
  61. * 1. Redistributions of source code must retain the above copyright
  62. * notice, this list of conditions and the following disclaimer.
  63. * 2. Redistributions in binary form must reproduce the above copyright
  64. * notice, this list of conditions and the following disclaimer in the
  65. * documentation and/or other materials provided with the distribution.
  66. *
  67. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  68. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  69. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  70. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  71. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  72. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  73. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  74. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  75. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  76. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  77. * SUCH DAMAGE.
  78. */
  79. #include <endian.h>
  80. #include <stdio.h>
  81. #include <string.h>
  82. #include <stdint.h>
  83. #include <stdbool.h>
  84. #include <unistd.h>
  85. #define ARRAY_SIZE(_n) (sizeof(_n) / sizeof((_n)[0]))
  86. static void
  87. be32enc(void *buf, uint32_t u)
  88. {
  89. uint8_t *p = buf;
  90. p[0] = ((uint8_t) ((u >> 24) & 0xff));
  91. p[1] = ((uint8_t) ((u >> 16) & 0xff));
  92. p[2] = ((uint8_t) ((u >> 8) & 0xff));
  93. p[3] = ((uint8_t) (u & 0xff));
  94. }
  95. static void
  96. be64enc(void *buf, uint64_t u)
  97. {
  98. uint8_t *p = buf;
  99. be32enc(p, ((uint32_t) (u >> 32)));
  100. be32enc(p + 4, ((uint32_t) (u & 0xffffffffULL)));
  101. }
  102. static uint16_t
  103. be16dec(const void *buf)
  104. {
  105. const uint8_t *p = buf;
  106. return (((uint16_t) p[0]) << 8) | p[1];
  107. }
  108. static uint32_t
  109. be32dec(const void *buf)
  110. {
  111. const uint8_t *p = buf;
  112. return (((uint32_t) be16dec(p)) << 16) | be16dec(p + 2);
  113. }
  114. #define MD5_DIGEST_LENGTH 16
  115. typedef struct MD5_CTX {
  116. uint32_t lo, hi;
  117. uint32_t a, b, c, d;
  118. unsigned char buffer[64];
  119. } MD5_CTX;
  120. /*
  121. * The basic MD5 functions.
  122. *
  123. * F and G are optimized compared to their RFC 1321 definitions for
  124. * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
  125. * implementation.
  126. */
  127. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  128. #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
  129. #define H(x, y, z) (((x) ^ (y)) ^ (z))
  130. #define H2(x, y, z) ((x) ^ ((y) ^ (z)))
  131. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  132. /*
  133. * The MD5 transformation for all four rounds.
  134. */
  135. #define STEP(f, a, b, c, d, x, t, s) \
  136. (a) += f((b), (c), (d)) + (x) + (t); \
  137. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  138. (a) += (b);
  139. /*
  140. * SET reads 4 input bytes in little-endian byte order and stores them
  141. * in a properly aligned word in host byte order.
  142. */
  143. #if __BYTE_ORDER == __LITTLE_ENDIAN
  144. #define SET(n) \
  145. (*(uint32_t *)&ptr[(n) * 4])
  146. #define GET(n) \
  147. SET(n)
  148. #else
  149. #define SET(n) \
  150. (block[(n)] = \
  151. (uint32_t)ptr[(n) * 4] | \
  152. ((uint32_t)ptr[(n) * 4 + 1] << 8) | \
  153. ((uint32_t)ptr[(n) * 4 + 2] << 16) | \
  154. ((uint32_t)ptr[(n) * 4 + 3] << 24))
  155. #define GET(n) \
  156. (block[(n)])
  157. #endif
  158. /*
  159. * This processes one or more 64-byte data blocks, but does NOT update
  160. * the bit counters. There are no alignment requirements.
  161. */
  162. static const void *MD5_body(MD5_CTX *ctx, const void *data, unsigned long size)
  163. {
  164. const unsigned char *ptr;
  165. uint32_t a, b, c, d;
  166. uint32_t saved_a, saved_b, saved_c, saved_d;
  167. #if __BYTE_ORDER != __LITTLE_ENDIAN
  168. uint32_t block[16];
  169. #endif
  170. ptr = (const unsigned char *)data;
  171. a = ctx->a;
  172. b = ctx->b;
  173. c = ctx->c;
  174. d = ctx->d;
  175. do {
  176. saved_a = a;
  177. saved_b = b;
  178. saved_c = c;
  179. saved_d = d;
  180. /* Round 1 */
  181. STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
  182. STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
  183. STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
  184. STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
  185. STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
  186. STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
  187. STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
  188. STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
  189. STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
  190. STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
  191. STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
  192. STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
  193. STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
  194. STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
  195. STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
  196. STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
  197. /* Round 2 */
  198. STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
  199. STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
  200. STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
  201. STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
  202. STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
  203. STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
  204. STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
  205. STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
  206. STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
  207. STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
  208. STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
  209. STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
  210. STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
  211. STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
  212. STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
  213. STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
  214. /* Round 3 */
  215. STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
  216. STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11)
  217. STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
  218. STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23)
  219. STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
  220. STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11)
  221. STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
  222. STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23)
  223. STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
  224. STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11)
  225. STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
  226. STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23)
  227. STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
  228. STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11)
  229. STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
  230. STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23)
  231. /* Round 4 */
  232. STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
  233. STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
  234. STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
  235. STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
  236. STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
  237. STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
  238. STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
  239. STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
  240. STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
  241. STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
  242. STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
  243. STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
  244. STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
  245. STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
  246. STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
  247. STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
  248. a += saved_a;
  249. b += saved_b;
  250. c += saved_c;
  251. d += saved_d;
  252. ptr += 64;
  253. } while (size -= 64);
  254. ctx->a = a;
  255. ctx->b = b;
  256. ctx->c = c;
  257. ctx->d = d;
  258. return ptr;
  259. }
  260. void MD5_begin(MD5_CTX *ctx)
  261. {
  262. ctx->a = 0x67452301;
  263. ctx->b = 0xefcdab89;
  264. ctx->c = 0x98badcfe;
  265. ctx->d = 0x10325476;
  266. ctx->lo = 0;
  267. ctx->hi = 0;
  268. }
  269. static void
  270. MD5_hash(const void *data, size_t size, MD5_CTX *ctx)
  271. {
  272. uint32_t saved_lo;
  273. unsigned long used, available;
  274. saved_lo = ctx->lo;
  275. if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
  276. ctx->hi++;
  277. ctx->hi += size >> 29;
  278. used = saved_lo & 0x3f;
  279. if (used) {
  280. available = 64 - used;
  281. if (size < available) {
  282. memcpy(&ctx->buffer[used], data, size);
  283. return;
  284. }
  285. memcpy(&ctx->buffer[used], data, available);
  286. data = (const unsigned char *)data + available;
  287. size -= available;
  288. MD5_body(ctx, ctx->buffer, 64);
  289. }
  290. if (size >= 64) {
  291. data = MD5_body(ctx, data, size & ~((size_t) 0x3f));
  292. size &= 0x3f;
  293. }
  294. memcpy(ctx->buffer, data, size);
  295. }
  296. static void
  297. MD5_end(void *resbuf, MD5_CTX *ctx)
  298. {
  299. unsigned char *result = resbuf;
  300. unsigned long used, available;
  301. used = ctx->lo & 0x3f;
  302. ctx->buffer[used++] = 0x80;
  303. available = 64 - used;
  304. if (available < 8) {
  305. memset(&ctx->buffer[used], 0, available);
  306. MD5_body(ctx, ctx->buffer, 64);
  307. used = 0;
  308. available = 64;
  309. }
  310. memset(&ctx->buffer[used], 0, available - 8);
  311. ctx->lo <<= 3;
  312. ctx->buffer[56] = ctx->lo;
  313. ctx->buffer[57] = ctx->lo >> 8;
  314. ctx->buffer[58] = ctx->lo >> 16;
  315. ctx->buffer[59] = ctx->lo >> 24;
  316. ctx->buffer[60] = ctx->hi;
  317. ctx->buffer[61] = ctx->hi >> 8;
  318. ctx->buffer[62] = ctx->hi >> 16;
  319. ctx->buffer[63] = ctx->hi >> 24;
  320. MD5_body(ctx, ctx->buffer, 64);
  321. result[0] = ctx->a;
  322. result[1] = ctx->a >> 8;
  323. result[2] = ctx->a >> 16;
  324. result[3] = ctx->a >> 24;
  325. result[4] = ctx->b;
  326. result[5] = ctx->b >> 8;
  327. result[6] = ctx->b >> 16;
  328. result[7] = ctx->b >> 24;
  329. result[8] = ctx->c;
  330. result[9] = ctx->c >> 8;
  331. result[10] = ctx->c >> 16;
  332. result[11] = ctx->c >> 24;
  333. result[12] = ctx->d;
  334. result[13] = ctx->d >> 8;
  335. result[14] = ctx->d >> 16;
  336. result[15] = ctx->d >> 24;
  337. memset(ctx, 0, sizeof(*ctx));
  338. }
  339. #define SHA256_BLOCK_LENGTH 64
  340. #define SHA256_DIGEST_LENGTH 32
  341. #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
  342. typedef struct SHA256Context {
  343. uint32_t state[8];
  344. uint64_t count;
  345. uint8_t buf[SHA256_BLOCK_LENGTH];
  346. } SHA256_CTX;
  347. #if BYTE_ORDER == BIG_ENDIAN
  348. /* Copy a vector of big-endian uint32_t into a vector of bytes */
  349. #define be32enc_vect(dst, src, len) \
  350. memcpy((void *)dst, (const void *)src, (size_t)len)
  351. /* Copy a vector of bytes into a vector of big-endian uint32_t */
  352. #define be32dec_vect(dst, src, len) \
  353. memcpy((void *)dst, (const void *)src, (size_t)len)
  354. #else /* BYTE_ORDER != BIG_ENDIAN */
  355. /*
  356. * Encode a length len/4 vector of (uint32_t) into a length len vector of
  357. * (unsigned char) in big-endian form. Assumes len is a multiple of 4.
  358. */
  359. static void
  360. be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len)
  361. {
  362. size_t i;
  363. for (i = 0; i < len / 4; i++)
  364. be32enc(dst + i * 4, src[i]);
  365. }
  366. /*
  367. * Decode a big-endian length len vector of (unsigned char) into a length
  368. * len/4 vector of (uint32_t). Assumes len is a multiple of 4.
  369. */
  370. static void
  371. be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len)
  372. {
  373. size_t i;
  374. for (i = 0; i < len / 4; i++)
  375. dst[i] = be32dec(src + i * 4);
  376. }
  377. #endif /* BYTE_ORDER != BIG_ENDIAN */
  378. /* Elementary functions used by SHA256 */
  379. #define Ch(x, y, z) ((x & (y ^ z)) ^ z)
  380. #define Maj(x, y, z) ((x & (y | z)) | (y & z))
  381. #define ROTR(x, n) ((x >> n) | (x << (32 - n)))
  382. /*
  383. * SHA256 block compression function. The 256-bit state is transformed via
  384. * the 512-bit input block to produce a new state.
  385. */
  386. static void
  387. SHA256_Transform(uint32_t * state, const unsigned char block[64])
  388. {
  389. /* SHA256 round constants. */
  390. static const uint32_t K[64] = {
  391. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  392. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  393. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  394. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  395. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  396. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  397. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  398. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  399. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  400. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  401. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  402. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  403. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  404. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  405. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  406. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  407. };
  408. uint32_t W[64];
  409. uint32_t S[8];
  410. int i;
  411. #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
  412. #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
  413. #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ (x >> 3))
  414. #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ (x >> 10))
  415. /* SHA256 round function */
  416. #define RND(a, b, c, d, e, f, g, h, k) \
  417. h += S1(e) + Ch(e, f, g) + k; \
  418. d += h; \
  419. h += S0(a) + Maj(a, b, c);
  420. /* Adjusted round function for rotating state */
  421. #define RNDr(S, W, i, ii) \
  422. RND(S[(64 - i) % 8], S[(65 - i) % 8], \
  423. S[(66 - i) % 8], S[(67 - i) % 8], \
  424. S[(68 - i) % 8], S[(69 - i) % 8], \
  425. S[(70 - i) % 8], S[(71 - i) % 8], \
  426. W[i + ii] + K[i + ii])
  427. /* Message schedule computation */
  428. #define MSCH(W, ii, i) \
  429. W[i + ii + 16] = s1(W[i + ii + 14]) + W[i + ii + 9] + s0(W[i + ii + 1]) + W[i + ii]
  430. /* 1. Prepare the first part of the message schedule W. */
  431. be32dec_vect(W, block, 64);
  432. /* 2. Initialize working variables. */
  433. memcpy(S, state, 32);
  434. /* 3. Mix. */
  435. for (i = 0; i < 64; i += 16) {
  436. RNDr(S, W, 0, i);
  437. RNDr(S, W, 1, i);
  438. RNDr(S, W, 2, i);
  439. RNDr(S, W, 3, i);
  440. RNDr(S, W, 4, i);
  441. RNDr(S, W, 5, i);
  442. RNDr(S, W, 6, i);
  443. RNDr(S, W, 7, i);
  444. RNDr(S, W, 8, i);
  445. RNDr(S, W, 9, i);
  446. RNDr(S, W, 10, i);
  447. RNDr(S, W, 11, i);
  448. RNDr(S, W, 12, i);
  449. RNDr(S, W, 13, i);
  450. RNDr(S, W, 14, i);
  451. RNDr(S, W, 15, i);
  452. if (i == 48)
  453. break;
  454. MSCH(W, 0, i);
  455. MSCH(W, 1, i);
  456. MSCH(W, 2, i);
  457. MSCH(W, 3, i);
  458. MSCH(W, 4, i);
  459. MSCH(W, 5, i);
  460. MSCH(W, 6, i);
  461. MSCH(W, 7, i);
  462. MSCH(W, 8, i);
  463. MSCH(W, 9, i);
  464. MSCH(W, 10, i);
  465. MSCH(W, 11, i);
  466. MSCH(W, 12, i);
  467. MSCH(W, 13, i);
  468. MSCH(W, 14, i);
  469. MSCH(W, 15, i);
  470. }
  471. #undef S0
  472. #undef s0
  473. #undef S1
  474. #undef s1
  475. #undef RND
  476. #undef RNDr
  477. #undef MSCH
  478. /* 4. Mix local working variables into global state */
  479. for (i = 0; i < 8; i++)
  480. state[i] += S[i];
  481. }
  482. static unsigned char PAD[64] = {
  483. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  484. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  485. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  486. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  487. };
  488. /* Add padding and terminating bit-count. */
  489. static void
  490. SHA256_Pad(SHA256_CTX * ctx)
  491. {
  492. size_t r;
  493. /* Figure out how many bytes we have buffered. */
  494. r = (ctx->count >> 3) & 0x3f;
  495. /* Pad to 56 mod 64, transforming if we finish a block en route. */
  496. if (r < 56) {
  497. /* Pad to 56 mod 64. */
  498. memcpy(&ctx->buf[r], PAD, 56 - r);
  499. } else {
  500. /* Finish the current block and mix. */
  501. memcpy(&ctx->buf[r], PAD, 64 - r);
  502. SHA256_Transform(ctx->state, ctx->buf);
  503. /* The start of the final block is all zeroes. */
  504. memset(&ctx->buf[0], 0, 56);
  505. }
  506. /* Add the terminating bit-count. */
  507. be64enc(&ctx->buf[56], ctx->count);
  508. /* Mix in the final block. */
  509. SHA256_Transform(ctx->state, ctx->buf);
  510. }
  511. /* SHA-256 initialization. Begins a SHA-256 operation. */
  512. static void
  513. SHA256_Init(SHA256_CTX * ctx)
  514. {
  515. /* Zero bits processed so far */
  516. ctx->count = 0;
  517. /* Magic initialization constants */
  518. ctx->state[0] = 0x6A09E667;
  519. ctx->state[1] = 0xBB67AE85;
  520. ctx->state[2] = 0x3C6EF372;
  521. ctx->state[3] = 0xA54FF53A;
  522. ctx->state[4] = 0x510E527F;
  523. ctx->state[5] = 0x9B05688C;
  524. ctx->state[6] = 0x1F83D9AB;
  525. ctx->state[7] = 0x5BE0CD19;
  526. }
  527. /* Add bytes into the hash */
  528. static void
  529. SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len)
  530. {
  531. uint64_t bitlen;
  532. uint32_t r;
  533. const unsigned char *src = in;
  534. /* Number of bytes left in the buffer from previous updates */
  535. r = (ctx->count >> 3) & 0x3f;
  536. /* Convert the length into a number of bits */
  537. bitlen = len << 3;
  538. /* Update number of bits */
  539. ctx->count += bitlen;
  540. /* Handle the case where we don't need to perform any transforms */
  541. if (len < 64 - r) {
  542. memcpy(&ctx->buf[r], src, len);
  543. return;
  544. }
  545. /* Finish the current block */
  546. memcpy(&ctx->buf[r], src, 64 - r);
  547. SHA256_Transform(ctx->state, ctx->buf);
  548. src += 64 - r;
  549. len -= 64 - r;
  550. /* Perform complete blocks */
  551. while (len >= 64) {
  552. SHA256_Transform(ctx->state, src);
  553. src += 64;
  554. len -= 64;
  555. }
  556. /* Copy left over data into buffer */
  557. memcpy(ctx->buf, src, len);
  558. }
  559. /*
  560. * SHA-256 finalization. Pads the input data, exports the hash value,
  561. * and clears the context state.
  562. */
  563. static void
  564. SHA256_Final(unsigned char digest[static SHA256_DIGEST_LENGTH], SHA256_CTX *ctx)
  565. {
  566. /* Add padding */
  567. SHA256_Pad(ctx);
  568. /* Write the hash */
  569. be32enc_vect(digest, ctx->state, SHA256_DIGEST_LENGTH);
  570. /* Clear the context state */
  571. memset(ctx, 0, sizeof(*ctx));
  572. }
  573. static void *hash_buf(FILE *f, int *len)
  574. {
  575. static char buf[1024];
  576. *len = fread(buf, 1, sizeof(buf), f);
  577. return *len > 0 ? buf : NULL;
  578. }
  579. static char *hash_string(unsigned char *buf, int len)
  580. {
  581. static char str[SHA256_DIGEST_LENGTH * 2 + 1];
  582. int i;
  583. if (len * 2 + 1 > sizeof(str))
  584. return NULL;
  585. for (i = 0; i < len; i++)
  586. sprintf(&str[i * 2], "%02x", buf[i]);
  587. return str;
  588. }
  589. static const char *md5_hash(FILE *f)
  590. {
  591. MD5_CTX ctx;
  592. unsigned char val[MD5_DIGEST_LENGTH];
  593. void *buf;
  594. int len;
  595. MD5_begin(&ctx);
  596. while ((buf = hash_buf(f, &len)) != NULL)
  597. MD5_hash(buf, len, &ctx);
  598. MD5_end(val, &ctx);
  599. return hash_string(val, MD5_DIGEST_LENGTH);
  600. }
  601. static const char *sha256_hash(FILE *f)
  602. {
  603. SHA256_CTX ctx;
  604. unsigned char val[SHA256_DIGEST_LENGTH];
  605. void *buf;
  606. int len;
  607. SHA256_Init(&ctx);
  608. while ((buf = hash_buf(f, &len)) != NULL)
  609. SHA256_Update(&ctx, buf, len);
  610. SHA256_Final(val, &ctx);
  611. return hash_string(val, SHA256_DIGEST_LENGTH);
  612. }
  613. struct hash_type {
  614. const char *name;
  615. const char *(*func)(FILE *f);
  616. int len;
  617. };
  618. struct hash_type types[] = {
  619. { "md5", md5_hash, MD5_DIGEST_LENGTH },
  620. { "sha256", sha256_hash, SHA256_DIGEST_LENGTH },
  621. };
  622. static int usage(const char *progname)
  623. {
  624. int i;
  625. fprintf(stderr, "Usage: %s <hash type> [<file>...]\n"
  626. "Supported hash types:", progname);
  627. for (i = 0; i < ARRAY_SIZE(types); i++)
  628. fprintf(stderr, "%s %s", i ? "," : "", types[i].name);
  629. fprintf(stderr, "\n");
  630. return 1;
  631. }
  632. static struct hash_type *get_hash_type(const char *name)
  633. {
  634. int i;
  635. for (i = 0; i < ARRAY_SIZE(types); i++) {
  636. struct hash_type *t = &types[i];
  637. if (!strcmp(t->name, name))
  638. return t;
  639. }
  640. return NULL;
  641. }
  642. static int hash_file(struct hash_type *t, const char *filename, bool add_filename)
  643. {
  644. const char *str;
  645. if (!filename || !strcmp(filename, "-")) {
  646. str = t->func(stdin);
  647. } else {
  648. FILE *f = fopen(filename, "r");
  649. if (!f) {
  650. fprintf(stderr, "Failed to open '%s'\n", filename);
  651. return 1;
  652. }
  653. str = t->func(f);
  654. fclose(f);
  655. }
  656. if (!str) {
  657. fprintf(stderr, "Failed to generate hash\n");
  658. return 1;
  659. }
  660. if (add_filename)
  661. printf("%s %s\n", str, filename ? filename : "-");
  662. else
  663. printf("%s\n", str);
  664. return 0;
  665. }
  666. int main(int argc, char **argv)
  667. {
  668. struct hash_type *t;
  669. const char *progname = argv[0];
  670. int i, ch;
  671. bool add_filename = false;
  672. while ((ch = getopt(argc, argv, "n")) != -1) {
  673. switch (ch) {
  674. case 'n':
  675. add_filename = true;
  676. break;
  677. default:
  678. return usage(progname);
  679. }
  680. }
  681. argc -= optind;
  682. argv += optind;
  683. if (argc < 1)
  684. return usage(progname);
  685. t = get_hash_type(argv[0]);
  686. if (!t)
  687. return usage(progname);
  688. if (argc < 2)
  689. return hash_file(t, NULL, add_filename);
  690. for (i = 0; i < argc - 1; i++)
  691. hash_file(t, argv[1 + i], add_filename);
  692. return 0;
  693. }