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-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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.0
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
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  for(i = 0; i < 8; i++)
185  {
186  context->buffer[56 + i] = totalSize & 0xFF;
187  totalSize >>= 8;
188  }
189 
190  //Calculate the message digest
191  md5ProcessBlock(context);
192 
193  //Copy the resulting digest
194  for(i = 0; i < (MD5_DIGEST_SIZE / 4); i++)
195  {
196  STORE32BE(context->h[i], digest + i * 4);
197  }
198 }
199 
200 
201 /**
202  * @brief Finish the MD5 message digest (no padding added)
203  * @param[in] context Pointer to the MD5 context
204  * @param[out] digest Calculated digest
205  **/
206 
207 void md5FinalRaw(Md5Context *context, uint8_t *digest)
208 {
209  uint_t i;
210 
211  //Copy the resulting digest
212  for(i = 0; i < (MD5_DIGEST_SIZE / 4); i++)
213  {
214  STORE32BE(context->h[i], digest + i * 4);
215  }
216 }
217 
218 
219 /**
220  * @brief Process message in 16-word blocks
221  * @param[in] context Pointer to the MD5 context
222  **/
223 
225 {
226  //Acquire exclusive access to the TSIP module
228 
229  //Accelerate MD5 inner compression loop
230  R_TSIP_Md5GenerateMessageDigestSub(context->h, context->x, 16, context->h);
231 
232  //Release exclusive access to the TSIP module
234 }
235 
236 #endif
237 #if (SHA1_SUPPORT == ENABLED)
238 
239 /**
240  * @brief Initialize SHA-1 message digest context
241  * @param[in] context Pointer to the SHA-1 context to initialize
242  **/
243 
244 void sha1Init(Sha1Context *context)
245 {
246  //Set initial hash value
247  context->h[0] = BETOH32(0x67452301);
248  context->h[1] = BETOH32(0xEFCDAB89);
249  context->h[2] = BETOH32(0x98BADCFE);
250  context->h[3] = BETOH32(0x10325476);
251  context->h[4] = BETOH32(0xC3D2E1F0);
252 
253  //Number of bytes in the buffer
254  context->size = 0;
255  //Total length of the message
256  context->totalSize = 0;
257 }
258 
259 
260 /**
261  * @brief Update the SHA-1 context with a portion of the message being hashed
262  * @param[in] context Pointer to the SHA-1 context
263  * @param[in] data Pointer to the buffer being hashed
264  * @param[in] length Length of the buffer
265  **/
266 
267 void sha1Update(Sha1Context *context, const void *data, size_t length)
268 {
269  size_t n;
270 
271  //Acquire exclusive access to the TSIP module
273 
274  //Process the incoming data
275  while(length > 0)
276  {
277  //Check whether some data is pending in the buffer
278  if(context->size == 0 && length >= 64)
279  {
280  //The length must be a multiple of 64 bytes
281  n = length - (length % 64);
282 
283  //Update hash value
285  context->h);
286 
287  //Update the SHA-1 context
288  context->totalSize += n;
289  //Advance the data pointer
290  data = (uint8_t *) data + n;
291  //Remaining bytes to process
292  length -= n;
293  }
294  else
295  {
296  //The buffer can hold at most 64 bytes
297  n = MIN(length, 64 - context->size);
298 
299  //Copy the data to the buffer
300  osMemcpy(context->buffer + context->size, data, n);
301 
302  //Update the SHA-1 context
303  context->size += n;
304  context->totalSize += n;
305  //Advance the data pointer
306  data = (uint8_t *) data + n;
307  //Remaining bytes to process
308  length -= n;
309 
310  //Check whether the buffer is full
311  if(context->size == 64)
312  {
313  //Update hash value
314  R_TSIP_Sha1GenerateMessageDigestSub(context->h, context->w, 16,
315  context->h);
316 
317  //Empty the buffer
318  context->size = 0;
319  }
320  }
321  }
322 
323  //Release exclusive access to the TSIP module
325 }
326 
327 
328 /**
329  * @brief Finish the SHA-1 message digest
330  * @param[in] context Pointer to the SHA-1 context
331  * @param[out] digest Calculated digest
332  **/
333 
334 void sha1Final(Sha1Context *context, uint8_t *digest)
335 {
336  uint_t i;
337  size_t paddingSize;
338  uint64_t totalSize;
339 
340  //Length of the original message (before padding)
341  totalSize = context->totalSize * 8;
342 
343  //Pad the message so that its length is congruent to 56 modulo 64
344  if(context->size < 56)
345  {
346  paddingSize = 56 - context->size;
347  }
348  else
349  {
350  paddingSize = 64 + 56 - context->size;
351  }
352 
353  //Append padding
354  sha1Update(context, padding, paddingSize);
355 
356  //Append the length of the original message
357  for(i = 0; i < 8; i++)
358  {
359  context->buffer[63 - i] = totalSize & 0xFF;
360  totalSize >>= 8;
361  }
362 
363  //Calculate the message digest
364  sha1ProcessBlock(context);
365 
366  //Copy the resulting digest
367  for(i = 0; i < (SHA1_DIGEST_SIZE / 4); i++)
368  {
369  STORE32LE(context->h[i], digest + i * 4);
370  }
371 }
372 
373 
374 /**
375  * @brief Finish the SHA-1 message digest (no padding added)
376  * @param[in] context Pointer to the SHA-1 context
377  * @param[out] digest Calculated digest
378  **/
379 
380 void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
381 {
382  uint_t i;
383 
384  //Copy the resulting digest
385  for(i = 0; i < (SHA1_DIGEST_SIZE / 4); i++)
386  {
387  STORE32LE(context->h[i], digest + i * 4);
388  }
389 }
390 
391 
392 /**
393  * @brief Process message in 16-word blocks
394  * @param[in] context Pointer to the SHA-1 context
395  **/
396 
398 {
399  //Acquire exclusive access to the TSIP module
401 
402  //Accelerate SHA-1 inner compression loop
403  R_TSIP_Sha1GenerateMessageDigestSub(context->h, context->w, 16, context->h);
404 
405  //Release exclusive access to the TSIP module
407 }
408 
409 #endif
410 #if (SHA224_SUPPORT == ENABLED)
411 
412 /**
413  * @brief Initialize SHA-224 message digest context
414  * @param[in] context Pointer to the SHA-224 context to initialize
415  **/
416 
417 void sha224Init(Sha224Context *context)
418 {
419  //Set initial hash value
420  context->h[0] = BETOH32(0xC1059ED8);
421  context->h[1] = BETOH32(0x367CD507);
422  context->h[2] = BETOH32(0x3070DD17);
423  context->h[3] = BETOH32(0xF70E5939);
424  context->h[4] = BETOH32(0xFFC00B31);
425  context->h[5] = BETOH32(0x68581511);
426  context->h[6] = BETOH32(0x64F98FA7);
427  context->h[7] = BETOH32(0xBEFA4FA4);
428 
429  //Number of bytes in the buffer
430  context->size = 0;
431  //Total length of the message
432  context->totalSize = 0;
433 }
434 
435 #endif
436 #if (SHA256_SUPPORT == ENABLED)
437 
438 /**
439  * @brief Initialize SHA-256 message digest context
440  * @param[in] context Pointer to the SHA-256 context to initialize
441  **/
442 
443 void sha256Init(Sha256Context *context)
444 {
445  //Set initial hash value
446  context->h[0] = BETOH32(0x6A09E667);
447  context->h[1] = BETOH32(0xBB67AE85);
448  context->h[2] = BETOH32(0x3C6EF372);
449  context->h[3] = BETOH32(0xA54FF53A);
450  context->h[4] = BETOH32(0x510E527F);
451  context->h[5] = BETOH32(0x9B05688C);
452  context->h[6] = BETOH32(0x1F83D9AB);
453  context->h[7] = BETOH32(0x5BE0CD19);
454 
455  //Number of bytes in the buffer
456  context->size = 0;
457  //Total length of the message
458  context->totalSize = 0;
459 }
460 
461 
462 /**
463  * @brief Update the SHA-256 context with a portion of the message being hashed
464  * @param[in] context Pointer to the SHA-256 context
465  * @param[in] data Pointer to the buffer being hashed
466  * @param[in] length Length of the buffer
467  **/
468 
469 void sha256Update(Sha256Context *context, const void *data, size_t length)
470 {
471  size_t n;
472 
473  //Acquire exclusive access to the TSIP module
475 
476  //Process the incoming data
477  while(length > 0)
478  {
479  //Check whether some data is pending in the buffer
480  if(context->size == 0 && length >= 64)
481  {
482  //The length must be a multiple of 64 bytes
483  n = length - (length % 64);
484 
485  //Update hash value
487  context->h);
488 
489  //Update the SHA-256 context
490  context->totalSize += n;
491  //Advance the data pointer
492  data = (uint8_t *) data + n;
493  //Remaining bytes to process
494  length -= n;
495  }
496  else
497  {
498  //The buffer can hold at most 64 bytes
499  n = MIN(length, 64 - context->size);
500 
501  //Copy the data to the buffer
502  osMemcpy(context->buffer + context->size, data, n);
503 
504  //Update the SHA-256 context
505  context->size += n;
506  context->totalSize += n;
507  //Advance the data pointer
508  data = (uint8_t *) data + n;
509  //Remaining bytes to process
510  length -= n;
511 
512  //Check whether the buffer is full
513  if(context->size == 64)
514  {
515  //Update hash value
516  R_TSIP_Sha224256GenerateMessageDigestSub(context->h, context->w, 16,
517  context->h);
518 
519  //Empty the buffer
520  context->size = 0;
521  }
522  }
523  }
524 
525  //Release exclusive access to the TSIP module
527 }
528 
529 
530 /**
531  * @brief Finish the SHA-256 message digest
532  * @param[in] context Pointer to the SHA-256 context
533  * @param[out] digest Calculated digest
534  **/
535 
536 void sha256Final(Sha256Context *context, uint8_t *digest)
537 {
538  uint_t i;
539  size_t paddingSize;
540  uint64_t totalSize;
541 
542  //Length of the original message (before padding)
543  totalSize = context->totalSize * 8;
544 
545  //Pad the message so that its length is congruent to 56 modulo 64
546  if(context->size < 56)
547  {
548  paddingSize = 56 - context->size;
549  }
550  else
551  {
552  paddingSize = 64 + 56 - context->size;
553  }
554 
555  //Append padding
556  sha256Update(context, padding, paddingSize);
557 
558  //Append the length of the original message
559  for(i = 0; i < 8; i++)
560  {
561  context->buffer[63 - i] = totalSize & 0xFF;
562  totalSize >>= 8;
563  }
564 
565  //Calculate the message digest
566  sha256ProcessBlock(context);
567 
568  //Copy the resulting digest
569  for(i = 0; i < (SHA256_DIGEST_SIZE / 4); i++)
570  {
571  STORE32LE(context->h[i], digest + i * 4);
572  }
573 }
574 
575 
576 /**
577  * @brief Finish the SHA-256 message digest (no padding added)
578  * @param[in] context Pointer to the SHA-256 context
579  * @param[out] digest Calculated digest
580  **/
581 
582 void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
583 {
584  uint_t i;
585 
586  //Copy the resulting digest
587  for(i = 0; i < (SHA256_DIGEST_SIZE / 4); i++)
588  {
589  STORE32LE(context->h[i], digest + i * 4);
590  }
591 }
592 
593 
594 /**
595  * @brief Process message in 16-word blocks
596  * @param[in] context Pointer to the SHA-256 context
597  **/
598 
600 {
601  //Acquire exclusive access to the TSIP module
603 
604  //Accelerate SHA-256 inner compression loop
605  R_TSIP_Sha224256GenerateMessageDigestSub(context->h, context->w, 16,
606  context->h);
607 
608  //Release exclusive access to the TSIP module
610 }
611 
612 #endif
613 #endif
void md5FinalRaw(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest (no padding added)
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
#define STORE32LE(a, p)
Definition: cpu_endian.h:279
size_t size
Definition: sha256.h:69
uint32_t h[8]
Definition: sha256.h:63
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:70
uint32_t x[16]
Definition: md5.h:66
OsMutex rx65nCryptoMutex
Definition: rx65n_crypto.c:41
uint8_t buffer[64]
Definition: md5.h:67
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
uint32_t w[16]
Definition: sha256.h:66
void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest (no padding added)
uint32_t h[5]
Definition: sha1.h:63
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:375
uint8_t buffer[64]
Definition: sha256.h:67
#define MIN(a, b)
Definition: os_port.h:63
uint64_t totalSize
Definition: md5.h:70
#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.
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.
#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:69
size_t size
Definition: md5.h:69
uint32_t w[16]
Definition: sha1.h:66
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:67
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:70
unsigned int uint_t
Definition: compiler_port.h:57
#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)
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
uint32_t h[4]
Definition: md5.h:63
RX65N hardware cryptographic accelerator (TSIP)
Debugging facilities.