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-2024 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.4.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 (optional parameter)
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  context->w[14] = htobe32((uint32_t) (totalSize >> 32));
240  context->w[15] = htobe32((uint32_t) totalSize);
241 
242  //Calculate the message digest
243  sha1ProcessBlock(context);
244 
245  //Convert from host byte order to big-endian byte order
246  for(i = 0; i < 5; i++)
247  {
248  context->h[i] = htobe32(context->h[i]);
249  }
250 
251  //Copy the resulting digest
252  if(digest != NULL)
253  {
254  osMemcpy(digest, context->digest, SHA1_DIGEST_SIZE);
255  }
256 }
257 
258 
259 /**
260  * @brief Finish the SHA-1 message digest (no padding added)
261  * @param[in] context Pointer to the SHA-1 context
262  * @param[out] digest Calculated digest
263  **/
264 
265 __weak_func void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
266 {
267  uint_t i;
268 
269  //Convert from host byte order to big-endian byte order
270  for(i = 0; i < 5; i++)
271  {
272  context->h[i] = htobe32(context->h[i]);
273  }
274 
275  //Copy the resulting digest
276  osMemcpy(digest, context->digest, SHA1_DIGEST_SIZE);
277 
278  //Convert from big-endian byte order to host byte order
279  for(i = 0; i < 5; i++)
280  {
281  context->h[i] = betoh32(context->h[i]);
282  }
283 }
284 
285 
286 /**
287  * @brief Process message in 16-word blocks
288  * @param[in] context Pointer to the SHA-1 context
289  **/
290 
291 __weak_func void sha1ProcessBlock(Sha1Context *context)
292 {
293  uint_t i;
294  uint32_t temp;
295 
296  //Initialize the 5 working registers
297  uint32_t a = context->h[0];
298  uint32_t b = context->h[1];
299  uint32_t c = context->h[2];
300  uint32_t d = context->h[3];
301  uint32_t e = context->h[4];
302 
303  //Process message in 16-word blocks
304  uint32_t *w = context->w;
305 
306  //Convert from big-endian byte order to host byte order
307  for(i = 0; i < 16; i++)
308  {
309  w[i] = betoh32(w[i]);
310  }
311 
312  //SHA-1 hash computation (alternate method)
313  for(i = 0; i < 80; i++)
314  {
315  //Prepare the message schedule
316  if(i >= 16)
317  {
318  W(i) = ROL32(W(i + 13) ^ W(i + 8) ^ W(i + 2) ^ W(i), 1);
319  }
320 
321  //Calculate T
322  if(i < 20)
323  {
324  temp = ROL32(a, 5) + CH(b, c, d) + e + W(i) + k[0];
325  }
326  else if(i < 40)
327  {
328  temp = ROL32(a, 5) + PARITY(b, c, d) + e + W(i) + k[1];
329  }
330  else if(i < 60)
331  {
332  temp = ROL32(a, 5) + MAJ(b, c, d) + e + W(i) + k[2];
333  }
334  else
335  {
336  temp = ROL32(a, 5) + PARITY(b, c, d) + e + W(i) + k[3];
337  }
338 
339  //Update working registers
340  e = d;
341  d = c;
342  c = ROL32(b, 30);
343  b = a;
344  a = temp;
345  }
346 
347  //Update the hash value
348  context->h[0] += a;
349  context->h[1] += b;
350  context->h[2] += c;
351  context->h[3] += d;
352  context->h[4] += e;
353 }
354 
355 #endif
unsigned int uint_t
Definition: compiler_port.h:50
#define betoh32(value)
Definition: cpu_endian.h:454
#define htobe32(value)
Definition: cpu_endian.h:446
General definitions for cryptographic algorithms.
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:956
void(* HashAlgoFinalRaw)(void *context, uint8_t *digest)
Definition: crypto.h:965
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:963
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:961
#define cryptoAllocMem(size)
Definition: crypto.h:765
#define cryptoFreeMem(p)
Definition: crypto.h:770
#define ROL32(a, n)
Definition: crypto.h:776
void(* HashAlgoInit)(void *context)
Definition: crypto.h:959
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
uint8_t data[]
Definition: ethernet.h:222
uint8_t b
Definition: nbns_common.h:104
uint8_t c
Definition: ndp.h:514
uint8_t a
Definition: ndp.h:411
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MIN(a, b)
Definition: os_port.h:63
#define TRUE
Definition: os_port.h:50
#define CH(x, y, z)
Definition: sha1.c:50
__weak_func void sha1Init(Sha1Context *context)
Initialize SHA-1 message digest context.
Definition: sha1.c:154
__weak_func void sha1ProcessBlock(Sha1Context *context)
Process message in 16-word blocks.
Definition: sha1.c:291
__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
__weak_func void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest (no padding added)
Definition: sha1.c:265
__weak_func void sha1Final(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest.
Definition: sha1.c:216
#define W(n)
Definition: sha1.c:47
__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 PARITY(x, y, z)
Definition: sha1.c:51
const uint8_t SHA1_OID[5]
Definition: sha1.c:73
#define MAJ(x, y, z)
Definition: sha1.c:52
const HashAlgo sha1HashAlgo
Definition: sha1.c:76
SHA-1 (Secure Hash Algorithm 1)
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
#define SHA1_BLOCK_SIZE
Definition: sha1.h:43
#define SHA1_MIN_PAD_SIZE
Definition: sha1.h:47
Common interface for hash algorithms.
Definition: crypto.h:1014
SHA-1 algorithm context.
Definition: sha1.h:62
uint8_t digest[20]
Definition: sha1.h:66
uint64_t totalSize
Definition: sha1.h:74
size_t size
Definition: sha1.h:73
uint32_t h[5]
Definition: sha1.h:65
uint8_t buffer[64]
Definition: sha1.h:71
uint32_t w[16]
Definition: sha1.h:70
uint8_t length
Definition: tcp.h:368