sha256.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include <stdlib.h>
  13. #include "sha256.h"
  14. #include "sdk_errors.h"
  15. #include "sdk_common.h"
  16. #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
  17. #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
  18. #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
  19. #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  20. #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
  21. #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
  22. #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
  23. #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
  24. static const uint32_t k[64] = {
  25. 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
  26. 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
  27. 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
  28. 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
  29. 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
  30. 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
  31. 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
  32. 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
  33. };
  34. /**@brief Function for calculating the hash of a 64-byte section of data.
  35. *
  36. * @param[in,out] ctx Hash instance.
  37. * @param[in] data Aray with data to be hashed. Assumed to be 64 bytes long.
  38. */
  39. void sha256_transform(sha256_context_t *ctx, const uint8_t * data)
  40. {
  41. uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
  42. for (i = 0, j = 0; i < 16; ++i, j += 4)
  43. m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
  44. for ( ; i < 64; ++i)
  45. m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
  46. a = ctx->state[0];
  47. b = ctx->state[1];
  48. c = ctx->state[2];
  49. d = ctx->state[3];
  50. e = ctx->state[4];
  51. f = ctx->state[5];
  52. g = ctx->state[6];
  53. h = ctx->state[7];
  54. for (i = 0; i < 64; ++i) {
  55. t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
  56. t2 = EP0(a) + MAJ(a,b,c);
  57. h = g;
  58. g = f;
  59. f = e;
  60. e = d + t1;
  61. d = c;
  62. c = b;
  63. b = a;
  64. a = t1 + t2;
  65. }
  66. ctx->state[0] += a;
  67. ctx->state[1] += b;
  68. ctx->state[2] += c;
  69. ctx->state[3] += d;
  70. ctx->state[4] += e;
  71. ctx->state[5] += f;
  72. ctx->state[6] += g;
  73. ctx->state[7] += h;
  74. }
  75. ret_code_t sha256_init(sha256_context_t *ctx)
  76. {
  77. VERIFY_PARAM_NOT_NULL(ctx);
  78. ctx->datalen = 0;
  79. ctx->bitlen = 0;
  80. ctx->state[0] = 0x6a09e667;
  81. ctx->state[1] = 0xbb67ae85;
  82. ctx->state[2] = 0x3c6ef372;
  83. ctx->state[3] = 0xa54ff53a;
  84. ctx->state[4] = 0x510e527f;
  85. ctx->state[5] = 0x9b05688c;
  86. ctx->state[6] = 0x1f83d9ab;
  87. ctx->state[7] = 0x5be0cd19;
  88. return NRF_SUCCESS;
  89. }
  90. ret_code_t sha256_update(sha256_context_t *ctx, const uint8_t * data, size_t len)
  91. {
  92. VERIFY_PARAM_NOT_NULL(ctx);
  93. if (((len > 0) && (data == NULL)))
  94. {
  95. return NRF_ERROR_NULL;
  96. }
  97. uint32_t i;
  98. for (i = 0; i < len; ++i) {
  99. ctx->data[ctx->datalen] = data[i];
  100. ctx->datalen++;
  101. if (ctx->datalen == 64) {
  102. sha256_transform(ctx, ctx->data);
  103. ctx->bitlen += 512;
  104. ctx->datalen = 0;
  105. }
  106. }
  107. return NRF_SUCCESS;
  108. }
  109. ret_code_t sha256_final(sha256_context_t *ctx, uint8_t * hash)
  110. {
  111. VERIFY_PARAM_NOT_NULL(ctx);
  112. VERIFY_PARAM_NOT_NULL(hash);
  113. uint32_t i;
  114. i = ctx->datalen;
  115. // Pad whatever data is left in the buffer.
  116. if (ctx->datalen < 56) {
  117. ctx->data[i++] = 0x80;
  118. while (i < 56)
  119. ctx->data[i++] = 0x00;
  120. }
  121. else {
  122. ctx->data[i++] = 0x80;
  123. while (i < 64)
  124. ctx->data[i++] = 0x00;
  125. sha256_transform(ctx, ctx->data);
  126. memset(ctx->data, 0, 56);
  127. }
  128. // Append to the padding the total message's length in bits and transform.
  129. ctx->bitlen += (uint64_t)ctx->datalen * 8;
  130. ctx->data[63] = ctx->bitlen;
  131. ctx->data[62] = ctx->bitlen >> 8;
  132. ctx->data[61] = ctx->bitlen >> 16;
  133. ctx->data[60] = ctx->bitlen >> 24;
  134. ctx->data[59] = ctx->bitlen >> 32;
  135. ctx->data[58] = ctx->bitlen >> 40;
  136. ctx->data[57] = ctx->bitlen >> 48;
  137. ctx->data[56] = ctx->bitlen >> 56;
  138. sha256_transform(ctx, ctx->data);
  139. // Since this implementation uses little endian uint8_t ordering and SHA uses big endian,
  140. // reverse all the uint8_ts when copying the final state to the output hash.
  141. for (i = 0; i < 4; ++i) {
  142. hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
  143. hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
  144. hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
  145. hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
  146. hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
  147. hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
  148. hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
  149. hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
  150. }
  151. return NRF_SUCCESS;
  152. }