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