11bb92983SJerome Forissier /* SPDX-License-Identifier: BSD-2-Clause */ 2b0104773SPascal Brand /* 3b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 4b0104773SPascal Brand */ 5b0104773SPascal Brand #ifndef UTEE_DEFINES_H 6b0104773SPascal Brand #define UTEE_DEFINES_H 7b0104773SPascal Brand 809d93d24SJens Wiklander #include <compiler.h> 991fc6bd8SJerome Forissier #include <tee_api_defines.h> 1009d93d24SJens Wiklander #include <types_ext.h> 1109d93d24SJens Wiklander 12b0104773SPascal Brand /* 13b0104773SPascal Brand * Copied from TEE Internal API specificaion v1.0 table 6-9 "Structure of 14b0104773SPascal Brand * Algorithm Identifier". 15b0104773SPascal Brand */ 16b0104773SPascal Brand #define TEE_MAIN_ALGO_MD5 0x01 17b0104773SPascal Brand #define TEE_MAIN_ALGO_SHA1 0x02 18b0104773SPascal Brand #define TEE_MAIN_ALGO_SHA224 0x03 19b0104773SPascal Brand #define TEE_MAIN_ALGO_SHA256 0x04 20b0104773SPascal Brand #define TEE_MAIN_ALGO_SHA384 0x05 21b0104773SPascal Brand #define TEE_MAIN_ALGO_SHA512 0x06 2247645577SJerome Forissier #define TEE_MAIN_ALGO_SM3 0x07 23b0104773SPascal Brand #define TEE_MAIN_ALGO_AES 0x10 24b0104773SPascal Brand #define TEE_MAIN_ALGO_DES 0x11 25b0104773SPascal Brand #define TEE_MAIN_ALGO_DES2 0x12 26b0104773SPascal Brand #define TEE_MAIN_ALGO_DES3 0x13 27ade6f848SJerome Forissier #define TEE_MAIN_ALGO_SM4 0x14 /* Not in v1.2, extrapolated */ 28b0104773SPascal Brand #define TEE_MAIN_ALGO_RSA 0x30 29b0104773SPascal Brand #define TEE_MAIN_ALGO_DSA 0x31 30b0104773SPascal Brand #define TEE_MAIN_ALGO_DH 0x32 31c988227aSPascal Brand #define TEE_MAIN_ALGO_ECDSA 0x41 32c988227aSPascal Brand #define TEE_MAIN_ALGO_ECDH 0x42 33*0f151943SJerome Forissier #define TEE_MAIN_ALGO_SM2_DSA_SM3 0x45 /* Not in v1.2 spec */ 3491fc6bd8SJerome Forissier #define TEE_MAIN_ALGO_SM2_PKE 0x47 /* Not in v1.2 spec */ 35cdb198a7SJerome Forissier #define TEE_MAIN_ALGO_HKDF 0xC0 /* OP-TEE extension */ 368854d3c6SJerome Forissier #define TEE_MAIN_ALGO_CONCAT_KDF 0xC1 /* OP-TEE extension */ 370f2293b7SJerome Forissier #define TEE_MAIN_ALGO_PBKDF2 0xC2 /* OP-TEE extension */ 388854d3c6SJerome Forissier 39b0104773SPascal Brand 40b0104773SPascal Brand #define TEE_CHAIN_MODE_ECB_NOPAD 0x0 41b0104773SPascal Brand #define TEE_CHAIN_MODE_CBC_NOPAD 0x1 42b0104773SPascal Brand #define TEE_CHAIN_MODE_CTR 0x2 43b0104773SPascal Brand #define TEE_CHAIN_MODE_CTS 0x3 44b0104773SPascal Brand #define TEE_CHAIN_MODE_XTS 0x4 45b0104773SPascal Brand #define TEE_CHAIN_MODE_CBC_MAC_PKCS5 0x5 46b0104773SPascal Brand #define TEE_CHAIN_MODE_CMAC 0x6 47b0104773SPascal Brand #define TEE_CHAIN_MODE_CCM 0x7 48b0104773SPascal Brand #define TEE_CHAIN_MODE_GCM 0x8 49b0104773SPascal Brand #define TEE_CHAIN_MODE_PKCS1_PSS_MGF1 0x9 /* ??? */ 50b0104773SPascal Brand 51b0104773SPascal Brand 5291fc6bd8SJerome Forissier static inline uint32_t __tee_alg_get_class(uint32_t algo) 5391fc6bd8SJerome Forissier { 5491fc6bd8SJerome Forissier if (algo == TEE_ALG_SM2_PKE) 5591fc6bd8SJerome Forissier return TEE_OPERATION_ASYMMETRIC_CIPHER; 56b0104773SPascal Brand 5791fc6bd8SJerome Forissier return (algo >> 28) & 0xF; /* Bits [31:28] */ 5891fc6bd8SJerome Forissier } 5991fc6bd8SJerome Forissier 6091fc6bd8SJerome Forissier #define TEE_ALG_GET_CLASS(algo) __tee_alg_get_class(algo) 6191fc6bd8SJerome Forissier 6291fc6bd8SJerome Forissier static inline uint32_t __tee_alg_get_main_alg(uint32_t algo) 6391fc6bd8SJerome Forissier { 6491fc6bd8SJerome Forissier switch (algo) { 6591fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 6691fc6bd8SJerome Forissier return TEE_MAIN_ALGO_SM2_PKE; 6791fc6bd8SJerome Forissier default: 6891fc6bd8SJerome Forissier break; 6991fc6bd8SJerome Forissier } 7091fc6bd8SJerome Forissier 7191fc6bd8SJerome Forissier return algo & 0xff; 7291fc6bd8SJerome Forissier } 7391fc6bd8SJerome Forissier 7491fc6bd8SJerome Forissier #define TEE_ALG_GET_MAIN_ALG(algo) __tee_alg_get_main_alg(algo) 75b0104773SPascal Brand 76b0104773SPascal Brand /* Bits [11:8] */ 77b0104773SPascal Brand #define TEE_ALG_GET_CHAIN_MODE(algo) (((algo) >> 8) & 0xF) 78b0104773SPascal Brand 79*0f151943SJerome Forissier /* 80*0f151943SJerome Forissier * Value not defined in the GP spec, and not used as bits 15-12 of any TEE_ALG* 81*0f151943SJerome Forissier * value. TEE_ALG_SM2_DSA_SM3 has value 0x6 for bits 15-12 which would yield the 82*0f151943SJerome Forissier * SHA512 digest if we were to apply the bit masks that were valid up to the TEE 83*0f151943SJerome Forissier * Internal Core API v1.1. 84*0f151943SJerome Forissier */ 85*0f151943SJerome Forissier #define __TEE_MAIN_HASH_SM3 0x7 86*0f151943SJerome Forissier 87*0f151943SJerome Forissier static inline uint32_t __tee_alg_get_digest_hash(uint32_t algo) 88*0f151943SJerome Forissier { 89*0f151943SJerome Forissier if (algo == TEE_ALG_SM2_DSA_SM3) 90*0f151943SJerome Forissier return __TEE_MAIN_HASH_SM3; 91*0f151943SJerome Forissier 92b0104773SPascal Brand /* Bits [15:12] */ 93*0f151943SJerome Forissier return (algo >> 12) & 0xF; 94*0f151943SJerome Forissier } 95*0f151943SJerome Forissier 96*0f151943SJerome Forissier #define TEE_ALG_GET_DIGEST_HASH(algo) __tee_alg_get_digest_hash(algo) 97b0104773SPascal Brand 98b0104773SPascal Brand /* Bits [23:20] */ 99b0104773SPascal Brand #define TEE_ALG_GET_INTERNAL_HASH(algo) (((algo) >> 20) & 0x7) 100b0104773SPascal Brand 10191fc6bd8SJerome Forissier static inline uint32_t __tee_alg_get_key_type(uint32_t algo, bool with_priv) 10291fc6bd8SJerome Forissier { 10391fc6bd8SJerome Forissier uint32_t key_type = 0xA0000000 | TEE_ALG_GET_MAIN_ALG(algo); 10491fc6bd8SJerome Forissier 10591fc6bd8SJerome Forissier if (with_priv) 10691fc6bd8SJerome Forissier key_type |= 0x01000000; 10791fc6bd8SJerome Forissier 10891fc6bd8SJerome Forissier return key_type; 10991fc6bd8SJerome Forissier } 11091fc6bd8SJerome Forissier 11191fc6bd8SJerome Forissier #define TEE_ALG_GET_KEY_TYPE(algo, with_private_key) \ 11291fc6bd8SJerome Forissier __tee_alg_get_key_type(algo, with_private_key) 11391fc6bd8SJerome Forissier 114*0f151943SJerome Forissier static inline uint32_t __tee_alg_hash_algo(uint32_t main_hash) 115*0f151943SJerome Forissier { 116*0f151943SJerome Forissier if (main_hash == __TEE_MAIN_HASH_SM3) 117*0f151943SJerome Forissier return TEE_ALG_SM3; 118*0f151943SJerome Forissier 119*0f151943SJerome Forissier return (TEE_OPERATION_DIGEST << 28) | main_hash; 120*0f151943SJerome Forissier } 121*0f151943SJerome Forissier 122b0104773SPascal Brand /* Return hash algorithm based on main hash */ 123*0f151943SJerome Forissier #define TEE_ALG_HASH_ALGO(main_hash) __tee_alg_hash_algo(main_hash) 124b0104773SPascal Brand 125b0104773SPascal Brand /* Extract internal hash and return hash algorithm */ 126b0104773SPascal Brand #define TEE_INTERNAL_HASH_TO_ALGO(algo) \ 127b0104773SPascal Brand TEE_ALG_HASH_ALGO(TEE_ALG_GET_INTERNAL_HASH(algo)) 128b0104773SPascal Brand 129b0104773SPascal Brand /* Extract digest hash and return hash algorithm */ 130b0104773SPascal Brand #define TEE_DIGEST_HASH_TO_ALGO(algo) \ 131b0104773SPascal Brand TEE_ALG_HASH_ALGO(TEE_ALG_GET_DIGEST_HASH(algo)) 132b0104773SPascal Brand 133cdb198a7SJerome Forissier /* Return HMAC algorithm based on main hash */ 134cdb198a7SJerome Forissier #define TEE_ALG_HMAC_ALGO(main_hash) \ 135cdb198a7SJerome Forissier (TEE_OPERATION_MAC << 28 | (main_hash)) 136cdb198a7SJerome Forissier 137b0104773SPascal Brand #define TEE_AES_BLOCK_SIZE 16UL 138b0104773SPascal Brand #define TEE_DES_BLOCK_SIZE 8UL 139ade6f848SJerome Forissier #define TEE_SM4_BLOCK_SIZE 16UL 140b0104773SPascal Brand 141b0104773SPascal Brand #define TEE_AES_MAX_KEY_SIZE 32UL 142b0104773SPascal Brand 143b0104773SPascal Brand /* SHA-512 */ 144b0104773SPascal Brand #ifndef TEE_MD5_HASH_SIZE 145b0104773SPascal Brand typedef enum { 146b0104773SPascal Brand TEE_MD5_HASH_SIZE = 16, 147b0104773SPascal Brand TEE_SHA1_HASH_SIZE = 20, 148b0104773SPascal Brand TEE_SHA224_HASH_SIZE = 28, 149b0104773SPascal Brand TEE_SHA256_HASH_SIZE = 32, 15047645577SJerome Forissier TEE_SM3_HASH_SIZE = 32, 151b0104773SPascal Brand TEE_SHA384_HASH_SIZE = 48, 152b0104773SPascal Brand TEE_SHA512_HASH_SIZE = 64, 153b0104773SPascal Brand TEE_MD5SHA1_HASH_SIZE = (TEE_MD5_HASH_SIZE + TEE_SHA1_HASH_SIZE), 154b0104773SPascal Brand TEE_MAX_HASH_SIZE = 64, 155b0104773SPascal Brand } t_hash_size; 156b0104773SPascal Brand #endif 157b0104773SPascal Brand 158b0104773SPascal Brand #define TEE_MAC_SIZE_AES_CBC_MAC_NOPAD 159b0104773SPascal Brand #define TEE_MAC_SIZE_AES_CBC_MAC_PKCS5 160b0104773SPascal Brand #define TEE_MAC_SIZE_AES_CMAC 161b0104773SPascal Brand #define TEE_MAC_SIZE_DES_CBC_MAC_PKCS5 162b0104773SPascal Brand 163b0104773SPascal Brand /* 164b0104773SPascal Brand * Bit indicating that the attribute is a value attribute 165b0104773SPascal Brand * See TEE Internal API specificaion v1.0 table 6-12 "Partial Structure of 166b0104773SPascal Brand * Attribute Identifier" 167b0104773SPascal Brand */ 168b0104773SPascal Brand 16909d93d24SJens Wiklander 17009d93d24SJens Wiklander #ifdef __compiler_bswap64 17109d93d24SJens Wiklander #define TEE_U64_BSWAP(x) __compiler_bswap64((x)) 17209d93d24SJens Wiklander #else 17309d93d24SJens Wiklander #define TEE_U64_BSWAP(x) ((uint64_t)( \ 17409d93d24SJens Wiklander (((uint64_t)(x) & UINT64_C(0xff00000000000000ULL)) >> 56) | \ 17509d93d24SJens Wiklander (((uint64_t)(x) & UINT64_C(0x00ff000000000000ULL)) >> 40) | \ 17609d93d24SJens Wiklander (((uint64_t)(x) & UINT64_C(0x0000ff0000000000ULL)) >> 24) | \ 17709d93d24SJens Wiklander (((uint64_t)(x) & UINT64_C(0x000000ff00000000ULL)) >> 8) | \ 17809d93d24SJens Wiklander (((uint64_t)(x) & UINT64_C(0x00000000ff000000ULL)) << 8) | \ 17909d93d24SJens Wiklander (((uint64_t)(x) & UINT64_C(0x0000000000ff0000ULL)) << 24) | \ 18009d93d24SJens Wiklander (((uint64_t)(x) & UINT64_C(0x000000000000ff00ULL)) << 40) | \ 18109d93d24SJens Wiklander (((uint64_t)(x) & UINT64_C(0x00000000000000ffULL)) << 56))) 18209d93d24SJens Wiklander #endif 18309d93d24SJens Wiklander 18409d93d24SJens Wiklander #ifdef __compiler_bswap32 18509d93d24SJens Wiklander #define TEE_U32_BSWAP(x) __compiler_bswap32((x)) 18609d93d24SJens Wiklander #else 18709d93d24SJens Wiklander #define TEE_U32_BSWAP(x) ((uint32_t)( \ 18809d93d24SJens Wiklander (((uint32_t)(x) & UINT32_C(0xff000000)) >> 24) | \ 18909d93d24SJens Wiklander (((uint32_t)(x) & UINT32_C(0x00ff0000)) >> 8) | \ 19009d93d24SJens Wiklander (((uint32_t)(x) & UINT32_C(0x0000ff00)) << 8) | \ 19109d93d24SJens Wiklander (((uint32_t)(x) & UINT32_C(0x000000ff)) << 24))) 19209d93d24SJens Wiklander #endif 19309d93d24SJens Wiklander 19409d93d24SJens Wiklander #ifdef __compiler_bswap16 19509d93d24SJens Wiklander #define TEE_U16_BSWAP(x) __compiler_bswap16((x)) 19609d93d24SJens Wiklander #else 19709d93d24SJens Wiklander #define TEE_U16_BSWAP(x) ((uint16_t)( \ 19809d93d24SJens Wiklander (((uint16_t)(x) & UINT16_C(0xff00)) >> 8) | \ 19909d93d24SJens Wiklander (((uint16_t)(x) & UINT16_C(0x00ff)) << 8))) 20009d93d24SJens Wiklander #endif 201b0104773SPascal Brand 202b0104773SPascal Brand /* If we we're on a big endian platform we'll have to update these */ 20309d93d24SJens Wiklander #define TEE_U64_FROM_BIG_ENDIAN(x) TEE_U64_BSWAP(x) 204b0104773SPascal Brand #define TEE_U32_FROM_BIG_ENDIAN(x) TEE_U32_BSWAP(x) 205b0104773SPascal Brand #define TEE_U16_FROM_BIG_ENDIAN(x) TEE_U16_BSWAP(x) 20609d93d24SJens Wiklander #define TEE_U64_TO_BIG_ENDIAN(x) TEE_U64_BSWAP(x) 207b0104773SPascal Brand #define TEE_U32_TO_BIG_ENDIAN(x) TEE_U32_BSWAP(x) 208b0104773SPascal Brand #define TEE_U16_TO_BIG_ENDIAN(x) TEE_U16_BSWAP(x) 209b0104773SPascal Brand 210b0104773SPascal Brand #define TEE_TIME_MILLIS_BASE 1000 211b0104773SPascal Brand 212b0104773SPascal Brand #define TEE_TIME_LT(t1, t2) \ 213b0104773SPascal Brand (((t1).seconds == (t2).seconds) ? \ 214b0104773SPascal Brand ((t1).millis < (t2).millis) : \ 215b0104773SPascal Brand ((t1).seconds < (t2).seconds)) 216b0104773SPascal Brand 217b0104773SPascal Brand #define TEE_TIME_LE(t1, t2) \ 218b0104773SPascal Brand (((t1).seconds == (t2).seconds) ? \ 219b0104773SPascal Brand ((t1).millis <= (t2).millis) : \ 220b0104773SPascal Brand ((t1).seconds <= (t2).seconds)) 221b0104773SPascal Brand 222b0104773SPascal Brand #define TEE_TIME_ADD(t1, t2, dst) do { \ 223b0104773SPascal Brand (dst).seconds = (t1).seconds + (t2).seconds; \ 224b0104773SPascal Brand (dst).millis = (t1).millis + (t2).millis; \ 225b0104773SPascal Brand if ((dst).millis >= TEE_TIME_MILLIS_BASE) { \ 226b0104773SPascal Brand (dst).seconds++; \ 227b0104773SPascal Brand (dst).millis -= TEE_TIME_MILLIS_BASE; \ 228b0104773SPascal Brand } \ 229b0104773SPascal Brand } while (0) 230b0104773SPascal Brand 231b0104773SPascal Brand #define TEE_TIME_SUB(t1, t2, dst) do { \ 232b0104773SPascal Brand (dst).seconds = (t1).seconds - (t2).seconds; \ 233b0104773SPascal Brand if ((t1).millis < (t2).millis) { \ 234b0104773SPascal Brand (dst).seconds--; \ 235b0104773SPascal Brand (dst).millis = (t1).millis + TEE_TIME_MILLIS_BASE - (t2).millis;\ 236b0104773SPascal Brand } else { \ 237b0104773SPascal Brand (dst).millis = (t1).millis - (t2).millis; \ 238b0104773SPascal Brand } \ 239b0104773SPascal Brand } while (0) 240b0104773SPascal Brand 241b0104773SPascal Brand /* ------------------------------------------------------------ */ 242b0104773SPascal Brand /* OTP mapping */ 243b0104773SPascal Brand /* ------------------------------------------------------------ */ 244b0104773SPascal Brand #define HW_UNIQUE_KEY_WORD1 (8) 245b0104773SPascal Brand #define HW_UNIQUE_KEY_LENGTH (16) 246b0104773SPascal Brand #define HW_UNIQUE_KEY_WORD2 (HW_UNIQUE_KEY_WORD1 + 1) 247b0104773SPascal Brand #define HW_UNIQUE_KEY_WORD3 (HW_UNIQUE_KEY_WORD1 + 2) 248b0104773SPascal Brand #define HW_UNIQUE_KEY_WORD4 (HW_UNIQUE_KEY_WORD1 + 3) 249b0104773SPascal Brand 250e86f1266SJens Wiklander #define UTEE_SE_READER_PRESENT (1 << 0) 251e86f1266SJens Wiklander #define UTEE_SE_READER_TEE_ONLY (1 << 1) 252e86f1266SJens Wiklander #define UTEE_SE_READER_SELECT_RESPONE_ENABLE (1 << 2) 253e86f1266SJens Wiklander 254b0104773SPascal Brand #endif /* UTEE_DEFINES_H */ 255