1 /*
2  * The register macro of SUNXI SecuritySystem controller.
3  *
4  * Copyright (C) 2014 Allwinner.
5  *
6  * Mintow <duanmintao@allwinnertech.com>
7  *
8  * This file is licensed under the terms of the GNU General Public
9  * License version 2.  This program is licensed "as is" without any
10  * warranty of any kind, whether express or implied.
11  */
12 
13 #ifndef _SUNXI_CRYPTO_ENGINE_REG_H_
14 #define _SUNXI_CRYPTO_ENGINE_REG_H_
15 
16 #include "platform_ce.h"
17 
18 #ifndef BIT
19 #define BIT(nr)     (1UL << (nr))
20 #endif
21 
22 #define AES_MIN_KEY_SIZE       16
23 
24 /* CE: Crypto Engine*/
25 #define CE_REG_TSK          0x00
26 #ifdef CE_SUPPORT_CE_V3_1
27 #define CE_REG_CTL          0x04
28 #endif
29 #define CE_REG_ICR          0x08
30 #define CE_REG_ISR          0x0C
31 #define CE_REG_TLR          0x10
32 #define CE_REG_TSR          0x14
33 #define CE_REG_ERR          0x18
34 #ifdef CE_SUPPORT_CE_V3_1
35 #define CE_REG_CSS          0x1C
36 #define CE_REG_CDS          0x20
37 #endif
38 #define CE_REG_CSA          0x24
39 #define CE_REG_CDA          0x28
40 #ifdef CE_SUPPORT_CE_V3_1
41 #define CE_REG_TPR          0x2C
42 #endif
43 #ifdef CE_SUPPORT_CE_V3_2
44 #define CE_REG_HCSA         0x34
45 #define CE_REG_HCDA         0x38
46 #define CE_REG_ACSA         0x44
47 #define CE_REG_ACDA         0x48
48 #define CE_REG_XCSA         0x54
49 #define CE_REG_XCDA         0x58
50 #endif
51 #define CE_REG_VER          0x90
52 
53 
54 
55 
56 #define CE_CHAN_INT_ENABLE      1
57 
58 #define CE_CHAN_PENDING         1
59 
60 #ifndef CE_SUPPORT_CE_V3_1
61 #define CE_REG_TLR_METHOD_TYPE_SHIFT        8
62 #endif
63 
64 #ifdef CE_SUPPORT_CE_V3_1
65 #define CE_REG_TSR_BUSY         1
66 #define CE_REG_TSR_IDLE         0
67 #define CE_REG_TSR_BUSY_SHIFT   0
68 #define CE_REG_TSR_BUSY_MASK    (0x1 << CE_REG_TSR_BUSY_SHIFT)
69 #endif
70 
71 #define CE_REG_ESR_ERR_UNSUPPORT    0
72 #define CE_REG_ESR_ERR_LEN          1
73 #define CE_REG_ESR_ERR_KEYSRAM      2
74 #ifdef CE_SUPPORT_CE_V3_1
75 #define CE_REG_ESR_ERR_ADDR         3
76 #else
77 #define CE_REG_ESR_ERR_ADDR         5
78 #endif
79 
80 #ifdef CE_SUPPORT_CE_V3_2
81 #define CE_REG_ESR_ERR_KEYLADDER        6
82 #endif
83 
84 #ifdef CE_SUPPORT_CE_V3_1
85 #define CE_REG_ESR_CHAN_SHIFT       4
86 #define CE_REG_ESR_CHAN_MASK(flow)  (0xF << (CE_REG_ESR_CHAN_SHIFT*flow))
87 #else
88 #define CE_REG_ESR_CHAN_SHIFT       8
89 #define CE_REG_ESR_CHAN_MASK(flow)  (0xFF << (CE_REG_ESR_CHAN_SHIFT*flow))
90 #endif
91 
92 #ifdef CE_SUPPORT_CE_V3_1
93 #define CE_REG_CSS_OFFSET_SHIFT     16
94 #define CE_REG_CDS_OFFSET_SHIFT     16
95 #endif
96 
97 /* About the common control word */
98 #ifdef CE_SUPPORT_CE_V3_1
99 #define CE_TASK_INT_ENABLE          1
100 #endif
101 #define CE_COMM_CTL_TASK_INT_SHIFT  31
102 #define CE_COMM_CTL_TASK_INT_MASK   (0x1 << CE_COMM_CTL_TASK_INT_SHIFT)
103 
104 #define CE_CBC_MAC_LEN_SHIFT        17
105 
106 #define CE_HASH_IV_DEFAULT          0
107 #define CE_HASH_IV_INPUT            1
108 #define CE_COMM_CTL_IV_MODE_SHIFT   16
109 
110 #define CE_HMAC_SHA1_LAST           BIT(15)
111 
112 #define CE_DIR_ENCRYPT              0
113 #define CE_DIR_DECRYPT              1
114 #define CE_COMM_CTL_OP_DIR_SHIFT    8
115 
116 #define CE_METHOD_AES               0
117 #define CE_METHOD_DES               1
118 #define CE_METHOD_3DES              2
119 #define CE_METHOD_MD5               16
120 #define CE_METHOD_SHA1              17
121 #define CE_METHOD_SHA224            18
122 #define CE_METHOD_SHA256            19
123 #define CE_METHOD_SHA384            20
124 #define CE_METHOD_SHA512            21
125 #define CE_METHOD_HMAC_SHA1         22
126 #define CE_METHOD_HMAC_SHA256       23
127 #define CE_METHOD_RSA               32
128 #define CE_METHOD_DH                CE_METHOD_RSA
129 #ifdef CE_SUPPORT_CE_V3_1
130 #define CE_METHOD_TRNG              48
131 #define CE_METHOD_PRNG              49
132 #define CE_METHOD_ECC               64
133 #else
134 #define CE_METHOD_TRNG              28
135 #define CE_METHOD_PRNG              29
136 #define CE_METHOD_ECC               33
137 #define CE_METHOD_RAES              48
138 #endif
139 #define SS_RNG_MAX_LEN              (0x2000)
140 #define CE_COMM_CTL_METHOD_SHIFT        0
141 #ifdef CE_SUPPORT_CE_V3_1
142 #define CE_COMM_CTL_METHOD_MASK     0x3F
143 #else
144 #define CE_COMM_CTL_METHOD_MASK     0x7F
145 #endif
146 
147 #define CE_METHOD_IS_HASH(type) ((type == CE_METHOD_MD5) \
148                 || (type == CE_METHOD_SHA1) \
149                 || (type == CE_METHOD_SHA224) \
150                 || (type == CE_METHOD_SHA256) \
151                 || (type == CE_METHOD_SHA384) \
152                 || (type == CE_METHOD_SHA512))
153 
154 #define CE_METHOD_IS_AES(type) ((type == CE_METHOD_AES) \
155                 || (type == CE_METHOD_DES) \
156                 || (type == CE_METHOD_3DES))
157 
158 #define CE_METHOD_IS_HMAC(type) ((type == CE_METHOD_HMAC_SHA1) \
159             || (type == CE_METHOD_HMAC_SHA256))
160 
161 /* About the symmetric control word */
162 
163 #define CE_KEY_SELECT_INPUT         0
164 #define CE_KEY_SELECT_SSK           1
165 #define CE_KEY_SELECT_HUK           2
166 #define CE_KEY_SELECT_RSSK          3
167 #ifdef CE_SUPPORT_CE_V3_2
168 #define CE_KEY_SELECT_SCK0          4
169 #define CE_KEY_SELECT_SCK1          5
170 #endif
171 #define CE_KEY_SELECT_INTERNAL_0    8
172 #define CE_KEY_SELECT_INTERNAL_1    9
173 #define CE_KEY_SELECT_INTERNAL_2    10
174 #define CE_KEY_SELECT_INTERNAL_3    11
175 #define CE_KEY_SELECT_INTERNAL_4    12
176 #define CE_KEY_SELECT_INTERNAL_5    13
177 #define CE_KEY_SELECT_INTERNAL_6    14
178 #define CE_KEY_SELECT_INTERNAL_7    15
179 #define CE_SYM_CTL_KEY_SELECT_SHIFT 20
180 
181 /* The identification string to indicate the key source. */
182 #define CE_KS_SSK           "KEY_SEL_SSK"
183 #define CE_KS_HUK           "KEY_SEL_HUK"
184 #define CE_KS_RSSK          "KEY_SEL_RSSK"
185 #ifdef CE_SUPPORT_CE_V3_2
186 #define CE_KS_SCK0          "KEY_SEL_SCK0"
187 #define CE_KS_SCK1          "KEY_SEL_SCK1"
188 #endif
189 #define CE_KS_INTERNAL_0    "KEY_SEL_INTRA_0"
190 #define CE_KS_INTERNAL_1    "KEY_SEL_INTRA_1"
191 #define CE_KS_INTERNAL_2    "KEY_SEL_INTRA_2"
192 #define CE_KS_INTERNAL_3    "KEY_SEL_INTRA_3"
193 #define CE_KS_INTERNAL_4    "KEY_SEL_INTRA_4"
194 #define CE_KS_INTERNAL_5    "KEY_SEL_INTRA_5"
195 #define CE_KS_INTERNAL_6    "KEY_SEL_INTRA_6"
196 #define CE_KS_INTERNAL_7    "KEY_SEL_INTRA_7"
197 
198 #define CE_CFB_WIDTH_1              0
199 #define CE_CFB_WIDTH_8              1
200 #define CE_CFB_WIDTH_64             2
201 #define CE_CFB_WIDTH_128            3
202 #define CE_SYM_CTL_CFB_WIDTH_SHIFT  18
203 
204 #define CE_SYM_CTL_AES_CTS_LAST     BIT(16)
205 
206 #ifndef CE_SUPPORT_CE_V3_1
207 #define CE_SYM_CTL_AES_XTS_LAST     BIT(13)
208 #define CE_SYM_CTL_AES_XTS_FIRST    BIT(12)
209 #endif
210 
211 #define CE_AES_MODE_ECB             0
212 #define CE_AES_MODE_CBC             1
213 #define CE_AES_MODE_CTR             2
214 #define CE_AES_MODE_CTS             3
215 #define CE_AES_MODE_OFB             4
216 #define CE_AES_MODE_CFB             5
217 #define CE_AES_MODE_CBC_MAC         6
218 #define CE_AES_MODE_XTS             9
219 #define CE_SYM_CTL_OP_MODE_SHIFT    8
220 
221 #define CE_CTR_SIZE_16              0
222 #define CE_CTR_SIZE_32              1
223 #define CE_CTR_SIZE_64              2
224 #define CE_CTR_SIZE_128             3
225 #define CE_SYM_CTL_CTR_SIZE_SHIFT   2
226 
227 #define CE_AES_KEY_SIZE_128         0
228 #define CE_AES_KEY_SIZE_192         1
229 #define CE_AES_KEY_SIZE_256         2
230 #define CE_SYM_CTL_KEY_SIZE_SHIFT   0
231 
232 #define CE_IS_AES_MODE(type, mode, M) (CE_METHOD_IS_AES(type) \
233                     && (mode == CE_AES_MODE_##M))
234 
235 /* About the asymmetric control word */
236 
237 #ifdef CE_SUPPORT_CE_V3_1
238 #define CE_RSA_PUB_MODULUS_WIDTH_512    0
239 #define CE_RSA_PUB_MODULUS_WIDTH_1024   1
240 #define CE_RSA_PUB_MODULUS_WIDTH_2048   2
241 #define CE_RSA_PUB_MODULUS_WIDTH_3072   3
242 #define CE_RSA_PUB_MODULUS_WIDTH_4096   4
243 #define CE_ASYM_CTL_RSA_PM_WIDTH_SHIFT  28
244 #endif
245 
246 #define CE_RSA_OP_M_EXP     0 /* modular exponentiation */
247 #ifdef CE_SUPPORT_CE_V3_1
248 #define CE_RSA_OP_M_MUL     2 /* modular multiplication */
249 #else
250 #define CE_RSA_OP_M_ADD                 1 /* modular add */
251 #define CE_RSA_OP_M_MINUS               2 /* modular minus */
252 #define CE_RSA_OP_M_MUL                 3 /* modular multiplication */
253 #endif
254 #define CE_ASYM_CTL_RSA_OP_SHIFT        16
255 
256 #ifdef CE_SUPPORT_CE_V3_1
257 #define CE_ECC_PARA_WIDTH_160           0
258 #define CE_ECC_PARA_WIDTH_224           2
259 #define CE_ECC_PARA_WIDTH_256           3
260 #define CE_ECC_PARA_WIDTH_521           5
261 #define CE_ASYM_CTL_ECC_PARA_WIDTH_SHIFT    12
262 #endif
263 
264 #ifdef CE_SUPPORT_CE_V3_1
265 #define CE_ECC_OP_POINT_MUL             0
266 #define CE_ECC_OP_POINT_ADD             1
267 #define CE_ECC_OP_POINT_DBL             2
268 #define CE_ECC_OP_POINT_VER             3
269 #define CE_ECC_OP_ENC                   4
270 #define CE_ECC_OP_DEC                   5
271 #define CE_ECC_OP_SIGN                  6
272 #define CE_ASYM_CTL_ECC_OP_SHIFT        4
273 #else
274 #define CE_ECC_OP_POINT_ADD             0 /* point add */
275 #define CE_ECC_OP_POINT_DBL             1 /* point double */
276 #define CE_ECC_OP_POINT_MUL             2 /* point multiplication */
277 #define CE_ECC_OP_POINT_VER             3 /* point verification */
278 #define CE_ECC_OP_ENC                   4 /* encryption */
279 #define CE_ECC_OP_DEC                   5 /* decryption */
280 #define CE_ECC_OP_SIGN                  6 /* sign */
281 #define CE_ECC_OP_VERIFY                7 /* verification */
282 #endif
283 
284 #define CE_SEED_SIZE            24
285 
286 /* The descriptor of a CE task. */
287 #define CE_SCATTERS_PER_TASK        8
288 
289 #endif /* end of _SUNXI_CRYPTO_ENGINE_REG_H_ */
290