xref: /optee_os/lib/libutee/include/utee_defines.h (revision 51ac0e23b5c2b3c84469a0de79c9f027a46d5747)
1 /*
2  * Copyright (c) 2014, STMicroelectronics International N.V.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #ifndef UTEE_DEFINES_H
28 #define UTEE_DEFINES_H
29 
30 #include <compiler.h>
31 #include <types_ext.h>
32 
33 /*
34  * Copied from TEE Internal API specificaion v1.0 table 6-9 "Structure of
35  * Algorithm Identifier".
36  */
37 #define TEE_MAIN_ALGO_MD5        0x01
38 #define TEE_MAIN_ALGO_SHA1       0x02
39 #define TEE_MAIN_ALGO_SHA224     0x03
40 #define TEE_MAIN_ALGO_SHA256     0x04
41 #define TEE_MAIN_ALGO_SHA384     0x05
42 #define TEE_MAIN_ALGO_SHA512     0x06
43 #define TEE_MAIN_ALGO_AES        0x10
44 #define TEE_MAIN_ALGO_DES        0x11
45 #define TEE_MAIN_ALGO_DES2       0x12
46 #define TEE_MAIN_ALGO_DES3       0x13
47 #define TEE_MAIN_ALGO_RSA        0x30
48 #define TEE_MAIN_ALGO_DSA        0x31
49 #define TEE_MAIN_ALGO_DH         0x32
50 #define TEE_MAIN_ALGO_ECDSA      0x41
51 #define TEE_MAIN_ALGO_ECDH       0x42
52 #define TEE_MAIN_ALGO_HKDF       0xC0 /* OP-TEE extension */
53 #define TEE_MAIN_ALGO_CONCAT_KDF 0xC1 /* OP-TEE extension */
54 #define TEE_MAIN_ALGO_PBKDF2     0xC2 /* OP-TEE extension */
55 
56 
57 #define TEE_CHAIN_MODE_ECB_NOPAD        0x0
58 #define TEE_CHAIN_MODE_CBC_NOPAD        0x1
59 #define TEE_CHAIN_MODE_CTR              0x2
60 #define TEE_CHAIN_MODE_CTS              0x3
61 #define TEE_CHAIN_MODE_XTS              0x4
62 #define TEE_CHAIN_MODE_CBC_MAC_PKCS5    0x5
63 #define TEE_CHAIN_MODE_CMAC             0x6
64 #define TEE_CHAIN_MODE_CCM              0x7
65 #define TEE_CHAIN_MODE_GCM              0x8
66 #define TEE_CHAIN_MODE_PKCS1_PSS_MGF1   0x9	/* ??? */
67 
68 	/* Bits [31:28] */
69 #define TEE_ALG_GET_CLASS(algo)         (((algo) >> 28) & 0xF)
70 
71 #define TEE_ALG_GET_KEY_TYPE(algo, with_private_key) \
72         (TEE_ALG_GET_MAIN_ALG(algo) | \
73             ((with_private_key) ? 0xA1000000 : 0xA0000000))
74 
75 	/* Bits [7:0] */
76 #define TEE_ALG_GET_MAIN_ALG(algo)      ((algo) & 0xFF)
77 
78 	/* Bits [11:8] */
79 #define TEE_ALG_GET_CHAIN_MODE(algo)    (((algo) >> 8) & 0xF)
80 
81 	/* Bits [15:12] */
82 #define TEE_ALG_GET_DIGEST_HASH(algo)   (((algo) >> 12) & 0xF)
83 
84 	/* Bits [23:20] */
85 #define TEE_ALG_GET_INTERNAL_HASH(algo) (((algo) >> 20) & 0x7)
86 
87 	/* Return hash algorithm based on main hash */
88 #define TEE_ALG_HASH_ALGO(main_hash) \
89         (TEE_OPERATION_DIGEST << 28 | (main_hash))
90 
91 	/* Extract internal hash and return hash algorithm */
92 #define TEE_INTERNAL_HASH_TO_ALGO(algo) \
93                 TEE_ALG_HASH_ALGO(TEE_ALG_GET_INTERNAL_HASH(algo))
94 
95 	/* Extract digest hash and return hash algorithm */
96 #define TEE_DIGEST_HASH_TO_ALGO(algo) \
97                 TEE_ALG_HASH_ALGO(TEE_ALG_GET_DIGEST_HASH(algo))
98 
99 /* Return HMAC algorithm based on main hash */
100 #define TEE_ALG_HMAC_ALGO(main_hash) \
101 	(TEE_OPERATION_MAC << 28 | (main_hash))
102 
103 #define TEE_AES_BLOCK_SIZE  16UL
104 #define TEE_DES_BLOCK_SIZE  8UL
105 
106 #define TEE_AES_MAX_KEY_SIZE    32UL
107 
108 	/* SHA-512 */
109 #ifndef TEE_MD5_HASH_SIZE
110 typedef enum {
111 	TEE_MD5_HASH_SIZE = 16,
112 	TEE_SHA1_HASH_SIZE = 20,
113 	TEE_SHA224_HASH_SIZE = 28,
114 	TEE_SHA256_HASH_SIZE = 32,
115 	TEE_SHA384_HASH_SIZE = 48,
116 	TEE_SHA512_HASH_SIZE = 64,
117 	TEE_MD5SHA1_HASH_SIZE = (TEE_MD5_HASH_SIZE + TEE_SHA1_HASH_SIZE),
118 	TEE_MAX_HASH_SIZE = 64,
119 } t_hash_size;
120 #endif
121 
122 #define TEE_MAC_SIZE_AES_CBC_MAC_NOPAD
123 #define TEE_MAC_SIZE_AES_CBC_MAC_PKCS5
124 #define TEE_MAC_SIZE_AES_CMAC
125 #define TEE_MAC_SIZE_DES_CBC_MAC_PKCS5
126 
127 /*
128  * Bit indicating that the attribute is a value attribute
129  * See TEE Internal API specificaion v1.0 table 6-12 "Partial Structure of
130  * Attribute Identifier"
131  */
132 
133 
134 #ifdef __compiler_bswap64
135 #define TEE_U64_BSWAP(x) __compiler_bswap64((x))
136 #else
137 #define TEE_U64_BSWAP(x) ((uint64_t)( \
138         (((uint64_t)(x) & UINT64_C(0xff00000000000000ULL)) >> 56) | \
139         (((uint64_t)(x) & UINT64_C(0x00ff000000000000ULL)) >> 40) | \
140         (((uint64_t)(x) & UINT64_C(0x0000ff0000000000ULL)) >> 24) | \
141         (((uint64_t)(x) & UINT64_C(0x000000ff00000000ULL)) >>  8) | \
142         (((uint64_t)(x) & UINT64_C(0x00000000ff000000ULL)) <<  8) | \
143         (((uint64_t)(x) & UINT64_C(0x0000000000ff0000ULL)) << 24) | \
144         (((uint64_t)(x) & UINT64_C(0x000000000000ff00ULL)) << 40) | \
145         (((uint64_t)(x) & UINT64_C(0x00000000000000ffULL)) << 56)))
146 #endif
147 
148 #ifdef __compiler_bswap32
149 #define TEE_U32_BSWAP(x) __compiler_bswap32((x))
150 #else
151 #define TEE_U32_BSWAP(x) ((uint32_t)( \
152         (((uint32_t)(x) & UINT32_C(0xff000000)) >> 24) | \
153         (((uint32_t)(x) & UINT32_C(0x00ff0000)) >>  8) | \
154         (((uint32_t)(x) & UINT32_C(0x0000ff00)) <<  8) | \
155         (((uint32_t)(x) & UINT32_C(0x000000ff)) << 24)))
156 #endif
157 
158 #ifdef __compiler_bswap16
159 #define TEE_U16_BSWAP(x) __compiler_bswap16((x))
160 #else
161 #define TEE_U16_BSWAP(x) ((uint16_t)( \
162         (((uint16_t)(x) & UINT16_C(0xff00)) >> 8) | \
163         (((uint16_t)(x) & UINT16_C(0x00ff)) << 8)))
164 #endif
165 
166 /* If we we're on a big endian platform we'll have to update these */
167 #define TEE_U64_FROM_BIG_ENDIAN(x)  TEE_U64_BSWAP(x)
168 #define TEE_U32_FROM_BIG_ENDIAN(x)  TEE_U32_BSWAP(x)
169 #define TEE_U16_FROM_BIG_ENDIAN(x)  TEE_U16_BSWAP(x)
170 #define TEE_U64_TO_BIG_ENDIAN(x)    TEE_U64_BSWAP(x)
171 #define TEE_U32_TO_BIG_ENDIAN(x)    TEE_U32_BSWAP(x)
172 #define TEE_U16_TO_BIG_ENDIAN(x)    TEE_U16_BSWAP(x)
173 
174 #define TEE_TIME_MILLIS_BASE    1000
175 
176 #define TEE_TIME_LT(t1, t2)				\
177     (((t1).seconds == (t2).seconds) ?			\
178         ((t1).millis < (t2).millis) :			\
179         ((t1).seconds < (t2).seconds))
180 
181 #define TEE_TIME_LE(t1, t2)				\
182     (((t1).seconds == (t2).seconds) ?			\
183         ((t1).millis <= (t2).millis) :			\
184         ((t1).seconds <= (t2).seconds))
185 
186 #define TEE_TIME_ADD(t1, t2, dst) do {                      \
187         (dst).seconds = (t1).seconds + (t2).seconds;        \
188         (dst).millis = (t1).millis + (t2).millis;           \
189         if ((dst).millis >= TEE_TIME_MILLIS_BASE) {         \
190             (dst).seconds++;                                \
191             (dst).millis -= TEE_TIME_MILLIS_BASE;           \
192         }                                                   \
193     } while (0)
194 
195 #define TEE_TIME_SUB(t1, t2, dst) do {                      \
196         (dst).seconds = (t1).seconds - (t2).seconds;        \
197         if ((t1).millis < (t2).millis) {                    \
198             (dst).seconds--;                                \
199             (dst).millis = (t1).millis + TEE_TIME_MILLIS_BASE - (t2).millis;\
200         } else {                                            \
201             (dst).millis = (t1).millis - (t2).millis;       \
202         }                                                   \
203     } while (0)
204 
205 /* ------------------------------------------------------------ */
206 /* OTP mapping                                                  */
207 /* ------------------------------------------------------------ */
208 #define HW_UNIQUE_KEY_WORD1      (8)
209 #define HW_UNIQUE_KEY_LENGTH     (16)
210 #define HW_UNIQUE_KEY_WORD2      (HW_UNIQUE_KEY_WORD1 + 1)
211 #define HW_UNIQUE_KEY_WORD3      (HW_UNIQUE_KEY_WORD1 + 2)
212 #define HW_UNIQUE_KEY_WORD4      (HW_UNIQUE_KEY_WORD1 + 3)
213 
214 #define UTEE_SE_READER_PRESENT			(1 << 0)
215 #define UTEE_SE_READER_TEE_ONLY			(1 << 1)
216 #define UTEE_SE_READER_SELECT_RESPONE_ENABLE	(1 << 2)
217 
218 #endif /* UTEE_DEFINES_H */
219