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-2025 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.5.0
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(n) w[(n) & 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 SHA1_OID[5] = {0x2B, 0x0E, 0x03, 0x02, 0x1A};
74 
75 //Common interface for hash algorithms
77 {
78  "SHA-1",
79  SHA1_OID,
80  sizeof(SHA1_OID),
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 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
112  Sha1Context *context;
113 #else
114  Sha1Context context[1];
115 #endif
116 
117  //Check parameters
118  if(data == NULL && length != 0)
120 
121  if(digest == NULL)
123 
124 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
125  //Allocate a memory buffer to hold the SHA-1 context
126  context = cryptoAllocMem(sizeof(Sha1Context));
127  //Failed to allocate memory?
128  if(context == NULL)
129  return ERROR_OUT_OF_MEMORY;
130 #endif
131 
132  //Initialize the SHA-1 context
133  sha1Init(context);
134  //Digest the message
135  sha1Update(context, data, length);
136  //Finalize the SHA-1 message digest
137  sha1Final(context, digest);
138 
139 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
140  //Free previously allocated memory
141  cryptoFreeMem(context);
142 #endif
143 
144  //Successful processing
145  return NO_ERROR;
146 }
147 
148 
149 /**
150  * @brief Initialize SHA-1 message digest context
151  * @param[in] context Pointer to the SHA-1 context to initialize
152  **/
153 
154 __weak_func void sha1Init(Sha1Context *context)
155 {
156  //Set initial hash value
157  context->h[0] = 0x67452301;
158  context->h[1] = 0xEFCDAB89;
159  context->h[2] = 0x98BADCFE;
160  context->h[3] = 0x10325476;
161  context->h[4] = 0xC3D2E1F0;
162 
163  //Number of bytes in the buffer
164  context->size = 0;
165  //Total length of the message
166  context->totalSize = 0;
167 }
168 
169 
170 /**
171  * @brief Update the SHA-1 context with a portion of the message being hashed
172  * @param[in] context Pointer to the SHA-1 context
173  * @param[in] data Pointer to the buffer being hashed
174  * @param[in] length Length of the buffer
175  **/
176 
177 __weak_func void sha1Update(Sha1Context *context, const void *data, size_t length)
178 {
179  size_t n;
180 
181  //Process the incoming data
182  while(length > 0)
183  {
184  //The buffer can hold at most 64 bytes
185  n = MIN(length, 64 - context->size);
186 
187  //Copy the data to the buffer
188  osMemcpy(context->buffer + context->size, data, n);
189 
190  //Update the SHA-1 context
191  context->size += n;
192  context->totalSize += n;
193  //Advance the data pointer
194  data = (uint8_t *) data + n;
195  //Remaining bytes to process
196  length -= n;
197 
198  //Process message in 16-word blocks
199  if(context->size == 64)
200  {
201  //Transform the 16-word block
202  sha1ProcessBlock(context);
203  //Empty the buffer
204  context->size = 0;
205  }
206  }
207 }
208 
209 
210 /**
211  * @brief Finish the SHA-1 message digest
212  * @param[in] context Pointer to the SHA-1 context
213  * @param[out] digest Calculated digest
214  **/
215 
216 __weak_func void sha1Final(Sha1Context *context, uint8_t *digest)
217 {
218  uint_t i;
219  size_t paddingSize;
220  uint64_t totalSize;
221 
222  //Length of the original message (before padding)
223  totalSize = context->totalSize * 8;
224 
225  //Pad the message so that its length is congruent to 56 modulo 64
226  if(context->size < 56)
227  {
228  paddingSize = 56 - context->size;
229  }
230  else
231  {
232  paddingSize = 64 + 56 - context->size;
233  }
234 
235  //Append padding
236  sha1Update(context, padding, paddingSize);
237 
238  //Append the length of the original message
239  for(i = 0; i < 8; i++)
240  {
241  context->buffer[63 - i] = totalSize & 0xFF;
242  totalSize >>= 8;
243  }
244 
245  //Calculate the message digest
246  sha1ProcessBlock(context);
247 
248  //Copy the resulting digest
249  for(i = 0; i < (SHA1_DIGEST_SIZE / 4); i++)
250  {
251  STORE32BE(context->h[i], digest + i * 4);
252  }
253 }
254 
255 
256 /**
257  * @brief Finish the SHA-1 message digest (no padding added)
258  * @param[in] context Pointer to the SHA-1 context
259  * @param[out] digest Calculated digest
260  **/
261 
262 __weak_func void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
263 {
264  uint_t i;
265 
266  //Copy the resulting digest
267  for(i = 0; i < (SHA1_DIGEST_SIZE / 4); i++)
268  {
269  STORE32BE(context->h[i], digest + i * 4);
270  }
271 }
272 
273 
274 /**
275  * @brief Process message in 16-word blocks
276  * @param[in] context Pointer to the SHA-1 context
277  **/
278 
279 __weak_func void sha1ProcessBlock(Sha1Context *context)
280 {
281  uint_t i;
282  uint32_t temp;
283 
284  //Initialize the 5 working registers
285  uint32_t a = context->h[0];
286  uint32_t b = context->h[1];
287  uint32_t c = context->h[2];
288  uint32_t d = context->h[3];
289  uint32_t e = context->h[4];
290 
291  //Process message in 16-word blocks
292  uint32_t *w = context->w;
293 
294  //Convert from big-endian byte order to host byte order
295  for(i = 0; i < 16; i++)
296  {
297  w[i] = LOAD32BE(context->buffer + i * 4);
298  }
299 
300  //SHA-1 hash computation (alternate method)
301  for(i = 0; i < 80; i++)
302  {
303  //Prepare the message schedule
304  if(i >= 16)
305  {
306  W(i) = ROL32(W(i + 13) ^ W(i + 8) ^ W(i + 2) ^ W(i), 1);
307  }
308 
309  //Calculate T
310  if(i < 20)
311  {
312  temp = ROL32(a, 5) + CH(b, c, d) + e + W(i) + k[0];
313  }
314  else if(i < 40)
315  {
316  temp = ROL32(a, 5) + PARITY(b, c, d) + e + W(i) + k[1];
317  }
318  else if(i < 60)
319  {
320  temp = ROL32(a, 5) + MAJ(b, c, d) + e + W(i) + k[2];
321  }
322  else
323  {
324  temp = ROL32(a, 5) + PARITY(b, c, d) + e + W(i) + k[3];
325  }
326 
327  //Update working registers
328  e = d;
329  d = c;
330  c = ROL32(b, 30);
331  b = a;
332  a = temp;
333  }
334 
335  //Update the hash value
336  context->h[0] += a;
337  context->h[1] += b;
338  context->h[2] += c;
339  context->h[3] += d;
340  context->h[4] += e;
341 }
342 
343 #endif
uint8_t b
Definition: nbns_common.h:104
#define W(n)
Definition: sha1.c:47
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1027
uint8_t a
Definition: ndp.h:411
#define LOAD32BE(p)
Definition: cpu_endian.h:210
SHA-1 (Secure Hash Algorithm 1)
#define SHA1_BLOCK_SIZE
Definition: sha1.h:43
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
#define PARITY(x, y, z)
Definition: sha1.c:51
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1029
uint64_t totalSize
Definition: sha1.h:70
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
__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:177
error_t
Error codes.
Definition: error.h:43
__weak_func void sha1ProcessBlock(Sha1Context *context)
Process message in 16-word blocks.
Definition: sha1.c:279
uint32_t h[5]
Definition: sha1.h:63
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
uint8_t length
Definition: tcp.h:375
#define MIN(a, b)
Definition: os_port.h:63
__weak_func void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest (no padding added)
Definition: sha1.c:262
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1031
#define CH(x, y, z)
Definition: sha1.c:50
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
#define ROL32(a, n)
Definition: crypto.h:832
uint8_t n
#define cryptoFreeMem(p)
Definition: crypto.h:826
size_t size
Definition: sha1.h:69
uint32_t w[16]
Definition: sha1.h:66
#define cryptoAllocMem(size)
Definition: crypto.h:821
SHA-1 algorithm context.
Definition: sha1.h:62
const HashAlgo sha1HashAlgo
Definition: sha1.c:76
const uint8_t SHA1_OID[5]
Definition: sha1.c:73
#define MAJ(x, y, z)
Definition: sha1.c:52
Common interface for hash algorithms.
Definition: crypto.h:1082
uint8_t buffer[64]
Definition: sha1.h:67
__weak_func void sha1Final(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest.
Definition: sha1.c:216
unsigned int uint_t
Definition: compiler_port.h:57
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1024
void(* HashAlgoFinalRaw)(void *context, uint8_t *digest)
Definition: crypto.h:1033
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:514
#define SHA1_MIN_PAD_SIZE
Definition: sha1.h:47
__weak_func void sha1Init(Sha1Context *context)
Initialize SHA-1 message digest context.
Definition: sha1.c:154