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