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