]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/ccree/cc_crypto_ctx.h
staging: ccree: remove spurious blank lines
[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        (CC_MULTI2_SYSTEM_KEY_SIZE + CC_MULTI2_DATA_KEY_SIZE)
88 #define CC_MULTI2_BLOCK_SIZE                                    8
89 #define CC_MULTI2_IV_SIZE                                       8
90 #define CC_MULTI2_MIN_NUM_ROUNDS                                8
91 #define CC_MULTI2_MAX_NUM_ROUNDS                                128
92
93 #define CC_DRV_ALG_MAX_BLOCK_SIZE CC_HASH_BLOCK_SIZE_MAX
94
95 enum drv_engine_type {
96         DRV_ENGINE_NULL = 0,
97         DRV_ENGINE_AES = 1,
98         DRV_ENGINE_DES = 2,
99         DRV_ENGINE_HASH = 3,
100         DRV_ENGINE_RC4 = 4,
101         DRV_ENGINE_DOUT = 5,
102         DRV_ENGINE_RESERVE32B = S32_MAX,
103 };
104
105 enum drv_crypto_alg {
106         DRV_CRYPTO_ALG_NULL = -1,
107         DRV_CRYPTO_ALG_AES  = 0,
108         DRV_CRYPTO_ALG_DES  = 1,
109         DRV_CRYPTO_ALG_HASH = 2,
110         DRV_CRYPTO_ALG_C2   = 3,
111         DRV_CRYPTO_ALG_HMAC = 4,
112         DRV_CRYPTO_ALG_AEAD = 5,
113         DRV_CRYPTO_ALG_BYPASS = 6,
114         DRV_CRYPTO_ALG_NUM = 7,
115         DRV_CRYPTO_ALG_RESERVE32B = S32_MAX
116 };
117
118 enum drv_crypto_direction {
119         DRV_CRYPTO_DIRECTION_NULL = -1,
120         DRV_CRYPTO_DIRECTION_ENCRYPT = 0,
121         DRV_CRYPTO_DIRECTION_DECRYPT = 1,
122         DRV_CRYPTO_DIRECTION_DECRYPT_ENCRYPT = 3,
123         DRV_CRYPTO_DIRECTION_RESERVE32B = S32_MAX
124 };
125
126 enum drv_cipher_mode {
127         DRV_CIPHER_NULL_MODE = -1,
128         DRV_CIPHER_ECB = 0,
129         DRV_CIPHER_CBC = 1,
130         DRV_CIPHER_CTR = 2,
131         DRV_CIPHER_CBC_MAC = 3,
132         DRV_CIPHER_XTS = 4,
133         DRV_CIPHER_XCBC_MAC = 5,
134         DRV_CIPHER_OFB = 6,
135         DRV_CIPHER_CMAC = 7,
136         DRV_CIPHER_CCM = 8,
137         DRV_CIPHER_CBC_CTS = 11,
138         DRV_CIPHER_GCTR = 12,
139         DRV_CIPHER_ESSIV = 13,
140         DRV_CIPHER_BITLOCKER = 14,
141         DRV_CIPHER_RESERVE32B = S32_MAX
142 };
143
144 enum drv_hash_mode {
145         DRV_HASH_NULL = -1,
146         DRV_HASH_SHA1 = 0,
147         DRV_HASH_SHA256 = 1,
148         DRV_HASH_SHA224 = 2,
149         DRV_HASH_SHA512 = 3,
150         DRV_HASH_SHA384 = 4,
151         DRV_HASH_MD5 = 5,
152         DRV_HASH_CBC_MAC = 6,
153         DRV_HASH_XCBC_MAC = 7,
154         DRV_HASH_CMAC = 8,
155         DRV_HASH_MODE_NUM = 9,
156         DRV_HASH_RESERVE32B = S32_MAX
157 };
158
159 enum drv_hash_hw_mode {
160         DRV_HASH_HW_MD5 = 0,
161         DRV_HASH_HW_SHA1 = 1,
162         DRV_HASH_HW_SHA256 = 2,
163         DRV_HASH_HW_SHA224 = 10,
164         DRV_HASH_HW_SHA512 = 4,
165         DRV_HASH_HW_SHA384 = 12,
166         DRV_HASH_HW_GHASH = 6,
167         DRV_HASH_HW_RESERVE32B = S32_MAX
168 };
169
170 enum drv_multi2_mode {
171         DRV_MULTI2_NULL = -1,
172         DRV_MULTI2_ECB = 0,
173         DRV_MULTI2_CBC = 1,
174         DRV_MULTI2_OFB = 2,
175         DRV_MULTI2_RESERVE32B = S32_MAX
176 };
177
178 /* drv_crypto_key_type[1:0] is mapped to cipher_do[1:0] */
179 /* drv_crypto_key_type[2] is mapped to cipher_config2 */
180 enum drv_crypto_key_type {
181         DRV_NULL_KEY = -1,
182         DRV_USER_KEY = 0,               /* 0x000 */
183         DRV_ROOT_KEY = 1,               /* 0x001 */
184         DRV_PROVISIONING_KEY = 2,       /* 0x010 */
185         DRV_SESSION_KEY = 3,            /* 0x011 */
186         DRV_APPLET_KEY = 4,             /* NA */
187         DRV_PLATFORM_KEY = 5,           /* 0x101 */
188         DRV_CUSTOMER_KEY = 6,           /* 0x110 */
189         DRV_END_OF_KEYS = S32_MAX,
190 };
191
192 enum drv_crypto_padding_type {
193         DRV_PADDING_NONE = 0,
194         DRV_PADDING_PKCS7 = 1,
195         DRV_PADDING_RESERVE32B = S32_MAX
196 };
197
198 /*******************************************************************/
199 /***************** DESCRIPTOR BASED CONTEXTS ***********************/
200 /*******************************************************************/
201
202  /* Generic context ("super-class") */
203 struct drv_ctx_generic {
204         enum drv_crypto_alg alg;
205 } __attribute__((__may_alias__));
206
207 struct drv_ctx_hash {
208         enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_HASH */
209         enum drv_hash_mode mode;
210         u8 digest[CC_DIGEST_SIZE_MAX];
211         /* reserve to end of allocated context size */
212         u8 reserved[CC_CTX_SIZE - 2 * sizeof(u32) -
213                         CC_DIGEST_SIZE_MAX];
214 };
215
216 /* !!!! drv_ctx_hmac should have the same structure as drv_ctx_hash except
217    k0, k0_size fields */
218 struct drv_ctx_hmac {
219         enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_HMAC */
220         enum drv_hash_mode mode;
221         u8 digest[CC_DIGEST_SIZE_MAX];
222         u32 k0[CC_HMAC_BLOCK_SIZE_MAX / sizeof(u32)];
223         u32 k0_size;
224         /* reserve to end of allocated context size */
225         u8 reserved[CC_CTX_SIZE - 3 * sizeof(u32) -
226                         CC_DIGEST_SIZE_MAX - CC_HMAC_BLOCK_SIZE_MAX];
227 };
228
229 struct drv_ctx_cipher {
230         enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_AES */
231         enum drv_cipher_mode mode;
232         enum drv_crypto_direction direction;
233         enum drv_crypto_key_type crypto_key_type;
234         enum drv_crypto_padding_type padding_type;
235         u32 key_size; /* numeric value in bytes   */
236         u32 data_unit_size; /* required for XTS */
237         /* block_state is the AES engine block state.
238         *  It is used by the host to pass IV or counter at initialization.
239         *  It is used by SeP for intermediate block chaining state and for
240         *  returning MAC algorithms results.           */
241         u8 block_state[CC_AES_BLOCK_SIZE];
242         u8 key[CC_AES_KEY_SIZE_MAX];
243         u8 xex_key[CC_AES_KEY_SIZE_MAX];
244         /* reserve to end of allocated context size */
245         u32 reserved[CC_DRV_CTX_SIZE_WORDS - 7 -
246                 CC_AES_BLOCK_SIZE / sizeof(u32) - 2 *
247                 (CC_AES_KEY_SIZE_MAX / sizeof(u32))];
248 };
249
250 /* authentication and encryption with associated data class */
251 struct drv_ctx_aead {
252         enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_AES */
253         enum drv_cipher_mode mode;
254         enum drv_crypto_direction direction;
255         u32 key_size; /* numeric value in bytes   */
256         u32 nonce_size; /* nonce size (octets) */
257         u32 header_size; /* finit additional data size (octets) */
258         u32 text_size; /* finit text data size (octets) */
259         u32 tag_size; /* mac size, element of {4, 6, 8, 10, 12, 14, 16} */
260         /* block_state1/2 is the AES engine block state */
261         u8 block_state[CC_AES_BLOCK_SIZE];
262         u8 mac_state[CC_AES_BLOCK_SIZE]; /* MAC result */
263         u8 nonce[CC_AES_BLOCK_SIZE]; /* nonce buffer */
264         u8 key[CC_AES_KEY_SIZE_MAX];
265         /* reserve to end of allocated context size */
266         u32 reserved[CC_DRV_CTX_SIZE_WORDS - 8 -
267                 3 * (CC_AES_BLOCK_SIZE / sizeof(u32)) -
268                 CC_AES_KEY_SIZE_MAX / sizeof(u32)];
269 };
270
271 /*******************************************************************/
272 /***************** MESSAGE BASED CONTEXTS **************************/
273 /*******************************************************************/
274
275 /* Get the address of a @member within a given @ctx address
276    @ctx: The context address
277    @type: Type of context structure
278    @member: Associated context field */
279 #define GET_CTX_FIELD_ADDR(ctx, type, member) (ctx + offsetof(type, member))
280
281 #endif /* _CC_CRYPTO_CTX_H_ */
282