2 * Copyright (C) 2012-2017 ARM Limited or its affiliates.
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.
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.
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/>.
18 #ifndef _CC_CRYPTO_CTX_H_
19 #define _CC_CRYPTO_CTX_H_
21 #include <linux/types.h>
25 #define max(a, b) ((a) > (b) ? (a) : (b))
26 #define min(a, b) ((a) < (b) ? (a) : (b))
30 #ifndef CC_CTX_SIZE_LOG2
31 #if (CC_SUPPORT_SHA > 256)
32 #define CC_CTX_SIZE_LOG2 8
34 #define CC_CTX_SIZE_LOG2 7
37 #define CC_CTX_SIZE (1<<CC_CTX_SIZE_LOG2)
38 #define CC_DRV_CTX_SIZE_WORDS (CC_CTX_SIZE >> 2)
40 #define CC_DRV_DES_IV_SIZE 8
41 #define CC_DRV_DES_BLOCK_SIZE 8
43 #define CC_DRV_DES_ONE_KEY_SIZE 8
44 #define CC_DRV_DES_DOUBLE_KEY_SIZE 16
45 #define CC_DRV_DES_TRIPLE_KEY_SIZE 24
46 #define CC_DRV_DES_KEY_SIZE_MAX CC_DRV_DES_TRIPLE_KEY_SIZE
48 #define CC_AES_IV_SIZE 16
49 #define CC_AES_IV_SIZE_WORDS (CC_AES_IV_SIZE >> 2)
51 #define CC_AES_BLOCK_SIZE 16
52 #define CC_AES_BLOCK_SIZE_WORDS 4
54 #define CC_AES_128_BIT_KEY_SIZE 16
55 #define CC_AES_128_BIT_KEY_SIZE_WORDS (CC_AES_128_BIT_KEY_SIZE >> 2)
56 #define CC_AES_192_BIT_KEY_SIZE 24
57 #define CC_AES_192_BIT_KEY_SIZE_WORDS (CC_AES_192_BIT_KEY_SIZE >> 2)
58 #define CC_AES_256_BIT_KEY_SIZE 32
59 #define CC_AES_256_BIT_KEY_SIZE_WORDS (CC_AES_256_BIT_KEY_SIZE >> 2)
60 #define CC_AES_KEY_SIZE_MAX CC_AES_256_BIT_KEY_SIZE
61 #define CC_AES_KEY_SIZE_WORDS_MAX (CC_AES_KEY_SIZE_MAX >> 2)
63 #define CC_MD5_DIGEST_SIZE 16
64 #define CC_SHA1_DIGEST_SIZE 20
65 #define CC_SHA224_DIGEST_SIZE 28
66 #define CC_SHA256_DIGEST_SIZE 32
67 #define CC_SHA256_DIGEST_SIZE_IN_WORDS 8
68 #define CC_SHA384_DIGEST_SIZE 48
69 #define CC_SHA512_DIGEST_SIZE 64
71 #define CC_SHA1_BLOCK_SIZE 64
72 #define CC_SHA1_BLOCK_SIZE_IN_WORDS 16
73 #define CC_MD5_BLOCK_SIZE 64
74 #define CC_MD5_BLOCK_SIZE_IN_WORDS 16
75 #define CC_SHA224_BLOCK_SIZE 64
76 #define CC_SHA256_BLOCK_SIZE 64
77 #define CC_SHA256_BLOCK_SIZE_IN_WORDS 16
78 #define CC_SHA1_224_256_BLOCK_SIZE 64
79 #define CC_SHA384_BLOCK_SIZE 128
80 #define CC_SHA512_BLOCK_SIZE 128
82 #if (CC_SUPPORT_SHA > 256)
83 #define CC_DIGEST_SIZE_MAX CC_SHA512_DIGEST_SIZE
84 #define CC_HASH_BLOCK_SIZE_MAX CC_SHA512_BLOCK_SIZE /*1024b*/
85 #else /* Only up to SHA256 */
86 #define CC_DIGEST_SIZE_MAX CC_SHA256_DIGEST_SIZE
87 #define CC_HASH_BLOCK_SIZE_MAX CC_SHA256_BLOCK_SIZE /*512b*/
90 #define CC_HMAC_BLOCK_SIZE_MAX CC_HASH_BLOCK_SIZE_MAX
92 #define CC_MULTI2_SYSTEM_KEY_SIZE 32
93 #define CC_MULTI2_DATA_KEY_SIZE 8
94 #define CC_MULTI2_SYSTEM_N_DATA_KEY_SIZE (CC_MULTI2_SYSTEM_KEY_SIZE + CC_MULTI2_DATA_KEY_SIZE)
95 #define CC_MULTI2_BLOCK_SIZE 8
96 #define CC_MULTI2_IV_SIZE 8
97 #define CC_MULTI2_MIN_NUM_ROUNDS 8
98 #define CC_MULTI2_MAX_NUM_ROUNDS 128
101 #define CC_DRV_ALG_MAX_BLOCK_SIZE CC_HASH_BLOCK_SIZE_MAX
104 enum drv_engine_type {
111 DRV_ENGINE_RESERVE32B = S32_MAX,
114 enum drv_crypto_alg {
115 DRV_CRYPTO_ALG_NULL = -1,
116 DRV_CRYPTO_ALG_AES = 0,
117 DRV_CRYPTO_ALG_DES = 1,
118 DRV_CRYPTO_ALG_HASH = 2,
119 DRV_CRYPTO_ALG_C2 = 3,
120 DRV_CRYPTO_ALG_HMAC = 4,
121 DRV_CRYPTO_ALG_AEAD = 5,
122 DRV_CRYPTO_ALG_BYPASS = 6,
123 DRV_CRYPTO_ALG_NUM = 7,
124 DRV_CRYPTO_ALG_RESERVE32B = S32_MAX
127 enum drv_crypto_direction {
128 DRV_CRYPTO_DIRECTION_NULL = -1,
129 DRV_CRYPTO_DIRECTION_ENCRYPT = 0,
130 DRV_CRYPTO_DIRECTION_DECRYPT = 1,
131 DRV_CRYPTO_DIRECTION_DECRYPT_ENCRYPT = 3,
132 DRV_CRYPTO_DIRECTION_RESERVE32B = S32_MAX
135 enum drv_cipher_mode {
136 DRV_CIPHER_NULL_MODE = -1,
140 DRV_CIPHER_CBC_MAC = 3,
142 DRV_CIPHER_XCBC_MAC = 5,
146 DRV_CIPHER_CBC_CTS = 11,
147 DRV_CIPHER_GCTR = 12,
148 DRV_CIPHER_ESSIV = 13,
149 DRV_CIPHER_BITLOCKER = 14,
150 DRV_CIPHER_RESERVE32B = S32_MAX
161 DRV_HASH_CBC_MAC = 6,
162 DRV_HASH_XCBC_MAC = 7,
164 DRV_HASH_MODE_NUM = 9,
165 DRV_HASH_RESERVE32B = S32_MAX
168 enum drv_hash_hw_mode {
170 DRV_HASH_HW_SHA1 = 1,
171 DRV_HASH_HW_SHA256 = 2,
172 DRV_HASH_HW_SHA224 = 10,
173 DRV_HASH_HW_SHA512 = 4,
174 DRV_HASH_HW_SHA384 = 12,
175 DRV_HASH_HW_GHASH = 6,
176 DRV_HASH_HW_RESERVE32B = S32_MAX
179 enum drv_multi2_mode {
180 DRV_MULTI2_NULL = -1,
184 DRV_MULTI2_RESERVE32B = S32_MAX
188 /* drv_crypto_key_type[1:0] is mapped to cipher_do[1:0] */
189 /* drv_crypto_key_type[2] is mapped to cipher_config2 */
190 enum drv_crypto_key_type {
192 DRV_USER_KEY = 0, /* 0x000 */
193 DRV_ROOT_KEY = 1, /* 0x001 */
194 DRV_PROVISIONING_KEY = 2, /* 0x010 */
195 DRV_SESSION_KEY = 3, /* 0x011 */
196 DRV_APPLET_KEY = 4, /* NA */
197 DRV_PLATFORM_KEY = 5, /* 0x101 */
198 DRV_CUSTOMER_KEY = 6, /* 0x110 */
199 DRV_END_OF_KEYS = S32_MAX,
202 enum drv_crypto_padding_type {
203 DRV_PADDING_NONE = 0,
204 DRV_PADDING_PKCS7 = 1,
205 DRV_PADDING_RESERVE32B = S32_MAX
208 /*******************************************************************/
209 /***************** DESCRIPTOR BASED CONTEXTS ***********************/
210 /*******************************************************************/
212 /* Generic context ("super-class") */
213 struct drv_ctx_generic {
214 enum drv_crypto_alg alg;
215 } __attribute__((__may_alias__));
218 struct drv_ctx_hash {
219 enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_HASH */
220 enum drv_hash_mode mode;
221 u8 digest[CC_DIGEST_SIZE_MAX];
222 /* reserve to end of allocated context size */
223 u8 reserved[CC_CTX_SIZE - 2 * sizeof(u32) -
227 /* !!!! drv_ctx_hmac should have the same structure as drv_ctx_hash except
228 k0, k0_size fields */
229 struct drv_ctx_hmac {
230 enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_HMAC */
231 enum drv_hash_mode mode;
232 u8 digest[CC_DIGEST_SIZE_MAX];
233 u32 k0[CC_HMAC_BLOCK_SIZE_MAX/sizeof(u32)];
235 /* reserve to end of allocated context size */
236 u8 reserved[CC_CTX_SIZE - 3 * sizeof(u32) -
237 CC_DIGEST_SIZE_MAX - CC_HMAC_BLOCK_SIZE_MAX];
240 struct drv_ctx_cipher {
241 enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_AES */
242 enum drv_cipher_mode mode;
243 enum drv_crypto_direction direction;
244 enum drv_crypto_key_type crypto_key_type;
245 enum drv_crypto_padding_type padding_type;
246 u32 key_size; /* numeric value in bytes */
247 u32 data_unit_size; /* required for XTS */
248 /* block_state is the AES engine block state.
249 * It is used by the host to pass IV or counter at initialization.
250 * It is used by SeP for intermediate block chaining state and for
251 * returning MAC algorithms results. */
252 u8 block_state[CC_AES_BLOCK_SIZE];
253 u8 key[CC_AES_KEY_SIZE_MAX];
254 u8 xex_key[CC_AES_KEY_SIZE_MAX];
255 /* reserve to end of allocated context size */
256 u32 reserved[CC_DRV_CTX_SIZE_WORDS - 7 -
257 CC_AES_BLOCK_SIZE/sizeof(u32) - 2 *
258 (CC_AES_KEY_SIZE_MAX/sizeof(u32))];
261 /* authentication and encryption with associated data class */
262 struct drv_ctx_aead {
263 enum drv_crypto_alg alg; /* DRV_CRYPTO_ALG_AES */
264 enum drv_cipher_mode mode;
265 enum drv_crypto_direction direction;
266 u32 key_size; /* numeric value in bytes */
267 u32 nonce_size; /* nonce size (octets) */
268 u32 header_size; /* finit additional data size (octets) */
269 u32 text_size; /* finit text data size (octets) */
270 u32 tag_size; /* mac size, element of {4, 6, 8, 10, 12, 14, 16} */
271 /* block_state1/2 is the AES engine block state */
272 u8 block_state[CC_AES_BLOCK_SIZE];
273 u8 mac_state[CC_AES_BLOCK_SIZE]; /* MAC result */
274 u8 nonce[CC_AES_BLOCK_SIZE]; /* nonce buffer */
275 u8 key[CC_AES_KEY_SIZE_MAX];
276 /* reserve to end of allocated context size */
277 u32 reserved[CC_DRV_CTX_SIZE_WORDS - 8 -
278 3 * (CC_AES_BLOCK_SIZE/sizeof(u32)) -
279 CC_AES_KEY_SIZE_MAX/sizeof(u32)];
282 /*******************************************************************/
283 /***************** MESSAGE BASED CONTEXTS **************************/
284 /*******************************************************************/
287 /* Get the address of a @member within a given @ctx address
288 @ctx: The context address
289 @type: Type of context structure
290 @member: Associated context field */
291 #define GET_CTX_FIELD_ADDR(ctx, type, member) (ctx + offsetof(type, member))
293 #endif /* _CC_CRYPTO_CTX_H_ */