xref: /optee_os/ta/pkcs11/src/token_capabilities.c (revision 2570cd0b8d773a181df1ddf8db266aa39a96518e)
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 	MECHANISM(PKCS11_CKM_GENERIC_SECRET_KEY_GEN, PKCS11_CKFM_GENERATE,
82 		  ANY_PART),
83 	/* HMAC */
84 	MECHANISM(PKCS11_CKM_MD5_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
85 	MECHANISM(PKCS11_CKM_SHA_1_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
86 	MECHANISM(PKCS11_CKM_SHA224_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
87 	MECHANISM(PKCS11_CKM_SHA256_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
88 	MECHANISM(PKCS11_CKM_SHA384_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
89 	MECHANISM(PKCS11_CKM_SHA512_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
90 };
91 
92 #if CFG_TEE_TA_LOG_LEVEL > 0
93 const char *mechanism_string_id(enum pkcs11_mechanism_id id)
94 {
95 	const size_t offset = sizeof("PKCS11_CKM_") - 1;
96 	size_t n = 0;
97 
98 	for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++)
99 		if (pkcs11_modes[n].id == id)
100 			return pkcs11_modes[n].string + offset;
101 
102 	return "Unknown ID";
103 }
104 #endif /*CFG_TEE_TA_LOG_LEVEL*/
105 
106 /*
107  * Return true if @id is a valid mechanism ID
108  */
109 bool mechanism_is_valid(enum pkcs11_mechanism_id id)
110 {
111 	size_t n = 0;
112 
113 	for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++)
114 		if (id == pkcs11_modes[n].id)
115 			return true;
116 
117 	return false;
118 }
119 
120 /*
121  * Return true if mechanism ID is valid and flags matches PKCS#11 compliancy
122  */
123 bool __maybe_unused mechanism_flags_complies_pkcs11(uint32_t mechanism_type,
124 						    uint32_t flags)
125 {
126 	size_t n = 0;
127 
128 	assert((flags & ~ALLOWED_PKCS11_CKFM) == 0);
129 
130 	for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++) {
131 		if (pkcs11_modes[n].id == mechanism_type) {
132 			if (flags & ~pkcs11_modes[n].flags)
133 				EMSG("%s flags: 0x%"PRIx32" vs 0x%"PRIx32,
134 				     id2str_mechanism(mechanism_type),
135 				     flags, pkcs11_modes[n].flags);
136 
137 			return (flags & ~pkcs11_modes[n].flags) == 0;
138 		}
139 	}
140 
141 	/* Mechanism ID unexpectedly not found */
142 	return false;
143 }
144 
145 bool mechanism_is_one_shot_only(uint32_t mechanism_type)
146 {
147 	size_t n = 0;
148 
149 	for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++)
150 		if (pkcs11_modes[n].id == mechanism_type)
151 			return pkcs11_modes[n].one_shot;
152 
153 	/* Mechanism ID unexpectedly not found */
154 	TEE_Panic(PKCS11_RV_NOT_FOUND);
155 	/* Dummy return to keep compiler happy */
156 	return false;
157 }
158 
159 /*
160  * Field single_part_only is unused from array token_mechanism[], hence
161  * simply use ANY_PART for all mechanism there.
162  */
163 #define TA_MECHANISM(_label, _flags)	MECHANISM((_label), (_flags), ANY_PART)
164 
165 /*
166  * Arrays that centralizes the IDs and processing flags for mechanisms
167  * supported by each embedded token. Currently none.
168  */
169 const struct pkcs11_mechachism_modes token_mechanism[] = {
170 	TA_MECHANISM(PKCS11_CKM_AES_ECB, CKFM_CIPHER),
171 	TA_MECHANISM(PKCS11_CKM_AES_CBC, CKFM_CIPHER),
172 	TA_MECHANISM(PKCS11_CKM_AES_CBC_PAD, CKFM_CIPHER),
173 	TA_MECHANISM(PKCS11_CKM_AES_CTR, CKFM_CIPHER),
174 	TA_MECHANISM(PKCS11_CKM_AES_CTS, CKFM_CIPHER),
175 	TA_MECHANISM(PKCS11_CKM_AES_ECB_ENCRYPT_DATA, PKCS11_CKFM_DERIVE),
176 	TA_MECHANISM(PKCS11_CKM_AES_CBC_ENCRYPT_DATA, PKCS11_CKFM_DERIVE),
177 	TA_MECHANISM(PKCS11_CKM_AES_KEY_GEN, PKCS11_CKFM_GENERATE),
178 	TA_MECHANISM(PKCS11_CKM_GENERIC_SECRET_KEY_GEN, PKCS11_CKFM_GENERATE),
179 	TA_MECHANISM(PKCS11_CKM_MD5_HMAC, CKFM_AUTH_NO_RECOVER),
180 	TA_MECHANISM(PKCS11_CKM_SHA_1_HMAC, CKFM_AUTH_NO_RECOVER),
181 	TA_MECHANISM(PKCS11_CKM_SHA224_HMAC, CKFM_AUTH_NO_RECOVER),
182 	TA_MECHANISM(PKCS11_CKM_SHA256_HMAC, CKFM_AUTH_NO_RECOVER),
183 	TA_MECHANISM(PKCS11_CKM_SHA384_HMAC, CKFM_AUTH_NO_RECOVER),
184 	TA_MECHANISM(PKCS11_CKM_SHA512_HMAC, CKFM_AUTH_NO_RECOVER),
185 };
186 
187 /*
188  * tee_malloc_mechanism_array - Allocate and fill array of supported mechanisms
189  * @count: [in] [out] Pointer to number of mechanism IDs in client resource
190  * Return allocated array of the supported mechanism IDs
191  *
192  * Allocates array with 32bit cells mechanism IDs for the supported ones only
193  * if *@count covers number mechanism IDs exposed.
194  */
195 uint32_t *tee_malloc_mechanism_list(size_t *out_count)
196 {
197 	size_t n = 0;
198 	size_t count = 0;
199 	uint32_t *array = NULL;
200 
201 	for (n = 0; n < ARRAY_SIZE(token_mechanism); n++)
202 		if (token_mechanism[n].flags)
203 			count++;
204 
205 	if (*out_count >= count)
206 		array = TEE_Malloc(count * sizeof(*array),
207 				   TEE_USER_MEM_HINT_NO_FILL_ZERO);
208 
209 	*out_count = count;
210 
211 	if (!array)
212 		return NULL;
213 
214 	for (n = 0; n < ARRAY_SIZE(token_mechanism); n++) {
215 		if (token_mechanism[n].flags) {
216 			count--;
217 			array[count] = token_mechanism[n].id;
218 		}
219 	}
220 	assert(!count);
221 
222 	return array;
223 }
224 
225 uint32_t mechanism_supported_flags(enum pkcs11_mechanism_id id)
226 {
227 	size_t n = 0;
228 
229 	for (n = 0; n < ARRAY_SIZE(token_mechanism); n++) {
230 		if (id == token_mechanism[n].id) {
231 			uint32_t flags = token_mechanism[n].flags;
232 
233 			assert(mechanism_flags_complies_pkcs11(id, flags));
234 			return flags;
235 		}
236 	}
237 
238 	return 0;
239 }
240 
241 void mechanism_supported_key_sizes(uint32_t proc_id, uint32_t *min_key_size,
242 				   uint32_t *max_key_size)
243 {
244 	switch (proc_id) {
245 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
246 		/* This mechanism expects the keysize to be returned in bits */
247 		*min_key_size = 1;		/* in bits */
248 		*max_key_size = 4096;		/* in bits */
249 		break;
250 	case PKCS11_CKM_MD5_HMAC:
251 		*min_key_size = 8;
252 		*max_key_size = 64;
253 		break;
254 	case PKCS11_CKM_SHA_1_HMAC:
255 		*min_key_size = 10;
256 		*max_key_size = 64;
257 		break;
258 	case PKCS11_CKM_SHA224_HMAC:
259 		*min_key_size = 14;
260 		*max_key_size = 64;
261 		break;
262 	case PKCS11_CKM_SHA256_HMAC:
263 		*min_key_size = 24;
264 		*max_key_size = 128;
265 		break;
266 	case PKCS11_CKM_SHA384_HMAC:
267 		*min_key_size = 32;
268 		*max_key_size = 128;
269 		break;
270 	case PKCS11_CKM_SHA512_HMAC:
271 		*min_key_size = 32;
272 		*max_key_size = 128;
273 		break;
274 	case PKCS11_CKM_AES_KEY_GEN:
275 	case PKCS11_CKM_AES_ECB:
276 	case PKCS11_CKM_AES_CBC:
277 	case PKCS11_CKM_AES_CBC_PAD:
278 	case PKCS11_CKM_AES_CTR:
279 	case PKCS11_CKM_AES_CTS:
280 		*min_key_size = 16;
281 		*max_key_size = 32;
282 		break;
283 	default:
284 		*min_key_size = 0;
285 		*max_key_size = 0;
286 		break;
287 	}
288 }
289