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