crypt-sha2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. This code is based on the code found from 7-Zip, which has a modified
  3. version of the SHA-256 found from Crypto++ <http://www.cryptopp.com/>.
  4. The code was modified a little to fit into liblzma and fitz.
  5. This file has been put into the public domain.
  6. You can do whatever you want with this file.
  7. SHA-384 and SHA-512 were also taken from Crypto++ and adapted for fitz.
  8. */
  9. #include "mupdf/fitz.h"
  10. #include <string.h>
  11. static inline int isbigendian(void)
  12. {
  13. static const int one = 1;
  14. return *(char*)&one == 0;
  15. }
  16. static inline unsigned int bswap32(unsigned int num)
  17. {
  18. return ( (((num) << 24))
  19. | (((num) << 8) & 0x00FF0000)
  20. | (((num) >> 8) & 0x0000FF00)
  21. | (((num) >> 24)) );
  22. }
  23. static inline uint64_t bswap64(uint64_t num)
  24. {
  25. return ( (((num) << 56))
  26. | (((num) << 40) & 0x00FF000000000000ULL)
  27. | (((num) << 24) & 0x0000FF0000000000ULL)
  28. | (((num) << 8) & 0x000000FF00000000ULL)
  29. | (((num) >> 8) & 0x00000000FF000000ULL)
  30. | (((num) >> 24) & 0x0000000000FF0000ULL)
  31. | (((num) >> 40) & 0x000000000000FF00ULL)
  32. | (((num) >> 56)) );
  33. }
  34. /* At least on x86, GCC is able to optimize this to a rotate instruction. */
  35. #define rotr(num, amount) ((num) >> (amount) | (num) << (8 * sizeof(num) - (amount)))
  36. #define blk0(i) (W[i] = data[i])
  37. #define blk2(i) (W[i & 15] += s1(W[(i - 2) & 15]) + W[(i - 7) & 15] \
  38. + s0(W[(i - 15) & 15]))
  39. #define Ch(x, y, z) (z ^ (x & (y ^ z)))
  40. #define Maj(x, y, z) ((x & y) | (z & (x | y)))
  41. #define a(i) T[(0 - i) & 7]
  42. #define b(i) T[(1 - i) & 7]
  43. #define c(i) T[(2 - i) & 7]
  44. #define d(i) T[(3 - i) & 7]
  45. #define e(i) T[(4 - i) & 7]
  46. #define f(i) T[(5 - i) & 7]
  47. #define g(i) T[(6 - i) & 7]
  48. #define h(i) T[(7 - i) & 7]
  49. #define R(i) \
  50. h(i) += S1(e(i)) + Ch(e(i), f(i), g(i)) + K[i + j] \
  51. + (j ? blk2(i) : blk0(i)); \
  52. d(i) += h(i); \
  53. h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))
  54. /* For SHA256 */
  55. #define S0(x) (rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22))
  56. #define S1(x) (rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25))
  57. #define s0(x) (rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3))
  58. #define s1(x) (rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10))
  59. static const unsigned int SHA256_K[64] = {
  60. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  61. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  62. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  63. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  64. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  65. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  66. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  67. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  68. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  69. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  70. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  71. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  72. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  73. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  74. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  75. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
  76. };
  77. static void
  78. transform256(unsigned int state[8], unsigned int data[16])
  79. {
  80. const unsigned int *K = SHA256_K;
  81. unsigned int W[16];
  82. unsigned int T[8];
  83. unsigned int j;
  84. /* ensure big-endian integers */
  85. if (!isbigendian())
  86. for (j = 0; j < 16; j++)
  87. data[j] = bswap32(data[j]);
  88. /* Copy state[] to working vars. */
  89. memcpy(T, state, sizeof(T));
  90. /* 64 operations, partially loop unrolled */
  91. for (j = 0; j < 64; j += 16) {
  92. R( 0); R( 1); R( 2); R( 3);
  93. R( 4); R( 5); R( 6); R( 7);
  94. R( 8); R( 9); R(10); R(11);
  95. R(12); R(13); R(14); R(15);
  96. }
  97. /* Add the working vars back into state[]. */
  98. state[0] += a(0);
  99. state[1] += b(0);
  100. state[2] += c(0);
  101. state[3] += d(0);
  102. state[4] += e(0);
  103. state[5] += f(0);
  104. state[6] += g(0);
  105. state[7] += h(0);
  106. }
  107. #undef S0
  108. #undef S1
  109. #undef s0
  110. #undef s1
  111. void fz_sha256_init(fz_sha256 *context)
  112. {
  113. context->count[0] = context->count[1] = 0;
  114. context->state[0] = 0x6A09E667;
  115. context->state[1] = 0xBB67AE85;
  116. context->state[2] = 0x3C6EF372;
  117. context->state[3] = 0xA54FF53A;
  118. context->state[4] = 0x510E527F;
  119. context->state[5] = 0x9B05688C;
  120. context->state[6] = 0x1F83D9AB;
  121. context->state[7] = 0x5BE0CD19;
  122. }
  123. void fz_sha256_update(fz_sha256 *context, const unsigned char *input, size_t inlen)
  124. {
  125. /* Copy the input data into a properly aligned temporary buffer.
  126. * This way we can be called with arbitrarily sized buffers
  127. * (no need to be multiple of 64 bytes), and the code works also
  128. * on architectures that don't allow unaligned memory access. */
  129. while (inlen > 0)
  130. {
  131. const unsigned int copy_start = context->count[0] & 0x3F;
  132. unsigned int copy_size = 64 - copy_start;
  133. if (copy_size > inlen)
  134. copy_size = (unsigned int)inlen;
  135. memcpy(context->buffer.u8 + copy_start, input, copy_size);
  136. input += copy_size;
  137. inlen -= copy_size;
  138. context->count[0] += copy_size;
  139. /* carry overflow from low to high */
  140. if (context->count[0] < copy_size)
  141. context->count[1]++;
  142. if ((context->count[0] & 0x3F) == 0)
  143. transform256(context->state, context->buffer.u32);
  144. }
  145. }
  146. void fz_sha256_final(fz_sha256 *context, unsigned char digest[32])
  147. {
  148. /* Add padding as described in RFC 3174 (it describes SHA-1 but
  149. * the same padding style is used for SHA-256 too). */
  150. unsigned int j = context->count[0] & 0x3F;
  151. context->buffer.u8[j++] = 0x80;
  152. while (j != 56)
  153. {
  154. if (j == 64)
  155. {
  156. transform256(context->state, context->buffer.u32);
  157. j = 0;
  158. }
  159. context->buffer.u8[j++] = 0x00;
  160. }
  161. /* Convert the message size from bytes to bits. */
  162. context->count[1] = (context->count[1] << 3) + (context->count[0] >> 29);
  163. context->count[0] = context->count[0] << 3;
  164. if (!isbigendian())
  165. {
  166. context->buffer.u32[14] = bswap32(context->count[1]);
  167. context->buffer.u32[15] = bswap32(context->count[0]);
  168. }
  169. else
  170. {
  171. context->buffer.u32[14] = context->count[1];
  172. context->buffer.u32[15] = context->count[0];
  173. }
  174. transform256(context->state, context->buffer.u32);
  175. if (!isbigendian())
  176. for (j = 0; j < 8; j++)
  177. context->state[j] = bswap32(context->state[j]);
  178. memcpy(digest, &context->state[0], 32);
  179. memset(context, 0, sizeof(fz_sha256));
  180. }
  181. /* For SHA512 */
  182. #define S0(x) (rotr(x, 28) ^ rotr(x, 34) ^ rotr(x, 39))
  183. #define S1(x) (rotr(x, 14) ^ rotr(x, 18) ^ rotr(x, 41))
  184. #define s0(x) (rotr(x, 1) ^ rotr(x, 8) ^ (x >> 7))
  185. #define s1(x) (rotr(x, 19) ^ rotr(x, 61) ^ (x >> 6))
  186. static const uint64_t SHA512_K[80] = {
  187. 0x428A2F98D728AE22ULL, 0x7137449123EF65CDULL,
  188. 0xB5C0FBCFEC4D3B2FULL, 0xE9B5DBA58189DBBCULL,
  189. 0x3956C25BF348B538ULL, 0x59F111F1B605D019ULL,
  190. 0x923F82A4AF194F9BULL, 0xAB1C5ED5DA6D8118ULL,
  191. 0xD807AA98A3030242ULL, 0x12835B0145706FBEULL,
  192. 0x243185BE4EE4B28CULL, 0x550C7DC3D5FFB4E2ULL,
  193. 0x72BE5D74F27B896FULL, 0x80DEB1FE3B1696B1ULL,
  194. 0x9BDC06A725C71235ULL, 0xC19BF174CF692694ULL,
  195. 0xE49B69C19EF14AD2ULL, 0xEFBE4786384F25E3ULL,
  196. 0x0FC19DC68B8CD5B5ULL, 0x240CA1CC77AC9C65ULL,
  197. 0x2DE92C6F592B0275ULL, 0x4A7484AA6EA6E483ULL,
  198. 0x5CB0A9DCBD41FBD4ULL, 0x76F988DA831153B5ULL,
  199. 0x983E5152EE66DFABULL, 0xA831C66D2DB43210ULL,
  200. 0xB00327C898FB213FULL, 0xBF597FC7BEEF0EE4ULL,
  201. 0xC6E00BF33DA88FC2ULL, 0xD5A79147930AA725ULL,
  202. 0x06CA6351E003826FULL, 0x142929670A0E6E70ULL,
  203. 0x27B70A8546D22FFCULL, 0x2E1B21385C26C926ULL,
  204. 0x4D2C6DFC5AC42AEDULL, 0x53380D139D95B3DFULL,
  205. 0x650A73548BAF63DEULL, 0x766A0ABB3C77B2A8ULL,
  206. 0x81C2C92E47EDAEE6ULL, 0x92722C851482353BULL,
  207. 0xA2BFE8A14CF10364ULL, 0xA81A664BBC423001ULL,
  208. 0xC24B8B70D0F89791ULL, 0xC76C51A30654BE30ULL,
  209. 0xD192E819D6EF5218ULL, 0xD69906245565A910ULL,
  210. 0xF40E35855771202AULL, 0x106AA07032BBD1B8ULL,
  211. 0x19A4C116B8D2D0C8ULL, 0x1E376C085141AB53ULL,
  212. 0x2748774CDF8EEB99ULL, 0x34B0BCB5E19B48A8ULL,
  213. 0x391C0CB3C5C95A63ULL, 0x4ED8AA4AE3418ACBULL,
  214. 0x5B9CCA4F7763E373ULL, 0x682E6FF3D6B2B8A3ULL,
  215. 0x748F82EE5DEFB2FCULL, 0x78A5636F43172F60ULL,
  216. 0x84C87814A1F0AB72ULL, 0x8CC702081A6439ECULL,
  217. 0x90BEFFFA23631E28ULL, 0xA4506CEBDE82BDE9ULL,
  218. 0xBEF9A3F7B2C67915ULL, 0xC67178F2E372532BULL,
  219. 0xCA273ECEEA26619CULL, 0xD186B8C721C0C207ULL,
  220. 0xEADA7DD6CDE0EB1EULL, 0xF57D4F7FEE6ED178ULL,
  221. 0x06F067AA72176FBAULL, 0x0A637DC5A2C898A6ULL,
  222. 0x113F9804BEF90DAEULL, 0x1B710B35131C471BULL,
  223. 0x28DB77F523047D84ULL, 0x32CAAB7B40C72493ULL,
  224. 0x3C9EBE0A15C9BEBCULL, 0x431D67C49C100D4CULL,
  225. 0x4CC5D4BECB3E42B6ULL, 0x597F299CFC657E2AULL,
  226. 0x5FCB6FAB3AD6FAECULL, 0x6C44198C4A475817ULL,
  227. };
  228. static void
  229. transform512(uint64_t state[8], uint64_t data[16])
  230. {
  231. const uint64_t *K = SHA512_K;
  232. uint64_t W[16];
  233. uint64_t T[8];
  234. unsigned int j;
  235. /* ensure big-endian integers */
  236. if (!isbigendian())
  237. for (j = 0; j < 16; j++)
  238. data[j] = bswap64(data[j]);
  239. /* Copy state[] to working vars. */
  240. memcpy(T, state, sizeof(T));
  241. /* 80 operations, partially loop unrolled */
  242. for (j = 0; j < 80; j+= 16) {
  243. R( 0); R( 1); R( 2); R( 3);
  244. R( 4); R( 5); R( 6); R( 7);
  245. R( 8); R( 9); R(10); R(11);
  246. R(12); R(13); R(14); R(15);
  247. }
  248. /* Add the working vars back into state[]. */
  249. state[0] += a(0);
  250. state[1] += b(0);
  251. state[2] += c(0);
  252. state[3] += d(0);
  253. state[4] += e(0);
  254. state[5] += f(0);
  255. state[6] += g(0);
  256. state[7] += h(0);
  257. }
  258. #undef S0
  259. #undef S1
  260. #undef s0
  261. #undef s1
  262. void fz_sha512_init(fz_sha512 *context)
  263. {
  264. context->count[0] = context->count[1] = 0;
  265. context->state[0] = 0x6A09E667F3BCC908ull;
  266. context->state[1] = 0xBB67AE8584CAA73Bull;
  267. context->state[2] = 0x3C6EF372FE94F82Bull;
  268. context->state[3] = 0xA54FF53A5F1D36F1ull;
  269. context->state[4] = 0x510E527FADE682D1ull;
  270. context->state[5] = 0x9B05688C2B3E6C1Full;
  271. context->state[6] = 0x1F83D9ABFB41BD6Bull;
  272. context->state[7] = 0x5BE0CD19137E2179ull;
  273. }
  274. void fz_sha512_update(fz_sha512 *context, const unsigned char *input, size_t inlen)
  275. {
  276. /* Copy the input data into a properly aligned temporary buffer.
  277. * This way we can be called with arbitrarily sized buffers
  278. * (no need to be multiple of 128 bytes), and the code works also
  279. * on architectures that don't allow unaligned memory access. */
  280. while (inlen > 0)
  281. {
  282. const unsigned int copy_start = context->count[0] & 0x7F;
  283. unsigned int copy_size = 128 - copy_start;
  284. if (copy_size > inlen)
  285. copy_size = (unsigned int)inlen;
  286. memcpy(context->buffer.u8 + copy_start, input, copy_size);
  287. input += copy_size;
  288. inlen -= copy_size;
  289. context->count[0] += copy_size;
  290. /* carry overflow from low to high */
  291. if (context->count[0] < copy_size)
  292. context->count[1]++;
  293. if ((context->count[0] & 0x7F) == 0)
  294. transform512(context->state, context->buffer.u64);
  295. }
  296. }
  297. void fz_sha512_final(fz_sha512 *context, unsigned char digest[64])
  298. {
  299. /* Add padding as described in RFC 3174 (it describes SHA-1 but
  300. * the same padding style is used for SHA-512 too). */
  301. unsigned int j = context->count[0] & 0x7F;
  302. context->buffer.u8[j++] = 0x80;
  303. while (j != 112)
  304. {
  305. if (j == 128)
  306. {
  307. transform512(context->state, context->buffer.u64);
  308. j = 0;
  309. }
  310. context->buffer.u8[j++] = 0x00;
  311. }
  312. /* Convert the message size from bytes to bits. */
  313. context->count[1] = (context->count[1] << 3) + (context->count[0] >> 29);
  314. context->count[0] = context->count[0] << 3;
  315. if (!isbigendian())
  316. {
  317. context->buffer.u64[14] = bswap64(context->count[1]);
  318. context->buffer.u64[15] = bswap64(context->count[0]);
  319. }
  320. else
  321. {
  322. context->buffer.u64[14] = context->count[1];
  323. context->buffer.u64[15] = context->count[0];
  324. }
  325. transform512(context->state, context->buffer.u64);
  326. if (!isbigendian())
  327. for (j = 0; j < 8; j++)
  328. context->state[j] = bswap64(context->state[j]);
  329. memcpy(digest, &context->state[0], 64);
  330. memset(context, 0, sizeof(fz_sha512));
  331. }
  332. void fz_sha384_init(fz_sha384 *context)
  333. {
  334. context->count[0] = context->count[1] = 0;
  335. context->state[0] = 0xCBBB9D5DC1059ED8ull;
  336. context->state[1] = 0x629A292A367CD507ull;
  337. context->state[2] = 0x9159015A3070DD17ull;
  338. context->state[3] = 0x152FECD8F70E5939ull;
  339. context->state[4] = 0x67332667FFC00B31ull;
  340. context->state[5] = 0x8EB44A8768581511ull;
  341. context->state[6] = 0xDB0C2E0D64F98FA7ull;
  342. context->state[7] = 0x47B5481DBEFA4FA4ull;
  343. }
  344. void fz_sha384_update(fz_sha384 *context, const unsigned char *input, size_t inlen)
  345. {
  346. fz_sha512_update(context, input, inlen);
  347. }
  348. void fz_sha384_final(fz_sha384 *context, unsigned char digest[64])
  349. {
  350. fz_sha512_final(context, digest);
  351. }