rx65n_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file rx65n_crypto_hash.c
3  * @brief RX65N hash hardware accelerator
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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "r_tsip_rx_if.h"
36 #include "core/crypto.h"
39 #include "hash/hash_algorithms.h"
40 #include "debug.h"
41 
42 //Check crypto library configuration
43 #if (RX65N_CRYPTO_HASH_SUPPORT == ENABLED)
44 
45 //Padding string
46 static const uint8_t padding[64] =
47 {
48  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
52 };
53 
54 //Forward declaration of functions
55 e_tsip_err_t R_TSIP_Md5GenerateMessageDigestSub(const uint32_t *hashIn,
56  const uint32_t *data, uint32_t length, uint32_t *hashOut);
57 
58 e_tsip_err_t R_TSIP_Sha1GenerateMessageDigestSub(const uint32_t *hashIn,
59  const uint32_t *data, uint32_t length, uint32_t *hashOut);
60 
61 e_tsip_err_t R_TSIP_Sha224256GenerateMessageDigestSub(const uint32_t *hashIn,
62  const uint32_t *data, uint32_t length, uint32_t *hashOut);
63 
64 
65 #if (MD5_SUPPORT == ENABLED)
66 
67 /**
68  * @brief Initialize MD5 message digest context
69  * @param[in] context Pointer to the MD5 context to initialize
70  **/
71 
72 void md5Init(Md5Context *context)
73 {
74  //Set initial hash value
75  context->h[0] = BETOH32(0x67452301);
76  context->h[1] = BETOH32(0xEFCDAB89);
77  context->h[2] = BETOH32(0x98BADCFE);
78  context->h[3] = BETOH32(0x10325476);
79 
80  //Number of bytes in the buffer
81  context->size = 0;
82  //Total length of the message
83  context->totalSize = 0;
84 }
85 
86 
87 /**
88  * @brief Update the MD5 context with a portion of the message being hashed
89  * @param[in] context Pointer to the MD5 context
90  * @param[in] data Pointer to the buffer being hashed
91  * @param[in] length Length of the buffer
92  **/
93 
94 void md5Update(Md5Context *context, const void *data, size_t length)
95 {
96  size_t n;
97 
98  //Acquire exclusive access to the TSIP module
100 
101  //Process the incoming data
102  while(length > 0)
103  {
104  //Check whether some data is pending in the buffer
105  if(context->size == 0 && length >= 64)
106  {
107  //The length must be a multiple of 64 bytes
108  n = length - (length % 64);
109 
110  //Update hash value
112  context->h);
113 
114  //Update the MD5 context
115  context->totalSize += n;
116  //Advance the data pointer
117  data = (uint8_t *) data + n;
118  //Remaining bytes to process
119  length -= n;
120  }
121  else
122  {
123  //The buffer can hold at most 64 bytes
124  n = MIN(length, 64 - context->size);
125 
126  //Copy the data to the buffer
127  osMemcpy(context->buffer + context->size, data, n);
128 
129  //Update the MD5 context
130  context->size += n;
131  context->totalSize += n;
132  //Advance the data pointer
133  data = (uint8_t *) data + n;
134  //Remaining bytes to process
135  length -= n;
136 
137  //Check whether the buffer is full
138  if(context->size == 64)
139  {
140  //Update hash value
141  R_TSIP_Md5GenerateMessageDigestSub(context->h, context->x, 16,
142  context->h);
143 
144  //Empty the buffer
145  context->size = 0;
146  }
147  }
148  }
149 
150  //Release exclusive access to the TSIP module
152 }
153 
154 
155 /**
156  * @brief Finish the MD5 message digest
157  * @param[in] context Pointer to the MD5 context
158  * @param[out] digest Calculated digest (optional parameter)
159  **/
160 
161 void md5Final(Md5Context *context, uint8_t *digest)
162 {
163  uint_t i;
164  size_t paddingSize;
165  uint64_t totalSize;
166 
167  //Length of the original message (before padding)
168  totalSize = context->totalSize * 8;
169 
170  //Pad the message so that its length is congruent to 56 modulo 64
171  if(context->size < 56)
172  {
173  paddingSize = 56 - context->size;
174  }
175  else
176  {
177  paddingSize = 64 + 56 - context->size;
178  }
179 
180  //Append padding
181  md5Update(context, padding, paddingSize);
182 
183  //Append the length of the original message
184  context->x[14] = htole32((uint32_t) totalSize);
185  context->x[15] = htole32((uint32_t) (totalSize >> 32));
186 
187  //Calculate the message digest
188  md5ProcessBlock(context);
189 
190  //Convert from host byte order to big-endian byte order
191  for(i = 0; i < 4; i++)
192  {
193  context->h[i] = htobe32(context->h[i]);
194  }
195 
196  //Copy the resulting digest
197  if(digest != NULL)
198  {
199  osMemcpy(digest, context->digest, MD5_DIGEST_SIZE);
200  }
201 }
202 
203 
204 /**
205  * @brief Finish the MD5 message digest (no padding added)
206  * @param[in] context Pointer to the MD5 context
207  * @param[out] digest Calculated digest
208  **/
209 
210 void md5FinalRaw(Md5Context *context, uint8_t *digest)
211 {
212  uint_t i;
213 
214  //Convert from host byte order to big-endian byte order
215  for(i = 0; i < 4; i++)
216  {
217  context->h[i] = htobe32(context->h[i]);
218  }
219 
220  //Copy the resulting digest
221  osMemcpy(digest, context->digest, MD5_DIGEST_SIZE);
222 
223  //Convert from big-endian byte order to host byte order
224  for(i = 0; i < 4; i++)
225  {
226  context->h[i] = betoh32(context->h[i]);
227  }
228 }
229 
230 
231 /**
232  * @brief Process message in 16-word blocks
233  * @param[in] context Pointer to the MD5 context
234  **/
235 
237 {
238  //Acquire exclusive access to the TSIP module
240 
241  //Accelerate MD5 inner compression loop
242  R_TSIP_Md5GenerateMessageDigestSub(context->h, context->x, 16, context->h);
243 
244  //Release exclusive access to the TSIP module
246 }
247 
248 #endif
249 #if (SHA1_SUPPORT == ENABLED)
250 
251 /**
252  * @brief Initialize SHA-1 message digest context
253  * @param[in] context Pointer to the SHA-1 context to initialize
254  **/
255 
256 void sha1Init(Sha1Context *context)
257 {
258  //Set initial hash value
259  context->h[0] = BETOH32(0x67452301);
260  context->h[1] = BETOH32(0xEFCDAB89);
261  context->h[2] = BETOH32(0x98BADCFE);
262  context->h[3] = BETOH32(0x10325476);
263  context->h[4] = BETOH32(0xC3D2E1F0);
264 
265  //Number of bytes in the buffer
266  context->size = 0;
267  //Total length of the message
268  context->totalSize = 0;
269 }
270 
271 
272 /**
273  * @brief Update the SHA-1 context with a portion of the message being hashed
274  * @param[in] context Pointer to the SHA-1 context
275  * @param[in] data Pointer to the buffer being hashed
276  * @param[in] length Length of the buffer
277  **/
278 
279 void sha1Update(Sha1Context *context, const void *data, size_t length)
280 {
281  size_t n;
282 
283  //Acquire exclusive access to the TSIP module
285 
286  //Process the incoming data
287  while(length > 0)
288  {
289  //Check whether some data is pending in the buffer
290  if(context->size == 0 && length >= 64)
291  {
292  //The length must be a multiple of 64 bytes
293  n = length - (length % 64);
294 
295  //Update hash value
297  context->h);
298 
299  //Update the SHA-1 context
300  context->totalSize += n;
301  //Advance the data pointer
302  data = (uint8_t *) data + n;
303  //Remaining bytes to process
304  length -= n;
305  }
306  else
307  {
308  //The buffer can hold at most 64 bytes
309  n = MIN(length, 64 - context->size);
310 
311  //Copy the data to the buffer
312  osMemcpy(context->buffer + context->size, data, n);
313 
314  //Update the SHA-1 context
315  context->size += n;
316  context->totalSize += n;
317  //Advance the data pointer
318  data = (uint8_t *) data + n;
319  //Remaining bytes to process
320  length -= n;
321 
322  //Check whether the buffer is full
323  if(context->size == 64)
324  {
325  //Update hash value
326  R_TSIP_Sha1GenerateMessageDigestSub(context->h, context->w, 16,
327  context->h);
328 
329  //Empty the buffer
330  context->size = 0;
331  }
332  }
333  }
334 
335  //Release exclusive access to the TSIP module
337 }
338 
339 
340 /**
341  * @brief Finish the SHA-1 message digest
342  * @param[in] context Pointer to the SHA-1 context
343  * @param[out] digest Calculated digest (optional parameter)
344  **/
345 
346 void sha1Final(Sha1Context *context, uint8_t *digest)
347 {
348  size_t paddingSize;
349  uint64_t totalSize;
350 
351  //Length of the original message (before padding)
352  totalSize = context->totalSize * 8;
353 
354  //Pad the message so that its length is congruent to 56 modulo 64
355  if(context->size < 56)
356  {
357  paddingSize = 56 - context->size;
358  }
359  else
360  {
361  paddingSize = 64 + 56 - context->size;
362  }
363 
364  //Append padding
365  sha1Update(context, padding, paddingSize);
366 
367  //Append the length of the original message
368  context->w[14] = htobe32((uint32_t) (totalSize >> 32));
369  context->w[15] = htobe32((uint32_t) totalSize);
370 
371  //Calculate the message digest
372  sha1ProcessBlock(context);
373 
374  //Copy the resulting digest
375  if(digest != NULL)
376  {
377  osMemcpy(digest, context->digest, SHA1_DIGEST_SIZE);
378  }
379 }
380 
381 
382 /**
383  * @brief Finish the SHA-1 message digest (no padding added)
384  * @param[in] context Pointer to the SHA-1 context
385  * @param[out] digest Calculated digest
386  **/
387 
388 void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
389 {
390  //Copy the resulting digest
391  osMemcpy(digest, context->digest, SHA1_DIGEST_SIZE);
392 }
393 
394 
395 /**
396  * @brief Process message in 16-word blocks
397  * @param[in] context Pointer to the SHA-1 context
398  **/
399 
401 {
402  //Acquire exclusive access to the TSIP module
404 
405  //Accelerate SHA-1 inner compression loop
406  R_TSIP_Sha1GenerateMessageDigestSub(context->h, context->w, 16, context->h);
407 
408  //Release exclusive access to the TSIP module
410 }
411 
412 #endif
413 #if (SHA224_SUPPORT == ENABLED)
414 
415 /**
416  * @brief Initialize SHA-224 message digest context
417  * @param[in] context Pointer to the SHA-224 context to initialize
418  **/
419 
420 void sha224Init(Sha224Context *context)
421 {
422  //Set initial hash value
423  context->h[0] = BETOH32(0xC1059ED8);
424  context->h[1] = BETOH32(0x367CD507);
425  context->h[2] = BETOH32(0x3070DD17);
426  context->h[3] = BETOH32(0xF70E5939);
427  context->h[4] = BETOH32(0xFFC00B31);
428  context->h[5] = BETOH32(0x68581511);
429  context->h[6] = BETOH32(0x64F98FA7);
430  context->h[7] = BETOH32(0xBEFA4FA4);
431 
432  //Number of bytes in the buffer
433  context->size = 0;
434  //Total length of the message
435  context->totalSize = 0;
436 }
437 
438 #endif
439 #if (SHA256_SUPPORT == ENABLED)
440 
441 /**
442  * @brief Initialize SHA-256 message digest context
443  * @param[in] context Pointer to the SHA-256 context to initialize
444  **/
445 
446 void sha256Init(Sha256Context *context)
447 {
448  //Set initial hash value
449  context->h[0] = BETOH32(0x6A09E667);
450  context->h[1] = BETOH32(0xBB67AE85);
451  context->h[2] = BETOH32(0x3C6EF372);
452  context->h[3] = BETOH32(0xA54FF53A);
453  context->h[4] = BETOH32(0x510E527F);
454  context->h[5] = BETOH32(0x9B05688C);
455  context->h[6] = BETOH32(0x1F83D9AB);
456  context->h[7] = BETOH32(0x5BE0CD19);
457 
458  //Number of bytes in the buffer
459  context->size = 0;
460  //Total length of the message
461  context->totalSize = 0;
462 }
463 
464 
465 /**
466  * @brief Update the SHA-256 context with a portion of the message being hashed
467  * @param[in] context Pointer to the SHA-256 context
468  * @param[in] data Pointer to the buffer being hashed
469  * @param[in] length Length of the buffer
470  **/
471 
472 void sha256Update(Sha256Context *context, const void *data, size_t length)
473 {
474  size_t n;
475 
476  //Acquire exclusive access to the TSIP module
478 
479  //Process the incoming data
480  while(length > 0)
481  {
482  //Check whether some data is pending in the buffer
483  if(context->size == 0 && length >= 64)
484  {
485  //The length must be a multiple of 64 bytes
486  n = length - (length % 64);
487 
488  //Update hash value
490  context->h);
491 
492  //Update the SHA-256 context
493  context->totalSize += n;
494  //Advance the data pointer
495  data = (uint8_t *) data + n;
496  //Remaining bytes to process
497  length -= n;
498  }
499  else
500  {
501  //The buffer can hold at most 64 bytes
502  n = MIN(length, 64 - context->size);
503 
504  //Copy the data to the buffer
505  osMemcpy(context->buffer + context->size, data, n);
506 
507  //Update the SHA-256 context
508  context->size += n;
509  context->totalSize += n;
510  //Advance the data pointer
511  data = (uint8_t *) data + n;
512  //Remaining bytes to process
513  length -= n;
514 
515  //Check whether the buffer is full
516  if(context->size == 64)
517  {
518  //Update hash value
519  R_TSIP_Sha224256GenerateMessageDigestSub(context->h, context->w, 16,
520  context->h);
521 
522  //Empty the buffer
523  context->size = 0;
524  }
525  }
526  }
527 
528  //Release exclusive access to the TSIP module
530 }
531 
532 
533 /**
534  * @brief Finish the SHA-256 message digest
535  * @param[in] context Pointer to the SHA-256 context
536  * @param[out] digest Calculated digest (optional parameter)
537  **/
538 
539 void sha256Final(Sha256Context *context, uint8_t *digest)
540 {
541  size_t paddingSize;
542  uint64_t totalSize;
543 
544  //Length of the original message (before padding)
545  totalSize = context->totalSize * 8;
546 
547  //Pad the message so that its length is congruent to 56 modulo 64
548  if(context->size < 56)
549  {
550  paddingSize = 56 - context->size;
551  }
552  else
553  {
554  paddingSize = 64 + 56 - context->size;
555  }
556 
557  //Append padding
558  sha256Update(context, padding, paddingSize);
559 
560  //Append the length of the original message
561  context->w[14] = htobe32((uint32_t) (totalSize >> 32));
562  context->w[15] = htobe32((uint32_t) totalSize);
563 
564  //Calculate the message digest
565  sha256ProcessBlock(context);
566 
567  //Copy the resulting digest
568  if(digest != NULL)
569  {
570  osMemcpy(digest, context->digest, SHA256_DIGEST_SIZE);
571  }
572 }
573 
574 
575 /**
576  * @brief Finish the SHA-256 message digest (no padding added)
577  * @param[in] context Pointer to the SHA-256 context
578  * @param[out] digest Calculated digest
579  **/
580 
581 void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
582 {
583  //Copy the resulting digest
584  osMemcpy(digest, context->digest, SHA256_DIGEST_SIZE);
585 }
586 
587 
588 /**
589  * @brief Process message in 16-word blocks
590  * @param[in] context Pointer to the SHA-256 context
591  **/
592 
594 {
595  //Acquire exclusive access to the TSIP module
597 
598  //Accelerate SHA-256 inner compression loop
599  R_TSIP_Sha224256GenerateMessageDigestSub(context->h, context->w, 16,
600  context->h);
601 
602  //Release exclusive access to the TSIP module
604 }
605 
606 #endif
607 #endif
#define betoh32(value)
Definition: cpu_endian.h:454
void md5FinalRaw(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest (no padding added)
uint8_t digest[16]
Definition: md5.h:66
void sha1Final(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest.
SHA-256 algorithm context.
Definition: sha256.h:62
uint8_t data[]
Definition: ethernet.h:222
size_t size
Definition: sha256.h:73
uint32_t h[8]
Definition: sha256.h:65
void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest (no padding added)
void md5Init(Md5Context *context)
Initialize MD5 message digest context.
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
#define BETOH32(value)
Definition: cpu_endian.h:451
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
uint64_t totalSize
Definition: sha1.h:74
uint32_t x[16]
Definition: md5.h:70
OsMutex rx65nCryptoMutex
Definition: rx65n_crypto.c:41
uint8_t buffer[64]
Definition: md5.h:71
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
uint32_t w[16]
Definition: sha256.h:70
#define htole32(value)
Definition: cpu_endian.h:430
void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest (no padding added)
uint32_t h[5]
Definition: sha1.h:65
General definitions for cryptographic algorithms.
MD5 algorithm context.
Definition: md5.h:62
void sha256ProcessBlock(Sha256Context *context)
Process message in 16-word blocks.
void sha256Init(Sha256Context *context)
Initialize SHA-256 message digest context.
e_tsip_err_t R_TSIP_Md5GenerateMessageDigestSub(const uint32_t *hashIn, const uint32_t *data, uint32_t length, uint32_t *hashOut)
uint8_t length
Definition: tcp.h:368
uint8_t buffer[64]
Definition: sha256.h:71
#define MIN(a, b)
Definition: os_port.h:63
uint64_t totalSize
Definition: md5.h:74
#define MD5_DIGEST_SIZE
Definition: md5.h:45
void sha256Final(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest.
Collection of hash algorithms.
#define htobe32(value)
Definition: cpu_endian.h:446
void sha1ProcessBlock(Sha1Context *context)
Process message in 16-word blocks.
void md5ProcessBlock(Md5Context *context)
Process message in 16-word blocks.
void md5Update(Md5Context *context, const void *data, size_t length)
Update the MD5 context with a portion of the message being hashed.
uint8_t digest[32]
Definition: sha256.h:66
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
uint8_t n
void sha224Init(Sha224Context *context)
Initialize SHA-224 message digest context.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
RX65N hash hardware accelerator.
size_t size
Definition: sha1.h:73
size_t size
Definition: md5.h:73
uint32_t w[16]
Definition: sha1.h:70
SHA-1 algorithm context.
Definition: sha1.h:62
e_tsip_err_t R_TSIP_Sha224256GenerateMessageDigestSub(const uint32_t *hashIn, const uint32_t *data, uint32_t length, uint32_t *hashOut)
uint8_t buffer[64]
Definition: sha1.h:71
void sha1Init(Sha1Context *context)
Initialize SHA-1 message digest context.
void md5Final(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest.
uint64_t totalSize
Definition: sha256.h:74
unsigned int uint_t
Definition: compiler_port.h:50
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
e_tsip_err_t R_TSIP_Sha1GenerateMessageDigestSub(const uint32_t *hashIn, const uint32_t *data, uint32_t length, uint32_t *hashOut)
uint32_t h[4]
Definition: md5.h:65
uint8_t digest[20]
Definition: sha1.h:66
RX65N hardware cryptographic accelerator (TSIP)
Debugging facilities.