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