sha256.c
Go to the documentation of this file.
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-2026 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.6.2
34  **/
35 
36 //Switch to the appropriate trace level
37 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
38 
39 //Dependencies
40 #include "core/crypto.h"
41 #include "hash/sha256.h"
42 
43 //Check crypto library configuration
44 #if (SHA224_SUPPORT == ENABLED || SHA256_SUPPORT == ENABLED)
45 
46 //Macro to access the workspace as a circular buffer
47 #define W(n) w[(n) & 0x0F]
48 
49 //SHA-256 auxiliary functions
50 #define CH(x, y, z) (((x) & (y)) | (~(x) & (z)))
51 #define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
52 #define SIGMA1(x) (ROR32(x, 2) ^ ROR32(x, 13) ^ ROR32(x, 22))
53 #define SIGMA2(x) (ROR32(x, 6) ^ ROR32(x, 11) ^ ROR32(x, 25))
54 #define SIGMA3(x) (ROR32(x, 7) ^ ROR32(x, 18) ^ ((x) >> 3))
55 #define SIGMA4(x) (ROR32(x, 17) ^ ROR32(x, 19) ^ ((x) >> 10))
56 
57 //SHA-256 padding
58 static const uint8_t padding[64] =
59 {
60  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
64 };
65 
66 //SHA-256 constants
67 static const uint32_t k[64] =
68 {
69  0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
70  0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
71  0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
72  0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
73  0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
74  0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
75  0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
76  0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
77 };
78 
79 #if (SHA256_SUPPORT == ENABLED)
80 
81 //SHA-256 object identifier (2.16.840.1.101.3.4.2.1)
82 const uint8_t SHA256_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01};
83 
84 //Common interface for hash algorithms
86 {
87  "SHA-256",
88  SHA256_OID,
89  sizeof(SHA256_OID),
90  sizeof(Sha256Context),
94  TRUE,
99 #if ((defined(MIMXRT1050_CRYPTO_HASH_SUPPORT) && MIMXRT1050_CRYPTO_HASH_SUPPORT == ENABLED) || \
100  (defined(MIMXRT1060_CRYPTO_HASH_SUPPORT) && MIMXRT1060_CRYPTO_HASH_SUPPORT == ENABLED) || \
101  (defined(MIMXRT1160_CRYPTO_HASH_SUPPORT) && MIMXRT1160_CRYPTO_HASH_SUPPORT == ENABLED) || \
102  (defined(MIMXRT1170_CRYPTO_HASH_SUPPORT) && MIMXRT1170_CRYPTO_HASH_SUPPORT == ENABLED))
103  NULL,
104 #else
106 #endif
107 };
108 
109 #endif
110 
111 
112 /**
113  * @brief Digest a message using SHA-256
114  * @param[in] data Pointer to the message being hashed
115  * @param[in] length Length of the message
116  * @param[out] digest Pointer to the calculated digest
117  * @return Error code
118  **/
119 
120 __weak_func error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
121 {
122 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
123  Sha256Context *context;
124 #else
125  Sha256Context context[1];
126 #endif
127 
128  //Check parameters
129  if(data == NULL && length != 0)
131 
132  if(digest == NULL)
134 
135 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
136  //Allocate a memory buffer to hold the SHA-256 context
137  context = cryptoAllocMem(sizeof(Sha256Context));
138  //Failed to allocate memory?
139  if(context == NULL)
140  return ERROR_OUT_OF_MEMORY;
141 #endif
142 
143  //Initialize the SHA-256 context
144  sha256Init(context);
145  //Digest the message
146  sha256Update(context, data, length);
147  //Finalize the SHA-256 message digest
148  sha256Final(context, digest);
149 
150 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
151  //Free previously allocated memory
152  cryptoFreeMem(context);
153 #endif
154 
155  //Successful processing
156  return NO_ERROR;
157 }
158 
159 
160 /**
161  * @brief Initialize SHA-256 message digest context
162  * @param[in] context Pointer to the SHA-256 context to initialize
163  **/
164 
165 __weak_func void sha256Init(Sha256Context *context)
166 {
167  //Set initial hash value
168  context->h[0] = 0x6A09E667;
169  context->h[1] = 0xBB67AE85;
170  context->h[2] = 0x3C6EF372;
171  context->h[3] = 0xA54FF53A;
172  context->h[4] = 0x510E527F;
173  context->h[5] = 0x9B05688C;
174  context->h[6] = 0x1F83D9AB;
175  context->h[7] = 0x5BE0CD19;
176 
177  //Number of bytes in the buffer
178  context->size = 0;
179  //Total length of the message
180  context->totalSize = 0;
181 }
182 
183 
184 /**
185  * @brief Update the SHA-256 context with a portion of the message being hashed
186  * @param[in] context Pointer to the SHA-256 context
187  * @param[in] data Pointer to the buffer being hashed
188  * @param[in] length Length of the buffer
189  **/
190 
191 __weak_func void sha256Update(Sha256Context *context, const void *data, size_t length)
192 {
193  size_t n;
194 
195  //Process the incoming data
196  while(length > 0)
197  {
198  //The buffer can hold at most 64 bytes
199  n = MIN(length, 64 - context->size);
200 
201  //Copy the data to the buffer
202  osMemcpy(context->buffer + context->size, data, n);
203 
204  //Update the SHA-256 context
205  context->size += n;
206  context->totalSize += n;
207  //Advance the data pointer
208  data = (uint8_t *) data + n;
209  //Remaining bytes to process
210  length -= n;
211 
212  //Process message in 16-word blocks
213  if(context->size == 64)
214  {
215  //Transform the 16-word block
216  sha256ProcessBlock(context);
217  //Empty the buffer
218  context->size = 0;
219  }
220  }
221 }
222 
223 
224 /**
225  * @brief Finish the SHA-256 message digest
226  * @param[in] context Pointer to the SHA-256 context
227  * @param[out] digest Calculated digest
228  **/
229 
230 __weak_func void sha256Final(Sha256Context *context, uint8_t *digest)
231 {
232  uint_t i;
233  size_t paddingSize;
234  uint64_t totalSize;
235 
236  //Length of the original message (before padding)
237  totalSize = context->totalSize * 8;
238 
239  //Pad the message so that its length is congruent to 56 modulo 64
240  if(context->size < 56)
241  {
242  paddingSize = 56 - context->size;
243  }
244  else
245  {
246  paddingSize = 64 + 56 - context->size;
247  }
248 
249  //Append padding
250  sha256Update(context, padding, paddingSize);
251 
252  //Append the length of the original message
253  for(i = 0; i < 8; i++)
254  {
255  context->buffer[63 - i] = totalSize & 0xFF;
256  totalSize >>= 8;
257  }
258 
259  //Calculate the message digest
260  sha256ProcessBlock(context);
261 
262  //Copy the resulting digest
263  for(i = 0; i < (SHA256_DIGEST_SIZE / 4); i++)
264  {
265  STORE32BE(context->h[i], digest + i * 4);
266  }
267 }
268 
269 
270 /**
271  * @brief Finish the SHA-256 message digest (no padding added)
272  * @param[in] context Pointer to the SHA-256 context
273  * @param[out] digest Calculated digest
274  **/
275 
276 __weak_func void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
277 {
278  uint_t i;
279 
280  //Copy the resulting digest
281  for(i = 0; i < (SHA256_DIGEST_SIZE / 4); i++)
282  {
283  STORE32BE(context->h[i], digest + i * 4);
284  }
285 }
286 
287 
288 /**
289  * @brief Process message in 16-word blocks
290  * @param[in] context Pointer to the SHA-256 context
291  **/
292 
293 __weak_func void sha256ProcessBlock(Sha256Context *context)
294 {
295  uint_t i;
296  uint32_t temp1;
297  uint32_t temp2;
298 
299  //Initialize the 8 working registers
300  uint32_t a = context->h[0];
301  uint32_t b = context->h[1];
302  uint32_t c = context->h[2];
303  uint32_t d = context->h[3];
304  uint32_t e = context->h[4];
305  uint32_t f = context->h[5];
306  uint32_t g = context->h[6];
307  uint32_t h = context->h[7];
308 
309  //Process message in 16-word blocks
310  uint32_t *w = context->w;
311 
312  //Convert from big-endian byte order to host byte order
313  for(i = 0; i < 16; i++)
314  {
315  w[i] = LOAD32BE(context->buffer + i * 4);
316  }
317 
318  //SHA-256 hash computation (alternate method)
319  for(i = 0; i < 64; i++)
320  {
321  //Prepare the message schedule
322  if(i >= 16)
323  {
324  W(i) += SIGMA4(W(i + 14)) + W(i + 9) + SIGMA3(W(i + 1));
325  }
326 
327  //Calculate T1 and T2
328  temp1 = h + SIGMA2(e) + CH(e, f, g) + k[i] + W(i);
329  temp2 = SIGMA1(a) + MAJ(a, b, c);
330 
331  //Update working registers
332  h = g;
333  g = f;
334  f = e;
335  e = d + temp1;
336  d = c;
337  c = b;
338  b = a;
339  a = temp1 + temp2;
340  }
341 
342  //Update the hash value
343  context->h[0] += a;
344  context->h[1] += b;
345  context->h[2] += c;
346  context->h[3] += d;
347  context->h[4] += e;
348  context->h[5] += f;
349  context->h[6] += g;
350  context->h[7] += h;
351 }
352 
353 #endif
uint8_t b
Definition: nbns_common.h:122
__weak_func void sha256ProcessBlock(Sha256Context *context)
Process message in 16-word blocks.
Definition: sha256.c:293
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1089
#define SIGMA3(x)
Definition: sha256.c:54
uint8_t a
Definition: ndp.h:411
SHA-256 algorithm context.
Definition: sha256.h:62
#define LOAD32BE(p)
Definition: cpu_endian.h:210
__weak_func void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest (no padding added)
Definition: sha256.c:276
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:224
#define SIGMA4(x)
Definition: sha256.c:55
size_t size
Definition: sha256.h:69
uint32_t h[8]
Definition: sha256.h:63
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
#define SIGMA2(x)
Definition: sha256.c:53
#define CH(x, y, z)
Definition: sha256.c:50
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1090
#define SHA256_BLOCK_SIZE
Definition: sha256.h:43
__weak_func void sha256Init(Sha256Context *context)
Initialize SHA-256 message digest context.
Definition: sha256.c:165
uint8_t h
Definition: ndp.h:302
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
__weak_func error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-256.
Definition: sha256.c:120
error_t
Error codes.
Definition: error.h:43
uint32_t w[16]
Definition: sha256.h:66
const HashAlgo sha256HashAlgo
Definition: sha256.c:85
const uint8_t SHA256_OID[9]
Definition: sha256.c:82
#define MAJ(x, y, z)
Definition: sha256.c:51
General definitions for cryptographic algorithms.
#define W(n)
Definition: sha256.c:47
uint8_t length
Definition: tcp.h:375
uint8_t buffer[64]
Definition: sha256.h:67
#define MIN(a, b)
Definition: os_port.h:63
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1091
SHA-256 (Secure Hash Algorithm 256)
uint8_t n
#define cryptoFreeMem(p)
Definition: crypto.h:861
__weak_func void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
Definition: sha256.c:191
#define cryptoAllocMem(size)
Definition: crypto.h:856
Common interface for hash algorithms.
Definition: crypto.h:1151
uint64_t totalSize
Definition: sha256.h:70
unsigned int uint_t
Definition: compiler_port.h:57
__weak_func void sha256Final(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest.
Definition: sha256.c:230
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1086
#define SIGMA1(x)
Definition: sha256.c:52
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
void(* HashAlgoFinalRaw)(void *context, uint8_t *digest)
Definition: crypto.h:1092
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:514
#define SHA256_MIN_PAD_SIZE
Definition: sha256.h:47