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 "mxc_device.h"
22 #include "mxc_errors.h"
23 #include "mxc_assert.h"
24 #include "mxc_sys.h"
25 #include "ctb_reva.h"
26 #include "ctb_common.h"
27 #include "trng_regs.h"
28
29 /* ************************************************************************* */
30 /* Global Control/Configuration functions */
31 /* ************************************************************************* */
32
33 /********************************************************/
34
MXC_CTB_Init(uint32_t features)35 int MXC_CTB_Init(uint32_t features)
36 {
37 // The crypto clock needs to be turned on for crypto to work.
38 if ((MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_ISO_EN) == 0) {
39 MXC_GCR->clkctrl |= MXC_F_GCR_CLKCTRL_ISO_EN;
40
41 // Check if CRYPTO clock is ready
42 if (MXC_SYS_Clock_Timeout(MXC_F_GCR_CLKCTRL_ISO_RDY) != E_NO_ERROR) {
43 return E_TIME_OUT;
44 }
45 }
46
47 if (features & ~MXC_CTB_FEATURE_TRNG) {
48 MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_CTB);
49 }
50
51 MXC_CTB_RevA_Init((mxc_ctb_reva_regs_t *)MXC_CTB, features);
52
53 if (features & MXC_CTB_FEATURE_TRNG) {
54 MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_TRNG);
55 }
56
57 return E_NO_ERROR;
58 }
59
MXC_CTB_CheckFeatures(void)60 uint32_t MXC_CTB_CheckFeatures(void)
61 {
62 return MXC_CTB_FEATURE_DMA | MXC_CTB_FEATURE_ECC | MXC_CTB_FEATURE_CIPHER |
63 MXC_CTB_FEATURE_HASH | MXC_CTB_FEATURE_CRC | MXC_CTB_FEATURE_TRNG;
64 }
65
MXC_CTB_EnableInt(void)66 void MXC_CTB_EnableInt(void)
67 {
68 MXC_CTB_RevA_EnableInt((mxc_ctb_reva_regs_t *)MXC_CTB);
69 }
70
MXC_CTB_DisableInt(void)71 void MXC_CTB_DisableInt(void)
72 {
73 MXC_CTB_RevA_DisableInt((mxc_ctb_reva_regs_t *)MXC_CTB);
74 }
75
MXC_CTB_Ready(void)76 int MXC_CTB_Ready(void)
77 {
78 return MXC_CTB_RevA_Ready((mxc_ctb_reva_regs_t *)MXC_CTB);
79 }
80
MXC_CTB_DoneClear(uint32_t features)81 void MXC_CTB_DoneClear(uint32_t features)
82 {
83 MXC_CTB_RevA_DoneClear((mxc_ctb_reva_regs_t *)MXC_CTB, features);
84 }
85
MXC_CTB_Done(void)86 uint32_t MXC_CTB_Done(void)
87 {
88 return MXC_CTB_RevA_Done((mxc_ctb_reva_regs_t *)MXC_CTB);
89 }
90
MXC_CTB_Reset(uint32_t features)91 void MXC_CTB_Reset(uint32_t features)
92 {
93 MXC_CTB_RevA_Reset(features);
94 }
95
MXC_CTB_Shutdown(uint32_t features)96 int MXC_CTB_Shutdown(uint32_t features)
97 {
98 uint32_t new_features;
99
100 int error = MXC_CTB_RevA_Shutdown(features);
101
102 if (error != E_NO_ERROR) {
103 return error;
104 }
105
106 new_features = MXC_CTB_GetEnabledFeatures();
107
108 // If CTB is not needed, disable clock
109 if ((new_features & ~MXC_CTB_FEATURE_TRNG) == 0) {
110 MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_CTB);
111 }
112
113 // If shutting down TRNG, disable clock
114 if (features & MXC_CTB_FEATURE_TRNG) {
115 MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TRNG);
116 }
117
118 return E_NO_ERROR;
119 }
120
MXC_CTB_GetEnabledFeatures(void)121 uint32_t MXC_CTB_GetEnabledFeatures(void)
122 {
123 return MXC_CTB_RevA_GetEnabledFeatures();
124 }
125
MXC_CTB_Handler(void)126 void MXC_CTB_Handler(void)
127 {
128 MXC_CTB_RevA_Handler((mxc_trng_reva_regs_t *)MXC_TRNG);
129 }
130
131 /************************************/
132 /* CTB DMA - Used for all features */
133 /************************************/
134
MXC_CTB_DMA_SetReadSource(mxc_ctb_dma_read_source_t source)135 void MXC_CTB_DMA_SetReadSource(mxc_ctb_dma_read_source_t source)
136 {
137 MXC_CTB_RevA_DMA_SetReadSource((mxc_ctb_reva_regs_t *)MXC_CTB, source);
138 }
139
MXC_CTB_DMA_GetReadSource(void)140 mxc_ctb_dma_read_source_t MXC_CTB_DMA_GetReadSource(void)
141 {
142 return MXC_CTB_RevA_DMA_GetReadSource((mxc_ctb_reva_regs_t *)MXC_CTB);
143 }
144
MXC_CTB_DMA_SetWriteSource(mxc_ctb_dma_write_source_t source)145 void MXC_CTB_DMA_SetWriteSource(mxc_ctb_dma_write_source_t source)
146 {
147 MXC_CTB_RevA_DMA_SetWriteSource((mxc_ctb_reva_regs_t *)MXC_CTB, source);
148 }
149
MXC_CTB_DMA_GetWriteSource(void)150 mxc_ctb_dma_write_source_t MXC_CTB_DMA_GetWriteSource(void)
151 {
152 return MXC_CTB_RevA_DMA_GetWriteSource((mxc_ctb_reva_regs_t *)MXC_CTB);
153 }
154
MXC_CTB_DMA_SetSource(uint8_t * source)155 void MXC_CTB_DMA_SetSource(uint8_t *source)
156 {
157 MXC_ASSERT(((uint32_t)source > 0x100FFFFF) || ((uint32_t)source < 0x08000000));
158 MXC_CTB_RevA_DMA_SetSource((mxc_ctb_reva_regs_t *)MXC_CTB, source);
159 }
160
MXC_CTB_DMA_SetDestination(uint8_t * dest)161 void MXC_CTB_DMA_SetDestination(uint8_t *dest)
162 {
163 MXC_CTB_RevA_DMA_SetDestination((mxc_ctb_reva_regs_t *)MXC_CTB, dest);
164 }
165
MXC_CTB_DMA_SetupOperation(mxc_ctb_dma_req_t * req)166 int MXC_CTB_DMA_SetupOperation(mxc_ctb_dma_req_t *req)
167 {
168 return MXC_CTB_RevA_DMA_SetupOperation((mxc_ctb_reva_dma_req_t *)req);
169 }
170
MXC_CTB_DMA_DoOperation(mxc_ctb_dma_req_t * req)171 int MXC_CTB_DMA_DoOperation(mxc_ctb_dma_req_t *req)
172 {
173 return MXC_CTB_RevA_DMA_DoOperation((mxc_ctb_reva_dma_req_t *)req);
174 }
175
MXC_CTB_DMA_StartTransfer(uint32_t length)176 void MXC_CTB_DMA_StartTransfer(uint32_t length)
177 {
178 MXC_CTB_RevA_DMA_StartTransfer((mxc_ctb_reva_regs_t *)MXC_CTB, length);
179 }
180
181 /* ************************************************************************* */
182 /* True Random Number Generator (TRNG) functions */
183 /* ************************************************************************* */
184
MXC_CTB_TRNG_RandomInt(void)185 int MXC_CTB_TRNG_RandomInt(void)
186 {
187 return MXC_CTB_RevA_TRNG_RandomInt((mxc_trng_reva_regs_t *)MXC_TRNG);
188 }
189
MXC_CTB_TRNG_Random(uint8_t * data,uint32_t len)190 int MXC_CTB_TRNG_Random(uint8_t *data, uint32_t len)
191 {
192 return MXC_CTB_RevA_TRNG_Random(data, len);
193 }
194
MXC_CTB_TRNG_RandomAsync(uint8_t * data,uint32_t len,mxc_ctb_complete_cb_t callback)195 void MXC_CTB_TRNG_RandomAsync(uint8_t *data, uint32_t len, mxc_ctb_complete_cb_t callback)
196 {
197 MXC_CTB_RevA_TRNG_RandomAsync((mxc_trng_reva_regs_t *)MXC_TRNG, data, len, callback);
198 }
199
200 /* ************************************************************************* */
201 /* Error Correction Code (ECC) functions */
202 /* ************************************************************************* */
203
204 /*******************************/
205 /* Low Level Functions */
206 /*******************************/
207
MXC_CTB_ECC_Enable(void)208 void MXC_CTB_ECC_Enable(void)
209 {
210 MXC_CTB_RevA_ECC_Enable((mxc_ctb_reva_regs_t *)MXC_CTB);
211 }
212
MXC_CTB_ECC_Disable(void)213 void MXC_CTB_ECC_Disable(void)
214 {
215 MXC_CTB_RevA_ECC_Disable((mxc_ctb_reva_regs_t *)MXC_CTB);
216 }
217
MXC_CTB_ECC_GetResult(void)218 uint32_t MXC_CTB_ECC_GetResult(void)
219 {
220 return MXC_CTB_RevA_ECC_GetResult((mxc_ctb_reva_regs_t *)MXC_CTB);
221 }
222
223 /*******************************/
224 /* High Level Functions */
225 /*******************************/
226
MXC_CTB_ECC_Compute(mxc_ctb_ecc_req_t * req)227 int MXC_CTB_ECC_Compute(mxc_ctb_ecc_req_t *req)
228 {
229 return MXC_CTB_RevA_ECC_Compute((mxc_ctb_reva_ecc_req_t *)req);
230 }
231
MXC_CTB_ECC_ErrorCheck(mxc_ctb_ecc_req_t * req)232 int MXC_CTB_ECC_ErrorCheck(mxc_ctb_ecc_req_t *req)
233 {
234 return MXC_CTB_RevA_ECC_ErrorCheck((mxc_ctb_reva_ecc_req_t *)req);
235 }
236
MXC_CTB_ECC_ComputeAsync(mxc_ctb_ecc_req_t * req)237 void MXC_CTB_ECC_ComputeAsync(mxc_ctb_ecc_req_t *req)
238 {
239 MXC_CTB_RevA_ECC_ComputeAsync((mxc_ctb_reva_ecc_req_t *)req);
240 }
241
MXC_CTB_ECC_ErrorCheckAsync(mxc_ctb_ecc_req_t * req)242 void MXC_CTB_ECC_ErrorCheckAsync(mxc_ctb_ecc_req_t *req)
243 {
244 MXC_CTB_RevA_ECC_ErrorCheckAsync((mxc_ctb_reva_ecc_req_t *)req);
245 }
246
247 /* ************************************************************************* */
248 /* Cyclic Redundancy Check (CRC) functions */
249 /* ************************************************************************* */
250
251 /*******************************/
252 /* Low Level Functions */
253 /*******************************/
254
MXC_CTB_CRC_SetDirection(mxc_ctb_crc_bitorder_t bitOrder)255 void MXC_CTB_CRC_SetDirection(mxc_ctb_crc_bitorder_t bitOrder)
256 {
257 MXC_CTB_RevA_CRC_SetDirection((mxc_ctb_reva_regs_t *)MXC_CTB, bitOrder);
258 }
259
MXC_CTB_CRC_GetDirection(void)260 mxc_ctb_crc_bitorder_t MXC_CTB_CRC_GetDirection(void)
261 {
262 return MXC_CTB_RevA_CRC_GetDirection((mxc_ctb_reva_regs_t *)MXC_CTB);
263 }
264
MXC_CTB_CRC_SetPoly(uint32_t poly)265 void MXC_CTB_CRC_SetPoly(uint32_t poly)
266 {
267 MXC_CTB_RevA_CRC_SetPoly((mxc_ctb_reva_regs_t *)MXC_CTB, poly);
268 }
269
MXC_CTB_CRC_GetPoly(void)270 uint32_t MXC_CTB_CRC_GetPoly(void)
271 {
272 return MXC_CTB_RevA_CRC_GetPoly((mxc_ctb_reva_regs_t *)MXC_CTB);
273 }
274
MXC_CTB_CRC_GetResult(void)275 uint32_t MXC_CTB_CRC_GetResult(void)
276 {
277 return MXC_CTB_RevA_CRC_GetResult((mxc_ctb_reva_regs_t *)MXC_CTB);
278 }
279
MXC_CTB_CRC_SetInitialValue(uint32_t seed)280 void MXC_CTB_CRC_SetInitialValue(uint32_t seed)
281 {
282 MXC_CTB_RevA_CRC_SetInitialValue(seed);
283 }
284
MXC_CTB_CRC_SetFinalXORValue(uint32_t xor)285 void MXC_CTB_CRC_SetFinalXORValue(uint32_t xor)
286 {
287 MXC_CTB_RevA_CRC_SetFinalXORValue(xor);
288 }
289
290 /*******************************/
291 /* High Level Functions */
292 /*******************************/
293
MXC_CTB_CRC_Compute(mxc_ctb_crc_req_t * req)294 int MXC_CTB_CRC_Compute(mxc_ctb_crc_req_t *req)
295 {
296 return MXC_CTB_RevA_CRC_Compute((mxc_ctb_reva_regs_t *)MXC_CTB, (mxc_ctb_reva_crc_req_t *)req);
297 }
298
MXC_CTB_CRC_ComputeAsync(mxc_ctb_crc_req_t * req)299 void MXC_CTB_CRC_ComputeAsync(mxc_ctb_crc_req_t *req)
300 {
301 MXC_CTB_RevA_CRC_ComputeAsync((mxc_ctb_reva_regs_t *)MXC_CTB, (mxc_ctb_reva_crc_req_t *)req);
302 }
303
304 /* ************************************************************************* */
305 /* Hash functions */
306 /* ************************************************************************* */
307
308 /***********************/
309 /* Low Level Functions */
310 /***********************/
311
MXC_CTB_Hash_GetBlockSize(mxc_ctb_hash_func_t function)312 unsigned int MXC_CTB_Hash_GetBlockSize(mxc_ctb_hash_func_t function)
313 {
314 return MXC_CTB_Common_Hash_GetBlockSize(function);
315 }
316
MXC_CTB_Hash_GetDigestSize(mxc_ctb_hash_func_t function)317 unsigned int MXC_CTB_Hash_GetDigestSize(mxc_ctb_hash_func_t function)
318 {
319 return MXC_CTB_Common_Hash_GetDigestSize(function);
320 }
321
MXC_CTB_Hash_SetFunction(mxc_ctb_hash_func_t function)322 void MXC_CTB_Hash_SetFunction(mxc_ctb_hash_func_t function)
323 {
324 MXC_CTB_RevA_Hash_SetFunction((mxc_ctb_reva_regs_t *)MXC_CTB, function);
325 }
326
MXC_CTB_Hash_GetFunction(void)327 mxc_ctb_hash_func_t MXC_CTB_Hash_GetFunction(void)
328 {
329 return MXC_CTB_RevA_Hash_GetFunction((mxc_ctb_reva_regs_t *)MXC_CTB);
330 }
331
MXC_CTB_Hash_SetAutoPad(int pad)332 void MXC_CTB_Hash_SetAutoPad(int pad)
333 {
334 MXC_CTB_RevA_Hash_SetAutoPad((mxc_ctb_reva_regs_t *)MXC_CTB, pad);
335 }
336
MXC_CTB_Hash_GetAutoPad(void)337 int MXC_CTB_Hash_GetAutoPad(void)
338 {
339 return MXC_CTB_RevA_Hash_GetAutoPad((mxc_ctb_reva_regs_t *)MXC_CTB);
340 }
341
MXC_CTB_Hash_GetResult(uint8_t * digest,int * len)342 void MXC_CTB_Hash_GetResult(uint8_t *digest, int *len)
343 {
344 MXC_CTB_RevA_Hash_GetResult((mxc_ctb_reva_regs_t *)MXC_CTB, digest, len);
345 }
346
MXC_CTB_Hash_SetMessageSize(uint32_t size)347 void MXC_CTB_Hash_SetMessageSize(uint32_t size)
348 {
349 MXC_CTB_RevA_Hash_SetMessageSize((mxc_ctb_reva_regs_t *)MXC_CTB, size);
350 }
351
MXC_CTB_Hash_SetSource(mxc_ctb_hash_source_t source)352 void MXC_CTB_Hash_SetSource(mxc_ctb_hash_source_t source)
353 {
354 MXC_CTB_RevA_Hash_SetSource((mxc_ctb_reva_regs_t *)MXC_CTB, source);
355 }
356
MXC_CTB_Hash_GetSource(void)357 mxc_ctb_hash_source_t MXC_CTB_Hash_GetSource(void)
358 {
359 return MXC_CTB_RevA_Hash_GetSource((mxc_ctb_reva_regs_t *)MXC_CTB);
360 }
361
MXC_CTB_Hash_InitializeHash(void)362 void MXC_CTB_Hash_InitializeHash(void)
363 {
364 MXC_CTB_RevA_Hash_InitializeHash((mxc_ctb_reva_regs_t *)MXC_CTB);
365 }
366
367 /************************/
368 /* High Level Functions */
369 /************************/
370
MXC_CTB_Hash_Compute(mxc_ctb_hash_req_t * req)371 int MXC_CTB_Hash_Compute(mxc_ctb_hash_req_t *req)
372 {
373 return MXC_CTB_RevA_Hash_Compute((mxc_ctb_reva_hash_req_t *)req);
374 }
375
MXC_CTB_Hash_ComputeAsync(mxc_ctb_hash_req_t * req)376 void MXC_CTB_Hash_ComputeAsync(mxc_ctb_hash_req_t *req)
377 {
378 MXC_CTB_RevA_Hash_ComputeAsync((mxc_ctb_reva_hash_req_t *)req);
379 }
380
381 /* ************************************************************************* */
382 /* Cipher functions */
383 /* ************************************************************************* */
384
385 /************************/
386 /* Low Level Functions */
387 /************************/
388
MXC_CTB_Cipher_GetKeySize(mxc_ctb_cipher_t cipher)389 unsigned int MXC_CTB_Cipher_GetKeySize(mxc_ctb_cipher_t cipher)
390 {
391 return MXC_CTB_Common_Cipher_GetKeySize(cipher);
392 }
393
MXC_CTB_Cipher_GetBlockSize(mxc_ctb_cipher_t cipher)394 unsigned int MXC_CTB_Cipher_GetBlockSize(mxc_ctb_cipher_t cipher)
395 {
396 return MXC_CTB_Common_Cipher_GetBlockSize(cipher);
397 }
398
MXC_CTB_Cipher_SetMode(mxc_ctb_cipher_mode_t mode)399 void MXC_CTB_Cipher_SetMode(mxc_ctb_cipher_mode_t mode)
400 {
401 MXC_CTB_RevA_Cipher_SetMode((mxc_ctb_reva_regs_t *)MXC_CTB, mode);
402 }
403
MXC_CTB_Cipher_GetMode(void)404 mxc_ctb_cipher_mode_t MXC_CTB_Cipher_GetMode(void)
405 {
406 return MXC_CTB_RevA_Cipher_GetMode((mxc_ctb_reva_regs_t *)MXC_CTB);
407 }
408
MXC_CTB_Cipher_SetCipher(mxc_ctb_cipher_t cipher)409 void MXC_CTB_Cipher_SetCipher(mxc_ctb_cipher_t cipher)
410 {
411 MXC_CTB_RevA_Cipher_SetCipher((mxc_ctb_reva_regs_t *)MXC_CTB, cipher);
412 }
413
MXC_CTB_Cipher_GetCipher(void)414 mxc_ctb_cipher_t MXC_CTB_Cipher_GetCipher(void)
415 {
416 return MXC_CTB_RevA_Cipher_GetCipher((mxc_ctb_reva_regs_t *)MXC_CTB);
417 }
418
MXC_CTB_Cipher_SetKeySource(mxc_ctb_cipher_key_t source)419 void MXC_CTB_Cipher_SetKeySource(mxc_ctb_cipher_key_t source)
420 {
421 MXC_CTB_RevA_Cipher_SetKeySource((mxc_ctb_reva_regs_t *)MXC_CTB, source);
422 }
423
MXC_CTB_Cipher_GetKeySource(void)424 mxc_ctb_cipher_key_t MXC_CTB_Cipher_GetKeySource(void)
425 {
426 return MXC_CTB_RevA_Cipher_GetKeySource((mxc_ctb_reva_regs_t *)MXC_CTB);
427 }
428
MXC_CTB_Cipher_LoadKey(void)429 void MXC_CTB_Cipher_LoadKey(void)
430 {
431 MXC_CTB_RevA_Cipher_LoadKey((mxc_ctb_reva_regs_t *)MXC_CTB);
432 }
433
MXC_CTB_Cipher_SetOperation(mxc_ctb_cipher_operation_t operation)434 void MXC_CTB_Cipher_SetOperation(mxc_ctb_cipher_operation_t operation)
435 {
436 MXC_CTB_RevA_Cipher_SetOperation((mxc_ctb_reva_regs_t *)MXC_CTB, operation);
437 }
438
MXC_CTB_Cipher_SetKey(uint8_t * key,uint32_t len)439 void MXC_CTB_Cipher_SetKey(uint8_t *key, uint32_t len)
440 {
441 MXC_CTB_RevA_Cipher_SetKey((mxc_ctb_reva_regs_t *)MXC_CTB, key, len);
442 }
443
MXC_CTB_Cipher_SetIV(uint8_t * iv,uint32_t len)444 void MXC_CTB_Cipher_SetIV(uint8_t *iv, uint32_t len)
445 {
446 MXC_CTB_RevA_Cipher_SetIV((mxc_ctb_reva_regs_t *)MXC_CTB, iv, len);
447 }
448
MXC_CTB_Cipher_GetIV(uint8_t * ivOut,uint32_t len)449 void MXC_CTB_Cipher_GetIV(uint8_t *ivOut, uint32_t len)
450 {
451 MXC_CTB_RevA_Cipher_GetIV((mxc_ctb_reva_regs_t *)MXC_CTB, ivOut, len);
452 }
453
454 /************************/
455 /* High Level Functions */
456 /************************/
457
MXC_CTB_Cipher_Encrypt(mxc_ctb_cipher_req_t * req)458 int MXC_CTB_Cipher_Encrypt(mxc_ctb_cipher_req_t *req)
459 {
460 return MXC_CTB_RevA_Cipher_Encrypt((mxc_ctb_reva_cipher_req_t *)req);
461 }
462
MXC_CTB_Cipher_Decrypt(mxc_ctb_cipher_req_t * req)463 int MXC_CTB_Cipher_Decrypt(mxc_ctb_cipher_req_t *req)
464 {
465 return MXC_CTB_RevA_Cipher_Decrypt((mxc_ctb_reva_cipher_req_t *)req);
466 }
467
MXC_CTB_Cipher_EncryptAsync(mxc_ctb_cipher_req_t * req)468 void MXC_CTB_Cipher_EncryptAsync(mxc_ctb_cipher_req_t *req)
469 {
470 MXC_CTB_RevA_Cipher_EncryptAsync((mxc_ctb_reva_cipher_req_t *)req);
471 }
472
MXC_CTB_Cipher_DecryptAsync(mxc_ctb_cipher_req_t * req)473 void MXC_CTB_Cipher_DecryptAsync(mxc_ctb_cipher_req_t *req)
474 {
475 MXC_CTB_RevA_Cipher_DecryptAsync((mxc_ctb_reva_cipher_req_t *)req);
476 }
477