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_DSA_SM3 0x45 /* Not in v1.2 spec */ 34 #define TEE_MAIN_ALGO_SM2_KEP 0x46 /* Not in v1.2 spec */ 35 #define TEE_MAIN_ALGO_SM2_PKE 0x47 /* Not in v1.2 spec */ 36 #define TEE_MAIN_ALGO_HKDF 0xC0 /* OP-TEE extension */ 37 #define TEE_MAIN_ALGO_CONCAT_KDF 0xC1 /* OP-TEE extension */ 38 #define TEE_MAIN_ALGO_PBKDF2 0xC2 /* OP-TEE extension */ 39 40 41 #define TEE_CHAIN_MODE_ECB_NOPAD 0x0 42 #define TEE_CHAIN_MODE_CBC_NOPAD 0x1 43 #define TEE_CHAIN_MODE_CTR 0x2 44 #define TEE_CHAIN_MODE_CTS 0x3 45 #define TEE_CHAIN_MODE_XTS 0x4 46 #define TEE_CHAIN_MODE_CBC_MAC_PKCS5 0x5 47 #define TEE_CHAIN_MODE_CMAC 0x6 48 #define TEE_CHAIN_MODE_CCM 0x7 49 #define TEE_CHAIN_MODE_GCM 0x8 50 #define TEE_CHAIN_MODE_PKCS1_PSS_MGF1 0x9 /* ??? */ 51 52 53 static inline uint32_t __tee_alg_get_class(uint32_t algo) 54 { 55 if (algo == TEE_ALG_SM2_PKE) 56 return TEE_OPERATION_ASYMMETRIC_CIPHER; 57 if (algo == TEE_ALG_SM2_KEP) 58 return TEE_OPERATION_KEY_DERIVATION; 59 60 return (algo >> 28) & 0xF; /* Bits [31:28] */ 61 } 62 63 #define TEE_ALG_GET_CLASS(algo) __tee_alg_get_class(algo) 64 65 static inline uint32_t __tee_alg_get_main_alg(uint32_t algo) 66 { 67 switch (algo) { 68 case TEE_ALG_SM2_PKE: 69 return TEE_MAIN_ALGO_SM2_PKE; 70 case TEE_ALG_SM2_KEP: 71 return TEE_MAIN_ALGO_SM2_KEP; 72 default: 73 break; 74 } 75 76 return algo & 0xff; 77 } 78 79 #define TEE_ALG_GET_MAIN_ALG(algo) __tee_alg_get_main_alg(algo) 80 81 /* Bits [11:8] */ 82 #define TEE_ALG_GET_CHAIN_MODE(algo) (((algo) >> 8) & 0xF) 83 84 /* 85 * Value not defined in the GP spec, and not used as bits 15-12 of any TEE_ALG* 86 * value. TEE_ALG_SM2_DSA_SM3 has value 0x6 for bits 15-12 which would yield the 87 * SHA512 digest if we were to apply the bit masks that were valid up to the TEE 88 * Internal Core API v1.1. 89 */ 90 #define __TEE_MAIN_HASH_SM3 0x7 91 92 static inline uint32_t __tee_alg_get_digest_hash(uint32_t algo) 93 { 94 if (algo == TEE_ALG_SM2_DSA_SM3) 95 return __TEE_MAIN_HASH_SM3; 96 97 /* Bits [15:12] */ 98 return (algo >> 12) & 0xF; 99 } 100 101 #define TEE_ALG_GET_DIGEST_HASH(algo) __tee_alg_get_digest_hash(algo) 102 103 /* Bits [23:20] */ 104 #define TEE_ALG_GET_INTERNAL_HASH(algo) (((algo) >> 20) & 0x7) 105 106 static inline uint32_t __tee_alg_get_key_type(uint32_t algo, bool with_priv) 107 { 108 uint32_t key_type = 0xA0000000 | TEE_ALG_GET_MAIN_ALG(algo); 109 110 if (with_priv) 111 key_type |= 0x01000000; 112 113 return key_type; 114 } 115 116 #define TEE_ALG_GET_KEY_TYPE(algo, with_private_key) \ 117 __tee_alg_get_key_type(algo, with_private_key) 118 119 static inline uint32_t __tee_alg_hash_algo(uint32_t main_hash) 120 { 121 if (main_hash == __TEE_MAIN_HASH_SM3) 122 return TEE_ALG_SM3; 123 124 return (TEE_OPERATION_DIGEST << 28) | main_hash; 125 } 126 127 /* Return hash algorithm based on main hash */ 128 #define TEE_ALG_HASH_ALGO(main_hash) __tee_alg_hash_algo(main_hash) 129 130 /* Extract internal hash and return hash algorithm */ 131 #define TEE_INTERNAL_HASH_TO_ALGO(algo) \ 132 TEE_ALG_HASH_ALGO(TEE_ALG_GET_INTERNAL_HASH(algo)) 133 134 /* Extract digest hash and return hash algorithm */ 135 #define TEE_DIGEST_HASH_TO_ALGO(algo) \ 136 TEE_ALG_HASH_ALGO(TEE_ALG_GET_DIGEST_HASH(algo)) 137 138 /* Return HMAC algorithm based on main hash */ 139 #define TEE_ALG_HMAC_ALGO(main_hash) \ 140 (TEE_OPERATION_MAC << 28 | (main_hash)) 141 142 #define TEE_AES_BLOCK_SIZE 16UL 143 #define TEE_DES_BLOCK_SIZE 8UL 144 #define TEE_SM4_BLOCK_SIZE 16UL 145 146 #define TEE_AES_MAX_KEY_SIZE 32UL 147 148 /* SHA-512 */ 149 #ifndef TEE_MD5_HASH_SIZE 150 typedef enum { 151 TEE_MD5_HASH_SIZE = 16, 152 TEE_SHA1_HASH_SIZE = 20, 153 TEE_SHA224_HASH_SIZE = 28, 154 TEE_SHA256_HASH_SIZE = 32, 155 TEE_SM3_HASH_SIZE = 32, 156 TEE_SHA384_HASH_SIZE = 48, 157 TEE_SHA512_HASH_SIZE = 64, 158 TEE_MD5SHA1_HASH_SIZE = (TEE_MD5_HASH_SIZE + TEE_SHA1_HASH_SIZE), 159 TEE_MAX_HASH_SIZE = 64, 160 } t_hash_size; 161 #endif 162 163 #define TEE_MAC_SIZE_AES_CBC_MAC_NOPAD 164 #define TEE_MAC_SIZE_AES_CBC_MAC_PKCS5 165 #define TEE_MAC_SIZE_AES_CMAC 166 #define TEE_MAC_SIZE_DES_CBC_MAC_PKCS5 167 168 /* 169 * Bit indicating that the attribute is a value attribute 170 * See TEE Internal API specificaion v1.0 table 6-12 "Partial Structure of 171 * Attribute Identifier" 172 */ 173 174 175 #ifdef __compiler_bswap64 176 #define TEE_U64_BSWAP(x) __compiler_bswap64((x)) 177 #else 178 #define TEE_U64_BSWAP(x) ((uint64_t)( \ 179 (((uint64_t)(x) & UINT64_C(0xff00000000000000ULL)) >> 56) | \ 180 (((uint64_t)(x) & UINT64_C(0x00ff000000000000ULL)) >> 40) | \ 181 (((uint64_t)(x) & UINT64_C(0x0000ff0000000000ULL)) >> 24) | \ 182 (((uint64_t)(x) & UINT64_C(0x000000ff00000000ULL)) >> 8) | \ 183 (((uint64_t)(x) & UINT64_C(0x00000000ff000000ULL)) << 8) | \ 184 (((uint64_t)(x) & UINT64_C(0x0000000000ff0000ULL)) << 24) | \ 185 (((uint64_t)(x) & UINT64_C(0x000000000000ff00ULL)) << 40) | \ 186 (((uint64_t)(x) & UINT64_C(0x00000000000000ffULL)) << 56))) 187 #endif 188 189 #ifdef __compiler_bswap32 190 #define TEE_U32_BSWAP(x) __compiler_bswap32((x)) 191 #else 192 #define TEE_U32_BSWAP(x) ((uint32_t)( \ 193 (((uint32_t)(x) & UINT32_C(0xff000000)) >> 24) | \ 194 (((uint32_t)(x) & UINT32_C(0x00ff0000)) >> 8) | \ 195 (((uint32_t)(x) & UINT32_C(0x0000ff00)) << 8) | \ 196 (((uint32_t)(x) & UINT32_C(0x000000ff)) << 24))) 197 #endif 198 199 #ifdef __compiler_bswap16 200 #define TEE_U16_BSWAP(x) __compiler_bswap16((x)) 201 #else 202 #define TEE_U16_BSWAP(x) ((uint16_t)( \ 203 (((uint16_t)(x) & UINT16_C(0xff00)) >> 8) | \ 204 (((uint16_t)(x) & UINT16_C(0x00ff)) << 8))) 205 #endif 206 207 /* If we we're on a big endian platform we'll have to update these */ 208 #define TEE_U64_FROM_BIG_ENDIAN(x) TEE_U64_BSWAP(x) 209 #define TEE_U32_FROM_BIG_ENDIAN(x) TEE_U32_BSWAP(x) 210 #define TEE_U16_FROM_BIG_ENDIAN(x) TEE_U16_BSWAP(x) 211 #define TEE_U64_TO_BIG_ENDIAN(x) TEE_U64_BSWAP(x) 212 #define TEE_U32_TO_BIG_ENDIAN(x) TEE_U32_BSWAP(x) 213 #define TEE_U16_TO_BIG_ENDIAN(x) TEE_U16_BSWAP(x) 214 215 #define TEE_TIME_MILLIS_BASE 1000 216 217 #define TEE_TIME_LT(t1, t2) \ 218 (((t1).seconds == (t2).seconds) ? \ 219 ((t1).millis < (t2).millis) : \ 220 ((t1).seconds < (t2).seconds)) 221 222 #define TEE_TIME_LE(t1, t2) \ 223 (((t1).seconds == (t2).seconds) ? \ 224 ((t1).millis <= (t2).millis) : \ 225 ((t1).seconds <= (t2).seconds)) 226 227 #define TEE_TIME_ADD(t1, t2, dst) do { \ 228 (dst).seconds = (t1).seconds + (t2).seconds; \ 229 (dst).millis = (t1).millis + (t2).millis; \ 230 if ((dst).millis >= TEE_TIME_MILLIS_BASE) { \ 231 (dst).seconds++; \ 232 (dst).millis -= TEE_TIME_MILLIS_BASE; \ 233 } \ 234 } while (0) 235 236 #define TEE_TIME_SUB(t1, t2, dst) do { \ 237 (dst).seconds = (t1).seconds - (t2).seconds; \ 238 if ((t1).millis < (t2).millis) { \ 239 (dst).seconds--; \ 240 (dst).millis = (t1).millis + TEE_TIME_MILLIS_BASE - (t2).millis;\ 241 } else { \ 242 (dst).millis = (t1).millis - (t2).millis; \ 243 } \ 244 } while (0) 245 246 /* ------------------------------------------------------------ */ 247 /* OTP mapping */ 248 /* ------------------------------------------------------------ */ 249 #define HW_UNIQUE_KEY_WORD1 (8) 250 #define HW_UNIQUE_KEY_LENGTH (16) 251 #define HW_UNIQUE_KEY_WORD2 (HW_UNIQUE_KEY_WORD1 + 1) 252 #define HW_UNIQUE_KEY_WORD3 (HW_UNIQUE_KEY_WORD1 + 2) 253 #define HW_UNIQUE_KEY_WORD4 (HW_UNIQUE_KEY_WORD1 + 3) 254 255 #define UTEE_SE_READER_PRESENT (1 << 0) 256 #define UTEE_SE_READER_TEE_ONLY (1 << 1) 257 #define UTEE_SE_READER_SELECT_RESPONE_ENABLE (1 << 2) 258 259 #endif /* UTEE_DEFINES_H */ 260