xref: /optee_os/lib/libutee/include/utee_defines.h (revision 03c42787bbb0d574300fdc79ef82316837aef3a3)
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 /*
31  * Copied from TEE Internal API specificaion v1.0 table 6-9 "Structure of
32  * Algorithm Identifier".
33  */
34 #define TEE_MAIN_ALGO_MD5       0x01
35 #define TEE_MAIN_ALGO_SHA1      0x02
36 #define TEE_MAIN_ALGO_SHA224    0x03
37 #define TEE_MAIN_ALGO_SHA256    0x04
38 #define TEE_MAIN_ALGO_SHA384    0x05
39 #define TEE_MAIN_ALGO_SHA512    0x06
40 #define TEE_MAIN_ALGO_AES       0x10
41 #define TEE_MAIN_ALGO_DES       0x11
42 #define TEE_MAIN_ALGO_DES2      0x12
43 #define TEE_MAIN_ALGO_DES3      0x13
44 #define TEE_MAIN_ALGO_RSA       0x30
45 #define TEE_MAIN_ALGO_DSA       0x31
46 #define TEE_MAIN_ALGO_DH        0x32
47 
48 #define TEE_CHAIN_MODE_ECB_NOPAD        0x0
49 #define TEE_CHAIN_MODE_CBC_NOPAD        0x1
50 #define TEE_CHAIN_MODE_CTR              0x2
51 #define TEE_CHAIN_MODE_CTS              0x3
52 #define TEE_CHAIN_MODE_XTS              0x4
53 #define TEE_CHAIN_MODE_CBC_MAC_PKCS5    0x5
54 #define TEE_CHAIN_MODE_CMAC             0x6
55 #define TEE_CHAIN_MODE_CCM              0x7
56 #define TEE_CHAIN_MODE_GCM              0x8
57 #define TEE_CHAIN_MODE_PKCS1_PSS_MGF1   0x9	/* ??? */
58 
59 	/* Bits [31:28] */
60 #define TEE_ALG_GET_CLASS(algo)         (((algo) >> 28) & 0xF)
61 
62 #define TEE_ALG_GET_KEY_TYPE(algo, with_private_key) \
63         (TEE_ALG_GET_MAIN_ALG(algo) | \
64             ((with_private_key) ? 0xA1000000 : 0xA0000000))
65 
66 	/* Bits [7:0] */
67 #define TEE_ALG_GET_MAIN_ALG(algo)      ((algo) & 0xFF)
68 
69 	/* Bits [11:8] */
70 #define TEE_ALG_GET_CHAIN_MODE(algo)    (((algo) >> 8) & 0xF)
71 
72 	/* Bits [15:12] */
73 #define TEE_ALG_GET_DIGEST_HASH(algo)   (((algo) >> 12) & 0xF)
74 
75 	/* Bits [23:20] */
76 #define TEE_ALG_GET_INTERNAL_HASH(algo) (((algo) >> 20) & 0x7)
77 
78 	/* Return hash algorithm based on main hash */
79 #define TEE_ALG_HASH_ALGO(main_hash) \
80         (TEE_OPERATION_DIGEST << 28 | (main_hash))
81 
82 	/* Extract internal hash and return hash algorithm */
83 #define TEE_INTERNAL_HASH_TO_ALGO(algo) \
84                 TEE_ALG_HASH_ALGO(TEE_ALG_GET_INTERNAL_HASH(algo))
85 
86 	/* Extract digest hash and return hash algorithm */
87 #define TEE_DIGEST_HASH_TO_ALGO(algo) \
88                 TEE_ALG_HASH_ALGO(TEE_ALG_GET_DIGEST_HASH(algo))
89 
90 #define TEE_AES_BLOCK_SIZE  16UL
91 #define TEE_DES_BLOCK_SIZE  8UL
92 
93 #define TEE_AES_MAX_KEY_SIZE    32UL
94 
95 	/* SHA-512 */
96 #ifndef TEE_MD5_HASH_SIZE
97 typedef enum {
98 	TEE_MD5_HASH_SIZE = 16,
99 	TEE_SHA1_HASH_SIZE = 20,
100 	TEE_SHA224_HASH_SIZE = 28,
101 	TEE_SHA256_HASH_SIZE = 32,
102 	TEE_SHA384_HASH_SIZE = 48,
103 	TEE_SHA512_HASH_SIZE = 64,
104 	TEE_MD5SHA1_HASH_SIZE = (TEE_MD5_HASH_SIZE + TEE_SHA1_HASH_SIZE),
105 	TEE_MAX_HASH_SIZE = 64,
106 } t_hash_size;
107 #endif
108 
109 #define TEE_MAC_SIZE_AES_CBC_MAC_NOPAD
110 #define TEE_MAC_SIZE_AES_CBC_MAC_PKCS5
111 #define TEE_MAC_SIZE_AES_CMAC
112 #define TEE_MAC_SIZE_DES_CBC_MAC_PKCS5
113 
114 /*
115  * Bit indicating that the attribute is a value attribute
116  * See TEE Internal API specificaion v1.0 table 6-12 "Partial Structure of
117  * Attribute Identifier"
118  */
119 #define TEE_ATTR_BIT_VALUE                  (1 << 29)
120 
121 #ifndef MAX
122 #define MAX(a, b) \
123        __extension__({ __typeof__(a) _a = (a); \
124            __typeof__(b) _b = (b); \
125          _a > _b ? _a : _b; })
126 
127 #define MIN(a, b) \
128        __extension__({ __typeof__(a) _a = (a); \
129            __typeof__(b) _b = (b); \
130          _a < _b ? _a : _b; })
131 #endif
132 
133 /* Round up the even multiple of size, size has to be a multiple of 2 */
134 #define ROUNDUP(v, size) (((v) + (size - 1)) & ~(size - 1))
135 
136 /* Round down the even multiple of size, size has to be a multiple of 2 */
137 #define ROUNDDOWN(v, size) ((v) & ~(size - 1))
138 
139 #define TEE_U32_BSWAP(x) ( \
140         (((x) & 0xff000000) >> 24) | \
141         (((x) & 0x00ff0000) >>  8) | \
142         (((x) & 0x0000ff00) <<  8) | \
143         (((x) & 0x000000ff) << 24))
144 
145 #define TEE_U16_BSWAP(x) ( \
146         (((x) & 0xff00) >> 8) | \
147         (((x) & 0x00ff) << 8))
148 
149 /* If we we're on a big endian platform we'll have to update these */
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_U32_TO_BIG_ENDIAN(x)    TEE_U32_BSWAP(x)
153 #define TEE_U16_TO_BIG_ENDIAN(x)    TEE_U16_BSWAP(x)
154 
155 #ifndef TEE_ALIGNMENT_IS_OK
156 #ifdef CFG_TC_NO_ALIGNOF
157 #define TEE_ALIGNMENT_1B_IS_OK(p, type)    (true)
158 #define TEE_ALIGNMENT_2B_IS_OK(p, type)    (((((unsigned long)&(p)) & 1) == 0) ? true : false)
159 #define TEE_ALIGNMENT_4B_IS_OK(p, type)    (((((unsigned long)&(p)) & 3) == 0) ? true : false)
160 #define TEE_ALIGNMENT_8B_IS_OK(p, type)    (((((unsigned long)&(p)) & 7) == 0) ? true : false)
161 #define TEE_ALIGNMENT_IS_OK(p, type)       TEE_ALIGNMENT_4B_IS_OK(p, type)
162 #else
163 #define TEE_ALIGNMENT_1B_IS_OK(p, type)    TEE_ALIGNMENT_WRAP_IS_OK(p, type)
164 #define TEE_ALIGNMENT_2B_IS_OK(p, type)    TEE_ALIGNMENT_WRAP_IS_OK(p, type)
165 #define TEE_ALIGNMENT_4B_IS_OK(p, type)    TEE_ALIGNMENT_WRAP_IS_OK(p, type)
166 #define TEE_ALIGNMENT_8B_IS_OK(p, type)    TEE_ALIGNMENT_WRAP_IS_OK(p, type)
167 #define TEE_ALIGNMENT_IS_OK(p, type)       TEE_ALIGNMENT_WRAP_IS_OK(p, type)
168 
169 #define TEE_ALIGNMENT_WRAP_IS_OK(p, type)                        \
170         (((uintptr_t)p & (__tee_assert_alignof__(type) - 1)) == 0)
171 
172 #define __tee_assert_alignof__(type) __alignof__(type)
173 #endif
174 #endif
175 
176 #define TEE_TIME_MILLIS_BASE    1000
177 
178 #define TEE_TIME_LT(t1, t2)				\
179     (((t1).seconds == (t2).seconds) ?			\
180         ((t1).millis < (t2).millis) :			\
181         ((t1).seconds < (t2).seconds))
182 
183 #define TEE_TIME_LE(t1, t2)				\
184     (((t1).seconds == (t2).seconds) ?			\
185         ((t1).millis <= (t2).millis) :			\
186         ((t1).seconds <= (t2).seconds))
187 
188 #define TEE_TIME_ADD(t1, t2, dst) do {                      \
189         (dst).seconds = (t1).seconds + (t2).seconds;        \
190         (dst).millis = (t1).millis + (t2).millis;           \
191         if ((dst).millis >= TEE_TIME_MILLIS_BASE) {         \
192             (dst).seconds++;                                \
193             (dst).millis -= TEE_TIME_MILLIS_BASE;           \
194         }                                                   \
195     } while (0)
196 
197 #define TEE_TIME_SUB(t1, t2, dst) do {                      \
198         (dst).seconds = (t1).seconds - (t2).seconds;        \
199         if ((t1).millis < (t2).millis) {                    \
200             (dst).seconds--;                                \
201             (dst).millis = (t1).millis + TEE_TIME_MILLIS_BASE - (t2).millis;\
202         } else {                                            \
203             (dst).millis = (t1).millis - (t2).millis;       \
204         }                                                   \
205     } while (0)
206 
207 /* ------------------------------------------------------------ */
208 /* OTP mapping                                                  */
209 /* ------------------------------------------------------------ */
210 #define HW_UNIQUE_KEY_WORD1      (8)
211 #define HW_UNIQUE_KEY_LENGTH     (16)
212 #define HW_UNIQUE_KEY_WORD2      (HW_UNIQUE_KEY_WORD1 + 1)
213 #define HW_UNIQUE_KEY_WORD3      (HW_UNIQUE_KEY_WORD1 + 2)
214 #define HW_UNIQUE_KEY_WORD4      (HW_UNIQUE_KEY_WORD1 + 3)
215 
216 #endif /* UTEE_DEFINES_H */
217