sha512.c
Go to the documentation of this file.
1 /**
2  * @file sha512.c
3  * @brief SHA-512 (Secure Hash Algorithm 512)
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-512 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/sha512.h"
42 
43 //Check crypto library configuration
44 #if (SHA384_SUPPORT == ENABLED || SHA512_SUPPORT == ENABLED || \
45  SHA512_224_SUPPORT == ENABLED || SHA512_256_SUPPORT == ENABLED)
46 
47 //Macro to access the workspace as a circular buffer
48 #define W(n) w[(n) & 0x0F]
49 
50 //SHA-512 auxiliary functions
51 #define CH(x, y, z) (((x) & (y)) | (~(x) & (z)))
52 #define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
53 #define SIGMA1(x) (ROR64(x, 28) ^ ROR64(x, 34) ^ ROR64(x, 39))
54 #define SIGMA2(x) (ROR64(x, 14) ^ ROR64(x, 18) ^ ROR64(x, 41))
55 #define SIGMA3(x) (ROR64(x, 1) ^ ROR64(x, 8) ^ ((x) >> 7))
56 #define SIGMA4(x) (ROR64(x, 19) ^ ROR64(x, 61) ^ ((x) >> 6))
57 
58 //SHA-512 padding
59 static const uint8_t padding[128] =
60 {
61  0x80, 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  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
69 };
70 
71 //SHA-512 constants
72 static const uint64_t k[80] =
73 {
74  0x428A2F98D728AE22, 0x7137449123EF65CD, 0xB5C0FBCFEC4D3B2F, 0xE9B5DBA58189DBBC,
75  0x3956C25BF348B538, 0x59F111F1B605D019, 0x923F82A4AF194F9B, 0xAB1C5ED5DA6D8118,
76  0xD807AA98A3030242, 0x12835B0145706FBE, 0x243185BE4EE4B28C, 0x550C7DC3D5FFB4E2,
77  0x72BE5D74F27B896F, 0x80DEB1FE3B1696B1, 0x9BDC06A725C71235, 0xC19BF174CF692694,
78  0xE49B69C19EF14AD2, 0xEFBE4786384F25E3, 0x0FC19DC68B8CD5B5, 0x240CA1CC77AC9C65,
79  0x2DE92C6F592B0275, 0x4A7484AA6EA6E483, 0x5CB0A9DCBD41FBD4, 0x76F988DA831153B5,
80  0x983E5152EE66DFAB, 0xA831C66D2DB43210, 0xB00327C898FB213F, 0xBF597FC7BEEF0EE4,
81  0xC6E00BF33DA88FC2, 0xD5A79147930AA725, 0x06CA6351E003826F, 0x142929670A0E6E70,
82  0x27B70A8546D22FFC, 0x2E1B21385C26C926, 0x4D2C6DFC5AC42AED, 0x53380D139D95B3DF,
83  0x650A73548BAF63DE, 0x766A0ABB3C77B2A8, 0x81C2C92E47EDAEE6, 0x92722C851482353B,
84  0xA2BFE8A14CF10364, 0xA81A664BBC423001, 0xC24B8B70D0F89791, 0xC76C51A30654BE30,
85  0xD192E819D6EF5218, 0xD69906245565A910, 0xF40E35855771202A, 0x106AA07032BBD1B8,
86  0x19A4C116B8D2D0C8, 0x1E376C085141AB53, 0x2748774CDF8EEB99, 0x34B0BCB5E19B48A8,
87  0x391C0CB3C5C95A63, 0x4ED8AA4AE3418ACB, 0x5B9CCA4F7763E373, 0x682E6FF3D6B2B8A3,
88  0x748F82EE5DEFB2FC, 0x78A5636F43172F60, 0x84C87814A1F0AB72, 0x8CC702081A6439EC,
89  0x90BEFFFA23631E28, 0xA4506CEBDE82BDE9, 0xBEF9A3F7B2C67915, 0xC67178F2E372532B,
90  0xCA273ECEEA26619C, 0xD186B8C721C0C207, 0xEADA7DD6CDE0EB1E, 0xF57D4F7FEE6ED178,
91  0x06F067AA72176FBA, 0x0A637DC5A2C898A6, 0x113F9804BEF90DAE, 0x1B710B35131C471B,
92  0x28DB77F523047D84, 0x32CAAB7B40C72493, 0x3C9EBE0A15C9BEBC, 0x431D67C49C100D4C,
93  0x4CC5D4BECB3E42B6, 0x597F299CFC657E2A, 0x5FCB6FAB3AD6FAEC, 0x6C44198C4A475817
94 };
95 
96 #if (SHA512_SUPPORT == ENABLED)
97 
98 //SHA-512 object identifier (2.16.840.1.101.3.4.2.3)
99 const uint8_t SHA512_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03};
100 
101 //Common interface for hash algorithms
103 {
104  "SHA-512",
105  SHA512_OID,
106  sizeof(SHA512_OID),
107  sizeof(Sha512Context),
111  TRUE,
116  NULL
117 };
118 
119 #endif
120 
121 
122 /**
123  * @brief Digest a message using SHA-512
124  * @param[in] data Pointer to the message being hashed
125  * @param[in] length Length of the message
126  * @param[out] digest Pointer to the calculated digest
127  * @return Error code
128  **/
129 
130 __weak_func error_t sha512Compute(const void *data, size_t length, uint8_t *digest)
131 {
132 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
133  Sha512Context *context;
134 #else
135  Sha512Context context[1];
136 #endif
137 
138  //Check parameters
139  if(data == NULL && length != 0)
141 
142  if(digest == NULL)
144 
145 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
146  //Allocate a memory buffer to hold the SHA-512 context
147  context = cryptoAllocMem(sizeof(Sha512Context));
148  //Failed to allocate memory?
149  if(context == NULL)
150  return ERROR_OUT_OF_MEMORY;
151 #endif
152 
153  //Initialize the SHA-512 context
154  sha512Init(context);
155  //Digest the message
156  sha512Update(context, data, length);
157  //Finalize the SHA-512 message digest
158  sha512Final(context, digest);
159 
160 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
161  //Free previously allocated memory
162  cryptoFreeMem(context);
163 #endif
164 
165  //Successful processing
166  return NO_ERROR;
167 }
168 
169 
170 /**
171  * @brief Initialize SHA-512 message digest context
172  * @param[in] context Pointer to the SHA-512 context to initialize
173  **/
174 
175 __weak_func void sha512Init(Sha512Context *context)
176 {
177  //Set initial hash value
178  context->h[0] = 0x6A09E667F3BCC908;
179  context->h[1] = 0xBB67AE8584CAA73B;
180  context->h[2] = 0x3C6EF372FE94F82B;
181  context->h[3] = 0xA54FF53A5F1D36F1;
182  context->h[4] = 0x510E527FADE682D1;
183  context->h[5] = 0x9B05688C2B3E6C1F;
184  context->h[6] = 0x1F83D9ABFB41BD6B;
185  context->h[7] = 0x5BE0CD19137E2179;
186 
187  //Number of bytes in the buffer
188  context->size = 0;
189  //Total length of the message
190  context->totalSize = 0;
191 }
192 
193 
194 /**
195  * @brief Update the SHA-512 context with a portion of the message being hashed
196  * @param[in] context Pointer to the SHA-512 context
197  * @param[in] data Pointer to the buffer being hashed
198  * @param[in] length Length of the buffer
199  **/
200 
201 __weak_func void sha512Update(Sha512Context *context, const void *data, size_t length)
202 {
203  size_t n;
204 
205  //Process the incoming data
206  while(length > 0)
207  {
208  //The buffer can hold at most 128 bytes
209  n = MIN(length, 128 - context->size);
210 
211  //Copy the data to the buffer
212  osMemcpy(context->buffer + context->size, data, n);
213 
214  //Update the SHA-512 context
215  context->size += n;
216  context->totalSize += n;
217  //Advance the data pointer
218  data = (uint8_t *) data + n;
219  //Remaining bytes to process
220  length -= n;
221 
222  //Process message in 16-word blocks
223  if(context->size == 128)
224  {
225  //Transform the 16-word block
226  sha512ProcessBlock(context);
227  //Empty the buffer
228  context->size = 0;
229  }
230  }
231 }
232 
233 
234 /**
235  * @brief Finish the SHA-512 message digest
236  * @param[in] context Pointer to the SHA-512 context
237  * @param[out] digest Calculated digest
238  **/
239 
240 __weak_func void sha512Final(Sha512Context *context, uint8_t *digest)
241 {
242  uint_t i;
243  size_t paddingSize;
244  uint64_t totalSize;
245 
246  //Length of the original message (before padding)
247  totalSize = context->totalSize * 8;
248 
249  //Pad the message so that its length is congruent to 112 modulo 128
250  if(context->size < 112)
251  {
252  paddingSize = 112 - context->size;
253  }
254  else
255  {
256  paddingSize = 128 + 112 - context->size;
257  }
258 
259  //Append padding
260  sha512Update(context, padding, paddingSize);
261 
262  //Append the length of the original message
263  for(i = 0; i < 16; i++)
264  {
265  context->buffer[127 - i] = totalSize & 0xFF;
266  totalSize >>= 8;
267  }
268 
269  //Calculate the message digest
270  sha512ProcessBlock(context);
271 
272  //Copy the resulting digest
273  for(i = 0; i < (SHA512_DIGEST_SIZE / 8); i++)
274  {
275  STORE64BE(context->h[i], digest + i * 8);
276  }
277 }
278 
279 
280 /**
281  * @brief Process message in 16-word blocks
282  * @param[in] context Pointer to the SHA-512 context
283  **/
284 
285 __weak_func void sha512ProcessBlock(Sha512Context *context)
286 {
287  uint_t i;
288  uint64_t temp1;
289  uint64_t temp2;
290 
291  //Initialize the 8 working registers
292  uint64_t a = context->h[0];
293  uint64_t b = context->h[1];
294  uint64_t c = context->h[2];
295  uint64_t d = context->h[3];
296  uint64_t e = context->h[4];
297  uint64_t f = context->h[5];
298  uint64_t g = context->h[6];
299  uint64_t h = context->h[7];
300 
301  //Process message in 16-word blocks
302  uint64_t *w = context->w;
303 
304  //Convert from big-endian byte order to host byte order
305  for(i = 0; i < 16; i++)
306  {
307  w[i] = LOAD64BE(context->buffer + i * 8);
308  }
309 
310  //SHA-512 hash computation (alternate method)
311  for(i = 0; i < 80; i++)
312  {
313  //Prepare the message schedule
314  if(i >= 16)
315  {
316  W(i) += SIGMA4(W(i + 14)) + W(i + 9) + SIGMA3(W(i + 1));
317  }
318 
319  //Calculate T1 and T2
320  temp1 = h + SIGMA2(e) + CH(e, f, g) + k[i] + W(i);
321  temp2 = SIGMA1(a) + MAJ(a, b, c);
322 
323  //Update working registers
324  h = g;
325  g = f;
326  f = e;
327  e = d + temp1;
328  d = c;
329  c = b;
330  b = a;
331  a = temp1 + temp2;
332  }
333 
334  //Update the hash value
335  context->h[0] += a;
336  context->h[1] += b;
337  context->h[2] += c;
338  context->h[3] += d;
339  context->h[4] += e;
340  context->h[5] += f;
341  context->h[6] += g;
342  context->h[7] += h;
343 }
344 
345 #endif
uint8_t b
Definition: nbns_common.h:122
#define SIGMA4(x)
Definition: sha512.c:56
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1089
uint8_t a
Definition: ndp.h:411
#define SHA512_MIN_PAD_SIZE
Definition: sha512.h:47
uint64_t w[16]
Definition: sha512.h:66
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:224
#define SIGMA1(x)
Definition: sha512.c:53
SHA-512 (Secure Hash Algorithm 512)
#define SIGMA3(x)
Definition: sha512.c:55
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1090
#define LOAD64BE(p)
Definition: cpu_endian.h:246
size_t size
Definition: sha512.h:69
#define MAJ(x, y, z)
Definition: sha512.c:52
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
error_t
Error codes.
Definition: error.h:43
SHA-512 algorithm context.
Definition: sha512.h:62
General definitions for cryptographic algorithms.
uint8_t buffer[128]
Definition: sha512.h:67
__weak_func void sha512Final(Sha512Context *context, uint8_t *digest)
Finish the SHA-512 message digest.
Definition: sha512.c:240
uint8_t length
Definition: tcp.h:375
#define MIN(a, b)
Definition: os_port.h:63
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1091
#define STORE64BE(a, p)
Definition: cpu_endian.h:322
__weak_func void sha512Update(Sha512Context *context, const void *data, size_t length)
Update the SHA-512 context with a portion of the message being hashed.
Definition: sha512.c:201
__weak_func void sha512Init(Sha512Context *context)
Initialize SHA-512 message digest context.
Definition: sha512.c:175
uint64_t h[8]
Definition: sha512.h:63
uint8_t n
#define W(n)
Definition: sha512.c:48
const HashAlgo sha512HashAlgo
Definition: sha512.c:102
__weak_func error_t sha512Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-512.
Definition: sha512.c:130
#define cryptoFreeMem(p)
Definition: crypto.h:861
#define SHA512_BLOCK_SIZE
Definition: sha512.h:43
const uint8_t SHA512_OID[9]
Definition: sha512.c:99
#define cryptoAllocMem(size)
Definition: crypto.h:856
__weak_func void sha512ProcessBlock(Sha512Context *context)
Process message in 16-word blocks.
Definition: sha512.c:285
Common interface for hash algorithms.
Definition: crypto.h:1151
uint64_t totalSize
Definition: sha512.h:70
unsigned int uint_t
Definition: compiler_port.h:57
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1086
#define SIGMA2(x)
Definition: sha512.c:54
#define SHA512_DIGEST_SIZE
Definition: sha512.h:45
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:514
#define CH(x, y, z)
Definition: sha512.c:51