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 /* **** Includes **** */
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "mxc_errors.h"
27 #include "mxc_sys.h"
28 #include "tpu.h"
29 #include "tpu_reva.h"
30 #include "trng.h"
31 
MXC_TPU_Init(mxc_sys_periph_clock_t clock)32 int MXC_TPU_Init(mxc_sys_periph_clock_t clock)
33 {
34     if (!(MXC_GCR->clk_ctrl & MXC_F_GCR_CLK_CTRL_CRYPTO_EN)) {
35         MXC_GCR->clk_ctrl |= MXC_F_GCR_CLK_CTRL_CRYPTO_EN;
36 
37         while (!(MXC_GCR->clk_ctrl & MXC_F_GCR_CLK_CTRL_CRYPTO_RDY)) {}
38     }
39 
40     if (clock == MXC_SYS_PERIPH_CLOCK_TPU) {
41         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_TPU);
42     } else if (clock == MXC_SYS_PERIPH_CLOCK_TRNG) {
43         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_TRNG);
44     }
45     return E_NO_ERROR;
46 }
47 
MXC_TPU_Shutdown(mxc_sys_periph_clock_t clock)48 int MXC_TPU_Shutdown(mxc_sys_periph_clock_t clock)
49 {
50     if (clock == MXC_SYS_PERIPH_CLOCK_TPU) {
51         MXC_SYS_Reset_Periph(MXC_SYS_RESET_TPU);
52         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TPU);
53     } else if (clock == MXC_SYS_PERIPH_CLOCK_TRNG) {
54         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TRNG);
55     }
56     return E_NO_ERROR;
57 }
58 
MXC_TPU_Reset(void)59 void MXC_TPU_Reset(void)
60 {
61     MXC_TPU_RevA_Reset((mxc_tpu_reva_regs_t *)MXC_TPU);
62 }
63 
64 /* ************************************************************************* */
65 /* Cyclic Redundancy Check (CRC) functions                                   */
66 /* ************************************************************************* */
67 
MXC_TPU_CRC_Config(void)68 int MXC_TPU_CRC_Config(void)
69 {
70     return MXC_TPU_RevA_CRC_Config((mxc_tpu_reva_regs_t *)MXC_TPU);
71 }
72 
MXC_TPU_CRC(const uint8_t * src,uint32_t len,uint32_t poly,uint32_t * crc)73 int MXC_TPU_CRC(const uint8_t *src, uint32_t len, uint32_t poly, uint32_t *crc)
74 {
75     return MXC_TPU_RevA_CRC((mxc_tpu_reva_regs_t *)MXC_TPU, src, len, poly, crc);
76 }
77 
MXC_TPU_Ham_Config(void)78 int MXC_TPU_Ham_Config(void)
79 {
80     return MXC_TPU_RevA_Ham_Config((mxc_tpu_reva_regs_t *)MXC_TPU);
81 }
82 
MXC_TPU_Ham(const uint8_t * src,uint32_t len,uint32_t * ecc)83 int MXC_TPU_Ham(const uint8_t *src, uint32_t len, uint32_t *ecc)
84 {
85     return MXC_TPU_RevA_Ham((mxc_tpu_reva_regs_t *)MXC_TPU, src, len, ecc);
86 }
87 
88 /* ************************************************************************* */
89 /* Cipher functions                                                          */
90 /* ************************************************************************* */
91 
MXC_TPU_Cipher_Get_Key_Size(mxc_tpu_ciphersel_t cipher)92 unsigned int MXC_TPU_Cipher_Get_Key_Size(mxc_tpu_ciphersel_t cipher)
93 {
94     // Key size indexed by 'opsel'
95     switch (cipher) {
96     case MXC_TPU_CIPHER_DIS:
97         return 0;
98     case MXC_TPU_CIPHER_AES128:
99         return 16;
100     case MXC_TPU_CIPHER_AES192:
101         return 24;
102     case MXC_TPU_CIPHER_AES256:
103         return 32;
104     case MXC_TPU_CIPHER_DES:
105         return 8;
106     case MXC_TPU_CIPHER_TDEA:
107         return 24;
108     }
109     // if returns this bad param was passed in or disable.
110     return 0;
111 }
112 
MXC_TPU_Cipher_Get_Block_Size(mxc_tpu_ciphersel_t cipher)113 unsigned int MXC_TPU_Cipher_Get_Block_Size(mxc_tpu_ciphersel_t cipher)
114 {
115     switch (cipher) {
116     case MXC_TPU_CIPHER_DIS:
117         return 0;
118     case MXC_TPU_CIPHER_AES128:
119         return AES_DATA_LEN;
120     case MXC_TPU_CIPHER_AES192:
121         return AES_DATA_LEN;
122     case MXC_TPU_CIPHER_AES256:
123         return AES_DATA_LEN;
124     case MXC_TPU_CIPHER_DES:
125         return DES_DATA_LEN;
126     case MXC_TPU_CIPHER_TDEA:
127         return DES_DATA_LEN;
128     }
129     // if returns this bad param was passed in or disable.
130     return 0;
131 }
132 
MXC_TPU_Cipher_GetLength(mxc_tpu_ciphersel_t cipher,unsigned int data_size)133 unsigned int MXC_TPU_Cipher_GetLength(mxc_tpu_ciphersel_t cipher, unsigned int data_size)
134 {
135     return MXC_TPU_RevA_Cipher_GetLength(cipher, data_size);
136 }
137 
MXC_TPU_Cipher_EncDecSelect(int enc)138 void MXC_TPU_Cipher_EncDecSelect(int enc)
139 {
140     MXC_TPU_RevA_Cipher_EncDecSelect((mxc_tpu_reva_regs_t *)MXC_TPU, enc);
141 }
142 
MXC_TPU_Cipher_Config(mxc_tpu_modesel_t mode,mxc_tpu_ciphersel_t cipher)143 int MXC_TPU_Cipher_Config(mxc_tpu_modesel_t mode, mxc_tpu_ciphersel_t cipher)
144 {
145     return MXC_TPU_RevA_Cipher_Config((mxc_tpu_reva_regs_t *)MXC_TPU, (mxc_tpu_reva_modesel_t)mode,
146                                       (mxc_tpu_reva_ciphersel_t)cipher);
147 }
148 
MXC_TPU_Cipher_KeySelect(mxc_tpu_keysrc_t key_src)149 int MXC_TPU_Cipher_KeySelect(mxc_tpu_keysrc_t key_src)
150 {
151     return MXC_TPU_RevA_Cipher_KeySelect((mxc_tpu_reva_regs_t *)MXC_TPU,
152                                          (mxc_tpu_reva_keysrc_t)key_src);
153 }
154 
MXC_TPU_Cipher_DoOperation(const char * src,const char * iv,const char * key,mxc_tpu_ciphersel_t cipher,mxc_tpu_modesel_t mode,unsigned int data_size,char * outptr)155 int MXC_TPU_Cipher_DoOperation(const char *src, const char *iv, const char *key,
156                                mxc_tpu_ciphersel_t cipher, mxc_tpu_modesel_t mode,
157                                unsigned int data_size, char *outptr)
158 {
159     return MXC_TPU_RevA_Cipher_DoOperation((mxc_tpu_reva_regs_t *)MXC_TPU, src, iv, key, cipher,
160                                            mode, data_size, outptr);
161 }
162 
MXC_TPU_Cipher_DES_Encrypt(const char * plaintext,const char * iv,const char * key,mxc_tpu_modesel_t mode,unsigned int data_size,char * outptr)163 int MXC_TPU_Cipher_DES_Encrypt(const char *plaintext, const char *iv, const char *key,
164                                mxc_tpu_modesel_t mode, unsigned int data_size, char *outptr)
165 {
166     return MXC_TPU_RevA_Cipher_DES_Encrypt(plaintext, iv, key, mode, data_size, outptr);
167 }
168 
MXC_TPU_Cipher_DES_Decrypt(const char * ciphertext,const char * iv,const char * key,mxc_tpu_modesel_t mode,unsigned int data_size,char * outptr)169 int MXC_TPU_Cipher_DES_Decrypt(const char *ciphertext, const char *iv, const char *key,
170                                mxc_tpu_modesel_t mode, unsigned int data_size, char *outptr)
171 {
172     return MXC_TPU_RevA_Cipher_DES_Decrypt(ciphertext, iv, key, mode, data_size, outptr);
173 }
174 
MXC_TPU_Cipher_TDES_Encrypt(const char * plaintext,const char * iv,const char * key,mxc_tpu_modesel_t mode,unsigned int data_size,char * outptr)175 int MXC_TPU_Cipher_TDES_Encrypt(const char *plaintext, const char *iv, const char *key,
176                                 mxc_tpu_modesel_t mode, unsigned int data_size, char *outptr)
177 {
178     return MXC_TPU_RevA_Cipher_TDES_Encrypt(plaintext, iv, key, mode, data_size, outptr);
179 }
180 
MXC_TPU_Cipher_TDES_Decrypt(const char * ciphertext,const char * iv,const char * key,mxc_tpu_modesel_t mode,unsigned int data_size,char * outptr)181 int MXC_TPU_Cipher_TDES_Decrypt(const char *ciphertext, const char *iv, const char *key,
182                                 mxc_tpu_modesel_t mode, unsigned int data_size, char *outptr)
183 {
184     return MXC_TPU_RevA_Cipher_TDES_Decrypt(ciphertext, iv, key, mode, data_size, outptr);
185 }
186 
MXC_TPU_Cipher_AES_Encrypt(const char * plaintext,const char * iv,const char * key,mxc_tpu_ciphersel_t cipher,mxc_tpu_modesel_t mode,unsigned int data_size,char * outptr)187 int MXC_TPU_Cipher_AES_Encrypt(const char *plaintext, const char *iv, const char *key,
188                                mxc_tpu_ciphersel_t cipher, mxc_tpu_modesel_t mode,
189                                unsigned int data_size, char *outptr)
190 {
191     return MXC_TPU_RevA_Cipher_AES_Encrypt(plaintext, iv, key, cipher, mode, data_size, outptr);
192 }
193 
MXC_TPU_Cipher_AES_Decrypt(const char * ciphertext,const char * iv,const char * key,mxc_tpu_ciphersel_t cipher,mxc_tpu_modesel_t mode,unsigned int data_size,char * outptr)194 int MXC_TPU_Cipher_AES_Decrypt(const char *ciphertext, const char *iv, const char *key,
195                                mxc_tpu_ciphersel_t cipher, mxc_tpu_modesel_t mode,
196                                unsigned int data_size, char *outptr)
197 {
198     return MXC_TPU_RevA_Cipher_AES_Decrypt(ciphertext, iv, key, cipher, mode, data_size, outptr);
199 }
200 
201 /* ************************************************************************* */
202 /* Hash functions                                                            */
203 /* ************************************************************************* */
204 
MXC_TPU_Hash_Get_Block_Size_SHA(mxc_tpu_hashfunsel_t func)205 unsigned int MXC_TPU_Hash_Get_Block_Size_SHA(mxc_tpu_hashfunsel_t func)
206 {
207     // Block size in bytes indexed by hash function
208     switch (func) {
209     case MXC_TPU_HASH_DIS:
210         return 0;
211     case MXC_TPU_HASH_SHA1:
212         return 64;
213     case MXC_TPU_HASH_SHA224:
214         return 64;
215     case MXC_TPU_HASH_SHA256:
216         return 64;
217     case MXC_TPU_HASH_SHA384:
218         return 128;
219     case MXC_TPU_HASH_SHA512:
220         return 128;
221     }
222     // if returns this bad param was passed in or disable.
223     return 0;
224 }
225 
MXC_TPU_Hash_Get_Dgst_Size(mxc_tpu_hashfunsel_t func)226 unsigned int MXC_TPU_Hash_Get_Dgst_Size(mxc_tpu_hashfunsel_t func)
227 {
228     // Digest length in bytes indexed by hash function
229     switch (func) {
230     case MXC_TPU_HASH_DIS:
231         return 0;
232     case MXC_TPU_HASH_SHA1:
233         return 20;
234     case MXC_TPU_HASH_SHA224:
235         return 28;
236     case MXC_TPU_HASH_SHA256:
237         return 32;
238     case MXC_TPU_HASH_SHA384:
239         return 48;
240     case MXC_TPU_HASH_SHA512:
241         return 64;
242     }
243     // if returns this bad param was passed in or disable.
244     return 0;
245 }
246 
MXC_TPU_Hash_SHA_Size(unsigned int * blocks,unsigned int * length,unsigned int * lbyte,mxc_tpu_hashfunsel_t fun)247 void MXC_TPU_Hash_SHA_Size(unsigned int *blocks, unsigned int *length, unsigned int *lbyte,
248                            mxc_tpu_hashfunsel_t fun)
249 {
250     MXC_TPU_RevA_Hash_SHA_Size(blocks, length, lbyte, fun);
251 }
252 
MXC_TPU_Hash_Config(mxc_tpu_hashfunsel_t func)253 int MXC_TPU_Hash_Config(mxc_tpu_hashfunsel_t func)
254 {
255     return MXC_TPU_RevA_Hash_Config((mxc_tpu_reva_regs_t *)MXC_TPU, func);
256 }
257 
MXC_TPU_Hash_SHA(const char * msg,mxc_tpu_hashfunsel_t fun,unsigned int byteLen,char * digest)258 int MXC_TPU_Hash_SHA(const char *msg, mxc_tpu_hashfunsel_t fun, unsigned int byteLen, char *digest)
259 {
260     return MXC_TPU_RevA_Hash_SHA((mxc_tpu_reva_regs_t *)MXC_TPU, msg, fun, byteLen, digest);
261 }
262 
263 /* ************************************************************************* */
264 /* True Random Number Generator (TRNG) functions                             */
265 /* ************************************************************************* */
266 
MXC_TPU_TRNG_Read8BIT(mxc_trng_regs_t * trng)267 uint8_t MXC_TPU_TRNG_Read8BIT(mxc_trng_regs_t *trng)
268 {
269     uint8_t rand_val;
270     MXC_TRNG_Random(&rand_val, 1);
271     return rand_val;
272 }
273 
MXC_TPU_TRNG_Read16BIT(mxc_trng_regs_t * trng)274 uint16_t MXC_TPU_TRNG_Read16BIT(mxc_trng_regs_t *trng)
275 {
276     uint16_t rand_val;
277     MXC_TRNG_Random((uint8_t *)&rand_val, 2);
278     return rand_val;
279 }
280 
MXC_TPU_TRNG_Read32BIT(mxc_trng_regs_t * trng)281 uint32_t MXC_TPU_TRNG_Read32BIT(mxc_trng_regs_t *trng)
282 {
283     return MXC_TRNG_RandomInt();
284 }
285 
MXC_TPU_TRNG_Read(mxc_trng_regs_t * trng,uint8_t * data,int len)286 void MXC_TPU_TRNG_Read(mxc_trng_regs_t *trng, uint8_t *data, int len)
287 {
288     MXC_TRNG_Random(data, len);
289 }
290 
MXC_TPU_TRNG_Generate_AES(mxc_trng_regs_t * trng)291 void MXC_TPU_TRNG_Generate_AES(mxc_trng_regs_t *trng)
292 {
293     MXC_TRNG_GenerateKey();
294 }
295 
296 /* ************************************************************************* */
297 /* Modular Arithmetic Accelerator (MAA) functions                             */
298 /* ************************************************************************* */
299 
MXC_TPU_MAA_Mem_Clear(void)300 void MXC_TPU_MAA_Mem_Clear(void)
301 {
302     MXC_TPU_RevA_MAA_Mem_Clear();
303 }
304 
MXC_TPU_MAA_Reset(void)305 void MXC_TPU_MAA_Reset(void)
306 {
307     MXC_TPU_RevA_MAA_Reset((mxc_tpu_reva_regs_t *)MXC_TPU);
308 }
309 
MXC_TPU_MAA_Init(unsigned int size)310 int MXC_TPU_MAA_Init(unsigned int size)
311 {
312     return MXC_TPU_RevA_MAA_Init((mxc_tpu_reva_regs_t *)MXC_TPU, size);
313 }
314 
MXC_TPU_MAA_Shutdown(void)315 int MXC_TPU_MAA_Shutdown(void)
316 {
317     return MXC_TPU_Shutdown(MXC_SYS_PERIPH_CLOCK_TPU);
318 }
319 
MXC_TPU_MAA_Compute(mxc_tpu_maa_clcsel_t clc,char * multiplier,char * multiplicand,char * exp,char * mod,int * result,unsigned int len)320 int MXC_TPU_MAA_Compute(mxc_tpu_maa_clcsel_t clc, char *multiplier, char *multiplicand, char *exp,
321                         char *mod, int *result, unsigned int len)
322 {
323     return MXC_TPU_RevA_MAA_Compute((mxc_tpu_reva_regs_t *)MXC_TPU, clc, multiplier, multiplicand,
324                                     exp, mod, result, len);
325 }
326