mimxrt1060_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file mimxrt1060_crypto_hash.c
3  * @brief i.MX RT1060 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 "fsl_device_registers.h"
36 #include "fsl_dcp.h"
37 #include "core/crypto.h"
40 #include "hash/hash_algorithms.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (MIMXRT1060_CRYPTO_HASH_SUPPORT == ENABLED)
45 
46 //IAR EWARM compiler?
47 #if defined(__ICCARM__)
48 
49 //DCP hash context
50 #pragma data_alignment = 16
51 #pragma location = MIMXRT1060_DCP_RAM_SECTION
52 static dcp_hash_ctx_t dcpHashContext;
53 
54 //DCP buffer
55 #pragma data_alignment = 16
56 #pragma location = MIMXRT1060_DCP_RAM_SECTION
57 static uint8_t dcpBuffer[MIMXRT1060_DCP_BUFFER_SIZE];
58 
59 //ARM or GCC compiler?
60 #else
61 
62 //DCP hash context
63 static dcp_hash_ctx_t dcpHashContext
64  __attribute__((aligned(16), __section__(MIMXRT1060_DCP_RAM_SECTION)));
65 
66 //DCP buffer
67 static uint8_t dcpBuffer[MIMXRT1060_DCP_BUFFER_SIZE]
68  __attribute__((aligned(16), __section__(MIMXRT1060_DCP_RAM_SECTION)));
69 
70 #endif
71 
72 #if (SHA1_SUPPORT == ENABLED)
73 
74 /**
75  * @brief Digest a message using SHA-1
76  * @param[in] data Pointer to the message being hashed
77  * @param[in] length Length of the message
78  * @param[out] digest Pointer to the calculated digest
79  * @return Error code
80  **/
81 
82 error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
83 {
84  size_t n;
85  status_t status;
86  dcp_handle_t dcpHandle;
87 
88  //Set DCP parameters
89  dcpHandle.channel = kDCP_Channel0;
90  dcpHandle.keySlot = kDCP_KeySlot0;
91  dcpHandle.swapConfig = kDCP_NoSwap;
92 
93  //Acquire exclusive access to the DCP module
95 
96  //Initialize hash computation
97  status = DCP_HASH_Init(DCP, &dcpHandle, &dcpHashContext, kDCP_Sha1);
98 
99  //Digest the message
100  while(length > 0 && status == kStatus_Success)
101  {
102  //Limit the number of data to process at a time
104  //Copy the data to the buffer
105  osMemcpy(dcpBuffer, data, n);
106 
107  //Update hash value
108  status = DCP_HASH_Update(DCP, &dcpHashContext, dcpBuffer, n);
109 
110  //Advance the data pointer
111  data = (uint8_t *) data + n;
112  //Remaining bytes to process
113  length -= n;
114  }
115 
116  //Check status code
117  if(status == kStatus_Success)
118  {
119  //Specify the size of the output buffer
121  //Finalize hash computation
122  status = DCP_HASH_Finish(DCP, &dcpHashContext, digest, &n);
123  }
124 
125  //Release exclusive access to the DCP module
127 
128  //Return status code
129  return (status == kStatus_Success) ? NO_ERROR : ERROR_FAILURE;
130 }
131 
132 
133 /**
134  * @brief Initialize SHA-1 message digest context
135  * @param[in] context Pointer to the SHA-1 context to initialize
136  **/
137 
138 void sha1Init(Sha1Context *context)
139 {
140  dcp_handle_t *dcpHandle;
141 
142  //Point to the DCP handle
143  dcpHandle = &context->dcpHandle;
144 
145  //Set DCP parameters
146  dcpHandle->channel = kDCP_Channel0;
147  dcpHandle->keySlot = kDCP_KeySlot0;
148  dcpHandle->swapConfig = kDCP_NoSwap;
149 
150  //Acquire exclusive access to the DCP module
152 
153  //Initialize hash computation
154  DCP_HASH_Init(DCP, dcpHandle, &dcpHashContext, kDCP_Sha1);
155  //Save hash context
156  context->dcpContext = dcpHashContext;
157 
158  //Release exclusive access to the DCP module
160 }
161 
162 
163 /**
164  * @brief Update the SHA-1 context with a portion of the message being hashed
165  * @param[in] context Pointer to the SHA-1 context
166  * @param[in] data Pointer to the buffer being hashed
167  * @param[in] length Length of the buffer
168  **/
169 
170 void sha1Update(Sha1Context *context, const void *data, size_t length)
171 {
172  size_t n;
173 
174  //Acquire exclusive access to the DCP module
176 
177  //Restore hash context
178  dcpHashContext = context->dcpContext;
179 
180  //Digest the message
181  while(length > 0)
182  {
183  //Limit the number of data to process at a time
185  //Copy the data to the buffer
186  osMemcpy(dcpBuffer, data, n);
187 
188  //Update hash value
189  DCP_HASH_Update(DCP, &dcpHashContext, dcpBuffer, n);
190 
191  //Advance the data pointer
192  data = (uint8_t *) data + n;
193  //Remaining bytes to process
194  length -= n;
195  }
196 
197  //Save hash context
198  context->dcpContext = dcpHashContext;
199 
200  //Release exclusive access to the DCP module
202 }
203 
204 
205 /**
206  * @brief Finish the SHA-1 message digest
207  * @param[in] context Pointer to the SHA-1 context
208  * @param[out] digest Calculated digest
209  **/
210 
211 void sha1Final(Sha1Context *context, uint8_t *digest)
212 {
213  size_t n;
214 
215  //Specify the size of the output buffer
217 
218  //Acquire exclusive access to the DCP module
220 
221  //Restore hash context
222  dcpHashContext = context->dcpContext;
223  //Finalize hash computation
224  DCP_HASH_Finish(DCP, &dcpHashContext, digest, &n);
225 
226  //Release exclusive access to the DCP module
228 }
229 
230 #endif
231 #if (SHA256_SUPPORT == ENABLED)
232 
233 /**
234  * @brief Digest a message using SHA-256
235  * @param[in] data Pointer to the message being hashed
236  * @param[in] length Length of the message
237  * @param[out] digest Pointer to the calculated digest
238  * @return Error code
239  **/
240 
241 error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
242 {
243  size_t n;
244  status_t status;
245  dcp_handle_t dcpHandle;
246 
247  //Set DCP parameters
248  dcpHandle.channel = kDCP_Channel0;
249  dcpHandle.keySlot = kDCP_KeySlot0;
250  dcpHandle.swapConfig = kDCP_NoSwap;
251 
252  //Acquire exclusive access to the DCP module
254 
255  //Initialize hash computation
256  status = DCP_HASH_Init(DCP, &dcpHandle, &dcpHashContext, kDCP_Sha256);
257 
258  //Digest the message
259  while(length > 0 && status == kStatus_Success)
260  {
261  //Limit the number of data to process at a time
263  //Copy the data to the buffer
264  osMemcpy(dcpBuffer, data, n);
265 
266  //Update hash value
267  status = DCP_HASH_Update(DCP, &dcpHashContext, dcpBuffer, n);
268 
269  //Advance the data pointer
270  data = (uint8_t *) data + n;
271  //Remaining bytes to process
272  length -= n;
273  }
274 
275  //Check status code
276  if(status == kStatus_Success)
277  {
278  //Specify the size of the output buffer
280  //Finalize hash computation
281  status = DCP_HASH_Finish(DCP, &dcpHashContext, digest, &n);
282  }
283 
284  //Release exclusive access to the DCP module
286 
287  //Return status code
288  return (status == kStatus_Success) ? NO_ERROR : ERROR_FAILURE;
289 }
290 
291 
292 /**
293  * @brief Initialize SHA-256 message digest context
294  * @param[in] context Pointer to the SHA-256 context to initialize
295  **/
296 
297 void sha256Init(Sha256Context *context)
298 {
299  dcp_handle_t *dcpHandle;
300 
301  //Point to the DCP handle
302  dcpHandle = &context->dcpHandle;
303 
304  //Set DCP parameters
305  dcpHandle->channel = kDCP_Channel0;
306  dcpHandle->keySlot = kDCP_KeySlot0;
307  dcpHandle->swapConfig = kDCP_NoSwap;
308 
309  //Acquire exclusive access to the DCP module
311 
312  //Initialize hash computation
313  DCP_HASH_Init(DCP, dcpHandle, &dcpHashContext, kDCP_Sha256);
314  //Save hash context
315  context->dcpContext = dcpHashContext;
316 
317  //Release exclusive access to the DCP module
319 }
320 
321 
322 /**
323  * @brief Update the SHA-256 context with a portion of the message being hashed
324  * @param[in] context Pointer to the SHA-256 context
325  * @param[in] data Pointer to the buffer being hashed
326  * @param[in] length Length of the buffer
327  **/
328 
329 void sha256Update(Sha256Context *context, const void *data, size_t length)
330 {
331  size_t n;
332 
333  //Acquire exclusive access to the DCP module
335 
336  //Restore hash context
337  dcpHashContext = context->dcpContext;
338 
339  //Digest the message
340  while(length > 0)
341  {
342  //Limit the number of data to process at a time
344  //Copy the data to the buffer
345  osMemcpy(dcpBuffer, data, n);
346 
347  //Update hash value
348  DCP_HASH_Update(DCP, &dcpHashContext, dcpBuffer, n);
349 
350  //Advance the data pointer
351  data = (uint8_t *) data + n;
352  //Remaining bytes to process
353  length -= n;
354  }
355 
356  //Save hash context
357  context->dcpContext = dcpHashContext;
358 
359  //Release exclusive access to the DCP module
361 }
362 
363 
364 /**
365  * @brief Finish the SHA-256 message digest
366  * @param[in] context Pointer to the SHA-256 context
367  * @param[out] digest Calculated digest
368  **/
369 
370 void sha256Final(Sha256Context *context, uint8_t *digest)
371 {
372  size_t n;
373 
374  //Specify the size of the output buffer
376 
377  //Acquire exclusive access to the DCP module
379 
380  //Restore hash context
381  dcpHashContext = context->dcpContext;
382  //Finalize hash computation
383  DCP_HASH_Finish(DCP, &dcpHashContext, digest, &n);
384 
385  //Release exclusive access to the DCP module
387 }
388 
389 #endif
390 #endif
i.MX RT1060 hash hardware accelerator
#define MIMXRT1060_DCP_BUFFER_SIZE
SHA-256 algorithm context.
Definition: sha256.h:62
void sha256Init(Sha256Context *context)
Initialize SHA-256 message digest context.
void sha1Final(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest.
uint8_t data[]
Definition: ethernet.h:222
error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-256.
void sha1Init(Sha1Context *context)
Initialize SHA-1 message digest context.
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
error_t
Error codes.
Definition: error.h:43
OsMutex mimxrt1060CryptoMutex
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
General definitions for cryptographic algorithms.
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
uint8_t length
Definition: tcp.h:375
#define MIN(a, b)
Definition: os_port.h:63
Collection of hash algorithms.
error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-1.
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
uint8_t n
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
#define MIMXRT1060_DCP_RAM_SECTION
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
SHA-1 algorithm context.
Definition: sha1.h:62
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
i.MX RT1060 hardware cryptographic accelerator (DCP)
void sha256Final(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest.
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.