xref: /optee_os/lib/libutee/include/utee_defines.h (revision ade6f848e084d7c63f1e3866d7c059b8a9f9e834)
11bb92983SJerome Forissier /* SPDX-License-Identifier: BSD-2-Clause */
2b0104773SPascal Brand /*
3b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
4b0104773SPascal Brand  */
5b0104773SPascal Brand #ifndef UTEE_DEFINES_H
6b0104773SPascal Brand #define UTEE_DEFINES_H
7b0104773SPascal Brand 
809d93d24SJens Wiklander #include <compiler.h>
909d93d24SJens Wiklander #include <types_ext.h>
1009d93d24SJens Wiklander 
11b0104773SPascal Brand /*
12b0104773SPascal Brand  * Copied from TEE Internal API specificaion v1.0 table 6-9 "Structure of
13b0104773SPascal Brand  * Algorithm Identifier".
14b0104773SPascal Brand  */
15b0104773SPascal Brand #define TEE_MAIN_ALGO_MD5        0x01
16b0104773SPascal Brand #define TEE_MAIN_ALGO_SHA1       0x02
17b0104773SPascal Brand #define TEE_MAIN_ALGO_SHA224     0x03
18b0104773SPascal Brand #define TEE_MAIN_ALGO_SHA256     0x04
19b0104773SPascal Brand #define TEE_MAIN_ALGO_SHA384     0x05
20b0104773SPascal Brand #define TEE_MAIN_ALGO_SHA512     0x06
21b0104773SPascal Brand #define TEE_MAIN_ALGO_AES        0x10
22b0104773SPascal Brand #define TEE_MAIN_ALGO_DES        0x11
23b0104773SPascal Brand #define TEE_MAIN_ALGO_DES2       0x12
24b0104773SPascal Brand #define TEE_MAIN_ALGO_DES3       0x13
25*ade6f848SJerome Forissier #define TEE_MAIN_ALGO_SM4        0x14 /* Not in v1.2, extrapolated */
26b0104773SPascal Brand #define TEE_MAIN_ALGO_RSA        0x30
27b0104773SPascal Brand #define TEE_MAIN_ALGO_DSA        0x31
28b0104773SPascal Brand #define TEE_MAIN_ALGO_DH         0x32
29c988227aSPascal Brand #define TEE_MAIN_ALGO_ECDSA      0x41
30c988227aSPascal Brand #define TEE_MAIN_ALGO_ECDH       0x42
31cdb198a7SJerome Forissier #define TEE_MAIN_ALGO_HKDF       0xC0 /* OP-TEE extension */
328854d3c6SJerome Forissier #define TEE_MAIN_ALGO_CONCAT_KDF 0xC1 /* OP-TEE extension */
330f2293b7SJerome Forissier #define TEE_MAIN_ALGO_PBKDF2     0xC2 /* OP-TEE extension */
348854d3c6SJerome Forissier 
35b0104773SPascal Brand 
36b0104773SPascal Brand #define TEE_CHAIN_MODE_ECB_NOPAD        0x0
37b0104773SPascal Brand #define TEE_CHAIN_MODE_CBC_NOPAD        0x1
38b0104773SPascal Brand #define TEE_CHAIN_MODE_CTR              0x2
39b0104773SPascal Brand #define TEE_CHAIN_MODE_CTS              0x3
40b0104773SPascal Brand #define TEE_CHAIN_MODE_XTS              0x4
41b0104773SPascal Brand #define TEE_CHAIN_MODE_CBC_MAC_PKCS5    0x5
42b0104773SPascal Brand #define TEE_CHAIN_MODE_CMAC             0x6
43b0104773SPascal Brand #define TEE_CHAIN_MODE_CCM              0x7
44b0104773SPascal Brand #define TEE_CHAIN_MODE_GCM              0x8
45b0104773SPascal Brand #define TEE_CHAIN_MODE_PKCS1_PSS_MGF1   0x9	/* ??? */
46b0104773SPascal Brand 
47b0104773SPascal Brand 	/* Bits [31:28] */
48b0104773SPascal Brand #define TEE_ALG_GET_CLASS(algo)         (((algo) >> 28) & 0xF)
49b0104773SPascal Brand 
50b0104773SPascal Brand #define TEE_ALG_GET_KEY_TYPE(algo, with_private_key) \
51b0104773SPascal Brand         (TEE_ALG_GET_MAIN_ALG(algo) | \
52b0104773SPascal Brand             ((with_private_key) ? 0xA1000000 : 0xA0000000))
53b0104773SPascal Brand 
54b0104773SPascal Brand 	/* Bits [7:0] */
55b0104773SPascal Brand #define TEE_ALG_GET_MAIN_ALG(algo)      ((algo) & 0xFF)
56b0104773SPascal Brand 
57b0104773SPascal Brand 	/* Bits [11:8] */
58b0104773SPascal Brand #define TEE_ALG_GET_CHAIN_MODE(algo)    (((algo) >> 8) & 0xF)
59b0104773SPascal Brand 
60b0104773SPascal Brand 	/* Bits [15:12] */
61b0104773SPascal Brand #define TEE_ALG_GET_DIGEST_HASH(algo)   (((algo) >> 12) & 0xF)
62b0104773SPascal Brand 
63b0104773SPascal Brand 	/* Bits [23:20] */
64b0104773SPascal Brand #define TEE_ALG_GET_INTERNAL_HASH(algo) (((algo) >> 20) & 0x7)
65b0104773SPascal Brand 
66b0104773SPascal Brand 	/* Return hash algorithm based on main hash */
67b0104773SPascal Brand #define TEE_ALG_HASH_ALGO(main_hash) \
68b0104773SPascal Brand         (TEE_OPERATION_DIGEST << 28 | (main_hash))
69b0104773SPascal Brand 
70b0104773SPascal Brand 	/* Extract internal hash and return hash algorithm */
71b0104773SPascal Brand #define TEE_INTERNAL_HASH_TO_ALGO(algo) \
72b0104773SPascal Brand                 TEE_ALG_HASH_ALGO(TEE_ALG_GET_INTERNAL_HASH(algo))
73b0104773SPascal Brand 
74b0104773SPascal Brand 	/* Extract digest hash and return hash algorithm */
75b0104773SPascal Brand #define TEE_DIGEST_HASH_TO_ALGO(algo) \
76b0104773SPascal Brand                 TEE_ALG_HASH_ALGO(TEE_ALG_GET_DIGEST_HASH(algo))
77b0104773SPascal Brand 
78cdb198a7SJerome Forissier /* Return HMAC algorithm based on main hash */
79cdb198a7SJerome Forissier #define TEE_ALG_HMAC_ALGO(main_hash) \
80cdb198a7SJerome Forissier 	(TEE_OPERATION_MAC << 28 | (main_hash))
81cdb198a7SJerome Forissier 
82b0104773SPascal Brand #define TEE_AES_BLOCK_SIZE  16UL
83b0104773SPascal Brand #define TEE_DES_BLOCK_SIZE  8UL
84*ade6f848SJerome Forissier #define TEE_SM4_BLOCK_SIZE  16UL
85b0104773SPascal Brand 
86b0104773SPascal Brand #define TEE_AES_MAX_KEY_SIZE    32UL
87b0104773SPascal Brand 
88b0104773SPascal Brand 	/* SHA-512 */
89b0104773SPascal Brand #ifndef TEE_MD5_HASH_SIZE
90b0104773SPascal Brand typedef enum {
91b0104773SPascal Brand 	TEE_MD5_HASH_SIZE = 16,
92b0104773SPascal Brand 	TEE_SHA1_HASH_SIZE = 20,
93b0104773SPascal Brand 	TEE_SHA224_HASH_SIZE = 28,
94b0104773SPascal Brand 	TEE_SHA256_HASH_SIZE = 32,
95b0104773SPascal Brand 	TEE_SHA384_HASH_SIZE = 48,
96b0104773SPascal Brand 	TEE_SHA512_HASH_SIZE = 64,
97b0104773SPascal Brand 	TEE_MD5SHA1_HASH_SIZE = (TEE_MD5_HASH_SIZE + TEE_SHA1_HASH_SIZE),
98b0104773SPascal Brand 	TEE_MAX_HASH_SIZE = 64,
99b0104773SPascal Brand } t_hash_size;
100b0104773SPascal Brand #endif
101b0104773SPascal Brand 
102b0104773SPascal Brand #define TEE_MAC_SIZE_AES_CBC_MAC_NOPAD
103b0104773SPascal Brand #define TEE_MAC_SIZE_AES_CBC_MAC_PKCS5
104b0104773SPascal Brand #define TEE_MAC_SIZE_AES_CMAC
105b0104773SPascal Brand #define TEE_MAC_SIZE_DES_CBC_MAC_PKCS5
106b0104773SPascal Brand 
107b0104773SPascal Brand /*
108b0104773SPascal Brand  * Bit indicating that the attribute is a value attribute
109b0104773SPascal Brand  * See TEE Internal API specificaion v1.0 table 6-12 "Partial Structure of
110b0104773SPascal Brand  * Attribute Identifier"
111b0104773SPascal Brand  */
112b0104773SPascal Brand 
11309d93d24SJens Wiklander 
11409d93d24SJens Wiklander #ifdef __compiler_bswap64
11509d93d24SJens Wiklander #define TEE_U64_BSWAP(x) __compiler_bswap64((x))
11609d93d24SJens Wiklander #else
11709d93d24SJens Wiklander #define TEE_U64_BSWAP(x) ((uint64_t)( \
11809d93d24SJens Wiklander         (((uint64_t)(x) & UINT64_C(0xff00000000000000ULL)) >> 56) | \
11909d93d24SJens Wiklander         (((uint64_t)(x) & UINT64_C(0x00ff000000000000ULL)) >> 40) | \
12009d93d24SJens Wiklander         (((uint64_t)(x) & UINT64_C(0x0000ff0000000000ULL)) >> 24) | \
12109d93d24SJens Wiklander         (((uint64_t)(x) & UINT64_C(0x000000ff00000000ULL)) >>  8) | \
12209d93d24SJens Wiklander         (((uint64_t)(x) & UINT64_C(0x00000000ff000000ULL)) <<  8) | \
12309d93d24SJens Wiklander         (((uint64_t)(x) & UINT64_C(0x0000000000ff0000ULL)) << 24) | \
12409d93d24SJens Wiklander         (((uint64_t)(x) & UINT64_C(0x000000000000ff00ULL)) << 40) | \
12509d93d24SJens Wiklander         (((uint64_t)(x) & UINT64_C(0x00000000000000ffULL)) << 56)))
12609d93d24SJens Wiklander #endif
12709d93d24SJens Wiklander 
12809d93d24SJens Wiklander #ifdef __compiler_bswap32
12909d93d24SJens Wiklander #define TEE_U32_BSWAP(x) __compiler_bswap32((x))
13009d93d24SJens Wiklander #else
13109d93d24SJens Wiklander #define TEE_U32_BSWAP(x) ((uint32_t)( \
13209d93d24SJens Wiklander         (((uint32_t)(x) & UINT32_C(0xff000000)) >> 24) | \
13309d93d24SJens Wiklander         (((uint32_t)(x) & UINT32_C(0x00ff0000)) >>  8) | \
13409d93d24SJens Wiklander         (((uint32_t)(x) & UINT32_C(0x0000ff00)) <<  8) | \
13509d93d24SJens Wiklander         (((uint32_t)(x) & UINT32_C(0x000000ff)) << 24)))
13609d93d24SJens Wiklander #endif
13709d93d24SJens Wiklander 
13809d93d24SJens Wiklander #ifdef __compiler_bswap16
13909d93d24SJens Wiklander #define TEE_U16_BSWAP(x) __compiler_bswap16((x))
14009d93d24SJens Wiklander #else
14109d93d24SJens Wiklander #define TEE_U16_BSWAP(x) ((uint16_t)( \
14209d93d24SJens Wiklander         (((uint16_t)(x) & UINT16_C(0xff00)) >> 8) | \
14309d93d24SJens Wiklander         (((uint16_t)(x) & UINT16_C(0x00ff)) << 8)))
14409d93d24SJens Wiklander #endif
145b0104773SPascal Brand 
146b0104773SPascal Brand /* If we we're on a big endian platform we'll have to update these */
14709d93d24SJens Wiklander #define TEE_U64_FROM_BIG_ENDIAN(x)  TEE_U64_BSWAP(x)
148b0104773SPascal Brand #define TEE_U32_FROM_BIG_ENDIAN(x)  TEE_U32_BSWAP(x)
149b0104773SPascal Brand #define TEE_U16_FROM_BIG_ENDIAN(x)  TEE_U16_BSWAP(x)
15009d93d24SJens Wiklander #define TEE_U64_TO_BIG_ENDIAN(x)    TEE_U64_BSWAP(x)
151b0104773SPascal Brand #define TEE_U32_TO_BIG_ENDIAN(x)    TEE_U32_BSWAP(x)
152b0104773SPascal Brand #define TEE_U16_TO_BIG_ENDIAN(x)    TEE_U16_BSWAP(x)
153b0104773SPascal Brand 
154b0104773SPascal Brand #define TEE_TIME_MILLIS_BASE    1000
155b0104773SPascal Brand 
156b0104773SPascal Brand #define TEE_TIME_LT(t1, t2)				\
157b0104773SPascal Brand     (((t1).seconds == (t2).seconds) ?			\
158b0104773SPascal Brand         ((t1).millis < (t2).millis) :			\
159b0104773SPascal Brand         ((t1).seconds < (t2).seconds))
160b0104773SPascal Brand 
161b0104773SPascal Brand #define TEE_TIME_LE(t1, t2)				\
162b0104773SPascal Brand     (((t1).seconds == (t2).seconds) ?			\
163b0104773SPascal Brand         ((t1).millis <= (t2).millis) :			\
164b0104773SPascal Brand         ((t1).seconds <= (t2).seconds))
165b0104773SPascal Brand 
166b0104773SPascal Brand #define TEE_TIME_ADD(t1, t2, dst) do {                      \
167b0104773SPascal Brand         (dst).seconds = (t1).seconds + (t2).seconds;        \
168b0104773SPascal Brand         (dst).millis = (t1).millis + (t2).millis;           \
169b0104773SPascal Brand         if ((dst).millis >= TEE_TIME_MILLIS_BASE) {         \
170b0104773SPascal Brand             (dst).seconds++;                                \
171b0104773SPascal Brand             (dst).millis -= TEE_TIME_MILLIS_BASE;           \
172b0104773SPascal Brand         }                                                   \
173b0104773SPascal Brand     } while (0)
174b0104773SPascal Brand 
175b0104773SPascal Brand #define TEE_TIME_SUB(t1, t2, dst) do {                      \
176b0104773SPascal Brand         (dst).seconds = (t1).seconds - (t2).seconds;        \
177b0104773SPascal Brand         if ((t1).millis < (t2).millis) {                    \
178b0104773SPascal Brand             (dst).seconds--;                                \
179b0104773SPascal Brand             (dst).millis = (t1).millis + TEE_TIME_MILLIS_BASE - (t2).millis;\
180b0104773SPascal Brand         } else {                                            \
181b0104773SPascal Brand             (dst).millis = (t1).millis - (t2).millis;       \
182b0104773SPascal Brand         }                                                   \
183b0104773SPascal Brand     } while (0)
184b0104773SPascal Brand 
185b0104773SPascal Brand /* ------------------------------------------------------------ */
186b0104773SPascal Brand /* OTP mapping                                                  */
187b0104773SPascal Brand /* ------------------------------------------------------------ */
188b0104773SPascal Brand #define HW_UNIQUE_KEY_WORD1      (8)
189b0104773SPascal Brand #define HW_UNIQUE_KEY_LENGTH     (16)
190b0104773SPascal Brand #define HW_UNIQUE_KEY_WORD2      (HW_UNIQUE_KEY_WORD1 + 1)
191b0104773SPascal Brand #define HW_UNIQUE_KEY_WORD3      (HW_UNIQUE_KEY_WORD1 + 2)
192b0104773SPascal Brand #define HW_UNIQUE_KEY_WORD4      (HW_UNIQUE_KEY_WORD1 + 3)
193b0104773SPascal Brand 
194e86f1266SJens Wiklander #define UTEE_SE_READER_PRESENT			(1 << 0)
195e86f1266SJens Wiklander #define UTEE_SE_READER_TEE_ONLY			(1 << 1)
196e86f1266SJens Wiklander #define UTEE_SE_READER_SELECT_RESPONE_ENABLE	(1 << 2)
197e86f1266SJens Wiklander 
198b0104773SPascal Brand #endif /* UTEE_DEFINES_H */
199