pic32cz_crypto_trng.c
Go to the documentation of this file.
1 /**
2  * @file pic32cz_crypto_trng.c
3  * @brief PIC32CZ 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.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "pic32c.h"
36 #include "core/crypto.h"
39 #include "debug.h"
40 
41 //Check crypto library configuration
42 #if (PIC32CZ_CRYPTO_TRNG_SUPPORT == ENABLED)
43 
44 
45 /**
46  * @brief TRNG module initialization
47  * @return Error code
48  **/
49 
51 {
52 #if defined(__PIC32CZ2051CA70064__) || defined(__PIC32CZ2051CA70100__) || \
53  defined(__PIC32CZ2051CA70144__)
54  //Enable TRNG peripheral clock
55  PMC_REGS->PMC_PCER1 = (1U << (ID_TRNG - 32));
56  //Enable TRNG
57  TRNG_REGS->TRNG_CR = TRNG_CR_KEY_PASSWD | TRNG_CR_ENABLE_Msk;
58 #else
59  //Enable TRNG bus clock (CLK_TRNG_APB)
60  MCLK_REGS->MCLK_CLKMSK[TRNG_MCLK_ID_APB / 32] |= (1U << (TRNG_MCLK_ID_APB % 32));
61  //Enable TRNG
62  TRNG_REGS->TRNG_CTRLA |= TRNG_CTRLA_ENABLE_Msk;
63 #endif
64 
65  //Successful initialization
66  return NO_ERROR;
67 }
68 
69 
70 /**
71  * @brief Get random data from the TRNG module
72  * @param[out] data Buffer where to store random data
73  * @param[in] length Number of random bytes to generate
74  **/
75 
77 {
78  size_t i;
79  uint32_t value;
80 
81  //Initialize variable
82  value = 0;
83 
84  //Acquire exclusive access to the TRNG module
86 
87  //Generate random data
88  for(i = 0; i < length; i++)
89  {
90  //Generate a new 32-bit random value when necessary
91  if((i % 4) == 0)
92  {
93 #if defined(__PIC32CZ2051CA70064__) || defined(__PIC32CZ2051CA70100__) || \
94  defined(__PIC32CZ2051CA70144__)
95  //Wait for the TRNG to contain a valid data
96  while((TRNG_REGS->TRNG_ISR & TRNG_ISR_DATRDY_Msk) == 0)
97  {
98  }
99 
100  //Get the 32-bit random value
101  value = TRNG_REGS->TRNG_ODATA;
102 #else
103  //Wait for the TRNG to contain a valid data
104  while((TRNG_REGS->TRNG_INTFLAG & TRNG_INTFLAG_DATARDY_Msk) == 0)
105  {
106  }
107 
108  //Get the 32-bit random value
109  value = TRNG_REGS->TRNG_DATA;
110 #endif
111  }
112 
113  //Copy random byte
114  data[i] = value & 0xFF;
115  //Shift the 32-bit random value
116  value >>= 8;
117  }
118 
119  //Release exclusive access to the TRNG module
121 
122  //Successful processing
123  return NO_ERROR;
124 }
125 
126 #endif
OsMutex pic32czCryptoMutex
uint8_t data[]
Definition: ethernet.h:222
error_t trngGetRandomData(uint8_t *data, size_t length)
Get random data from the TRNG module.
error_t
Error codes.
Definition: error.h:43
General definitions for cryptographic algorithms.
error_t trngInit(void)
TRNG module initialization.
uint8_t length
Definition: tcp.h:375
PIC32CZ hardware cryptographic accelerator.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
uint8_t value[]
Definition: tcp.h:376
PIC32CZ true random number generator.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.