pic32mz_crypto_trng.c
Go to the documentation of this file.
1 /**
2  * @file pic32mz_crypto_trng.c
3  * @brief PIC32MZ true random number generator
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.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include <p32xxxx.h>
36 #include "core/crypto.h"
39 #include "debug.h"
40 
41 //Check crypto library configuration
42 #if (PIC32MZ_CRYPTO_TRNG_SUPPORT == ENABLED)
43 
44 
45 /**
46  * @brief TRNG module initialization
47  * @return Error code
48  **/
49 
51 {
52  //Enable TRNG
53  RNGCON |= _RNGCON_TRNGMODE_MASK | _RNGCON_TRNGEN_MASK;
54 
55  //Successful initialization
56  return NO_ERROR;
57 }
58 
59 
60 /**
61  * @brief Get random data from the TRNG module
62  * @param[out] data Buffer where to store random data
63  * @param[in] length Number of random bytes to generate
64  **/
65 
67 {
68  size_t i;
69  uint32_t value1;
70  uint32_t value2;
71 
72  //Acquire exclusive access to the RNG module
74 
75  //Generate random data
76  for(i = 0; i < length; i++)
77  {
78  //Generate a new 64-bit random value when necessary
79  if((i % 8) == 0)
80  {
81  //Wait for the RNG to contain a valid data
82  while(RNGCNT < 64)
83  {
84  }
85 
86  //Get the 64-bit random value
87  value2 = RNGSEED2;
88  value1 = RNGSEED1;
89  }
90 
91  //Extract a random byte
92  if((i % 8) < 4)
93  {
94  //Copy random byte
95  data[i] = value1 & 0xFF;
96  //Shift the 32-bit random value
97  value1 >>= 8;
98  }
99  else
100  {
101  //Copy random byte
102  data[i] = value2 & 0xFF;
103  //Shift the 32-bit random value
104  value2 >>= 8;
105  }
106  }
107 
108  //Release exclusive access to the RNG module
110 
111  //Successful processing
112  return NO_ERROR;
113 }
114 
115 #endif
uint8_t data[]
Definition: ethernet.h:222
OsMutex pic32mzCryptoMutex
PIC32MZ hardware cryptographic accelerator.
error_t trngInit(void)
TRNG module initialization.
error_t
Error codes.
Definition: error.h:43
error_t trngGetRandomData(uint8_t *data, size_t length)
Get random data from the TRNG module.
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:368
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
PIC32MZ true random number generator.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.