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 #ifndef LIBRARIES_PERIPHDRIVERS_SOURCE_TPU_TPU_REVA_H_
22 #define LIBRARIES_PERIPHDRIVERS_SOURCE_TPU_TPU_REVA_H_
23 
24 #include "tpu.h"
25 #include "tpu_reva_regs.h"
26 #include "trng_revc_regs.h"
27 
28 /***************************************************************************************************************
29                   DATA STRUCTURES FOR CRYPTO INITIALIZATION
30 ***************************************************************************************************************/
31 /**
32   * Enumeration type for the Crypto Cipher Operation(128/192/256-bit key)
33   */
34 typedef enum {
35     MXC_TPU_REVA_CIPHER_DIS = MXC_V_TPU_REVA_CIPHER_CTRL_CIPHER_DIS, // Disable
36     MXC_TPU_REVA_CIPHER_AES128 = MXC_V_TPU_REVA_CIPHER_CTRL_CIPHER_AES128, // Select AES-128
37     MXC_TPU_REVA_CIPHER_AES192 = MXC_V_TPU_REVA_CIPHER_CTRL_CIPHER_AES192, // Select AES-192
38     MXC_TPU_REVA_CIPHER_AES256 = MXC_V_TPU_REVA_CIPHER_CTRL_CIPHER_AES256, // Select AES-256
39     MXC_TPU_REVA_CIPHER_DES = MXC_V_TPU_REVA_CIPHER_CTRL_CIPHER_DES, // Select DES
40     MXC_TPU_REVA_CIPHER_TDES = MXC_V_TPU_REVA_CIPHER_CTRL_CIPHER_TDES // Select TDEA
41 } mxc_tpu_reva_ciphersel_t;
42 
43 /**
44   * Enumeration type to select which key is used in Cipher operations
45   */
46 typedef enum {
47     MXC_TPU_REVA_KEYSRC_KEY0 =
48         MXC_S_TPU_REVA_CIPHER_CTRL_SRC_CIPHERKEY, // Use key in CIPHER_KEY[0:7]
49     MXC_TPU_REVA_KEYSRC_KEY2 =
50         MXC_S_TPU_REVA_CIPHER_CTRL_SRC_REGFILE, // Use key 0 in AES_KEY registers
51     MXC_TPU_REVA_KEYSRC_KEY3 =
52         MXC_S_TPU_REVA_CIPHER_CTRL_SRC_QSPIKEY_REGFILE // Use key 1 in AES_KEY registers
53 } mxc_tpu_reva_keysrc_t;
54 
55 /**
56   * Enumeration type for the Crypto Mode Select
57   */
58 typedef enum {
59     MXC_TPU_REVA_MODE_ECB = MXC_V_TPU_REVA_CIPHER_CTRL_MODE_ECB, // Select ECB
60     MXC_TPU_REVA_MODE_CBC = MXC_V_TPU_REVA_CIPHER_CTRL_MODE_CBC, // Select CBC
61     MXC_TPU_REVA_MODE_CFB = MXC_V_TPU_REVA_CIPHER_CTRL_MODE_CFB, // Select CFB
62     MXC_TPU_REVA_MODE_CTR = MXC_V_TPU_REVA_CIPHER_CTRL_MODE_CTR // Select CTR
63 } mxc_tpu_reva_modesel_t;
64 
65 /**
66   * Enumeration type for Hash function Select
67   */
68 typedef enum {
69     MXC_TPU_REVA_HASH_DIS = MXC_V_TPU_REVA_HASH_CTRL_HASH_DIS, // Disable
70     MXC_TPU_REVA_HASH_SHA1 = MXC_V_TPU_REVA_HASH_CTRL_HASH_SHA1, // Select SHA1
71     MXC_TPU_REVA_HASH_SHA224 = MXC_V_TPU_REVA_HASH_CTRL_HASH_SHA224, // Select SHA224
72     MXC_TPU_REVA_HASH_SHA256 = MXC_V_TPU_REVA_HASH_CTRL_HASH_SHA256, // Select SHA256
73     MXC_TPU_REVA_HASH_SHA384 = MXC_V_TPU_REVA_HASH_CTRL_HASH_SHA384, // Select SHA384
74     MXC_TPU_REVA_HASH_SHA512 = MXC_V_TPU_REVA_HASH_CTRL_HASH_SHA512 // Select SHA384
75 } mxc_tpu_reva_hashfunsel_t;
76 
77 /**
78   * Enumeration type for MAA initialization
79   */
80 typedef enum {
81     MXC_TPU_REVA_MAA_EXP = MXC_V_TPU_REVA_MAA_CTRL_CLC_EXP, // Select exponentiation operation
82     MXC_TPU_REVA_MAA_SQ = MXC_V_TPU_REVA_MAA_CTRL_CLC_SQ, // Select square operation
83     MXC_TPU_REVA_MAA_MUL = MXC_V_TPU_REVA_MAA_CTRL_CLC_MUL, // Select multiplication operation
84     MXC_TPU_REVA_MAA_SQMUL =
85         MXC_V_TPU_REVA_MAA_CTRL_CLC_SQMUL, // Select square followed by multiplication operation
86     MXC_TPU_REVA_MAA_ADD = MXC_V_TPU_REVA_MAA_CTRL_CLC_ADD, // Select add operation
87     MXC_TPU_REVA_MAA_SUB = MXC_V_TPU_REVA_MAA_CTRL_CLC_SUB // Select subtract operation
88 } mxc_tpu_reva_maa_clcsel_t;
89 
90 /* ************************************************************************* */
91 /* Global Control/Configuration functions                                    */
92 /* ************************************************************************* */
93 
94 void MXC_TPU_RevA_Clear_Done_Flags(mxc_tpu_reva_regs_t *tpu);
95 void MXC_TPU_RevA_Reset(mxc_tpu_reva_regs_t *tpu);
96 
97 /* ************************************************************************* */
98 /* Cyclic Redundancy Check (CRC) functions                                   */
99 /* ************************************************************************* */
100 
101 int MXC_TPU_RevA_CRC_Config(mxc_tpu_reva_regs_t *tpu);
102 int MXC_TPU_RevA_CRC(mxc_tpu_reva_regs_t *tpu, const uint8_t *src, uint32_t len, uint32_t poly,
103                      uint32_t *crc);
104 int MXC_TPU_RevA_Ham_Config(mxc_tpu_reva_regs_t *tpu);
105 int MXC_TPU_RevA_Ham(mxc_tpu_reva_regs_t *tpu, const uint8_t *src, uint32_t len, uint32_t *ecc);
106 
107 /* ************************************************************************* */
108 /* Cipher functions                                                          */
109 /* ************************************************************************* */
110 
111 unsigned int MXC_TPU_RevA_Cipher_GetLength(mxc_tpu_ciphersel_t cipher, unsigned int data_size);
112 void MXC_TPU_RevA_Cipher_EncDecSelect(mxc_tpu_reva_regs_t *tpu, int enc);
113 int MXC_TPU_RevA_Cipher_Config(mxc_tpu_reva_regs_t *tpu, mxc_tpu_reva_modesel_t mode,
114                                mxc_tpu_reva_ciphersel_t cipher);
115 int MXC_TPU_RevA_Cipher_KeySelect(mxc_tpu_reva_regs_t *tpu, mxc_tpu_reva_keysrc_t key_src);
116 int MXC_TPU_RevA_Cipher_DoOperation(mxc_tpu_reva_regs_t *tpu, const char *src, const char *iv,
117                                     const char *key, mxc_tpu_ciphersel_t cipher,
118                                     mxc_tpu_modesel_t mode, unsigned int data_size, char *outptr);
119 int MXC_TPU_RevA_Cipher_DES_Encrypt(const char *plaintext, const char *iv, const char *key,
120                                     mxc_tpu_modesel_t mode, unsigned int data_size, char *outptr);
121 int MXC_TPU_RevA_Cipher_DES_Decrypt(const char *ciphertext, const char *iv, const char *key,
122                                     mxc_tpu_modesel_t mode, unsigned int data_size, char *outptr);
123 int MXC_TPU_RevA_Cipher_TDES_Encrypt(const char *plaintext, const char *iv, const char *key,
124                                      mxc_tpu_modesel_t mode, unsigned int data_size, char *outptr);
125 int MXC_TPU_RevA_Cipher_TDES_Decrypt(const char *ciphertext, const char *iv, const char *key,
126                                      mxc_tpu_modesel_t mode, unsigned int data_size, char *outptr);
127 int MXC_TPU_RevA_Cipher_AES_Encrypt(const char *plaintext, const char *iv, const char *key,
128                                     mxc_tpu_ciphersel_t cipher, mxc_tpu_modesel_t mode,
129                                     unsigned int data_size, char *outptr);
130 int MXC_TPU_RevA_Cipher_AES_Decrypt(const char *ciphertext, const char *iv, const char *key,
131                                     mxc_tpu_ciphersel_t cipher, mxc_tpu_modesel_t mode,
132                                     unsigned int data_size, char *outptr);
133 
134 /* ************************************************************************* */
135 /* Hash functions                                                            */
136 /* ************************************************************************* */
137 
138 void MXC_TPU_RevA_Hash_SHA_Size(unsigned int *blocks, unsigned int *length, unsigned int *lbyte,
139                                 mxc_tpu_hashfunsel_t fun);
140 int MXC_TPU_RevA_Hash_Config(mxc_tpu_reva_regs_t *tpu, mxc_tpu_hashfunsel_t func);
141 int MXC_TPU_RevA_Hash_SHA(mxc_tpu_reva_regs_t *tpu, const char *msg, mxc_tpu_hashfunsel_t fun,
142                           unsigned int byteLen, char *digest);
143 
144 /* ************************************************************************* */
145 /* True Random Number Generator (TRNG) functions                             */
146 /* ************************************************************************* */
147 
148 uint8_t MXC_TPU_RevA_TRNG_Read8BIT(mxc_trng_revc_regs_t *trng);
149 uint16_t MXC_TPU_RevA_TRNG_Read16BIT(mxc_trng_revc_regs_t *trng);
150 uint32_t MXC_TPU_RevA_TRNG_Read32BIT(mxc_trng_revc_regs_t *trng);
151 void MXC_TPU_RevA_TRNG_Read(mxc_trng_revc_regs_t *trng, uint8_t *data, int len);
152 void MXC_TPU_RevA_TRNG_Generate_AES(mxc_trng_revc_regs_t *trng);
153 
154 /* ************************************************************************* */
155 /* Modular Arithmetic Accelerator (MAA) functions                             */
156 /* ************************************************************************* */
157 
158 void MXC_TPU_RevA_MAA_Mem_Clear(void);
159 void MXC_TPU_RevA_MAA_Reset(mxc_tpu_reva_regs_t *tpu);
160 int MXC_TPU_RevA_MAA_Init(mxc_tpu_reva_regs_t *tpu, unsigned int size);
161 int MXC_TPU_RevA_MAA_Compute(mxc_tpu_reva_regs_t *tpu, mxc_tpu_maa_clcsel_t clc, char *multiplier,
162                              char *multiplicand, char *exp, char *mod, int *result,
163                              unsigned int len);
164 
165 #endif // LIBRARIES_PERIPHDRIVERS_SOURCE_TPU_TPU_REVA_H_
166