xref: /optee_os/lib/libutee/include/utee_defines.h (revision 43be6453dd3e98d39721c8bc6725416772f4205c)
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 <types_ext.h>
10 
11 /*
12  * Copied from TEE Internal API specificaion v1.0 table 6-9 "Structure of
13  * Algorithm Identifier".
14  */
15 #define TEE_MAIN_ALGO_MD5        0x01
16 #define TEE_MAIN_ALGO_SHA1       0x02
17 #define TEE_MAIN_ALGO_SHA224     0x03
18 #define TEE_MAIN_ALGO_SHA256     0x04
19 #define TEE_MAIN_ALGO_SHA384     0x05
20 #define TEE_MAIN_ALGO_SHA512     0x06
21 #define TEE_MAIN_ALGO_SM3        0x07
22 #define TEE_MAIN_ALGO_AES        0x10
23 #define TEE_MAIN_ALGO_DES        0x11
24 #define TEE_MAIN_ALGO_DES2       0x12
25 #define TEE_MAIN_ALGO_DES3       0x13
26 #define TEE_MAIN_ALGO_SM4        0x14 /* Not in v1.2, extrapolated */
27 #define TEE_MAIN_ALGO_RSA        0x30
28 #define TEE_MAIN_ALGO_DSA        0x31
29 #define TEE_MAIN_ALGO_DH         0x32
30 #define TEE_MAIN_ALGO_ECDSA      0x41
31 #define TEE_MAIN_ALGO_ECDH       0x42
32 #define TEE_MAIN_ALGO_HKDF       0xC0 /* OP-TEE extension */
33 #define TEE_MAIN_ALGO_CONCAT_KDF 0xC1 /* OP-TEE extension */
34 #define TEE_MAIN_ALGO_PBKDF2     0xC2 /* OP-TEE extension */
35 
36 
37 #define TEE_CHAIN_MODE_ECB_NOPAD        0x0
38 #define TEE_CHAIN_MODE_CBC_NOPAD        0x1
39 #define TEE_CHAIN_MODE_CTR              0x2
40 #define TEE_CHAIN_MODE_CTS              0x3
41 #define TEE_CHAIN_MODE_XTS              0x4
42 #define TEE_CHAIN_MODE_CBC_MAC_PKCS5    0x5
43 #define TEE_CHAIN_MODE_CMAC             0x6
44 #define TEE_CHAIN_MODE_CCM              0x7
45 #define TEE_CHAIN_MODE_GCM              0x8
46 #define TEE_CHAIN_MODE_PKCS1_PSS_MGF1   0x9	/* ??? */
47 
48 	/* Bits [31:28] */
49 #define TEE_ALG_GET_CLASS(algo)         (((algo) >> 28) & 0xF)
50 
51 #define TEE_ALG_GET_KEY_TYPE(algo, with_private_key) \
52         (TEE_ALG_GET_MAIN_ALG(algo) | \
53             ((with_private_key) ? 0xA1000000 : 0xA0000000))
54 
55 	/* Bits [7:0] */
56 #define TEE_ALG_GET_MAIN_ALG(algo)      ((algo) & 0xFF)
57 
58 	/* Bits [11:8] */
59 #define TEE_ALG_GET_CHAIN_MODE(algo)    (((algo) >> 8) & 0xF)
60 
61 	/* Bits [15:12] */
62 #define TEE_ALG_GET_DIGEST_HASH(algo)   (((algo) >> 12) & 0xF)
63 
64 	/* Bits [23:20] */
65 #define TEE_ALG_GET_INTERNAL_HASH(algo) (((algo) >> 20) & 0x7)
66 
67 	/* Return hash algorithm based on main hash */
68 #define TEE_ALG_HASH_ALGO(main_hash) \
69         (TEE_OPERATION_DIGEST << 28 | (main_hash))
70 
71 	/* Extract internal hash and return hash algorithm */
72 #define TEE_INTERNAL_HASH_TO_ALGO(algo) \
73                 TEE_ALG_HASH_ALGO(TEE_ALG_GET_INTERNAL_HASH(algo))
74 
75 	/* Extract digest hash and return hash algorithm */
76 #define TEE_DIGEST_HASH_TO_ALGO(algo) \
77                 TEE_ALG_HASH_ALGO(TEE_ALG_GET_DIGEST_HASH(algo))
78 
79 /* Return HMAC algorithm based on main hash */
80 #define TEE_ALG_HMAC_ALGO(main_hash) \
81 	(TEE_OPERATION_MAC << 28 | (main_hash))
82 
83 #define TEE_AES_BLOCK_SIZE  16UL
84 #define TEE_DES_BLOCK_SIZE  8UL
85 #define TEE_SM4_BLOCK_SIZE  16UL
86 
87 #define TEE_AES_MAX_KEY_SIZE    32UL
88 
89 	/* SHA-512 */
90 #ifndef TEE_MD5_HASH_SIZE
91 typedef enum {
92 	TEE_MD5_HASH_SIZE = 16,
93 	TEE_SHA1_HASH_SIZE = 20,
94 	TEE_SHA224_HASH_SIZE = 28,
95 	TEE_SHA256_HASH_SIZE = 32,
96 	TEE_SM3_HASH_SIZE = 32,
97 	TEE_SHA384_HASH_SIZE = 48,
98 	TEE_SHA512_HASH_SIZE = 64,
99 	TEE_MD5SHA1_HASH_SIZE = (TEE_MD5_HASH_SIZE + TEE_SHA1_HASH_SIZE),
100 	TEE_MAX_HASH_SIZE = 64,
101 } t_hash_size;
102 #endif
103 
104 #define TEE_MAC_SIZE_AES_CBC_MAC_NOPAD
105 #define TEE_MAC_SIZE_AES_CBC_MAC_PKCS5
106 #define TEE_MAC_SIZE_AES_CMAC
107 #define TEE_MAC_SIZE_DES_CBC_MAC_PKCS5
108 
109 /*
110  * Bit indicating that the attribute is a value attribute
111  * See TEE Internal API specificaion v1.0 table 6-12 "Partial Structure of
112  * Attribute Identifier"
113  */
114 
115 
116 #ifdef __compiler_bswap64
117 #define TEE_U64_BSWAP(x) __compiler_bswap64((x))
118 #else
119 #define TEE_U64_BSWAP(x) ((uint64_t)( \
120         (((uint64_t)(x) & UINT64_C(0xff00000000000000ULL)) >> 56) | \
121         (((uint64_t)(x) & UINT64_C(0x00ff000000000000ULL)) >> 40) | \
122         (((uint64_t)(x) & UINT64_C(0x0000ff0000000000ULL)) >> 24) | \
123         (((uint64_t)(x) & UINT64_C(0x000000ff00000000ULL)) >>  8) | \
124         (((uint64_t)(x) & UINT64_C(0x00000000ff000000ULL)) <<  8) | \
125         (((uint64_t)(x) & UINT64_C(0x0000000000ff0000ULL)) << 24) | \
126         (((uint64_t)(x) & UINT64_C(0x000000000000ff00ULL)) << 40) | \
127         (((uint64_t)(x) & UINT64_C(0x00000000000000ffULL)) << 56)))
128 #endif
129 
130 #ifdef __compiler_bswap32
131 #define TEE_U32_BSWAP(x) __compiler_bswap32((x))
132 #else
133 #define TEE_U32_BSWAP(x) ((uint32_t)( \
134         (((uint32_t)(x) & UINT32_C(0xff000000)) >> 24) | \
135         (((uint32_t)(x) & UINT32_C(0x00ff0000)) >>  8) | \
136         (((uint32_t)(x) & UINT32_C(0x0000ff00)) <<  8) | \
137         (((uint32_t)(x) & UINT32_C(0x000000ff)) << 24)))
138 #endif
139 
140 #ifdef __compiler_bswap16
141 #define TEE_U16_BSWAP(x) __compiler_bswap16((x))
142 #else
143 #define TEE_U16_BSWAP(x) ((uint16_t)( \
144         (((uint16_t)(x) & UINT16_C(0xff00)) >> 8) | \
145         (((uint16_t)(x) & UINT16_C(0x00ff)) << 8)))
146 #endif
147 
148 /* If we we're on a big endian platform we'll have to update these */
149 #define TEE_U64_FROM_BIG_ENDIAN(x)  TEE_U64_BSWAP(x)
150 #define TEE_U32_FROM_BIG_ENDIAN(x)  TEE_U32_BSWAP(x)
151 #define TEE_U16_FROM_BIG_ENDIAN(x)  TEE_U16_BSWAP(x)
152 #define TEE_U64_TO_BIG_ENDIAN(x)    TEE_U64_BSWAP(x)
153 #define TEE_U32_TO_BIG_ENDIAN(x)    TEE_U32_BSWAP(x)
154 #define TEE_U16_TO_BIG_ENDIAN(x)    TEE_U16_BSWAP(x)
155 
156 #define TEE_TIME_MILLIS_BASE    1000
157 
158 #define TEE_TIME_LT(t1, t2)				\
159     (((t1).seconds == (t2).seconds) ?			\
160         ((t1).millis < (t2).millis) :			\
161         ((t1).seconds < (t2).seconds))
162 
163 #define TEE_TIME_LE(t1, t2)				\
164     (((t1).seconds == (t2).seconds) ?			\
165         ((t1).millis <= (t2).millis) :			\
166         ((t1).seconds <= (t2).seconds))
167 
168 #define TEE_TIME_ADD(t1, t2, dst) do {                      \
169         (dst).seconds = (t1).seconds + (t2).seconds;        \
170         (dst).millis = (t1).millis + (t2).millis;           \
171         if ((dst).millis >= TEE_TIME_MILLIS_BASE) {         \
172             (dst).seconds++;                                \
173             (dst).millis -= TEE_TIME_MILLIS_BASE;           \
174         }                                                   \
175     } while (0)
176 
177 #define TEE_TIME_SUB(t1, t2, dst) do {                      \
178         (dst).seconds = (t1).seconds - (t2).seconds;        \
179         if ((t1).millis < (t2).millis) {                    \
180             (dst).seconds--;                                \
181             (dst).millis = (t1).millis + TEE_TIME_MILLIS_BASE - (t2).millis;\
182         } else {                                            \
183             (dst).millis = (t1).millis - (t2).millis;       \
184         }                                                   \
185     } while (0)
186 
187 /* ------------------------------------------------------------ */
188 /* OTP mapping                                                  */
189 /* ------------------------------------------------------------ */
190 #define HW_UNIQUE_KEY_WORD1      (8)
191 #define HW_UNIQUE_KEY_LENGTH     (16)
192 #define HW_UNIQUE_KEY_WORD2      (HW_UNIQUE_KEY_WORD1 + 1)
193 #define HW_UNIQUE_KEY_WORD3      (HW_UNIQUE_KEY_WORD1 + 2)
194 #define HW_UNIQUE_KEY_WORD4      (HW_UNIQUE_KEY_WORD1 + 3)
195 
196 #define UTEE_SE_READER_PRESENT			(1 << 0)
197 #define UTEE_SE_READER_TEE_ONLY			(1 << 1)
198 #define UTEE_SE_READER_SELECT_RESPONE_ENABLE	(1 << 2)
199 
200 #endif /* UTEE_DEFINES_H */
201