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_CMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
77 	MECHANISM(PKCS11_CKM_AES_CMAC_GENERAL, CKFM_AUTH_NO_RECOVER, ANY_PART),
78 	MECHANISM(PKCS11_CKM_AES_ECB_ENCRYPT_DATA, PKCS11_CKFM_DERIVE,
79 		  ANY_PART),
80 	MECHANISM(PKCS11_CKM_AES_CBC_ENCRYPT_DATA, PKCS11_CKFM_DERIVE,
81 		  ANY_PART),
82 	MECHANISM(PKCS11_CKM_AES_KEY_GEN, PKCS11_CKFM_GENERATE, ANY_PART),
83 	MECHANISM(PKCS11_CKM_GENERIC_SECRET_KEY_GEN, PKCS11_CKFM_GENERATE,
84 		  ANY_PART),
85 	/* Digest */
86 	MECHANISM(PKCS11_CKM_MD5, PKCS11_CKFM_DIGEST, ANY_PART),
87 	MECHANISM(PKCS11_CKM_SHA_1, PKCS11_CKFM_DIGEST, ANY_PART),
88 	MECHANISM(PKCS11_CKM_SHA224, PKCS11_CKFM_DIGEST, ANY_PART),
89 	MECHANISM(PKCS11_CKM_SHA256, PKCS11_CKFM_DIGEST, ANY_PART),
90 	MECHANISM(PKCS11_CKM_SHA384, PKCS11_CKFM_DIGEST, ANY_PART),
91 	MECHANISM(PKCS11_CKM_SHA512, PKCS11_CKFM_DIGEST, ANY_PART),
92 	/* HMAC */
93 	MECHANISM(PKCS11_CKM_MD5_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
94 	MECHANISM(PKCS11_CKM_SHA_1_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
95 	MECHANISM(PKCS11_CKM_SHA224_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
96 	MECHANISM(PKCS11_CKM_SHA256_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
97 	MECHANISM(PKCS11_CKM_SHA384_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
98 	MECHANISM(PKCS11_CKM_SHA512_HMAC, CKFM_AUTH_NO_RECOVER, ANY_PART),
99 	MECHANISM(PKCS11_CKM_MD5_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER, ANY_PART),
100 	MECHANISM(PKCS11_CKM_SHA_1_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER,
101 		  ANY_PART),
102 	MECHANISM(PKCS11_CKM_SHA224_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER,
103 		  ANY_PART),
104 	MECHANISM(PKCS11_CKM_SHA256_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER,
105 		  ANY_PART),
106 	MECHANISM(PKCS11_CKM_SHA384_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER,
107 		  ANY_PART),
108 	MECHANISM(PKCS11_CKM_SHA512_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER,
109 		  ANY_PART),
110 	/* EC */
111 	MECHANISM(PKCS11_CKM_EC_KEY_PAIR_GEN, PKCS11_CKFM_GENERATE_KEY_PAIR,
112 		  ANY_PART),
113 	MECHANISM(PKCS11_CKM_ECDSA, CKFM_AUTH_NO_RECOVER, SINGLE_PART_ONLY),
114 	MECHANISM(PKCS11_CKM_ECDSA_SHA1, CKFM_AUTH_NO_RECOVER, ANY_PART),
115 	MECHANISM(PKCS11_CKM_ECDSA_SHA224, CKFM_AUTH_NO_RECOVER, ANY_PART),
116 	MECHANISM(PKCS11_CKM_ECDSA_SHA256, CKFM_AUTH_NO_RECOVER, ANY_PART),
117 	MECHANISM(PKCS11_CKM_ECDSA_SHA384, CKFM_AUTH_NO_RECOVER, ANY_PART),
118 	MECHANISM(PKCS11_CKM_ECDSA_SHA512, CKFM_AUTH_NO_RECOVER, ANY_PART),
119 	/* RSA */
120 	MECHANISM(PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN,
121 		  PKCS11_CKFM_GENERATE_KEY_PAIR, ANY_PART),
122 	MECHANISM(PKCS11_CKM_RSA_PKCS, CKFM_CIPHER_WRAP | CKFM_AUTH_NO_RECOVER |
123 		  CKFM_AUTH_WITH_RECOVER, SINGLE_PART_ONLY),
124 	MECHANISM(PKCS11_CKM_RSA_PKCS_OAEP, CKFM_CIPHER_WRAP,
125 		  SINGLE_PART_ONLY),
126 	MECHANISM(PKCS11_CKM_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER,
127 		  SINGLE_PART_ONLY),
128 	MECHANISM(PKCS11_CKM_MD5_RSA_PKCS, CKFM_AUTH_NO_RECOVER, ANY_PART),
129 	MECHANISM(PKCS11_CKM_SHA1_RSA_PKCS, CKFM_AUTH_NO_RECOVER, ANY_PART),
130 	MECHANISM(PKCS11_CKM_SHA1_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER, ANY_PART),
131 	MECHANISM(PKCS11_CKM_SHA256_RSA_PKCS, CKFM_AUTH_NO_RECOVER, ANY_PART),
132 	MECHANISM(PKCS11_CKM_SHA384_RSA_PKCS, CKFM_AUTH_NO_RECOVER, ANY_PART),
133 	MECHANISM(PKCS11_CKM_SHA512_RSA_PKCS, CKFM_AUTH_NO_RECOVER, ANY_PART),
134 	MECHANISM(PKCS11_CKM_SHA256_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER,
135 		  ANY_PART),
136 	MECHANISM(PKCS11_CKM_SHA384_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER,
137 		  ANY_PART),
138 	MECHANISM(PKCS11_CKM_SHA512_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER,
139 		  ANY_PART),
140 	MECHANISM(PKCS11_CKM_SHA224_RSA_PKCS, CKFM_AUTH_NO_RECOVER, ANY_PART),
141 	MECHANISM(PKCS11_CKM_SHA224_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER,
142 		  ANY_PART),
143 };
144 
145 #if CFG_TEE_TA_LOG_LEVEL > 0
mechanism_string_id(enum pkcs11_mechanism_id id)146 const char *mechanism_string_id(enum pkcs11_mechanism_id id)
147 {
148 	const size_t offset = sizeof("PKCS11_CKM_") - 1;
149 	size_t n = 0;
150 
151 	for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++)
152 		if (pkcs11_modes[n].id == id)
153 			return pkcs11_modes[n].string + offset;
154 
155 	return "Unknown ID";
156 }
157 #endif /*CFG_TEE_TA_LOG_LEVEL*/
158 
159 /*
160  * Return true if @id is a valid mechanism ID
161  */
mechanism_is_valid(enum pkcs11_mechanism_id id)162 bool mechanism_is_valid(enum pkcs11_mechanism_id id)
163 {
164 	size_t n = 0;
165 
166 	for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++)
167 		if (id == pkcs11_modes[n].id)
168 			return true;
169 
170 	return false;
171 }
172 
173 /*
174  * Return true if mechanism ID is valid and flags matches PKCS#11 compliancy
175  */
mechanism_flags_complies_pkcs11(uint32_t mechanism_type,uint32_t flags)176 bool __maybe_unused mechanism_flags_complies_pkcs11(uint32_t mechanism_type,
177 						    uint32_t flags)
178 {
179 	size_t n = 0;
180 
181 	assert((flags & ~ALLOWED_PKCS11_CKFM) == 0);
182 
183 	for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++) {
184 		if (pkcs11_modes[n].id == mechanism_type) {
185 			if (flags & ~pkcs11_modes[n].flags)
186 				EMSG("%s flags: 0x%"PRIx32" vs 0x%"PRIx32,
187 				     id2str_mechanism(mechanism_type),
188 				     flags, pkcs11_modes[n].flags);
189 
190 			return (flags & ~pkcs11_modes[n].flags) == 0;
191 		}
192 	}
193 
194 	/* Mechanism ID unexpectedly not found */
195 	return false;
196 }
197 
mechanism_is_one_shot_only(uint32_t mechanism_type)198 bool mechanism_is_one_shot_only(uint32_t mechanism_type)
199 {
200 	size_t n = 0;
201 
202 	for (n = 0; n < ARRAY_SIZE(pkcs11_modes); n++)
203 		if (pkcs11_modes[n].id == mechanism_type)
204 			return pkcs11_modes[n].one_shot;
205 
206 	/* Mechanism ID unexpectedly not found */
207 	TEE_Panic(PKCS11_RV_NOT_FOUND);
208 	/* Dummy return to keep compiler happy */
209 	return false;
210 }
211 
212 /*
213  * Field single_part_only is unused from array token_mechanism[], hence
214  * simply use ANY_PART for all mechanism there.
215  */
216 #define TA_MECHANISM(_label, _flags)	MECHANISM((_label), (_flags), ANY_PART)
217 
218 /*
219  * Arrays that centralizes the IDs and processing flags for mechanisms
220  * supported by each embedded token.
221  */
222 const struct pkcs11_mechachism_modes token_mechanism[] = {
223 	TA_MECHANISM(PKCS11_CKM_AES_ECB, CKFM_CIPHER_WRAP),
224 	TA_MECHANISM(PKCS11_CKM_AES_CBC, CKFM_CIPHER_WRAP),
225 	TA_MECHANISM(PKCS11_CKM_AES_CBC_PAD, CKFM_CIPHER),
226 	TA_MECHANISM(PKCS11_CKM_AES_CTR, CKFM_CIPHER),
227 	TA_MECHANISM(PKCS11_CKM_AES_CTS, CKFM_CIPHER),
228 	TA_MECHANISM(PKCS11_CKM_AES_CMAC, CKFM_AUTH_NO_RECOVER),
229 	TA_MECHANISM(PKCS11_CKM_AES_CMAC_GENERAL, CKFM_AUTH_NO_RECOVER),
230 	TA_MECHANISM(PKCS11_CKM_AES_ECB_ENCRYPT_DATA, PKCS11_CKFM_DERIVE),
231 	TA_MECHANISM(PKCS11_CKM_AES_CBC_ENCRYPT_DATA, PKCS11_CKFM_DERIVE),
232 	TA_MECHANISM(PKCS11_CKM_AES_KEY_GEN, PKCS11_CKFM_GENERATE),
233 	TA_MECHANISM(PKCS11_CKM_GENERIC_SECRET_KEY_GEN, PKCS11_CKFM_GENERATE),
234 	TA_MECHANISM(PKCS11_CKM_MD5, PKCS11_CKFM_DIGEST),
235 	TA_MECHANISM(PKCS11_CKM_SHA_1, PKCS11_CKFM_DIGEST),
236 	TA_MECHANISM(PKCS11_CKM_SHA224, PKCS11_CKFM_DIGEST),
237 	TA_MECHANISM(PKCS11_CKM_SHA256, PKCS11_CKFM_DIGEST),
238 	TA_MECHANISM(PKCS11_CKM_SHA384, PKCS11_CKFM_DIGEST),
239 	TA_MECHANISM(PKCS11_CKM_SHA512, PKCS11_CKFM_DIGEST),
240 	TA_MECHANISM(PKCS11_CKM_MD5_HMAC, CKFM_AUTH_NO_RECOVER),
241 	TA_MECHANISM(PKCS11_CKM_SHA_1_HMAC, CKFM_AUTH_NO_RECOVER),
242 	TA_MECHANISM(PKCS11_CKM_SHA224_HMAC, CKFM_AUTH_NO_RECOVER),
243 	TA_MECHANISM(PKCS11_CKM_SHA256_HMAC, CKFM_AUTH_NO_RECOVER),
244 	TA_MECHANISM(PKCS11_CKM_SHA384_HMAC, CKFM_AUTH_NO_RECOVER),
245 	TA_MECHANISM(PKCS11_CKM_SHA512_HMAC, CKFM_AUTH_NO_RECOVER),
246 	TA_MECHANISM(PKCS11_CKM_MD5_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER),
247 	TA_MECHANISM(PKCS11_CKM_SHA_1_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER),
248 	TA_MECHANISM(PKCS11_CKM_SHA224_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER),
249 	TA_MECHANISM(PKCS11_CKM_SHA256_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER),
250 	TA_MECHANISM(PKCS11_CKM_SHA384_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER),
251 	TA_MECHANISM(PKCS11_CKM_SHA512_HMAC_GENERAL, CKFM_AUTH_NO_RECOVER),
252 	TA_MECHANISM(PKCS11_CKM_EC_KEY_PAIR_GEN,
253 		     PKCS11_CKFM_GENERATE_KEY_PAIR),
254 	TA_MECHANISM(PKCS11_CKM_ECDSA, CKFM_AUTH_NO_RECOVER),
255 	TA_MECHANISM(PKCS11_CKM_ECDSA_SHA1, CKFM_AUTH_NO_RECOVER),
256 	TA_MECHANISM(PKCS11_CKM_ECDSA_SHA224, CKFM_AUTH_NO_RECOVER),
257 	TA_MECHANISM(PKCS11_CKM_ECDSA_SHA256, CKFM_AUTH_NO_RECOVER),
258 	TA_MECHANISM(PKCS11_CKM_ECDSA_SHA384, CKFM_AUTH_NO_RECOVER),
259 	TA_MECHANISM(PKCS11_CKM_ECDSA_SHA512, CKFM_AUTH_NO_RECOVER),
260 	TA_MECHANISM(PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN,
261 		     PKCS11_CKFM_GENERATE_KEY_PAIR),
262 	TA_MECHANISM(PKCS11_CKM_RSA_PKCS, CKFM_CIPHER | CKFM_AUTH_NO_RECOVER),
263 	TA_MECHANISM(PKCS11_CKM_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER),
264 	TA_MECHANISM(PKCS11_CKM_MD5_RSA_PKCS, CKFM_AUTH_NO_RECOVER),
265 	TA_MECHANISM(PKCS11_CKM_SHA1_RSA_PKCS, CKFM_AUTH_NO_RECOVER),
266 	TA_MECHANISM(PKCS11_CKM_RSA_PKCS_OAEP, CKFM_CIPHER),
267 	TA_MECHANISM(PKCS11_CKM_SHA1_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER),
268 	TA_MECHANISM(PKCS11_CKM_SHA256_RSA_PKCS, CKFM_AUTH_NO_RECOVER),
269 	TA_MECHANISM(PKCS11_CKM_SHA384_RSA_PKCS, CKFM_AUTH_NO_RECOVER),
270 	TA_MECHANISM(PKCS11_CKM_SHA512_RSA_PKCS, CKFM_AUTH_NO_RECOVER),
271 	TA_MECHANISM(PKCS11_CKM_SHA256_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER),
272 	TA_MECHANISM(PKCS11_CKM_SHA384_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER),
273 	TA_MECHANISM(PKCS11_CKM_SHA512_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER),
274 	TA_MECHANISM(PKCS11_CKM_SHA224_RSA_PKCS, CKFM_AUTH_NO_RECOVER),
275 	TA_MECHANISM(PKCS11_CKM_SHA224_RSA_PKCS_PSS, CKFM_AUTH_NO_RECOVER),
276 };
277 
278 /*
279  * tee_malloc_mechanism_array - Allocate and fill array of supported mechanisms
280  * @count: [in] [out] Pointer to number of mechanism IDs in client resource
281  * Return allocated array of the supported mechanism IDs
282  *
283  * Allocates array with 32bit cells mechanism IDs for the supported ones only
284  * if *@count covers number mechanism IDs exposed.
285  */
tee_malloc_mechanism_list(size_t * out_count)286 uint32_t *tee_malloc_mechanism_list(size_t *out_count)
287 {
288 	size_t n = 0;
289 	size_t count = 0;
290 	uint32_t *array = NULL;
291 
292 	for (n = 0; n < ARRAY_SIZE(token_mechanism); n++)
293 		if (token_mechanism[n].flags)
294 			count++;
295 
296 	if (*out_count >= count)
297 		array = TEE_Malloc(count * sizeof(*array),
298 				   TEE_USER_MEM_HINT_NO_FILL_ZERO);
299 
300 	*out_count = count;
301 
302 	if (!array)
303 		return NULL;
304 
305 	for (n = 0; n < ARRAY_SIZE(token_mechanism); n++) {
306 		if (token_mechanism[n].flags) {
307 			count--;
308 			array[count] = token_mechanism[n].id;
309 		}
310 	}
311 	assert(!count);
312 
313 	return array;
314 }
315 
mechanism_supported_flags(enum pkcs11_mechanism_id id)316 uint32_t mechanism_supported_flags(enum pkcs11_mechanism_id id)
317 {
318 	size_t n = 0;
319 
320 	for (n = 0; n < ARRAY_SIZE(token_mechanism); n++) {
321 		if (id == token_mechanism[n].id) {
322 			uint32_t flags = token_mechanism[n].flags;
323 
324 			assert(mechanism_flags_complies_pkcs11(id, flags));
325 			return flags;
326 		}
327 	}
328 
329 	return 0;
330 }
331 
pkcs11_mechanism_supported_key_sizes(uint32_t proc_id,uint32_t * min_key_size,uint32_t * max_key_size)332 void pkcs11_mechanism_supported_key_sizes(uint32_t proc_id,
333 					  uint32_t *min_key_size,
334 					  uint32_t *max_key_size)
335 {
336 	switch (proc_id) {
337 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
338 		/* This mechanism expects the keysize to be returned in bits */
339 		*min_key_size = 1;		/* in bits */
340 		*max_key_size = 4096;		/* in bits */
341 		break;
342 	case PKCS11_CKM_MD5_HMAC:
343 	case PKCS11_CKM_MD5_HMAC_GENERAL:
344 		*min_key_size = 8;
345 		*max_key_size = 64;
346 		break;
347 	case PKCS11_CKM_SHA_1_HMAC:
348 	case PKCS11_CKM_SHA_1_HMAC_GENERAL:
349 		*min_key_size = 10;
350 		*max_key_size = 64;
351 		break;
352 	case PKCS11_CKM_SHA224_HMAC:
353 	case PKCS11_CKM_SHA224_HMAC_GENERAL:
354 		*min_key_size = 14;
355 		*max_key_size = 64;
356 		break;
357 	case PKCS11_CKM_SHA256_HMAC:
358 	case PKCS11_CKM_SHA256_HMAC_GENERAL:
359 		*min_key_size = 24;
360 		*max_key_size = 128;
361 		break;
362 	case PKCS11_CKM_SHA384_HMAC:
363 	case PKCS11_CKM_SHA384_HMAC_GENERAL:
364 		*min_key_size = 32;
365 		*max_key_size = 128;
366 		break;
367 	case PKCS11_CKM_SHA512_HMAC:
368 	case PKCS11_CKM_SHA512_HMAC_GENERAL:
369 		*min_key_size = 32;
370 		*max_key_size = 128;
371 		break;
372 	case PKCS11_CKM_AES_KEY_GEN:
373 	case PKCS11_CKM_AES_ECB:
374 	case PKCS11_CKM_AES_CBC:
375 	case PKCS11_CKM_AES_CBC_PAD:
376 	case PKCS11_CKM_AES_CTR:
377 	case PKCS11_CKM_AES_CTS:
378 	case PKCS11_CKM_AES_CMAC:
379 	case PKCS11_CKM_AES_CMAC_GENERAL:
380 		*min_key_size = 16;
381 		*max_key_size = 32;
382 		break;
383 	case PKCS11_CKM_EC_KEY_PAIR_GEN:
384 	case PKCS11_CKM_ECDSA:
385 	case PKCS11_CKM_ECDSA_SHA1:
386 	case PKCS11_CKM_ECDSA_SHA224:
387 	case PKCS11_CKM_ECDSA_SHA256:
388 	case PKCS11_CKM_ECDSA_SHA384:
389 	case PKCS11_CKM_ECDSA_SHA512:
390 		*min_key_size = 160;	/* in bits */
391 		*max_key_size = 521;	/* in bits */
392 		break;
393 	case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN:
394 	case PKCS11_CKM_RSA_PKCS:
395 	case PKCS11_CKM_MD5_RSA_PKCS:
396 	case PKCS11_CKM_SHA1_RSA_PKCS:
397 	case PKCS11_CKM_RSA_PKCS_OAEP:
398 	case PKCS11_CKM_SHA1_RSA_PKCS_PSS:
399 	case PKCS11_CKM_SHA256_RSA_PKCS:
400 	case PKCS11_CKM_SHA384_RSA_PKCS:
401 	case PKCS11_CKM_SHA512_RSA_PKCS:
402 	case PKCS11_CKM_SHA256_RSA_PKCS_PSS:
403 	case PKCS11_CKM_SHA384_RSA_PKCS_PSS:
404 	case PKCS11_CKM_SHA512_RSA_PKCS_PSS:
405 	case PKCS11_CKM_SHA224_RSA_PKCS:
406 	case PKCS11_CKM_SHA224_RSA_PKCS_PSS:
407 		*min_key_size = 256;	/* in bits */
408 		*max_key_size = 4096;	/* in bits */
409 		break;
410 	default:
411 		*min_key_size = 0;
412 		*max_key_size = 0;
413 		break;
414 	}
415 }
416 
mechanism_supported_key_sizes_bytes(uint32_t proc_id,uint32_t * min_key_size,uint32_t * max_key_size)417 void mechanism_supported_key_sizes_bytes(uint32_t proc_id,
418 					 uint32_t *min_key_size,
419 					 uint32_t *max_key_size)
420 {
421 	pkcs11_mechanism_supported_key_sizes(proc_id, min_key_size,
422 					     max_key_size);
423 
424 	switch (proc_id) {
425 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
426 	case PKCS11_CKM_EC_KEY_PAIR_GEN:
427 	case PKCS11_CKM_ECDSA:
428 	case PKCS11_CKM_ECDSA_SHA1:
429 	case PKCS11_CKM_ECDSA_SHA224:
430 	case PKCS11_CKM_ECDSA_SHA256:
431 	case PKCS11_CKM_ECDSA_SHA384:
432 	case PKCS11_CKM_ECDSA_SHA512:
433 	case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN:
434 		/* Size is in bits -> convert to bytes and ceil */
435 		*min_key_size = ROUNDUP(*min_key_size, 8) / 8;
436 		*max_key_size = ROUNDUP(*max_key_size, 8) / 8;
437 		break;
438 	default:
439 		/* Size is already in bytes */
440 		break;
441 	}
442 }
443