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