Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key abstraction layer |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PK_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/pk.h" |
Manuel Pégourié-Gonnard | 50518f4 | 2015-05-26 11:04:15 +0200 | [diff] [blame] | 30 | #include "mbedtls/pk_internal.h" |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 8cd2889 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 33 | #include "mbedtls/rsa.h" |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 34 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/ecp.h" |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 37 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 39 | #include "mbedtls/ecdsa.h" |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 40 | #endif |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 42 | #include "tinycrypt/ecc.h" |
| 43 | #include "tinycrypt/ecc_dsa.h" |
| 44 | #include "mbedtls/asn1.h" |
| 45 | #include "mbedtls/asn1write.h" |
| 46 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
| 47 | |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 48 | #include "mbedtls/platform_util.h" |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 49 | |
| 50 | #if defined(MBEDTLS_PLATFORM_C) |
| 51 | #include "mbedtls/platform.h" |
| 52 | #else |
| 53 | #include <stdlib.h> |
| 54 | #define mbedtls_calloc calloc |
| 55 | #define mbedtls_free free |
| 56 | #endif |
| 57 | |
Manuel Pégourié-Gonnard | 8cd2889 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 58 | #include <string.h> |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 59 | #include <limits.h> |
| 60 | #include <stdint.h> |
| 61 | |
Manuel Pégourié-Gonnard | 8cd2889 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 62 | /* Parameter validation macros based on platform_util.h */ |
| 63 | #define PK_VALIDATE_RET( cond ) \ |
| 64 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA ) |
| 65 | #define PK_VALIDATE( cond ) \ |
| 66 | MBEDTLS_INTERNAL_VALIDATE( cond ) |
| 67 | |
| 68 | /* |
| 69 | * Internal wrappers around RSA functions |
| 70 | */ |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 71 | #if defined(MBEDTLS_RSA_C) |
| 72 | static int rsa_can_do( mbedtls_pk_type_t type ) |
| 73 | { |
| 74 | return( type == MBEDTLS_PK_RSA || |
| 75 | type == MBEDTLS_PK_RSASSA_PSS ); |
| 76 | } |
| 77 | |
| 78 | static size_t rsa_get_bitlen( const void *ctx ) |
| 79 | { |
| 80 | const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx; |
| 81 | return( 8 * mbedtls_rsa_get_len( rsa ) ); |
| 82 | } |
| 83 | |
| 84 | static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 85 | const unsigned char *hash, size_t hash_len, |
| 86 | const unsigned char *sig, size_t sig_len ) |
| 87 | { |
| 88 | int ret; |
| 89 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; |
| 90 | size_t rsa_len = mbedtls_rsa_get_len( rsa ); |
| 91 | |
| 92 | #if SIZE_MAX > UINT_MAX |
| 93 | if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) |
| 94 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
| 95 | #endif /* SIZE_MAX > UINT_MAX */ |
| 96 | |
| 97 | if( sig_len < rsa_len ) |
| 98 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
| 99 | |
| 100 | if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL, |
| 101 | MBEDTLS_RSA_PUBLIC, md_alg, |
| 102 | (unsigned int) hash_len, hash, sig ) ) != 0 ) |
| 103 | return( ret ); |
| 104 | |
| 105 | /* The buffer contains a valid signature followed by extra data. |
| 106 | * We have a special error code for that so that so that callers can |
| 107 | * use mbedtls_pk_verify() to check "Does the buffer start with a |
| 108 | * valid signature?" and not just "Does the buffer contain a valid |
| 109 | * signature?". */ |
| 110 | if( sig_len > rsa_len ) |
| 111 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); |
| 112 | |
| 113 | return( 0 ); |
| 114 | } |
| 115 | |
| 116 | static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 117 | const unsigned char *hash, size_t hash_len, |
| 118 | unsigned char *sig, size_t *sig_len, |
| 119 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 120 | { |
| 121 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; |
| 122 | |
| 123 | #if SIZE_MAX > UINT_MAX |
| 124 | if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) |
| 125 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
| 126 | #endif /* SIZE_MAX > UINT_MAX */ |
| 127 | |
| 128 | *sig_len = mbedtls_rsa_get_len( rsa ); |
| 129 | |
| 130 | return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, |
| 131 | md_alg, (unsigned int) hash_len, hash, sig ) ); |
| 132 | } |
| 133 | |
| 134 | static int rsa_decrypt_wrap( void *ctx, |
| 135 | const unsigned char *input, size_t ilen, |
| 136 | unsigned char *output, size_t *olen, size_t osize, |
| 137 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 138 | { |
| 139 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; |
| 140 | |
| 141 | if( ilen != mbedtls_rsa_get_len( rsa ) ) |
| 142 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 143 | |
| 144 | return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng, |
| 145 | MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) ); |
| 146 | } |
| 147 | |
| 148 | static int rsa_encrypt_wrap( void *ctx, |
| 149 | const unsigned char *input, size_t ilen, |
| 150 | unsigned char *output, size_t *olen, size_t osize, |
| 151 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 152 | { |
| 153 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; |
| 154 | *olen = mbedtls_rsa_get_len( rsa ); |
| 155 | |
| 156 | if( *olen > osize ) |
| 157 | return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE ); |
| 158 | |
| 159 | return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC, |
| 160 | ilen, input, output ) ); |
| 161 | } |
| 162 | |
| 163 | static int rsa_check_pair_wrap( const void *pub, const void *prv ) |
| 164 | { |
| 165 | return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub, |
| 166 | (const mbedtls_rsa_context *) prv ) ); |
| 167 | } |
| 168 | |
| 169 | static void *rsa_alloc_wrap( void ) |
| 170 | { |
| 171 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) ); |
| 172 | |
| 173 | if( ctx != NULL ) |
| 174 | mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 ); |
| 175 | |
| 176 | return( ctx ); |
| 177 | } |
| 178 | |
| 179 | static void rsa_free_wrap( void *ctx ) |
| 180 | { |
| 181 | mbedtls_rsa_free( (mbedtls_rsa_context *) ctx ); |
| 182 | mbedtls_free( ctx ); |
| 183 | } |
| 184 | |
| 185 | static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items ) |
| 186 | { |
| 187 | items->type = MBEDTLS_PK_DEBUG_MPI; |
| 188 | items->name = "rsa.N"; |
| 189 | items->value = &( ((mbedtls_rsa_context *) ctx)->N ); |
| 190 | |
| 191 | items++; |
| 192 | |
| 193 | items->type = MBEDTLS_PK_DEBUG_MPI; |
| 194 | items->name = "rsa.E"; |
| 195 | items->value = &( ((mbedtls_rsa_context *) ctx)->E ); |
| 196 | } |
| 197 | |
| 198 | const mbedtls_pk_info_t mbedtls_rsa_info = { |
| 199 | MBEDTLS_PK_RSA, |
| 200 | "RSA", |
| 201 | rsa_get_bitlen, |
| 202 | rsa_can_do, |
| 203 | rsa_verify_wrap, |
| 204 | rsa_sign_wrap, |
| 205 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 206 | NULL, |
| 207 | NULL, |
| 208 | #endif |
| 209 | rsa_decrypt_wrap, |
| 210 | rsa_encrypt_wrap, |
| 211 | rsa_check_pair_wrap, |
| 212 | rsa_alloc_wrap, |
| 213 | rsa_free_wrap, |
| 214 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 215 | NULL, |
| 216 | NULL, |
| 217 | #endif |
| 218 | rsa_debug, |
| 219 | }; |
| 220 | #endif /* MBEDTLS_RSA_C */ |
| 221 | |
Manuel Pégourié-Gonnard | 8cd2889 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 222 | /* |
| 223 | * Internal wrappers around ECC functions - based on ECP module |
| 224 | */ |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 225 | #if defined(MBEDTLS_ECP_C) |
| 226 | /* |
| 227 | * Generic EC key |
| 228 | */ |
| 229 | static int eckey_can_do( mbedtls_pk_type_t type ) |
| 230 | { |
| 231 | return( type == MBEDTLS_PK_ECKEY || |
| 232 | type == MBEDTLS_PK_ECKEY_DH || |
| 233 | type == MBEDTLS_PK_ECDSA ); |
| 234 | } |
| 235 | |
| 236 | static size_t eckey_get_bitlen( const void *ctx ) |
| 237 | { |
| 238 | return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits ); |
| 239 | } |
| 240 | |
| 241 | #if defined(MBEDTLS_ECDSA_C) |
| 242 | /* Forward declarations */ |
| 243 | static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 244 | const unsigned char *hash, size_t hash_len, |
| 245 | const unsigned char *sig, size_t sig_len ); |
| 246 | |
| 247 | static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 248 | const unsigned char *hash, size_t hash_len, |
| 249 | unsigned char *sig, size_t *sig_len, |
| 250 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 251 | |
| 252 | static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 253 | const unsigned char *hash, size_t hash_len, |
| 254 | const unsigned char *sig, size_t sig_len ) |
| 255 | { |
| 256 | int ret; |
| 257 | mbedtls_ecdsa_context ecdsa; |
| 258 | |
| 259 | mbedtls_ecdsa_init( &ecdsa ); |
| 260 | |
| 261 | if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
| 262 | ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len ); |
| 263 | |
| 264 | mbedtls_ecdsa_free( &ecdsa ); |
| 265 | |
| 266 | return( ret ); |
| 267 | } |
| 268 | |
| 269 | static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 270 | const unsigned char *hash, size_t hash_len, |
| 271 | unsigned char *sig, size_t *sig_len, |
| 272 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 273 | { |
| 274 | int ret; |
| 275 | mbedtls_ecdsa_context ecdsa; |
| 276 | |
| 277 | mbedtls_ecdsa_init( &ecdsa ); |
| 278 | |
| 279 | if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
| 280 | ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len, |
| 281 | f_rng, p_rng ); |
| 282 | |
| 283 | mbedtls_ecdsa_free( &ecdsa ); |
| 284 | |
| 285 | return( ret ); |
| 286 | } |
| 287 | |
| 288 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 289 | /* Forward declarations */ |
| 290 | static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 291 | const unsigned char *hash, size_t hash_len, |
| 292 | const unsigned char *sig, size_t sig_len, |
| 293 | void *rs_ctx ); |
| 294 | |
| 295 | static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 296 | const unsigned char *hash, size_t hash_len, |
| 297 | unsigned char *sig, size_t *sig_len, |
| 298 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 299 | void *rs_ctx ); |
| 300 | |
| 301 | /* |
| 302 | * Restart context for ECDSA operations with ECKEY context |
| 303 | * |
| 304 | * We need to store an actual ECDSA context, as we need to pass the same to |
| 305 | * the underlying ecdsa function, so we can't create it on the fly every time. |
| 306 | */ |
| 307 | typedef struct |
| 308 | { |
| 309 | mbedtls_ecdsa_restart_ctx ecdsa_rs; |
| 310 | mbedtls_ecdsa_context ecdsa_ctx; |
| 311 | } eckey_restart_ctx; |
| 312 | |
| 313 | static void *eckey_rs_alloc( void ) |
| 314 | { |
| 315 | eckey_restart_ctx *rs_ctx; |
| 316 | |
| 317 | void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) ); |
| 318 | |
| 319 | if( ctx != NULL ) |
| 320 | { |
| 321 | rs_ctx = ctx; |
| 322 | mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs ); |
| 323 | mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx ); |
| 324 | } |
| 325 | |
| 326 | return( ctx ); |
| 327 | } |
| 328 | |
| 329 | static void eckey_rs_free( void *ctx ) |
| 330 | { |
| 331 | eckey_restart_ctx *rs_ctx; |
| 332 | |
| 333 | if( ctx == NULL) |
| 334 | return; |
| 335 | |
| 336 | rs_ctx = ctx; |
| 337 | mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs ); |
| 338 | mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx ); |
| 339 | |
| 340 | mbedtls_free( ctx ); |
| 341 | } |
| 342 | |
| 343 | static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 344 | const unsigned char *hash, size_t hash_len, |
| 345 | const unsigned char *sig, size_t sig_len, |
| 346 | void *rs_ctx ) |
| 347 | { |
| 348 | int ret; |
| 349 | eckey_restart_ctx *rs = rs_ctx; |
| 350 | |
| 351 | /* Should never happen */ |
| 352 | if( rs == NULL ) |
| 353 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
| 354 | |
| 355 | /* set up our own sub-context if needed (that is, on first run) */ |
| 356 | if( rs->ecdsa_ctx.grp.pbits == 0 ) |
| 357 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) ); |
| 358 | |
| 359 | MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx, |
| 360 | md_alg, hash, hash_len, |
| 361 | sig, sig_len, &rs->ecdsa_rs ) ); |
| 362 | |
| 363 | cleanup: |
| 364 | return( ret ); |
| 365 | } |
| 366 | |
| 367 | static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 368 | const unsigned char *hash, size_t hash_len, |
| 369 | unsigned char *sig, size_t *sig_len, |
| 370 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 371 | void *rs_ctx ) |
| 372 | { |
| 373 | int ret; |
| 374 | eckey_restart_ctx *rs = rs_ctx; |
| 375 | |
| 376 | /* Should never happen */ |
| 377 | if( rs == NULL ) |
| 378 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
| 379 | |
| 380 | /* set up our own sub-context if needed (that is, on first run) */ |
| 381 | if( rs->ecdsa_ctx.grp.pbits == 0 ) |
| 382 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) ); |
| 383 | |
| 384 | MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg, |
| 385 | hash, hash_len, sig, sig_len, |
| 386 | f_rng, p_rng, &rs->ecdsa_rs ) ); |
| 387 | |
| 388 | cleanup: |
| 389 | return( ret ); |
| 390 | } |
| 391 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 392 | #endif /* MBEDTLS_ECDSA_C */ |
| 393 | |
| 394 | static int eckey_check_pair( const void *pub, const void *prv ) |
| 395 | { |
| 396 | return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub, |
| 397 | (const mbedtls_ecp_keypair *) prv ) ); |
| 398 | } |
| 399 | |
| 400 | static void *eckey_alloc_wrap( void ) |
| 401 | { |
| 402 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 403 | |
| 404 | if( ctx != NULL ) |
| 405 | mbedtls_ecp_keypair_init( ctx ); |
| 406 | |
| 407 | return( ctx ); |
| 408 | } |
| 409 | |
| 410 | static void eckey_free_wrap( void *ctx ) |
| 411 | { |
| 412 | mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx ); |
| 413 | mbedtls_free( ctx ); |
| 414 | } |
| 415 | |
| 416 | static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items ) |
| 417 | { |
| 418 | items->type = MBEDTLS_PK_DEBUG_ECP; |
| 419 | items->name = "eckey.Q"; |
| 420 | items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q ); |
| 421 | } |
| 422 | |
| 423 | const mbedtls_pk_info_t mbedtls_eckey_info = { |
| 424 | MBEDTLS_PK_ECKEY, |
| 425 | "EC", |
| 426 | eckey_get_bitlen, |
| 427 | eckey_can_do, |
| 428 | #if defined(MBEDTLS_ECDSA_C) |
| 429 | eckey_verify_wrap, |
| 430 | eckey_sign_wrap, |
| 431 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 432 | eckey_verify_rs_wrap, |
| 433 | eckey_sign_rs_wrap, |
| 434 | #endif |
| 435 | #else /* MBEDTLS_ECDSA_C */ |
| 436 | NULL, |
| 437 | NULL, |
| 438 | #endif /* MBEDTLS_ECDSA_C */ |
| 439 | NULL, |
| 440 | NULL, |
| 441 | eckey_check_pair, |
| 442 | eckey_alloc_wrap, |
| 443 | eckey_free_wrap, |
| 444 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 445 | eckey_rs_alloc, |
| 446 | eckey_rs_free, |
| 447 | #endif |
| 448 | eckey_debug, |
| 449 | }; |
| 450 | |
| 451 | /* |
| 452 | * EC key restricted to ECDH |
| 453 | */ |
| 454 | static int eckeydh_can_do( mbedtls_pk_type_t type ) |
| 455 | { |
| 456 | return( type == MBEDTLS_PK_ECKEY || |
| 457 | type == MBEDTLS_PK_ECKEY_DH ); |
| 458 | } |
| 459 | |
| 460 | const mbedtls_pk_info_t mbedtls_eckeydh_info = { |
| 461 | MBEDTLS_PK_ECKEY_DH, |
| 462 | "EC_DH", |
| 463 | eckey_get_bitlen, /* Same underlying key structure */ |
| 464 | eckeydh_can_do, |
| 465 | NULL, |
| 466 | NULL, |
| 467 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 468 | NULL, |
| 469 | NULL, |
| 470 | #endif |
| 471 | NULL, |
| 472 | NULL, |
| 473 | eckey_check_pair, |
| 474 | eckey_alloc_wrap, /* Same underlying key structure */ |
| 475 | eckey_free_wrap, /* Same underlying key structure */ |
| 476 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 477 | NULL, |
| 478 | NULL, |
| 479 | #endif |
| 480 | eckey_debug, /* Same underlying key structure */ |
| 481 | }; |
| 482 | #endif /* MBEDTLS_ECP_C */ |
| 483 | |
Manuel Pégourié-Gonnard | 8cd2889 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 484 | /* |
| 485 | * Internal wrappers around ECC functions - based on TinyCrypt |
| 486 | */ |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 487 | #if defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 488 | /* |
| 489 | * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of |
| 490 | * those integers and convert it to the fixed-length encoding. |
| 491 | */ |
| 492 | static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end, |
| 493 | unsigned char *to, size_t to_len ) |
| 494 | { |
| 495 | int ret; |
| 496 | size_t unpadded_len, padding_len; |
| 497 | |
| 498 | if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len, |
| 499 | MBEDTLS_ASN1_INTEGER ) ) != 0 ) |
| 500 | { |
| 501 | return( ret ); |
| 502 | } |
| 503 | |
| 504 | while( unpadded_len > 0 && **from == 0x00 ) |
| 505 | { |
| 506 | ( *from )++; |
| 507 | unpadded_len--; |
| 508 | } |
| 509 | |
| 510 | if( unpadded_len > to_len || unpadded_len == 0 ) |
| 511 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
| 512 | |
| 513 | padding_len = to_len - unpadded_len; |
| 514 | memset( to, 0x00, padding_len ); |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 515 | mbedtls_platform_memcpy( to + padding_len, *from, unpadded_len ); |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 516 | ( *from ) += unpadded_len; |
| 517 | |
| 518 | return( 0 ); |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Convert a signature from an ASN.1 sequence of two integers |
| 523 | * to a raw {r,s} buffer. Note: the provided sig buffer must be at least |
| 524 | * twice as big as int_size. |
| 525 | */ |
| 526 | static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end, |
| 527 | unsigned char *sig, size_t int_size ) |
| 528 | { |
| 529 | int ret; |
| 530 | size_t tmp_size; |
| 531 | |
| 532 | if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size, |
| 533 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 534 | return( ret ); |
| 535 | |
| 536 | /* Extract r */ |
| 537 | if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 ) |
| 538 | return( ret ); |
| 539 | /* Extract s */ |
| 540 | if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 ) |
| 541 | return( ret ); |
| 542 | |
| 543 | return( 0 ); |
| 544 | } |
| 545 | |
| 546 | static size_t uecc_eckey_get_bitlen( const void *ctx ) |
| 547 | { |
| 548 | (void) ctx; |
| 549 | return( (size_t) ( NUM_ECC_BYTES * 8 ) ); |
| 550 | } |
| 551 | |
| 552 | static int uecc_eckey_check_pair( const void *pub, const void *prv ) |
| 553 | { |
| 554 | const mbedtls_uecc_keypair *uecc_pub = |
| 555 | (const mbedtls_uecc_keypair *) pub; |
| 556 | const mbedtls_uecc_keypair *uecc_prv = |
| 557 | (const mbedtls_uecc_keypair *) prv; |
| 558 | |
Teppo Järvelin | 61f412e | 2019-10-03 12:25:22 +0300 | [diff] [blame] | 559 | if( mbedtls_platform_memcmp( uecc_pub->public_key, |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 560 | uecc_prv->public_key, |
| 561 | 2 * NUM_ECC_BYTES ) == 0 ) |
| 562 | { |
| 563 | return( 0 ); |
| 564 | } |
| 565 | |
| 566 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
| 567 | } |
| 568 | |
| 569 | static int uecc_eckey_can_do( mbedtls_pk_type_t type ) |
| 570 | { |
| 571 | return( type == MBEDTLS_PK_ECDSA || |
| 572 | type == MBEDTLS_PK_ECKEY ); |
| 573 | } |
| 574 | |
| 575 | static int uecc_eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 576 | const unsigned char *hash, size_t hash_len, |
| 577 | const unsigned char *sig, size_t sig_len ) |
| 578 | { |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 579 | int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; |
| 580 | volatile int ret_fi = UECC_FAULT_DETECTED; |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 581 | uint8_t signature[2*NUM_ECC_BYTES]; |
| 582 | unsigned char *p; |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 583 | const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx; |
| 584 | |
| 585 | ((void) md_alg); |
| 586 | p = (unsigned char*) sig; |
| 587 | |
| 588 | ret = extract_ecdsa_sig( &p, sig + sig_len, signature, NUM_ECC_BYTES ); |
| 589 | if( ret != 0 ) |
| 590 | return( ret ); |
| 591 | |
Manuel Pégourié-Gonnard | ca7b5ab | 2019-11-06 11:56:25 +0100 | [diff] [blame] | 592 | ret_fi = uECC_verify( keypair->public_key, hash, |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 593 | (unsigned) hash_len, signature ); |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 594 | |
Manuel Pégourié-Gonnard | 4d6186b | 2019-11-25 10:53:24 +0100 | [diff] [blame] | 595 | if( ret_fi == UECC_FAULT_DETECTED ) |
Manuel Pégourié-Gonnard | ca7b5ab | 2019-11-06 11:56:25 +0100 | [diff] [blame] | 596 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); |
| 597 | |
| 598 | if( ret_fi == UECC_SUCCESS ) |
| 599 | { |
Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 600 | mbedtls_platform_random_delay(); |
Manuel Pégourié-Gonnard | ca7b5ab | 2019-11-06 11:56:25 +0100 | [diff] [blame] | 601 | if( ret_fi == UECC_SUCCESS ) |
| 602 | return( 0 ); |
| 603 | else |
| 604 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); |
| 605 | } |
| 606 | |
| 607 | return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* |
| 611 | * Simultaneously convert and move raw MPI from the beginning of a buffer |
| 612 | * to an ASN.1 MPI at the end of the buffer. |
| 613 | * See also mbedtls_asn1_write_mpi(). |
| 614 | * |
| 615 | * p: pointer to the end of the output buffer |
| 616 | * start: start of the output buffer, and also of the mpi to write at the end |
| 617 | * n_len: length of the mpi to read from start |
| 618 | * |
| 619 | * Warning: |
| 620 | * The total length of the output buffer must be smaller than 128 Bytes. |
| 621 | */ |
| 622 | static int asn1_write_mpibuf( unsigned char **p, unsigned char *start, |
| 623 | size_t n_len ) |
| 624 | { |
| 625 | size_t len = 0; |
Piotr Nowicki | ce0aab4 | 2020-06-08 14:08:49 +0200 | [diff] [blame^] | 626 | int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 627 | |
| 628 | if( (size_t)( *p - start ) < n_len ) |
| 629 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 630 | |
| 631 | len = n_len; |
| 632 | *p -= len; |
Piotr Nowicki | ce0aab4 | 2020-06-08 14:08:49 +0200 | [diff] [blame^] | 633 | ret = mbedtls_platform_memmove( *p, start, len ); |
| 634 | if( ret != 0 ) |
Piotr Nowicki | 5d5841f | 2020-06-05 16:33:24 +0200 | [diff] [blame] | 635 | { |
Piotr Nowicki | ce0aab4 | 2020-06-08 14:08:49 +0200 | [diff] [blame^] | 636 | return( ret ); |
Piotr Nowicki | 5d5841f | 2020-06-05 16:33:24 +0200 | [diff] [blame] | 637 | } |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 638 | |
| 639 | /* ASN.1 DER encoding requires minimal length, so skip leading 0s. |
| 640 | * Neither r nor s should be 0, but as a failsafe measure, still detect |
| 641 | * that rather than overflowing the buffer in case of an error. */ |
| 642 | while( len > 0 && **p == 0x00 ) |
| 643 | { |
| 644 | ++(*p); |
| 645 | --len; |
| 646 | } |
| 647 | |
| 648 | /* this is only reached if the signature was invalid */ |
| 649 | if( len == 0 ) |
| 650 | return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED ); |
| 651 | |
| 652 | /* if the msb is 1, ASN.1 requires that we prepend a 0. |
| 653 | * Neither r nor s can be 0, so we can assume len > 0 at all times. */ |
| 654 | if( **p & 0x80 ) |
| 655 | { |
| 656 | if( *p - start < 1 ) |
| 657 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 658 | |
| 659 | *--(*p) = 0x00; |
| 660 | len += 1; |
| 661 | } |
| 662 | |
| 663 | /* The ASN.1 length encoding is just a single Byte containing the length, |
| 664 | * as we assume that the total buffer length is smaller than 128 Bytes. */ |
| 665 | *--(*p) = len; |
| 666 | *--(*p) = MBEDTLS_ASN1_INTEGER; |
| 667 | len += 2; |
| 668 | |
| 669 | return( (int) len ); |
| 670 | } |
| 671 | |
| 672 | /* Transcode signature from uECC format to ASN.1 sequence. |
| 673 | * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of |
| 674 | * MPIs, and in-place. |
| 675 | * |
| 676 | * [in/out] sig: the signature pre- and post-transcoding |
| 677 | * [in/out] sig_len: signature length pre- and post-transcoding |
| 678 | * [int] buf_len: the available size the in/out buffer |
| 679 | * |
| 680 | * Warning: buf_len must be smaller than 128 Bytes. |
| 681 | */ |
| 682 | static int pk_ecdsa_sig_asn1_from_uecc( unsigned char *sig, size_t *sig_len, |
| 683 | size_t buf_len ) |
| 684 | { |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 685 | int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 686 | size_t len = 0; |
| 687 | const size_t rs_len = *sig_len / 2; |
| 688 | unsigned char *p = sig + buf_len; |
| 689 | |
| 690 | MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) ); |
| 691 | MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) ); |
| 692 | |
| 693 | /* The ASN.1 length encoding is just a single Byte containing the length, |
| 694 | * as we assume that the total buffer length is smaller than 128 Bytes. */ |
| 695 | *--p = len; |
| 696 | *--p = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 697 | len += 2; |
| 698 | |
Piotr Nowicki | ce0aab4 | 2020-06-08 14:08:49 +0200 | [diff] [blame^] | 699 | ret = mbedtls_platform_memmove( sig, p, len ); |
| 700 | if( ret != 0 ) |
Piotr Nowicki | 5d5841f | 2020-06-05 16:33:24 +0200 | [diff] [blame] | 701 | { |
Piotr Nowicki | ce0aab4 | 2020-06-08 14:08:49 +0200 | [diff] [blame^] | 702 | return( ret ); |
Piotr Nowicki | 5d5841f | 2020-06-05 16:33:24 +0200 | [diff] [blame] | 703 | } |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 704 | *sig_len = len; |
| 705 | |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 706 | return( ret ); |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | static int uecc_eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 710 | const unsigned char *hash, size_t hash_len, |
| 711 | unsigned char *sig, size_t *sig_len, |
| 712 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 713 | { |
| 714 | const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx; |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 715 | int ret; |
| 716 | |
| 717 | /* |
| 718 | * RFC-4492 page 20: |
| 719 | * |
| 720 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 721 | * r INTEGER, |
| 722 | * s INTEGER |
| 723 | * } |
| 724 | * |
| 725 | * Size is at most |
| 726 | * 1 (tag) + 1 (len) + 1 (initial 0) + NUM_ECC_BYTES for each of r and s, |
| 727 | * twice that + 1 (tag) + 2 (len) for the sequence |
| 728 | * |
| 729 | * (The ASN.1 length encodings are all 1-Byte encodings because |
| 730 | * the total size is smaller than 128 Bytes). |
| 731 | */ |
| 732 | #define MAX_SECP256R1_ECDSA_SIG_LEN ( 3 + 2 * ( 3 + NUM_ECC_BYTES ) ) |
| 733 | |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 734 | ret = uECC_sign( keypair->private_key, hash, hash_len, sig ); |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 735 | if( ret == UECC_FAULT_DETECTED ) |
| 736 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); |
| 737 | if( ret != UECC_SUCCESS ) |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 738 | return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED ); |
| 739 | |
| 740 | *sig_len = 2 * NUM_ECC_BYTES; |
| 741 | |
| 742 | /* uECC owns its rng function pointer */ |
| 743 | (void) f_rng; |
| 744 | (void) p_rng; |
| 745 | (void) md_alg; |
| 746 | |
| 747 | return( pk_ecdsa_sig_asn1_from_uecc( sig, sig_len, |
| 748 | MAX_SECP256R1_ECDSA_SIG_LEN ) ); |
| 749 | |
| 750 | #undef MAX_SECP256R1_ECDSA_SIG_LEN |
| 751 | } |
| 752 | |
Manuel Pégourié-Gonnard | a77e9b5 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 753 | #if !defined(MBEDTLS_PK_SINGLE_TYPE) |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 754 | static void *uecc_eckey_alloc_wrap( void ) |
| 755 | { |
| 756 | return( mbedtls_calloc( 1, sizeof( mbedtls_uecc_keypair ) ) ); |
| 757 | } |
| 758 | |
| 759 | static void uecc_eckey_free_wrap( void *ctx ) |
| 760 | { |
| 761 | if( ctx == NULL ) |
| 762 | return; |
| 763 | |
| 764 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_uecc_keypair ) ); |
| 765 | mbedtls_free( ctx ); |
| 766 | } |
Manuel Pégourié-Gonnard | a77e9b5 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 767 | #endif /* MBEDTLS_PK_SINGLE_TYPE */ |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 768 | |
Manuel Pégourié-Gonnard | f8b7c7f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 769 | #if !defined(MBEDTLS_PK_SINGLE_TYPE) |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 770 | const mbedtls_pk_info_t mbedtls_uecc_eckey_info = |
| 771 | MBEDTLS_PK_INFO( MBEDTLS_PK_INFO_ECKEY ); |
Manuel Pégourié-Gonnard | f8b7c7f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 772 | #endif |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 773 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
| 774 | |
Manuel Pégourié-Gonnard | 8cd2889 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 775 | /* |
| 776 | * Internal wrappers around ECDSA functions |
| 777 | */ |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 778 | #if defined(MBEDTLS_ECDSA_C) |
| 779 | static int ecdsa_can_do( mbedtls_pk_type_t type ) |
| 780 | { |
| 781 | return( type == MBEDTLS_PK_ECDSA ); |
| 782 | } |
| 783 | |
| 784 | static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 785 | const unsigned char *hash, size_t hash_len, |
| 786 | const unsigned char *sig, size_t sig_len ) |
| 787 | { |
| 788 | int ret; |
| 789 | ((void) md_alg); |
| 790 | |
| 791 | ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx, |
| 792 | hash, hash_len, sig, sig_len ); |
| 793 | |
| 794 | if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH ) |
| 795 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); |
| 796 | |
| 797 | return( ret ); |
| 798 | } |
| 799 | |
| 800 | static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 801 | const unsigned char *hash, size_t hash_len, |
| 802 | unsigned char *sig, size_t *sig_len, |
| 803 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 804 | { |
| 805 | return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx, |
| 806 | md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) ); |
| 807 | } |
| 808 | |
| 809 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 810 | static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 811 | const unsigned char *hash, size_t hash_len, |
| 812 | const unsigned char *sig, size_t sig_len, |
| 813 | void *rs_ctx ) |
| 814 | { |
| 815 | int ret; |
| 816 | ((void) md_alg); |
| 817 | |
| 818 | ret = mbedtls_ecdsa_read_signature_restartable( |
| 819 | (mbedtls_ecdsa_context *) ctx, |
| 820 | hash, hash_len, sig, sig_len, |
| 821 | (mbedtls_ecdsa_restart_ctx *) rs_ctx ); |
| 822 | |
| 823 | if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH ) |
| 824 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); |
| 825 | |
| 826 | return( ret ); |
| 827 | } |
| 828 | |
| 829 | static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 830 | const unsigned char *hash, size_t hash_len, |
| 831 | unsigned char *sig, size_t *sig_len, |
| 832 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 833 | void *rs_ctx ) |
| 834 | { |
| 835 | return( mbedtls_ecdsa_write_signature_restartable( |
| 836 | (mbedtls_ecdsa_context *) ctx, |
| 837 | md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng, |
| 838 | (mbedtls_ecdsa_restart_ctx *) rs_ctx ) ); |
| 839 | |
| 840 | } |
| 841 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 842 | |
| 843 | static void *ecdsa_alloc_wrap( void ) |
| 844 | { |
| 845 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) ); |
| 846 | |
| 847 | if( ctx != NULL ) |
| 848 | mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx ); |
| 849 | |
| 850 | return( ctx ); |
| 851 | } |
| 852 | |
| 853 | static void ecdsa_free_wrap( void *ctx ) |
| 854 | { |
| 855 | mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx ); |
| 856 | mbedtls_free( ctx ); |
| 857 | } |
| 858 | |
| 859 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 860 | static void *ecdsa_rs_alloc( void ) |
| 861 | { |
| 862 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) ); |
| 863 | |
| 864 | if( ctx != NULL ) |
| 865 | mbedtls_ecdsa_restart_init( ctx ); |
| 866 | |
| 867 | return( ctx ); |
| 868 | } |
| 869 | |
| 870 | static void ecdsa_rs_free( void *ctx ) |
| 871 | { |
| 872 | mbedtls_ecdsa_restart_free( ctx ); |
| 873 | mbedtls_free( ctx ); |
| 874 | } |
| 875 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 876 | |
| 877 | const mbedtls_pk_info_t mbedtls_ecdsa_info = { |
| 878 | MBEDTLS_PK_ECDSA, |
| 879 | "ECDSA", |
| 880 | eckey_get_bitlen, /* Compatible key structures */ |
| 881 | ecdsa_can_do, |
| 882 | ecdsa_verify_wrap, |
| 883 | ecdsa_sign_wrap, |
| 884 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 885 | ecdsa_verify_rs_wrap, |
| 886 | ecdsa_sign_rs_wrap, |
| 887 | #endif |
| 888 | NULL, |
| 889 | NULL, |
| 890 | eckey_check_pair, /* Compatible key structures */ |
| 891 | ecdsa_alloc_wrap, |
| 892 | ecdsa_free_wrap, |
| 893 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 894 | ecdsa_rs_alloc, |
| 895 | ecdsa_rs_free, |
| 896 | #endif |
| 897 | eckey_debug, /* Compatible key structures */ |
| 898 | }; |
| 899 | #endif /* MBEDTLS_ECDSA_C */ |
| 900 | |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 901 | /* |
Manuel Pégourié-Gonnard | 8cd2889 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 902 | * Internal wrappers for RSA-alt support |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 903 | */ |
Manuel Pégourié-Gonnard | 8cd2889 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 904 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 905 | static int rsa_alt_can_do( mbedtls_pk_type_t type ) |
| 906 | { |
| 907 | return( type == MBEDTLS_PK_RSA ); |
| 908 | } |
| 909 | |
| 910 | static size_t rsa_alt_get_bitlen( const void *ctx ) |
| 911 | { |
| 912 | const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx; |
| 913 | |
| 914 | return( 8 * rsa_alt->key_len_func( rsa_alt->key ) ); |
| 915 | } |
| 916 | |
| 917 | static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 918 | const unsigned char *hash, size_t hash_len, |
| 919 | unsigned char *sig, size_t *sig_len, |
| 920 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 921 | { |
| 922 | mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; |
| 923 | |
| 924 | #if SIZE_MAX > UINT_MAX |
| 925 | if( UINT_MAX < hash_len ) |
| 926 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
| 927 | #endif /* SIZE_MAX > UINT_MAX */ |
| 928 | |
| 929 | *sig_len = rsa_alt->key_len_func( rsa_alt->key ); |
| 930 | |
| 931 | return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, |
| 932 | md_alg, (unsigned int) hash_len, hash, sig ) ); |
| 933 | } |
| 934 | |
| 935 | static int rsa_alt_decrypt_wrap( void *ctx, |
| 936 | const unsigned char *input, size_t ilen, |
| 937 | unsigned char *output, size_t *olen, size_t osize, |
| 938 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 939 | { |
| 940 | mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; |
| 941 | |
| 942 | ((void) f_rng); |
| 943 | ((void) p_rng); |
| 944 | |
| 945 | if( ilen != rsa_alt->key_len_func( rsa_alt->key ) ) |
| 946 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 947 | |
| 948 | return( rsa_alt->decrypt_func( rsa_alt->key, |
| 949 | MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) ); |
| 950 | } |
| 951 | |
| 952 | #if defined(MBEDTLS_RSA_C) |
| 953 | static int rsa_alt_check_pair( const void *pub, const void *prv ) |
| 954 | { |
| 955 | unsigned char sig[MBEDTLS_MPI_MAX_SIZE]; |
| 956 | unsigned char hash[32]; |
| 957 | size_t sig_len = 0; |
| 958 | int ret; |
| 959 | |
| 960 | if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) ) |
| 961 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
| 962 | |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 963 | mbedtls_platform_memset( hash, 0x2a, sizeof( hash ) ); |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 964 | |
| 965 | if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE, |
| 966 | hash, sizeof( hash ), |
| 967 | sig, &sig_len, NULL, NULL ) ) != 0 ) |
| 968 | { |
| 969 | return( ret ); |
| 970 | } |
| 971 | |
| 972 | if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE, |
| 973 | hash, sizeof( hash ), sig, sig_len ) != 0 ) |
| 974 | { |
| 975 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
| 976 | } |
| 977 | |
| 978 | return( 0 ); |
| 979 | } |
| 980 | #endif /* MBEDTLS_RSA_C */ |
| 981 | |
| 982 | static void *rsa_alt_alloc_wrap( void ) |
| 983 | { |
| 984 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) ); |
| 985 | |
| 986 | if( ctx != NULL ) |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 987 | mbedtls_platform_memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) ); |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 988 | |
| 989 | return( ctx ); |
| 990 | } |
| 991 | |
| 992 | static void rsa_alt_free_wrap( void *ctx ) |
| 993 | { |
| 994 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) ); |
| 995 | mbedtls_free( ctx ); |
| 996 | } |
| 997 | |
| 998 | const mbedtls_pk_info_t mbedtls_rsa_alt_info = { |
| 999 | MBEDTLS_PK_RSA_ALT, |
| 1000 | "RSA-alt", |
| 1001 | rsa_alt_get_bitlen, |
| 1002 | rsa_alt_can_do, |
| 1003 | NULL, |
| 1004 | rsa_alt_sign_wrap, |
| 1005 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1006 | NULL, |
| 1007 | NULL, |
| 1008 | #endif |
| 1009 | rsa_alt_decrypt_wrap, |
| 1010 | NULL, |
| 1011 | #if defined(MBEDTLS_RSA_C) |
| 1012 | rsa_alt_check_pair, |
| 1013 | #else |
| 1014 | NULL, |
| 1015 | #endif |
| 1016 | rsa_alt_alloc_wrap, |
| 1017 | rsa_alt_free_wrap, |
| 1018 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1019 | NULL, |
| 1020 | NULL, |
| 1021 | #endif |
| 1022 | NULL, |
| 1023 | }; |
Manuel Pégourié-Gonnard | 4ed179f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1024 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
| 1025 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1026 | /* |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1027 | * Access to members of the pk_info structure. When a single PK type is |
| 1028 | * hardcoded, these should have zero runtime cost; otherwise, the usual |
| 1029 | * dynamic dispatch based on pk_info is used. |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1030 | * |
| 1031 | * For function members, don't make a getter, but a function that directly |
| 1032 | * calls the method, so that we can entirely get rid of function pointers |
| 1033 | * when hardcoding a single PK - some compilers optimize better that way. |
| 1034 | * |
| 1035 | * Not implemented for members that are only present in builds with |
Manuel Pégourié-Gonnard | 8b5e6bd | 2019-09-20 08:57:18 +0200 | [diff] [blame] | 1036 | * MBEDTLS_ECP_RESTARTABLE for now, as the main target for this is builds |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1037 | * with MBEDTLS_USE_TINYCRYPT, which don't have MBEDTLS_ECP_RESTARTABLE. |
| 1038 | */ |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1039 | #if defined(MBEDTLS_PK_SINGLE_TYPE) |
| 1040 | |
| 1041 | MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1042 | mbedtls_pk_handle_t info ) |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1043 | { |
| 1044 | (void) info; |
| 1045 | return( MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) ); |
| 1046 | } |
| 1047 | |
| 1048 | MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1049 | mbedtls_pk_handle_t info ) |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1050 | { |
| 1051 | (void) info; |
| 1052 | return( MBEDTLS_PK_INFO_NAME( MBEDTLS_PK_SINGLE_TYPE ) ); |
| 1053 | } |
| 1054 | |
| 1055 | MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1056 | mbedtls_pk_handle_t info, const void *ctx ) |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1057 | { |
| 1058 | (void) info; |
| 1059 | return( MBEDTLS_PK_INFO_GET_BITLEN( MBEDTLS_PK_SINGLE_TYPE )( ctx ) ); |
| 1060 | } |
| 1061 | |
| 1062 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1063 | mbedtls_pk_handle_t info, mbedtls_pk_type_t type ) |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1064 | { |
| 1065 | (void) info; |
| 1066 | return( MBEDTLS_PK_INFO_CAN_DO( MBEDTLS_PK_SINGLE_TYPE )( type ) ); |
| 1067 | } |
| 1068 | |
| 1069 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1070 | mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1071 | const unsigned char *hash, size_t hash_len, |
| 1072 | const unsigned char *sig, size_t sig_len ) |
| 1073 | { |
| 1074 | (void) info; |
| 1075 | #if MBEDTLS_PK_INFO_VERIFY_OMIT( MBEDTLS_PK_SINGLE_TYPE ) |
| 1076 | (void) ctx; |
| 1077 | (void) md_alg; |
| 1078 | (void) hash; |
| 1079 | (void) hash_len; |
| 1080 | (void) sig; |
| 1081 | (void) sig_len; |
| 1082 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
| 1083 | #else |
| 1084 | return( MBEDTLS_PK_INFO_VERIFY_FUNC( MBEDTLS_PK_SINGLE_TYPE )( |
| 1085 | ctx, md_alg, hash, hash_len, sig, sig_len ) ); |
| 1086 | #endif |
| 1087 | } |
| 1088 | |
| 1089 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1090 | mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1091 | const unsigned char *hash, size_t hash_len, |
| 1092 | unsigned char *sig, size_t *sig_len, |
| 1093 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1094 | void *p_rng ) |
| 1095 | { |
| 1096 | (void) info; |
| 1097 | #if MBEDTLS_PK_INFO_SIGN_OMIT( MBEDTLS_PK_SINGLE_TYPE ) |
| 1098 | (void) ctx; |
| 1099 | (void) md_alg; |
| 1100 | (void) hash; |
| 1101 | (void) hash_len; |
| 1102 | (void) sig; |
| 1103 | (void) sig_len; |
| 1104 | (void) f_rng; |
| 1105 | (void) p_rng; |
| 1106 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
| 1107 | #else |
| 1108 | return( MBEDTLS_PK_INFO_SIGN_FUNC( MBEDTLS_PK_SINGLE_TYPE )( |
| 1109 | ctx, md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) ); |
| 1110 | #endif |
| 1111 | } |
| 1112 | |
| 1113 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1114 | mbedtls_pk_handle_t info, void *ctx, |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1115 | const unsigned char *input, size_t ilen, |
| 1116 | unsigned char *output, size_t *olen, size_t osize, |
| 1117 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1118 | void *p_rng ) |
| 1119 | { |
| 1120 | (void) info; |
| 1121 | #if MBEDTLS_PK_INFO_DECRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE ) |
| 1122 | (void) ctx; |
| 1123 | (void) input; |
| 1124 | (void) ilen; |
| 1125 | (void) output; |
| 1126 | (void) olen; |
| 1127 | (void) osize; |
| 1128 | (void) f_rng; |
| 1129 | (void) p_rng; |
| 1130 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
| 1131 | #else |
| 1132 | return( MBEDTLS_PK_INFO_DECRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )( |
| 1133 | ctx, input, ilen, output, olen, osize, f_rng, p_rng ) ); |
| 1134 | #endif |
| 1135 | } |
| 1136 | |
| 1137 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1138 | mbedtls_pk_handle_t info, void *ctx, |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1139 | const unsigned char *input, size_t ilen, |
| 1140 | unsigned char *output, size_t *olen, size_t osize, |
| 1141 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1142 | void *p_rng ) |
| 1143 | { |
| 1144 | (void) info; |
| 1145 | #if MBEDTLS_PK_INFO_ENCRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE ) |
| 1146 | (void) ctx; |
| 1147 | (void) input; |
| 1148 | (void) ilen; |
| 1149 | (void) output; |
| 1150 | (void) olen; |
| 1151 | (void) osize; |
| 1152 | (void) f_rng; |
| 1153 | (void) p_rng; |
| 1154 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
| 1155 | #else |
| 1156 | return( MBEDTLS_PK_INFO_ENCRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )( |
| 1157 | ctx, input, ilen, output, olen, osize, f_rng, p_rng ) ); |
| 1158 | #endif |
| 1159 | } |
| 1160 | |
| 1161 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1162 | mbedtls_pk_handle_t info, const void *pub, const void *prv ) |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1163 | { |
| 1164 | (void) info; |
| 1165 | #if MBEDTLS_PK_INFO_CHECK_PAIR_OMIT( MBEDTLS_PK_SINGLE_TYPE ) |
| 1166 | (void) pub; |
| 1167 | (void) prv; |
| 1168 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
| 1169 | #else |
| 1170 | return( MBEDTLS_PK_INFO_CHECK_PAIR_FUNC( MBEDTLS_PK_SINGLE_TYPE )( |
| 1171 | pub, prv ) ); |
| 1172 | #endif |
| 1173 | } |
| 1174 | |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1175 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1176 | mbedtls_pk_handle_t info, |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1177 | const void *ctx, mbedtls_pk_debug_item *items ) |
| 1178 | { |
| 1179 | (void) info; |
| 1180 | #if MBEDTLS_PK_INFO_DEBUG_OMIT( MBEDTLS_PK_SINGLE_TYPE ) |
| 1181 | (void) ctx; |
| 1182 | (void) items; |
| 1183 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
| 1184 | #else |
| 1185 | return( MBEDTLS_PK_INFO_DEBUG_FUNC( MBEDTLS_PK_SINGLE_TYPE )( ctx, items ) ); |
| 1186 | #endif |
| 1187 | } |
| 1188 | |
| 1189 | #else /* MBEDTLS_PK_SINGLE_TYPE */ |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1190 | |
| 1191 | MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1192 | mbedtls_pk_handle_t info ) |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1193 | { |
| 1194 | return( info->type ); |
| 1195 | } |
| 1196 | |
| 1197 | MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1198 | mbedtls_pk_handle_t info ) |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1199 | { |
| 1200 | return( info->name ); |
| 1201 | } |
| 1202 | |
| 1203 | MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1204 | mbedtls_pk_handle_t info, const void *ctx ) |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1205 | { |
| 1206 | return( info->get_bitlen( ctx ) ); |
| 1207 | } |
| 1208 | |
| 1209 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1210 | mbedtls_pk_handle_t info, mbedtls_pk_type_t type ) |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1211 | { |
| 1212 | return( info->can_do( type ) ); |
| 1213 | } |
| 1214 | |
| 1215 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1216 | mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1217 | const unsigned char *hash, size_t hash_len, |
| 1218 | const unsigned char *sig, size_t sig_len ) |
| 1219 | { |
Manuel Pégourié-Gonnard | 57d96cd | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1220 | if( info->verify_func == NULL ) |
| 1221 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
| 1222 | |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1223 | return( info->verify_func( ctx, md_alg, hash, hash_len, sig, sig_len ) ); |
| 1224 | } |
| 1225 | |
| 1226 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1227 | mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1228 | const unsigned char *hash, size_t hash_len, |
| 1229 | unsigned char *sig, size_t *sig_len, |
| 1230 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1231 | void *p_rng ) |
| 1232 | { |
Manuel Pégourié-Gonnard | 57d96cd | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1233 | if( info->sign_func == NULL ) |
| 1234 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
| 1235 | |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1236 | return( info->sign_func( ctx, md_alg, hash, hash_len, sig, sig_len, |
| 1237 | f_rng, p_rng ) ); |
| 1238 | } |
| 1239 | |
| 1240 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1241 | mbedtls_pk_handle_t info, void *ctx, |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1242 | const unsigned char *input, size_t ilen, |
| 1243 | unsigned char *output, size_t *olen, size_t osize, |
| 1244 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1245 | void *p_rng ) |
| 1246 | { |
Manuel Pégourié-Gonnard | 57d96cd | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1247 | if( info->decrypt_func == NULL ) |
| 1248 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
| 1249 | |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1250 | return( info->decrypt_func( ctx, input, ilen, output, olen, osize, |
| 1251 | f_rng, p_rng ) ); |
| 1252 | } |
| 1253 | |
| 1254 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1255 | mbedtls_pk_handle_t info, void *ctx, |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1256 | const unsigned char *input, size_t ilen, |
| 1257 | unsigned char *output, size_t *olen, size_t osize, |
| 1258 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1259 | void *p_rng ) |
| 1260 | { |
Manuel Pégourié-Gonnard | 57d96cd | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1261 | if( info->encrypt_func == NULL ) |
| 1262 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
| 1263 | |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1264 | return( info->encrypt_func( ctx, input, ilen, output, olen, osize, |
| 1265 | f_rng, p_rng ) ); |
| 1266 | } |
| 1267 | |
| 1268 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1269 | mbedtls_pk_handle_t info, const void *pub, const void *prv ) |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1270 | { |
Manuel Pégourié-Gonnard | 57d96cd | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1271 | if( info->check_pair_func == NULL ) |
| 1272 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
| 1273 | |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1274 | return( info->check_pair_func( pub, prv ) ); |
| 1275 | } |
| 1276 | |
| 1277 | MBEDTLS_ALWAYS_INLINE static inline void *pk_info_ctx_alloc_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1278 | mbedtls_pk_handle_t info ) |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1279 | { |
| 1280 | return( info->ctx_alloc_func( ) ); |
| 1281 | } |
| 1282 | |
| 1283 | MBEDTLS_ALWAYS_INLINE static inline void pk_info_ctx_free_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1284 | mbedtls_pk_handle_t info, void *ctx ) |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1285 | { |
| 1286 | info->ctx_free_func( ctx ); |
| 1287 | } |
| 1288 | |
Manuel Pégourié-Gonnard | 57d96cd | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1289 | MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func( |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1290 | mbedtls_pk_handle_t info, |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1291 | const void *ctx, mbedtls_pk_debug_item *items ) |
| 1292 | { |
Manuel Pégourié-Gonnard | 57d96cd | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1293 | if( info->debug_func == NULL ) |
| 1294 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
| 1295 | |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1296 | info->debug_func( ctx, items ); |
Manuel Pégourié-Gonnard | 57d96cd | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1297 | return( 0 ); |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1298 | } |
| 1299 | |
Manuel Pégourié-Gonnard | 08620cb | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1300 | #endif /* MBEDTLS_PK_SINGLE_TYPE */ |
| 1301 | |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1302 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1303 | * Initialise a mbedtls_pk_context |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1304 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1305 | void mbedtls_pk_init( mbedtls_pk_context *ctx ) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1306 | { |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1307 | PK_VALIDATE( ctx != NULL ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1308 | |
Manuel Pégourié-Gonnard | 073c1e1 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1309 | #if !defined(MBEDTLS_PK_SINGLE_TYPE) |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1310 | ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE; |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 1311 | ctx->pk_ctx = NULL; |
Manuel Pégourié-Gonnard | a77e9b5 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1312 | #else |
Manuel Pégourié-Gonnard | 9941933 | 2019-10-03 10:40:57 +0200 | [diff] [blame] | 1313 | memset( ctx, 0, sizeof( mbedtls_pk_context ) ); |
Manuel Pégourié-Gonnard | a77e9b5 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1314 | #endif |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1318 | * Free (the components of) a mbedtls_pk_context |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1319 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1320 | void mbedtls_pk_free( mbedtls_pk_context *ctx ) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1321 | { |
irwir | 2239a86 | 2018-06-12 18:25:09 +0300 | [diff] [blame] | 1322 | if( ctx == NULL ) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1323 | return; |
| 1324 | |
Manuel Pégourié-Gonnard | a77e9b5 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1325 | #if !defined(MBEDTLS_PK_SINGLE_TYPE) |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1326 | if( MBEDTLS_PK_CTX_IS_VALID( ctx ) ) |
| 1327 | pk_info_ctx_free_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx ); |
Manuel Pégourié-Gonnard | a77e9b5 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1328 | #endif |
Manuel Pégourié-Gonnard | 1f73a65 | 2013-07-09 10:26:41 +0200 | [diff] [blame] | 1329 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1330 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 1331 | } |
| 1332 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1333 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 1334 | /* |
| 1335 | * Initialize a restart context |
| 1336 | */ |
| 1337 | void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx ) |
| 1338 | { |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1339 | PK_VALIDATE( ctx != NULL ); |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1340 | ctx->pk_info = NULL; |
| 1341 | ctx->rs_ctx = NULL; |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | /* |
| 1345 | * Free the components of a restart context |
| 1346 | */ |
| 1347 | void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx ) |
| 1348 | { |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1349 | if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) || |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1350 | ctx->pk_info->rs_free_func == NULL ) |
| 1351 | { |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 1352 | return; |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1353 | } |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 1354 | |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1355 | ctx->pk_info->rs_free_func( ctx->rs_ctx ); |
| 1356 | |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1357 | ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE; |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1358 | ctx->rs_ctx = NULL; |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 1359 | } |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1360 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 1361 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 1362 | /* |
| 1363 | * Get pk_info structure from type |
| 1364 | */ |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1365 | mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type ) |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 1366 | { |
Manuel Pégourié-Gonnard | f8b7c7f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1367 | #if defined(MBEDTLS_PK_SINGLE_TYPE) |
| 1368 | if( pk_type == MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) ) |
| 1369 | return( MBEDTLS_PK_UNIQUE_VALID_HANDLE ); |
| 1370 | |
| 1371 | return( MBEDTLS_PK_INVALID_HANDLE ); |
| 1372 | |
| 1373 | #else /* MBEDTLS_PK_SINGLE_TYPE */ |
| 1374 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 1375 | switch( pk_type ) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1376 | #if defined(MBEDTLS_RSA_C) |
| 1377 | case MBEDTLS_PK_RSA: |
| 1378 | return( &mbedtls_rsa_info ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 1379 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1380 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1381 | case MBEDTLS_PK_ECKEY_DH: |
| 1382 | return( &mbedtls_eckeydh_info ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 1383 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1384 | #if defined(MBEDTLS_ECDSA_C) |
| 1385 | case MBEDTLS_PK_ECDSA: |
| 1386 | return( &mbedtls_ecdsa_info ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 1387 | #endif |
Hanno Becker | adf11e1 | 2019-08-21 13:03:44 +0100 | [diff] [blame] | 1388 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 1389 | case MBEDTLS_PK_ECKEY: |
| 1390 | return( &mbedtls_uecc_eckey_info ); |
| 1391 | #else /* MBEDTLS_USE_TINYCRYPT */ |
| 1392 | #if defined(MBEDTLS_ECP_C) |
| 1393 | case MBEDTLS_PK_ECKEY: |
| 1394 | return( &mbedtls_eckey_info ); |
| 1395 | #endif |
Jarno Lamsa | 42b83db | 2019-04-16 16:48:22 +0300 | [diff] [blame] | 1396 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1397 | /* MBEDTLS_PK_RSA_ALT omitted on purpose */ |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 1398 | default: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1399 | return( NULL ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 1400 | } |
Manuel Pégourié-Gonnard | f8b7c7f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1401 | #endif /* MBEDTLS_PK_SINGLE_TYPE */ |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | /* |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 1405 | * Initialise context |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1406 | */ |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1407 | int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info ) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1408 | { |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1409 | PK_VALIDATE_RET( ctx != NULL ); |
Manuel Pégourié-Gonnard | 073c1e1 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1410 | if( info == MBEDTLS_PK_INVALID_HANDLE ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1411 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 073c1e1 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1412 | |
| 1413 | #if !defined(MBEDTLS_PK_SINGLE_TYPE) |
| 1414 | if( ctx->pk_info != NULL ) |
| 1415 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
| 1416 | |
| 1417 | ctx->pk_info = info; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1418 | |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1419 | if( ( ctx->pk_ctx = pk_info_ctx_alloc_func( info ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1420 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | a77e9b5 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1421 | #else |
| 1422 | (void) ctx; |
| 1423 | #endif |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1424 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1425 | return( 0 ); |
| 1426 | } |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1427 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1428 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | b4fae57 | 2014-01-20 11:22:25 +0100 | [diff] [blame] | 1429 | /* |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1430 | * Initialize an RSA-alt context |
| 1431 | */ |
Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 1432 | int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1433 | mbedtls_pk_rsa_alt_decrypt_func decrypt_func, |
| 1434 | mbedtls_pk_rsa_alt_sign_func sign_func, |
| 1435 | mbedtls_pk_rsa_alt_key_len_func key_len_func ) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1436 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1437 | mbedtls_rsa_alt_context *rsa_alt; |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1438 | mbedtls_pk_handle_t info = &mbedtls_rsa_alt_info; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1439 | |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1440 | PK_VALIDATE_RET( ctx != NULL ); |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1441 | if( MBEDTLS_PK_CTX_IS_VALID( ctx ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1442 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1443 | |
| 1444 | if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1445 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1446 | |
| 1447 | ctx->pk_info = info; |
| 1448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1450 | |
| 1451 | rsa_alt->key = key; |
| 1452 | rsa_alt->decrypt_func = decrypt_func; |
| 1453 | rsa_alt->sign_func = sign_func; |
| 1454 | rsa_alt->key_len_func = key_len_func; |
| 1455 | |
| 1456 | return( 0 ); |
| 1457 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1458 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1459 | |
| 1460 | /* |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1461 | * Tell if a PK can do the operations of the given type |
| 1462 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1463 | int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1464 | { |
Gilles Peskine | 6af45ec | 2018-12-19 17:52:05 +0100 | [diff] [blame] | 1465 | /* A context with null pk_info is not set up yet and can't do anything. |
| 1466 | * For backward compatibility, also accept NULL instead of a context |
| 1467 | * pointer. */ |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1468 | if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1469 | return( 0 ); |
| 1470 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1471 | return( pk_info_can_do( MBEDTLS_PK_CTX_INFO( ctx ), type ) ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1475 | * Helper for mbedtls_pk_sign and mbedtls_pk_verify |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 1476 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1477 | static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len ) |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 1478 | { |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1479 | mbedtls_md_handle_t md_info; |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 1480 | |
| 1481 | if( *hash_len != 0 ) |
| 1482 | return( 0 ); |
| 1483 | |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1484 | if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == |
| 1485 | MBEDTLS_MD_INVALID_HANDLE ) |
| 1486 | { |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 1487 | return( -1 ); |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1488 | } |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 1489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1490 | *hash_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 1491 | return( 0 ); |
| 1492 | } |
| 1493 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1494 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1495 | /* |
| 1496 | * Helper to set up a restart context if needed |
| 1497 | */ |
| 1498 | static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx, |
Manuel Pégourié-Gonnard | 020d9ba | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1499 | mbedtls_pk_handle_t info ) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1500 | { |
Manuel Pégourié-Gonnard | c8c12b6 | 2018-07-02 13:09:39 +0200 | [diff] [blame] | 1501 | /* Don't do anything if already set up or invalid */ |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1502 | if( ctx == NULL || MBEDTLS_PK_CTX_IS_VALID( ctx ) ) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1503 | return( 0 ); |
| 1504 | |
| 1505 | /* Should never happen when we're called */ |
| 1506 | if( info->rs_alloc_func == NULL || info->rs_free_func == NULL ) |
| 1507 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
| 1508 | |
| 1509 | if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL ) |
| 1510 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); |
| 1511 | |
| 1512 | ctx->pk_info = info; |
| 1513 | |
| 1514 | return( 0 ); |
| 1515 | } |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1516 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1517 | |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 1518 | /* |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1519 | * Verify a signature (restartable) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1520 | */ |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1521 | int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx, |
| 1522 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 1523 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1524 | const unsigned char *sig, size_t sig_len, |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 1525 | mbedtls_pk_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1526 | { |
Jarno Lamsa | b83a213 | 2019-12-13 14:40:06 +0200 | [diff] [blame] | 1527 | volatile int verify_ret = MBEDTLS_ERR_PK_HW_ACCEL_FAILED; |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1528 | PK_VALIDATE_RET( ctx != NULL ); |
Gilles Peskine | ee3cfec | 2018-12-19 17:10:02 +0100 | [diff] [blame] | 1529 | PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) || |
| 1530 | hash != NULL ); |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1531 | PK_VALIDATE_RET( sig != NULL ); |
| 1532 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1533 | if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) || |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 1534 | pk_hashlen_helper( md_alg, &hash_len ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1535 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1536 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1537 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | d55f776 | 2017-08-18 17:40:15 +0200 | [diff] [blame] | 1538 | /* optimization: use non-restartable version if restart disabled */ |
| 1539 | if( rs_ctx != NULL && |
Manuel Pégourié-Gonnard | b843b15 | 2018-10-16 10:41:31 +0200 | [diff] [blame] | 1540 | mbedtls_ecp_restart_is_enabled() && |
Manuel Pégourié-Gonnard | d55f776 | 2017-08-18 17:40:15 +0200 | [diff] [blame] | 1541 | ctx->pk_info->verify_rs_func != NULL ) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1542 | { |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1543 | int ret; |
| 1544 | |
| 1545 | if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 ) |
| 1546 | return( ret ); |
| 1547 | |
| 1548 | ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx, |
| 1549 | md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx ); |
| 1550 | |
| 1551 | if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 1552 | mbedtls_pk_restart_free( rs_ctx ); |
| 1553 | |
| 1554 | return( ret ); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1555 | } |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1556 | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1557 | (void) rs_ctx; |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1558 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1559 | |
Jarno Lamsa | b83a213 | 2019-12-13 14:40:06 +0200 | [diff] [blame] | 1560 | verify_ret = pk_info_verify_func( MBEDTLS_PK_CTX_INFO( ctx ), |
| 1561 | ctx->pk_ctx, md_alg, hash, hash_len, sig, sig_len ); |
Jarno Lamsa | 91dbb79 | 2019-12-16 12:20:27 +0200 | [diff] [blame] | 1562 | |
Jarno Lamsa | b83a213 | 2019-12-13 14:40:06 +0200 | [diff] [blame] | 1563 | if( verify_ret == 0 ) |
| 1564 | { |
Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 1565 | mbedtls_platform_random_delay(); |
Jarno Lamsa | b83a213 | 2019-12-13 14:40:06 +0200 | [diff] [blame] | 1566 | if( verify_ret == 0 ) |
| 1567 | { |
| 1568 | return( verify_ret ); |
| 1569 | } |
Jarno Lamsa | 91dbb79 | 2019-12-16 12:20:27 +0200 | [diff] [blame] | 1570 | else |
| 1571 | { |
| 1572 | verify_ret = MBEDTLS_ERR_PK_HW_ACCEL_FAILED; |
| 1573 | } |
Jarno Lamsa | b83a213 | 2019-12-13 14:40:06 +0200 | [diff] [blame] | 1574 | } |
| 1575 | |
Jarno Lamsa | 91dbb79 | 2019-12-16 12:20:27 +0200 | [diff] [blame] | 1576 | return( verify_ret ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1580 | * Verify a signature |
| 1581 | */ |
| 1582 | int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
| 1583 | const unsigned char *hash, size_t hash_len, |
| 1584 | const unsigned char *sig, size_t sig_len ) |
| 1585 | { |
| 1586 | return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len, |
| 1587 | sig, sig_len, NULL ) ); |
| 1588 | } |
| 1589 | |
| 1590 | /* |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1591 | * Verify a signature with options |
| 1592 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1593 | int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options, |
| 1594 | mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1595 | const unsigned char *hash, size_t hash_len, |
| 1596 | const unsigned char *sig, size_t sig_len ) |
| 1597 | { |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1598 | PK_VALIDATE_RET( ctx != NULL ); |
Gilles Peskine | ee3cfec | 2018-12-19 17:10:02 +0100 | [diff] [blame] | 1599 | PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) || |
| 1600 | hash != NULL ); |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1601 | PK_VALIDATE_RET( sig != NULL ); |
| 1602 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1603 | if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1604 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1605 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1606 | if( ! mbedtls_pk_can_do( ctx, type ) ) |
| 1607 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1608 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1609 | if( type == MBEDTLS_PK_RSASSA_PSS ) |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1610 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1611 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1612 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1613 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1614 | |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 1615 | #if SIZE_MAX > UINT_MAX |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 1616 | if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) |
| 1617 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 1618 | #endif /* SIZE_MAX > UINT_MAX */ |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 1619 | |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1620 | if( options == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1621 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1623 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) options; |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1624 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1625 | if( sig_len < mbedtls_pk_get_len( ctx ) ) |
| 1626 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1628 | ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ), |
| 1629 | NULL, NULL, MBEDTLS_RSA_PUBLIC, |
Sander Niemeijer | ef5087d | 2014-08-16 12:45:52 +0200 | [diff] [blame] | 1630 | md_alg, (unsigned int) hash_len, hash, |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1631 | pss_opts->mgf1_hash_id, |
| 1632 | pss_opts->expected_salt_len, |
| 1633 | sig ); |
| 1634 | if( ret != 0 ) |
| 1635 | return( ret ); |
| 1636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1637 | if( sig_len > mbedtls_pk_get_len( ctx ) ) |
| 1638 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1639 | |
| 1640 | return( 0 ); |
| 1641 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1642 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 1643 | #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */ |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | /* General case: no options */ |
| 1647 | if( options != NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1648 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1650 | return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) ); |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /* |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1654 | * Make a signature (restartable) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1655 | */ |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1656 | int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx, |
| 1657 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1658 | const unsigned char *hash, size_t hash_len, |
| 1659 | unsigned char *sig, size_t *sig_len, |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1660 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 1661 | mbedtls_pk_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1662 | { |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1663 | PK_VALIDATE_RET( ctx != NULL ); |
Gilles Peskine | ee3cfec | 2018-12-19 17:10:02 +0100 | [diff] [blame] | 1664 | PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) || |
| 1665 | hash != NULL ); |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1666 | PK_VALIDATE_RET( sig != NULL ); |
| 1667 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1668 | if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) || |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 1669 | pk_hashlen_helper( md_alg, &hash_len ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1670 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1671 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1672 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | d55f776 | 2017-08-18 17:40:15 +0200 | [diff] [blame] | 1673 | /* optimization: use non-restartable version if restart disabled */ |
| 1674 | if( rs_ctx != NULL && |
Manuel Pégourié-Gonnard | b843b15 | 2018-10-16 10:41:31 +0200 | [diff] [blame] | 1675 | mbedtls_ecp_restart_is_enabled() && |
Manuel Pégourié-Gonnard | d55f776 | 2017-08-18 17:40:15 +0200 | [diff] [blame] | 1676 | ctx->pk_info->sign_rs_func != NULL ) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1677 | { |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1678 | int ret; |
| 1679 | |
| 1680 | if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 ) |
| 1681 | return( ret ); |
| 1682 | |
| 1683 | ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg, |
| 1684 | hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx ); |
| 1685 | |
| 1686 | if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 1687 | mbedtls_pk_restart_free( rs_ctx ); |
| 1688 | |
| 1689 | return( ret ); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1690 | } |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1691 | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1692 | (void) rs_ctx; |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1693 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1694 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1695 | return( pk_info_sign_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, |
| 1696 | md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | /* |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1700 | * Make a signature |
| 1701 | */ |
| 1702 | int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
| 1703 | const unsigned char *hash, size_t hash_len, |
| 1704 | unsigned char *sig, size_t *sig_len, |
| 1705 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1706 | { |
| 1707 | return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len, |
| 1708 | sig, sig_len, f_rng, p_rng, NULL ) ); |
| 1709 | } |
| 1710 | |
| 1711 | /* |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1712 | * Decrypt message |
| 1713 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1714 | int mbedtls_pk_decrypt( mbedtls_pk_context *ctx, |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1715 | const unsigned char *input, size_t ilen, |
| 1716 | unsigned char *output, size_t *olen, size_t osize, |
| 1717 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1718 | { |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1719 | PK_VALIDATE_RET( ctx != NULL ); |
| 1720 | PK_VALIDATE_RET( input != NULL || ilen == 0 ); |
| 1721 | PK_VALIDATE_RET( output != NULL || osize == 0 ); |
| 1722 | PK_VALIDATE_RET( olen != NULL ); |
| 1723 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1724 | if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1725 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1726 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1727 | return( pk_info_decrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, |
| 1728 | input, ilen, output, olen, osize, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | /* |
| 1732 | * Encrypt message |
| 1733 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1734 | int mbedtls_pk_encrypt( mbedtls_pk_context *ctx, |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1735 | const unsigned char *input, size_t ilen, |
| 1736 | unsigned char *output, size_t *olen, size_t osize, |
| 1737 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1738 | { |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1739 | PK_VALIDATE_RET( ctx != NULL ); |
| 1740 | PK_VALIDATE_RET( input != NULL || ilen == 0 ); |
| 1741 | PK_VALIDATE_RET( output != NULL || osize == 0 ); |
| 1742 | PK_VALIDATE_RET( olen != NULL ); |
| 1743 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1744 | if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1745 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1746 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1747 | return( pk_info_encrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, |
| 1748 | input, ilen, output, olen, osize, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | /* |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1752 | * Check public-private key pair |
| 1753 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1754 | int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv ) |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1755 | { |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1756 | PK_VALIDATE_RET( pub != NULL ); |
| 1757 | PK_VALIDATE_RET( prv != NULL ); |
| 1758 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1759 | if( !MBEDTLS_PK_CTX_IS_VALID( pub ) || !MBEDTLS_PK_CTX_IS_VALID( prv ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1760 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1761 | |
Manuel Pégourié-Gonnard | 2d9466f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1762 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1763 | if( pk_info_type( prv->pk_info ) == MBEDTLS_PK_RSA_ALT ) |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1764 | { |
Manuel Pégourié-Gonnard | c10f092 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1765 | if( pk_info_type( pub->pk_info ) != MBEDTLS_PK_RSA ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1766 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1767 | } |
| 1768 | else |
Manuel Pégourié-Gonnard | 2d9466f | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1769 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1770 | { |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1771 | if( MBEDTLS_PK_CTX_INFO( pub ) != MBEDTLS_PK_CTX_INFO( prv ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1772 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1773 | } |
| 1774 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1775 | return( pk_info_check_pair_func( MBEDTLS_PK_CTX_INFO( prv ), |
| 1776 | pub->pk_ctx, prv->pk_ctx ) ); |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | /* |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1780 | * Get key size in bits |
| 1781 | */ |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 1782 | size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1783 | { |
Gilles Peskine | 6af45ec | 2018-12-19 17:52:05 +0100 | [diff] [blame] | 1784 | /* For backward compatibility, accept NULL or a context that |
| 1785 | * isn't set up yet, and return a fake value that should be safe. */ |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1786 | if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1787 | return( 0 ); |
| 1788 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1789 | return( pk_info_get_bitlen( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx ) ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1790 | } |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 1791 | |
| 1792 | /* |
| 1793 | * Export debug information |
| 1794 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1795 | int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items ) |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 1796 | { |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1797 | PK_VALIDATE_RET( ctx != NULL ); |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1798 | if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1799 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 1800 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1801 | return( pk_info_debug_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, items ) ); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 1802 | } |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 1803 | |
| 1804 | /* |
| 1805 | * Access the PK type name |
| 1806 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1807 | const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx ) |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 1808 | { |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1809 | if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ) |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 1810 | return( "invalid PK" ); |
| 1811 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1812 | return( pk_info_name( MBEDTLS_PK_CTX_INFO( ctx ) ) ); |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 1813 | } |
Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 1814 | |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1815 | /* |
| 1816 | * Access the PK type |
| 1817 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1818 | mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx ) |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1819 | { |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1820 | if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1821 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1822 | |
Manuel Pégourié-Gonnard | 4223ce4 | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 1823 | return( pk_info_type( MBEDTLS_PK_CTX_INFO( ctx ) ) ); |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1824 | } |
| 1825 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1826 | #endif /* MBEDTLS_PK_C */ |