sha256.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * Copyright (c) 2015 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdlib.h>
  41. #include "sha256.h"
  42. #include "sdk_errors.h"
  43. #include "sdk_common.h"
  44. #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
  45. #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32 - (b))))
  46. #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
  47. #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  48. #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
  49. #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
  50. #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
  51. #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
  52. static const uint32_t k[64] = {
  53. 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
  54. 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
  55. 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
  56. 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
  57. 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
  58. 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
  59. 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
  60. 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
  61. };
  62. /**@brief Function for calculating the hash of a 64-byte section of data.
  63. *
  64. * @param[in,out] ctx Hash instance.
  65. * @param[in] data Aray with data to be hashed. Assumed to be 64 bytes long.
  66. */
  67. void sha256_transform(sha256_context_t *ctx, const uint8_t * data)
  68. {
  69. uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
  70. for (i = 0, j = 0; i < 16; ++i, j += 4)
  71. m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
  72. for ( ; i < 64; ++i)
  73. m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
  74. a = ctx->state[0];
  75. b = ctx->state[1];
  76. c = ctx->state[2];
  77. d = ctx->state[3];
  78. e = ctx->state[4];
  79. f = ctx->state[5];
  80. g = ctx->state[6];
  81. h = ctx->state[7];
  82. for (i = 0; i < 64; ++i) {
  83. t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
  84. t2 = EP0(a) + MAJ(a,b,c);
  85. h = g;
  86. g = f;
  87. f = e;
  88. e = d + t1;
  89. d = c;
  90. c = b;
  91. b = a;
  92. a = t1 + t2;
  93. }
  94. ctx->state[0] += a;
  95. ctx->state[1] += b;
  96. ctx->state[2] += c;
  97. ctx->state[3] += d;
  98. ctx->state[4] += e;
  99. ctx->state[5] += f;
  100. ctx->state[6] += g;
  101. ctx->state[7] += h;
  102. }
  103. ret_code_t sha256_init(sha256_context_t *ctx)
  104. {
  105. VERIFY_PARAM_NOT_NULL(ctx);
  106. ctx->datalen = 0;
  107. ctx->bitlen = 0;
  108. ctx->state[0] = 0x6a09e667;
  109. ctx->state[1] = 0xbb67ae85;
  110. ctx->state[2] = 0x3c6ef372;
  111. ctx->state[3] = 0xa54ff53a;
  112. ctx->state[4] = 0x510e527f;
  113. ctx->state[5] = 0x9b05688c;
  114. ctx->state[6] = 0x1f83d9ab;
  115. ctx->state[7] = 0x5be0cd19;
  116. return NRF_SUCCESS;
  117. }
  118. ret_code_t sha256_update(sha256_context_t *ctx, const uint8_t * data, size_t len)
  119. {
  120. VERIFY_PARAM_NOT_NULL(ctx);
  121. if (((len > 0) && (data == NULL)))
  122. {
  123. return NRF_ERROR_NULL;
  124. }
  125. uint32_t i;
  126. for (i = 0; i < len; ++i) {
  127. ctx->data[ctx->datalen] = data[i];
  128. ctx->datalen++;
  129. if (ctx->datalen == 64) {
  130. sha256_transform(ctx, ctx->data);
  131. ctx->bitlen += 512;
  132. ctx->datalen = 0;
  133. }
  134. }
  135. return NRF_SUCCESS;
  136. }
  137. ret_code_t sha256_final(sha256_context_t *ctx, uint8_t * hash, uint8_t le)
  138. {
  139. uint32_t i;
  140. VERIFY_PARAM_NOT_NULL(ctx);
  141. VERIFY_PARAM_NOT_NULL(hash);
  142. i = ctx->datalen;
  143. // Pad whatever data is left in the buffer.
  144. if (ctx->datalen < 56) {
  145. ctx->data[i++] = 0x80;
  146. while (i < 56)
  147. ctx->data[i++] = 0x00;
  148. }
  149. else {
  150. ctx->data[i++] = 0x80;
  151. while (i < 64)
  152. ctx->data[i++] = 0x00;
  153. sha256_transform(ctx, ctx->data);
  154. memset(ctx->data, 0, 56);
  155. }
  156. // Append to the padding the total message's length in bits and transform.
  157. ctx->bitlen += (uint64_t)ctx->datalen * 8;
  158. ctx->data[63] = ctx->bitlen;
  159. ctx->data[62] = ctx->bitlen >> 8;
  160. ctx->data[61] = ctx->bitlen >> 16;
  161. ctx->data[60] = ctx->bitlen >> 24;
  162. ctx->data[59] = ctx->bitlen >> 32;
  163. ctx->data[58] = ctx->bitlen >> 40;
  164. ctx->data[57] = ctx->bitlen >> 48;
  165. ctx->data[56] = ctx->bitlen >> 56;
  166. sha256_transform(ctx, ctx->data);
  167. if (le)
  168. {
  169. for (i = 0; i < 4; ++i) {
  170. hash[i] = (ctx->state[7] >> (i * 8)) & 0x000000ff;
  171. hash[i + 4] = (ctx->state[6] >> (i * 8)) & 0x000000ff;
  172. hash[i + 8] = (ctx->state[5] >> (i * 8)) & 0x000000ff;
  173. hash[i + 12] = (ctx->state[4] >> (i * 8)) & 0x000000ff;
  174. hash[i + 16] = (ctx->state[3] >> (i * 8)) & 0x000000ff;
  175. hash[i + 20] = (ctx->state[2] >> (i * 8)) & 0x000000ff;
  176. hash[i + 24] = (ctx->state[1] >> (i * 8)) & 0x000000ff;
  177. hash[i + 28] = (ctx->state[0] >> (i * 8)) & 0x000000ff;
  178. }
  179. }
  180. else
  181. {
  182. // Since this implementation uses little endian uint8_t ordering and SHA uses big endian,
  183. // reverse all the uint8_ts when copying the final state to the output hash.
  184. for (i = 0; i < 4; ++i) {
  185. hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
  186. hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
  187. hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
  188. hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
  189. hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
  190. hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
  191. hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
  192. hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
  193. }
  194. }
  195. return NRF_SUCCESS;
  196. }