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