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