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