1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2017-2020, Linaro Limited 4 */ 5 6 #include <assert.h> 7 #include <pkcs11_ta.h> 8 #include <string.h> 9 #include <util.h> 10 #include <tee_api.h> 11 #include <tee_internal_api_extensions.h> 12 13 #include "pkcs11_helpers.h" 14 #include "token_capabilities.h" 15 16 #define ALLOWED_PKCS11_CKFM \ 17 (PKCS11_CKFM_ENCRYPT | PKCS11_CKFM_DECRYPT | \ 18 PKCS11_CKFM_DERIVE | PKCS11_CKFM_DIGEST | \ 19 PKCS11_CKFM_SIGN | PKCS11_CKFM_SIGN_RECOVER | \ 20 PKCS11_CKFM_VERIFY | PKCS11_CKFM_VERIFY_RECOVER | \ 21 PKCS11_CKFM_GENERATE | PKCS11_CKFM_GENERATE_KEY_PAIR | \ 22 PKCS11_CKFM_WRAP | PKCS11_CKFM_UNWRAP) 23 24 /* 25 * Definition of supported processings for a PKCS#11 mechanisms 26 * @id: Mechanism ID 27 * @flags: Valid PKCS11_CKFM_* for a mechanism as per PKCS#11 28 * @one_shot: true of mechanism can be used for a one-short processing 29 * @string: Helper string of the mechanism ID for debug purpose 30 */ 31 struct pkcs11_mechachism_modes { 32 uint32_t id; 33 uint32_t flags; 34 bool one_shot; 35 #if CFG_TEE_TA_LOG_LEVEL > 0 36 const char *string; 37 #endif 38 }; 39 40 #if CFG_TEE_TA_LOG_LEVEL > 0 41 #define MECHANISM(_label, _flags, _single_part) \ 42 { \ 43 .id = _label, \ 44 .one_shot = (_single_part), \ 45 .flags = (_flags), \ 46 .string = #_label, \ 47 } 48 #else 49 #define MECHANISM(_label, _flags, _single_part) \ 50 { \ 51 .id = _label, \ 52 .one_shot = (_single_part), \ 53 .flags = (_flags), \ 54 } 55 #endif 56 57 #define SINGLE_PART_ONLY true 58 #define ANY_PART false 59 60 #define CKFM_CIPHER (PKCS11_CKFM_ENCRYPT | PKCS11_CKFM_DECRYPT) 61 #define CKFM_WRAP_UNWRAP (PKCS11_CKFM_WRAP | PKCS11_CKFM_UNWRAP) 62 #define CKFM_CIPHER_WRAP (CKFM_CIPHER | CKFM_WRAP_UNWRAP) 63 #define CKFM_CIPHER_WRAP_DERIVE (CKFM_CIPHER_WRAP | PKCS11_CKFM_DERIVE) 64 #define CKFM_AUTH_NO_RECOVER (PKCS11_CKFM_SIGN | PKCS11_CKFM_VERIFY) 65 #define CKFM_AUTH_WITH_RECOVER (PKCS11_CKFM_SIGN_RECOVER | \ 66 PKCS11_CKFM_VERIFY_RECOVER) 67 68 /* PKCS#11 specificies permitted operation for each mechanism */ 69 static const struct pkcs11_mechachism_modes pkcs11_modes[] = { 70 /* AES */ 71 MECHANISM(PKCS11_CKM_AES_ECB, CKFM_CIPHER_WRAP, ANY_PART), 72 MECHANISM(PKCS11_CKM_AES_CBC, CKFM_CIPHER_WRAP, ANY_PART), 73 MECHANISM(PKCS11_CKM_AES_CBC_PAD, CKFM_CIPHER_WRAP, ANY_PART), 74 MECHANISM(PKCS11_CKM_AES_CTS, CKFM_CIPHER_WRAP, ANY_PART), 75 MECHANISM(PKCS11_CKM_AES_CTR, CKFM_CIPHER_WRAP, ANY_PART), 76 MECHANISM(PKCS11_CKM_AES_ECB_ENCRYPT_DATA, PKCS11_CKFM_DERIVE, 77 ANY_PART), 78 MECHANISM(PKCS11_CKM_AES_CBC_ENCRYPT_DATA, PKCS11_CKFM_DERIVE, 79 ANY_PART), 80 MECHANISM(PKCS11_CKM_AES_KEY_GEN, PKCS11_CKFM_GENERATE, ANY_PART), 81 }; 82 83 #if CFG_TEE_TA_LOG_LEVEL > 0 84 const char *mechanism_string_id(enum pkcs11_mechanism_id id) 85 { 86 const size_t offset = sizeof("PKCS11_CKM_") - 1; 87 size_t n = 0; 88 89 for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++) 90 if (pkcs11_modes[n].id == id) 91 return pkcs11_modes[n].string + offset; 92 93 return "Unknown ID"; 94 } 95 #endif /*CFG_TEE_TA_LOG_LEVEL*/ 96 97 /* 98 * Return true if @id is a valid mechanism ID 99 */ 100 bool mechanism_is_valid(enum pkcs11_mechanism_id id) 101 { 102 size_t n = 0; 103 104 for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++) 105 if (id == pkcs11_modes[n].id) 106 return true; 107 108 return false; 109 } 110 111 /* 112 * Return true if mechanism ID is valid and flags matches PKCS#11 compliancy 113 */ 114 bool __maybe_unused mechanism_flags_complies_pkcs11(uint32_t mechanism_type, 115 uint32_t flags) 116 { 117 size_t n = 0; 118 119 assert((flags & ~ALLOWED_PKCS11_CKFM) == 0); 120 121 for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++) { 122 if (pkcs11_modes[n].id == mechanism_type) { 123 if (flags & ~pkcs11_modes[n].flags) 124 EMSG("%s flags: 0x%"PRIx32" vs 0x%"PRIx32, 125 id2str_mechanism(mechanism_type), 126 flags, pkcs11_modes[n].flags); 127 128 return (flags & ~pkcs11_modes[n].flags) == 0; 129 } 130 } 131 132 /* Mechanism ID unexpectedly not found */ 133 return false; 134 } 135 136 bool mechanism_is_one_shot_only(uint32_t mechanism_type) 137 { 138 size_t n = 0; 139 140 for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++) 141 if (pkcs11_modes[n].id == mechanism_type) 142 return pkcs11_modes[n].one_shot; 143 144 /* Mechanism ID unexpectedly not found */ 145 TEE_Panic(PKCS11_RV_NOT_FOUND); 146 /* Dummy return to keep compiler happy */ 147 return false; 148 } 149 150 /* 151 * Field single_part_only is unused from array token_mechanism[], hence 152 * simply use ANY_PART for all mechanism there. 153 */ 154 #define TA_MECHANISM(_label, _flags) MECHANISM((_label), (_flags), ANY_PART) 155 156 /* 157 * Arrays that centralizes the IDs and processing flags for mechanisms 158 * supported by each embedded token. Currently none. 159 */ 160 const struct pkcs11_mechachism_modes token_mechanism[] = { 161 TA_MECHANISM(PKCS11_CKM_AES_ECB, CKFM_CIPHER), 162 TA_MECHANISM(PKCS11_CKM_AES_CBC, CKFM_CIPHER), 163 TA_MECHANISM(PKCS11_CKM_AES_CBC_PAD, CKFM_CIPHER), 164 TA_MECHANISM(PKCS11_CKM_AES_CTR, CKFM_CIPHER), 165 TA_MECHANISM(PKCS11_CKM_AES_CTS, CKFM_CIPHER), 166 TA_MECHANISM(PKCS11_CKM_AES_ECB_ENCRYPT_DATA, PKCS11_CKFM_DERIVE), 167 TA_MECHANISM(PKCS11_CKM_AES_CBC_ENCRYPT_DATA, PKCS11_CKFM_DERIVE), 168 }; 169 170 /* 171 * tee_malloc_mechanism_array - Allocate and fill array of supported mechanisms 172 * @count: [in] [out] Pointer to number of mechanism IDs in client resource 173 * Return allocated array of the supported mechanism IDs 174 * 175 * Allocates array with 32bit cells mechanism IDs for the supported ones only 176 * if *@count covers number mechanism IDs exposed. 177 */ 178 uint32_t *tee_malloc_mechanism_list(size_t *out_count) 179 { 180 size_t n = 0; 181 size_t count = 0; 182 uint32_t *array = NULL; 183 184 for (n = 0; n < ARRAY_SIZE(token_mechanism); n++) 185 if (token_mechanism[n].flags) 186 count++; 187 188 if (*out_count >= count) 189 array = TEE_Malloc(count * sizeof(*array), 190 TEE_USER_MEM_HINT_NO_FILL_ZERO); 191 192 *out_count = count; 193 194 if (!array) 195 return NULL; 196 197 for (n = 0; n < ARRAY_SIZE(token_mechanism); n++) { 198 if (token_mechanism[n].flags) { 199 count--; 200 array[count] = token_mechanism[n].id; 201 } 202 } 203 assert(!count); 204 205 return array; 206 } 207 208 uint32_t mechanism_supported_flags(enum pkcs11_mechanism_id id) 209 { 210 size_t n = 0; 211 212 for (n = 0; n < ARRAY_SIZE(token_mechanism); n++) { 213 if (id == token_mechanism[n].id) { 214 uint32_t flags = token_mechanism[n].flags; 215 216 assert(mechanism_flags_complies_pkcs11(id, flags)); 217 return flags; 218 } 219 } 220 221 return 0; 222 } 223 224 void mechanism_supported_key_sizes(uint32_t proc_id, uint32_t *min_key_size, 225 uint32_t *max_key_size) 226 { 227 switch (proc_id) { 228 case PKCS11_CKM_AES_KEY_GEN: 229 case PKCS11_CKM_AES_ECB: 230 case PKCS11_CKM_AES_CBC: 231 case PKCS11_CKM_AES_CBC_PAD: 232 case PKCS11_CKM_AES_CTR: 233 case PKCS11_CKM_AES_CTS: 234 *min_key_size = 16; 235 *max_key_size = 32; 236 break; 237 default: 238 *min_key_size = 0; 239 *max_key_size = 0; 240 break; 241 } 242 } 243