Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key abstraction layer |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 8 | #include "common.h" |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10 | #if defined(MBEDTLS_PK_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 11 | #include "mbedtls/pk.h" |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 12 | #include "pk_wrap.h" |
Neil Armstrong | ca5b55f | 2022-03-15 15:00:55 +0100 | [diff] [blame] | 13 | #include "pkwrite.h" |
Valerio Setti | 3f00b84 | 2023-05-15 12:57:06 +0200 | [diff] [blame] | 14 | #include "pk_internal.h" |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 15 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 16 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 17 | #include "mbedtls/error.h" |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 18 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 19 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 20 | #include "mbedtls/rsa.h" |
Tomi Fontanilles | 573dc23 | 2023-12-10 14:57:51 +0200 | [diff] [blame] | 21 | #include "rsa_internal.h" |
| 22 | #endif |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 23 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/ecp.h" |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 25 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 27 | #include "mbedtls/ecdsa.h" |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 28 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 29 | |
Valerio Setti | 01ba66d | 2024-02-09 11:35:42 +0100 | [diff] [blame] | 30 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 31 | #include "psa_util_internal.h" |
Valerio Setti | 384fbde | 2024-01-02 13:26:40 +0100 | [diff] [blame] | 32 | #include "mbedtls/psa_util.h" |
Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 33 | #endif |
| 34 | |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 35 | #include <limits.h> |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 36 | #include <stdint.h> |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 37 | |
Valerio Setti | 070d95e | 2024-02-01 11:29:15 +0100 | [diff] [blame] | 38 | #define PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE \ |
| 39 | (PSA_EXPORT_KEY_PAIR_MAX_SIZE > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) ? \ |
| 40 | PSA_EXPORT_KEY_PAIR_MAX_SIZE : PSA_EXPORT_PUBLIC_KEY_MAX_SIZE |
| 41 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 42 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | * Initialise a mbedtls_pk_context |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 44 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | void mbedtls_pk_init(mbedtls_pk_context *ctx) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 46 | { |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 47 | ctx->pk_info = NULL; |
| 48 | ctx->pk_ctx = NULL; |
Tomi Fontanilles | bad170e | 2023-12-14 22:03:12 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 4f387ef | 2023-05-02 14:15:59 +0200 | [diff] [blame] | 50 | ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT; |
Tomi Fontanilles | bad170e | 2023-12-14 22:03:12 +0200 | [diff] [blame] | 51 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Valerio Setti | 722f8f7 | 2023-05-17 15:31:21 +0200 | [diff] [blame] | 52 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 53 | memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw)); |
| 54 | ctx->pub_raw_len = 0; |
| 55 | ctx->ec_family = 0; |
| 56 | ctx->ec_bits = 0; |
| 57 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | * Free (the components of) a mbedtls_pk_context |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 62 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | void mbedtls_pk_free(mbedtls_pk_context *ctx) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 64 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | if (ctx == NULL) { |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 66 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 67 | } |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 68 | |
Valerio Setti | e00954d | 2023-04-28 15:24:32 +0200 | [diff] [blame] | 69 | if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 70 | ctx->pk_info->ctx_free_func(ctx->pk_ctx); |
| 71 | } |
Manuel Pégourié-Gonnard | 1f73a65 | 2013-07-09 10:26:41 +0200 | [diff] [blame] | 72 | |
Valerio Setti | b536126 | 2023-05-18 18:51:58 +0200 | [diff] [blame] | 73 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 74 | /* The ownership of the priv_id key for opaque keys is external of the PK |
| 75 | * module. It's the user responsibility to clear it after use. */ |
| 76 | if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) { |
| 77 | psa_destroy_key(ctx->priv_id); |
| 78 | } |
| 79 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 80 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context)); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 84 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 85 | /* |
| 86 | * Initialize a restart context |
| 87 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 89 | { |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 90 | ctx->pk_info = NULL; |
| 91 | ctx->rs_ctx = NULL; |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Free the components of a restart context |
| 96 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 98 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | if (ctx == NULL || ctx->pk_info == NULL || |
| 100 | ctx->pk_info->rs_free_func == NULL) { |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 101 | return; |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 102 | } |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | ctx->pk_info->rs_free_func(ctx->rs_ctx); |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 105 | |
| 106 | ctx->pk_info = NULL; |
| 107 | ctx->rs_ctx = NULL; |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 108 | } |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 109 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 111 | /* |
| 112 | * Get pk_info structure from type |
| 113 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | const mbedtls_pk_info_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] | 115 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | switch (pk_type) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | #if defined(MBEDTLS_RSA_C) |
| 118 | case MBEDTLS_PK_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | return &mbedtls_rsa_info; |
Valerio Setti | 0d2980f | 2023-04-05 18:17:13 +0200 | [diff] [blame] | 120 | #endif /* MBEDTLS_RSA_C */ |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 121 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | case MBEDTLS_PK_ECKEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | return &mbedtls_eckey_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | case MBEDTLS_PK_ECKEY_DH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 125 | return &mbedtls_eckeydh_info; |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 126 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Valerio Setti | 7ca1318 | 2023-01-27 13:22:42 +0100 | [diff] [blame] | 127 | #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | case MBEDTLS_PK_ECDSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | return &mbedtls_ecdsa_info; |
Valerio Setti | 0d2980f | 2023-04-05 18:17:13 +0200 | [diff] [blame] | 130 | #endif /* MBEDTLS_PK_CAN_ECDSA_SOME */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | /* MBEDTLS_PK_RSA_ALT omitted on purpose */ |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 132 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | return NULL; |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 134 | } |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 138 | * Initialise context |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 139 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 140 | int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 141 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | if (info == NULL || ctx->pk_info != NULL) { |
| 143 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 144 | } |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 145 | |
Valerio Setti | b536126 | 2023-05-18 18:51:58 +0200 | [diff] [blame] | 146 | if ((info->ctx_alloc_func != NULL) && |
Valerio Setti | e00954d | 2023-04-28 15:24:32 +0200 | [diff] [blame] | 147 | ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 149 | } |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 150 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 151 | ctx->pk_info = info; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | return 0; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 154 | } |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 155 | |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 156 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 157 | /* |
| 158 | * Initialise a PSA-wrapping context |
| 159 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx, |
| 161 | const mbedtls_svc_key_id_t key) |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 162 | { |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 163 | const mbedtls_pk_info_t *info = NULL; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 164 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 165 | psa_key_type_t type; |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | if (ctx == NULL || ctx->pk_info != NULL) { |
| 168 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 169 | } |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) { |
| 172 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 173 | } |
| 174 | type = psa_get_key_type(&attributes); |
| 175 | psa_reset_key_attributes(&attributes); |
Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 176 | |
Valerio Setti | f7cd419 | 2023-06-30 18:11:29 +0200 | [diff] [blame] | 177 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 179 | info = &mbedtls_ecdsa_opaque_info; |
Valerio Setti | f7cd419 | 2023-06-30 18:11:29 +0200 | [diff] [blame] | 180 | } else |
| 181 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
| 182 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 183 | info = &mbedtls_rsa_opaque_info; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | } else { |
| 185 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 186 | } |
Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 187 | |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 188 | ctx->pk_info = info; |
Valerio Setti | 4f387ef | 2023-05-02 14:15:59 +0200 | [diff] [blame] | 189 | ctx->priv_id = key; |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | return 0; |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 192 | } |
| 193 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | b4fae57 | 2014-01-20 11:22:25 +0100 | [diff] [blame] | 196 | /* |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 197 | * Initialize an RSA-alt context |
| 198 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key, |
| 200 | mbedtls_pk_rsa_alt_decrypt_func decrypt_func, |
| 201 | mbedtls_pk_rsa_alt_sign_func sign_func, |
| 202 | mbedtls_pk_rsa_alt_key_len_func key_len_func) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 203 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | mbedtls_rsa_alt_context *rsa_alt; |
| 205 | const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | if (ctx->pk_info != NULL) { |
| 208 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 209 | } |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 211 | if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) { |
| 212 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 213 | } |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 214 | |
| 215 | ctx->pk_info = info; |
| 216 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 217 | rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 218 | |
| 219 | rsa_alt->key = key; |
| 220 | rsa_alt->decrypt_func = decrypt_func; |
| 221 | rsa_alt->sign_func = sign_func; |
| 222 | rsa_alt->key_len_func = key_len_func; |
| 223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 224 | return 0; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 225 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 227 | |
| 228 | /* |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 229 | * Tell if a PK can do the operations of the given type |
| 230 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 231 | 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] | 232 | { |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 233 | /* A context with null pk_info is not set up yet and can't do anything. |
| 234 | * For backward compatibility, also accept NULL instead of a context |
| 235 | * pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | if (ctx == NULL || ctx->pk_info == NULL) { |
| 237 | return 0; |
| 238 | } |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | return ctx->pk_info->can_do(type); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 243 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 244 | /* |
| 245 | * Tell if a PK can do the operations of the given PSA algorithm |
| 246 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg, |
| 248 | psa_key_usage_t usage) |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 249 | { |
Neil Armstrong | 408f6a6 | 2022-05-17 14:23:20 +0200 | [diff] [blame] | 250 | psa_key_usage_t key_usage; |
| 251 | |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 252 | /* A context with null pk_info is not set up yet and can't do anything. |
| 253 | * For backward compatibility, also accept NULL instead of a context |
| 254 | * pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | if (ctx == NULL || ctx->pk_info == NULL) { |
| 256 | return 0; |
| 257 | } |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 258 | |
| 259 | /* Filter out non allowed algorithms */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | if (PSA_ALG_IS_ECDSA(alg) == 0 && |
| 261 | PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 && |
| 262 | PSA_ALG_IS_RSA_PSS(alg) == 0 && |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 263 | alg != PSA_ALG_RSA_PKCS1V15_CRYPT && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | PSA_ALG_IS_ECDH(alg) == 0) { |
| 265 | return 0; |
| 266 | } |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 267 | |
Neil Armstrong | 408f6a6 | 2022-05-17 14:23:20 +0200 | [diff] [blame] | 268 | /* Filter out non allowed usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | if (usage == 0 || |
| 270 | (usage & ~(PSA_KEY_USAGE_SIGN_HASH | |
| 271 | PSA_KEY_USAGE_DECRYPT | |
| 272 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 273 | return 0; |
| 274 | } |
Neil Armstrong | 408f6a6 | 2022-05-17 14:23:20 +0200 | [diff] [blame] | 275 | |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 276 | /* Wildcard hash is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | if (PSA_ALG_IS_SIGN_HASH(alg) && |
| 278 | PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) { |
| 279 | return 0; |
| 280 | } |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) { |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 283 | mbedtls_pk_type_t type; |
| 284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 285 | if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) { |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 286 | type = MBEDTLS_PK_ECKEY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 288 | alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 289 | type = MBEDTLS_PK_RSA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | } else if (PSA_ALG_IS_RSA_PSS(alg)) { |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 291 | type = MBEDTLS_PK_RSASSA_PSS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | } else { |
| 293 | return 0; |
| 294 | } |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | if (ctx->pk_info->can_do(type) == 0) { |
| 297 | return 0; |
| 298 | } |
Neil Armstrong | 408f6a6 | 2022-05-17 14:23:20 +0200 | [diff] [blame] | 299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | switch (type) { |
Neil Armstrong | 084338d | 2022-05-19 16:22:40 +0200 | [diff] [blame] | 301 | case MBEDTLS_PK_ECKEY: |
| 302 | key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE; |
| 303 | break; |
| 304 | case MBEDTLS_PK_RSA: |
| 305 | case MBEDTLS_PK_RSASSA_PSS: |
| 306 | key_usage = PSA_KEY_USAGE_SIGN_HASH | |
| 307 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 308 | PSA_KEY_USAGE_DECRYPT; |
| 309 | break; |
| 310 | default: |
Neil Armstrong | b80785f | 2022-05-20 09:25:55 +0200 | [diff] [blame] | 311 | /* Should never happen */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | return 0; |
Neil Armstrong | 084338d | 2022-05-19 16:22:40 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | return (key_usage & usage) == usage; |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 316 | } |
| 317 | |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 318 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 319 | psa_status_t status; |
| 320 | |
Valerio Setti | 4f387ef | 2023-05-02 14:15:59 +0200 | [diff] [blame] | 321 | status = psa_get_key_attributes(ctx->priv_id, &attributes); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | if (status != PSA_SUCCESS) { |
| 323 | return 0; |
| 324 | } |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 325 | |
Valerio Setti | 2bd5366 | 2023-12-05 10:14:06 +0100 | [diff] [blame] | 326 | psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes); |
| 327 | /* Key's enrollment is available only when MBEDTLS_PSA_CRYPTO_CLIENT is |
| 328 | * defined, i.e. when the Mbed TLS implementation of PSA Crypto is being used. |
| 329 | * Even though we don't officially support using other implementations of PSA |
| 330 | * Crypto with TLS and X.509 (yet), we're still trying to simplify the life of |
| 331 | * people who would like to try it before it's officially supported. */ |
| 332 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) |
| 333 | psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes); |
| 334 | #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 335 | key_usage = psa_get_key_usage_flags(&attributes); |
| 336 | psa_reset_key_attributes(&attributes); |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 338 | if ((key_usage & usage) != usage) { |
| 339 | return 0; |
| 340 | } |
Neil Armstrong | 408f6a6 | 2022-05-17 14:23:20 +0200 | [diff] [blame] | 341 | |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 342 | /* |
Valerio Setti | 2bd5366 | 2023-12-05 10:14:06 +0100 | [diff] [blame] | 343 | * Common case: the key alg [or alg2] only allows alg. |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 344 | * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH |
| 345 | * directly. |
| 346 | * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with |
Valerio Setti | 2bd5366 | 2023-12-05 10:14:06 +0100 | [diff] [blame] | 347 | * a fixed hash on key_alg [or key_alg2]. |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 348 | */ |
Valerio Setti | 2bd5366 | 2023-12-05 10:14:06 +0100 | [diff] [blame] | 349 | if (alg == key_alg) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | return 1; |
| 351 | } |
Valerio Setti | 2bd5366 | 2023-12-05 10:14:06 +0100 | [diff] [blame] | 352 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) |
| 353 | if (alg == key_alg2) { |
| 354 | return 1; |
| 355 | } |
| 356 | #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */ |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 357 | |
| 358 | /* |
Valerio Setti | 2bd5366 | 2023-12-05 10:14:06 +0100 | [diff] [blame] | 359 | * If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash, |
Neil Armstrong | bbb8b75 | 2022-05-17 14:58:27 +0200 | [diff] [blame] | 360 | * and alg is the same hash-and-sign family with any hash, |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 361 | * then alg is compliant with this key alg |
| 362 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | if (PSA_ALG_IS_SIGN_HASH(key_alg) && |
| 366 | PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH && |
| 367 | (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) { |
| 368 | return 1; |
| 369 | } |
Valerio Setti | 2bd5366 | 2023-12-05 10:14:06 +0100 | [diff] [blame] | 370 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 371 | if (PSA_ALG_IS_SIGN_HASH(key_alg2) && |
| 372 | PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH && |
| 373 | (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) { |
| 374 | return 1; |
| 375 | } |
Valerio Setti | 2bd5366 | 2023-12-05 10:14:06 +0100 | [diff] [blame] | 376 | #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */ |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | return 0; |
Neil Armstrong | a88b158 | 2022-05-11 14:11:25 +0200 | [diff] [blame] | 380 | } |
| 381 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 382 | |
Gilles Peskine | 9cd2e9a | 2024-01-24 13:40:09 +0100 | [diff] [blame] | 383 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Gilles Peskine | 6ea1836 | 2024-01-18 14:16:27 +0100 | [diff] [blame] | 384 | #if defined(MBEDTLS_RSA_C) |
| 385 | static psa_algorithm_t psa_algorithm_for_rsa(const mbedtls_rsa_context *rsa, |
| 386 | int want_crypt) |
| 387 | { |
| 388 | if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) { |
| 389 | if (want_crypt) { |
Dave Rodgman | aa74165 | 2024-02-13 13:40:26 +0000 | [diff] [blame] | 390 | mbedtls_md_type_t md_type = (mbedtls_md_type_t) mbedtls_rsa_get_md_alg(rsa); |
Gilles Peskine | 6ea1836 | 2024-01-18 14:16:27 +0100 | [diff] [blame] | 391 | return PSA_ALG_RSA_OAEP(mbedtls_md_psa_alg_from_type(md_type)); |
| 392 | } else { |
| 393 | return PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_ANY_HASH); |
| 394 | } |
| 395 | } else { |
| 396 | if (want_crypt) { |
| 397 | return PSA_ALG_RSA_PKCS1V15_CRYPT; |
| 398 | } else { |
| 399 | return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | #endif /* MBEDTLS_RSA_C */ |
| 404 | |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 405 | int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk, |
| 406 | psa_key_usage_t usage, |
| 407 | psa_key_attributes_t *attributes) |
| 408 | { |
| 409 | mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk); |
| 410 | |
Gilles Peskine | ace7c77 | 2024-01-18 17:47:54 +0100 | [diff] [blame] | 411 | psa_key_usage_t more_usage = usage; |
| 412 | if (usage == PSA_KEY_USAGE_SIGN_MESSAGE) { |
| 413 | more_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE; |
| 414 | } else if (usage == PSA_KEY_USAGE_SIGN_HASH) { |
| 415 | more_usage |= PSA_KEY_USAGE_VERIFY_HASH; |
| 416 | } else if (usage == PSA_KEY_USAGE_DECRYPT) { |
| 417 | more_usage |= PSA_KEY_USAGE_ENCRYPT; |
| 418 | } |
| 419 | more_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY; |
| 420 | |
Gilles Peskine | c09df2f | 2024-01-23 17:03:31 +0100 | [diff] [blame] | 421 | int want_private = !(usage == PSA_KEY_USAGE_VERIFY_MESSAGE || |
| 422 | usage == PSA_KEY_USAGE_VERIFY_HASH || |
| 423 | usage == PSA_KEY_USAGE_ENCRYPT); |
| 424 | |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 425 | switch (pk_type) { |
Gilles Peskine | 6ea1836 | 2024-01-18 14:16:27 +0100 | [diff] [blame] | 426 | #if defined(MBEDTLS_RSA_C) |
| 427 | case MBEDTLS_PK_RSA: |
Gilles Peskine | ace7c77 | 2024-01-18 17:47:54 +0100 | [diff] [blame] | 428 | { |
Gilles Peskine | e820975 | 2024-02-01 21:00:33 +0100 | [diff] [blame] | 429 | int want_crypt = 0; /* 0: sign/verify; 1: encrypt/decrypt */ |
Gilles Peskine | 6ea1836 | 2024-01-18 14:16:27 +0100 | [diff] [blame] | 430 | switch (usage) { |
| 431 | case PSA_KEY_USAGE_SIGN_MESSAGE: |
Gilles Peskine | 6ea1836 | 2024-01-18 14:16:27 +0100 | [diff] [blame] | 432 | case PSA_KEY_USAGE_SIGN_HASH: |
Gilles Peskine | 6ea1836 | 2024-01-18 14:16:27 +0100 | [diff] [blame] | 433 | case PSA_KEY_USAGE_VERIFY_MESSAGE: |
| 434 | case PSA_KEY_USAGE_VERIFY_HASH: |
Gilles Peskine | c09df2f | 2024-01-23 17:03:31 +0100 | [diff] [blame] | 435 | /* Nothing to do. */ |
Gilles Peskine | 6ea1836 | 2024-01-18 14:16:27 +0100 | [diff] [blame] | 436 | break; |
Gilles Peskine | c09df2f | 2024-01-23 17:03:31 +0100 | [diff] [blame] | 437 | case PSA_KEY_USAGE_DECRYPT: |
Gilles Peskine | 6ea1836 | 2024-01-18 14:16:27 +0100 | [diff] [blame] | 438 | case PSA_KEY_USAGE_ENCRYPT: |
| 439 | want_crypt = 1; |
| 440 | break; |
| 441 | default: |
| 442 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 443 | } |
| 444 | /* Detect the presence of a private key in a way that works both |
| 445 | * in CRT and non-CRT configurations. */ |
| 446 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk); |
| 447 | int has_private = (mbedtls_rsa_check_privkey(rsa) == 0); |
| 448 | if (want_private && !has_private) { |
| 449 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 450 | } |
| 451 | psa_set_key_type(attributes, (want_private ? |
| 452 | PSA_KEY_TYPE_RSA_KEY_PAIR : |
| 453 | PSA_KEY_TYPE_RSA_PUBLIC_KEY)); |
Gilles Peskine | 55effd9 | 2024-01-23 18:07:36 +0100 | [diff] [blame] | 454 | psa_set_key_bits(attributes, mbedtls_pk_get_bitlen(pk)); |
Gilles Peskine | 6ea1836 | 2024-01-18 14:16:27 +0100 | [diff] [blame] | 455 | psa_set_key_algorithm(attributes, |
| 456 | psa_algorithm_for_rsa(rsa, want_crypt)); |
| 457 | break; |
Gilles Peskine | ace7c77 | 2024-01-18 17:47:54 +0100 | [diff] [blame] | 458 | } |
Gilles Peskine | 6ea1836 | 2024-01-18 14:16:27 +0100 | [diff] [blame] | 459 | #endif /* MBEDTLS_RSA_C */ |
| 460 | |
Gilles Peskine | ace7c77 | 2024-01-18 17:47:54 +0100 | [diff] [blame] | 461 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
| 462 | case MBEDTLS_PK_ECKEY: |
| 463 | case MBEDTLS_PK_ECKEY_DH: |
| 464 | case MBEDTLS_PK_ECDSA: |
| 465 | { |
| 466 | int sign_ok = (pk_type != MBEDTLS_PK_ECKEY_DH); |
| 467 | int derive_ok = (pk_type != MBEDTLS_PK_ECDSA); |
Gilles Peskine | f3dbc98 | 2024-01-23 11:05:34 +0100 | [diff] [blame] | 468 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
Gilles Peskine | cb3b4ca | 2024-02-02 13:12:39 +0100 | [diff] [blame] | 469 | psa_ecc_family_t family = pk->ec_family; |
| 470 | size_t bits = pk->ec_bits; |
| 471 | int has_private = 0; |
| 472 | if (pk->priv_id != MBEDTLS_SVC_KEY_ID_INIT) { |
| 473 | has_private = 1; |
Gilles Peskine | f3dbc98 | 2024-01-23 11:05:34 +0100 | [diff] [blame] | 474 | } |
Gilles Peskine | f3dbc98 | 2024-01-23 11:05:34 +0100 | [diff] [blame] | 475 | #else |
Gilles Peskine | ae2668b | 2024-02-01 20:48:04 +0100 | [diff] [blame] | 476 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk); |
Gilles Peskine | ace7c77 | 2024-01-18 17:47:54 +0100 | [diff] [blame] | 477 | int has_private = (ec->d.n != 0); |
| 478 | size_t bits = 0; |
| 479 | psa_ecc_family_t family = |
| 480 | mbedtls_ecc_group_to_psa(ec->grp.id, &bits); |
Gilles Peskine | f3dbc98 | 2024-01-23 11:05:34 +0100 | [diff] [blame] | 481 | #endif |
Gilles Peskine | ace7c77 | 2024-01-18 17:47:54 +0100 | [diff] [blame] | 482 | psa_algorithm_t alg = 0; |
| 483 | switch (usage) { |
| 484 | case PSA_KEY_USAGE_SIGN_MESSAGE: |
| 485 | case PSA_KEY_USAGE_SIGN_HASH: |
Gilles Peskine | ace7c77 | 2024-01-18 17:47:54 +0100 | [diff] [blame] | 486 | case PSA_KEY_USAGE_VERIFY_MESSAGE: |
| 487 | case PSA_KEY_USAGE_VERIFY_HASH: |
| 488 | if (!sign_ok) { |
| 489 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 490 | } |
| 491 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 492 | alg = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH); |
| 493 | #else |
| 494 | alg = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH); |
| 495 | #endif |
| 496 | break; |
| 497 | case PSA_KEY_USAGE_DERIVE: |
Gilles Peskine | ace7c77 | 2024-01-18 17:47:54 +0100 | [diff] [blame] | 498 | alg = PSA_ALG_ECDH; |
| 499 | if (!derive_ok) { |
| 500 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 501 | } |
| 502 | break; |
| 503 | default: |
| 504 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 505 | } |
| 506 | if (want_private && !has_private) { |
| 507 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 508 | } |
| 509 | psa_set_key_type(attributes, (want_private ? |
| 510 | PSA_KEY_TYPE_ECC_KEY_PAIR(family) : |
| 511 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(family))); |
| 512 | psa_set_key_bits(attributes, bits); |
| 513 | psa_set_key_algorithm(attributes, alg); |
| 514 | break; |
| 515 | } |
| 516 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
| 517 | |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 518 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
| 519 | case MBEDTLS_PK_RSA_ALT: |
| 520 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 521 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
| 522 | |
Gilles Peskine | 758d8c7 | 2024-01-22 20:53:21 +0100 | [diff] [blame] | 523 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 524 | case MBEDTLS_PK_OPAQUE: |
| 525 | { |
| 526 | psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 527 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 528 | status = psa_get_key_attributes(pk->priv_id, &old_attributes); |
| 529 | if (status != PSA_SUCCESS) { |
| 530 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 531 | } |
| 532 | psa_key_type_t old_type = psa_get_key_type(&old_attributes); |
| 533 | switch (usage) { |
| 534 | case PSA_KEY_USAGE_SIGN_MESSAGE: |
| 535 | case PSA_KEY_USAGE_SIGN_HASH: |
| 536 | case PSA_KEY_USAGE_VERIFY_MESSAGE: |
| 537 | case PSA_KEY_USAGE_VERIFY_HASH: |
| 538 | if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type) || |
| 539 | old_type == PSA_KEY_TYPE_RSA_KEY_PAIR)) { |
| 540 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 541 | } |
| 542 | break; |
| 543 | case PSA_KEY_USAGE_DECRYPT: |
| 544 | case PSA_KEY_USAGE_ENCRYPT: |
| 545 | if (old_type != PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 546 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 547 | } |
| 548 | break; |
| 549 | case PSA_KEY_USAGE_DERIVE: |
| 550 | if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type))) { |
| 551 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 552 | } |
| 553 | break; |
Gilles Peskine | 758d8c7 | 2024-01-22 20:53:21 +0100 | [diff] [blame] | 554 | default: |
| 555 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 556 | } |
| 557 | psa_key_type_t new_type = old_type; |
| 558 | /* Opaque keys are always key pairs, so we don't need a check |
| 559 | * on the input if the required usage is private. We just need |
| 560 | * to adjust the type correctly if the required usage is public. */ |
Gilles Peskine | c09df2f | 2024-01-23 17:03:31 +0100 | [diff] [blame] | 561 | if (!want_private) { |
Gilles Peskine | 758d8c7 | 2024-01-22 20:53:21 +0100 | [diff] [blame] | 562 | new_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(new_type); |
| 563 | } |
| 564 | more_usage = psa_get_key_usage_flags(&old_attributes); |
Gilles Peskine | 793920c | 2024-02-01 21:26:54 +0100 | [diff] [blame] | 565 | if ((usage & more_usage) == 0) { |
| 566 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 567 | } |
Gilles Peskine | 758d8c7 | 2024-01-22 20:53:21 +0100 | [diff] [blame] | 568 | psa_set_key_type(attributes, new_type); |
| 569 | psa_set_key_bits(attributes, psa_get_key_bits(&old_attributes)); |
| 570 | psa_set_key_algorithm(attributes, psa_get_key_algorithm(&old_attributes)); |
| 571 | break; |
| 572 | } |
| 573 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 574 | |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 575 | default: |
| 576 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 577 | } |
| 578 | |
Gilles Peskine | ace7c77 | 2024-01-18 17:47:54 +0100 | [diff] [blame] | 579 | psa_set_key_usage_flags(attributes, more_usage); |
Gilles Peskine | 1f97e73 | 2024-01-18 14:14:24 +0100 | [diff] [blame] | 580 | psa_set_key_enrollment_algorithm(attributes, PSA_ALG_NONE); |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 581 | |
| 582 | return 0; |
| 583 | } |
Gilles Peskine | fc3d866 | 2024-02-09 19:26:37 +0100 | [diff] [blame] | 584 | |
| 585 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) || defined(MBEDTLS_USE_PSA_CRYPTO) |
| 586 | static psa_status_t export_import_into_psa(mbedtls_svc_key_id_t old_key_id, |
| 587 | const psa_key_attributes_t *attributes, |
| 588 | mbedtls_svc_key_id_t *new_key_id) |
| 589 | { |
| 590 | unsigned char key_buffer[PSA_EXPORT_KEY_PAIR_MAX_SIZE]; |
| 591 | size_t key_length = 0; |
| 592 | psa_status_t status = psa_export_key(old_key_id, |
| 593 | key_buffer, sizeof(key_buffer), |
| 594 | &key_length); |
| 595 | if (status != PSA_SUCCESS) { |
| 596 | return status; |
| 597 | } |
| 598 | status = psa_import_key(attributes, key_buffer, key_length, new_key_id); |
| 599 | mbedtls_platform_zeroize(key_buffer, key_length); |
| 600 | return status; |
| 601 | } |
| 602 | |
| 603 | static int copy_into_psa(mbedtls_svc_key_id_t old_key_id, |
| 604 | const psa_key_attributes_t *attributes, |
| 605 | mbedtls_svc_key_id_t *new_key_id) |
| 606 | { |
| 607 | /* Normally, we prefer copying: it's more efficient and works even |
| 608 | * for non-exportable keys. */ |
| 609 | psa_status_t status = psa_copy_key(old_key_id, attributes, new_key_id); |
| 610 | if (status == PSA_ERROR_NOT_PERMITTED /*missing COPY usage*/ || |
| 611 | status == PSA_ERROR_INVALID_ARGUMENT /*incompatible policy*/) { |
| 612 | /* There are edge cases where copying won't work, but export+import |
| 613 | * might: |
| 614 | * - If the old key does not allow PSA_KEY_USAGE_COPY. |
| 615 | * - If the old key's usage does not allow what attributes wants. |
| 616 | * Because the key was intended for use in the pk module, and may |
| 617 | * have had a policy chosen solely for what pk needs rather than |
| 618 | * based on a detailed understanding of PSA policies, we are a bit |
| 619 | * more liberal than psa_copy_key() here. |
| 620 | */ |
| 621 | /* Here we need to check that the types match, otherwise we risk |
| 622 | * importing nonsensical data. */ |
| 623 | psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 624 | status = psa_get_key_attributes(old_key_id, &old_attributes); |
| 625 | if (status != PSA_SUCCESS) { |
| 626 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 627 | } |
| 628 | psa_key_type_t old_type = psa_get_key_type(&old_attributes); |
| 629 | psa_reset_key_attributes(&old_attributes); |
| 630 | if (old_type != psa_get_key_type(attributes)) { |
| 631 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 632 | } |
| 633 | status = export_import_into_psa(old_key_id, attributes, new_key_id); |
| 634 | } |
| 635 | return PSA_PK_TO_MBEDTLS_ERR(status); |
| 636 | } |
| 637 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_USE_PSA_CRYPTO */ |
| 638 | |
| 639 | static int import_pair_into_psa(const mbedtls_pk_context *pk, |
| 640 | const psa_key_attributes_t *attributes, |
| 641 | mbedtls_svc_key_id_t *key_id) |
| 642 | { |
| 643 | switch (mbedtls_pk_get_type(pk)) { |
| 644 | #if defined(MBEDTLS_RSA_C) |
| 645 | case MBEDTLS_PK_RSA: |
| 646 | { |
| 647 | if (psa_get_key_type(attributes) != PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 648 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 649 | } |
| 650 | unsigned char key_buffer[ |
| 651 | PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)]; |
| 652 | unsigned char *const key_end = key_buffer + sizeof(key_buffer); |
| 653 | unsigned char *key_data = key_end; |
| 654 | int ret = mbedtls_rsa_write_key(mbedtls_pk_rsa(*pk), |
| 655 | key_buffer, &key_data); |
| 656 | if (ret < 0) { |
| 657 | return ret; |
| 658 | } |
| 659 | size_t key_length = key_end - key_data; |
| 660 | ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes, |
| 661 | key_data, key_length, |
| 662 | key_id)); |
| 663 | mbedtls_platform_zeroize(key_data, key_length); |
| 664 | return ret; |
| 665 | } |
| 666 | #endif /* MBEDTLS_RSA_C */ |
| 667 | |
| 668 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
| 669 | case MBEDTLS_PK_ECKEY: |
| 670 | case MBEDTLS_PK_ECKEY_DH: |
| 671 | case MBEDTLS_PK_ECDSA: |
| 672 | { |
| 673 | /* We need to check the curve family, otherwise the import could |
| 674 | * succeed with nonsensical data. |
| 675 | * We don't check the bit-size: it's optional in attributes, |
| 676 | * and if it's specified, psa_import_key() will know from the key |
| 677 | * data length and will check that the bit-size matches. */ |
| 678 | psa_key_type_t to_type = psa_get_key_type(attributes); |
| 679 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 680 | psa_ecc_family_t from_family = pk->ec_family; |
| 681 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Gilles Peskine | 84a7bfb | 2024-02-28 14:21:32 +0100 | [diff] [blame] | 682 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk); |
Gilles Peskine | fc3d866 | 2024-02-09 19:26:37 +0100 | [diff] [blame] | 683 | size_t from_bits = 0; |
| 684 | psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id, |
| 685 | &from_bits); |
| 686 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 687 | if (to_type != PSA_KEY_TYPE_ECC_KEY_PAIR(from_family)) { |
| 688 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 689 | } |
| 690 | |
| 691 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 692 | if (mbedtls_svc_key_id_is_null(pk->priv_id)) { |
| 693 | /* We have a public key and want a key pair. */ |
| 694 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 695 | } |
| 696 | return copy_into_psa(pk->priv_id, attributes, key_id); |
| 697 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 698 | if (ec->d.n == 0) { |
| 699 | /* Private key not set. Assume the input is a public key only. |
| 700 | * (The other possibility is that it's an incomplete object |
| 701 | * where the group is set but neither the public key nor |
| 702 | * the private key. This is not possible through ecp.h |
| 703 | * functions, so we don't bother reporting a more suitable |
| 704 | * error in that case.) */ |
| 705 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 706 | } |
| 707 | unsigned char key_buffer[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)]; |
Gilles Peskine | 84a7bfb | 2024-02-28 14:21:32 +0100 | [diff] [blame] | 708 | size_t key_length = 0; |
| 709 | int ret = mbedtls_ecp_write_key_ext(ec, &key_length, |
| 710 | key_buffer, sizeof(key_buffer)); |
Gilles Peskine | fc3d866 | 2024-02-09 19:26:37 +0100 | [diff] [blame] | 711 | if (ret < 0) { |
| 712 | return ret; |
| 713 | } |
Gilles Peskine | fc3d866 | 2024-02-09 19:26:37 +0100 | [diff] [blame] | 714 | ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes, |
Gilles Peskine | 83b8baf | 2024-02-15 17:26:07 +0100 | [diff] [blame] | 715 | key_buffer, key_length, |
Gilles Peskine | fc3d866 | 2024-02-09 19:26:37 +0100 | [diff] [blame] | 716 | key_id)); |
Gilles Peskine | 83b8baf | 2024-02-15 17:26:07 +0100 | [diff] [blame] | 717 | mbedtls_platform_zeroize(key_buffer, key_length); |
Gilles Peskine | fc3d866 | 2024-02-09 19:26:37 +0100 | [diff] [blame] | 718 | return ret; |
| 719 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 720 | } |
| 721 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
| 722 | |
| 723 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 724 | case MBEDTLS_PK_OPAQUE: |
| 725 | return copy_into_psa(pk->priv_id, attributes, key_id); |
| 726 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 727 | |
| 728 | default: |
| 729 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | static int import_public_into_psa(const mbedtls_pk_context *pk, |
| 734 | const psa_key_attributes_t *attributes, |
| 735 | mbedtls_svc_key_id_t *key_id) |
| 736 | { |
| 737 | psa_key_type_t psa_type = psa_get_key_type(attributes); |
| 738 | |
| 739 | #if defined(MBEDTLS_RSA_C) || \ |
| 740 | (defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA)) || \ |
| 741 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 742 | unsigned char key_buffer[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE]; |
| 743 | #endif |
| 744 | unsigned char *key_data = NULL; |
| 745 | size_t key_length = 0; |
| 746 | |
| 747 | switch (mbedtls_pk_get_type(pk)) { |
| 748 | #if defined(MBEDTLS_RSA_C) |
| 749 | case MBEDTLS_PK_RSA: |
| 750 | { |
| 751 | if (psa_type != PSA_KEY_TYPE_RSA_PUBLIC_KEY) { |
| 752 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 753 | } |
| 754 | unsigned char *const key_end = key_buffer + sizeof(key_buffer); |
| 755 | key_data = key_end; |
| 756 | int ret = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*pk), |
| 757 | key_buffer, &key_data); |
| 758 | if (ret < 0) { |
| 759 | return ret; |
| 760 | } |
| 761 | key_length = (size_t) ret; |
| 762 | break; |
| 763 | } |
| 764 | #endif /*MBEDTLS_RSA_C */ |
| 765 | |
| 766 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
| 767 | case MBEDTLS_PK_ECKEY: |
| 768 | case MBEDTLS_PK_ECKEY_DH: |
| 769 | case MBEDTLS_PK_ECDSA: |
| 770 | { |
| 771 | /* We need to check the curve family, otherwise the import could |
| 772 | * succeed with nonsensical data. |
| 773 | * We don't check the bit-size: it's optional in attributes, |
| 774 | * and if it's specified, psa_import_key() will know from the key |
| 775 | * data length and will check that the bit-size matches. */ |
| 776 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 777 | if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family)) { |
| 778 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 779 | } |
| 780 | key_data = (unsigned char *) pk->pub_raw; |
| 781 | key_length = pk->pub_raw_len; |
| 782 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 783 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk); |
| 784 | size_t from_bits = 0; |
| 785 | psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id, |
| 786 | &from_bits); |
Gilles Peskine | 1d33876 | 2024-02-12 14:18:26 +0100 | [diff] [blame] | 787 | if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(from_family)) { |
Gilles Peskine | fc3d866 | 2024-02-09 19:26:37 +0100 | [diff] [blame] | 788 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 789 | } |
| 790 | int ret = mbedtls_ecp_write_public_key( |
| 791 | ec, MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 792 | &key_length, key_buffer, sizeof(key_buffer)); |
| 793 | if (ret < 0) { |
| 794 | return ret; |
| 795 | } |
| 796 | key_data = key_buffer; |
| 797 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 798 | break; |
| 799 | } |
| 800 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
| 801 | |
| 802 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 803 | case MBEDTLS_PK_OPAQUE: |
| 804 | { |
| 805 | psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 806 | psa_status_t status = |
| 807 | psa_get_key_attributes(pk->priv_id, &old_attributes); |
| 808 | if (status != PSA_SUCCESS) { |
| 809 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 810 | } |
| 811 | psa_key_type_t old_type = psa_get_key_type(&old_attributes); |
| 812 | psa_reset_key_attributes(&old_attributes); |
| 813 | if (psa_type != PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(old_type)) { |
| 814 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 815 | } |
| 816 | status = psa_export_public_key(pk->priv_id, |
| 817 | key_buffer, sizeof(key_buffer), |
| 818 | &key_length); |
| 819 | if (status != PSA_SUCCESS) { |
| 820 | return PSA_PK_TO_MBEDTLS_ERR(status); |
| 821 | } |
| 822 | key_data = key_buffer; |
| 823 | break; |
| 824 | } |
| 825 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 826 | |
| 827 | default: |
| 828 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 829 | } |
| 830 | |
| 831 | return PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes, |
| 832 | key_data, key_length, |
| 833 | key_id)); |
| 834 | } |
| 835 | |
| 836 | int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk, |
| 837 | const psa_key_attributes_t *attributes, |
| 838 | mbedtls_svc_key_id_t *key_id) |
| 839 | { |
| 840 | /* Set the output immediately so that it won't contain garbage even |
| 841 | * if we error out before calling psa_import_key(). */ |
| 842 | *key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 843 | |
| 844 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
| 845 | if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA_ALT) { |
| 846 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 847 | } |
| 848 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
| 849 | |
| 850 | int want_public = PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(attributes)); |
| 851 | if (want_public) { |
| 852 | return import_public_into_psa(pk, attributes, key_id); |
| 853 | } else { |
| 854 | return import_pair_into_psa(pk, attributes, key_id); |
| 855 | } |
| 856 | } |
Gilles Peskine | 9cd2e9a | 2024-01-24 13:40:09 +0100 | [diff] [blame] | 857 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 858 | |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 859 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 860 | * Helper for mbedtls_pk_sign and mbedtls_pk_verify |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 861 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 862 | 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] | 863 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 864 | if (*hash_len != 0) { |
| 865 | return 0; |
| 866 | } |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 867 | |
Manuel Pégourié-Gonnard | 9b41eb8 | 2023-03-28 11:14:24 +0200 | [diff] [blame] | 868 | *hash_len = mbedtls_md_get_size_from_type(md_alg); |
Manuel Pégourié-Gonnard | a370e06 | 2022-07-05 11:55:20 +0200 | [diff] [blame] | 869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 870 | if (*hash_len == 0) { |
| 871 | return -1; |
| 872 | } |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 874 | return 0; |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 875 | } |
| 876 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 877 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 878 | /* |
| 879 | * Helper to set up a restart context if needed |
| 880 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 881 | static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx, |
| 882 | const mbedtls_pk_info_t *info) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 883 | { |
Manuel Pégourié-Gonnard | c8c12b6 | 2018-07-02 13:09:39 +0200 | [diff] [blame] | 884 | /* Don't do anything if already set up or invalid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | if (ctx == NULL || ctx->pk_info != NULL) { |
| 886 | return 0; |
| 887 | } |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 888 | |
| 889 | /* Should never happen when we're called */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 890 | if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) { |
| 891 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 892 | } |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 894 | if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) { |
| 895 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 896 | } |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 897 | |
| 898 | ctx->pk_info = info; |
| 899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 900 | return 0; |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 901 | } |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 902 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 903 | |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 904 | /* |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 905 | * Verify a signature (restartable) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 906 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 907 | int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx, |
| 908 | mbedtls_md_type_t md_alg, |
| 909 | const unsigned char *hash, size_t hash_len, |
| 910 | const unsigned char *sig, size_t sig_len, |
| 911 | mbedtls_pk_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 912 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 913 | if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) { |
Tuvshinzaya Erdenekhuu | 78c1d8c | 2022-07-29 14:51:50 +0100 | [diff] [blame] | 914 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 915 | } |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 917 | if (ctx->pk_info == NULL || |
| 918 | pk_hashlen_helper(md_alg, &hash_len) != 0) { |
| 919 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 920 | } |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 921 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 922 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | d55f776 | 2017-08-18 17:40:15 +0200 | [diff] [blame] | 923 | /* optimization: use non-restartable version if restart disabled */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 924 | if (rs_ctx != NULL && |
Manuel Pégourié-Gonnard | b843b15 | 2018-10-16 10:41:31 +0200 | [diff] [blame] | 925 | mbedtls_ecp_restart_is_enabled() && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 926 | ctx->pk_info->verify_rs_func != NULL) { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 927 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 929 | if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) { |
| 930 | return ret; |
| 931 | } |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 932 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 933 | ret = ctx->pk_info->verify_rs_func(ctx, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 934 | md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx); |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 936 | if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 937 | mbedtls_pk_restart_free(rs_ctx); |
| 938 | } |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 940 | return ret; |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 941 | } |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 942 | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 943 | (void) rs_ctx; |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 944 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 946 | if (ctx->pk_info->verify_func == NULL) { |
| 947 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 948 | } |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 949 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 950 | return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 951 | sig, sig_len); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 955 | * Verify a signature |
| 956 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 957 | int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
| 958 | const unsigned char *hash, size_t hash_len, |
| 959 | const unsigned char *sig, size_t sig_len) |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 960 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len, |
| 962 | sig, sig_len, NULL); |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | /* |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 966 | * Verify a signature with options |
| 967 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 968 | int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options, |
| 969 | mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
| 970 | const unsigned char *hash, size_t hash_len, |
| 971 | const unsigned char *sig, size_t sig_len) |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 972 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 973 | if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) { |
Tuvshinzaya Erdenekhuu | 78c1d8c | 2022-07-29 14:51:50 +0100 | [diff] [blame] | 974 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 975 | } |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 977 | if (ctx->pk_info == NULL) { |
| 978 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 979 | } |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 981 | if (!mbedtls_pk_can_do(ctx, type)) { |
| 982 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 983 | } |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | if (type != MBEDTLS_PK_RSASSA_PSS) { |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 986 | /* General case: no options */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 987 | if (options != NULL) { |
| 988 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 989 | } |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 991 | return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len); |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 992 | } |
| 993 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 994 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 995 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 996 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 997 | |
Dave Rodgman | 02a53d7 | 2023-09-28 17:17:07 +0100 | [diff] [blame] | 998 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 999 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
| 1000 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1001 | } |
Dave Rodgman | 02a53d7 | 2023-09-28 17:17:07 +0100 | [diff] [blame] | 1002 | #endif |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 1003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1004 | if (options == NULL) { |
| 1005 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1006 | } |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1007 | |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1008 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) options; |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1009 | |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1010 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1011 | if (pss_opts->mgf1_hash_id == md_alg) { |
Manuel Pégourié-Gonnard | 4dacf58 | 2022-06-10 12:50:00 +0200 | [diff] [blame] | 1012 | unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES]; |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1013 | unsigned char *p; |
Andrzej Kurek | 5955053 | 2022-02-16 07:46:42 -0500 | [diff] [blame] | 1014 | int key_len; |
| 1015 | size_t signature_length; |
Andrzej Kurek | d70fa0e | 2022-02-17 10:51:15 -0500 | [diff] [blame] | 1016 | psa_status_t status = PSA_ERROR_DATA_CORRUPT; |
| 1017 | psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT; |
Andrzej Kurek | 5955053 | 2022-02-16 07:46:42 -0500 | [diff] [blame] | 1018 | |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 1019 | psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg); |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1020 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 1021 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1022 | psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg); |
| 1023 | p = buf + sizeof(buf); |
Valerio Setti | 3a815cb | 2024-02-14 09:54:18 +0100 | [diff] [blame] | 1024 | key_len = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*ctx), buf, &p); |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1026 | if (key_len < 0) { |
| 1027 | return key_len; |
| 1028 | } |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1030 | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY); |
| 1031 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 1032 | psa_set_key_algorithm(&attributes, psa_sig_alg); |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1034 | status = psa_import_key(&attributes, |
| 1035 | buf + sizeof(buf) - key_len, key_len, |
| 1036 | &key_id); |
| 1037 | if (status != PSA_SUCCESS) { |
| 1038 | psa_destroy_key(key_id); |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1039 | return PSA_PK_TO_MBEDTLS_ERR(status); |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1040 | } |
| 1041 | |
Andrzej Kurek | 4a953cd | 2022-02-16 06:13:35 -0500 | [diff] [blame] | 1042 | /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH |
| 1043 | * on a valid signature with trailing data in a buffer, but |
| 1044 | * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact, |
| 1045 | * so for this reason the passed sig_len is overwritten. Smaller |
| 1046 | * signature lengths should not be accepted for verification. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1047 | signature_length = sig_len > mbedtls_pk_get_len(ctx) ? |
| 1048 | mbedtls_pk_get_len(ctx) : sig_len; |
| 1049 | status = psa_verify_hash(key_id, psa_sig_alg, hash, |
| 1050 | hash_len, sig, signature_length); |
| 1051 | destruction_status = psa_destroy_key(key_id); |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1053 | if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) { |
| 1054 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 1055 | } |
Andrzej Kurek | 8666df6 | 2022-02-15 08:23:02 -0500 | [diff] [blame] | 1056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1057 | if (status == PSA_SUCCESS) { |
Andrzej Kurek | d70fa0e | 2022-02-17 10:51:15 -0500 | [diff] [blame] | 1058 | status = destruction_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1059 | } |
Andrzej Kurek | d70fa0e | 2022-02-17 10:51:15 -0500 | [diff] [blame] | 1060 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1061 | return PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1062 | } else |
Tomi Fontanilles | 8174662 | 2023-07-16 13:06:06 +0300 | [diff] [blame] | 1063 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1064 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1065 | if (sig_len < mbedtls_pk_get_len(ctx)) { |
| 1066 | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
| 1067 | } |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1069 | ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx), |
| 1070 | md_alg, (unsigned int) hash_len, hash, |
| 1071 | pss_opts->mgf1_hash_id, |
| 1072 | pss_opts->expected_salt_len, |
| 1073 | sig); |
| 1074 | if (ret != 0) { |
| 1075 | return ret; |
| 1076 | } |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1078 | if (sig_len > mbedtls_pk_get_len(ctx)) { |
| 1079 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 1080 | } |
Andrzej Kurek | 90ba2cb | 2022-02-15 08:18:44 -0500 | [diff] [blame] | 1081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1082 | return 0; |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1083 | } |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1084 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1085 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Andrzej Kurek | 7db1b78 | 2022-02-09 14:13:44 -0500 | [diff] [blame] | 1086 | #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */ |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /* |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1090 | * Make a signature (restartable) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1091 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1092 | int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx, |
| 1093 | mbedtls_md_type_t md_alg, |
| 1094 | const unsigned char *hash, size_t hash_len, |
| 1095 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1096 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 1097 | mbedtls_pk_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1098 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1099 | if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) { |
Tuvshinzaya Erdenekhuu | 78c1d8c | 2022-07-29 14:51:50 +0100 | [diff] [blame] | 1100 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1101 | } |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1103 | if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) { |
| 1104 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1105 | } |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1106 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1107 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | d55f776 | 2017-08-18 17:40:15 +0200 | [diff] [blame] | 1108 | /* optimization: use non-restartable version if restart disabled */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1109 | if (rs_ctx != NULL && |
Manuel Pégourié-Gonnard | b843b15 | 2018-10-16 10:41:31 +0200 | [diff] [blame] | 1110 | mbedtls_ecp_restart_is_enabled() && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1111 | ctx->pk_info->sign_rs_func != NULL) { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1112 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1114 | if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) { |
| 1115 | return ret; |
| 1116 | } |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1117 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1118 | ret = ctx->pk_info->sign_rs_func(ctx, md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1119 | hash, hash_len, |
| 1120 | sig, sig_size, sig_len, |
| 1121 | f_rng, p_rng, rs_ctx->rs_ctx); |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 1124 | mbedtls_pk_restart_free(rs_ctx); |
| 1125 | } |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1127 | return ret; |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1128 | } |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1129 | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1130 | (void) rs_ctx; |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1131 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1133 | if (ctx->pk_info->sign_func == NULL) { |
| 1134 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 1135 | } |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1136 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1137 | return ctx->pk_info->sign_func(ctx, md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1138 | hash, hash_len, |
| 1139 | sig, sig_size, sig_len, |
| 1140 | f_rng, p_rng); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /* |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1144 | * Make a signature |
| 1145 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1146 | int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
| 1147 | const unsigned char *hash, size_t hash_len, |
| 1148 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1149 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1150 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1151 | return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len, |
| 1152 | sig, sig_size, sig_len, |
| 1153 | f_rng, p_rng, NULL); |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /* |
Jerry Yu | fb0621d | 2022-03-23 11:42:06 +0800 | [diff] [blame] | 1157 | * Make a signature given a signature type. |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 1158 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1159 | int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type, |
| 1160 | mbedtls_pk_context *ctx, |
| 1161 | mbedtls_md_type_t md_alg, |
| 1162 | const unsigned char *hash, size_t hash_len, |
| 1163 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1164 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1165 | void *p_rng) |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 1166 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1167 | if (ctx->pk_info == NULL) { |
| 1168 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1169 | } |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 1170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1171 | if (!mbedtls_pk_can_do(ctx, pk_type)) { |
| 1172 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 1173 | } |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 1174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1175 | if (pk_type != MBEDTLS_PK_RSASSA_PSS) { |
| 1176 | return mbedtls_pk_sign(ctx, md_alg, hash, hash_len, |
| 1177 | sig, sig_size, sig_len, f_rng, p_rng); |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 1178 | } |
Neil Armstrong | 13e76be | 2022-04-21 12:08:52 +0200 | [diff] [blame] | 1179 | |
Tomi Fontanilles | 8174662 | 2023-07-16 13:06:06 +0300 | [diff] [blame] | 1180 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 1181 | |
| 1182 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1183 | const psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1184 | if (psa_md_alg == 0) { |
| 1185 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1186 | } |
Neil Armstrong | 13e76be | 2022-04-21 12:08:52 +0200 | [diff] [blame] | 1187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1188 | if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) { |
Valerio Setti | b484e37 | 2024-02-27 16:21:12 +0100 | [diff] [blame] | 1189 | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 1190 | psa_algorithm_t psa_alg, psa_enrollment_alg, sign_alg; |
Neil Armstrong | 13e76be | 2022-04-21 12:08:52 +0200 | [diff] [blame] | 1191 | psa_status_t status; |
| 1192 | |
Valerio Setti | b484e37 | 2024-02-27 16:21:12 +0100 | [diff] [blame] | 1193 | status = psa_get_key_attributes(ctx->priv_id, &key_attr); |
| 1194 | if (status != PSA_SUCCESS) { |
| 1195 | return PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
| 1196 | } |
| 1197 | psa_alg = psa_get_key_algorithm(&key_attr); |
| 1198 | psa_enrollment_alg = psa_get_key_enrollment_algorithm(&key_attr); |
| 1199 | psa_reset_key_attributes(&key_attr); |
| 1200 | |
| 1201 | /* Since we're PK type is MBEDTLS_PK_RSASSA_PSS at least one between |
| 1202 | * alg and enrollment alg should be of type RSA_PSS. */ |
| 1203 | if (PSA_ALG_IS_RSA_PSS(psa_alg)) { |
| 1204 | sign_alg = psa_alg; |
| 1205 | } else if (PSA_ALG_IS_RSA_PSS(psa_enrollment_alg)) { |
| 1206 | sign_alg = psa_enrollment_alg; |
| 1207 | } else { |
| 1208 | /* The opaque key has no RSA PSS algorithm associated. */ |
| 1209 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1210 | } |
| 1211 | /* Adjust the hashing algorithm. */ |
| 1212 | sign_alg = (sign_alg & ~PSA_ALG_HASH_MASK) | PSA_ALG_GET_HASH(psa_md_alg); |
| 1213 | |
| 1214 | status = psa_sign_hash(ctx->priv_id, sign_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1215 | hash, hash_len, |
| 1216 | sig, sig_size, sig_len); |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1217 | return PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 13e76be | 2022-04-21 12:08:52 +0200 | [diff] [blame] | 1218 | } |
| 1219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1220 | return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg), |
| 1221 | ctx->pk_ctx, hash, hash_len, |
| 1222 | sig, sig_size, sig_len); |
Tomi Fontanilles | 8174662 | 2023-07-16 13:06:06 +0300 | [diff] [blame] | 1223 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 1224 | |
Tomi Fontanilles | 8174662 | 2023-07-16 13:06:06 +0300 | [diff] [blame] | 1225 | if (sig_size < mbedtls_pk_get_len(ctx)) { |
| 1226 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
| 1227 | } |
| 1228 | |
| 1229 | if (pk_hashlen_helper(md_alg, &hash_len) != 0) { |
| 1230 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1231 | } |
| 1232 | |
| 1233 | mbedtls_rsa_context *const rsa_ctx = mbedtls_pk_rsa(*ctx); |
| 1234 | |
Tomi Fontanilles | 573dc23 | 2023-12-10 14:57:51 +0200 | [diff] [blame] | 1235 | const int ret = mbedtls_rsa_rsassa_pss_sign_no_mode_check(rsa_ctx, f_rng, p_rng, md_alg, |
| 1236 | (unsigned int) hash_len, hash, sig); |
Tomi Fontanilles | 8174662 | 2023-07-16 13:06:06 +0300 | [diff] [blame] | 1237 | if (ret == 0) { |
| 1238 | *sig_len = rsa_ctx->len; |
| 1239 | } |
| 1240 | return ret; |
| 1241 | |
| 1242 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1243 | |
| 1244 | #else |
| 1245 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 1246 | #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */ |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | /* |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1250 | * Decrypt message |
| 1251 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1252 | int mbedtls_pk_decrypt(mbedtls_pk_context *ctx, |
| 1253 | const unsigned char *input, size_t ilen, |
| 1254 | unsigned char *output, size_t *olen, size_t osize, |
| 1255 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1256 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1257 | if (ctx->pk_info == NULL) { |
| 1258 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1259 | } |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1261 | if (ctx->pk_info->decrypt_func == NULL) { |
| 1262 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 1263 | } |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1264 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1265 | return ctx->pk_info->decrypt_func(ctx, input, ilen, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1266 | output, olen, osize, f_rng, p_rng); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | /* |
| 1270 | * Encrypt message |
| 1271 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1272 | int mbedtls_pk_encrypt(mbedtls_pk_context *ctx, |
| 1273 | const unsigned char *input, size_t ilen, |
| 1274 | unsigned char *output, size_t *olen, size_t osize, |
| 1275 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1276 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1277 | if (ctx->pk_info == NULL) { |
| 1278 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1279 | } |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1281 | if (ctx->pk_info->encrypt_func == NULL) { |
| 1282 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 1283 | } |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1284 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1285 | return ctx->pk_info->encrypt_func(ctx, input, ilen, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1286 | output, olen, osize, f_rng, p_rng); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | /* |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1290 | * Check public-private key pair |
| 1291 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1292 | int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, |
| 1293 | const mbedtls_pk_context *prv, |
| 1294 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1295 | void *p_rng) |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1296 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1297 | if (pub->pk_info == NULL || |
| 1298 | prv->pk_info == NULL) { |
| 1299 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1300 | } |
| 1301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | if (f_rng == NULL) { |
| 1303 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1304 | } |
| 1305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1306 | if (prv->pk_info->check_pair_func == NULL) { |
| 1307 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 1308 | } |
| 1309 | |
| 1310 | if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) { |
| 1311 | if (pub->pk_info->type != MBEDTLS_PK_RSA) { |
| 1312 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 1313 | } |
| 1314 | } else { |
valerio | 8cbef4d | 2023-06-01 10:59:03 +0200 | [diff] [blame] | 1315 | if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) && |
| 1316 | (pub->pk_info != prv->pk_info)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1317 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 1318 | } |
| 1319 | } |
| 1320 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1321 | return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub, |
| 1322 | (mbedtls_pk_context *) prv, |
| 1323 | f_rng, p_rng); |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | /* |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1327 | * Get key size in bits |
| 1328 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1329 | 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] | 1330 | { |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1331 | /* For backward compatibility, accept NULL or a context that |
| 1332 | * isn't set up yet, and return a fake value that should be safe. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1333 | if (ctx == NULL || ctx->pk_info == NULL) { |
| 1334 | return 0; |
| 1335 | } |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1336 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1337 | return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 1338 | } |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 1339 | |
| 1340 | /* |
| 1341 | * Export debug information |
| 1342 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1343 | 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] | 1344 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1345 | if (ctx->pk_info == NULL) { |
| 1346 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1347 | } |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 1348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1349 | if (ctx->pk_info->debug_func == NULL) { |
| 1350 | return MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 1351 | } |
Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1352 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1353 | ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | return 0; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 1355 | } |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 1356 | |
| 1357 | /* |
| 1358 | * Access the PK type name |
| 1359 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1360 | 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] | 1361 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1362 | if (ctx == NULL || ctx->pk_info == NULL) { |
| 1363 | return "invalid PK"; |
| 1364 | } |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 1365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1366 | return ctx->pk_info->name; |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 1367 | } |
Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 1368 | |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1369 | /* |
| 1370 | * Access the PK type |
| 1371 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1372 | 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] | 1373 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1374 | if (ctx == NULL || ctx->pk_info == NULL) { |
| 1375 | return MBEDTLS_PK_NONE; |
| 1376 | } |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1378 | return ctx->pk_info->type; |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1379 | } |
| 1380 | |
Valerio Setti | 01ba66d | 2024-02-09 11:35:42 +0100 | [diff] [blame] | 1381 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Valerio Setti | 070d95e | 2024-02-01 11:29:15 +0100 | [diff] [blame] | 1382 | int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk) |
| 1383 | { |
| 1384 | psa_status_t status; |
| 1385 | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 1386 | psa_key_type_t key_type; |
| 1387 | psa_algorithm_t alg_type; |
| 1388 | size_t key_bits; |
| 1389 | /* Use a buffer size large enough to contain either a key pair or public key. */ |
| 1390 | unsigned char exp_key[PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE]; |
| 1391 | size_t exp_key_len; |
| 1392 | int ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1393 | |
| 1394 | if (pk == NULL) { |
| 1395 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1396 | } |
| 1397 | |
| 1398 | status = psa_get_key_attributes(key_id, &key_attr); |
| 1399 | if (status != PSA_SUCCESS) { |
| 1400 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1401 | } |
| 1402 | |
Valerio Setti | 070d95e | 2024-02-01 11:29:15 +0100 | [diff] [blame] | 1403 | status = psa_export_key(key_id, exp_key, sizeof(exp_key), &exp_key_len); |
| 1404 | if (status != PSA_SUCCESS) { |
Valerio Setti | 5ac511b | 2024-02-14 10:17:08 +0100 | [diff] [blame^] | 1405 | ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
Valerio Setti | 070d95e | 2024-02-01 11:29:15 +0100 | [diff] [blame] | 1406 | goto exit; |
| 1407 | } |
| 1408 | |
| 1409 | key_type = psa_get_key_type(&key_attr); |
| 1410 | key_bits = psa_get_key_bits(&key_attr); |
| 1411 | alg_type = psa_get_key_algorithm(&key_attr); |
| 1412 | |
| 1413 | #if defined(MBEDTLS_RSA_C) |
| 1414 | if ((key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) || |
| 1415 | (key_type == PSA_KEY_TYPE_RSA_PUBLIC_KEY)) { |
| 1416 | if (!PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg_type) && |
| 1417 | (alg_type != PSA_ALG_RSA_PKCS1V15_CRYPT) && |
| 1418 | !PSA_ALG_IS_RSA_OAEP(alg_type) && |
| 1419 | !PSA_ALG_IS_RSA_PSS(alg_type)) { |
| 1420 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1421 | } |
| 1422 | |
| 1423 | ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)); |
| 1424 | if (ret != 0) { |
| 1425 | goto exit; |
| 1426 | } |
| 1427 | |
| 1428 | if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 1429 | ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), exp_key, exp_key_len); |
| 1430 | } else { |
| 1431 | ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), exp_key, exp_key_len); |
| 1432 | } |
| 1433 | if (ret != 0) { |
| 1434 | goto exit; |
| 1435 | } |
| 1436 | |
| 1437 | mbedtls_md_type_t md_type = MBEDTLS_MD_NONE; |
| 1438 | if ((alg_type != PSA_ALG_RSA_PKCS1V15_CRYPT) && |
| 1439 | (PSA_ALG_GET_HASH(alg_type) != PSA_ALG_ANY_HASH)) { |
| 1440 | md_type = mbedtls_md_type_from_psa_alg(alg_type); |
| 1441 | } |
| 1442 | |
| 1443 | if (PSA_ALG_IS_RSA_OAEP(alg_type) || PSA_ALG_IS_RSA_PSS(alg_type)) { |
| 1444 | ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V21, md_type); |
| 1445 | } else { |
| 1446 | ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V15, md_type); |
| 1447 | } |
| 1448 | if (ret != 0) { |
| 1449 | goto exit; |
| 1450 | } |
| 1451 | } else |
| 1452 | #endif /* MBEDTLS_RSA_C */ |
| 1453 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
| 1454 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) || |
| 1455 | PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type)) { |
| 1456 | mbedtls_ecp_group_id grp_id; |
| 1457 | |
| 1458 | if (!PSA_ALG_IS_ECDSA(alg_type)) { |
| 1459 | ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1460 | goto exit; |
| 1461 | } |
| 1462 | |
| 1463 | ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)); |
| 1464 | if (ret != 0) { |
| 1465 | goto exit; |
| 1466 | } |
| 1467 | |
| 1468 | grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(key_type), key_bits); |
| 1469 | ret = mbedtls_pk_ecc_set_group(pk, grp_id); |
| 1470 | if (ret != 0) { |
| 1471 | goto exit; |
| 1472 | } |
| 1473 | |
| 1474 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) { |
| 1475 | ret = mbedtls_pk_ecc_set_key(pk, exp_key, exp_key_len); |
| 1476 | if (ret != 0) { |
| 1477 | goto exit; |
| 1478 | } |
| 1479 | ret = mbedtls_pk_ecc_set_pubkey_from_prv(pk, exp_key, exp_key_len, |
| 1480 | mbedtls_psa_get_random, |
| 1481 | MBEDTLS_PSA_RANDOM_STATE); |
| 1482 | } else { |
| 1483 | ret = mbedtls_pk_ecc_set_pubkey(pk, exp_key, exp_key_len); |
| 1484 | } |
| 1485 | if (ret != 0) { |
| 1486 | goto exit; |
| 1487 | } |
| 1488 | } else |
| 1489 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
| 1490 | { |
| 1491 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1492 | } |
| 1493 | |
| 1494 | exit: |
| 1495 | psa_reset_key_attributes(&key_attr); |
| 1496 | mbedtls_platform_zeroize(exp_key, sizeof(exp_key)); |
| 1497 | |
| 1498 | return ret; |
| 1499 | } |
Valerio Setti | 01ba66d | 2024-02-09 11:35:42 +0100 | [diff] [blame] | 1500 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
Valerio Setti | 070d95e | 2024-02-01 11:29:15 +0100 | [diff] [blame] | 1501 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1502 | #endif /* MBEDTLS_PK_C */ |