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