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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneCrypto Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @section Description
26  *
27  * SHA-512 is a secure hash algorithm for computing a condensed representation
28  * of an electronic message. Refer to FIPS 180-4 for more details
29  *
30  * @author Oryx Embedded SARL (www.oryx-embedded.com)
31  * @version 1.9.0
32  **/
33 
34 //Switch to the appropriate trace level
35 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
36 
37 //Dependencies
38 #include "core/crypto.h"
39 #include "hash/sha512.h"
40 
41 //Check crypto library configuration
42 #if (SHA384_SUPPORT == ENABLED || SHA512_SUPPORT == ENABLED || \
43  SHA512_224_SUPPORT == ENABLED || SHA512_256_SUPPORT == ENABLED)
44 
45 //Macro to access the workspace as a circular buffer
46 #define W(t) w[(t) & 0x0F]
47 
48 //SHA-512 auxiliary functions
49 #define CH(x, y, z) (((x) & (y)) | (~(x) & (z)))
50 #define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
51 #define SIGMA1(x) (ROR64(x, 28) ^ ROR64(x, 34) ^ ROR64(x, 39))
52 #define SIGMA2(x) (ROR64(x, 14) ^ ROR64(x, 18) ^ ROR64(x, 41))
53 #define SIGMA3(x) (ROR64(x, 1) ^ ROR64(x, 8) ^ SHR64(x, 7))
54 #define SIGMA4(x) (ROR64(x, 19) ^ ROR64(x, 61) ^ SHR64(x, 6))
55 
56 //SHA-512 padding
57 static const uint8_t padding[128] =
58 {
59  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60  0x00, 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  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 };
68 
69 //SHA-512 constants
70 static const uint64_t k[80] =
71 {
72  0x428A2F98D728AE22, 0x7137449123EF65CD, 0xB5C0FBCFEC4D3B2F, 0xE9B5DBA58189DBBC,
73  0x3956C25BF348B538, 0x59F111F1B605D019, 0x923F82A4AF194F9B, 0xAB1C5ED5DA6D8118,
74  0xD807AA98A3030242, 0x12835B0145706FBE, 0x243185BE4EE4B28C, 0x550C7DC3D5FFB4E2,
75  0x72BE5D74F27B896F, 0x80DEB1FE3B1696B1, 0x9BDC06A725C71235, 0xC19BF174CF692694,
76  0xE49B69C19EF14AD2, 0xEFBE4786384F25E3, 0x0FC19DC68B8CD5B5, 0x240CA1CC77AC9C65,
77  0x2DE92C6F592B0275, 0x4A7484AA6EA6E483, 0x5CB0A9DCBD41FBD4, 0x76F988DA831153B5,
78  0x983E5152EE66DFAB, 0xA831C66D2DB43210, 0xB00327C898FB213F, 0xBF597FC7BEEF0EE4,
79  0xC6E00BF33DA88FC2, 0xD5A79147930AA725, 0x06CA6351E003826F, 0x142929670A0E6E70,
80  0x27B70A8546D22FFC, 0x2E1B21385C26C926, 0x4D2C6DFC5AC42AED, 0x53380D139D95B3DF,
81  0x650A73548BAF63DE, 0x766A0ABB3C77B2A8, 0x81C2C92E47EDAEE6, 0x92722C851482353B,
82  0xA2BFE8A14CF10364, 0xA81A664BBC423001, 0xC24B8B70D0F89791, 0xC76C51A30654BE30,
83  0xD192E819D6EF5218, 0xD69906245565A910, 0xF40E35855771202A, 0x106AA07032BBD1B8,
84  0x19A4C116B8D2D0C8, 0x1E376C085141AB53, 0x2748774CDF8EEB99, 0x34B0BCB5E19B48A8,
85  0x391C0CB3C5C95A63, 0x4ED8AA4AE3418ACB, 0x5B9CCA4F7763E373, 0x682E6FF3D6B2B8A3,
86  0x748F82EE5DEFB2FC, 0x78A5636F43172F60, 0x84C87814A1F0AB72, 0x8CC702081A6439EC,
87  0x90BEFFFA23631E28, 0xA4506CEBDE82BDE9, 0xBEF9A3F7B2C67915, 0xC67178F2E372532B,
88  0xCA273ECEEA26619C, 0xD186B8C721C0C207, 0xEADA7DD6CDE0EB1E, 0xF57D4F7FEE6ED178,
89  0x06F067AA72176FBA, 0x0A637DC5A2C898A6, 0x113F9804BEF90DAE, 0x1B710B35131C471B,
90  0x28DB77F523047D84, 0x32CAAB7B40C72493, 0x3C9EBE0A15C9BEBC, 0x431D67C49C100D4C,
91  0x4CC5D4BECB3E42B6, 0x597F299CFC657E2A, 0x5FCB6FAB3AD6FAEC, 0x6C44198C4A475817
92 };
93 
94 //SHA-512 object identifier (2.16.840.1.101.3.4.2.3)
95 const uint8_t sha512Oid[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03};
96 
97 //Common interface for hash algorithms
99 {
100  "SHA-512",
101  sha512Oid,
102  sizeof(sha512Oid),
103  sizeof(Sha512Context),
110 };
111 
112 
113 /**
114  * @brief Digest a message using SHA-512
115  * @param[in] data Pointer to the message being hashed
116  * @param[in] length Length of the message
117  * @param[out] digest Pointer to the calculated digest
118  * @return Error code
119  **/
120 
121 error_t sha512Compute(const void *data, size_t length, uint8_t *digest)
122 {
123  //Allocate a memory buffer to hold the SHA-512 context
124  Sha512Context *context = cryptoAllocMem(sizeof(Sha512Context));
125  //Failed to allocate memory?
126  if(context == NULL)
127  return ERROR_OUT_OF_MEMORY;
128 
129  //Initialize the SHA-512 context
130  sha512Init(context);
131  //Digest the message
132  sha512Update(context, data, length);
133  //Finalize the SHA-512 message digest
134  sha512Final(context, digest);
135 
136  //Free previously allocated memory
137  cryptoFreeMem(context);
138  //Successful processing
139  return NO_ERROR;
140 }
141 
142 
143 /**
144  * @brief Initialize SHA-512 message digest context
145  * @param[in] context Pointer to the SHA-512 context to initialize
146  **/
147 
148 void sha512Init(Sha512Context *context)
149 {
150  //Set initial hash value
151  context->h[0] = 0x6A09E667F3BCC908;
152  context->h[1] = 0xBB67AE8584CAA73B;
153  context->h[2] = 0x3C6EF372FE94F82B;
154  context->h[3] = 0xA54FF53A5F1D36F1;
155  context->h[4] = 0x510E527FADE682D1;
156  context->h[5] = 0x9B05688C2B3E6C1F;
157  context->h[6] = 0x1F83D9ABFB41BD6B;
158  context->h[7] = 0x5BE0CD19137E2179;
159 
160  //Number of bytes in the buffer
161  context->size = 0;
162  //Total length of the message
163  context->totalSize = 0;
164 }
165 
166 
167 /**
168  * @brief Update the SHA-512 context with a portion of the message being hashed
169  * @param[in] context Pointer to the SHA-512 context
170  * @param[in] data Pointer to the buffer being hashed
171  * @param[in] length Length of the buffer
172  **/
173 
174 void sha512Update(Sha512Context *context, const void *data, size_t length)
175 {
176  size_t n;
177 
178  //Process the incoming data
179  while(length > 0)
180  {
181  //The buffer can hold at most 128 bytes
182  n = MIN(length, 128 - context->size);
183 
184  //Copy the data to the buffer
185  cryptoMemcpy(context->buffer + context->size, data, n);
186 
187  //Update the SHA-512 context
188  context->size += n;
189  context->totalSize += n;
190  //Advance the data pointer
191  data = (uint8_t *) data + n;
192  //Remaining bytes to process
193  length -= n;
194 
195  //Process message in 16-word blocks
196  if(context->size == 128)
197  {
198  //Transform the 16-word block
199  sha512ProcessBlock(context);
200  //Empty the buffer
201  context->size = 0;
202  }
203  }
204 }
205 
206 
207 /**
208  * @brief Finish the SHA-512 message digest
209  * @param[in] context Pointer to the SHA-512 context
210  * @param[out] digest Calculated digest (optional parameter)
211  **/
212 
213 void sha512Final(Sha512Context *context, uint8_t *digest)
214 {
215  uint_t i;
216  size_t paddingSize;
217  uint64_t totalSize;
218 
219  //Length of the original message (before padding)
220  totalSize = context->totalSize * 8;
221 
222  //Pad the message so that its length is congruent to 112 modulo 128
223  if(context->size < 112)
224  paddingSize = 112 - context->size;
225  else
226  paddingSize = 128 + 112 - context->size;
227 
228  //Append padding
229  sha512Update(context, padding, paddingSize);
230 
231  //Append the length of the original message
232  context->w[14] = 0;
233  context->w[15] = htobe64(totalSize);
234 
235  //Calculate the message digest
236  sha512ProcessBlock(context);
237 
238  //Convert from host byte order to big-endian byte order
239  for(i = 0; i < 8; i++)
240  context->h[i] = htobe64(context->h[i]);
241 
242  //Copy the resulting digest
243  if(digest != NULL)
244  cryptoMemcpy(digest, context->digest, SHA512_DIGEST_SIZE);
245 }
246 
247 
248 /**
249  * @brief Process message in 16-word blocks
250  * @param[in] context Pointer to the SHA-512 context
251  **/
252 
254 {
255  uint_t t;
256  uint64_t temp1;
257  uint64_t temp2;
258 
259  //Initialize the 8 working registers
260  uint64_t a = context->h[0];
261  uint64_t b = context->h[1];
262  uint64_t c = context->h[2];
263  uint64_t d = context->h[3];
264  uint64_t e = context->h[4];
265  uint64_t f = context->h[5];
266  uint64_t g = context->h[6];
267  uint64_t h = context->h[7];
268 
269  //Process message in 16-word blocks
270  uint64_t *w = context->w;
271 
272  //Convert from big-endian byte order to host byte order
273  for(t = 0; t < 16; t++)
274  w[t] = betoh64(w[t]);
275 
276  //SHA-512 hash computation (alternate method)
277  for(t = 0; t < 80; t++)
278  {
279  //Prepare the message schedule
280  if(t >= 16)
281  W(t) += SIGMA4(W(t + 14)) + W(t + 9) + SIGMA3(W(t + 1));
282 
283  //Calculate T1 and T2
284  temp1 = h + SIGMA2(e) + CH(e, f, g) + k[t] + W(t);
285  temp2 = SIGMA1(a) + MAJ(a, b, c);
286 
287  //Update the working registers
288  h = g;
289  g = f;
290  f = e;
291  e = d + temp1;
292  d = c;
293  c = b;
294  b = a;
295  a = temp1 + temp2;
296  }
297 
298  //Update the hash value
299  context->h[0] += a;
300  context->h[1] += b;
301  context->h[2] += c;
302  context->h[3] += d;
303  context->h[4] += e;
304  context->h[5] += f;
305  context->h[6] += g;
306  context->h[7] += h;
307 }
308 
309 #endif
uint8_t c
Definition: ndp.h:510
#define cryptoMemcpy(dest, src, length)
Definition: crypto.h:590
#define cryptoFreeMem(p)
Definition: crypto.h:578
SHA-512 algorithm context.
Definition: sha512.h:54
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1020
#define cryptoAllocMem(size)
Definition: crypto.h:573
General definitions for cryptographic algorithms.
#define SIGMA4(x)
Definition: sha512.c:54
void sha512Final(Sha512Context *context, uint8_t *digest)
Finish the SHA-512 message digest.
Definition: sha512.c:213
size_t size
Definition: sha512.h:66
uint32_t totalSize
#define SHA512_BLOCK_SIZE
Definition: sha512.h:36
uint8_t a
Definition: ndp.h:407
uint16_t w[3]
Definition: ethernet.h:154
#define SIGMA1(x)
Definition: sha512.c:51
const HashAlgo sha512HashAlgo
Definition: sha512.c:98
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1021
const uint8_t sha512Oid[9]
Definition: sha512.c:95
uint8_t buffer[128]
Definition: sha512.h:64
#define CH(x, y, z)
Definition: sha512.c:49
#define MIN(a, b)
Definition: os_port.h:60
error_t sha512Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-512.
Definition: sha512.c:121
uint64_t h[8]
Definition: sha512.h:58
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:174
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1022
#define SIGMA3(x)
Definition: sha512.c:53
Success.
Definition: error.h:42
#define SIGMA2(x)
Definition: sha512.c:52
error_t
Error codes.
Definition: error.h:40
void sha512ProcessBlock(Sha512Context *context)
Process message in 16-word blocks.
Definition: sha512.c:253
#define SHA512_DIGEST_SIZE
Definition: sha512.h:38
#define MAJ(x, y, z)
Definition: sha512.c:50
unsigned int uint_t
Definition: compiler_port.h:43
#define betoh64(value)
Definition: cpu_endian.h:429
#define htobe64(value)
Definition: cpu_endian.h:421
uint8_t data[]
Definition: dtls_misc.h:167
SHA-512 (Secure Hash Algorithm 512)
uint64_t w[16]
Definition: sha512.h:63
void sha512Init(Sha512Context *context)
Initialize SHA-512 message digest context.
Definition: sha512.c:148
uint8_t digest[64]
Definition: sha512.h:59
Common interface for hash algorithms.
Definition: crypto.h:1054
uint8_t length
Definition: dtls_misc.h:140
uint8_t n
uint8_t h
Definition: ndp.h:297
uint64_t totalSize
Definition: sha512.h:67
uint8_t b[6]
Definition: dtls_misc.h:130
#define W(t)
Definition: sha512.c:46
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1023