mk6x_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file mk6x_crypto_hash.c
3  * @brief Kinetis K6x 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.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "fsl_device_registers.h"
36 #include "fsl_mmcau.h"
37 #include "core/crypto.h"
40 #include "hash/hash_algorithms.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (MK6X_CRYPTO_HASH_SUPPORT == ENABLED)
45 #if (MD5_SUPPORT == ENABLED)
46 
47 /**
48  * @brief Update the MD5 context with a portion of the message being hashed
49  * @param[in] context Pointer to the MD5 context
50  * @param[in] data Pointer to the buffer being hashed
51  * @param[in] length Length of the buffer
52  **/
53 
54 void md5Update(Md5Context *context, const void *data, size_t length)
55 {
56  size_t n;
57 
58  //Acquire exclusive access to the MMCAU module
60 
61  //Process the incoming data
62  while(length > 0)
63  {
64  //Check whether some data is pending in the buffer
65  if(context->size == 0 && length >= 64)
66  {
67  //Update hash value
68  MMCAU_MD5_HashN(data, 1, context->h);
69 
70  //Update the MD5 context
71  context->totalSize += 64;
72  //Advance the data pointer
73  data = (uint8_t *) data + 64;
74  //Remaining bytes to process
75  length -= 64;
76  }
77  else
78  {
79  //The buffer can hold at most 64 bytes
80  n = MIN(length, 64 - context->size);
81 
82  //Copy the data to the buffer
83  osMemcpy(context->buffer + context->size, data, n);
84 
85  //Update the MD5 context
86  context->size += n;
87  context->totalSize += n;
88  //Advance the data pointer
89  data = (uint8_t *) data + n;
90  //Remaining bytes to process
91  length -= n;
92 
93  //Check whether the buffer is full
94  if(context->size == 64)
95  {
96  //Update hash value
97  MMCAU_MD5_HashN(context->buffer, 1, context->h);
98  //Empty the buffer
99  context->size = 0;
100  }
101  }
102  }
103 
104  //Release exclusive access to the MMCAU module
106 }
107 
108 
109 /**
110  * @brief Process message in 16-word blocks
111  * @param[in] context Pointer to the MD5 context
112  **/
113 
115 {
116  //Acquire exclusive access to the MMCAU module
118  //Accelerate MD5 inner compression loop
119  MMCAU_MD5_HashN(context->buffer, 1, context->h);
120  //Release exclusive access to the MMCAU module
122 }
123 
124 #endif
125 #if (SHA1_SUPPORT == ENABLED)
126 
127 /**
128  * @brief Update the SHA-1 context with a portion of the message being hashed
129  * @param[in] context Pointer to the SHA-1 context
130  * @param[in] data Pointer to the buffer being hashed
131  * @param[in] length Length of the buffer
132  **/
133 
134 void sha1Update(Sha1Context *context, const void *data, size_t length)
135 {
136  size_t n;
137 
138  //Acquire exclusive access to the MMCAU module
140 
141  //Process the incoming data
142  while(length > 0)
143  {
144  //Check whether some data is pending in the buffer
145  if(context->size == 0 && length >= 64)
146  {
147  //Update hash value
148  MMCAU_SHA1_HashN(data, 1, context->h);
149 
150  //Update the SHA-1 context
151  context->totalSize += 64;
152  //Advance the data pointer
153  data = (uint8_t *) data + 64;
154  //Remaining bytes to process
155  length -= 64;
156  }
157  else
158  {
159  //The buffer can hold at most 64 bytes
160  n = MIN(length, 64 - context->size);
161 
162  //Copy the data to the buffer
163  osMemcpy(context->buffer + context->size, data, n);
164 
165  //Update the SHA-1 context
166  context->size += n;
167  context->totalSize += n;
168  //Advance the data pointer
169  data = (uint8_t *) data + n;
170  //Remaining bytes to process
171  length -= n;
172 
173  //Check whether the buffer is full
174  if(context->size == 64)
175  {
176  //Update hash value
177  MMCAU_SHA1_HashN(context->buffer, 1, context->h);
178  //Empty the buffer
179  context->size = 0;
180  }
181  }
182  }
183 
184  //Release exclusive access to the MMCAU module
186 }
187 
188 
189 /**
190  * @brief Process message in 16-word blocks
191  * @param[in] context Pointer to the SHA-1 context
192  **/
193 
195 {
196  //Acquire exclusive access to the MMCAU module
198  //Accelerate SHA-1 inner compression loop
199  MMCAU_SHA1_HashN(context->buffer, 1, context->h);
200  //Release exclusive access to the MMCAU module
202 }
203 
204 #endif
205 #if (SHA256_SUPPORT == ENABLED)
206 
207 /**
208  * @brief Update the SHA-256 context with a portion of the message being hashed
209  * @param[in] context Pointer to the SHA-256 context
210  * @param[in] data Pointer to the buffer being hashed
211  * @param[in] length Length of the buffer
212  **/
213 
214 void sha256Update(Sha256Context *context, const void *data, size_t length)
215 {
216  size_t n;
217 
218  //Acquire exclusive access to the MMCAU module
220 
221  //Process the incoming data
222  while(length > 0)
223  {
224  //Check whether some data is pending in the buffer
225  if(context->size == 0 && length >= 64)
226  {
227  //Update hash value
228  MMCAU_SHA256_HashN(data, 1, context->h);
229 
230  //Update the SHA-256 context
231  context->totalSize += 64;
232  //Advance the data pointer
233  data = (uint8_t *) data + 64;
234  //Remaining bytes to process
235  length -= 64;
236  }
237  else
238  {
239  //The buffer can hold at most 64 bytes
240  n = MIN(length, 64 - context->size);
241 
242  //Copy the data to the buffer
243  osMemcpy(context->buffer + context->size, data, n);
244 
245  //Update the SHA-256 context
246  context->size += n;
247  context->totalSize += n;
248  //Advance the data pointer
249  data = (uint8_t *) data + n;
250  //Remaining bytes to process
251  length -= n;
252 
253  //Check whether the buffer is full
254  if(context->size == 64)
255  {
256  //Update hash value
257  MMCAU_SHA256_HashN(context->buffer, 1, context->h);
258  //Empty the buffer
259  context->size = 0;
260  }
261  }
262  }
263 
264  //Release exclusive access to the MMCAU module
266 }
267 
268 
269 /**
270  * @brief Process message in 16-word blocks
271  * @param[in] context Pointer to the SHA-256 context
272  **/
273 
275 {
276  //Acquire exclusive access to the MMCAU module
278  //Accelerate SHA-256 inner compression loop
279  MMCAU_SHA256_HashN(context->buffer, 1, context->h);
280  //Release exclusive access to the MMCAU module
282 }
283 
284 #endif
285 #endif
General definitions for cryptographic algorithms.
Debugging facilities.
uint8_t n
uint8_t data[]
Definition: ethernet.h:222
Collection of hash algorithms.
OsMutex mk6xCryptoMutex
Definition: mk6x_crypto.c:41
Kinetis K6x hardware cryptographic accelerator.
void sha1ProcessBlock(Sha1Context *context)
Process message in 16-word blocks.
void md5ProcessBlock(Md5Context *context)
Process message in 16-word blocks.
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
void sha256ProcessBlock(Sha256Context *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.
Kinetis K6x hash hardware accelerator.
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MIN(a, b)
Definition: os_port.h:63
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
MD5 algorithm context.
Definition: md5.h:62
uint32_t h[4]
Definition: md5.h:65
uint64_t totalSize
Definition: md5.h:74
size_t size
Definition: md5.h:73
uint8_t buffer[64]
Definition: md5.h:71
SHA-1 algorithm context.
Definition: sha1.h:62
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
SHA-256 algorithm context.
Definition: sha256.h:62
uint64_t totalSize
Definition: sha256.h:74
size_t size
Definition: sha256.h:73
uint8_t buffer[64]
Definition: sha256.h:71
uint32_t h[8]
Definition: sha256.h:65
uint8_t length
Definition: tcp.h:368