SHA512.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // This code is public domain, taken from a PD crypto source file on GitHub.
  2. #include "SHA512.hpp"
  3. #include "Utils.hpp"
  4. #include <algorithm>
  5. namespace ZeroTier {
  6. #ifndef ZT_HAVE_NATIVE_SHA512
  7. namespace {
  8. struct sha512_state {
  9. uint64_t length, state[8];
  10. unsigned long curlen;
  11. uint8_t buf[128];
  12. };
  13. static const uint64_t K[80] = { 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL,
  14. 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
  15. 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL,
  16. 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
  17. 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL,
  18. 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
  19. 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL,
  20. 0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
  21. 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL };
  22. #define STORE64H(x, y) Utils::storeBigEndian<uint64_t>(y, x)
  23. #define LOAD64H(x, y) x = Utils::loadBigEndian<uint64_t>(y)
  24. #define ROL64c(x, y) (((x) << (y)) | ((x) >> (64 - (y))))
  25. #define ROR64c(x, y) (((x) >> (y)) | ((x) << (64 - (y))))
  26. #define Ch(x, y, z) (z ^ (x & (y ^ z)))
  27. #define Maj(x, y, z) (((x | y) & z) | (x & y))
  28. #define S(x, n) ROR64c(x, n)
  29. #define R(x, n) ((x) >> (n))
  30. #define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
  31. #define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
  32. #define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
  33. #define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
  34. static ZT_INLINE void sha512_compress(sha512_state* const md, uint8_t* const buf)
  35. {
  36. uint64_t S[8], W[80], t0, t1;
  37. int i;
  38. for (i = 0; i < 8; i++) {
  39. S[i] = md->state[i];
  40. }
  41. for (i = 0; i < 16; i++) {
  42. LOAD64H(W[i], buf + (8 * i));
  43. }
  44. for (i = 16; i < 80; i++) {
  45. W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
  46. }
  47. #define RND(a, b, c, d, e, f, g, h, i) \
  48. t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
  49. t1 = Sigma0(a) + Maj(a, b, c); \
  50. d += t0; \
  51. h = t0 + t1;
  52. for (i = 0; i < 80; i += 8) {
  53. RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i + 0);
  54. RND(S[7], S[0], S[1], S[2], S[3], S[4], S[5], S[6], i + 1);
  55. RND(S[6], S[7], S[0], S[1], S[2], S[3], S[4], S[5], i + 2);
  56. RND(S[5], S[6], S[7], S[0], S[1], S[2], S[3], S[4], i + 3);
  57. RND(S[4], S[5], S[6], S[7], S[0], S[1], S[2], S[3], i + 4);
  58. RND(S[3], S[4], S[5], S[6], S[7], S[0], S[1], S[2], i + 5);
  59. RND(S[2], S[3], S[4], S[5], S[6], S[7], S[0], S[1], i + 6);
  60. RND(S[1], S[2], S[3], S[4], S[5], S[6], S[7], S[0], i + 7);
  61. }
  62. for (i = 0; i < 8; i++) {
  63. md->state[i] = md->state[i] + S[i];
  64. }
  65. }
  66. static ZT_INLINE void sha384_init(sha512_state* const md)
  67. {
  68. md->curlen = 0;
  69. md->length = 0;
  70. md->state[0] = 0xcbbb9d5dc1059ed8ULL;
  71. md->state[1] = 0x629a292a367cd507ULL;
  72. md->state[2] = 0x9159015a3070dd17ULL;
  73. md->state[3] = 0x152fecd8f70e5939ULL;
  74. md->state[4] = 0x67332667ffc00b31ULL;
  75. md->state[5] = 0x8eb44a8768581511ULL;
  76. md->state[6] = 0xdb0c2e0d64f98fa7ULL;
  77. md->state[7] = 0x47b5481dbefa4fa4ULL;
  78. }
  79. static ZT_INLINE void sha512_init(sha512_state* const md)
  80. {
  81. md->curlen = 0;
  82. md->length = 0;
  83. md->state[0] = 0x6a09e667f3bcc908ULL;
  84. md->state[1] = 0xbb67ae8584caa73bULL;
  85. md->state[2] = 0x3c6ef372fe94f82bULL;
  86. md->state[3] = 0xa54ff53a5f1d36f1ULL;
  87. md->state[4] = 0x510e527fade682d1ULL;
  88. md->state[5] = 0x9b05688c2b3e6c1fULL;
  89. md->state[6] = 0x1f83d9abfb41bd6bULL;
  90. md->state[7] = 0x5be0cd19137e2179ULL;
  91. }
  92. static void sha512_process(sha512_state* const md, const uint8_t* in, unsigned long inlen)
  93. {
  94. while (inlen > 0) {
  95. if (md->curlen == 0 && inlen >= 128) {
  96. sha512_compress(md, (uint8_t*)in);
  97. md->length += 128 * 8;
  98. in += 128;
  99. inlen -= 128;
  100. }
  101. else {
  102. unsigned long n = std::min(inlen, (128 - md->curlen));
  103. Utils::copy(md->buf + md->curlen, in, n);
  104. md->curlen += n;
  105. in += n;
  106. inlen -= n;
  107. if (md->curlen == 128) {
  108. sha512_compress(md, md->buf);
  109. md->length += 8 * 128;
  110. md->curlen = 0;
  111. }
  112. }
  113. }
  114. }
  115. static ZT_INLINE void sha512_done(sha512_state* const md, uint8_t* out)
  116. {
  117. int i;
  118. md->length += md->curlen * 8ULL;
  119. md->buf[md->curlen++] = (uint8_t)0x80;
  120. if (md->curlen > 112) {
  121. while (md->curlen < 128) {
  122. md->buf[md->curlen++] = (uint8_t)0;
  123. }
  124. sha512_compress(md, md->buf);
  125. md->curlen = 0;
  126. }
  127. while (md->curlen < 120) {
  128. md->buf[md->curlen++] = (uint8_t)0;
  129. }
  130. STORE64H(md->length, md->buf + 120);
  131. sha512_compress(md, md->buf);
  132. for (i = 0; i < 8; i++) {
  133. STORE64H(md->state[i], out + (8 * i));
  134. }
  135. }
  136. } // anonymous namespace
  137. void SHA512(void* digest, const void* data, unsigned int len)
  138. {
  139. sha512_state state;
  140. sha512_init(&state);
  141. sha512_process(&state, (uint8_t*)data, (unsigned long)len);
  142. sha512_done(&state, (uint8_t*)digest);
  143. }
  144. void SHA384(void* digest, const void* data, unsigned int len)
  145. {
  146. uint8_t tmp[64];
  147. sha512_state state;
  148. sha384_init(&state);
  149. sha512_process(&state, (uint8_t*)data, (unsigned long)len);
  150. sha512_done(&state, tmp);
  151. Utils::copy<48>(digest, tmp);
  152. }
  153. void SHA384(void* digest, const void* data0, unsigned int len0, const void* data1, unsigned int len1)
  154. {
  155. uint8_t tmp[64];
  156. sha512_state state;
  157. sha384_init(&state);
  158. sha512_process(&state, (uint8_t*)data0, (unsigned long)len0);
  159. sha512_process(&state, (uint8_t*)data1, (unsigned long)len1);
  160. sha512_done(&state, tmp);
  161. Utils::copy<48>(digest, tmp);
  162. }
  163. #endif // !ZT_HAVE_NATIVE_SHA512
  164. void HMACSHA384(const uint8_t key[ZT_SYMMETRIC_KEY_SIZE], const void* msg, const unsigned int msglen, uint8_t mac[48])
  165. {
  166. uint64_t kInPadded[16]; // input padded key
  167. uint64_t outer[22]; // output padded key | H(input padded key | msg)
  168. const uint64_t k0 = Utils::loadMachineEndian<uint64_t>(key);
  169. const uint64_t k1 = Utils::loadMachineEndian<uint64_t>(key + 8);
  170. const uint64_t k2 = Utils::loadMachineEndian<uint64_t>(key + 16);
  171. const uint64_t k3 = Utils::loadMachineEndian<uint64_t>(key + 24);
  172. const uint64_t k4 = Utils::loadMachineEndian<uint64_t>(key + 32);
  173. const uint64_t k5 = Utils::loadMachineEndian<uint64_t>(key + 40);
  174. const uint64_t ipad = 0x3636363636363636ULL;
  175. kInPadded[0] = k0 ^ ipad;
  176. kInPadded[1] = k1 ^ ipad;
  177. kInPadded[2] = k2 ^ ipad;
  178. kInPadded[3] = k3 ^ ipad;
  179. kInPadded[4] = k4 ^ ipad;
  180. kInPadded[5] = k5 ^ ipad;
  181. kInPadded[6] = ipad;
  182. kInPadded[7] = ipad;
  183. kInPadded[8] = ipad;
  184. kInPadded[9] = ipad;
  185. kInPadded[10] = ipad;
  186. kInPadded[11] = ipad;
  187. kInPadded[12] = ipad;
  188. kInPadded[13] = ipad;
  189. kInPadded[14] = ipad;
  190. kInPadded[15] = ipad;
  191. const uint64_t opad = 0x5c5c5c5c5c5c5c5cULL;
  192. outer[0] = k0 ^ opad;
  193. outer[1] = k1 ^ opad;
  194. outer[2] = k2 ^ opad;
  195. outer[3] = k3 ^ opad;
  196. outer[4] = k4 ^ opad;
  197. outer[5] = k5 ^ opad;
  198. outer[6] = opad;
  199. outer[7] = opad;
  200. outer[8] = opad;
  201. outer[9] = opad;
  202. outer[10] = opad;
  203. outer[11] = opad;
  204. outer[12] = opad;
  205. outer[13] = opad;
  206. outer[14] = opad;
  207. outer[15] = opad;
  208. // H(output padded key | H(input padded key | msg))
  209. SHA384(reinterpret_cast<uint8_t*>(outer) + 128, kInPadded, 128, msg, msglen);
  210. SHA384(mac, outer, 176);
  211. }
  212. void KBKDFHMACSHA384(const uint8_t key[ZT_SYMMETRIC_KEY_SIZE], const char label, const char context, const uint32_t iter, uint8_t out[ZT_SYMMETRIC_KEY_SIZE])
  213. {
  214. uint8_t kbkdfMsg[13];
  215. Utils::storeBigEndian<uint32_t>(kbkdfMsg, (uint32_t)iter);
  216. kbkdfMsg[4] = (uint8_t)'Z';
  217. kbkdfMsg[5] = (uint8_t)'T'; // preface our labels with something ZT-specific
  218. kbkdfMsg[6] = (uint8_t)label;
  219. kbkdfMsg[7] = 0;
  220. kbkdfMsg[8] = (uint8_t)context;
  221. // Output key length: 384 bits (as 32-bit big-endian value)
  222. kbkdfMsg[9] = 0;
  223. kbkdfMsg[10] = 0;
  224. kbkdfMsg[11] = 0x01;
  225. kbkdfMsg[12] = 0x80;
  226. static_assert(ZT_SYMMETRIC_KEY_SIZE == ZT_SHA384_DIGEST_SIZE, "sizeof(out) != ZT_SHA384_DIGEST_SIZE");
  227. HMACSHA384(key, &kbkdfMsg, sizeof(kbkdfMsg), out);
  228. }
  229. } // namespace ZeroTier
  230. // Internally re-export to included C code, which includes some fast crypto code ported in on some platforms.
  231. // This eliminates the need to link against a third party SHA512() from this code
  232. extern "C" void ZT_sha512internal(void* digest, const void* data, unsigned int len)
  233. {
  234. ZeroTier::SHA512(digest, data, len);
  235. }