/src/mbedtls/library/pk.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Public Key abstraction layer |
3 | | * |
4 | | * Copyright The Mbed TLS Contributors |
5 | | * SPDX-License-Identifier: Apache-2.0 |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
8 | | * not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
15 | | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include "common.h" |
21 | | |
22 | | #if defined(MBEDTLS_PK_C) |
23 | | #include "mbedtls/pk.h" |
24 | | #include "pk_wrap.h" |
25 | | #include "pkwrite.h" |
26 | | |
27 | | #include "hash_info.h" |
28 | | |
29 | | #include "mbedtls/platform_util.h" |
30 | | #include "mbedtls/error.h" |
31 | | |
32 | | #if defined(MBEDTLS_RSA_C) |
33 | | #include "mbedtls/rsa.h" |
34 | | #endif |
35 | | #if defined(MBEDTLS_ECP_C) |
36 | | #include "mbedtls/ecp.h" |
37 | | #endif |
38 | | #if defined(MBEDTLS_ECDSA_C) |
39 | | #include "mbedtls/ecdsa.h" |
40 | | #endif |
41 | | |
42 | | #if defined(MBEDTLS_PSA_CRYPTO_C) |
43 | | #include "mbedtls/psa_util.h" |
44 | | #endif |
45 | | |
46 | | #include <limits.h> |
47 | | #include <stdint.h> |
48 | | |
49 | | /* |
50 | | * Initialise a mbedtls_pk_context |
51 | | */ |
52 | | void mbedtls_pk_init(mbedtls_pk_context *ctx) |
53 | 0 | { |
54 | 0 | ctx->pk_info = NULL; |
55 | 0 | ctx->pk_ctx = NULL; |
56 | 0 | } |
57 | | |
58 | | /* |
59 | | * Free (the components of) a mbedtls_pk_context |
60 | | */ |
61 | | void mbedtls_pk_free(mbedtls_pk_context *ctx) |
62 | 30.3k | { |
63 | 30.3k | if (ctx == NULL) { |
64 | 0 | return; |
65 | 0 | } |
66 | | |
67 | 30.3k | if (ctx->pk_info != NULL) { |
68 | 18.9k | ctx->pk_info->ctx_free_func(ctx->pk_ctx); |
69 | 18.9k | } |
70 | | |
71 | 30.3k | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context)); |
72 | 30.3k | } |
73 | | |
74 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
75 | | /* |
76 | | * Initialize a restart context |
77 | | */ |
78 | | void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx) |
79 | | { |
80 | | ctx->pk_info = NULL; |
81 | | ctx->rs_ctx = NULL; |
82 | | } |
83 | | |
84 | | /* |
85 | | * Free the components of a restart context |
86 | | */ |
87 | | void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx) |
88 | | { |
89 | | if (ctx == NULL || ctx->pk_info == NULL || |
90 | | ctx->pk_info->rs_free_func == NULL) { |
91 | | return; |
92 | | } |
93 | | |
94 | | ctx->pk_info->rs_free_func(ctx->rs_ctx); |
95 | | |
96 | | ctx->pk_info = NULL; |
97 | | ctx->rs_ctx = NULL; |
98 | | } |
99 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
100 | | |
101 | | /* |
102 | | * Get pk_info structure from type |
103 | | */ |
104 | | const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type) |
105 | 23.3k | { |
106 | 23.3k | switch (pk_type) { |
107 | 0 | #if defined(MBEDTLS_RSA_C) |
108 | 18.6k | case MBEDTLS_PK_RSA: |
109 | 18.6k | return &mbedtls_rsa_info; |
110 | 0 | #endif |
111 | 0 | #if defined(MBEDTLS_ECP_C) |
112 | 4.74k | case MBEDTLS_PK_ECKEY: |
113 | 4.74k | return &mbedtls_eckey_info; |
114 | 0 | case MBEDTLS_PK_ECKEY_DH: |
115 | 0 | return &mbedtls_eckeydh_info; |
116 | 0 | #endif |
117 | 0 | #if defined(MBEDTLS_ECDSA_C) |
118 | 0 | case MBEDTLS_PK_ECDSA: |
119 | 0 | return &mbedtls_ecdsa_info; |
120 | 0 | #endif |
121 | | /* MBEDTLS_PK_RSA_ALT omitted on purpose */ |
122 | 0 | default: |
123 | 0 | return NULL; |
124 | 23.3k | } |
125 | 23.3k | } |
126 | | |
127 | | /* |
128 | | * Initialise context |
129 | | */ |
130 | | int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info) |
131 | 23.3k | { |
132 | 23.3k | if (info == NULL || ctx->pk_info != NULL) { |
133 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
134 | 0 | } |
135 | | |
136 | 23.3k | if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) { |
137 | 0 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
138 | 0 | } |
139 | | |
140 | 23.3k | ctx->pk_info = info; |
141 | | |
142 | 23.3k | return 0; |
143 | 23.3k | } |
144 | | |
145 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
146 | | /* |
147 | | * Initialise a PSA-wrapping context |
148 | | */ |
149 | | int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx, |
150 | | const mbedtls_svc_key_id_t key) |
151 | | { |
152 | | const mbedtls_pk_info_t *info = NULL; |
153 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
154 | | mbedtls_svc_key_id_t *pk_ctx; |
155 | | psa_key_type_t type; |
156 | | |
157 | | if (ctx == NULL || ctx->pk_info != NULL) { |
158 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
159 | | } |
160 | | |
161 | | if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) { |
162 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
163 | | } |
164 | | type = psa_get_key_type(&attributes); |
165 | | psa_reset_key_attributes(&attributes); |
166 | | |
167 | | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
168 | | info = &mbedtls_pk_ecdsa_opaque_info; |
169 | | } else if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
170 | | info = &mbedtls_pk_rsa_opaque_info; |
171 | | } else { |
172 | | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
173 | | } |
174 | | |
175 | | if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) { |
176 | | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
177 | | } |
178 | | |
179 | | ctx->pk_info = info; |
180 | | |
181 | | pk_ctx = (mbedtls_svc_key_id_t *) ctx->pk_ctx; |
182 | | *pk_ctx = key; |
183 | | |
184 | | return 0; |
185 | | } |
186 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
187 | | |
188 | | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
189 | | /* |
190 | | * Initialize an RSA-alt context |
191 | | */ |
192 | | int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key, |
193 | | mbedtls_pk_rsa_alt_decrypt_func decrypt_func, |
194 | | mbedtls_pk_rsa_alt_sign_func sign_func, |
195 | | mbedtls_pk_rsa_alt_key_len_func key_len_func) |
196 | 0 | { |
197 | 0 | mbedtls_rsa_alt_context *rsa_alt; |
198 | 0 | const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info; |
199 | |
|
200 | 0 | if (ctx->pk_info != NULL) { |
201 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
202 | 0 | } |
203 | | |
204 | 0 | if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) { |
205 | 0 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
206 | 0 | } |
207 | | |
208 | 0 | ctx->pk_info = info; |
209 | |
|
210 | 0 | rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx; |
211 | |
|
212 | 0 | rsa_alt->key = key; |
213 | 0 | rsa_alt->decrypt_func = decrypt_func; |
214 | 0 | rsa_alt->sign_func = sign_func; |
215 | 0 | rsa_alt->key_len_func = key_len_func; |
216 | |
|
217 | 0 | return 0; |
218 | 0 | } |
219 | | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
220 | | |
221 | | /* |
222 | | * Tell if a PK can do the operations of the given type |
223 | | */ |
224 | | int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type) |
225 | 2.88k | { |
226 | | /* A context with null pk_info is not set up yet and can't do anything. |
227 | | * For backward compatibility, also accept NULL instead of a context |
228 | | * pointer. */ |
229 | 2.88k | if (ctx == NULL || ctx->pk_info == NULL) { |
230 | 30 | return 0; |
231 | 30 | } |
232 | | |
233 | 2.85k | return ctx->pk_info->can_do(type); |
234 | 2.88k | } |
235 | | |
236 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
237 | | /* |
238 | | * Tell if a PK can do the operations of the given PSA algorithm |
239 | | */ |
240 | | int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg, |
241 | | psa_key_usage_t usage) |
242 | | { |
243 | | psa_key_usage_t key_usage; |
244 | | |
245 | | /* A context with null pk_info is not set up yet and can't do anything. |
246 | | * For backward compatibility, also accept NULL instead of a context |
247 | | * pointer. */ |
248 | | if (ctx == NULL || ctx->pk_info == NULL) { |
249 | | return 0; |
250 | | } |
251 | | |
252 | | /* Filter out non allowed algorithms */ |
253 | | if (PSA_ALG_IS_ECDSA(alg) == 0 && |
254 | | PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 && |
255 | | PSA_ALG_IS_RSA_PSS(alg) == 0 && |
256 | | alg != PSA_ALG_RSA_PKCS1V15_CRYPT && |
257 | | PSA_ALG_IS_ECDH(alg) == 0) { |
258 | | return 0; |
259 | | } |
260 | | |
261 | | /* Filter out non allowed usage flags */ |
262 | | if (usage == 0 || |
263 | | (usage & ~(PSA_KEY_USAGE_SIGN_HASH | |
264 | | PSA_KEY_USAGE_DECRYPT | |
265 | | PSA_KEY_USAGE_DERIVE)) != 0) { |
266 | | return 0; |
267 | | } |
268 | | |
269 | | /* Wildcard hash is not allowed */ |
270 | | if (PSA_ALG_IS_SIGN_HASH(alg) && |
271 | | PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) { |
272 | | return 0; |
273 | | } |
274 | | |
275 | | if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) { |
276 | | mbedtls_pk_type_t type; |
277 | | |
278 | | if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) { |
279 | | type = MBEDTLS_PK_ECKEY; |
280 | | } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
281 | | alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
282 | | type = MBEDTLS_PK_RSA; |
283 | | } else if (PSA_ALG_IS_RSA_PSS(alg)) { |
284 | | type = MBEDTLS_PK_RSASSA_PSS; |
285 | | } else { |
286 | | return 0; |
287 | | } |
288 | | |
289 | | if (ctx->pk_info->can_do(type) == 0) { |
290 | | return 0; |
291 | | } |
292 | | |
293 | | switch (type) { |
294 | | case MBEDTLS_PK_ECKEY: |
295 | | key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE; |
296 | | break; |
297 | | case MBEDTLS_PK_RSA: |
298 | | case MBEDTLS_PK_RSASSA_PSS: |
299 | | key_usage = PSA_KEY_USAGE_SIGN_HASH | |
300 | | PSA_KEY_USAGE_SIGN_MESSAGE | |
301 | | PSA_KEY_USAGE_DECRYPT; |
302 | | break; |
303 | | default: |
304 | | /* Should never happen */ |
305 | | return 0; |
306 | | } |
307 | | |
308 | | return (key_usage & usage) == usage; |
309 | | } |
310 | | |
311 | | const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx; |
312 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
313 | | psa_algorithm_t key_alg, key_alg2; |
314 | | psa_status_t status; |
315 | | |
316 | | status = psa_get_key_attributes(*key, &attributes); |
317 | | if (status != PSA_SUCCESS) { |
318 | | return 0; |
319 | | } |
320 | | |
321 | | key_alg = psa_get_key_algorithm(&attributes); |
322 | | key_alg2 = psa_get_key_enrollment_algorithm(&attributes); |
323 | | key_usage = psa_get_key_usage_flags(&attributes); |
324 | | psa_reset_key_attributes(&attributes); |
325 | | |
326 | | if ((key_usage & usage) != usage) { |
327 | | return 0; |
328 | | } |
329 | | |
330 | | /* |
331 | | * Common case: the key alg or alg2 only allows alg. |
332 | | * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH |
333 | | * directly. |
334 | | * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with |
335 | | * a fixed hash on key_alg/key_alg2. |
336 | | */ |
337 | | if (alg == key_alg || alg == key_alg2) { |
338 | | return 1; |
339 | | } |
340 | | |
341 | | /* |
342 | | * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash, |
343 | | * and alg is the same hash-and-sign family with any hash, |
344 | | * then alg is compliant with this key alg |
345 | | */ |
346 | | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
347 | | |
348 | | if (PSA_ALG_IS_SIGN_HASH(key_alg) && |
349 | | PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH && |
350 | | (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) { |
351 | | return 1; |
352 | | } |
353 | | |
354 | | if (PSA_ALG_IS_SIGN_HASH(key_alg2) && |
355 | | PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH && |
356 | | (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) { |
357 | | return 1; |
358 | | } |
359 | | } |
360 | | |
361 | | return 0; |
362 | | } |
363 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
364 | | |
365 | | /* |
366 | | * Helper for mbedtls_pk_sign and mbedtls_pk_verify |
367 | | */ |
368 | | static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len) |
369 | 568 | { |
370 | 568 | if (*hash_len != 0) { |
371 | 568 | return 0; |
372 | 568 | } |
373 | | |
374 | 0 | *hash_len = mbedtls_hash_info_get_size(md_alg); |
375 | |
|
376 | 0 | if (*hash_len == 0) { |
377 | 0 | return -1; |
378 | 0 | } |
379 | | |
380 | 0 | return 0; |
381 | 0 | } |
382 | | |
383 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
384 | | /* |
385 | | * Helper to set up a restart context if needed |
386 | | */ |
387 | | static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx, |
388 | | const mbedtls_pk_info_t *info) |
389 | | { |
390 | | /* Don't do anything if already set up or invalid */ |
391 | | if (ctx == NULL || ctx->pk_info != NULL) { |
392 | | return 0; |
393 | | } |
394 | | |
395 | | /* Should never happen when we're called */ |
396 | | if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) { |
397 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
398 | | } |
399 | | |
400 | | if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) { |
401 | | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
402 | | } |
403 | | |
404 | | ctx->pk_info = info; |
405 | | |
406 | | return 0; |
407 | | } |
408 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
409 | | |
410 | | /* |
411 | | * Verify a signature (restartable) |
412 | | */ |
413 | | int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx, |
414 | | mbedtls_md_type_t md_alg, |
415 | | const unsigned char *hash, size_t hash_len, |
416 | | const unsigned char *sig, size_t sig_len, |
417 | | mbedtls_pk_restart_ctx *rs_ctx) |
418 | 568 | { |
419 | 568 | if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) { |
420 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
421 | 0 | } |
422 | | |
423 | 568 | if (ctx->pk_info == NULL || |
424 | 568 | pk_hashlen_helper(md_alg, &hash_len) != 0) { |
425 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
426 | 0 | } |
427 | | |
428 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
429 | | /* optimization: use non-restartable version if restart disabled */ |
430 | | if (rs_ctx != NULL && |
431 | | mbedtls_ecp_restart_is_enabled() && |
432 | | ctx->pk_info->verify_rs_func != NULL) { |
433 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
434 | | |
435 | | if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) { |
436 | | return ret; |
437 | | } |
438 | | |
439 | | ret = ctx->pk_info->verify_rs_func(ctx->pk_ctx, |
440 | | md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx); |
441 | | |
442 | | if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) { |
443 | | mbedtls_pk_restart_free(rs_ctx); |
444 | | } |
445 | | |
446 | | return ret; |
447 | | } |
448 | | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
449 | 568 | (void) rs_ctx; |
450 | 568 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
451 | | |
452 | 568 | if (ctx->pk_info->verify_func == NULL) { |
453 | 0 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
454 | 0 | } |
455 | | |
456 | 568 | return ctx->pk_info->verify_func(ctx->pk_ctx, md_alg, hash, hash_len, |
457 | 568 | sig, sig_len); |
458 | 568 | } |
459 | | |
460 | | /* |
461 | | * Verify a signature |
462 | | */ |
463 | | int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
464 | | const unsigned char *hash, size_t hash_len, |
465 | | const unsigned char *sig, size_t sig_len) |
466 | 0 | { |
467 | 0 | return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len, |
468 | 0 | sig, sig_len, NULL); |
469 | 0 | } |
470 | | |
471 | | /* |
472 | | * Verify a signature with options |
473 | | */ |
474 | | int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options, |
475 | | mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
476 | | const unsigned char *hash, size_t hash_len, |
477 | | const unsigned char *sig, size_t sig_len) |
478 | 401 | { |
479 | 401 | if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) { |
480 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
481 | 0 | } |
482 | | |
483 | 401 | if (ctx->pk_info == NULL) { |
484 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
485 | 0 | } |
486 | | |
487 | 401 | if (!mbedtls_pk_can_do(ctx, type)) { |
488 | 0 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
489 | 0 | } |
490 | | |
491 | 401 | if (type != MBEDTLS_PK_RSASSA_PSS) { |
492 | | /* General case: no options */ |
493 | 0 | if (options != NULL) { |
494 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
495 | 0 | } |
496 | | |
497 | 0 | return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len); |
498 | 0 | } |
499 | | |
500 | 401 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
501 | 401 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
502 | 401 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
503 | | |
504 | 401 | #if SIZE_MAX > UINT_MAX |
505 | 401 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
506 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
507 | 0 | } |
508 | 401 | #endif /* SIZE_MAX > UINT_MAX */ |
509 | | |
510 | 401 | if (options == NULL) { |
511 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
512 | 0 | } |
513 | | |
514 | 401 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) options; |
515 | | |
516 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
517 | | if (pss_opts->mgf1_hash_id == md_alg) { |
518 | | unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES]; |
519 | | unsigned char *p; |
520 | | int key_len; |
521 | | size_t signature_length; |
522 | | psa_status_t status = PSA_ERROR_DATA_CORRUPT; |
523 | | psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT; |
524 | | |
525 | | psa_algorithm_t psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg); |
526 | | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
527 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
528 | | psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg); |
529 | | p = buf + sizeof(buf); |
530 | | key_len = mbedtls_pk_write_pubkey(&p, buf, ctx); |
531 | | |
532 | | if (key_len < 0) { |
533 | | return key_len; |
534 | | } |
535 | | |
536 | | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY); |
537 | | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
538 | | psa_set_key_algorithm(&attributes, psa_sig_alg); |
539 | | |
540 | | status = psa_import_key(&attributes, |
541 | | buf + sizeof(buf) - key_len, key_len, |
542 | | &key_id); |
543 | | if (status != PSA_SUCCESS) { |
544 | | psa_destroy_key(key_id); |
545 | | return mbedtls_pk_error_from_psa(status); |
546 | | } |
547 | | |
548 | | /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH |
549 | | * on a valid signature with trailing data in a buffer, but |
550 | | * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact, |
551 | | * so for this reason the passed sig_len is overwritten. Smaller |
552 | | * signature lengths should not be accepted for verification. */ |
553 | | signature_length = sig_len > mbedtls_pk_get_len(ctx) ? |
554 | | mbedtls_pk_get_len(ctx) : sig_len; |
555 | | status = psa_verify_hash(key_id, psa_sig_alg, hash, |
556 | | hash_len, sig, signature_length); |
557 | | destruction_status = psa_destroy_key(key_id); |
558 | | |
559 | | if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) { |
560 | | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
561 | | } |
562 | | |
563 | | if (status == PSA_SUCCESS) { |
564 | | status = destruction_status; |
565 | | } |
566 | | |
567 | | return mbedtls_pk_error_from_psa_rsa(status); |
568 | | } else |
569 | | #endif |
570 | 401 | { |
571 | 401 | if (sig_len < mbedtls_pk_get_len(ctx)) { |
572 | 26 | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
573 | 26 | } |
574 | | |
575 | 375 | ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx), |
576 | 375 | md_alg, (unsigned int) hash_len, hash, |
577 | 375 | pss_opts->mgf1_hash_id, |
578 | 375 | pss_opts->expected_salt_len, |
579 | 375 | sig); |
580 | 375 | if (ret != 0) { |
581 | 375 | return ret; |
582 | 375 | } |
583 | | |
584 | 0 | if (sig_len > mbedtls_pk_get_len(ctx)) { |
585 | 0 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
586 | 0 | } |
587 | | |
588 | 0 | return 0; |
589 | 0 | } |
590 | | #else |
591 | | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
592 | | #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */ |
593 | 0 | } |
594 | | |
595 | | /* |
596 | | * Make a signature (restartable) |
597 | | */ |
598 | | int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx, |
599 | | mbedtls_md_type_t md_alg, |
600 | | const unsigned char *hash, size_t hash_len, |
601 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
602 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
603 | | mbedtls_pk_restart_ctx *rs_ctx) |
604 | 0 | { |
605 | 0 | if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) { |
606 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
607 | 0 | } |
608 | | |
609 | 0 | if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) { |
610 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
611 | 0 | } |
612 | | |
613 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
614 | | /* optimization: use non-restartable version if restart disabled */ |
615 | | if (rs_ctx != NULL && |
616 | | mbedtls_ecp_restart_is_enabled() && |
617 | | ctx->pk_info->sign_rs_func != NULL) { |
618 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
619 | | |
620 | | if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) { |
621 | | return ret; |
622 | | } |
623 | | |
624 | | ret = ctx->pk_info->sign_rs_func(ctx->pk_ctx, md_alg, |
625 | | hash, hash_len, |
626 | | sig, sig_size, sig_len, |
627 | | f_rng, p_rng, rs_ctx->rs_ctx); |
628 | | |
629 | | if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) { |
630 | | mbedtls_pk_restart_free(rs_ctx); |
631 | | } |
632 | | |
633 | | return ret; |
634 | | } |
635 | | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
636 | 0 | (void) rs_ctx; |
637 | 0 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
638 | |
|
639 | 0 | if (ctx->pk_info->sign_func == NULL) { |
640 | 0 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
641 | 0 | } |
642 | | |
643 | 0 | return ctx->pk_info->sign_func(ctx->pk_ctx, md_alg, |
644 | 0 | hash, hash_len, |
645 | 0 | sig, sig_size, sig_len, |
646 | 0 | f_rng, p_rng); |
647 | 0 | } |
648 | | |
649 | | /* |
650 | | * Make a signature |
651 | | */ |
652 | | int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
653 | | const unsigned char *hash, size_t hash_len, |
654 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
655 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
656 | 0 | { |
657 | 0 | return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len, |
658 | 0 | sig, sig_size, sig_len, |
659 | 0 | f_rng, p_rng, NULL); |
660 | 0 | } |
661 | | |
662 | | #if defined(MBEDTLS_PSA_CRYPTO_C) |
663 | | /* |
664 | | * Make a signature given a signature type. |
665 | | */ |
666 | | int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type, |
667 | | mbedtls_pk_context *ctx, |
668 | | mbedtls_md_type_t md_alg, |
669 | | const unsigned char *hash, size_t hash_len, |
670 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
671 | | int (*f_rng)(void *, unsigned char *, size_t), |
672 | | void *p_rng) |
673 | 0 | { |
674 | 0 | #if defined(MBEDTLS_RSA_C) |
675 | 0 | psa_algorithm_t psa_md_alg; |
676 | 0 | #endif /* MBEDTLS_RSA_C */ |
677 | 0 | *sig_len = 0; |
678 | |
|
679 | 0 | if (ctx->pk_info == NULL) { |
680 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
681 | 0 | } |
682 | | |
683 | 0 | if (!mbedtls_pk_can_do(ctx, pk_type)) { |
684 | 0 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
685 | 0 | } |
686 | | |
687 | 0 | if (pk_type != MBEDTLS_PK_RSASSA_PSS) { |
688 | 0 | return mbedtls_pk_sign(ctx, md_alg, hash, hash_len, |
689 | 0 | sig, sig_size, sig_len, f_rng, p_rng); |
690 | 0 | } |
691 | | |
692 | 0 | #if defined(MBEDTLS_RSA_C) |
693 | 0 | psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg); |
694 | 0 | if (psa_md_alg == 0) { |
695 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
696 | 0 | } |
697 | | |
698 | 0 | if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) { |
699 | 0 | const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx; |
700 | 0 | psa_status_t status; |
701 | |
|
702 | 0 | status = psa_sign_hash(*key, PSA_ALG_RSA_PSS(psa_md_alg), |
703 | 0 | hash, hash_len, |
704 | 0 | sig, sig_size, sig_len); |
705 | 0 | return mbedtls_pk_error_from_psa_rsa(status); |
706 | 0 | } |
707 | | |
708 | 0 | return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg), |
709 | 0 | ctx->pk_ctx, hash, hash_len, |
710 | 0 | sig, sig_size, sig_len); |
711 | | #else /* MBEDTLS_RSA_C */ |
712 | | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
713 | | #endif /* !MBEDTLS_RSA_C */ |
714 | |
|
715 | 0 | } |
716 | | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
717 | | |
718 | | /* |
719 | | * Decrypt message |
720 | | */ |
721 | | int mbedtls_pk_decrypt(mbedtls_pk_context *ctx, |
722 | | const unsigned char *input, size_t ilen, |
723 | | unsigned char *output, size_t *olen, size_t osize, |
724 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
725 | 0 | { |
726 | 0 | if (ctx->pk_info == NULL) { |
727 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
728 | 0 | } |
729 | | |
730 | 0 | if (ctx->pk_info->decrypt_func == NULL) { |
731 | 0 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
732 | 0 | } |
733 | | |
734 | 0 | return ctx->pk_info->decrypt_func(ctx->pk_ctx, input, ilen, |
735 | 0 | output, olen, osize, f_rng, p_rng); |
736 | 0 | } |
737 | | |
738 | | /* |
739 | | * Encrypt message |
740 | | */ |
741 | | int mbedtls_pk_encrypt(mbedtls_pk_context *ctx, |
742 | | const unsigned char *input, size_t ilen, |
743 | | unsigned char *output, size_t *olen, size_t osize, |
744 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
745 | 335 | { |
746 | 335 | if (ctx->pk_info == NULL) { |
747 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
748 | 0 | } |
749 | | |
750 | 335 | if (ctx->pk_info->encrypt_func == NULL) { |
751 | 0 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
752 | 0 | } |
753 | | |
754 | 335 | return ctx->pk_info->encrypt_func(ctx->pk_ctx, input, ilen, |
755 | 335 | output, olen, osize, f_rng, p_rng); |
756 | 335 | } |
757 | | |
758 | | /* |
759 | | * Check public-private key pair |
760 | | */ |
761 | | int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, |
762 | | const mbedtls_pk_context *prv, |
763 | | int (*f_rng)(void *, unsigned char *, size_t), |
764 | | void *p_rng) |
765 | 0 | { |
766 | 0 | if (pub->pk_info == NULL || |
767 | 0 | prv->pk_info == NULL) { |
768 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
769 | 0 | } |
770 | | |
771 | 0 | if (f_rng == NULL) { |
772 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
773 | 0 | } |
774 | | |
775 | 0 | if (prv->pk_info->check_pair_func == NULL) { |
776 | 0 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
777 | 0 | } |
778 | | |
779 | 0 | if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) { |
780 | 0 | if (pub->pk_info->type != MBEDTLS_PK_RSA) { |
781 | 0 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
782 | 0 | } |
783 | 0 | } else { |
784 | 0 | if (pub->pk_info != prv->pk_info) { |
785 | 0 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
786 | 0 | } |
787 | 0 | } |
788 | | |
789 | 0 | return prv->pk_info->check_pair_func(pub->pk_ctx, prv->pk_ctx, f_rng, p_rng); |
790 | 0 | } |
791 | | |
792 | | /* |
793 | | * Get key size in bits |
794 | | */ |
795 | | size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx) |
796 | 401 | { |
797 | | /* For backward compatibility, accept NULL or a context that |
798 | | * isn't set up yet, and return a fake value that should be safe. */ |
799 | 401 | if (ctx == NULL || ctx->pk_info == NULL) { |
800 | 0 | return 0; |
801 | 0 | } |
802 | | |
803 | 401 | return ctx->pk_info->get_bitlen(ctx->pk_ctx); |
804 | 401 | } |
805 | | |
806 | | /* |
807 | | * Export debug information |
808 | | */ |
809 | | int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items) |
810 | 0 | { |
811 | 0 | if (ctx->pk_info == NULL) { |
812 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
813 | 0 | } |
814 | | |
815 | 0 | if (ctx->pk_info->debug_func == NULL) { |
816 | 0 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
817 | 0 | } |
818 | | |
819 | 0 | ctx->pk_info->debug_func(ctx->pk_ctx, items); |
820 | 0 | return 0; |
821 | 0 | } |
822 | | |
823 | | /* |
824 | | * Access the PK type name |
825 | | */ |
826 | | const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx) |
827 | 0 | { |
828 | 0 | if (ctx == NULL || ctx->pk_info == NULL) { |
829 | 0 | return "invalid PK"; |
830 | 0 | } |
831 | | |
832 | 0 | return ctx->pk_info->name; |
833 | 0 | } |
834 | | |
835 | | /* |
836 | | * Access the PK type |
837 | | */ |
838 | | mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx) |
839 | 28.5k | { |
840 | 28.5k | if (ctx == NULL || ctx->pk_info == NULL) { |
841 | 0 | return MBEDTLS_PK_NONE; |
842 | 0 | } |
843 | | |
844 | 28.5k | return ctx->pk_info->type; |
845 | 28.5k | } |
846 | | |
847 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
848 | | /* |
849 | | * Load the key to a PSA key slot, |
850 | | * then turn the PK context into a wrapper for that key slot. |
851 | | * |
852 | | * Currently only works for EC & RSA private keys. |
853 | | */ |
854 | | int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk, |
855 | | mbedtls_svc_key_id_t *key, |
856 | | psa_algorithm_t alg, |
857 | | psa_key_usage_t usage, |
858 | | psa_algorithm_t alg2) |
859 | | { |
860 | | #if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_RSA_C) |
861 | | ((void) pk); |
862 | | ((void) key); |
863 | | ((void) alg); |
864 | | ((void) usage); |
865 | | ((void) alg2); |
866 | | #else |
867 | | #if defined(MBEDTLS_ECP_C) |
868 | | if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) { |
869 | | const mbedtls_ecp_keypair *ec; |
870 | | unsigned char d[MBEDTLS_ECP_MAX_BYTES]; |
871 | | size_t d_len; |
872 | | psa_ecc_family_t curve_id; |
873 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
874 | | psa_key_type_t key_type; |
875 | | size_t bits; |
876 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
877 | | psa_status_t status; |
878 | | |
879 | | /* export the private key material in the format PSA wants */ |
880 | | ec = mbedtls_pk_ec(*pk); |
881 | | d_len = PSA_BITS_TO_BYTES(ec->grp.nbits); |
882 | | if ((ret = mbedtls_mpi_write_binary(&ec->d, d, d_len)) != 0) { |
883 | | return ret; |
884 | | } |
885 | | |
886 | | curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits); |
887 | | key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id); |
888 | | |
889 | | /* prepare the key attributes */ |
890 | | psa_set_key_type(&attributes, key_type); |
891 | | psa_set_key_bits(&attributes, bits); |
892 | | psa_set_key_usage_flags(&attributes, usage); |
893 | | psa_set_key_algorithm(&attributes, alg); |
894 | | if (alg2 != PSA_ALG_NONE) { |
895 | | psa_set_key_enrollment_algorithm(&attributes, alg2); |
896 | | } |
897 | | |
898 | | /* import private key into PSA */ |
899 | | status = psa_import_key(&attributes, d, d_len, key); |
900 | | if (status != PSA_SUCCESS) { |
901 | | return mbedtls_pk_error_from_psa(status); |
902 | | } |
903 | | |
904 | | /* make PK context wrap the key slot */ |
905 | | mbedtls_pk_free(pk); |
906 | | mbedtls_pk_init(pk); |
907 | | |
908 | | return mbedtls_pk_setup_opaque(pk, *key); |
909 | | } else |
910 | | #endif /* MBEDTLS_ECP_C */ |
911 | | #if defined(MBEDTLS_RSA_C) |
912 | | if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) { |
913 | | unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES]; |
914 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
915 | | int key_len; |
916 | | psa_status_t status; |
917 | | |
918 | | /* export the private key material in the format PSA wants */ |
919 | | key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf)); |
920 | | if (key_len <= 0) { |
921 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
922 | | } |
923 | | |
924 | | /* prepare the key attributes */ |
925 | | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR); |
926 | | psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk)); |
927 | | psa_set_key_usage_flags(&attributes, usage); |
928 | | psa_set_key_algorithm(&attributes, alg); |
929 | | if (alg2 != PSA_ALG_NONE) { |
930 | | psa_set_key_enrollment_algorithm(&attributes, alg2); |
931 | | } |
932 | | |
933 | | /* import private key into PSA */ |
934 | | status = psa_import_key(&attributes, |
935 | | buf + sizeof(buf) - key_len, |
936 | | key_len, key); |
937 | | |
938 | | mbedtls_platform_zeroize(buf, sizeof(buf)); |
939 | | |
940 | | if (status != PSA_SUCCESS) { |
941 | | return mbedtls_pk_error_from_psa(status); |
942 | | } |
943 | | |
944 | | /* make PK context wrap the key slot */ |
945 | | mbedtls_pk_free(pk); |
946 | | mbedtls_pk_init(pk); |
947 | | |
948 | | return mbedtls_pk_setup_opaque(pk, *key); |
949 | | } else |
950 | | #endif /* MBEDTLS_RSA_C */ |
951 | | #endif /* !MBEDTLS_ECP_C && !MBEDTLS_RSA_C */ |
952 | | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
953 | | } |
954 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
955 | | #endif /* MBEDTLS_PK_C */ |