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 "mbedtls/pk_internal.h"
25 
26 #include "mbedtls/platform_util.h"
27 #include "mbedtls/error.h"
28 
29 #if defined(MBEDTLS_RSA_C)
30 #include "mbedtls/rsa.h"
31 #endif
32 #if defined(MBEDTLS_ECP_C)
33 #include "mbedtls/ecp.h"
34 #endif
35 #if defined(MBEDTLS_ECDSA_C)
36 #include "mbedtls/ecdsa.h"
37 #endif
38 
39 #if defined(MBEDTLS_USE_PSA_CRYPTO)
40 #include "mbedtls/psa_util.h"
41 #endif
42 
43 #include <limits.h>
44 #include <stdint.h>
45 
46 /* Parameter validation macros based on platform_util.h */
47 #define PK_VALIDATE_RET( cond )    \
48     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
49 #define PK_VALIDATE( cond )        \
50     MBEDTLS_INTERNAL_VALIDATE( cond )
51 
52 /*
53  * Initialise a mbedtls_pk_context
54  */
mbedtls_pk_init(mbedtls_pk_context * ctx)55 void mbedtls_pk_init( mbedtls_pk_context *ctx )
56 {
57     PK_VALIDATE( ctx != NULL );
58 
59     ctx->pk_info = NULL;
60     ctx->pk_ctx = NULL;
61 }
62 
63 /*
64  * Free (the components of) a mbedtls_pk_context
65  */
mbedtls_pk_free(mbedtls_pk_context * ctx)66 void mbedtls_pk_free( mbedtls_pk_context *ctx )
67 {
68     if( ctx == NULL )
69         return;
70 
71     if ( ctx->pk_info != NULL )
72         ctx->pk_info->ctx_free_func( ctx->pk_ctx );
73 
74     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
75 }
76 
77 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
78 /*
79  * Initialize a restart context
80  */
mbedtls_pk_restart_init(mbedtls_pk_restart_ctx * ctx)81 void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
82 {
83     PK_VALIDATE( ctx != NULL );
84     ctx->pk_info = NULL;
85     ctx->rs_ctx = NULL;
86 }
87 
88 /*
89  * Free the components of a restart context
90  */
mbedtls_pk_restart_free(mbedtls_pk_restart_ctx * ctx)91 void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
92 {
93     if( ctx == NULL || ctx->pk_info == NULL ||
94         ctx->pk_info->rs_free_func == NULL )
95     {
96         return;
97     }
98 
99     ctx->pk_info->rs_free_func( ctx->rs_ctx );
100 
101     ctx->pk_info = NULL;
102     ctx->rs_ctx = NULL;
103 }
104 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
105 
106 /*
107  * Get pk_info structure from type
108  */
mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)109 const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
110 {
111     switch( pk_type ) {
112 #if defined(MBEDTLS_RSA_C)
113         case MBEDTLS_PK_RSA:
114             return( &mbedtls_rsa_info );
115 #endif
116 #if defined(MBEDTLS_ECP_C)
117         case MBEDTLS_PK_ECKEY:
118             return( &mbedtls_eckey_info );
119         case MBEDTLS_PK_ECKEY_DH:
120             return( &mbedtls_eckeydh_info );
121 #endif
122 #if defined(MBEDTLS_ECDSA_C)
123         case MBEDTLS_PK_ECDSA:
124             return( &mbedtls_ecdsa_info );
125 #endif
126         /* MBEDTLS_PK_RSA_ALT omitted on purpose */
127         default:
128             return( NULL );
129     }
130 }
131 
132 /*
133  * Initialise context
134  */
mbedtls_pk_setup(mbedtls_pk_context * ctx,const mbedtls_pk_info_t * info)135 int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
136 {
137     PK_VALIDATE_RET( ctx != NULL );
138     if( info == NULL || ctx->pk_info != NULL )
139         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
140 
141     if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
142         return( MBEDTLS_ERR_PK_ALLOC_FAILED );
143 
144     ctx->pk_info = info;
145 
146     return( 0 );
147 }
148 
149 #if defined(MBEDTLS_USE_PSA_CRYPTO)
150 /*
151  * Initialise a PSA-wrapping context
152  */
mbedtls_pk_setup_opaque(mbedtls_pk_context * ctx,const psa_key_id_t key)153 int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx,
154                              const psa_key_id_t key )
155 {
156     const mbedtls_pk_info_t * const info = &mbedtls_pk_opaque_info;
157     psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
158     psa_key_id_t *pk_ctx;
159     psa_key_type_t type;
160 
161     if( ctx == NULL || ctx->pk_info != NULL )
162         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
163 
164     if( PSA_SUCCESS != psa_get_key_attributes( key, &attributes ) )
165         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
166     type = psa_get_key_type( &attributes );
167     psa_reset_key_attributes( &attributes );
168 
169     /* Current implementation of can_do() relies on this. */
170     if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
171         return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE) ;
172 
173     if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
174         return( MBEDTLS_ERR_PK_ALLOC_FAILED );
175 
176     ctx->pk_info = info;
177 
178     pk_ctx = (psa_key_id_t *) ctx->pk_ctx;
179     *pk_ctx = key;
180 
181     return( 0 );
182 }
183 #endif /* MBEDTLS_USE_PSA_CRYPTO */
184 
185 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
186 /*
187  * Initialize an RSA-alt context
188  */
mbedtls_pk_setup_rsa_alt(mbedtls_pk_context * ctx,void * key,mbedtls_pk_rsa_alt_decrypt_func decrypt_func,mbedtls_pk_rsa_alt_sign_func sign_func,mbedtls_pk_rsa_alt_key_len_func key_len_func)189 int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
190                          mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
191                          mbedtls_pk_rsa_alt_sign_func sign_func,
192                          mbedtls_pk_rsa_alt_key_len_func key_len_func )
193 {
194     mbedtls_rsa_alt_context *rsa_alt;
195     const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
196 
197     PK_VALIDATE_RET( ctx != NULL );
198     if( ctx->pk_info != NULL )
199         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
200 
201     if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
202         return( MBEDTLS_ERR_PK_ALLOC_FAILED );
203 
204     ctx->pk_info = info;
205 
206     rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
207 
208     rsa_alt->key = key;
209     rsa_alt->decrypt_func = decrypt_func;
210     rsa_alt->sign_func = sign_func;
211     rsa_alt->key_len_func = key_len_func;
212 
213     return( 0 );
214 }
215 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
216 
217 /*
218  * Tell if a PK can do the operations of the given type
219  */
mbedtls_pk_can_do(const mbedtls_pk_context * ctx,mbedtls_pk_type_t type)220 int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
221 {
222     /* A context with null pk_info is not set up yet and can't do anything.
223      * For backward compatibility, also accept NULL instead of a context
224      * pointer. */
225     if( ctx == NULL || ctx->pk_info == NULL )
226         return( 0 );
227 
228     return( ctx->pk_info->can_do( type ) );
229 }
230 
231 /*
232  * Helper for mbedtls_pk_sign and mbedtls_pk_verify
233  */
pk_hashlen_helper(mbedtls_md_type_t md_alg,size_t * hash_len)234 static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
235 {
236     const mbedtls_md_info_t *md_info;
237 
238     if( *hash_len != 0 )
239         return( 0 );
240 
241     if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
242         return( -1 );
243 
244     *hash_len = mbedtls_md_get_size( md_info );
245     return( 0 );
246 }
247 
248 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
249 /*
250  * Helper to set up a restart context if needed
251  */
pk_restart_setup(mbedtls_pk_restart_ctx * ctx,const mbedtls_pk_info_t * info)252 static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
253                              const mbedtls_pk_info_t *info )
254 {
255     /* Don't do anything if already set up or invalid */
256     if( ctx == NULL || ctx->pk_info != NULL )
257         return( 0 );
258 
259     /* Should never happen when we're called */
260     if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
261         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
262 
263     if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
264         return( MBEDTLS_ERR_PK_ALLOC_FAILED );
265 
266     ctx->pk_info = info;
267 
268     return( 0 );
269 }
270 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
271 
272 /*
273  * Verify a signature (restartable)
274  */
mbedtls_pk_verify_restartable(mbedtls_pk_context * ctx,mbedtls_md_type_t md_alg,const unsigned char * hash,size_t hash_len,const unsigned char * sig,size_t sig_len,mbedtls_pk_restart_ctx * rs_ctx)275 int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
276                mbedtls_md_type_t md_alg,
277                const unsigned char *hash, size_t hash_len,
278                const unsigned char *sig, size_t sig_len,
279                mbedtls_pk_restart_ctx *rs_ctx )
280 {
281     PK_VALIDATE_RET( ctx != NULL );
282     PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
283                      hash != NULL );
284     PK_VALIDATE_RET( sig != NULL );
285 
286     if( ctx->pk_info == NULL ||
287         pk_hashlen_helper( md_alg, &hash_len ) != 0 )
288         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
289 
290 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
291     /* optimization: use non-restartable version if restart disabled */
292     if( rs_ctx != NULL &&
293         mbedtls_ecp_restart_is_enabled() &&
294         ctx->pk_info->verify_rs_func != NULL )
295     {
296         int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
297 
298         if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
299             return( ret );
300 
301         ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
302                    md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
303 
304         if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
305             mbedtls_pk_restart_free( rs_ctx );
306 
307         return( ret );
308     }
309 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
310     (void) rs_ctx;
311 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
312 
313     if( ctx->pk_info->verify_func == NULL )
314         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
315 
316     return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
317                                        sig, sig_len ) );
318 }
319 
320 /*
321  * Verify a signature
322  */
mbedtls_pk_verify(mbedtls_pk_context * ctx,mbedtls_md_type_t md_alg,const unsigned char * hash,size_t hash_len,const unsigned char * sig,size_t sig_len)323 int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
324                const unsigned char *hash, size_t hash_len,
325                const unsigned char *sig, size_t sig_len )
326 {
327     return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
328                                            sig, sig_len, NULL ) );
329 }
330 
331 /*
332  * Verify a signature with options
333  */
mbedtls_pk_verify_ext(mbedtls_pk_type_t type,const void * options,mbedtls_pk_context * ctx,mbedtls_md_type_t md_alg,const unsigned char * hash,size_t hash_len,const unsigned char * sig,size_t sig_len)334 int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
335                    mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
336                    const unsigned char *hash, size_t hash_len,
337                    const unsigned char *sig, size_t sig_len )
338 {
339     PK_VALIDATE_RET( ctx != NULL );
340     PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
341                      hash != NULL );
342     PK_VALIDATE_RET( sig != NULL );
343 
344     if( ctx->pk_info == NULL )
345         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
346 
347     if( ! mbedtls_pk_can_do( ctx, type ) )
348         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
349 
350     if( type == MBEDTLS_PK_RSASSA_PSS )
351     {
352 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
353         int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
354         const mbedtls_pk_rsassa_pss_options *pss_opts;
355 
356 #if SIZE_MAX > UINT_MAX
357         if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
358             return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
359 #endif /* SIZE_MAX > UINT_MAX */
360 
361         if( options == NULL )
362             return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
363 
364         pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
365 
366         if( sig_len < mbedtls_pk_get_len( ctx ) )
367             return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
368 
369         ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
370                 NULL, NULL, MBEDTLS_RSA_PUBLIC,
371                 md_alg, (unsigned int) hash_len, hash,
372                 pss_opts->mgf1_hash_id,
373                 pss_opts->expected_salt_len,
374                 sig );
375         if( ret != 0 )
376             return( ret );
377 
378         if( sig_len > mbedtls_pk_get_len( ctx ) )
379             return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
380 
381         return( 0 );
382 #else
383         return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
384 #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
385     }
386 
387     /* General case: no options */
388     if( options != NULL )
389         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
390 
391     return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
392 }
393 
394 /*
395  * Make a signature (restartable)
396  */
mbedtls_pk_sign_restartable(mbedtls_pk_context * ctx,mbedtls_md_type_t md_alg,const unsigned char * hash,size_t hash_len,unsigned char * sig,size_t * sig_len,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng,mbedtls_pk_restart_ctx * rs_ctx)397 int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
398              mbedtls_md_type_t md_alg,
399              const unsigned char *hash, size_t hash_len,
400              unsigned char *sig, size_t *sig_len,
401              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
402              mbedtls_pk_restart_ctx *rs_ctx )
403 {
404     PK_VALIDATE_RET( ctx != NULL );
405     PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
406                      hash != NULL );
407     PK_VALIDATE_RET( sig != NULL );
408 
409     if( ctx->pk_info == NULL ||
410         pk_hashlen_helper( md_alg, &hash_len ) != 0 )
411         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
412 
413 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
414     /* optimization: use non-restartable version if restart disabled */
415     if( rs_ctx != NULL &&
416         mbedtls_ecp_restart_is_enabled() &&
417         ctx->pk_info->sign_rs_func != NULL )
418     {
419         int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
420 
421         if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
422             return( ret );
423 
424         ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
425                 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
426 
427         if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
428             mbedtls_pk_restart_free( rs_ctx );
429 
430         return( ret );
431     }
432 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
433     (void) rs_ctx;
434 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
435 
436     if( ctx->pk_info->sign_func == NULL )
437         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
438 
439     return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
440                                      sig, sig_len, f_rng, p_rng ) );
441 }
442 
443 /*
444  * Make a signature
445  */
mbedtls_pk_sign(mbedtls_pk_context * ctx,mbedtls_md_type_t md_alg,const unsigned char * hash,size_t hash_len,unsigned char * sig,size_t * sig_len,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)446 int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
447              const unsigned char *hash, size_t hash_len,
448              unsigned char *sig, size_t *sig_len,
449              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
450 {
451     return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
452                                          sig, sig_len, f_rng, p_rng, NULL ) );
453 }
454 
455 /*
456  * Decrypt message
457  */
mbedtls_pk_decrypt(mbedtls_pk_context * ctx,const unsigned char * input,size_t ilen,unsigned char * output,size_t * olen,size_t osize,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)458 int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
459                 const unsigned char *input, size_t ilen,
460                 unsigned char *output, size_t *olen, size_t osize,
461                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
462 {
463     PK_VALIDATE_RET( ctx != NULL );
464     PK_VALIDATE_RET( input != NULL || ilen == 0 );
465     PK_VALIDATE_RET( output != NULL || osize == 0 );
466     PK_VALIDATE_RET( olen != NULL );
467 
468     if( ctx->pk_info == NULL )
469         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
470 
471     if( ctx->pk_info->decrypt_func == NULL )
472         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
473 
474     return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
475                 output, olen, osize, f_rng, p_rng ) );
476 }
477 
478 /*
479  * Encrypt message
480  */
mbedtls_pk_encrypt(mbedtls_pk_context * ctx,const unsigned char * input,size_t ilen,unsigned char * output,size_t * olen,size_t osize,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)481 int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
482                 const unsigned char *input, size_t ilen,
483                 unsigned char *output, size_t *olen, size_t osize,
484                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
485 {
486     PK_VALIDATE_RET( ctx != NULL );
487     PK_VALIDATE_RET( input != NULL || ilen == 0 );
488     PK_VALIDATE_RET( output != NULL || osize == 0 );
489     PK_VALIDATE_RET( olen != NULL );
490 
491     if( ctx->pk_info == NULL )
492         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
493 
494     if( ctx->pk_info->encrypt_func == NULL )
495         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
496 
497     return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
498                 output, olen, osize, f_rng, p_rng ) );
499 }
500 
501 /*
502  * Check public-private key pair
503  */
mbedtls_pk_check_pair(const mbedtls_pk_context * pub,const mbedtls_pk_context * prv)504 int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
505 {
506     PK_VALIDATE_RET( pub != NULL );
507     PK_VALIDATE_RET( prv != NULL );
508 
509     if( pub->pk_info == NULL ||
510         prv->pk_info == NULL )
511     {
512         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
513     }
514 
515     if( prv->pk_info->check_pair_func == NULL )
516         return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
517 
518     if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
519     {
520         if( pub->pk_info->type != MBEDTLS_PK_RSA )
521             return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
522     }
523     else
524     {
525         if( pub->pk_info != prv->pk_info )
526             return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
527     }
528 
529     return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
530 }
531 
532 /*
533  * Get key size in bits
534  */
mbedtls_pk_get_bitlen(const mbedtls_pk_context * ctx)535 size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
536 {
537     /* For backward compatibility, accept NULL or a context that
538      * isn't set up yet, and return a fake value that should be safe. */
539     if( ctx == NULL || ctx->pk_info == NULL )
540         return( 0 );
541 
542     return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
543 }
544 
545 /*
546  * Export debug information
547  */
mbedtls_pk_debug(const mbedtls_pk_context * ctx,mbedtls_pk_debug_item * items)548 int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
549 {
550     PK_VALIDATE_RET( ctx != NULL );
551     if( ctx->pk_info == NULL )
552         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
553 
554     if( ctx->pk_info->debug_func == NULL )
555         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
556 
557     ctx->pk_info->debug_func( ctx->pk_ctx, items );
558     return( 0 );
559 }
560 
561 /*
562  * Access the PK type name
563  */
mbedtls_pk_get_name(const mbedtls_pk_context * ctx)564 const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
565 {
566     if( ctx == NULL || ctx->pk_info == NULL )
567         return( "invalid PK" );
568 
569     return( ctx->pk_info->name );
570 }
571 
572 /*
573  * Access the PK type
574  */
mbedtls_pk_get_type(const mbedtls_pk_context * ctx)575 mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
576 {
577     if( ctx == NULL || ctx->pk_info == NULL )
578         return( MBEDTLS_PK_NONE );
579 
580     return( ctx->pk_info->type );
581 }
582 
583 #if defined(MBEDTLS_USE_PSA_CRYPTO)
584 /*
585  * Load the key to a PSA key slot,
586  * then turn the PK context into a wrapper for that key slot.
587  *
588  * Currently only works for EC private keys.
589  */
mbedtls_pk_wrap_as_opaque(mbedtls_pk_context * pk,psa_key_id_t * key,psa_algorithm_t hash_alg)590 int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
591                                psa_key_id_t *key,
592                                psa_algorithm_t hash_alg )
593 {
594 #if !defined(MBEDTLS_ECP_C)
595     ((void) pk);
596     ((void) key);
597     ((void) hash_alg);
598     return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
599 #else
600     const mbedtls_ecp_keypair *ec;
601     unsigned char d[MBEDTLS_ECP_MAX_BYTES];
602     size_t d_len;
603     psa_ecc_family_t curve_id;
604     psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
605     psa_key_type_t key_type;
606     size_t bits;
607     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
608 
609     /* export the private key material in the format PSA wants */
610     if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
611         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
612 
613     ec = mbedtls_pk_ec( *pk );
614     d_len = ( ec->grp.nbits + 7 ) / 8;
615     if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 )
616         return( ret );
617 
618     curve_id = mbedtls_ecc_group_to_psa( ec->grp.id, &bits );
619     key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve_id );
620 
621     /* prepare the key attributes */
622     psa_set_key_type( &attributes, key_type );
623     psa_set_key_bits( &attributes, bits );
624     psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
625     psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(hash_alg) );
626 
627     /* import private key into PSA */
628     if( PSA_SUCCESS != psa_import_key( &attributes, d, d_len, key ) )
629         return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
630 
631     /* make PK context wrap the key slot */
632     mbedtls_pk_free( pk );
633     mbedtls_pk_init( pk );
634 
635     return( mbedtls_pk_setup_opaque( pk, *key ) );
636 #endif /* MBEDTLS_ECP_C */
637 }
638 #endif /* MBEDTLS_USE_PSA_CRYPTO */
639 #endif /* MBEDTLS_PK_C */
640