sha256.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* sha256.c - an implementation of SHA-256/224 hash functions
  2. * based on FIPS 180-3 (Federal Information Processing Standart).
  3. *
  4. * Copyright: 2010-2012 Aleksey Kravchenko <rhash.admin@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk!
  16. */
  17. #include <string.h>
  18. #include "byte_order.h"
  19. #include "sha256.h"
  20. /* SHA-224 and SHA-256 constants for 64 rounds. These words represent
  21. * the first 32 bits of the fractional parts of the cube
  22. * roots of the first 64 prime numbers. */
  23. static const unsigned rhash_k256[64] = {
  24. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
  25. 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  26. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
  27. 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  28. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
  29. 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  30. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
  31. 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  32. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
  33. 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  34. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  35. };
  36. /* The SHA256/224 functions defined by FIPS 180-3, 4.1.2 */
  37. /* Optimized version of Ch(x,y,z)=((x & y) | (~x & z)) */
  38. #define Ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  39. /* Optimized version of Maj(x,y,z)=((x & y) ^ (x & z) ^ (y & z)) */
  40. #define Maj(x,y,z) (((x) & (y)) ^ ((z) & ((x) ^ (y))))
  41. #define Sigma0(x) (ROTR32((x), 2) ^ ROTR32((x), 13) ^ ROTR32((x), 22))
  42. #define Sigma1(x) (ROTR32((x), 6) ^ ROTR32((x), 11) ^ ROTR32((x), 25))
  43. #define sigma0(x) (ROTR32((x), 7) ^ ROTR32((x), 18) ^ ((x) >> 3))
  44. #define sigma1(x) (ROTR32((x),17) ^ ROTR32((x), 19) ^ ((x) >> 10))
  45. /* Recalculate element n-th of circular buffer W using formula
  46. * W[n] = sigma1(W[n - 2]) + W[n - 7] + sigma0(W[n - 15]) + W[n - 16]; */
  47. #define RECALCULATE_W(W,n) (W[n] += \
  48. (sigma1(W[(n - 2) & 15]) + W[(n - 7) & 15] + sigma0(W[(n - 15) & 15])))
  49. #define ROUND(a,b,c,d,e,f,g,h,k,data) { \
  50. unsigned T1 = h + Sigma1(e) + Ch(e,f,g) + k + (data); \
  51. d += T1, h = T1 + Sigma0(a) + Maj(a,b,c); }
  52. #define ROUND_1_16(a,b,c,d,e,f,g,h,n) \
  53. ROUND(a,b,c,d,e,f,g,h, rhash_k256[n], W[n] = be2me_32(block[n]))
  54. #define ROUND_17_64(a,b,c,d,e,f,g,h,n) \
  55. ROUND(a,b,c,d,e,f,g,h, k[n], RECALCULATE_W(W, n))
  56. /**
  57. * Initialize context before calculaing hash.
  58. *
  59. * @param ctx context to initialize
  60. */
  61. void rhash_sha256_init(sha256_ctx *ctx)
  62. {
  63. /* Initial values. These words were obtained by taking the first 32
  64. * bits of the fractional parts of the square roots of the first
  65. * eight prime numbers. */
  66. static const unsigned SHA256_H0[8] = {
  67. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  68. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  69. };
  70. ctx->length = 0;
  71. ctx->digest_length = sha256_hash_size;
  72. /* initialize algorithm state */
  73. memcpy(ctx->hash, SHA256_H0, sizeof(ctx->hash));
  74. }
  75. /**
  76. * Initialize context before calculaing hash.
  77. *
  78. * @param ctx context to initialize
  79. */
  80. void rhash_sha224_init(struct sha256_ctx *ctx)
  81. {
  82. /* Initial values from FIPS 180-3. These words were obtained by taking
  83. * bits from 33th to 64th of the fractional parts of the square
  84. * roots of ninth through sixteenth prime numbers. */
  85. static const unsigned SHA224_H0[8] = {
  86. 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
  87. 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
  88. };
  89. ctx->length = 0;
  90. ctx->digest_length = sha224_hash_size;
  91. memcpy(ctx->hash, SHA224_H0, sizeof(ctx->hash));
  92. }
  93. /**
  94. * The core transformation. Process a 512-bit block.
  95. *
  96. * @param hash algorithm state
  97. * @param block the message block to process
  98. */
  99. static void rhash_sha256_process_block(unsigned hash[8], unsigned block[16])
  100. {
  101. unsigned A, B, C, D, E, F, G, H;
  102. unsigned W[16];
  103. const unsigned *k;
  104. int i;
  105. A = hash[0], B = hash[1], C = hash[2], D = hash[3];
  106. E = hash[4], F = hash[5], G = hash[6], H = hash[7];
  107. /* Compute SHA using alternate Method: FIPS 180-3 6.1.3 */
  108. ROUND_1_16(A, B, C, D, E, F, G, H, 0);
  109. ROUND_1_16(H, A, B, C, D, E, F, G, 1);
  110. ROUND_1_16(G, H, A, B, C, D, E, F, 2);
  111. ROUND_1_16(F, G, H, A, B, C, D, E, 3);
  112. ROUND_1_16(E, F, G, H, A, B, C, D, 4);
  113. ROUND_1_16(D, E, F, G, H, A, B, C, 5);
  114. ROUND_1_16(C, D, E, F, G, H, A, B, 6);
  115. ROUND_1_16(B, C, D, E, F, G, H, A, 7);
  116. ROUND_1_16(A, B, C, D, E, F, G, H, 8);
  117. ROUND_1_16(H, A, B, C, D, E, F, G, 9);
  118. ROUND_1_16(G, H, A, B, C, D, E, F, 10);
  119. ROUND_1_16(F, G, H, A, B, C, D, E, 11);
  120. ROUND_1_16(E, F, G, H, A, B, C, D, 12);
  121. ROUND_1_16(D, E, F, G, H, A, B, C, 13);
  122. ROUND_1_16(C, D, E, F, G, H, A, B, 14);
  123. ROUND_1_16(B, C, D, E, F, G, H, A, 15);
  124. for (i = 16, k = &rhash_k256[16]; i < 64; i += 16, k += 16) {
  125. ROUND_17_64(A, B, C, D, E, F, G, H, 0);
  126. ROUND_17_64(H, A, B, C, D, E, F, G, 1);
  127. ROUND_17_64(G, H, A, B, C, D, E, F, 2);
  128. ROUND_17_64(F, G, H, A, B, C, D, E, 3);
  129. ROUND_17_64(E, F, G, H, A, B, C, D, 4);
  130. ROUND_17_64(D, E, F, G, H, A, B, C, 5);
  131. ROUND_17_64(C, D, E, F, G, H, A, B, 6);
  132. ROUND_17_64(B, C, D, E, F, G, H, A, 7);
  133. ROUND_17_64(A, B, C, D, E, F, G, H, 8);
  134. ROUND_17_64(H, A, B, C, D, E, F, G, 9);
  135. ROUND_17_64(G, H, A, B, C, D, E, F, 10);
  136. ROUND_17_64(F, G, H, A, B, C, D, E, 11);
  137. ROUND_17_64(E, F, G, H, A, B, C, D, 12);
  138. ROUND_17_64(D, E, F, G, H, A, B, C, 13);
  139. ROUND_17_64(C, D, E, F, G, H, A, B, 14);
  140. ROUND_17_64(B, C, D, E, F, G, H, A, 15);
  141. }
  142. hash[0] += A, hash[1] += B, hash[2] += C, hash[3] += D;
  143. hash[4] += E, hash[5] += F, hash[6] += G, hash[7] += H;
  144. }
  145. /**
  146. * Calculate message hash.
  147. * Can be called repeatedly with chunks of the message to be hashed.
  148. *
  149. * @param ctx the algorithm context containing current hashing state
  150. * @param msg message chunk
  151. * @param size length of the message chunk
  152. */
  153. void rhash_sha256_update(sha256_ctx *ctx, const unsigned char *msg, size_t size)
  154. {
  155. size_t index = (size_t)ctx->length & 63;
  156. ctx->length += size;
  157. /* fill partial block */
  158. if (index) {
  159. size_t left = sha256_block_size - index;
  160. memcpy((char*)ctx->message + index, msg, (size < left ? size : left));
  161. if (size < left) return;
  162. /* process partial block */
  163. rhash_sha256_process_block(ctx->hash, (unsigned*)ctx->message);
  164. msg += left;
  165. size -= left;
  166. }
  167. while (size >= sha256_block_size) {
  168. unsigned* aligned_message_block;
  169. if (IS_ALIGNED_32(msg)) {
  170. /* the most common case is processing of an already aligned message
  171. without copying it */
  172. aligned_message_block = (unsigned*)msg;
  173. } else {
  174. memcpy(ctx->message, msg, sha256_block_size);
  175. aligned_message_block = (unsigned*)ctx->message;
  176. }
  177. rhash_sha256_process_block(ctx->hash, aligned_message_block);
  178. msg += sha256_block_size;
  179. size -= sha256_block_size;
  180. }
  181. if (size) {
  182. memcpy(ctx->message, msg, size); /* save leftovers */
  183. }
  184. }
  185. /**
  186. * Store calculated hash into the given array.
  187. *
  188. * @param ctx the algorithm context containing current hashing state
  189. * @param result calculated hash in binary form
  190. */
  191. void rhash_sha256_final(sha256_ctx *ctx, unsigned char* result)
  192. {
  193. size_t index = ((unsigned)ctx->length & 63) >> 2;
  194. unsigned shift = ((unsigned)ctx->length & 3) * 8;
  195. /* pad message and run for last block */
  196. /* append the byte 0x80 to the message */
  197. ctx->message[index] &= le2me_32(~(0xFFFFFFFFu << shift));
  198. ctx->message[index++] ^= le2me_32(0x80u << shift);
  199. /* if no room left in the message to store 64-bit message length */
  200. if (index > 14) {
  201. /* then fill the rest with zeros and process it */
  202. while (index < 16) {
  203. ctx->message[index++] = 0;
  204. }
  205. rhash_sha256_process_block(ctx->hash, ctx->message);
  206. index = 0;
  207. }
  208. while (index < 14) {
  209. ctx->message[index++] = 0;
  210. }
  211. ctx->message[14] = be2me_32( (unsigned)(ctx->length >> 29) );
  212. ctx->message[15] = be2me_32( (unsigned)(ctx->length << 3) );
  213. rhash_sha256_process_block(ctx->hash, ctx->message);
  214. if (result) be32_copy(result, 0, ctx->hash, ctx->digest_length);
  215. }