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