| 1 | /* |
| 2 | * Public Key abstraction layer |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #include "common.h" |
| 21 | |
| 22 | #if defined(MBEDTLS_PK_C) |
| 23 | #include "mbedtls/pk.h" |
| 24 | #include "mbedtls/pk_internal.h" |
| 25 | |
| 26 | #include "mbedtls/platform_util.h" |
| 27 | #include "mbedtls/error.h" |
| 28 | |
| 29 | #if defined(MBEDTLS_RSA_C) |
| 30 | #include "mbedtls/rsa.h" |
| 31 | #endif |
| 32 | #if defined(MBEDTLS_ECP_C) |
| 33 | #include "mbedtls/ecp.h" |
| 34 | #endif |
| 35 | #if defined(MBEDTLS_ECDSA_C) |
| 36 | #include "mbedtls/ecdsa.h" |
| 37 | #endif |
| 38 | |
| 39 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 40 | #include "mbedtls/psa_util.h" |
| 41 | #endif |
| 42 | |
| 43 | #include <limits.h> |
| 44 | #include <stdint.h> |
| 45 | |
| 46 | /* Parameter validation macros based on platform_util.h */ |
| 47 | #define PK_VALIDATE_RET(cond) \ |
| 48 | MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA) |
| 49 | #define PK_VALIDATE(cond) \ |
| 50 | MBEDTLS_INTERNAL_VALIDATE(cond) |
| 51 | |
| 52 | /* |
| 53 | * Initialise a mbedtls_pk_context |
| 54 | */ |
| 55 | void mbedtls_pk_init(mbedtls_pk_context *ctx) |
| 56 | { |
| 57 | PK_VALIDATE(ctx != NULL); |
| 58 | |
| 59 | ctx->pk_info = NULL; |
| 60 | ctx->pk_ctx = NULL; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Free (the components of) a mbedtls_pk_context |
| 65 | */ |
| 66 | void mbedtls_pk_free(mbedtls_pk_context *ctx) |
| 67 | { |
| 68 | if (ctx == NULL) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | if (ctx->pk_info != NULL) { |
| 73 | ctx->pk_info->ctx_free_func(ctx->pk_ctx); |
| 74 | } |
| 75 | |
| 76 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context)); |
| 77 | } |
| 78 | |
| 79 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 80 | /* |
| 81 | * Initialize a restart context |
| 82 | */ |
| 83 | void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx) |
| 84 | { |
| 85 | PK_VALIDATE(ctx != NULL); |
| 86 | ctx->pk_info = NULL; |
| 87 | ctx->rs_ctx = NULL; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Free the components of a restart context |
| 92 | */ |
| 93 | void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx) |
| 94 | { |
| 95 | if (ctx == NULL || ctx->pk_info == NULL || |
| 96 | ctx->pk_info->rs_free_func == NULL) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | ctx->pk_info->rs_free_func(ctx->rs_ctx); |
| 101 | |
| 102 | ctx->pk_info = NULL; |
| 103 | ctx->rs_ctx = NULL; |
| 104 | } |
| 105 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 106 | |
| 107 | /* |
| 108 | * Get pk_info structure from type |
| 109 | */ |
| 110 | const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type) |
| 111 | { |
| 112 | switch (pk_type) { |
| 113 | #if defined(MBEDTLS_RSA_C) |
| 114 | case MBEDTLS_PK_RSA: |
| 115 | return &mbedtls_rsa_info; |
| 116 | #endif |
| 117 | #if defined(MBEDTLS_ECP_C) |
| 118 | case MBEDTLS_PK_ECKEY: |
| 119 | return &mbedtls_eckey_info; |
| 120 | case MBEDTLS_PK_ECKEY_DH: |
| 121 | return &mbedtls_eckeydh_info; |
| 122 | #endif |
| 123 | #if defined(MBEDTLS_ECDSA_C) |
| 124 | case MBEDTLS_PK_ECDSA: |
| 125 | return &mbedtls_ecdsa_info; |
| 126 | #endif |
| 127 | /* MBEDTLS_PK_RSA_ALT omitted on purpose */ |
| 128 | default: |
| 129 | return NULL; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Initialise context |
| 135 | */ |
| 136 | int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info) |
| 137 | { |
| 138 | PK_VALIDATE_RET(ctx != NULL); |
| 139 | if (info == NULL || ctx->pk_info != NULL) { |
| 140 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 141 | } |
| 142 | |
| 143 | if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) { |
| 144 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 145 | } |
| 146 | |
| 147 | ctx->pk_info = info; |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 153 | /* |
| 154 | * Initialise a PSA-wrapping context |
| 155 | */ |
| 156 | int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx, |
| 157 | const psa_key_id_t key) |
| 158 | { |
| 159 | const mbedtls_pk_info_t * const info = &mbedtls_pk_opaque_info; |
| 160 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 161 | psa_key_id_t *pk_ctx; |
| 162 | psa_key_type_t type; |
| 163 | |
| 164 | if (ctx == NULL || ctx->pk_info != NULL) { |
| 165 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 166 | } |
| 167 | |
| 168 | if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) { |
| 169 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 170 | } |
| 171 | type = psa_get_key_type(&attributes); |
| 172 | psa_reset_key_attributes(&attributes); |
| 173 | |
| 174 | /* Current implementation of can_do() relies on this. */ |
| 175 | if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
| 176 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 177 | } |
| 178 | |
| 179 | if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) { |
| 180 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 181 | } |
| 182 | |
| 183 | ctx->pk_info = info; |
| 184 | |
| 185 | pk_ctx = (psa_key_id_t *) ctx->pk_ctx; |
| 186 | *pk_ctx = key; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 191 | |
| 192 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
| 193 | /* |
| 194 | * Initialize an RSA-alt context |
| 195 | */ |
| 196 | int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key, |
| 197 | mbedtls_pk_rsa_alt_decrypt_func decrypt_func, |
| 198 | mbedtls_pk_rsa_alt_sign_func sign_func, |
| 199 | mbedtls_pk_rsa_alt_key_len_func key_len_func) |
| 200 | { |
| 201 | mbedtls_rsa_alt_context *rsa_alt; |
| 202 | const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info; |
| 203 | |
| 204 | PK_VALIDATE_RET(ctx != NULL); |
| 205 | if (ctx->pk_info != NULL) { |
| 206 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 207 | } |
| 208 | |
| 209 | if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) { |
| 210 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 211 | } |
| 212 | |
| 213 | ctx->pk_info = info; |
| 214 | |
| 215 | rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx; |
| 216 | |
| 217 | rsa_alt->key = key; |
| 218 | rsa_alt->decrypt_func = decrypt_func; |
| 219 | rsa_alt->sign_func = sign_func; |
| 220 | rsa_alt->key_len_func = key_len_func; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
| 225 | |
| 226 | /* |
| 227 | * Tell if a PK can do the operations of the given type |
| 228 | */ |
| 229 | int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type) |
| 230 | { |
| 231 | /* A context with null pk_info is not set up yet and can't do anything. |
| 232 | * For backward compatibility, also accept NULL instead of a context |
| 233 | * pointer. */ |
| 234 | if (ctx == NULL || ctx->pk_info == NULL) { |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | return ctx->pk_info->can_do(type); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Helper for mbedtls_pk_sign and mbedtls_pk_verify |
| 243 | */ |
| 244 | static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len) |
| 245 | { |
| 246 | const mbedtls_md_info_t *md_info; |
| 247 | |
| 248 | if (*hash_len != 0 && md_alg == MBEDTLS_MD_NONE) { |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | if ((md_info = mbedtls_md_info_from_type(md_alg)) == NULL) { |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | if (*hash_len != 0 && *hash_len != mbedtls_md_get_size(md_info)) { |
| 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | *hash_len = mbedtls_md_get_size(md_info); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 265 | /* |
| 266 | * Helper to set up a restart context if needed |
| 267 | */ |
| 268 | static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx, |
| 269 | const mbedtls_pk_info_t *info) |
| 270 | { |
| 271 | /* Don't do anything if already set up or invalid */ |
| 272 | if (ctx == NULL || ctx->pk_info != NULL) { |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /* Should never happen when we're called */ |
| 277 | if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) { |
| 278 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 279 | } |
| 280 | |
| 281 | if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) { |
| 282 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 283 | } |
| 284 | |
| 285 | ctx->pk_info = info; |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 290 | |
| 291 | /* |
| 292 | * Verify a signature (restartable) |
| 293 | */ |
| 294 | int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx, |
| 295 | mbedtls_md_type_t md_alg, |
| 296 | const unsigned char *hash, size_t hash_len, |
| 297 | const unsigned char *sig, size_t sig_len, |
| 298 | mbedtls_pk_restart_ctx *rs_ctx) |
| 299 | { |
| 300 | PK_VALIDATE_RET(ctx != NULL); |
| 301 | PK_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hash_len == 0) || |
| 302 | hash != NULL); |
| 303 | PK_VALIDATE_RET(sig != NULL); |
| 304 | |
| 305 | if (ctx->pk_info == NULL || |
| 306 | pk_hashlen_helper(md_alg, &hash_len) != 0) { |
| 307 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 308 | } |
| 309 | |
| 310 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 311 | /* optimization: use non-restartable version if restart disabled */ |
| 312 | if (rs_ctx != NULL && |
| 313 | mbedtls_ecp_restart_is_enabled() && |
| 314 | ctx->pk_info->verify_rs_func != NULL) { |
| 315 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 316 | |
| 317 | if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) { |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | ret = ctx->pk_info->verify_rs_func(ctx->pk_ctx, |
| 322 | md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx); |
| 323 | |
| 324 | if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 325 | mbedtls_pk_restart_free(rs_ctx); |
| 326 | } |
| 327 | |
| 328 | return ret; |
| 329 | } |
| 330 | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 331 | (void) rs_ctx; |
| 332 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 333 | |
| 334 | if (ctx->pk_info->verify_func == NULL) { |
| 335 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 336 | } |
| 337 | |
| 338 | return ctx->pk_info->verify_func(ctx->pk_ctx, md_alg, hash, hash_len, |
| 339 | sig, sig_len); |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Verify a signature |
| 344 | */ |
| 345 | int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
| 346 | const unsigned char *hash, size_t hash_len, |
| 347 | const unsigned char *sig, size_t sig_len) |
| 348 | { |
| 349 | return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len, |
| 350 | sig, sig_len, NULL); |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Verify a signature with options |
| 355 | */ |
| 356 | int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options, |
| 357 | mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
| 358 | const unsigned char *hash, size_t hash_len, |
| 359 | const unsigned char *sig, size_t sig_len) |
| 360 | { |
| 361 | PK_VALIDATE_RET(ctx != NULL); |
| 362 | PK_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hash_len == 0) || |
| 363 | hash != NULL); |
| 364 | PK_VALIDATE_RET(sig != NULL); |
| 365 | |
| 366 | if (ctx->pk_info == NULL) { |
| 367 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 368 | } |
| 369 | |
| 370 | if (!mbedtls_pk_can_do(ctx, type)) { |
| 371 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 372 | } |
| 373 | |
| 374 | if (type == MBEDTLS_PK_RSASSA_PSS) { |
| 375 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 376 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 377 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
| 378 | |
| 379 | #if SIZE_MAX > UINT_MAX |
| 380 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
| 381 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 382 | } |
| 383 | #endif /* SIZE_MAX > UINT_MAX */ |
| 384 | |
| 385 | if (options == NULL) { |
| 386 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 387 | } |
| 388 | |
| 389 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) options; |
| 390 | |
| 391 | if (sig_len < mbedtls_pk_get_len(ctx)) { |
| 392 | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
| 393 | } |
| 394 | |
| 395 | ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx), |
| 396 | NULL, NULL, MBEDTLS_RSA_PUBLIC, |
| 397 | md_alg, (unsigned int) hash_len, hash, |
| 398 | pss_opts->mgf1_hash_id, |
| 399 | pss_opts->expected_salt_len, |
| 400 | sig); |
| 401 | if (ret != 0) { |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | if (sig_len > mbedtls_pk_get_len(ctx)) { |
| 406 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 407 | } |
| 408 | |
| 409 | return 0; |
| 410 | #else |
| 411 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 412 | #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */ |
| 413 | } |
| 414 | |
| 415 | /* General case: no options */ |
| 416 | if (options != NULL) { |
| 417 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 418 | } |
| 419 | |
| 420 | return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len); |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Make a signature (restartable) |
| 425 | */ |
| 426 | int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx, |
| 427 | mbedtls_md_type_t md_alg, |
| 428 | const unsigned char *hash, size_t hash_len, |
| 429 | unsigned char *sig, size_t *sig_len, |
| 430 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 431 | mbedtls_pk_restart_ctx *rs_ctx) |
| 432 | { |
| 433 | PK_VALIDATE_RET(ctx != NULL); |
| 434 | PK_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hash_len == 0) || |
| 435 | hash != NULL); |
| 436 | PK_VALIDATE_RET(sig != NULL); |
| 437 | |
| 438 | if (ctx->pk_info == NULL || |
| 439 | pk_hashlen_helper(md_alg, &hash_len) != 0) { |
| 440 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 441 | } |
| 442 | |
| 443 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 444 | /* optimization: use non-restartable version if restart disabled */ |
| 445 | if (rs_ctx != NULL && |
| 446 | mbedtls_ecp_restart_is_enabled() && |
| 447 | ctx->pk_info->sign_rs_func != NULL) { |
| 448 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 449 | |
| 450 | if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) { |
| 451 | return ret; |
| 452 | } |
| 453 | |
| 454 | ret = ctx->pk_info->sign_rs_func(ctx->pk_ctx, md_alg, |
| 455 | hash, hash_len, sig, sig_len, f_rng, p_rng, |
| 456 | rs_ctx->rs_ctx); |
| 457 | |
| 458 | if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 459 | mbedtls_pk_restart_free(rs_ctx); |
| 460 | } |
| 461 | |
| 462 | return ret; |
| 463 | } |
| 464 | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 465 | (void) rs_ctx; |
| 466 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 467 | |
| 468 | if (ctx->pk_info->sign_func == NULL) { |
| 469 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 470 | } |
| 471 | |
| 472 | return ctx->pk_info->sign_func(ctx->pk_ctx, md_alg, hash, hash_len, |
| 473 | sig, sig_len, f_rng, p_rng); |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Make a signature |
| 478 | */ |
| 479 | int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
| 480 | const unsigned char *hash, size_t hash_len, |
| 481 | unsigned char *sig, size_t *sig_len, |
| 482 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
| 483 | { |
| 484 | return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len, |
| 485 | sig, sig_len, f_rng, p_rng, NULL); |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Decrypt message |
| 490 | */ |
| 491 | int mbedtls_pk_decrypt(mbedtls_pk_context *ctx, |
| 492 | const unsigned char *input, size_t ilen, |
| 493 | unsigned char *output, size_t *olen, size_t osize, |
| 494 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
| 495 | { |
| 496 | PK_VALIDATE_RET(ctx != NULL); |
| 497 | PK_VALIDATE_RET(input != NULL || ilen == 0); |
| 498 | PK_VALIDATE_RET(output != NULL || osize == 0); |
| 499 | PK_VALIDATE_RET(olen != NULL); |
| 500 | |
| 501 | if (ctx->pk_info == NULL) { |
| 502 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 503 | } |
| 504 | |
| 505 | if (ctx->pk_info->decrypt_func == NULL) { |
| 506 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 507 | } |
| 508 | |
| 509 | return ctx->pk_info->decrypt_func(ctx->pk_ctx, input, ilen, |
| 510 | output, olen, osize, f_rng, p_rng); |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Encrypt message |
| 515 | */ |
| 516 | int mbedtls_pk_encrypt(mbedtls_pk_context *ctx, |
| 517 | const unsigned char *input, size_t ilen, |
| 518 | unsigned char *output, size_t *olen, size_t osize, |
| 519 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
| 520 | { |
| 521 | PK_VALIDATE_RET(ctx != NULL); |
| 522 | PK_VALIDATE_RET(input != NULL || ilen == 0); |
| 523 | PK_VALIDATE_RET(output != NULL || osize == 0); |
| 524 | PK_VALIDATE_RET(olen != NULL); |
| 525 | |
| 526 | if (ctx->pk_info == NULL) { |
| 527 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 528 | } |
| 529 | |
| 530 | if (ctx->pk_info->encrypt_func == NULL) { |
| 531 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 532 | } |
| 533 | |
| 534 | return ctx->pk_info->encrypt_func(ctx->pk_ctx, input, ilen, |
| 535 | output, olen, osize, f_rng, p_rng); |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Check public-private key pair |
| 540 | */ |
| 541 | int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv) |
| 542 | { |
| 543 | PK_VALIDATE_RET(pub != NULL); |
| 544 | PK_VALIDATE_RET(prv != NULL); |
| 545 | |
| 546 | if (pub->pk_info == NULL || |
| 547 | prv->pk_info == NULL) { |
| 548 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 549 | } |
| 550 | |
| 551 | if (prv->pk_info->check_pair_func == NULL) { |
| 552 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 553 | } |
| 554 | |
| 555 | if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) { |
| 556 | if (pub->pk_info->type != MBEDTLS_PK_RSA) { |
| 557 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 558 | } |
| 559 | } else { |
| 560 | if (pub->pk_info != prv->pk_info) { |
| 561 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return prv->pk_info->check_pair_func(pub->pk_ctx, prv->pk_ctx); |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Get key size in bits |
| 570 | */ |
| 571 | size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx) |
| 572 | { |
| 573 | /* For backward compatibility, accept NULL or a context that |
| 574 | * isn't set up yet, and return a fake value that should be safe. */ |
| 575 | if (ctx == NULL || ctx->pk_info == NULL) { |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | return ctx->pk_info->get_bitlen(ctx->pk_ctx); |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Export debug information |
| 584 | */ |
| 585 | int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items) |
| 586 | { |
| 587 | PK_VALIDATE_RET(ctx != NULL); |
| 588 | if (ctx->pk_info == NULL) { |
| 589 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 590 | } |
| 591 | |
| 592 | if (ctx->pk_info->debug_func == NULL) { |
| 593 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 594 | } |
| 595 | |
| 596 | ctx->pk_info->debug_func(ctx->pk_ctx, items); |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * Access the PK type name |
| 602 | */ |
| 603 | const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx) |
| 604 | { |
| 605 | if (ctx == NULL || ctx->pk_info == NULL) { |
| 606 | return "invalid PK" ; |
| 607 | } |
| 608 | |
| 609 | return ctx->pk_info->name; |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Access the PK type |
| 614 | */ |
| 615 | mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx) |
| 616 | { |
| 617 | if (ctx == NULL || ctx->pk_info == NULL) { |
| 618 | return MBEDTLS_PK_NONE; |
| 619 | } |
| 620 | |
| 621 | return ctx->pk_info->type; |
| 622 | } |
| 623 | |
| 624 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 625 | /* |
| 626 | * Load the key to a PSA key slot, |
| 627 | * then turn the PK context into a wrapper for that key slot. |
| 628 | * |
| 629 | * Currently only works for EC private keys. |
| 630 | */ |
| 631 | int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk, |
| 632 | psa_key_id_t *key, |
| 633 | psa_algorithm_t hash_alg) |
| 634 | { |
| 635 | #if !defined(MBEDTLS_ECP_C) |
| 636 | ((void) pk); |
| 637 | ((void) key); |
| 638 | ((void) hash_alg); |
| 639 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 640 | #else |
| 641 | const mbedtls_ecp_keypair *ec; |
| 642 | unsigned char d[MBEDTLS_ECP_MAX_BYTES]; |
| 643 | size_t d_len; |
| 644 | psa_ecc_family_t curve_id; |
| 645 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 646 | psa_key_type_t key_type; |
| 647 | size_t bits; |
| 648 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 649 | psa_status_t status; |
| 650 | |
| 651 | /* export the private key material in the format PSA wants */ |
| 652 | if (mbedtls_pk_get_type(pk) != MBEDTLS_PK_ECKEY) { |
| 653 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 654 | } |
| 655 | |
| 656 | ec = mbedtls_pk_ec(*pk); |
| 657 | d_len = (ec->grp.nbits + 7) / 8; |
| 658 | if ((ret = mbedtls_mpi_write_binary(&ec->d, d, d_len)) != 0) { |
| 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits); |
| 663 | key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id); |
| 664 | |
| 665 | /* prepare the key attributes */ |
| 666 | psa_set_key_type(&attributes, key_type); |
| 667 | psa_set_key_bits(&attributes, bits); |
| 668 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 669 | psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(hash_alg)); |
| 670 | |
| 671 | /* import private key into PSA */ |
| 672 | status = psa_import_key(&attributes, d, d_len, key); |
| 673 | mbedtls_platform_zeroize(d, sizeof(d)); |
| 674 | if (status != PSA_SUCCESS) { |
| 675 | return MBEDTLS_ERR_PK_HW_ACCEL_FAILED; |
| 676 | } |
| 677 | |
| 678 | /* make PK context wrap the key slot */ |
| 679 | mbedtls_pk_free(pk); |
| 680 | mbedtls_pk_init(pk); |
| 681 | |
| 682 | return mbedtls_pk_setup_opaque(pk, *key); |
| 683 | #endif /* MBEDTLS_ECP_C */ |
| 684 | } |
| 685 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 686 | #endif /* MBEDTLS_PK_C */ |
| 687 | |