sha256.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* sha256.c - TinyCrypt SHA-256 crypto hash algorithm implementation */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <tinycrypt/sha256.h>
  32. #include <tinycrypt/constants.h>
  33. #include <tinycrypt/utils.h>
  34. static void compress(unsigned int *iv, const uint8_t *data);
  35. int tc_sha256_init(TCSha256State_t s)
  36. {
  37. /* input sanity check: */
  38. if (s == (TCSha256State_t) 0) {
  39. return TC_CRYPTO_FAIL;
  40. }
  41. /*
  42. * Setting the initial state values.
  43. * These values correspond to the first 32 bits of the fractional parts
  44. * of the square roots of the first 8 primes: 2, 3, 5, 7, 11, 13, 17
  45. * and 19.
  46. */
  47. _set((uint8_t *) s, 0x00, sizeof(*s));
  48. s->iv[0] = 0x6a09e667;
  49. s->iv[1] = 0xbb67ae85;
  50. s->iv[2] = 0x3c6ef372;
  51. s->iv[3] = 0xa54ff53a;
  52. s->iv[4] = 0x510e527f;
  53. s->iv[5] = 0x9b05688c;
  54. s->iv[6] = 0x1f83d9ab;
  55. s->iv[7] = 0x5be0cd19;
  56. return TC_CRYPTO_SUCCESS;
  57. }
  58. int tc_sha256_update(TCSha256State_t s, const uint8_t *data, size_t datalen)
  59. {
  60. /* input sanity check: */
  61. if (s == (TCSha256State_t) 0 ||
  62. data == (void *) 0) {
  63. return TC_CRYPTO_FAIL;
  64. } else if (datalen == 0) {
  65. return TC_CRYPTO_SUCCESS;
  66. }
  67. while (datalen-- > 0) {
  68. s->leftover[s->leftover_offset++] = *(data++);
  69. if (s->leftover_offset >= TC_SHA256_BLOCK_SIZE) {
  70. compress(s->iv, s->leftover);
  71. s->leftover_offset = 0;
  72. s->bits_hashed += (TC_SHA256_BLOCK_SIZE << 3);
  73. }
  74. }
  75. return TC_CRYPTO_SUCCESS;
  76. }
  77. int tc_sha256_final(uint8_t *digest, TCSha256State_t s)
  78. {
  79. unsigned int i;
  80. /* input sanity check: */
  81. if (digest == (uint8_t *) 0 ||
  82. s == (TCSha256State_t) 0) {
  83. return TC_CRYPTO_FAIL;
  84. }
  85. s->bits_hashed += (s->leftover_offset << 3);
  86. s->leftover[s->leftover_offset++] = 0x80; /* always room for one byte */
  87. if (s->leftover_offset > (sizeof(s->leftover) - 8)) {
  88. /* there is not room for all the padding in this block */
  89. _set(s->leftover + s->leftover_offset, 0x00,
  90. sizeof(s->leftover) - s->leftover_offset);
  91. compress(s->iv, s->leftover);
  92. s->leftover_offset = 0;
  93. }
  94. /* add the padding and the length in big-Endian format */
  95. _set(s->leftover + s->leftover_offset, 0x00,
  96. sizeof(s->leftover) - 8 - s->leftover_offset);
  97. s->leftover[sizeof(s->leftover) - 1] = (uint8_t)(s->bits_hashed);
  98. s->leftover[sizeof(s->leftover) - 2] = (uint8_t)(s->bits_hashed >> 8);
  99. s->leftover[sizeof(s->leftover) - 3] = (uint8_t)(s->bits_hashed >> 16);
  100. s->leftover[sizeof(s->leftover) - 4] = (uint8_t)(s->bits_hashed >> 24);
  101. s->leftover[sizeof(s->leftover) - 5] = (uint8_t)(s->bits_hashed >> 32);
  102. s->leftover[sizeof(s->leftover) - 6] = (uint8_t)(s->bits_hashed >> 40);
  103. s->leftover[sizeof(s->leftover) - 7] = (uint8_t)(s->bits_hashed >> 48);
  104. s->leftover[sizeof(s->leftover) - 8] = (uint8_t)(s->bits_hashed >> 56);
  105. /* hash the padding and length */
  106. compress(s->iv, s->leftover);
  107. /* copy the iv out to digest */
  108. for (i = 0; i < TC_SHA256_STATE_BLOCKS; ++i) {
  109. unsigned int t = *((unsigned int *) &s->iv[i]);
  110. *digest++ = (uint8_t)(t >> 24);
  111. *digest++ = (uint8_t)(t >> 16);
  112. *digest++ = (uint8_t)(t >> 8);
  113. *digest++ = (uint8_t)(t);
  114. }
  115. /* destroy the current state */
  116. _set(s, 0, sizeof(*s));
  117. return TC_CRYPTO_SUCCESS;
  118. }
  119. /*
  120. * Initializing SHA-256 Hash constant words K.
  121. * These values correspond to the first 32 bits of the fractional parts of the
  122. * cube roots of the first 64 primes between 2 and 311.
  123. */
  124. static const unsigned int k256[64] = {
  125. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
  126. 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  127. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
  128. 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  129. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
  130. 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  131. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
  132. 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  133. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
  134. 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  135. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  136. };
  137. static inline unsigned int ROTR(unsigned int a, unsigned int n)
  138. {
  139. return (((a) >> n) | ((a) << (32 - n)));
  140. }
  141. #define Sigma0(a)(ROTR((a), 2) ^ ROTR((a), 13) ^ ROTR((a), 22))
  142. #define Sigma1(a)(ROTR((a), 6) ^ ROTR((a), 11) ^ ROTR((a), 25))
  143. #define sigma0(a)(ROTR((a), 7) ^ ROTR((a), 18) ^ ((a) >> 3))
  144. #define sigma1(a)(ROTR((a), 17) ^ ROTR((a), 19) ^ ((a) >> 10))
  145. #define Ch(a, b, c)(((a) & (b)) ^ ((~(a)) & (c)))
  146. #define Maj(a, b, c)(((a) & (b)) ^ ((a) & (c)) ^ ((b) & (c)))
  147. static inline unsigned int BigEndian(const uint8_t **c)
  148. {
  149. unsigned int n = 0;
  150. n = (((unsigned int)(*((*c)++))) << 24);
  151. n |= ((unsigned int)(*((*c)++)) << 16);
  152. n |= ((unsigned int)(*((*c)++)) << 8);
  153. n |= ((unsigned int)(*((*c)++)));
  154. return n;
  155. }
  156. static void compress(unsigned int *iv, const uint8_t *data)
  157. {
  158. unsigned int a, b, c, d, e, f, g, h;
  159. unsigned int s0, s1;
  160. unsigned int t1, t2;
  161. unsigned int work_space[16];
  162. unsigned int n;
  163. unsigned int i;
  164. a = iv[0]; b = iv[1]; c = iv[2]; d = iv[3];
  165. e = iv[4]; f = iv[5]; g = iv[6]; h = iv[7];
  166. for (i = 0; i < 16; ++i) {
  167. n = BigEndian(&data);
  168. t1 = work_space[i] = n;
  169. t1 += h + Sigma1(e) + Ch(e, f, g) + k256[i];
  170. t2 = Sigma0(a) + Maj(a, b, c);
  171. h = g; g = f; f = e; e = d + t1;
  172. d = c; c = b; b = a; a = t1 + t2;
  173. }
  174. for ( ; i < 64; ++i) {
  175. s0 = work_space[(i+1)&0x0f];
  176. s0 = sigma0(s0);
  177. s1 = work_space[(i+14)&0x0f];
  178. s1 = sigma1(s1);
  179. t1 = work_space[i&0xf] += s0 + s1 + work_space[(i+9)&0xf];
  180. t1 += h + Sigma1(e) + Ch(e, f, g) + k256[i];
  181. t2 = Sigma0(a) + Maj(a, b, c);
  182. h = g; g = f; f = e; e = d + t1;
  183. d = c; c = b; b = a; a = t1 + t2;
  184. }
  185. iv[0] += a; iv[1] += b; iv[2] += c; iv[3] += d;
  186. iv[4] += e; iv[5] += f; iv[6] += g; iv[7] += h;
  187. }