mcxn947_crypto.c
Go to the documentation of this file.
1 /**
2  * @file mcxn947_crypto.c
3  * @brief NXP MCX N947 hardware cryptographic 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.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include <ip_platform.h>
36 #include <mcuxClEls.h>
37 #include <mcuxClSession.h>
38 #include <mcuxClRandom.h>
39 #include <mcuxClRandomModes.h>
40 #include <mcuxClRsa.h>
41 #include <mcuxClEcc.h>
42 #include "core/crypto.h"
45 #include "debug.h"
46 
47 //Maximum cpuWA usage
48 #define MAX_CPUWA_SIZE MCUXCLCORE_MAX(MCUXCLRANDOMMODES_NCINIT_WACPU_SIZE, \
49  MCUXCLCORE_MAX(MCUXCLRANDOMMODES_INIT_WACPU_SIZE, \
50  MCUXCLCORE_MAX(MCUXCLRSA_VERIFY_NOVERIFY_WACPU_SIZE, \
51  MCUXCLCORE_MAX(MCUXCLRSA_SIGN_PLAIN_NOENCODE_4096_WACPU_SIZE, \
52  MCUXCLCORE_MAX(MCUXCLRSA_SIGN_CRT_NOENCODE_4096_WACPU_SIZE, \
53  MCUXCLCORE_MAX(MCUXCLECC_POINTMULT_WACPU_SIZE, \
54  MCUXCLCORE_MAX(MCUXCLECC_SIGN_WACPU_SIZE, \
55  MCUXCLCORE_MAX(MCUXCLECC_VERIFY_WACPU_SIZE, \
56  MCUXCLCORE_MAX(MCUXCLECC_MONTDH_KEYAGREEMENT_CURVE25519_WACPU_SIZE, \
57  MCUXCLCORE_MAX(MCUXCLECC_MONTDH_KEYAGREEMENT_CURVE448_WACPU_SIZE, \
58  MCUXCLCORE_MAX(MCUXCLECC_EDDSA_GENERATEKEYPAIR_ED25519_WACPU_SIZE, \
59  MCUXCLCORE_MAX(MCUXCLECC_EDDSA_GENERATESIGNATURE_ED25519_WACPU_SIZE, \
60  MCUXCLECC_EDDSA_VERIFYSIGNATURE_ED25519_WACPU_SIZE))))))))))))
61 
62 //Maximum pkcWA usage
63 #define MAX_PKCWA_SIZE MCUXCLCORE_MAX(MCUXCLRSA_VERIFY_4096_WAPKC_SIZE, \
64  MCUXCLCORE_MAX(MCUXCLRSA_SIGN_PLAIN_4096_WAPKC_SIZE, \
65  MCUXCLCORE_MAX(MCUXCLRSA_SIGN_CRT_4096_WAPKC_SIZE, \
66  MCUXCLCORE_MAX(MCUXCLECC_POINTMULT_WAPKC_SIZE_640, \
67  MCUXCLCORE_MAX(MCUXCLECC_SIGN_WAPKC_SIZE_640, \
68  MCUXCLCORE_MAX(MCUXCLECC_VERIFY_WAPKC_SIZE_640, \
69  MCUXCLCORE_MAX(MCUXCLECC_MONTDH_KEYAGREEMENT_CURVE25519_WAPKC_SIZE, \
70  MCUXCLCORE_MAX(MCUXCLECC_MONTDH_KEYAGREEMENT_CURVE448_WAPKC_SIZE, \
71  MCUXCLCORE_MAX(MCUXCLECC_EDDSA_GENERATEKEYPAIR_ED25519_WAPKC_SIZE, \
72  MCUXCLCORE_MAX(MCUXCLECC_EDDSA_GENERATESIGNATURE_ED25519_WAPKC_SIZE, \
73  MCUXCLECC_EDDSA_VERIFYSIGNATURE_ED25519_WAPKC_SIZE))))))))))
74 
75 //Global variables
77 mcuxClSession_Descriptor_t elsSession;
78 
79 //cpuWA buffer
80 static uint32_t cpuWaBuffer[MAX_CPUWA_SIZE / 4];
81 
82 
83 /**
84  * @brief Initialize hardware cryptographic accelerator
85  * @return Error code
86  **/
87 
89 {
90  error_t error;
91 
92  //Initialize status code
93  error = NO_ERROR;
94 
95  //Create a mutex to prevent simultaneous access to the hardware
96  //cryptographic accelerator
98  {
99  //Failed to create mutex
100  error = ERROR_OUT_OF_RESOURCES;
101  }
102 
103  //Check status code
104  if(!error)
105  {
106  //Enable ELS module
107  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(status, token, mcuxClEls_Enable_Async());
108 
109  //Check the protection token and the return value
110  if(token != MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClEls_Enable_Async) ||
111  status != MCUXCLELS_STATUS_OK_WAIT)
112  {
113  error = ERROR_FAILURE;
114  }
115 
116  //End of function call
117  MCUX_CSSL_FP_FUNCTION_CALL_END();
118  }
119 
120  //Check status code
121  if(!error)
122  {
123  //Wait for the operation to complete
124  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(status, token, mcuxClEls_WaitForOperation(
125  MCUXCLELS_ERROR_FLAGS_CLEAR));
126 
127  //Check the protection token and the return value
128  if(token != MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClEls_WaitForOperation) ||
129  status != MCUXCLELS_STATUS_OK)
130  {
131  error = ERROR_FAILURE;
132  }
133 
134  //End of function call
135  MCUX_CSSL_FP_FUNCTION_CALL_END();
136  }
137 
138  //Check status code
139  if(!error)
140  {
141  //Reset ELS module
142  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(status, token, mcuxClEls_Reset_Async(
143  MCUXCLELS_RESET_DO_NOT_CANCEL));
144 
145  //Check the protection token and the return value
146  if(token != MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClEls_Reset_Async) ||
147  status != MCUXCLELS_STATUS_OK_WAIT)
148  {
149  error = ERROR_FAILURE;
150  }
151 
152  //End of function call
153  MCUX_CSSL_FP_FUNCTION_CALL_END();
154  }
155 
156  //Check status code
157  if(!error)
158  {
159  //Wait for the operation to complete
160  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(status, token, mcuxClEls_WaitForOperation(
161  MCUXCLELS_ERROR_FLAGS_CLEAR));
162 
163  //Check the protection token and the return value
164  if(token != MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClEls_WaitForOperation) ||
165  status != MCUXCLELS_STATUS_OK)
166  {
167  error = ERROR_FAILURE;
168  }
169 
170  //End of function call
171  MCUX_CSSL_FP_FUNCTION_CALL_END();
172  }
173 
174  //Check status code
175  if(!error)
176  {
177  //Allocate and initialize session with pkcWA on the beginning of PKC RAM
178  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(status, token, mcuxClSession_init(
179  &elsSession, cpuWaBuffer, MAX_CPUWA_SIZE, (uint32_t *) PKC_RAM_ADDR,
180  MAX_PKCWA_SIZE));
181 
182  //Check the protection token and the return value
183  if(token != MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClSession_init) ||
184  status != MCUXCLSESSION_STATUS_OK)
185  {
186  error = ERROR_FAILURE;
187  }
188 
189  //End of function call
190  MCUX_CSSL_FP_FUNCTION_CALL_END();
191  }
192 
193  //Check status code
194  if(!error)
195  {
196  //Initialize RNG context
197  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(status, token, mcuxClRandom_init(
198  &elsSession, NULL, mcuxClRandomModes_Mode_ELS_Drbg));
199 
200  //Check the protection token and the return value
201  if(token != MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_init) ||
202  status != MCUXCLRANDOM_STATUS_OK)
203  {
204  error = ERROR_FAILURE;
205  }
206 
207  //End of function call
208  MCUX_CSSL_FP_FUNCTION_CALL_END();
209  }
210 
211  //Check status code
212  if(!error)
213  {
214  //Initialize PRNG
215  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(status, token, mcuxClRandom_ncInit(&elsSession));
216 
217  //Check the protection token and the return value
218  if(token != MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_ncInit) ||
219  status != MCUXCLRANDOM_STATUS_OK)
220  {
221  error = ERROR_FAILURE;
222  }
223 
224  //End of function call
225  MCUX_CSSL_FP_FUNCTION_CALL_END();
226  }
227 
228 #if (MCXN947_CRYPTO_TRNG_SUPPORT == ENABLED)
229  //Check status code
230  if(!error)
231  {
232  //Initialize TRNG module
233  error = trngInit();
234  }
235 #endif
236 
237  //Return status code
238  return error;
239 }
NXP MCX N947 hardware cryptographic accelerator.
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
#define MAX_CPUWA_SIZE
OsMutex mcxn947CryptoMutex
error_t trngInit(void)
TRNG module initialization.
error_t
Error codes.
Definition: error.h:43
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
NXP MCX N947 true random number generator.
General definitions for cryptographic algorithms.
#define MAX_PKCWA_SIZE
error_t mcxn947CryptoInit(void)
Initialize hardware cryptographic accelerator.
Mutex object.
mcuxClSession_Descriptor_t elsSession
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
uint8_t token[]
Definition: coap_common.h:181