mcxn547_crypto_trng.c
Go to the documentation of this file.
1 /**
2  * @file mcxn547_crypto_trng.c
3  * @brief NXP MCX N547 true random number generator
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 <mcuxClEls.h>
36 #include <internal/mcuxClTrng_Internal.h>
37 #include "core/crypto.h"
40 #include "debug.h"
41 
42 //Global variables
43 static uint32_t buffer[MCUXCLTRNG_ELS_TRNG_OUTPUT_SIZE / 4];
44 static size_t bufferPos;
45 
46 //Check crypto library configuration
47 #if (MCXN547_CRYPTO_TRNG_SUPPORT == ENABLED)
48 
49 
50 /**
51  * @brief TRNG module initialization
52  * @return Error code
53  **/
54 
56 {
57  error_t error;
58 
59  //Initialize status code
60  error = NO_ERROR;
61 
62  //Mark the buffer as empty
63  bufferPos = MCUXCLTRNG_ELS_TRNG_OUTPUT_SIZE;
64 
65  //Initialize TRNG
66  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(status, token, mcuxClTrng_Init());
67 
68  //Check the protection token and the return value
69  if(token != MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClTrng_Init) ||
70  status != MCUXCLTRNG_STATUS_OK)
71  {
72  error = ERROR_FAILURE;
73  }
74 
75  //End of function call
76  MCUX_CSSL_FP_FUNCTION_CALL_END();
77 
78  //Return status code
79  return error;
80 }
81 
82 
83 /**
84  * @brief Get random data from the TRNG module
85  * @param[out] data Buffer where to store random data
86  * @param[in] length Number of random bytes to generate
87  **/
88 
90 {
91  error_t error;
92  size_t i;
93  uint32_t temp;
94 
95  //Initialize status code
96  error = NO_ERROR;
97 
98  //Acquire exclusive access to the ELS module
100 
101  //Generate random data
102  for(i = 0; i < length && !error; i++)
103  {
104  //Generate a new 32-byte random value when necessary
105  if(bufferPos >= MCUXCLTRNG_ELS_TRNG_OUTPUT_SIZE)
106  {
107  //Generate random bytes
108  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(status, token, mcuxClTrng_getEntropyInput(
109  &elsSession, buffer, MCUXCLTRNG_ELS_TRNG_OUTPUT_SIZE));
110 
111  //Check the protection token and the return value
112  if(token != MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClTrng_getEntropyInput) ||
113  status != MCUXCLTRNG_STATUS_OK)
114  {
115  error = ERROR_FAILURE;
116  }
117 
118  //End of function call
119  MCUX_CSSL_FP_FUNCTION_CALL_END();
120 
121  //Rewind to the beginning of the buffer
122  bufferPos = 0;
123  }
124 
125  //Extract a random byte
126  data[i] = (buffer[bufferPos / 4] >> ((bufferPos % 4) * 8)) & 0xFF;
127  //Increment index
128  bufferPos++;
129  }
130 
131  //Release exclusive access to the ELS module
133 
134  //Return status code
135  return error;
136 }
137 
138 #endif
uint8_t data[]
Definition: ethernet.h:224
error_t
Error codes.
Definition: error.h:43
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
error_t trngGetRandomData(uint8_t *data, size_t length)
Get random data from the TRNG module.
mcuxClSession_Descriptor_t elsSession
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:375
NXP MCX N547 true random number generator.
OsMutex mcxn547CryptoMutex
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
NXP MCX N547 hardware cryptographic accelerator.
error_t trngInit(void)
TRNG module initialization.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
uint8_t token[]
Definition: coap_common.h:181