1 /******************************************************************************
2 *
3 * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4 * Analog Devices, Inc.),
5 * Copyright (C) 2023-2024 Analog Devices, Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************************/
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "mxc_sys.h"
25 #include "mxc_device.h"
26 #include "mxc_errors.h"
27 #include "mxc_assert.h"
28 #include "mxc_lock.h"
29
30 #include "trng_regs.h"
31 #include "trng.h"
32 #include "trng_revb.h"
33
34 /***** Global Variables *****/
35
36 static mxc_trng_complete_t MXC_TRNG_Callback;
37
38 static uint32_t TRNG_count, TRNG_maxLength;
39 static uint8_t *TRNG_data;
40
41 /* ************************************************************************* */
42 /* Global Control/Configuration functions */
43 /* ************************************************************************* */
44
MXC_TRNG_RevB_Init(void)45 int MXC_TRNG_RevB_Init(void)
46 {
47 return E_NO_ERROR;
48 }
49
MXC_TRNG_RevB_EnableInt(mxc_trng_revb_regs_t * trng)50 void MXC_TRNG_RevB_EnableInt(mxc_trng_revb_regs_t *trng)
51 {
52 trng->ctrl |= MXC_F_TRNG_REVB_CTRL_RND_IE;
53 }
54
MXC_TRNG_RevB_DisableInt(mxc_trng_revb_regs_t * trng)55 void MXC_TRNG_RevB_DisableInt(mxc_trng_revb_regs_t *trng)
56 {
57 trng->ctrl &= ~MXC_F_TRNG_REVB_CTRL_RND_IE;
58 }
59
MXC_TRNG_RevB_Shutdown(void)60 int MXC_TRNG_RevB_Shutdown(void)
61 {
62 return E_NO_ERROR;
63 }
64
MXC_TRNG_RevB_Handler(mxc_trng_revb_regs_t * trng)65 void MXC_TRNG_RevB_Handler(mxc_trng_revb_regs_t *trng)
66 {
67 uint32_t temp;
68 mxc_trng_complete_t cb;
69
70 // if this is last block, disable interrupt before reading trng->data
71 if (TRNG_maxLength <= TRNG_count + 4) {
72 trng->ctrl &= ~MXC_F_TRNG_REVB_CTRL_RND_IE;
73 }
74
75 temp = trng->data;
76
77 if ((TRNG_count + 3) < TRNG_maxLength) {
78 memcpy(&(TRNG_data[TRNG_count]), (uint8_t *)(&temp), 4);
79 TRNG_count += 4;
80 } else {
81 memcpy(&(TRNG_data[TRNG_count]), (uint8_t *)(&temp), TRNG_maxLength & 0x03);
82 TRNG_count += (TRNG_maxLength & 0x03);
83 }
84
85 if (TRNG_maxLength == TRNG_count) {
86 cb = MXC_TRNG_Callback;
87 cb(0, 0);
88 }
89 }
90
91 /* ************************************************************************* */
92 /* True Random Number Generator(TRNG) functions */
93 /* ************************************************************************* */
94
MXC_TRNG_RevB_RandomInt(mxc_trng_revb_regs_t * trng)95 int MXC_TRNG_RevB_RandomInt(mxc_trng_revb_regs_t *trng)
96 {
97 while (!(trng->status & MXC_F_TRNG_REVB_STATUS_RDY)) {}
98
99 return (int)trng->data;
100 }
101
MXC_TRNG_RevB_Random(uint8_t * data,uint32_t len)102 int MXC_TRNG_RevB_Random(uint8_t *data, uint32_t len)
103 {
104 unsigned int i, temp;
105
106 if (data == NULL) {
107 return E_NULL_PTR;
108 }
109
110 for (i = 0; (i + 3) < len; i += 4) {
111 temp = MXC_TRNG_RandomInt();
112 memcpy(&(data[i]), (uint8_t *)(&temp), 4);
113 }
114
115 if (len & 0x03) {
116 temp = MXC_TRNG_RandomInt();
117 memcpy(&(data[i]), (uint8_t *)(&temp), len & 0x03);
118 }
119
120 return E_NO_ERROR;
121 }
122
MXC_TRNG_RevB_RandomAsync(mxc_trng_revb_regs_t * trng,uint8_t * data,uint32_t len,mxc_trng_complete_t callback)123 void MXC_TRNG_RevB_RandomAsync(mxc_trng_revb_regs_t *trng, uint8_t *data, uint32_t len,
124 mxc_trng_complete_t callback)
125 {
126 MXC_ASSERT(data && callback);
127
128 if (len == 0) {
129 return;
130 }
131
132 TRNG_data = data;
133 TRNG_count = 0;
134 TRNG_maxLength = len;
135 MXC_TRNG_Callback = callback;
136
137 // Enable interrupts
138 trng->ctrl |= MXC_F_TRNG_REVB_CTRL_RND_IE;
139 }
140
MXC_TRNG_RevB_GenerateKey(mxc_trng_revb_regs_t * trng)141 void MXC_TRNG_RevB_GenerateKey(mxc_trng_revb_regs_t *trng)
142 {
143 // Generate AES Key
144 trng->ctrl |= MXC_F_TRNG_REVB_CTRL_AESKG_USR;
145
146 // Wait for key transfer to complete
147 while (trng->status & MXC_F_TRNG_REVB_STATUS_AESKGD) {}
148 }
149
MXC_TRNG_RevB_HealthTest(mxc_trng_revb_regs_t * trng)150 int MXC_TRNG_RevB_HealthTest(mxc_trng_revb_regs_t *trng)
151 {
152 /* Clear on-going test if necessary */
153 if (trng->ctrl & MXC_F_TRNG_REVB_CTRL_ODHT) {
154 trng->ctrl &= ~MXC_F_TRNG_REVB_CTRL_ODHT;
155 while (trng->status & MXC_F_TRNG_REVB_STATUS_ODHT) {}
156 }
157
158 /* Start on-demand health test */
159 trng->ctrl |= MXC_F_TRNG_REVB_CTRL_ODHT;
160
161 /* Wait for test to finish */
162 while (trng->status & MXC_F_TRNG_REVB_STATUS_ODHT) {}
163
164 /* Check results of test */
165 if (trng->status & MXC_F_TRNG_REVB_STATUS_HT) {
166 return E_BAD_STATE;
167 }
168 return E_NO_ERROR;
169 }
170