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-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-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.4.4
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) ^ SHR64(x, 7))
56 #define SIGMA4(x) (ROR64(x, 19) ^ ROR64(x, 61) ^ SHR64(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 //SHA-512 object identifier (2.16.840.1.101.3.4.2.3)
97 const uint8_t SHA512_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03};
98 
99 //Common interface for hash algorithms
101 {
102  "SHA-512",
103  SHA512_OID,
104  sizeof(SHA512_OID),
105  sizeof(Sha512Context),
109  TRUE,
114  NULL
115 };
116 
117 
118 /**
119  * @brief Digest a message using SHA-512
120  * @param[in] data Pointer to the message being hashed
121  * @param[in] length Length of the message
122  * @param[out] digest Pointer to the calculated digest
123  * @return Error code
124  **/
125 
126 __weak_func error_t sha512Compute(const void *data, size_t length, uint8_t *digest)
127 {
128 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
129  Sha512Context *context;
130 #else
131  Sha512Context context[1];
132 #endif
133 
134  //Check parameters
135  if(data == NULL && length != 0)
137 
138  if(digest == NULL)
140 
141 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
142  //Allocate a memory buffer to hold the SHA-512 context
143  context = cryptoAllocMem(sizeof(Sha512Context));
144  //Failed to allocate memory?
145  if(context == NULL)
146  return ERROR_OUT_OF_MEMORY;
147 #endif
148 
149  //Initialize the SHA-512 context
150  sha512Init(context);
151  //Digest the message
152  sha512Update(context, data, length);
153  //Finalize the SHA-512 message digest
154  sha512Final(context, digest);
155 
156 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
157  //Free previously allocated memory
158  cryptoFreeMem(context);
159 #endif
160 
161  //Successful processing
162  return NO_ERROR;
163 }
164 
165 
166 /**
167  * @brief Initialize SHA-512 message digest context
168  * @param[in] context Pointer to the SHA-512 context to initialize
169  **/
170 
171 __weak_func void sha512Init(Sha512Context *context)
172 {
173  //Set initial hash value
174  context->h[0] = 0x6A09E667F3BCC908;
175  context->h[1] = 0xBB67AE8584CAA73B;
176  context->h[2] = 0x3C6EF372FE94F82B;
177  context->h[3] = 0xA54FF53A5F1D36F1;
178  context->h[4] = 0x510E527FADE682D1;
179  context->h[5] = 0x9B05688C2B3E6C1F;
180  context->h[6] = 0x1F83D9ABFB41BD6B;
181  context->h[7] = 0x5BE0CD19137E2179;
182 
183  //Number of bytes in the buffer
184  context->size = 0;
185  //Total length of the message
186  context->totalSize = 0;
187 }
188 
189 
190 /**
191  * @brief Update the SHA-512 context with a portion of the message being hashed
192  * @param[in] context Pointer to the SHA-512 context
193  * @param[in] data Pointer to the buffer being hashed
194  * @param[in] length Length of the buffer
195  **/
196 
197 __weak_func void sha512Update(Sha512Context *context, const void *data, size_t length)
198 {
199  size_t n;
200 
201  //Process the incoming data
202  while(length > 0)
203  {
204  //The buffer can hold at most 128 bytes
205  n = MIN(length, 128 - context->size);
206 
207  //Copy the data to the buffer
208  osMemcpy(context->buffer + context->size, data, n);
209 
210  //Update the SHA-512 context
211  context->size += n;
212  context->totalSize += n;
213  //Advance the data pointer
214  data = (uint8_t *) data + n;
215  //Remaining bytes to process
216  length -= n;
217 
218  //Process message in 16-word blocks
219  if(context->size == 128)
220  {
221  //Transform the 16-word block
222  sha512ProcessBlock(context);
223  //Empty the buffer
224  context->size = 0;
225  }
226  }
227 }
228 
229 
230 /**
231  * @brief Finish the SHA-512 message digest
232  * @param[in] context Pointer to the SHA-512 context
233  * @param[out] digest Calculated digest (optional parameter)
234  **/
235 
236 __weak_func void sha512Final(Sha512Context *context, uint8_t *digest)
237 {
238  uint_t i;
239  size_t paddingSize;
240  uint64_t totalSize;
241 
242  //Length of the original message (before padding)
243  totalSize = context->totalSize * 8;
244 
245  //Pad the message so that its length is congruent to 112 modulo 128
246  if(context->size < 112)
247  {
248  paddingSize = 112 - context->size;
249  }
250  else
251  {
252  paddingSize = 128 + 112 - context->size;
253  }
254 
255  //Append padding
256  sha512Update(context, padding, paddingSize);
257 
258  //Append the length of the original message
259  context->w[14] = 0;
260  context->w[15] = htobe64(totalSize);
261 
262  //Calculate the message digest
263  sha512ProcessBlock(context);
264 
265  //Convert from host byte order to big-endian byte order
266  for(i = 0; i < 8; i++)
267  {
268  context->h[i] = htobe64(context->h[i]);
269  }
270 
271  //Copy the resulting digest
272  if(digest != NULL)
273  {
274  osMemcpy(digest, context->digest, SHA512_DIGEST_SIZE);
275  }
276 }
277 
278 
279 /**
280  * @brief Process message in 16-word blocks
281  * @param[in] context Pointer to the SHA-512 context
282  **/
283 
284 __weak_func void sha512ProcessBlock(Sha512Context *context)
285 {
286  uint_t i;
287  uint64_t temp1;
288  uint64_t temp2;
289 
290  //Initialize the 8 working registers
291  uint64_t a = context->h[0];
292  uint64_t b = context->h[1];
293  uint64_t c = context->h[2];
294  uint64_t d = context->h[3];
295  uint64_t e = context->h[4];
296  uint64_t f = context->h[5];
297  uint64_t g = context->h[6];
298  uint64_t h = context->h[7];
299 
300  //Process message in 16-word blocks
301  uint64_t *w = context->w;
302 
303  //Convert from big-endian byte order to host byte order
304  for(i = 0; i < 16; i++)
305  {
306  w[i] = betoh64(w[i]);
307  }
308 
309  //SHA-512 hash computation (alternate method)
310  for(i = 0; i < 80; i++)
311  {
312  //Prepare the message schedule
313  if(i >= 16)
314  {
315  W(i) += SIGMA4(W(i + 14)) + W(i + 9) + SIGMA3(W(i + 1));
316  }
317 
318  //Calculate T1 and T2
319  temp1 = h + SIGMA2(e) + CH(e, f, g) + k[i] + W(i);
320  temp2 = SIGMA1(a) + MAJ(a, b, c);
321 
322  //Update working registers
323  h = g;
324  g = f;
325  f = e;
326  e = d + temp1;
327  d = c;
328  c = b;
329  b = a;
330  a = temp1 + temp2;
331  }
332 
333  //Update the hash value
334  context->h[0] += a;
335  context->h[1] += b;
336  context->h[2] += c;
337  context->h[3] += d;
338  context->h[4] += e;
339  context->h[5] += f;
340  context->h[6] += g;
341  context->h[7] += h;
342 }
343 
344 #endif
uint8_t b
Definition: nbns_common.h:104
#define SIGMA4(x)
Definition: sha512.c:56
void(* HashAlgoInit)(void *context)
Definition: crypto.h:991
uint8_t a
Definition: ndp.h:411
#define SHA512_MIN_PAD_SIZE
Definition: sha512.h:47
uint64_t w[16]
Definition: sha512.h:70
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
#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:993
size_t size
Definition: sha512.h:73
#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:141
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:71
#define htobe64(value)
Definition: cpu_endian.h:447
__weak_func void sha512Final(Sha512Context *context, uint8_t *digest)
Finish the SHA-512 message digest.
Definition: sha512.c:236
uint8_t length
Definition: tcp.h:368
#define MIN(a, b)
Definition: os_port.h:63
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:995
__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:197
__weak_func void sha512Init(Sha512Context *context)
Initialize SHA-512 message digest context.
Definition: sha512.c:171
uint64_t h[8]
Definition: sha512.h:65
uint8_t n
#define W(n)
Definition: sha512.c:48
const HashAlgo sha512HashAlgo
Definition: sha512.c:100
#define betoh64(value)
Definition: cpu_endian.h:455
__weak_func error_t sha512Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-512.
Definition: sha512.c:126
#define cryptoFreeMem(p)
Definition: crypto.h:791
uint8_t digest[64]
Definition: sha512.h:66
#define SHA512_BLOCK_SIZE
Definition: sha512.h:43
const uint8_t SHA512_OID[9]
Definition: sha512.c:97
#define cryptoAllocMem(size)
Definition: crypto.h:786
__weak_func void sha512ProcessBlock(Sha512Context *context)
Process message in 16-word blocks.
Definition: sha512.c:284
Common interface for hash algorithms.
Definition: crypto.h:1046
uint64_t totalSize
Definition: sha512.h:74
unsigned int uint_t
Definition: compiler_port.h:50
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:988
#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