1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2021, SumUp Services GmbH
5  */
6 #ifndef UTEE_DEFINES_H
7 #define UTEE_DEFINES_H
8 
9 #include <compiler.h>
10 #include <tee_api_defines.h>
11 #include <tee_api_defines_extensions.h>
12 #include <types_ext.h>
13 
14 /*
15  * Copied from TEE Internal API specificaion v1.0 table 6-9 "Structure of
16  * Algorithm Identifier".
17  */
18 #define TEE_MAIN_ALGO_MD5        0x01
19 #define TEE_MAIN_ALGO_SHA1       0x02
20 #define TEE_MAIN_ALGO_SHA224     0x03
21 #define TEE_MAIN_ALGO_SHA256     0x04
22 #define TEE_MAIN_ALGO_SHA384     0x05
23 #define TEE_MAIN_ALGO_SHA512     0x06
24 #define TEE_MAIN_ALGO_SM3        0x07
25 #define TEE_MAIN_ALGO_AES        0x10
26 #define TEE_MAIN_ALGO_DES        0x11
27 #define TEE_MAIN_ALGO_DES2       0x12
28 #define TEE_MAIN_ALGO_DES3       0x13
29 #define TEE_MAIN_ALGO_SM4        0x14 /* Not in v1.2, extrapolated */
30 #define TEE_MAIN_ALGO_RSA        0x30
31 #define TEE_MAIN_ALGO_DSA        0x31
32 #define TEE_MAIN_ALGO_DH         0x32
33 #define TEE_MAIN_ALGO_ECDSA      0x41
34 #define TEE_MAIN_ALGO_ECDH       0x42
35 #define TEE_MAIN_ALGO_ED25519    0x43
36 #define TEE_MAIN_ALGO_SM2_DSA_SM3 0x45 /* Not in v1.2 spec */
37 #define TEE_MAIN_ALGO_SM2_KEP    0x46 /* Not in v1.2 spec */
38 #define TEE_MAIN_ALGO_SM2_PKE    0x47 /* Not in v1.2 spec */
39 #define TEE_MAIN_ALGO_HKDF       0xC0 /* OP-TEE extension */
40 #define TEE_MAIN_ALGO_CONCAT_KDF 0xC1 /* OP-TEE extension */
41 #define TEE_MAIN_ALGO_PBKDF2     0xC2 /* OP-TEE extension */
42 #define TEE_MAIN_ALGO_X25519     0x44 /* Not in v1.2 spec */
43 
44 
45 #define TEE_CHAIN_MODE_ECB_NOPAD        0x0
46 #define TEE_CHAIN_MODE_CBC_NOPAD        0x1
47 #define TEE_CHAIN_MODE_CTR              0x2
48 #define TEE_CHAIN_MODE_CTS              0x3
49 #define TEE_CHAIN_MODE_XTS              0x4
50 #define TEE_CHAIN_MODE_CBC_MAC_PKCS5    0x5
51 #define TEE_CHAIN_MODE_CMAC             0x6
52 #define TEE_CHAIN_MODE_CCM              0x7
53 #define TEE_CHAIN_MODE_GCM              0x8
54 #define TEE_CHAIN_MODE_PKCS1_PSS_MGF1   0x9	/* ??? */
55 
56 
__tee_alg_get_class(uint32_t algo)57 static inline uint32_t __tee_alg_get_class(uint32_t algo)
58 {
59 	if (algo == TEE_ALG_SM2_PKE)
60 		return TEE_OPERATION_ASYMMETRIC_CIPHER;
61 	if (algo == TEE_ALG_SM2_KEP)
62 		return TEE_OPERATION_KEY_DERIVATION;
63 	if (algo == TEE_ALG_RSASSA_PKCS1_V1_5)
64 		return TEE_OPERATION_ASYMMETRIC_SIGNATURE;
65 	if (algo == TEE_ALG_DES3_CMAC)
66 		return TEE_OPERATION_MAC;
67 	if (algo == TEE_ALG_SM4_XTS)
68 		return TEE_OPERATION_CIPHER;
69 
70 	return (algo >> 28) & 0xF; /* Bits [31:28] */
71 }
72 
73 #define TEE_ALG_GET_CLASS(algo) __tee_alg_get_class(algo)
74 
__tee_alg_get_main_alg(uint32_t algo)75 static inline uint32_t __tee_alg_get_main_alg(uint32_t algo)
76 {
77 	switch (algo) {
78 	case TEE_ALG_SM2_PKE:
79 		return TEE_MAIN_ALGO_SM2_PKE;
80 	case TEE_ALG_SM2_KEP:
81 		return TEE_MAIN_ALGO_SM2_KEP;
82 	case TEE_ALG_X25519:
83 		return TEE_MAIN_ALGO_X25519;
84 	case TEE_ALG_ED25519:
85 		return TEE_MAIN_ALGO_ED25519;
86 	default:
87 		break;
88 	}
89 
90 	return algo & 0xff;
91 }
92 
93 #define TEE_ALG_GET_MAIN_ALG(algo) __tee_alg_get_main_alg(algo)
94 
95 	/* Bits [11:8] */
96 #define TEE_ALG_GET_CHAIN_MODE(algo)    (((algo) >> 8) & 0xF)
97 
98 /*
99  * Value not defined in the GP spec, and not used as bits 15-12 of any TEE_ALG*
100  * value. TEE_ALG_SM2_DSA_SM3 has value 0x6 for bits 15-12 which would yield the
101  * SHA512 digest if we were to apply the bit masks that were valid up to the TEE
102  * Internal Core API v1.1.
103  */
104 #define __TEE_MAIN_HASH_SM3 0x7
105 
__tee_alg_get_digest_hash(uint32_t algo)106 static inline uint32_t __tee_alg_get_digest_hash(uint32_t algo)
107 {
108 	if (algo == TEE_ALG_SM2_DSA_SM3)
109 		return __TEE_MAIN_HASH_SM3;
110 
111 	/* Bits [15:12] */
112 	return (algo >> 12) & 0xF;
113 }
114 
115 #define TEE_ALG_GET_DIGEST_HASH(algo) __tee_alg_get_digest_hash(algo)
116 
117 	/* Bits [23:20] */
118 #define TEE_ALG_GET_INTERNAL_HASH(algo) (((algo) >> 20) & 0x7)
119 
__tee_alg_get_key_type(uint32_t algo,bool with_priv)120 static inline uint32_t __tee_alg_get_key_type(uint32_t algo, bool with_priv)
121 {
122 	uint32_t key_type = 0xA0000000 |  TEE_ALG_GET_MAIN_ALG(algo);
123 
124 	if (with_priv)
125 		key_type |= 0x01000000;
126 
127 	return key_type;
128 }
129 
130 #define TEE_ALG_GET_KEY_TYPE(algo, with_private_key) \
131 	__tee_alg_get_key_type(algo, with_private_key)
132 
__tee_alg_hash_algo(uint32_t main_hash)133 static inline uint32_t __tee_alg_hash_algo(uint32_t main_hash)
134 {
135 	if (main_hash == __TEE_MAIN_HASH_SM3)
136 		return TEE_ALG_SM3;
137 
138 	return (TEE_OPERATION_DIGEST << 28) | main_hash;
139 }
140 
141 	/* Return hash algorithm based on main hash */
142 #define TEE_ALG_HASH_ALGO(main_hash) __tee_alg_hash_algo(main_hash)
143 
144 	/* Extract internal hash and return hash algorithm */
145 #define TEE_INTERNAL_HASH_TO_ALGO(algo) \
146                 TEE_ALG_HASH_ALGO(TEE_ALG_GET_INTERNAL_HASH(algo))
147 
148 	/* Extract digest hash and return hash algorithm */
149 #define TEE_DIGEST_HASH_TO_ALGO(algo) \
150                 TEE_ALG_HASH_ALGO(TEE_ALG_GET_DIGEST_HASH(algo))
151 
152 /* Return HMAC algorithm based on main hash */
153 #define TEE_ALG_HMAC_ALGO(main_hash) \
154 	(TEE_OPERATION_MAC << 28 | (main_hash))
155 
156 #define TEE_AES_BLOCK_SIZE  16UL
157 #define TEE_DES_BLOCK_SIZE  8UL
158 #define TEE_SM4_BLOCK_SIZE  16UL
159 
160 #define TEE_AES_MAX_KEY_SIZE    32UL
161 
162 	/* SHA-512 */
163 #ifndef TEE_MD5_HASH_SIZE
164 typedef enum {
165 	TEE_MD5_HASH_SIZE = 16,
166 	TEE_SHA1_HASH_SIZE = 20,
167 	TEE_SHA224_HASH_SIZE = 28,
168 	TEE_SHA256_HASH_SIZE = 32,
169 	TEE_SM3_HASH_SIZE = 32,
170 	TEE_SHA384_HASH_SIZE = 48,
171 	TEE_SHA512_HASH_SIZE = 64,
172 	TEE_MD5SHA1_HASH_SIZE = (TEE_MD5_HASH_SIZE + TEE_SHA1_HASH_SIZE),
173 	TEE_MAX_HASH_SIZE = 64,
174 } t_hash_size;
175 #endif
176 
177 #define TEE_MAC_SIZE_AES_CBC_MAC_NOPAD
178 #define TEE_MAC_SIZE_AES_CBC_MAC_PKCS5
179 #define TEE_MAC_SIZE_AES_CMAC
180 #define TEE_MAC_SIZE_DES_CBC_MAC_PKCS5
181 
__tee_alg_get_digest_size(uint32_t algo)182 static inline size_t __tee_alg_get_digest_size(uint32_t algo)
183 {
184 	switch (algo) {
185 	case TEE_ALG_MD5:
186 	case TEE_ALG_HMAC_MD5:
187 		return TEE_MD5_HASH_SIZE;
188 	case TEE_ALG_SHA1:
189 	case TEE_ALG_HMAC_SHA1:
190 	case TEE_ALG_DSA_SHA1:
191 		return TEE_SHA1_HASH_SIZE;
192 	case TEE_ALG_SHA224:
193 	case TEE_ALG_HMAC_SHA224:
194 	case TEE_ALG_DSA_SHA224:
195 		return TEE_SHA224_HASH_SIZE;
196 	case TEE_ALG_SHA256:
197 	case TEE_ALG_HMAC_SHA256:
198 	case TEE_ALG_DSA_SHA256:
199 		return TEE_SHA256_HASH_SIZE;
200 	case TEE_ALG_SHA384:
201 	case TEE_ALG_HMAC_SHA384:
202 		return TEE_SHA384_HASH_SIZE;
203 	case TEE_ALG_SHA512:
204 	case TEE_ALG_HMAC_SHA512:
205 		return TEE_SHA512_HASH_SIZE;
206 	case TEE_ALG_SM3:
207 	case TEE_ALG_HMAC_SM3:
208 		return TEE_SM3_HASH_SIZE;
209 	case TEE_ALG_AES_CBC_MAC_NOPAD:
210 	case TEE_ALG_AES_CBC_MAC_PKCS5:
211 	case TEE_ALG_AES_CMAC:
212 		return TEE_AES_BLOCK_SIZE;
213 	case TEE_ALG_DES_CBC_MAC_NOPAD:
214 	case TEE_ALG_DES_CBC_MAC_PKCS5:
215 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
216 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
217 	case TEE_ALG_DES3_CMAC:
218 		return TEE_DES_BLOCK_SIZE;
219 	default:
220 		return 0;
221 	}
222 }
223 
224 	/* Return algorithm digest size */
225 #define TEE_ALG_GET_DIGEST_SIZE(algo) __tee_alg_get_digest_size(algo)
226 
227 /*
228  * Bit indicating that the attribute is a value attribute
229  * See TEE Internal API specificaion v1.0 table 6-12 "Partial Structure of
230  * Attribute Identifier"
231  */
232 
233 
234 #ifdef __compiler_bswap64
235 #define TEE_U64_BSWAP(x) __compiler_bswap64((x))
236 #else
237 #define TEE_U64_BSWAP(x) ((uint64_t)( \
238         (((uint64_t)(x) & UINT64_C(0xff00000000000000ULL)) >> 56) | \
239         (((uint64_t)(x) & UINT64_C(0x00ff000000000000ULL)) >> 40) | \
240         (((uint64_t)(x) & UINT64_C(0x0000ff0000000000ULL)) >> 24) | \
241         (((uint64_t)(x) & UINT64_C(0x000000ff00000000ULL)) >>  8) | \
242         (((uint64_t)(x) & UINT64_C(0x00000000ff000000ULL)) <<  8) | \
243         (((uint64_t)(x) & UINT64_C(0x0000000000ff0000ULL)) << 24) | \
244         (((uint64_t)(x) & UINT64_C(0x000000000000ff00ULL)) << 40) | \
245         (((uint64_t)(x) & UINT64_C(0x00000000000000ffULL)) << 56)))
246 #endif
247 
248 #ifdef __compiler_bswap32
249 #define TEE_U32_BSWAP(x) __compiler_bswap32((x))
250 #else
251 #define TEE_U32_BSWAP(x) ((uint32_t)( \
252         (((uint32_t)(x) & UINT32_C(0xff000000)) >> 24) | \
253         (((uint32_t)(x) & UINT32_C(0x00ff0000)) >>  8) | \
254         (((uint32_t)(x) & UINT32_C(0x0000ff00)) <<  8) | \
255         (((uint32_t)(x) & UINT32_C(0x000000ff)) << 24)))
256 #endif
257 
258 #ifdef __compiler_bswap16
259 #define TEE_U16_BSWAP(x) __compiler_bswap16((x))
260 #else
261 #define TEE_U16_BSWAP(x) ((uint16_t)( \
262         (((uint16_t)(x) & UINT16_C(0xff00)) >> 8) | \
263         (((uint16_t)(x) & UINT16_C(0x00ff)) << 8)))
264 #endif
265 
266 /* If we we're on a big endian platform we'll have to update these */
267 #define TEE_U64_FROM_BIG_ENDIAN(x)  TEE_U64_BSWAP(x)
268 #define TEE_U32_FROM_BIG_ENDIAN(x)  TEE_U32_BSWAP(x)
269 #define TEE_U16_FROM_BIG_ENDIAN(x)  TEE_U16_BSWAP(x)
270 #define TEE_U64_TO_BIG_ENDIAN(x)    TEE_U64_BSWAP(x)
271 #define TEE_U32_TO_BIG_ENDIAN(x)    TEE_U32_BSWAP(x)
272 #define TEE_U16_TO_BIG_ENDIAN(x)    TEE_U16_BSWAP(x)
273 
274 #define TEE_TIME_MILLIS_BASE    1000
275 
276 #define TEE_TIME_LT(t1, t2)				\
277     (((t1).seconds == (t2).seconds) ?			\
278         ((t1).millis < (t2).millis) :			\
279         ((t1).seconds < (t2).seconds))
280 
281 #define TEE_TIME_LE(t1, t2)				\
282     (((t1).seconds == (t2).seconds) ?			\
283         ((t1).millis <= (t2).millis) :			\
284         ((t1).seconds <= (t2).seconds))
285 
286 #define TEE_TIME_ADD(t1, t2, dst) do {                      \
287         (dst).seconds = (t1).seconds + (t2).seconds;        \
288         (dst).millis = (t1).millis + (t2).millis;           \
289         if ((dst).millis >= TEE_TIME_MILLIS_BASE) {         \
290             (dst).seconds++;                                \
291             (dst).millis -= TEE_TIME_MILLIS_BASE;           \
292         }                                                   \
293     } while (0)
294 
295 #define TEE_TIME_SUB(t1, t2, dst) do {                      \
296         (dst).seconds = (t1).seconds - (t2).seconds;        \
297         if ((t1).millis < (t2).millis) {                    \
298             (dst).seconds--;                                \
299             (dst).millis = (t1).millis + TEE_TIME_MILLIS_BASE - (t2).millis;\
300         } else {                                            \
301             (dst).millis = (t1).millis - (t2).millis;       \
302         }                                                   \
303     } while (0)
304 
305 /* ------------------------------------------------------------ */
306 /* OTP mapping                                                  */
307 /* ------------------------------------------------------------ */
308 #define HW_UNIQUE_KEY_WORD1      (8)
309 #define HW_UNIQUE_KEY_LENGTH     (16)
310 #define HW_UNIQUE_KEY_WORD2      (HW_UNIQUE_KEY_WORD1 + 1)
311 #define HW_UNIQUE_KEY_WORD3      (HW_UNIQUE_KEY_WORD1 + 2)
312 #define HW_UNIQUE_KEY_WORD4      (HW_UNIQUE_KEY_WORD1 + 3)
313 
314 #define UTEE_SE_READER_PRESENT			(1 << 0)
315 #define UTEE_SE_READER_TEE_ONLY			(1 << 1)
316 #define UTEE_SE_READER_SELECT_RESPONE_ENABLE	(1 << 2)
317 
318 #endif /* UTEE_DEFINES_H */
319