sha512.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2015, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <string.h>
  31. #include "os_port.h"
  32. #include "crypto.h"
  33. #define SHR64(a, n) ((a) >> (n))
  34. #define ROR64(a, n) (((a) >> (n)) | ((a) << (64 - (n))))
  35. #define CH(x, y, z) (((x) & (y)) | (~(x) & (z)))
  36. #define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  37. #define SIGMA1(x) (ROR64(x, 28) ^ ROR64(x, 34) ^ ROR64(x, 39))
  38. #define SIGMA2(x) (ROR64(x, 14) ^ ROR64(x, 18) ^ ROR64(x, 41))
  39. #define SIGMA3(x) (ROR64(x, 1) ^ ROR64(x, 8) ^ SHR64(x, 7))
  40. #define SIGMA4(x) (ROR64(x, 19) ^ ROR64(x, 61) ^ SHR64(x, 6))
  41. #define MIN(x, y) ((x) < (y) ? x : y)
  42. static const uint8_t padding[128] =
  43. {
  44. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  52. };
  53. static const uint64_t k[80] =
  54. {
  55. 0x428A2F98D728AE22, 0x7137449123EF65CD, 0xB5C0FBCFEC4D3B2F, 0xE9B5DBA58189DBBC,
  56. 0x3956C25BF348B538, 0x59F111F1B605D019, 0x923F82A4AF194F9B, 0xAB1C5ED5DA6D8118,
  57. 0xD807AA98A3030242, 0x12835B0145706FBE, 0x243185BE4EE4B28C, 0x550C7DC3D5FFB4E2,
  58. 0x72BE5D74F27B896F, 0x80DEB1FE3B1696B1, 0x9BDC06A725C71235, 0xC19BF174CF692694,
  59. 0xE49B69C19EF14AD2, 0xEFBE4786384F25E3, 0x0FC19DC68B8CD5B5, 0x240CA1CC77AC9C65,
  60. 0x2DE92C6F592B0275, 0x4A7484AA6EA6E483, 0x5CB0A9DCBD41FBD4, 0x76F988DA831153B5,
  61. 0x983E5152EE66DFAB, 0xA831C66D2DB43210, 0xB00327C898FB213F, 0xBF597FC7BEEF0EE4,
  62. 0xC6E00BF33DA88FC2, 0xD5A79147930AA725, 0x06CA6351E003826F, 0x142929670A0E6E70,
  63. 0x27B70A8546D22FFC, 0x2E1B21385C26C926, 0x4D2C6DFC5AC42AED, 0x53380D139D95B3DF,
  64. 0x650A73548BAF63DE, 0x766A0ABB3C77B2A8, 0x81C2C92E47EDAEE6, 0x92722C851482353B,
  65. 0xA2BFE8A14CF10364, 0xA81A664BBC423001, 0xC24B8B70D0F89791, 0xC76C51A30654BE30,
  66. 0xD192E819D6EF5218, 0xD69906245565A910, 0xF40E35855771202A, 0x106AA07032BBD1B8,
  67. 0x19A4C116B8D2D0C8, 0x1E376C085141AB53, 0x2748774CDF8EEB99, 0x34B0BCB5E19B48A8,
  68. 0x391C0CB3C5C95A63, 0x4ED8AA4AE3418ACB, 0x5B9CCA4F7763E373, 0x682E6FF3D6B2B8A3,
  69. 0x748F82EE5DEFB2FC, 0x78A5636F43172F60, 0x84C87814A1F0AB72, 0x8CC702081A6439EC,
  70. 0x90BEFFFA23631E28, 0xA4506CEBDE82BDE9, 0xBEF9A3F7B2C67915, 0xC67178F2E372532B,
  71. 0xCA273ECEEA26619C, 0xD186B8C721C0C207, 0xEADA7DD6CDE0EB1E, 0xF57D4F7FEE6ED178,
  72. 0x06F067AA72176FBA, 0x0A637DC5A2C898A6, 0x113F9804BEF90DAE, 0x1B710B35131C471B,
  73. 0x28DB77F523047D84, 0x32CAAB7B40C72493, 0x3C9EBE0A15C9BEBC, 0x431D67C49C100D4C,
  74. 0x4CC5D4BECB3E42B6, 0x597F299CFC657E2A, 0x5FCB6FAB3AD6FAEC, 0x6C44198C4A475817
  75. };
  76. /**
  77. * Initialize the SHA512 context
  78. */
  79. void SHA512_Init(SHA512_CTX *ctx)
  80. {
  81. ctx->h_dig.h[0] = 0x6A09E667F3BCC908;
  82. ctx->h_dig.h[1] = 0xBB67AE8584CAA73B;
  83. ctx->h_dig.h[2] = 0x3C6EF372FE94F82B;
  84. ctx->h_dig.h[3] = 0xA54FF53A5F1D36F1;
  85. ctx->h_dig.h[4] = 0x510E527FADE682D1;
  86. ctx->h_dig.h[5] = 0x9B05688C2B3E6C1F;
  87. ctx->h_dig.h[6] = 0x1F83D9ABFB41BD6B;
  88. ctx->h_dig.h[7] = 0x5BE0CD19137E2179;
  89. ctx->size = 0;
  90. ctx->totalSize = 0;
  91. }
  92. static void SHA512_Process(SHA512_CTX *ctx)
  93. {
  94. int t;
  95. uint64_t temp1;
  96. uint64_t temp2;
  97. // Initialize the 8 working registers
  98. uint64_t a = ctx->h_dig.h[0];
  99. uint64_t b = ctx->h_dig.h[1];
  100. uint64_t c = ctx->h_dig.h[2];
  101. uint64_t d = ctx->h_dig.h[3];
  102. uint64_t e = ctx->h_dig.h[4];
  103. uint64_t f = ctx->h_dig.h[5];
  104. uint64_t g = ctx->h_dig.h[6];
  105. uint64_t h = ctx->h_dig.h[7];
  106. // Process message in 16-word blocks
  107. uint64_t *w = ctx->w_buf.w;
  108. // Convert from big-endian byte order to host byte order
  109. for (t = 0; t < 16; t++)
  110. w[t] = be64toh(w[t]);
  111. // Prepare the message schedule
  112. for (t = 16; t < 80; t++)
  113. w[t] = SIGMA4(w[t - 2]) + w[t - 7] + SIGMA3(w[t - 15]) + w[t - 16];
  114. // SHA-512 hash computation
  115. for (t = 0; t < 80; t++)
  116. {
  117. // Calculate T1 and T2
  118. temp1 = h + SIGMA2(e) + CH(e, f, g) + k[t] + w[t];
  119. temp2 = SIGMA1(a) + MAJ(a, b, c);
  120. // Update the working registers
  121. h = g;
  122. g = f;
  123. f = e;
  124. e = d + temp1;
  125. d = c;
  126. c = b;
  127. b = a;
  128. a = temp1 + temp2;
  129. }
  130. // Update the hash value
  131. ctx->h_dig.h[0] += a;
  132. ctx->h_dig.h[1] += b;
  133. ctx->h_dig.h[2] += c;
  134. ctx->h_dig.h[3] += d;
  135. ctx->h_dig.h[4] += e;
  136. ctx->h_dig.h[5] += f;
  137. ctx->h_dig.h[6] += g;
  138. ctx->h_dig.h[7] += h;
  139. }
  140. /**
  141. * Accepts an array of octets as the next portion of the message.
  142. */
  143. void SHA512_Update(SHA512_CTX *ctx, const uint8_t * msg, int len)
  144. {
  145. // Process the incoming data
  146. while (len > 0)
  147. {
  148. // The buffer can hold at most 128 bytes
  149. size_t n = MIN(len, 128 - ctx->size);
  150. // Copy the data to the buffer
  151. memcpy(ctx->w_buf.buffer + ctx->size, msg, n);
  152. // Update the SHA-512 ctx
  153. ctx->size += n;
  154. ctx->totalSize += n;
  155. // Advance the data pointer
  156. msg = (uint8_t *) msg + n;
  157. // Remaining bytes to process
  158. len -= n;
  159. // Process message in 16-word blocks
  160. if (ctx->size == 128)
  161. {
  162. // Transform the 16-word block
  163. SHA512_Process(ctx);
  164. // Empty the buffer
  165. ctx->size = 0;
  166. }
  167. }
  168. }
  169. /**
  170. * Return the 512-bit message digest into the user's array
  171. */
  172. void SHA512_Final(uint8_t *digest, SHA512_CTX *ctx)
  173. {
  174. int i;
  175. size_t paddingSize;
  176. uint64_t totalSize;
  177. // Length of the original message (before padding)
  178. totalSize = ctx->totalSize * 8;
  179. // Pad the message so that its length is congruent to 112 modulo 128
  180. paddingSize = (ctx->size < 112) ? (112 - ctx->size) :
  181. (128 + 112 - ctx->size);
  182. // Append padding
  183. SHA512_Update(ctx, padding, paddingSize);
  184. // Append the length of the original message
  185. ctx->w_buf.w[14] = 0;
  186. ctx->w_buf.w[15] = be64toh(totalSize);
  187. // Calculate the message digest
  188. SHA512_Process(ctx);
  189. // Convert from host byte order to big-endian byte order
  190. for (i = 0; i < 8; i++)
  191. ctx->h_dig.h[i] = be64toh(ctx->h_dig.h[i]);
  192. // Copy the resulting digest
  193. if (digest != NULL)
  194. memcpy(digest, ctx->h_dig.digest, SHA512_SIZE);
  195. }