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_HKDF 0xC0 /* OP-TEE extension */ 48 #define TEE_MAIN_ALGO_CONCAT_KDF 0xC1 /* OP-TEE extension */ 49 #define TEE_MAIN_ALGO_PBKDF2 0xC2 /* OP-TEE extension */ 50 51 52 #define TEE_CHAIN_MODE_ECB_NOPAD 0x0 53 #define TEE_CHAIN_MODE_CBC_NOPAD 0x1 54 #define TEE_CHAIN_MODE_CTR 0x2 55 #define TEE_CHAIN_MODE_CTS 0x3 56 #define TEE_CHAIN_MODE_XTS 0x4 57 #define TEE_CHAIN_MODE_CBC_MAC_PKCS5 0x5 58 #define TEE_CHAIN_MODE_CMAC 0x6 59 #define TEE_CHAIN_MODE_CCM 0x7 60 #define TEE_CHAIN_MODE_GCM 0x8 61 #define TEE_CHAIN_MODE_PKCS1_PSS_MGF1 0x9 /* ??? */ 62 63 /* Bits [31:28] */ 64 #define TEE_ALG_GET_CLASS(algo) (((algo) >> 28) & 0xF) 65 66 #define TEE_ALG_GET_KEY_TYPE(algo, with_private_key) \ 67 (TEE_ALG_GET_MAIN_ALG(algo) | \ 68 ((with_private_key) ? 0xA1000000 : 0xA0000000)) 69 70 /* Bits [7:0] */ 71 #define TEE_ALG_GET_MAIN_ALG(algo) ((algo) & 0xFF) 72 73 /* Bits [11:8] */ 74 #define TEE_ALG_GET_CHAIN_MODE(algo) (((algo) >> 8) & 0xF) 75 76 /* Bits [15:12] */ 77 #define TEE_ALG_GET_DIGEST_HASH(algo) (((algo) >> 12) & 0xF) 78 79 /* Bits [23:20] */ 80 #define TEE_ALG_GET_INTERNAL_HASH(algo) (((algo) >> 20) & 0x7) 81 82 /* Return hash algorithm based on main hash */ 83 #define TEE_ALG_HASH_ALGO(main_hash) \ 84 (TEE_OPERATION_DIGEST << 28 | (main_hash)) 85 86 /* Extract internal hash and return hash algorithm */ 87 #define TEE_INTERNAL_HASH_TO_ALGO(algo) \ 88 TEE_ALG_HASH_ALGO(TEE_ALG_GET_INTERNAL_HASH(algo)) 89 90 /* Extract digest hash and return hash algorithm */ 91 #define TEE_DIGEST_HASH_TO_ALGO(algo) \ 92 TEE_ALG_HASH_ALGO(TEE_ALG_GET_DIGEST_HASH(algo)) 93 94 /* Return HMAC algorithm based on main hash */ 95 #define TEE_ALG_HMAC_ALGO(main_hash) \ 96 (TEE_OPERATION_MAC << 28 | (main_hash)) 97 98 #define TEE_AES_BLOCK_SIZE 16UL 99 #define TEE_DES_BLOCK_SIZE 8UL 100 101 #define TEE_AES_MAX_KEY_SIZE 32UL 102 103 /* SHA-512 */ 104 #ifndef TEE_MD5_HASH_SIZE 105 typedef enum { 106 TEE_MD5_HASH_SIZE = 16, 107 TEE_SHA1_HASH_SIZE = 20, 108 TEE_SHA224_HASH_SIZE = 28, 109 TEE_SHA256_HASH_SIZE = 32, 110 TEE_SHA384_HASH_SIZE = 48, 111 TEE_SHA512_HASH_SIZE = 64, 112 TEE_MD5SHA1_HASH_SIZE = (TEE_MD5_HASH_SIZE + TEE_SHA1_HASH_SIZE), 113 TEE_MAX_HASH_SIZE = 64, 114 } t_hash_size; 115 #endif 116 117 #define TEE_MAC_SIZE_AES_CBC_MAC_NOPAD 118 #define TEE_MAC_SIZE_AES_CBC_MAC_PKCS5 119 #define TEE_MAC_SIZE_AES_CMAC 120 #define TEE_MAC_SIZE_DES_CBC_MAC_PKCS5 121 122 /* 123 * Bit indicating that the attribute is a value attribute 124 * See TEE Internal API specificaion v1.0 table 6-12 "Partial Structure of 125 * Attribute Identifier" 126 */ 127 #define TEE_U32_BSWAP(x) ( \ 128 (((x) & 0xff000000) >> 24) | \ 129 (((x) & 0x00ff0000) >> 8) | \ 130 (((x) & 0x0000ff00) << 8) | \ 131 (((x) & 0x000000ff) << 24)) 132 133 #define TEE_U16_BSWAP(x) ( \ 134 (((x) & 0xff00) >> 8) | \ 135 (((x) & 0x00ff) << 8)) 136 137 /* If we we're on a big endian platform we'll have to update these */ 138 #define TEE_U32_FROM_BIG_ENDIAN(x) TEE_U32_BSWAP(x) 139 #define TEE_U16_FROM_BIG_ENDIAN(x) TEE_U16_BSWAP(x) 140 #define TEE_U32_TO_BIG_ENDIAN(x) TEE_U32_BSWAP(x) 141 #define TEE_U16_TO_BIG_ENDIAN(x) TEE_U16_BSWAP(x) 142 143 #ifndef TEE_ALIGNMENT_IS_OK 144 #ifdef CFG_TC_NO_ALIGNOF 145 #define TEE_ALIGNMENT_1B_IS_OK(p, type) (true) 146 #define TEE_ALIGNMENT_2B_IS_OK(p, type) (((((unsigned long)&(p)) & 1) == 0) ? true : false) 147 #define TEE_ALIGNMENT_4B_IS_OK(p, type) (((((unsigned long)&(p)) & 3) == 0) ? true : false) 148 #define TEE_ALIGNMENT_8B_IS_OK(p, type) (((((unsigned long)&(p)) & 7) == 0) ? true : false) 149 #define TEE_ALIGNMENT_IS_OK(p, type) TEE_ALIGNMENT_4B_IS_OK(p, type) 150 #else 151 #define TEE_ALIGNMENT_1B_IS_OK(p, type) TEE_ALIGNMENT_WRAP_IS_OK(p, type) 152 #define TEE_ALIGNMENT_2B_IS_OK(p, type) TEE_ALIGNMENT_WRAP_IS_OK(p, type) 153 #define TEE_ALIGNMENT_4B_IS_OK(p, type) TEE_ALIGNMENT_WRAP_IS_OK(p, type) 154 #define TEE_ALIGNMENT_8B_IS_OK(p, type) TEE_ALIGNMENT_WRAP_IS_OK(p, type) 155 #define TEE_ALIGNMENT_IS_OK(p, type) TEE_ALIGNMENT_WRAP_IS_OK(p, type) 156 157 #define TEE_ALIGNMENT_WRAP_IS_OK(p, type) \ 158 (((uintptr_t)p & (__tee_assert_alignof__(type) - 1)) == 0) 159 160 #define __tee_assert_alignof__(type) __alignof__(type) 161 #endif 162 #endif 163 164 #define TEE_TIME_MILLIS_BASE 1000 165 166 #define TEE_TIME_LT(t1, t2) \ 167 (((t1).seconds == (t2).seconds) ? \ 168 ((t1).millis < (t2).millis) : \ 169 ((t1).seconds < (t2).seconds)) 170 171 #define TEE_TIME_LE(t1, t2) \ 172 (((t1).seconds == (t2).seconds) ? \ 173 ((t1).millis <= (t2).millis) : \ 174 ((t1).seconds <= (t2).seconds)) 175 176 #define TEE_TIME_ADD(t1, t2, dst) do { \ 177 (dst).seconds = (t1).seconds + (t2).seconds; \ 178 (dst).millis = (t1).millis + (t2).millis; \ 179 if ((dst).millis >= TEE_TIME_MILLIS_BASE) { \ 180 (dst).seconds++; \ 181 (dst).millis -= TEE_TIME_MILLIS_BASE; \ 182 } \ 183 } while (0) 184 185 #define TEE_TIME_SUB(t1, t2, dst) do { \ 186 (dst).seconds = (t1).seconds - (t2).seconds; \ 187 if ((t1).millis < (t2).millis) { \ 188 (dst).seconds--; \ 189 (dst).millis = (t1).millis + TEE_TIME_MILLIS_BASE - (t2).millis;\ 190 } else { \ 191 (dst).millis = (t1).millis - (t2).millis; \ 192 } \ 193 } while (0) 194 195 /* ------------------------------------------------------------ */ 196 /* OTP mapping */ 197 /* ------------------------------------------------------------ */ 198 #define HW_UNIQUE_KEY_WORD1 (8) 199 #define HW_UNIQUE_KEY_LENGTH (16) 200 #define HW_UNIQUE_KEY_WORD2 (HW_UNIQUE_KEY_WORD1 + 1) 201 #define HW_UNIQUE_KEY_WORD3 (HW_UNIQUE_KEY_WORD1 + 2) 202 #define HW_UNIQUE_KEY_WORD4 (HW_UNIQUE_KEY_WORD1 + 3) 203 204 #endif /* UTEE_DEFINES_H */ 205