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