sha256.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * @file sha256.c
  3. * @brief SHA-256 (Secure Hash Algorithm 256)
  4. *
  5. * @section License
  6. *
  7. * SPDX-License-Identifier: GPL-2.0-or-later
  8. *
  9. * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
  10. *
  11. * This file is part of CycloneCRYPTO Open.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 2
  16. * of the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software Foundation,
  25. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  26. *
  27. * @section Description
  28. *
  29. * SHA-256 is a secure hash algorithm for computing a condensed representation
  30. * of an electronic message. Refer to FIPS 180-4 for more details
  31. *
  32. * @author Oryx Embedded SARL (www.oryx-embedded.com)
  33. * @version 2.4.2
  34. **/
  35. //Dependencies
  36. #include <string.h>
  37. #include <stdint.h>
  38. #include "sha256.h"
  39. #include "cpu_endian.h"
  40. typedef unsigned int uint_t;
  41. //Macro to access the workspace as a circular buffer
  42. #define W(n) w[(n) & 0x0F]
  43. //shift operation
  44. #define ROR32(a, n) (((a) >> (n)) | ((a) << (32 - (n))))
  45. #define SHR32(a, n) ((a) >> (n))
  46. //SHA-256 auxiliary functions
  47. #define CH(x, y, z) (((x) & (y)) | (~(x) & (z)))
  48. #define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  49. #define SIGMA1(x) (ROR32(x, 2) ^ ROR32(x, 13) ^ ROR32(x, 22))
  50. #define SIGMA2(x) (ROR32(x, 6) ^ ROR32(x, 11) ^ ROR32(x, 25))
  51. #define SIGMA3(x) (ROR32(x, 7) ^ ROR32(x, 18) ^ SHR32(x, 3))
  52. #define SIGMA4(x) (ROR32(x, 17) ^ ROR32(x, 19) ^ SHR32(x, 10))
  53. //SHA-256 padding
  54. static const uint8_t padding[64] =
  55. {
  56. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  60. };
  61. //SHA-256 constants
  62. static const uint32_t k[64] =
  63. {
  64. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  65. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  66. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  67. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  68. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  69. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  70. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  71. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
  72. };
  73. //SHA-256 object identifier (2.16.840.1.101.3.4.2.1)
  74. const uint8_t SHA256_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01};
  75. /**
  76. * @brief Digest a message using SHA-256
  77. * @param[in] data Pointer to the message being hashed
  78. * @param[in] length Length of the message
  79. * @param[out] digest Pointer to the calculated digest
  80. * @return Error code
  81. **/
  82. int sha256Compute(const void *data, size_t length, uint8_t *digest)
  83. {
  84. Sha256Context context[1];
  85. //Check parameters
  86. if(data == NULL && length != 0)
  87. return -1;
  88. if(digest == NULL)
  89. return -1;
  90. //Initialize the SHA-256 context
  91. sha256Init(context);
  92. //Digest the message
  93. sha256Update(context, data, length);
  94. //Finalize the SHA-256 message digest
  95. sha256Final(context, digest);
  96. //Successful processing
  97. return 0;
  98. }
  99. /**
  100. * @brief Initialize SHA-256 message digest context
  101. * @param[in] context Pointer to the SHA-256 context to initialize
  102. **/
  103. void sha256Init(Sha256Context *context)
  104. {
  105. //Set initial hash value
  106. context->h[0] = 0x6A09E667;
  107. context->h[1] = 0xBB67AE85;
  108. context->h[2] = 0x3C6EF372;
  109. context->h[3] = 0xA54FF53A;
  110. context->h[4] = 0x510E527F;
  111. context->h[5] = 0x9B05688C;
  112. context->h[6] = 0x1F83D9AB;
  113. context->h[7] = 0x5BE0CD19;
  114. //Number of bytes in the buffer
  115. context->size = 0;
  116. //Total length of the message
  117. context->totalSize = 0;
  118. }
  119. /**
  120. * @brief Update the SHA-256 context with a portion of the message being hashed
  121. * @param[in] context Pointer to the SHA-256 context
  122. * @param[in] data Pointer to the buffer being hashed
  123. * @param[in] length Length of the buffer
  124. **/
  125. void sha256Update(Sha256Context *context, const void *data, size_t length)
  126. {
  127. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  128. size_t n;
  129. //Process the incoming data
  130. while(length > 0)
  131. {
  132. //The buffer can hold at most 64 bytes
  133. n = MIN(length, 64 - context->size);
  134. //Copy the data to the buffer
  135. memcpy(context->buffer + context->size, data, n);
  136. //Update the SHA-256 context
  137. context->size += n;
  138. context->totalSize += n;
  139. //Advance the data pointer
  140. data = (uint8_t *) data + n;
  141. //Remaining bytes to process
  142. length -= n;
  143. //Process message in 16-word blocks
  144. if(context->size == 64)
  145. {
  146. //Transform the 16-word block
  147. sha256ProcessBlock(context);
  148. //Empty the buffer
  149. context->size = 0;
  150. }
  151. }
  152. #undef MIN
  153. }
  154. /**
  155. * @brief Finish the SHA-256 message digest
  156. * @param[in] context Pointer to the SHA-256 context
  157. * @param[out] digest Calculated digest (optional parameter)
  158. **/
  159. void sha256Final(Sha256Context *context, uint8_t *digest)
  160. {
  161. uint_t i;
  162. size_t paddingSize;
  163. uint64_t totalSize;
  164. //Length of the original message (before padding)
  165. totalSize = context->totalSize * 8;
  166. //Pad the message so that its length is congruent to 56 modulo 64
  167. if(context->size < 56)
  168. {
  169. paddingSize = 56 - context->size;
  170. }
  171. else
  172. {
  173. paddingSize = 64 + 56 - context->size;
  174. }
  175. //Append padding
  176. sha256Update(context, padding, paddingSize);
  177. //Append the length of the original message
  178. context->w[14] = htobe32((uint32_t) (totalSize >> 32));
  179. context->w[15] = htobe32((uint32_t) totalSize);
  180. //Calculate the message digest
  181. sha256ProcessBlock(context);
  182. //Convert from host byte order to big-endian byte order
  183. for(i = 0; i < 8; i++)
  184. {
  185. context->h[i] = htobe32(context->h[i]);
  186. }
  187. //Copy the resulting digest
  188. if(digest != NULL)
  189. {
  190. memcpy(digest, context->digest, SHA256_DIGEST_SIZE);
  191. }
  192. }
  193. /**
  194. * @brief Finish the SHA-256 message digest (no padding added)
  195. * @param[in] context Pointer to the SHA-256 context
  196. * @param[out] digest Calculated digest
  197. **/
  198. void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
  199. {
  200. uint_t i;
  201. //Convert from host byte order to big-endian byte order
  202. for(i = 0; i < 8; i++)
  203. {
  204. context->h[i] = htobe32(context->h[i]);
  205. }
  206. //Copy the resulting digest
  207. memcpy(digest, context->digest, SHA256_DIGEST_SIZE);
  208. //Convert from big-endian byte order to host byte order
  209. for(i = 0; i < 8; i++)
  210. {
  211. context->h[i] = betoh32(context->h[i]);
  212. }
  213. }
  214. /**
  215. * @brief Process message in 16-word blocks
  216. * @param[in] context Pointer to the SHA-256 context
  217. **/
  218. void sha256ProcessBlock(Sha256Context *context)
  219. {
  220. uint_t i;
  221. uint32_t temp1;
  222. uint32_t temp2;
  223. //Initialize the 8 working registers
  224. uint32_t a = context->h[0];
  225. uint32_t b = context->h[1];
  226. uint32_t c = context->h[2];
  227. uint32_t d = context->h[3];
  228. uint32_t e = context->h[4];
  229. uint32_t f = context->h[5];
  230. uint32_t g = context->h[6];
  231. uint32_t h = context->h[7];
  232. //Process message in 16-word blocks
  233. uint32_t *w = context->w;
  234. //Convert from big-endian byte order to host byte order
  235. for(i = 0; i < 16; i++)
  236. {
  237. w[i] = betoh32(w[i]);
  238. }
  239. //SHA-256 hash computation (alternate method)
  240. for(i = 0; i < 64; i++)
  241. {
  242. //Prepare the message schedule
  243. if(i >= 16)
  244. {
  245. W(i) += SIGMA4(W(i + 14)) + W(i + 9) + SIGMA3(W(i + 1));
  246. }
  247. //Calculate T1 and T2
  248. temp1 = h + SIGMA2(e) + CH(e, f, g) + k[i] + W(i);
  249. temp2 = SIGMA1(a) + MAJ(a, b, c);
  250. //Update working registers
  251. h = g;
  252. g = f;
  253. f = e;
  254. e = d + temp1;
  255. d = c;
  256. c = b;
  257. b = a;
  258. a = temp1 + temp2;
  259. }
  260. //Update the hash value
  261. context->h[0] += a;
  262. context->h[1] += b;
  263. context->h[2] += c;
  264. context->h[3] += d;
  265. context->h[4] += e;
  266. context->h[5] += f;
  267. context->h[6] += g;
  268. context->h[7] += h;
  269. }