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 && md_alg == MBEDTLS_MD_NONE )
239         return( 0 );
240 
241     if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
242         return( -1 );
243 
244     if ( *hash_len != 0 && *hash_len != mbedtls_md_get_size( md_info ) )
245         return ( -1 );
246 
247     *hash_len = mbedtls_md_get_size( md_info );
248     return( 0 );
249 }
250 
251 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
252 /*
253  * Helper to set up a restart context if needed
254  */
pk_restart_setup(mbedtls_pk_restart_ctx * ctx,const mbedtls_pk_info_t * info)255 static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
256                              const mbedtls_pk_info_t *info )
257 {
258     /* Don't do anything if already set up or invalid */
259     if( ctx == NULL || ctx->pk_info != NULL )
260         return( 0 );
261 
262     /* Should never happen when we're called */
263     if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
264         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
265 
266     if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
267         return( MBEDTLS_ERR_PK_ALLOC_FAILED );
268 
269     ctx->pk_info = info;
270 
271     return( 0 );
272 }
273 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
274 
275 /*
276  * Verify a signature (restartable)
277  */
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)278 int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
279                mbedtls_md_type_t md_alg,
280                const unsigned char *hash, size_t hash_len,
281                const unsigned char *sig, size_t sig_len,
282                mbedtls_pk_restart_ctx *rs_ctx )
283 {
284     PK_VALIDATE_RET( ctx != NULL );
285     PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
286                      hash != NULL );
287     PK_VALIDATE_RET( sig != NULL );
288 
289     if( ctx->pk_info == NULL ||
290         pk_hashlen_helper( md_alg, &hash_len ) != 0 )
291         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
292 
293 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
294     /* optimization: use non-restartable version if restart disabled */
295     if( rs_ctx != NULL &&
296         mbedtls_ecp_restart_is_enabled() &&
297         ctx->pk_info->verify_rs_func != NULL )
298     {
299         int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
300 
301         if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
302             return( ret );
303 
304         ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
305                    md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
306 
307         if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
308             mbedtls_pk_restart_free( rs_ctx );
309 
310         return( ret );
311     }
312 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
313     (void) rs_ctx;
314 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
315 
316     if( ctx->pk_info->verify_func == NULL )
317         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
318 
319     return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
320                                        sig, sig_len ) );
321 }
322 
323 /*
324  * Verify a signature
325  */
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)326 int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
327                const unsigned char *hash, size_t hash_len,
328                const unsigned char *sig, size_t sig_len )
329 {
330     return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
331                                            sig, sig_len, NULL ) );
332 }
333 
334 /*
335  * Verify a signature with options
336  */
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)337 int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
338                    mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
339                    const unsigned char *hash, size_t hash_len,
340                    const unsigned char *sig, size_t sig_len )
341 {
342     PK_VALIDATE_RET( ctx != NULL );
343     PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
344                      hash != NULL );
345     PK_VALIDATE_RET( sig != NULL );
346 
347     if( ctx->pk_info == NULL )
348         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
349 
350     if( ! mbedtls_pk_can_do( ctx, type ) )
351         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
352 
353     if( type == MBEDTLS_PK_RSASSA_PSS )
354     {
355 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
356         int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
357         const mbedtls_pk_rsassa_pss_options *pss_opts;
358 
359 #if SIZE_MAX > UINT_MAX
360         if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
361             return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
362 #endif /* SIZE_MAX > UINT_MAX */
363 
364         if( options == NULL )
365             return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
366 
367         pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
368 
369         if( sig_len < mbedtls_pk_get_len( ctx ) )
370             return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
371 
372         ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
373                 NULL, NULL, MBEDTLS_RSA_PUBLIC,
374                 md_alg, (unsigned int) hash_len, hash,
375                 pss_opts->mgf1_hash_id,
376                 pss_opts->expected_salt_len,
377                 sig );
378         if( ret != 0 )
379             return( ret );
380 
381         if( sig_len > mbedtls_pk_get_len( ctx ) )
382             return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
383 
384         return( 0 );
385 #else
386         return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
387 #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
388     }
389 
390     /* General case: no options */
391     if( options != NULL )
392         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
393 
394     return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
395 }
396 
397 /*
398  * Make a signature (restartable)
399  */
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)400 int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
401              mbedtls_md_type_t md_alg,
402              const unsigned char *hash, size_t hash_len,
403              unsigned char *sig, size_t *sig_len,
404              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
405              mbedtls_pk_restart_ctx *rs_ctx )
406 {
407     PK_VALIDATE_RET( ctx != NULL );
408     PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
409                      hash != NULL );
410     PK_VALIDATE_RET( sig != NULL );
411 
412     if( ctx->pk_info == NULL ||
413         pk_hashlen_helper( md_alg, &hash_len ) != 0 )
414         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
415 
416 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
417     /* optimization: use non-restartable version if restart disabled */
418     if( rs_ctx != NULL &&
419         mbedtls_ecp_restart_is_enabled() &&
420         ctx->pk_info->sign_rs_func != NULL )
421     {
422         int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
423 
424         if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
425             return( ret );
426 
427         ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
428                 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
429 
430         if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
431             mbedtls_pk_restart_free( rs_ctx );
432 
433         return( ret );
434     }
435 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
436     (void) rs_ctx;
437 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
438 
439     if( ctx->pk_info->sign_func == NULL )
440         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
441 
442     return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
443                                      sig, sig_len, f_rng, p_rng ) );
444 }
445 
446 /*
447  * Make a signature
448  */
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)449 int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
450              const unsigned char *hash, size_t hash_len,
451              unsigned char *sig, size_t *sig_len,
452              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
453 {
454     return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
455                                          sig, sig_len, f_rng, p_rng, NULL ) );
456 }
457 
458 /*
459  * Decrypt message
460  */
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)461 int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
462                 const unsigned char *input, size_t ilen,
463                 unsigned char *output, size_t *olen, size_t osize,
464                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
465 {
466     PK_VALIDATE_RET( ctx != NULL );
467     PK_VALIDATE_RET( input != NULL || ilen == 0 );
468     PK_VALIDATE_RET( output != NULL || osize == 0 );
469     PK_VALIDATE_RET( olen != NULL );
470 
471     if( ctx->pk_info == NULL )
472         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
473 
474     if( ctx->pk_info->decrypt_func == NULL )
475         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
476 
477     return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
478                 output, olen, osize, f_rng, p_rng ) );
479 }
480 
481 /*
482  * Encrypt message
483  */
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)484 int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
485                 const unsigned char *input, size_t ilen,
486                 unsigned char *output, size_t *olen, size_t osize,
487                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
488 {
489     PK_VALIDATE_RET( ctx != NULL );
490     PK_VALIDATE_RET( input != NULL || ilen == 0 );
491     PK_VALIDATE_RET( output != NULL || osize == 0 );
492     PK_VALIDATE_RET( olen != NULL );
493 
494     if( ctx->pk_info == NULL )
495         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
496 
497     if( ctx->pk_info->encrypt_func == NULL )
498         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
499 
500     return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
501                 output, olen, osize, f_rng, p_rng ) );
502 }
503 
504 /*
505  * Check public-private key pair
506  */
mbedtls_pk_check_pair(const mbedtls_pk_context * pub,const mbedtls_pk_context * prv)507 int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
508 {
509     PK_VALIDATE_RET( pub != NULL );
510     PK_VALIDATE_RET( prv != NULL );
511 
512     if( pub->pk_info == NULL ||
513         prv->pk_info == NULL )
514     {
515         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
516     }
517 
518     if( prv->pk_info->check_pair_func == NULL )
519         return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
520 
521     if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
522     {
523         if( pub->pk_info->type != MBEDTLS_PK_RSA )
524             return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
525     }
526     else
527     {
528         if( pub->pk_info != prv->pk_info )
529             return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
530     }
531 
532     return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
533 }
534 
535 /*
536  * Get key size in bits
537  */
mbedtls_pk_get_bitlen(const mbedtls_pk_context * ctx)538 size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
539 {
540     /* For backward compatibility, accept NULL or a context that
541      * isn't set up yet, and return a fake value that should be safe. */
542     if( ctx == NULL || ctx->pk_info == NULL )
543         return( 0 );
544 
545     return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
546 }
547 
548 /*
549  * Export debug information
550  */
mbedtls_pk_debug(const mbedtls_pk_context * ctx,mbedtls_pk_debug_item * items)551 int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
552 {
553     PK_VALIDATE_RET( ctx != NULL );
554     if( ctx->pk_info == NULL )
555         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
556 
557     if( ctx->pk_info->debug_func == NULL )
558         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
559 
560     ctx->pk_info->debug_func( ctx->pk_ctx, items );
561     return( 0 );
562 }
563 
564 /*
565  * Access the PK type name
566  */
mbedtls_pk_get_name(const mbedtls_pk_context * ctx)567 const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
568 {
569     if( ctx == NULL || ctx->pk_info == NULL )
570         return( "invalid PK" );
571 
572     return( ctx->pk_info->name );
573 }
574 
575 /*
576  * Access the PK type
577  */
mbedtls_pk_get_type(const mbedtls_pk_context * ctx)578 mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
579 {
580     if( ctx == NULL || ctx->pk_info == NULL )
581         return( MBEDTLS_PK_NONE );
582 
583     return( ctx->pk_info->type );
584 }
585 
586 #if defined(MBEDTLS_USE_PSA_CRYPTO)
587 /*
588  * Load the key to a PSA key slot,
589  * then turn the PK context into a wrapper for that key slot.
590  *
591  * Currently only works for EC private keys.
592  */
mbedtls_pk_wrap_as_opaque(mbedtls_pk_context * pk,psa_key_id_t * key,psa_algorithm_t hash_alg)593 int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
594                                psa_key_id_t *key,
595                                psa_algorithm_t hash_alg )
596 {
597 #if !defined(MBEDTLS_ECP_C)
598     ((void) pk);
599     ((void) key);
600     ((void) hash_alg);
601     return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
602 #else
603     const mbedtls_ecp_keypair *ec;
604     unsigned char d[MBEDTLS_ECP_MAX_BYTES];
605     size_t d_len;
606     psa_ecc_family_t curve_id;
607     psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
608     psa_key_type_t key_type;
609     size_t bits;
610     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
611 
612     /* export the private key material in the format PSA wants */
613     if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
614         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
615 
616     ec = mbedtls_pk_ec( *pk );
617     d_len = ( ec->grp.nbits + 7 ) / 8;
618     if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 )
619         return( ret );
620 
621     curve_id = mbedtls_ecc_group_to_psa( ec->grp.id, &bits );
622     key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve_id );
623 
624     /* prepare the key attributes */
625     psa_set_key_type( &attributes, key_type );
626     psa_set_key_bits( &attributes, bits );
627     psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
628     psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(hash_alg) );
629 
630     /* import private key into PSA */
631     if( PSA_SUCCESS != psa_import_key( &attributes, d, d_len, key ) )
632         return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
633 
634     /* make PK context wrap the key slot */
635     mbedtls_pk_free( pk );
636     mbedtls_pk_init( pk );
637 
638     return( mbedtls_pk_setup_opaque( pk, *key ) );
639 #endif /* MBEDTLS_ECP_C */
640 }
641 #endif /* MBEDTLS_USE_PSA_CRYPTO */
642 #endif /* MBEDTLS_PK_C */
643