sha1.c
Go to the documentation of this file.
1 /**
2  * @file sha1.c
3  * @brief SHA-1 (Secure Hash Algorithm 1)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2023 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-1 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.2.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/sha1.h"
42 
43 //Check crypto library configuration
44 #if (SHA1_SUPPORT == ENABLED)
45 
46 //Macro to access the workspace as a circular buffer
47 #define W(t) w[(t) & 0x0F]
48 
49 //SHA-1 auxiliary functions
50 #define CH(x, y, z) (((x) & (y)) | (~(x) & (z)))
51 #define PARITY(x, y, z) ((x) ^ (y) ^ (z))
52 #define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
53 
54 //SHA-1 padding
55 static const uint8_t padding[64] =
56 {
57  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59  0x00, 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 };
62 
63 //SHA-1 constants
64 static const uint32_t k[4] =
65 {
66  0x5A827999,
67  0x6ED9EBA1,
68  0x8F1BBCDC,
69  0xCA62C1D6
70 };
71 
72 //SHA-1 object identifier (1.3.14.3.2.26)
73 const uint8_t sha1Oid[5] = {0x2B, 0x0E, 0x03, 0x02, 0x1A};
74 
75 //Common interface for hash algorithms
77 {
78  "SHA-1",
79  sha1Oid,
80  sizeof(sha1Oid),
81  sizeof(Sha1Context),
85  TRUE,
90 #if ((defined(MIMXRT1050_CRYPTO_HASH_SUPPORT) && MIMXRT1050_CRYPTO_HASH_SUPPORT == ENABLED) || \
91  (defined(MIMXRT1060_CRYPTO_HASH_SUPPORT) && MIMXRT1060_CRYPTO_HASH_SUPPORT == ENABLED) || \
92  (defined(MIMXRT1160_CRYPTO_HASH_SUPPORT) && MIMXRT1160_CRYPTO_HASH_SUPPORT == ENABLED) || \
93  (defined(MIMXRT1170_CRYPTO_HASH_SUPPORT) && MIMXRT1170_CRYPTO_HASH_SUPPORT == ENABLED))
94  NULL,
95 #else
97 #endif
98 };
99 
100 
101 /**
102  * @brief Digest a message using SHA-1
103  * @param[in] data Pointer to the message being hashed
104  * @param[in] length Length of the message
105  * @param[out] digest Pointer to the calculated digest
106  * @return Error code
107  **/
108 
109 __weak_func error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
110 {
111  error_t error;
112  Sha1Context *context;
113 
114  //Allocate a memory buffer to hold the SHA-1 context
115  context = cryptoAllocMem(sizeof(Sha1Context));
116 
117  //Successful memory allocation?
118  if(context != NULL)
119  {
120  //Initialize the SHA-1 context
121  sha1Init(context);
122  //Digest the message
123  sha1Update(context, data, length);
124  //Finalize the SHA-1 message digest
125  sha1Final(context, digest);
126 
127  //Free previously allocated memory
128  cryptoFreeMem(context);
129 
130  //Successful processing
131  error = NO_ERROR;
132  }
133  else
134  {
135  //Failed to allocate memory
136  error = ERROR_OUT_OF_MEMORY;
137  }
138 
139  //Return status code
140  return error;
141 }
142 
143 
144 /**
145  * @brief Initialize SHA-1 message digest context
146  * @param[in] context Pointer to the SHA-1 context to initialize
147  **/
148 
149 __weak_func void sha1Init(Sha1Context *context)
150 {
151  //Set initial hash value
152  context->h[0] = 0x67452301;
153  context->h[1] = 0xEFCDAB89;
154  context->h[2] = 0x98BADCFE;
155  context->h[3] = 0x10325476;
156  context->h[4] = 0xC3D2E1F0;
157 
158  //Number of bytes in the buffer
159  context->size = 0;
160  //Total length of the message
161  context->totalSize = 0;
162 }
163 
164 
165 /**
166  * @brief Update the SHA-1 context with a portion of the message being hashed
167  * @param[in] context Pointer to the SHA-1 context
168  * @param[in] data Pointer to the buffer being hashed
169  * @param[in] length Length of the buffer
170  **/
171 
172 __weak_func void sha1Update(Sha1Context *context, const void *data, size_t length)
173 {
174  size_t n;
175 
176  //Process the incoming data
177  while(length > 0)
178  {
179  //The buffer can hold at most 64 bytes
180  n = MIN(length, 64 - context->size);
181 
182  //Copy the data to the buffer
183  osMemcpy(context->buffer + context->size, data, n);
184 
185  //Update the SHA-1 context
186  context->size += n;
187  context->totalSize += n;
188  //Advance the data pointer
189  data = (uint8_t *) data + n;
190  //Remaining bytes to process
191  length -= n;
192 
193  //Process message in 16-word blocks
194  if(context->size == 64)
195  {
196  //Transform the 16-word block
197  sha1ProcessBlock(context);
198  //Empty the buffer
199  context->size = 0;
200  }
201  }
202 }
203 
204 
205 /**
206  * @brief Finish the SHA-1 message digest
207  * @param[in] context Pointer to the SHA-1 context
208  * @param[out] digest Calculated digest (optional parameter)
209  **/
210 
211 __weak_func void sha1Final(Sha1Context *context, uint8_t *digest)
212 {
213  uint_t i;
214  size_t paddingSize;
215  uint64_t totalSize;
216 
217  //Length of the original message (before padding)
218  totalSize = context->totalSize * 8;
219 
220  //Pad the message so that its length is congruent to 56 modulo 64
221  if(context->size < 56)
222  {
223  paddingSize = 56 - context->size;
224  }
225  else
226  {
227  paddingSize = 64 + 56 - context->size;
228  }
229 
230  //Append padding
231  sha1Update(context, padding, paddingSize);
232 
233  //Append the length of the original message
234  context->w[14] = htobe32((uint32_t) (totalSize >> 32));
235  context->w[15] = htobe32((uint32_t) totalSize);
236 
237  //Calculate the message digest
238  sha1ProcessBlock(context);
239 
240  //Convert from host byte order to big-endian byte order
241  for(i = 0; i < 5; i++)
242  {
243  context->h[i] = htobe32(context->h[i]);
244  }
245 
246  //Copy the resulting digest
247  if(digest != NULL)
248  {
249  osMemcpy(digest, context->digest, SHA1_DIGEST_SIZE);
250  }
251 }
252 
253 
254 /**
255  * @brief Finish the SHA-1 message digest (no padding added)
256  * @param[in] context Pointer to the SHA-1 context
257  * @param[out] digest Calculated digest
258  **/
259 
260 __weak_func void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
261 {
262  uint_t i;
263 
264  //Convert from host byte order to big-endian byte order
265  for(i = 0; i < 5; i++)
266  {
267  context->h[i] = htobe32(context->h[i]);
268  }
269 
270  //Copy the resulting digest
271  osMemcpy(digest, context->digest, SHA1_DIGEST_SIZE);
272 
273  //Convert from big-endian byte order to host byte order
274  for(i = 0; i < 5; i++)
275  {
276  context->h[i] = betoh32(context->h[i]);
277  }
278 }
279 
280 
281 /**
282  * @brief Process message in 16-word blocks
283  * @param[in] context Pointer to the SHA-1 context
284  **/
285 
286 __weak_func void sha1ProcessBlock(Sha1Context *context)
287 {
288  uint_t t;
289  uint32_t temp;
290 
291  //Initialize the 5 working registers
292  uint32_t a = context->h[0];
293  uint32_t b = context->h[1];
294  uint32_t c = context->h[2];
295  uint32_t d = context->h[3];
296  uint32_t e = context->h[4];
297 
298  //Process message in 16-word blocks
299  uint32_t *w = context->w;
300 
301  //Convert from big-endian byte order to host byte order
302  for(t = 0; t < 16; t++)
303  {
304  w[t] = betoh32(w[t]);
305  }
306 
307  //SHA-1 hash computation (alternate method)
308  for(t = 0; t < 80; t++)
309  {
310  //Prepare the message schedule
311  if(t >= 16)
312  {
313  W(t) = ROL32(W(t + 13) ^ W(t + 8) ^ W(t + 2) ^ W(t), 1);
314  }
315 
316  //Calculate T
317  if(t < 20)
318  {
319  temp = ROL32(a, 5) + CH(b, c, d) + e + W(t) + k[0];
320  }
321  else if(t < 40)
322  {
323  temp = ROL32(a, 5) + PARITY(b, c, d) + e + W(t) + k[1];
324  }
325  else if(t < 60)
326  {
327  temp = ROL32(a, 5) + MAJ(b, c, d) + e + W(t) + k[2];
328  }
329  else
330  {
331  temp = ROL32(a, 5) + PARITY(b, c, d) + e + W(t) + k[3];
332  }
333 
334  //Update the working registers
335  e = d;
336  d = c;
337  c = ROL32(b, 30);
338  b = a;
339  a = temp;
340  }
341 
342  //Update the hash value
343  context->h[0] += a;
344  context->h[1] += b;
345  context->h[2] += c;
346  context->h[3] += d;
347  context->h[4] += e;
348 }
349 
350 #endif
uint8_t length
Definition: coap_common.h:193
#define betoh32(value)
Definition: cpu_endian.h:454
void(* HashAlgoInit)(void *context)
Definition: crypto.h:882
uint8_t a
Definition: ndp.h:409
uint8_t data[]
Definition: ethernet.h:220
SHA-1 (Secure Hash Algorithm 1)
#define SHA1_BLOCK_SIZE
Definition: sha1.h:43
uint8_t t
Definition: lldp_ext_med.h:210
#define TRUE
Definition: os_port.h:50
#define PARITY(x, y, z)
Definition: sha1.c:51
@ 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:884
uint64_t totalSize
Definition: sha1.h:76
#define osMemcpy(dest, src, length)
Definition: os_port.h:137
__weak_func void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
Definition: sha1.c:172
error_t
Error codes.
Definition: error.h:43
__weak_func void sha1ProcessBlock(Sha1Context *context)
Process message in 16-word blocks.
Definition: sha1.c:286
uint32_t h[5]
Definition: sha1.h:67
General definitions for cryptographic algorithms.
__weak_func error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-1.
Definition: sha1.c:109
#define MIN(a, b)
Definition: os_port.h:62
__weak_func void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest (no padding added)
Definition: sha1.c:260
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:886
#define CH(x, y, z)
Definition: sha1.c:50
#define htobe32(value)
Definition: cpu_endian.h:446
uint8_t b[6]
Definition: ethernet.h:190
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
#define ROL32(a, n)
Definition: crypto.h:699
uint8_t n
#define cryptoFreeMem(p)
Definition: crypto.h:693
uint32_t totalSize
size_t size
Definition: sha1.h:75
uint32_t w[16]
Definition: sha1.h:72
#define cryptoAllocMem(size)
Definition: crypto.h:688
SHA-1 algorithm context.
Definition: sha1.h:64
const HashAlgo sha1HashAlgo
Definition: sha1.c:76
#define MAJ(x, y, z)
Definition: sha1.c:52
Common interface for hash algorithms.
Definition: crypto.h:937
uint8_t buffer[64]
Definition: sha1.h:73
__weak_func void sha1Final(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest.
Definition: sha1.c:211
unsigned int uint_t
Definition: compiler_port.h:50
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:879
void(* HashAlgoFinalRaw)(void *context, uint8_t *digest)
Definition: crypto.h:888
#define W(t)
Definition: sha1.c:47
uint8_t digest[20]
Definition: sha1.h:68
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:512
#define SHA1_MIN_PAD_SIZE
Definition: sha1.h:47
const uint8_t sha1Oid[5]
Definition: sha1.c:73
__weak_func void sha1Init(Sha1Context *context)
Initialize SHA-1 message digest context.
Definition: sha1.c:149