blob: 9ded7e06dc9e5d5588e9a35fb30d9853daf3ff08 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020030#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020031
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050032#include "mbedtls/platform_util.h"
Andres AG72849872017-01-19 11:24:33 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020042#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020043
Andres AG72849872017-01-19 11:24:33 +000044#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010045#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020046
Gilles Peskinee97dc602018-12-19 00:51:38 +010047/* Parameter validation macros based on platform_util.h */
48#define PK_VALIDATE_RET( cond ) \
49 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
50#define PK_VALIDATE( cond ) \
51 MBEDTLS_INTERNAL_VALIDATE( cond )
52
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053/*
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +020054 * Access to members of the pk_info structure. These are meant to be replaced
55 * by zero-runtime-cost accessors when a single PK type is hardcoded.
56 *
57 * For function members, don't make a getter, but a function that directly
58 * calls the method, so that we can entirely get rid of function pointers
59 * when hardcoding a single PK - some compilers optimize better that way.
60 *
61 * Not implemented for members that are only present in builds with
62 * MBEDTLS_ECP_RESTARTABLE for now, as the main target for hardcoded is builds
63 * with MBEDTLS_USE_TINYCRYPT, which don't have MBEDTLS_ECP_RESTARTABLE.
64 */
65
66MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
67 const mbedtls_pk_info_t *info )
68{
69 return( info->type );
70}
71
72MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
73 const mbedtls_pk_info_t *info )
74{
75 return( info->name );
76}
77
78MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
79 const mbedtls_pk_info_t *info, const void *ctx )
80{
81 return( info->get_bitlen( ctx ) );
82}
83
84MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
85 const mbedtls_pk_info_t *info, mbedtls_pk_type_t type )
86{
87 return( info->can_do( type ) );
88}
89
90MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
91 const mbedtls_pk_info_t *info, void *ctx, mbedtls_md_type_t md_alg,
92 const unsigned char *hash, size_t hash_len,
93 const unsigned char *sig, size_t sig_len )
94{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020095 if( info->verify_func == NULL )
96 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
97
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +020098 return( info->verify_func( ctx, md_alg, hash, hash_len, sig, sig_len ) );
99}
100
101MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
102 const mbedtls_pk_info_t *info, void *ctx, mbedtls_md_type_t md_alg,
103 const unsigned char *hash, size_t hash_len,
104 unsigned char *sig, size_t *sig_len,
105 int (*f_rng)(void *, unsigned char *, size_t),
106 void *p_rng )
107{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +0200108 if( info->sign_func == NULL )
109 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
110
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200111 return( info->sign_func( ctx, md_alg, hash, hash_len, sig, sig_len,
112 f_rng, p_rng ) );
113}
114
115MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
116 const mbedtls_pk_info_t *info, void *ctx,
117 const unsigned char *input, size_t ilen,
118 unsigned char *output, size_t *olen, size_t osize,
119 int (*f_rng)(void *, unsigned char *, size_t),
120 void *p_rng )
121{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +0200122 if( info->decrypt_func == NULL )
123 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
124
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200125 return( info->decrypt_func( ctx, input, ilen, output, olen, osize,
126 f_rng, p_rng ) );
127}
128
129MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
130 const mbedtls_pk_info_t *info, void *ctx,
131 const unsigned char *input, size_t ilen,
132 unsigned char *output, size_t *olen, size_t osize,
133 int (*f_rng)(void *, unsigned char *, size_t),
134 void *p_rng )
135{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +0200136 if( info->encrypt_func == NULL )
137 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
138
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200139 return( info->encrypt_func( ctx, input, ilen, output, olen, osize,
140 f_rng, p_rng ) );
141}
142
143MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
144 const mbedtls_pk_info_t *info, const void *pub, const void *prv )
145{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +0200146 if( info->check_pair_func == NULL )
147 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
148
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200149 return( info->check_pair_func( pub, prv ) );
150}
151
152MBEDTLS_ALWAYS_INLINE static inline void *pk_info_ctx_alloc_func(
153 const mbedtls_pk_info_t *info )
154{
155 return( info->ctx_alloc_func( ) );
156}
157
158MBEDTLS_ALWAYS_INLINE static inline void pk_info_ctx_free_func(
159 const mbedtls_pk_info_t *info, void *ctx )
160{
161 info->ctx_free_func( ctx );
162}
163
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +0200164MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200165 const mbedtls_pk_info_t *info,
166 const void *ctx, mbedtls_pk_debug_item *items )
167{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +0200168 if( info->debug_func == NULL )
169 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
170
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200171 info->debug_func( ctx, items );
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +0200172 return( 0 );
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200173}
174
175/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200179{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100180 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200181
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200182 ctx->pk_info = NULL;
183 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200184}
185
186/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200190{
irwir2239a862018-06-12 18:25:09 +0300191 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200192 return;
193
irwir2239a862018-06-12 18:25:09 +0300194 if ( ctx->pk_info != NULL )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200195 pk_info_ctx_free_func( ctx->pk_info, ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +0200196
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500197 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200198}
199
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200200#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200201/*
202 * Initialize a restart context
203 */
204void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
205{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100206 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200207 ctx->pk_info = NULL;
208 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200209}
210
211/*
212 * Free the components of a restart context
213 */
214void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
215{
Gilles Peskine1f19fa62018-12-19 14:18:39 +0100216 if( ctx == NULL || ctx->pk_info == NULL ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200217 ctx->pk_info->rs_free_func == NULL )
218 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200219 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200220 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200221
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200222 ctx->pk_info->rs_free_func( ctx->rs_ctx );
223
224 ctx->pk_info = NULL;
225 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200226}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200227#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200228
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200229/*
230 * Get pk_info structure from type
231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200233{
234 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_RSA_C)
236 case MBEDTLS_PK_RSA:
237 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 case MBEDTLS_PK_ECKEY_DH:
241 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200242#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_ECDSA_C)
244 case MBEDTLS_PK_ECDSA:
245 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200246#endif
Hanno Beckeradf11e12019-08-21 13:03:44 +0100247#if defined(MBEDTLS_USE_TINYCRYPT)
248 case MBEDTLS_PK_ECKEY:
249 return( &mbedtls_uecc_eckey_info );
250#else /* MBEDTLS_USE_TINYCRYPT */
251#if defined(MBEDTLS_ECP_C)
252 case MBEDTLS_PK_ECKEY:
253 return( &mbedtls_eckey_info );
254#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300255#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200257 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200258 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200259 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200260}
261
262/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200263 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200264 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200265int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200266{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100267 PK_VALIDATE_RET( ctx != NULL );
268 if( info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200270
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200271 if( ( ctx->pk_ctx = pk_info_ctx_alloc_func( info ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200272 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200273
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200274 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200275
276 return( 0 );
277}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100280/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200281 * Initialize an RSA-alt context
282 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200283int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
285 mbedtls_pk_rsa_alt_sign_func sign_func,
286 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200287{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_rsa_alt_context *rsa_alt;
289 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200290
Gilles Peskinee97dc602018-12-19 00:51:38 +0100291 PK_VALIDATE_RET( ctx != NULL );
292 if( ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200294
295 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200296 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200297
298 ctx->pk_info = info;
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200301
302 rsa_alt->key = key;
303 rsa_alt->decrypt_func = decrypt_func;
304 rsa_alt->sign_func = sign_func;
305 rsa_alt->key_len_func = key_len_func;
306
307 return( 0 );
308}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200310
311/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200312 * Tell if a PK can do the operations of the given type
313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200315{
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100316 /* A context with null pk_info is not set up yet and can't do anything.
317 * For backward compatibility, also accept NULL instead of a context
318 * pointer. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200319 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200320 return( 0 );
321
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200322 return( pk_info_can_do( ctx->pk_info, type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200323}
324
325/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200329{
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100330 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200331
332 if( *hash_len != 0 )
333 return( 0 );
334
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100335 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) ==
336 MBEDTLS_MD_INVALID_HANDLE )
337 {
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200338 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100339 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200342 return( 0 );
343}
344
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200345#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200346/*
347 * Helper to set up a restart context if needed
348 */
349static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200350 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200351{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200352 /* Don't do anything if already set up or invalid */
353 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200354 return( 0 );
355
356 /* Should never happen when we're called */
357 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
358 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
359
360 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
361 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
362
363 ctx->pk_info = info;
364
365 return( 0 );
366}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200367#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200368
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200369/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200370 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200371 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200372int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
373 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200374 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200375 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200376 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200377{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100378 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100379 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
380 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100381 PK_VALIDATE_RET( sig != NULL );
382
383 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200384 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200386
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200387#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200388 /* optimization: use non-restartable version if restart disabled */
389 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200390 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200391 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200392 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200393 int ret;
394
395 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
396 return( ret );
397
398 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
399 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
400
401 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
402 mbedtls_pk_restart_free( rs_ctx );
403
404 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200405 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200406#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200407 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200408#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200409
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200410 return( pk_info_verify_func( ctx->pk_info, ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200411 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200412}
413
414/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200415 * Verify a signature
416 */
417int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
418 const unsigned char *hash, size_t hash_len,
419 const unsigned char *sig, size_t sig_len )
420{
421 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
422 sig, sig_len, NULL ) );
423}
424
425/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200426 * Verify a signature with options
427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
429 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200430 const unsigned char *hash, size_t hash_len,
431 const unsigned char *sig, size_t sig_len )
432{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100433 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100434 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
435 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100436 PK_VALIDATE_RET( sig != NULL );
437
438 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 if( ! mbedtls_pk_can_do( ctx, type ) )
442 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200447 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200449
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100450#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000451 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
452 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100453#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000454
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200455 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( sig_len < mbedtls_pk_get_len( ctx ) )
461 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
464 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200465 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200466 pss_opts->mgf1_hash_id,
467 pss_opts->expected_salt_len,
468 sig );
469 if( ret != 0 )
470 return( ret );
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 if( sig_len > mbedtls_pk_get_len( ctx ) )
473 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200474
475 return( 0 );
476#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000478#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200479 }
480
481 /* General case: no options */
482 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200486}
487
488/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200489 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200490 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200491int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
492 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200493 const unsigned char *hash, size_t hash_len,
494 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200495 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200496 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200497{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100498 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100499 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
500 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100501 PK_VALIDATE_RET( sig != NULL );
502
503 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200504 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200506
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200507#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200508 /* optimization: use non-restartable version if restart disabled */
509 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200510 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200511 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200512 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200513 int ret;
514
515 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
516 return( ret );
517
518 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
519 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
520
521 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
522 mbedtls_pk_restart_free( rs_ctx );
523
524 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200525 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200526#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200527 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200528#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200529
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200530 return( pk_info_sign_func( ctx->pk_info, ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200531 sig, sig_len, f_rng, p_rng ) );
532}
533
534/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200535 * Make a signature
536 */
537int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
538 const unsigned char *hash, size_t hash_len,
539 unsigned char *sig, size_t *sig_len,
540 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
541{
542 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
543 sig, sig_len, f_rng, p_rng, NULL ) );
544}
545
546/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200547 * Decrypt message
548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200550 const unsigned char *input, size_t ilen,
551 unsigned char *output, size_t *olen, size_t osize,
552 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
553{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100554 PK_VALIDATE_RET( ctx != NULL );
555 PK_VALIDATE_RET( input != NULL || ilen == 0 );
556 PK_VALIDATE_RET( output != NULL || osize == 0 );
557 PK_VALIDATE_RET( olen != NULL );
558
559 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200561
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200562 return( pk_info_decrypt_func( ctx->pk_info, ctx->pk_ctx, input, ilen,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200563 output, olen, osize, f_rng, p_rng ) );
564}
565
566/*
567 * Encrypt message
568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200570 const unsigned char *input, size_t ilen,
571 unsigned char *output, size_t *olen, size_t osize,
572 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
573{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100574 PK_VALIDATE_RET( ctx != NULL );
575 PK_VALIDATE_RET( input != NULL || ilen == 0 );
576 PK_VALIDATE_RET( output != NULL || osize == 0 );
577 PK_VALIDATE_RET( olen != NULL );
578
579 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200581
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200582 return( pk_info_encrypt_func( ctx->pk_info, ctx->pk_ctx, input, ilen,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200583 output, olen, osize, f_rng, p_rng ) );
584}
585
586/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100587 * Check public-private key pair
588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100590{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100591 PK_VALIDATE_RET( pub != NULL );
592 PK_VALIDATE_RET( prv != NULL );
593
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +0200594 if( pub->pk_info == NULL || prv->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100596
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +0200597#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200598 if( pk_info_type( prv->pk_info ) == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100599 {
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200600 if( pk_info_type( pub->pk_info ) != MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100602 }
603 else
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +0200604#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100605 {
606 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100608 }
609
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200610 return( pk_info_check_pair_func( prv->pk_info, pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100611}
612
613/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200614 * Get key size in bits
615 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200616size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200617{
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100618 /* For backward compatibility, accept NULL or a context that
619 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200620 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200621 return( 0 );
622
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200623 return( pk_info_get_bitlen( ctx->pk_info, ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200624}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200625
626/*
627 * Export debug information
628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200630{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100631 PK_VALIDATE_RET( ctx != NULL );
632 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200634
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +0200635 return( pk_info_debug_func( ctx->pk_info, ctx->pk_ctx, items ) );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200636}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200637
638/*
639 * Access the PK type name
640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200642{
643 if( ctx == NULL || ctx->pk_info == NULL )
644 return( "invalid PK" );
645
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200646 return( pk_info_name( ctx->pk_info ) );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200647}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200648
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200649/*
650 * Access the PK type
651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200653{
654 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200656
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +0200657 return( pk_info_type( ctx->pk_info ) );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200658}
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#endif /* MBEDTLS_PK_C */