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