sha512.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* sha512.c - an implementation of SHA-384/512 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 "sha512.h"
  20. /* SHA-384 and SHA-512 constants for 80 rounds. These qwords represent
  21. * the first 64 bits of the fractional parts of the cube
  22. * roots of the first 80 prime numbers. */
  23. static const uint64_t rhash_k512[80] = {
  24. I64(0x428a2f98d728ae22), I64(0x7137449123ef65cd), I64(0xb5c0fbcfec4d3b2f),
  25. I64(0xe9b5dba58189dbbc), I64(0x3956c25bf348b538), I64(0x59f111f1b605d019),
  26. I64(0x923f82a4af194f9b), I64(0xab1c5ed5da6d8118), I64(0xd807aa98a3030242),
  27. I64(0x12835b0145706fbe), I64(0x243185be4ee4b28c), I64(0x550c7dc3d5ffb4e2),
  28. I64(0x72be5d74f27b896f), I64(0x80deb1fe3b1696b1), I64(0x9bdc06a725c71235),
  29. I64(0xc19bf174cf692694), I64(0xe49b69c19ef14ad2), I64(0xefbe4786384f25e3),
  30. I64(0x0fc19dc68b8cd5b5), I64(0x240ca1cc77ac9c65), I64(0x2de92c6f592b0275),
  31. I64(0x4a7484aa6ea6e483), I64(0x5cb0a9dcbd41fbd4), I64(0x76f988da831153b5),
  32. I64(0x983e5152ee66dfab), I64(0xa831c66d2db43210), I64(0xb00327c898fb213f),
  33. I64(0xbf597fc7beef0ee4), I64(0xc6e00bf33da88fc2), I64(0xd5a79147930aa725),
  34. I64(0x06ca6351e003826f), I64(0x142929670a0e6e70), I64(0x27b70a8546d22ffc),
  35. I64(0x2e1b21385c26c926), I64(0x4d2c6dfc5ac42aed), I64(0x53380d139d95b3df),
  36. I64(0x650a73548baf63de), I64(0x766a0abb3c77b2a8), I64(0x81c2c92e47edaee6),
  37. I64(0x92722c851482353b), I64(0xa2bfe8a14cf10364), I64(0xa81a664bbc423001),
  38. I64(0xc24b8b70d0f89791), I64(0xc76c51a30654be30), I64(0xd192e819d6ef5218),
  39. I64(0xd69906245565a910), I64(0xf40e35855771202a), I64(0x106aa07032bbd1b8),
  40. I64(0x19a4c116b8d2d0c8), I64(0x1e376c085141ab53), I64(0x2748774cdf8eeb99),
  41. I64(0x34b0bcb5e19b48a8), I64(0x391c0cb3c5c95a63), I64(0x4ed8aa4ae3418acb),
  42. I64(0x5b9cca4f7763e373), I64(0x682e6ff3d6b2b8a3), I64(0x748f82ee5defb2fc),
  43. I64(0x78a5636f43172f60), I64(0x84c87814a1f0ab72), I64(0x8cc702081a6439ec),
  44. I64(0x90befffa23631e28), I64(0xa4506cebde82bde9), I64(0xbef9a3f7b2c67915),
  45. I64(0xc67178f2e372532b), I64(0xca273eceea26619c), I64(0xd186b8c721c0c207),
  46. I64(0xeada7dd6cde0eb1e), I64(0xf57d4f7fee6ed178), I64(0x06f067aa72176fba),
  47. I64(0x0a637dc5a2c898a6), I64(0x113f9804bef90dae), I64(0x1b710b35131c471b),
  48. I64(0x28db77f523047d84), I64(0x32caab7b40c72493), I64(0x3c9ebe0a15c9bebc),
  49. I64(0x431d67c49c100d4c), I64(0x4cc5d4becb3e42b6), I64(0x597f299cfc657e2a),
  50. I64(0x5fcb6fab3ad6faec), I64(0x6c44198c4a475817)
  51. };
  52. /* The SHA512/384 functions defined by FIPS 180-3, 4.1.3 */
  53. /* Optimized version of Ch(x,y,z)=((x & y) | (~x & z)) */
  54. #define Ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  55. /* Optimized version of Maj(x,y,z)=((x & y) ^ (x & z) ^ (y & z)) */
  56. #define Maj(x,y,z) (((x) & (y)) ^ ((z) & ((x) ^ (y))))
  57. #define Sigma0(x) (ROTR64((x), 28) ^ ROTR64((x), 34) ^ ROTR64((x), 39))
  58. #define Sigma1(x) (ROTR64((x), 14) ^ ROTR64((x), 18) ^ ROTR64((x), 41))
  59. #define sigma0(x) (ROTR64((x), 1) ^ ROTR64((x), 8) ^ ((x) >> 7))
  60. #define sigma1(x) (ROTR64((x), 19) ^ ROTR64((x), 61) ^ ((x) >> 6))
  61. /* Recalculate element n-th of circular buffer W using formula
  62. * W[n] = sigma1(W[n - 2]) + W[n - 7] + sigma0(W[n - 15]) + W[n - 16]; */
  63. #define RECALCULATE_W(W,n) (W[n] += \
  64. (sigma1(W[(n - 2) & 15]) + W[(n - 7) & 15] + sigma0(W[(n - 15) & 15])))
  65. #define ROUND(a,b,c,d,e,f,g,h,k,data) { \
  66. uint64_t T1 = h + Sigma1(e) + Ch(e,f,g) + k + (data); \
  67. d += T1, h = T1 + Sigma0(a) + Maj(a,b,c); }
  68. #define ROUND_1_16(a,b,c,d,e,f,g,h,n) \
  69. ROUND(a,b,c,d,e,f,g,h, rhash_k512[n], W[n] = be2me_64(block[n]))
  70. #define ROUND_17_80(a,b,c,d,e,f,g,h,n) \
  71. ROUND(a,b,c,d,e,f,g,h, k[n], RECALCULATE_W(W, n))
  72. /**
  73. * Initialize context before calculating hash.
  74. *
  75. * @param ctx context to initialize
  76. */
  77. void rhash_sha512_init(sha512_ctx *ctx)
  78. {
  79. /* Initial values. These words were obtained by taking the first 32
  80. * bits of the fractional parts of the square roots of the first
  81. * eight prime numbers. */
  82. static const uint64_t SHA512_H0[8] = {
  83. I64(0x6a09e667f3bcc908), I64(0xbb67ae8584caa73b), I64(0x3c6ef372fe94f82b),
  84. I64(0xa54ff53a5f1d36f1), I64(0x510e527fade682d1), I64(0x9b05688c2b3e6c1f),
  85. I64(0x1f83d9abfb41bd6b), I64(0x5be0cd19137e2179)
  86. };
  87. ctx->length = 0;
  88. ctx->digest_length = sha512_hash_size;
  89. /* initialize algorithm state */
  90. memcpy(ctx->hash, SHA512_H0, sizeof(ctx->hash));
  91. }
  92. /**
  93. * Initialize context before calculaing hash.
  94. *
  95. * @param ctx context to initialize
  96. */
  97. void rhash_sha384_init(struct sha512_ctx *ctx)
  98. {
  99. /* Initial values from FIPS 180-3. These words were obtained by taking
  100. * the first sixty-four bits of the fractional parts of the square
  101. * roots of ninth through sixteenth prime numbers. */
  102. static const uint64_t SHA384_H0[8] = {
  103. I64(0xcbbb9d5dc1059ed8), I64(0x629a292a367cd507), I64(0x9159015a3070dd17),
  104. I64(0x152fecd8f70e5939), I64(0x67332667ffc00b31), I64(0x8eb44a8768581511),
  105. I64(0xdb0c2e0d64f98fa7), I64(0x47b5481dbefa4fa4)
  106. };
  107. ctx->length = 0;
  108. ctx->digest_length = sha384_hash_size;
  109. memcpy(ctx->hash, SHA384_H0, sizeof(ctx->hash));
  110. }
  111. /**
  112. * The core transformation. Process a 512-bit block.
  113. *
  114. * @param hash algorithm state
  115. * @param block the message block to process
  116. */
  117. static void rhash_sha512_process_block(uint64_t hash[8], uint64_t block[16])
  118. {
  119. uint64_t A, B, C, D, E, F, G, H;
  120. uint64_t W[16];
  121. const uint64_t *k;
  122. int i;
  123. A = hash[0], B = hash[1], C = hash[2], D = hash[3];
  124. E = hash[4], F = hash[5], G = hash[6], H = hash[7];
  125. /* Compute SHA using alternate Method: FIPS 180-3 6.1.3 */
  126. ROUND_1_16(A, B, C, D, E, F, G, H, 0);
  127. ROUND_1_16(H, A, B, C, D, E, F, G, 1);
  128. ROUND_1_16(G, H, A, B, C, D, E, F, 2);
  129. ROUND_1_16(F, G, H, A, B, C, D, E, 3);
  130. ROUND_1_16(E, F, G, H, A, B, C, D, 4);
  131. ROUND_1_16(D, E, F, G, H, A, B, C, 5);
  132. ROUND_1_16(C, D, E, F, G, H, A, B, 6);
  133. ROUND_1_16(B, C, D, E, F, G, H, A, 7);
  134. ROUND_1_16(A, B, C, D, E, F, G, H, 8);
  135. ROUND_1_16(H, A, B, C, D, E, F, G, 9);
  136. ROUND_1_16(G, H, A, B, C, D, E, F, 10);
  137. ROUND_1_16(F, G, H, A, B, C, D, E, 11);
  138. ROUND_1_16(E, F, G, H, A, B, C, D, 12);
  139. ROUND_1_16(D, E, F, G, H, A, B, C, 13);
  140. ROUND_1_16(C, D, E, F, G, H, A, B, 14);
  141. ROUND_1_16(B, C, D, E, F, G, H, A, 15);
  142. for (i = 16, k = &rhash_k512[16]; i < 80; i += 16, k += 16) {
  143. ROUND_17_80(A, B, C, D, E, F, G, H, 0);
  144. ROUND_17_80(H, A, B, C, D, E, F, G, 1);
  145. ROUND_17_80(G, H, A, B, C, D, E, F, 2);
  146. ROUND_17_80(F, G, H, A, B, C, D, E, 3);
  147. ROUND_17_80(E, F, G, H, A, B, C, D, 4);
  148. ROUND_17_80(D, E, F, G, H, A, B, C, 5);
  149. ROUND_17_80(C, D, E, F, G, H, A, B, 6);
  150. ROUND_17_80(B, C, D, E, F, G, H, A, 7);
  151. ROUND_17_80(A, B, C, D, E, F, G, H, 8);
  152. ROUND_17_80(H, A, B, C, D, E, F, G, 9);
  153. ROUND_17_80(G, H, A, B, C, D, E, F, 10);
  154. ROUND_17_80(F, G, H, A, B, C, D, E, 11);
  155. ROUND_17_80(E, F, G, H, A, B, C, D, 12);
  156. ROUND_17_80(D, E, F, G, H, A, B, C, 13);
  157. ROUND_17_80(C, D, E, F, G, H, A, B, 14);
  158. ROUND_17_80(B, C, D, E, F, G, H, A, 15);
  159. }
  160. hash[0] += A, hash[1] += B, hash[2] += C, hash[3] += D;
  161. hash[4] += E, hash[5] += F, hash[6] += G, hash[7] += H;
  162. }
  163. /**
  164. * Calculate message hash.
  165. * Can be called repeatedly with chunks of the message to be hashed.
  166. *
  167. * @param ctx the algorithm context containing current hashing state
  168. * @param msg message chunk
  169. * @param size length of the message chunk
  170. */
  171. void rhash_sha512_update(sha512_ctx *ctx, const unsigned char *msg, size_t size)
  172. {
  173. size_t index = (size_t)ctx->length & 127;
  174. ctx->length += size;
  175. /* fill partial block */
  176. if (index) {
  177. size_t left = sha512_block_size - index;
  178. memcpy((char*)ctx->message + index, msg, (size < left ? size : left));
  179. if (size < left) return;
  180. /* process partial block */
  181. rhash_sha512_process_block(ctx->hash, ctx->message);
  182. msg += left;
  183. size -= left;
  184. }
  185. while (size >= sha512_block_size) {
  186. uint64_t* aligned_message_block;
  187. if (IS_ALIGNED_64(msg)) {
  188. /* the most common case is processing of an already aligned message
  189. without copying it */
  190. aligned_message_block = (uint64_t*)msg;
  191. } else {
  192. memcpy(ctx->message, msg, sha512_block_size);
  193. aligned_message_block = ctx->message;
  194. }
  195. rhash_sha512_process_block(ctx->hash, aligned_message_block);
  196. msg += sha512_block_size;
  197. size -= sha512_block_size;
  198. }
  199. if (size) {
  200. memcpy(ctx->message, msg, size); /* save leftovers */
  201. }
  202. }
  203. /**
  204. * Store calculated hash into the given array.
  205. *
  206. * @param ctx the algorithm context containing current hashing state
  207. * @param result calculated hash in binary form
  208. */
  209. void rhash_sha512_final(sha512_ctx *ctx, unsigned char* result)
  210. {
  211. size_t index = ((unsigned)ctx->length & 127) >> 3;
  212. unsigned shift = ((unsigned)ctx->length & 7) * 8;
  213. /* pad message and process the last block */
  214. /* append the byte 0x80 to the message */
  215. ctx->message[index] &= le2me_64( ~(I64(0xFFFFFFFFFFFFFFFF) << shift) );
  216. ctx->message[index++] ^= le2me_64( I64(0x80) << shift );
  217. /* if no room left in the message to store 128-bit message length */
  218. if (index >= 15) {
  219. if (index == 15) ctx->message[index] = 0;
  220. rhash_sha512_process_block(ctx->hash, ctx->message);
  221. index = 0;
  222. }
  223. while (index < 15) {
  224. ctx->message[index++] = 0;
  225. }
  226. ctx->message[15] = be2me_64(ctx->length << 3);
  227. rhash_sha512_process_block(ctx->hash, ctx->message);
  228. if (result) be64_copy(result, 0, ctx->hash, ctx->digest_length);
  229. }