]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/ccree/cc_crypto_ctx.h
staging: ccree: add parentheses to macro argument
[karo-tx-linux.git] / drivers / staging / ccree / cc_crypto_ctx.h
1 /*
2  * Copyright (C) 2012-2017 ARM Limited or its affiliates.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #ifndef _CC_CRYPTO_CTX_H_
18 #define _CC_CRYPTO_CTX_H_
19
20 #include <linux/types.h>
21
22 /* context size */
23 #ifndef CC_CTX_SIZE_LOG2
24 #if (CC_SUPPORT_SHA > 256)
25 #define CC_CTX_SIZE_LOG2 8
26 #else
27 #define CC_CTX_SIZE_LOG2 7
28 #endif
29 #endif
30 #define CC_CTX_SIZE (1 << CC_CTX_SIZE_LOG2)
31 #define CC_DRV_CTX_SIZE_WORDS (CC_CTX_SIZE >> 2)
32
33 #define CC_DRV_DES_IV_SIZE 8
34 #define CC_DRV_DES_BLOCK_SIZE 8
35
36 #define CC_DRV_DES_ONE_KEY_SIZE 8
37 #define CC_DRV_DES_DOUBLE_KEY_SIZE 16
38 #define CC_DRV_DES_TRIPLE_KEY_SIZE 24
39 #define CC_DRV_DES_KEY_SIZE_MAX CC_DRV_DES_TRIPLE_KEY_SIZE
40
41 #define CC_AES_IV_SIZE 16
42 #define CC_AES_IV_SIZE_WORDS (CC_AES_IV_SIZE >> 2)
43
44 #define CC_AES_BLOCK_SIZE 16
45 #define CC_AES_BLOCK_SIZE_WORDS 4
46
47 #define CC_AES_128_BIT_KEY_SIZE 16
48 #define CC_AES_128_BIT_KEY_SIZE_WORDS   (CC_AES_128_BIT_KEY_SIZE >> 2)
49 #define CC_AES_192_BIT_KEY_SIZE 24
50 #define CC_AES_192_BIT_KEY_SIZE_WORDS   (CC_AES_192_BIT_KEY_SIZE >> 2)
51 #define CC_AES_256_BIT_KEY_SIZE 32
52 #define CC_AES_256_BIT_KEY_SIZE_WORDS   (CC_AES_256_BIT_KEY_SIZE >> 2)
53 #define CC_AES_KEY_SIZE_MAX                     CC_AES_256_BIT_KEY_SIZE
54 #define CC_AES_KEY_SIZE_WORDS_MAX               (CC_AES_KEY_SIZE_MAX >> 2)
55
56 #define CC_MD5_DIGEST_SIZE      16
57 #define CC_SHA1_DIGEST_SIZE     20
58 #define CC_SHA224_DIGEST_SIZE   28
59 #define CC_SHA256_DIGEST_SIZE   32
60 #define CC_SHA256_DIGEST_SIZE_IN_WORDS 8
61 #define CC_SHA384_DIGEST_SIZE   48
62 #define CC_SHA512_DIGEST_SIZE   64
63
64 #define CC_SHA1_BLOCK_SIZE 64
65 #define CC_SHA1_BLOCK_SIZE_IN_WORDS 16
66 #define CC_MD5_BLOCK_SIZE 64
67 #define CC_MD5_BLOCK_SIZE_IN_WORDS 16
68 #define CC_SHA224_BLOCK_SIZE 64
69 #define CC_SHA256_BLOCK_SIZE 64
70 #define CC_SHA256_BLOCK_SIZE_IN_WORDS 16
71 #define CC_SHA1_224_256_BLOCK_SIZE 64
72 #define CC_SHA384_BLOCK_SIZE 128
73 #define CC_SHA512_BLOCK_SIZE 128
74
75 #if (CC_SUPPORT_SHA > 256)
76 #define CC_DIGEST_SIZE_MAX CC_SHA512_DIGEST_SIZE
77 #define CC_HASH_BLOCK_SIZE_MAX CC_SHA512_BLOCK_SIZE /*1024b*/
78 #else /* Only up to SHA256 */
79 #define CC_DIGEST_SIZE_MAX CC_SHA256_DIGEST_SIZE
80 #define CC_HASH_BLOCK_SIZE_MAX CC_SHA256_BLOCK_SIZE /*512b*/
81 #endif
82
83 #define CC_HMAC_BLOCK_SIZE_MAX CC_HASH_BLOCK_SIZE_MAX
84
85 #define CC_MULTI2_SYSTEM_KEY_SIZE               32
86 #define CC_MULTI2_DATA_KEY_SIZE         8
87 #define CC_MULTI2_SYSTEM_N_DATA_KEY_SIZE \
88                 (CC_MULTI2_SYSTEM_KEY_SIZE + CC_MULTI2_DATA_KEY_SIZE)
89 #define CC_MULTI2_BLOCK_SIZE                                    8
90 #define CC_MULTI2_IV_SIZE                                       8
91 #define CC_MULTI2_MIN_NUM_ROUNDS                                8
92 #define CC_MULTI2_MAX_NUM_ROUNDS                                128
93
94 #define CC_DRV_ALG_MAX_BLOCK_SIZE CC_HASH_BLOCK_SIZE_MAX
95
96 enum drv_engine_type {
97         DRV_ENGINE_NULL = 0,
98         DRV_ENGINE_AES = 1,
99         DRV_ENGINE_DES = 2,
100         DRV_ENGINE_HASH = 3,
101         DRV_ENGINE_RC4 = 4,
102         DRV_ENGINE_DOUT = 5,
103         DRV_ENGINE_RESERVE32B = S32_MAX,
104 };
105
106 enum drv_crypto_alg {
107         DRV_CRYPTO_ALG_NULL = -1,
108         DRV_CRYPTO_ALG_AES  = 0,
109         DRV_CRYPTO_ALG_DES  = 1,
110         DRV_CRYPTO_ALG_HASH = 2,
111         DRV_CRYPTO_ALG_C2   = 3,
112         DRV_CRYPTO_ALG_HMAC = 4,
113         DRV_CRYPTO_ALG_AEAD = 5,
114         DRV_CRYPTO_ALG_BYPASS = 6,
115         DRV_CRYPTO_ALG_NUM = 7,
116         DRV_CRYPTO_ALG_RESERVE32B = S32_MAX
117 };
118
119 enum drv_crypto_direction {
120         DRV_CRYPTO_DIRECTION_NULL = -1,
121         DRV_CRYPTO_DIRECTION_ENCRYPT = 0,
122         DRV_CRYPTO_DIRECTION_DECRYPT = 1,
123         DRV_CRYPTO_DIRECTION_DECRYPT_ENCRYPT = 3,
124         DRV_CRYPTO_DIRECTION_RESERVE32B = S32_MAX
125 };
126
127 enum drv_cipher_mode {
128         DRV_CIPHER_NULL_MODE = -1,
129         DRV_CIPHER_ECB = 0,
130         DRV_CIPHER_CBC = 1,
131         DRV_CIPHER_CTR = 2,
132         DRV_CIPHER_CBC_MAC = 3,
133         DRV_CIPHER_XTS = 4,
134         DRV_CIPHER_XCBC_MAC = 5,
135         DRV_CIPHER_OFB = 6,
136         DRV_CIPHER_CMAC = 7,
137         DRV_CIPHER_CCM = 8,
138         DRV_CIPHER_CBC_CTS = 11,
139         DRV_CIPHER_GCTR = 12,
140         DRV_CIPHER_ESSIV = 13,
141         DRV_CIPHER_BITLOCKER = 14,
142         DRV_CIPHER_RESERVE32B = S32_MAX
143 };
144
145 enum drv_hash_mode {
146         DRV_HASH_NULL = -1,
147         DRV_HASH_SHA1 = 0,
148         DRV_HASH_SHA256 = 1,
149         DRV_HASH_SHA224 = 2,
150         DRV_HASH_SHA512 = 3,
151         DRV_HASH_SHA384 = 4,
152         DRV_HASH_MD5 = 5,
153         DRV_HASH_CBC_MAC = 6,
154         DRV_HASH_XCBC_MAC = 7,
155         DRV_HASH_CMAC = 8,
156         DRV_HASH_MODE_NUM = 9,
157         DRV_HASH_RESERVE32B = S32_MAX
158 };
159
160 enum drv_hash_hw_mode {
161         DRV_HASH_HW_MD5 = 0,
162         DRV_HASH_HW_SHA1 = 1,
163         DRV_HASH_HW_SHA256 = 2,
164         DRV_HASH_HW_SHA224 = 10,
165         DRV_HASH_HW_SHA512 = 4,
166         DRV_HASH_HW_SHA384 = 12,
167         DRV_HASH_HW_GHASH = 6,
168         DRV_HASH_HW_RESERVE32B = S32_MAX
169 };
170
171 enum drv_multi2_mode {
172         DRV_MULTI2_NULL = -1,
173         DRV_MULTI2_ECB = 0,
174         DRV_MULTI2_CBC = 1,
175         DRV_MULTI2_OFB = 2,
176         DRV_MULTI2_RESERVE32B = S32_MAX
177 };
178
179 /* drv_crypto_key_type[1:0] is mapped to cipher_do[1:0] */
180 /* drv_crypto_key_type[2] is mapped to cipher_config2 */
181 enum drv_crypto_key_type {
182         DRV_NULL_KEY = -1,
183         DRV_USER_KEY = 0,               /* 0x000 */
184         DRV_ROOT_KEY = 1,               /* 0x001 */
185         DRV_PROVISIONING_KEY = 2,       /* 0x010 */
186         DRV_SESSION_KEY = 3,            /* 0x011 */
187         DRV_APPLET_KEY = 4,             /* NA */
188         DRV_PLATFORM_KEY = 5,           /* 0x101 */
189         DRV_CUSTOMER_KEY = 6,           /* 0x110 */
190         DRV_END_OF_KEYS = S32_MAX,
191 };
192
193 enum drv_crypto_padding_type {
194         DRV_PADDING_NONE = 0,
195         DRV_PADDING_PKCS7 = 1,
196         DRV_PADDING_RESERVE32B = S32_MAX
197 };
198
199 /*******************************************************************/
200 /***************** DESCRIPTOR BASED CONTEXTS ***********************/
201 /*******************************************************************/
202
203  /* Generic context ("super-class") */
204 struct drv_ctx_generic {
205         enum drv_crypto_alg alg;
206 } __attribute__((__may_alias__));
207
208 struct drv_ctx_hash {
209         enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_HASH */
210         enum drv_hash_mode mode;
211         u8 digest[CC_DIGEST_SIZE_MAX];
212         /* reserve to end of allocated context size */
213         u8 reserved[CC_CTX_SIZE - 2 * sizeof(u32) -
214                         CC_DIGEST_SIZE_MAX];
215 };
216
217 /* NOTE! drv_ctx_hmac should have the same structure as drv_ctx_hash except
218  * k0, k0_size fields
219  */
220 struct drv_ctx_hmac {
221         enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_HMAC */
222         enum drv_hash_mode mode;
223         u8 digest[CC_DIGEST_SIZE_MAX];
224         u32 k0[CC_HMAC_BLOCK_SIZE_MAX / sizeof(u32)];
225         u32 k0_size;
226         /* reserve to end of allocated context size */
227         u8 reserved[CC_CTX_SIZE - 3 * sizeof(u32) -
228                         CC_DIGEST_SIZE_MAX - CC_HMAC_BLOCK_SIZE_MAX];
229 };
230
231 struct drv_ctx_cipher {
232         enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_AES */
233         enum drv_cipher_mode mode;
234         enum drv_crypto_direction direction;
235         enum drv_crypto_key_type crypto_key_type;
236         enum drv_crypto_padding_type padding_type;
237         u32 key_size; /* numeric value in bytes   */
238         u32 data_unit_size; /* required for XTS */
239         /* block_state is the AES engine block state.
240          * It is used by the host to pass IV or counter at initialization.
241          * It is used by SeP for intermediate block chaining state and for
242          * returning MAC algorithms results.
243          */
244         u8 block_state[CC_AES_BLOCK_SIZE];
245         u8 key[CC_AES_KEY_SIZE_MAX];
246         u8 xex_key[CC_AES_KEY_SIZE_MAX];
247         /* reserve to end of allocated context size */
248         u32 reserved[CC_DRV_CTX_SIZE_WORDS - 7 -
249                 CC_AES_BLOCK_SIZE / sizeof(u32) - 2 *
250                 (CC_AES_KEY_SIZE_MAX / sizeof(u32))];
251 };
252
253 /* authentication and encryption with associated data class */
254 struct drv_ctx_aead {
255         enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_AES */
256         enum drv_cipher_mode mode;
257         enum drv_crypto_direction direction;
258         u32 key_size; /* numeric value in bytes   */
259         u32 nonce_size; /* nonce size (octets) */
260         u32 header_size; /* finit additional data size (octets) */
261         u32 text_size; /* finit text data size (octets) */
262         u32 tag_size; /* mac size, element of {4, 6, 8, 10, 12, 14, 16} */
263         /* block_state1/2 is the AES engine block state */
264         u8 block_state[CC_AES_BLOCK_SIZE];
265         u8 mac_state[CC_AES_BLOCK_SIZE]; /* MAC result */
266         u8 nonce[CC_AES_BLOCK_SIZE]; /* nonce buffer */
267         u8 key[CC_AES_KEY_SIZE_MAX];
268         /* reserve to end of allocated context size */
269         u32 reserved[CC_DRV_CTX_SIZE_WORDS - 8 -
270                 3 * (CC_AES_BLOCK_SIZE / sizeof(u32)) -
271                 CC_AES_KEY_SIZE_MAX / sizeof(u32)];
272 };
273
274 /*******************************************************************/
275 /***************** MESSAGE BASED CONTEXTS **************************/
276 /*******************************************************************/
277
278 /* Get the address of a @member within a given @ctx address
279  * @ctx: The context address
280  * @type: Type of context structure
281  * @member: Associated context field
282  */
283 #define GET_CTX_FIELD_ADDR(ctx, type, member) ((ctx) + offsetof(type, member))
284
285 #endif /* _CC_CRYPTO_CTX_H_ */
286