sc594_crypto_trng.c
Go to the documentation of this file.
1 /**
2  * @file sc594_crypto_trng.c
3  * @brief ADSP-SC594 true random number generator
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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.6.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include <sys/platform.h>
36 #include <sys/adi_core.h>
37 #include <drivers/crypto/adi_trng.h>
38 #include "core/crypto.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (SC594_CRYPTO_TRNG_SUPPORT == ENABLED)
45 
46 //Global variables
47 static uint8_t trngMemory[ADI_TRNG_MEMORY_SIZE];
48 static ADI_TRNG_HANDLE trngHandle;
49 
50 
51 /**
52  * @brief TRNG module initialization
53  * @return Error code
54  **/
55 
57 {
58  ADI_TRNG_RESULT res;
59 
60  //Initialize TRNG peripheral
61  res = adi_TRNG_Open(&trngHandle, trngMemory, sizeof(trngMemory));
62 
63  //Return status code
64  return (res == ADI_TRNG_SUCCESS) ? NO_ERROR : ERROR_FAILURE;
65 }
66 
67 
68 /**
69  * @brief Get random data from the TRNG module
70  * @param[out] data Buffer where to store random data
71  * @param[in] length Number of random bytes to generate
72  **/
73 
75 {
76  size_t i;
77  uint32_t value;
78  ADI_TRNG_RESULT res;
79 
80  //Initialize status code
81  res = ADI_TRNG_SUCCESS;
82 
83  //Acquire exclusive access to the TRNG module
85 
86  //Generate random data
87  for(i = 0; i < length; i++)
88  {
89  //Generate a new 32-bit random value when necessary
90  if((i % 4) == 0)
91  {
92  //Get 32-bit random value
93  res = adi_TRNG_Read_Output(trngHandle, &value, ADI_TRNG_32_BITS);
94  //Check status code
95  if(res != ADI_TRNG_SUCCESS)
96  {
97  break;
98  }
99  }
100 
101  //Copy random byte
102  data[i] = value & 0xFF;
103  //Shift the 32-bit random value
104  value >>= 8;
105  }
106 
107  //Release exclusive access to the TRNG module
109 
110  //Return status code
111  return (res == ADI_TRNG_SUCCESS) ? NO_ERROR : ERROR_FAILURE;
112 }
113 
114 #endif
OsMutex sc594CryptoMutex
Definition: sc594_crypto.c:41
uint8_t data[]
Definition: ethernet.h:224
const uint8_t res[]
error_t
Error codes.
Definition: error.h:43
ADSP-SC594 true random number generator.
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
ADSP-SC594 hardware cryptographic accelerator.
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:375
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
error_t trngGetRandomData(uint8_t *data, size_t length)
Get random data from the TRNG module.
uint8_t value[]
Definition: tcp.h:376
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
error_t trngInit(void)
TRNG module initialization.