Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key abstraction layer: wrapper functions |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 21 | |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 22 | #include "mbedtls/platform_util.h" |
| 23 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #if defined(MBEDTLS_PK_C) |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 25 | #include "pk_wrap.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 26 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | e511ffc | 2013-08-22 17:33:21 +0200 | [diff] [blame] | 28 | /* Even if RSA not activated, for the sake of RSA-alt */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/rsa.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 32 | #include "mbedtls/ecp.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 33 | #endif |
| 34 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/ecdsa.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 37 | #endif |
| 38 | |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 39 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PSA_CRYPTO_C) |
| 40 | #include "pkwrite.h" |
Andres Amaya Garcia | e32df08 | 2017-10-25 09:37:04 +0100 | [diff] [blame] | 41 | #endif |
| 42 | |
Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 43 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 44 | #include "psa/crypto.h" |
Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 45 | #include "mbedtls/psa_util.h" |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 46 | #include "hash_info.h" |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 47 | |
Valerio Setti | 80d0798 | 2023-02-08 13:49:17 +0100 | [diff] [blame] | 48 | #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 49 | #include "mbedtls/asn1write.h" |
| 50 | #include "mbedtls/asn1.h" |
Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 51 | #endif |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 52 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 53 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 55 | |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 56 | #include <limits.h> |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 57 | #include <stdint.h> |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 58 | #include <string.h> |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 59 | |
Jerry Yu | b02ee18 | 2022-03-16 10:30:41 +0800 | [diff] [blame] | 60 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | int mbedtls_pk_error_from_psa(psa_status_t status) |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 62 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | switch (status) { |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 64 | case PSA_SUCCESS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | return 0; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 66 | case PSA_ERROR_INVALID_HANDLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 67 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 68 | case PSA_ERROR_NOT_PERMITTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | return MBEDTLS_ERR_ERROR_GENERIC_ERROR; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 70 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 72 | case PSA_ERROR_NOT_SUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 74 | case PSA_ERROR_INVALID_ARGUMENT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | return MBEDTLS_ERR_PK_INVALID_ALG; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 76 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 78 | case PSA_ERROR_BAD_STATE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 79 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 80 | case PSA_ERROR_COMMUNICATION_FAILURE: |
| 81 | case PSA_ERROR_HARDWARE_FAILURE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 83 | case PSA_ERROR_DATA_CORRUPT: |
| 84 | case PSA_ERROR_DATA_INVALID: |
| 85 | case PSA_ERROR_STORAGE_FAILURE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 87 | case PSA_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 89 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | return MBEDTLS_ERR_ERROR_GENERIC_ERROR; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 94 | #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \ |
| 95 | defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | int mbedtls_pk_error_from_psa_rsa(psa_status_t status) |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 97 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | switch (status) { |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 99 | case PSA_ERROR_NOT_PERMITTED: |
| 100 | case PSA_ERROR_INVALID_ARGUMENT: |
| 101 | case PSA_ERROR_INVALID_HANDLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 103 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 105 | case PSA_ERROR_INSUFFICIENT_ENTROPY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | return MBEDTLS_ERR_RSA_RNG_FAILED; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 107 | case PSA_ERROR_INVALID_SIGNATURE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 109 | case PSA_ERROR_INVALID_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | return MBEDTLS_ERR_RSA_INVALID_PADDING; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 111 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | return mbedtls_pk_error_from_psa(status); |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 113 | } |
| 114 | } |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 115 | #endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */ |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 116 | |
| 117 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
| 118 | |
| 119 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 120 | |
| 121 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 122 | int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status) |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 123 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | switch (status) { |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 125 | case PSA_ERROR_NOT_PERMITTED: |
| 126 | case PSA_ERROR_INVALID_ARGUMENT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 128 | case PSA_ERROR_INVALID_HANDLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 130 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 132 | case PSA_ERROR_INSUFFICIENT_ENTROPY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | return MBEDTLS_ERR_ECP_RANDOM_FAILED; |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 134 | case PSA_ERROR_INVALID_SIGNATURE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | return MBEDTLS_ERR_ECP_VERIFY_FAILED; |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 136 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | return mbedtls_pk_error_from_psa(status); |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 138 | } |
| 139 | } |
Jerry Yu | 7533982 | 2022-03-23 12:06:31 +0800 | [diff] [blame] | 140 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 141 | |
Jerry Yu | 7533982 | 2022-03-23 12:06:31 +0800 | [diff] [blame] | 142 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | static int rsa_can_do(mbedtls_pk_type_t type) |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 146 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | return type == MBEDTLS_PK_RSA || |
| 148 | type == MBEDTLS_PK_RSASSA_PSS; |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | static size_t rsa_get_bitlen(const void *ctx) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 152 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) ctx; |
| 154 | return 8 * mbedtls_rsa_get_len(rsa); |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 157 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 158 | static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 159 | const unsigned char *hash, size_t hash_len, |
| 160 | const unsigned char *sig, size_t sig_len) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 161 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 162 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 163 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 164 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 165 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 166 | psa_status_t status; |
| 167 | mbedtls_pk_context key; |
| 168 | int key_len; |
Neil Armstrong | 6baea78 | 2022-03-01 13:52:02 +0100 | [diff] [blame] | 169 | unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES]; |
Neil Armstrong | 82cf804 | 2022-03-03 12:30:59 +0100 | [diff] [blame] | 170 | psa_algorithm_t psa_alg_md = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg)); |
| 172 | size_t rsa_len = mbedtls_rsa_get_len(rsa); |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
| 175 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 176 | } |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | if (sig_len < rsa_len) { |
| 179 | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
| 180 | } |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 181 | |
Neil Armstrong | ea54dbe | 2022-03-14 09:26:48 +0100 | [diff] [blame] | 182 | /* mbedtls_pk_write_pubkey_der() expects a full PK context; |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 183 | * re-construct one to make it happy */ |
Neil Armstrong | 253e9e7 | 2022-03-16 15:32:23 +0100 | [diff] [blame] | 184 | key.pk_info = &mbedtls_rsa_info; |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 185 | key.pk_ctx = ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf)); |
| 187 | if (key_len <= 0) { |
| 188 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 189 | } |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 192 | psa_set_key_algorithm(&attributes, psa_alg_md); |
| 193 | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY); |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | status = psa_import_key(&attributes, |
| 196 | buf + sizeof(buf) - key_len, key_len, |
| 197 | &key_id); |
| 198 | if (status != PSA_SUCCESS) { |
| 199 | ret = mbedtls_pk_error_from_psa(status); |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 200 | goto cleanup; |
| 201 | } |
| 202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len, |
| 204 | sig, sig_len); |
| 205 | if (status != PSA_SUCCESS) { |
| 206 | ret = mbedtls_pk_error_from_psa_rsa(status); |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 207 | goto cleanup; |
| 208 | } |
| 209 | ret = 0; |
| 210 | |
| 211 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 212 | status = psa_destroy_key(key_id); |
| 213 | if (ret == 0 && status != PSA_SUCCESS) { |
| 214 | ret = mbedtls_pk_error_from_psa(status); |
| 215 | } |
Neil Armstrong | a33280a | 2022-02-24 15:17:47 +0100 | [diff] [blame] | 216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | return ret; |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 218 | } |
| 219 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 221 | const unsigned char *hash, size_t hash_len, |
| 222 | const unsigned char *sig, size_t sig_len) |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 223 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 224 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
| 226 | size_t rsa_len = mbedtls_rsa_get_len(rsa); |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
| 229 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 230 | } |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | if (sig_len < rsa_len) { |
| 233 | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
| 234 | } |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg, |
| 237 | (unsigned int) hash_len, |
| 238 | hash, sig)) != 0) { |
| 239 | return ret; |
| 240 | } |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 241 | |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 242 | /* The buffer contains a valid signature followed by extra data. |
| 243 | * We have a special error code for that so that so that callers can |
| 244 | * use mbedtls_pk_verify() to check "Does the buffer start with a |
| 245 | * valid signature?" and not just "Does the buffer contain a valid |
| 246 | * signature?". */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | if (sig_len > rsa_len) { |
| 248 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 249 | } |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 251 | return 0; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 252 | } |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 253 | #endif |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 254 | |
Jerry Yu | b02ee18 | 2022-03-16 10:30:41 +0800 | [diff] [blame] | 255 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg, |
| 257 | mbedtls_rsa_context *rsa_ctx, |
| 258 | const unsigned char *hash, size_t hash_len, |
| 259 | unsigned char *sig, size_t sig_size, |
| 260 | size_t *sig_len) |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 261 | { |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 262 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 263 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 264 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 265 | psa_status_t status; |
| 266 | mbedtls_pk_context key; |
| 267 | int key_len; |
Neil Armstrong | 4b1a059 | 2022-02-25 08:58:12 +0100 | [diff] [blame] | 268 | unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES]; |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 269 | mbedtls_pk_info_t pk_info = mbedtls_rsa_info; |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | *sig_len = mbedtls_rsa_get_len(rsa_ctx); |
| 272 | if (sig_size < *sig_len) { |
| 273 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
| 274 | } |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 275 | |
Neil Armstrong | e4f2868 | 2022-02-24 15:41:39 +0100 | [diff] [blame] | 276 | /* mbedtls_pk_write_key_der() expects a full PK context; |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 277 | * re-construct one to make it happy */ |
| 278 | key.pk_info = &pk_info; |
Jerry Yu | e010de4 | 2022-03-23 11:45:55 +0800 | [diff] [blame] | 279 | key.pk_ctx = rsa_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf)); |
| 281 | if (key_len <= 0) { |
| 282 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 283 | } |
| 284 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 285 | psa_set_key_algorithm(&attributes, alg); |
| 286 | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR); |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | status = psa_import_key(&attributes, |
| 289 | buf + sizeof(buf) - key_len, key_len, |
| 290 | &key_id); |
| 291 | if (status != PSA_SUCCESS) { |
| 292 | ret = mbedtls_pk_error_from_psa(status); |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 293 | goto cleanup; |
| 294 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | status = psa_sign_hash(key_id, alg, hash, hash_len, |
| 296 | sig, sig_size, sig_len); |
| 297 | if (status != PSA_SUCCESS) { |
| 298 | ret = mbedtls_pk_error_from_psa_rsa(status); |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 299 | goto cleanup; |
| 300 | } |
| 301 | |
| 302 | ret = 0; |
| 303 | |
| 304 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 305 | status = psa_destroy_key(key_id); |
| 306 | if (ret == 0 && status != PSA_SUCCESS) { |
| 307 | ret = mbedtls_pk_error_from_psa(status); |
| 308 | } |
| 309 | return ret; |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 310 | } |
Jerry Yu | 406cf27 | 2022-03-22 11:33:42 +0800 | [diff] [blame] | 311 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 312 | |
| 313 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 315 | const unsigned char *hash, size_t hash_len, |
| 316 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 317 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 318 | { |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 319 | ((void) f_rng); |
| 320 | ((void) p_rng); |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 321 | |
Jerry Yu | bd1b327 | 2022-03-24 13:05:20 +0800 | [diff] [blame] | 322 | psa_algorithm_t psa_md_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg); |
| 324 | if (psa_md_alg == 0) { |
| 325 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 326 | } |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN( |
| 329 | psa_md_alg), |
| 330 | ctx, hash, hash_len, |
| 331 | sig, sig_size, sig_len); |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 332 | } |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 333 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 335 | const unsigned char *hash, size_t hash_len, |
| 336 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 337 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 338 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 339 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 341 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
| 342 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 343 | } |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | *sig_len = mbedtls_rsa_get_len(rsa); |
| 346 | if (sig_size < *sig_len) { |
| 347 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
| 348 | } |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng, |
| 351 | md_alg, (unsigned int) hash_len, |
| 352 | hash, sig); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 353 | } |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 354 | #endif |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 355 | |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 356 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | static int rsa_decrypt_wrap(void *ctx, |
| 358 | const unsigned char *input, size_t ilen, |
| 359 | unsigned char *output, size_t *olen, size_t osize, |
| 360 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 361 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 362 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 363 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 364 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 365 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 366 | psa_status_t status; |
| 367 | mbedtls_pk_context key; |
| 368 | int key_len; |
Neil Armstrong | b556a42 | 2022-02-25 08:58:12 +0100 | [diff] [blame] | 369 | unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES]; |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 370 | |
| 371 | ((void) f_rng); |
| 372 | ((void) p_rng); |
| 373 | |
| 374 | #if !defined(MBEDTLS_RSA_ALT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 375 | if (rsa->padding != MBEDTLS_RSA_PKCS_V15) { |
| 376 | return MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 377 | } |
Neil Armstrong | 8e80504 | 2022-03-16 15:30:31 +0100 | [diff] [blame] | 378 | #endif /* !MBEDTLS_RSA_ALT */ |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | if (ilen != mbedtls_rsa_get_len(rsa)) { |
| 381 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 382 | } |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 383 | |
| 384 | /* mbedtls_pk_write_key_der() expects a full PK context; |
| 385 | * re-construct one to make it happy */ |
Neil Armstrong | 6b03a3d | 2022-03-16 15:31:07 +0100 | [diff] [blame] | 386 | key.pk_info = &mbedtls_rsa_info; |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 387 | key.pk_ctx = ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 388 | key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf)); |
| 389 | if (key_len <= 0) { |
| 390 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 391 | } |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR); |
| 394 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 395 | psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT); |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | status = psa_import_key(&attributes, |
| 398 | buf + sizeof(buf) - key_len, key_len, |
| 399 | &key_id); |
| 400 | if (status != PSA_SUCCESS) { |
| 401 | ret = mbedtls_pk_error_from_psa(status); |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 402 | goto cleanup; |
| 403 | } |
| 404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT, |
| 406 | input, ilen, |
| 407 | NULL, 0, |
| 408 | output, osize, olen); |
| 409 | if (status != PSA_SUCCESS) { |
| 410 | ret = mbedtls_pk_error_from_psa_rsa(status); |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 411 | goto cleanup; |
| 412 | } |
| 413 | |
| 414 | ret = 0; |
| 415 | |
| 416 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
| 418 | status = psa_destroy_key(key_id); |
| 419 | if (ret == 0 && status != PSA_SUCCESS) { |
| 420 | ret = mbedtls_pk_error_from_psa(status); |
| 421 | } |
Neil Armstrong | f1b564b | 2022-02-24 15:17:47 +0100 | [diff] [blame] | 422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | return ret; |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 424 | } |
| 425 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | static int rsa_decrypt_wrap(void *ctx, |
| 427 | const unsigned char *input, size_t ilen, |
| 428 | unsigned char *output, size_t *olen, size_t osize, |
| 429 | 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] | 430 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | if (ilen != mbedtls_rsa_get_len(rsa)) { |
| 434 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 435 | } |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng, |
| 438 | olen, input, output, osize); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 439 | } |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 440 | #endif |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 441 | |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 442 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | static int rsa_encrypt_wrap(void *ctx, |
| 444 | const unsigned char *input, size_t ilen, |
| 445 | unsigned char *output, size_t *olen, size_t osize, |
| 446 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 447 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 449 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 450 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 451 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 452 | psa_status_t status; |
| 453 | mbedtls_pk_context key; |
| 454 | int key_len; |
Neil Armstrong | deb4bfb | 2022-02-25 08:58:12 +0100 | [diff] [blame] | 455 | unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES]; |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 456 | |
| 457 | ((void) f_rng); |
| 458 | ((void) p_rng); |
| 459 | |
| 460 | #if !defined(MBEDTLS_RSA_ALT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 461 | if (rsa->padding != MBEDTLS_RSA_PKCS_V15) { |
| 462 | return MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 463 | } |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 464 | #endif |
| 465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | if (mbedtls_rsa_get_len(rsa) > osize) { |
| 467 | return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 468 | } |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 469 | |
Neil Armstrong | ac014ca | 2022-02-24 15:27:54 +0100 | [diff] [blame] | 470 | /* mbedtls_pk_write_pubkey_der() expects a full PK context; |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 471 | * re-construct one to make it happy */ |
Neil Armstrong | da1d80d | 2022-03-16 15:36:32 +0100 | [diff] [blame] | 472 | key.pk_info = &mbedtls_rsa_info; |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 473 | key.pk_ctx = ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf)); |
| 475 | if (key_len <= 0) { |
| 476 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 477 | } |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 480 | psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT); |
| 481 | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY); |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | status = psa_import_key(&attributes, |
| 484 | buf + sizeof(buf) - key_len, key_len, |
| 485 | &key_id); |
| 486 | if (status != PSA_SUCCESS) { |
| 487 | ret = mbedtls_pk_error_from_psa(status); |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 488 | goto cleanup; |
| 489 | } |
| 490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT, |
| 492 | input, ilen, |
| 493 | NULL, 0, |
| 494 | output, osize, olen); |
| 495 | if (status != PSA_SUCCESS) { |
| 496 | ret = mbedtls_pk_error_from_psa_rsa(status); |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 497 | goto cleanup; |
| 498 | } |
| 499 | |
| 500 | ret = 0; |
| 501 | |
| 502 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | status = psa_destroy_key(key_id); |
| 504 | if (ret == 0 && status != PSA_SUCCESS) { |
| 505 | ret = mbedtls_pk_error_from_psa(status); |
| 506 | } |
Neil Armstrong | 7dd3b20 | 2022-02-24 15:29:18 +0100 | [diff] [blame] | 507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 508 | return ret; |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 509 | } |
| 510 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 511 | static int rsa_encrypt_wrap(void *ctx, |
| 512 | const unsigned char *input, size_t ilen, |
| 513 | unsigned char *output, size_t *olen, size_t osize, |
| 514 | 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] | 515 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
| 517 | *olen = mbedtls_rsa_get_len(rsa); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | if (*olen > osize) { |
| 520 | return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 521 | } |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 523 | return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng, |
| 524 | ilen, input, output); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 525 | } |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 526 | #endif |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 528 | static int rsa_check_pair_wrap(const void *pub, const void *prv, |
| 529 | int (*f_rng)(void *, unsigned char *, size_t), |
| 530 | void *p_rng) |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 531 | { |
Manuel Pégourié-Gonnard | 39be141 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 532 | (void) f_rng; |
| 533 | (void) p_rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub, |
| 535 | (const mbedtls_rsa_context *) prv); |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | static void *rsa_alloc_wrap(void) |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 539 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 540 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context)); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | if (ctx != NULL) { |
| 543 | mbedtls_rsa_init((mbedtls_rsa_context *) ctx); |
| 544 | } |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | return ctx; |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 547 | } |
| 548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | static void rsa_free_wrap(void *ctx) |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 550 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | mbedtls_rsa_free((mbedtls_rsa_context *) ctx); |
| 552 | mbedtls_free(ctx); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 555 | static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items) |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 556 | { |
Gilles Peskine | 85b1bc6 | 2021-05-25 09:20:26 +0200 | [diff] [blame] | 557 | #if defined(MBEDTLS_RSA_ALT) |
| 558 | /* Not supported */ |
| 559 | (void) ctx; |
| 560 | (void) items; |
| 561 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | items->type = MBEDTLS_PK_DEBUG_MPI; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 563 | items->name = "rsa.N"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | items->value = &(((mbedtls_rsa_context *) ctx)->N); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 565 | |
| 566 | items++; |
| 567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 568 | items->type = MBEDTLS_PK_DEBUG_MPI; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 569 | items->name = "rsa.E"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | items->value = &(((mbedtls_rsa_context *) ctx)->E); |
Gilles Peskine | 85b1bc6 | 2021-05-25 09:20:26 +0200 | [diff] [blame] | 571 | #endif |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 572 | } |
| 573 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | const mbedtls_pk_info_t mbedtls_rsa_info = { |
| 575 | MBEDTLS_PK_RSA, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 576 | "RSA", |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 577 | rsa_get_bitlen, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 578 | rsa_can_do, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 579 | rsa_verify_wrap, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 580 | rsa_sign_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 581 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 582 | NULL, |
| 583 | NULL, |
| 584 | #endif |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 585 | rsa_decrypt_wrap, |
| 586 | rsa_encrypt_wrap, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 587 | rsa_check_pair_wrap, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 588 | rsa_alloc_wrap, |
| 589 | rsa_free_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 590 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 591 | NULL, |
| 592 | NULL, |
| 593 | #endif |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 594 | rsa_debug, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 595 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 597 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 598 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 599 | /* |
| 600 | * Generic EC key |
| 601 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 602 | static int eckey_can_do(mbedtls_pk_type_t type) |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 603 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | return type == MBEDTLS_PK_ECKEY || |
| 605 | type == MBEDTLS_PK_ECKEY_DH || |
| 606 | type == MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 607 | } |
| 608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 609 | static size_t eckey_get_bitlen(const void *ctx) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 610 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 611 | return ((mbedtls_ecp_keypair *) ctx)->grp.pbits; |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 614 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 615 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 616 | /* |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 617 | * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of |
| 618 | * those integers and convert it to the fixed-length encoding expected by PSA. |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 619 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 620 | static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end, |
| 621 | unsigned char *to, size_t to_len) |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 622 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 623 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 624 | size_t unpadded_len, padding_len; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len, |
| 627 | MBEDTLS_ASN1_INTEGER)) != 0) { |
| 628 | return ret; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 629 | } |
| 630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 631 | while (unpadded_len > 0 && **from == 0x00) { |
| 632 | (*from)++; |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 633 | unpadded_len--; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 634 | } |
| 635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 636 | if (unpadded_len > to_len || unpadded_len == 0) { |
| 637 | return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 638 | } |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 639 | |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 640 | padding_len = to_len - unpadded_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | memset(to, 0x00, padding_len); |
| 642 | memcpy(to + padding_len, *from, unpadded_len); |
| 643 | (*from) += unpadded_len; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 645 | return 0; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 649 | * Convert a signature from an ASN.1 sequence of two integers |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 650 | * to a raw {r,s} buffer. Note: the provided sig buffer must be at least |
Andrzej Kurek | b6016c5 | 2018-11-19 17:41:58 -0500 | [diff] [blame] | 651 | * twice as big as int_size. |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 652 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 653 | static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end, |
| 654 | unsigned char *sig, size_t int_size) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 655 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 656 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 657 | size_t tmp_size; |
Andrzej Kurek | 3f864c2 | 2018-11-07 09:30:50 -0500 | [diff] [blame] | 658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 659 | if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size, |
| 660 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 661 | return ret; |
| 662 | } |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 663 | |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 664 | /* Extract r */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) { |
| 666 | return ret; |
| 667 | } |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 668 | /* Extract s */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 669 | if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) { |
| 670 | return ret; |
| 671 | } |
Andrzej Kurek | 3f864c2 | 2018-11-07 09:30:50 -0500 | [diff] [blame] | 672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 673 | return 0; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 674 | } |
| 675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 676 | static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg, |
| 677 | const unsigned char *hash, size_t hash_len, |
| 678 | const unsigned char *sig, size_t sig_len) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 679 | { |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 680 | mbedtls_ecp_keypair *ctx = ctx_arg; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 681 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 682 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 683 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 684 | psa_status_t status; |
Gilles Peskine | b4a87b0 | 2022-10-04 22:54:26 +0200 | [diff] [blame] | 685 | size_t key_len; |
Valerio Setti | 5c032b5 | 2023-02-02 15:10:32 +0100 | [diff] [blame] | 686 | /* This buffer will initially contain the public key and then the signature |
| 687 | * but at different points in time. For all curves except secp224k1, which |
| 688 | * is not currently supported in PSA, the public key is one byte longer |
| 689 | * (header byte + 2 numbers, while the signature is only 2 numbers), |
| 690 | * so use that as the buffer size. */ |
Valerio Setti | 1337a4f | 2023-01-30 15:54:55 +0100 | [diff] [blame] | 691 | unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
Hanno Becker | a985111 | 2019-01-25 16:37:10 +0000 | [diff] [blame] | 692 | unsigned char *p; |
John Durkop | 2ec2eaa | 2020-08-24 18:29:15 -0700 | [diff] [blame] | 693 | psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY; |
Gilles Peskine | fc2459d | 2019-12-12 17:50:44 +0100 | [diff] [blame] | 694 | size_t curve_bits; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 695 | psa_ecc_family_t curve = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits); |
| 697 | const size_t signature_part_size = (ctx->grp.nbits + 7) / 8; |
John Durkop | 2ec2eaa | 2020-08-24 18:29:15 -0700 | [diff] [blame] | 698 | ((void) md_alg); |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | if (curve == 0) { |
| 701 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 702 | } |
Andrzej Kurek | b3d1b12 | 2018-11-07 08:18:52 -0500 | [diff] [blame] | 703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 704 | psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve)); |
| 705 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 706 | psa_set_key_algorithm(&attributes, psa_sig_md); |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 707 | |
Gilles Peskine | b4a87b0 | 2022-10-04 22:54:26 +0200 | [diff] [blame] | 708 | ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q, |
| 709 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 710 | &key_len, buf, sizeof(buf)); |
| 711 | if (ret != 0) { |
| 712 | goto cleanup; |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 713 | } |
| 714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 715 | status = psa_import_key(&attributes, |
Gilles Peskine | b4a87b0 | 2022-10-04 22:54:26 +0200 | [diff] [blame] | 716 | buf, key_len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 717 | &key_id); |
| 718 | if (status != PSA_SUCCESS) { |
| 719 | ret = mbedtls_pk_error_from_psa(status); |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 720 | goto cleanup; |
| 721 | } |
| 722 | |
Andrzej Kurek | eeac03b | 2018-11-20 06:39:06 -0500 | [diff] [blame] | 723 | /* We don't need the exported key anymore and can |
| 724 | * reuse its buffer for signature extraction. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 725 | if (2 * signature_part_size > sizeof(buf)) { |
Andrzej Kurek | b6016c5 | 2018-11-19 17:41:58 -0500 | [diff] [blame] | 726 | ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 727 | goto cleanup; |
| 728 | } |
Andrzej Kurek | b6016c5 | 2018-11-19 17:41:58 -0500 | [diff] [blame] | 729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 730 | p = (unsigned char *) sig; |
| 731 | if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf, |
| 732 | signature_part_size)) != 0) { |
Andrzej Kurek | b6016c5 | 2018-11-19 17:41:58 -0500 | [diff] [blame] | 733 | goto cleanup; |
| 734 | } |
| 735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 736 | status = psa_verify_hash(key_id, psa_sig_md, |
| 737 | hash, hash_len, |
| 738 | buf, 2 * signature_part_size); |
| 739 | if (status != PSA_SUCCESS) { |
| 740 | ret = mbedtls_pk_error_from_psa_ecdsa(status); |
| 741 | goto cleanup; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 742 | } |
Andrzej Kurek | ad5d581 | 2018-11-20 07:59:18 -0500 | [diff] [blame] | 743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 744 | if (p != sig + sig_len) { |
Andrzej Kurek | ad5d581 | 2018-11-20 07:59:18 -0500 | [diff] [blame] | 745 | ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 746 | goto cleanup; |
| 747 | } |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 748 | ret = 0; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 749 | |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 750 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 751 | status = psa_destroy_key(key_id); |
| 752 | if (ret == 0 && status != PSA_SUCCESS) { |
| 753 | ret = mbedtls_pk_error_from_psa(status); |
| 754 | } |
Neil Armstrong | 9dccd86 | 2022-02-24 15:33:13 +0100 | [diff] [blame] | 755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 756 | return ret; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 757 | } |
| 758 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 759 | static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 760 | const unsigned char *hash, size_t hash_len, |
| 761 | const unsigned char *sig, size_t sig_len) |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 762 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 763 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 764 | ((void) md_alg); |
| 765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 766 | ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) ctx, |
| 767 | hash, hash_len, sig, sig_len); |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 769 | if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) { |
| 770 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 771 | } |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 773 | return ret; |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 774 | } |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 775 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 776 | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 777 | |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 778 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 779 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 780 | /* |
| 781 | * Simultaneously convert and move raw MPI from the beginning of a buffer |
| 782 | * to an ASN.1 MPI at the end of the buffer. |
| 783 | * See also mbedtls_asn1_write_mpi(). |
| 784 | * |
| 785 | * p: pointer to the end of the output buffer |
| 786 | * start: start of the output buffer, and also of the mpi to write at the end |
| 787 | * n_len: length of the mpi to read from start |
| 788 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 789 | static int asn1_write_mpibuf(unsigned char **p, unsigned char *start, |
| 790 | size_t n_len) |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 791 | { |
| 792 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 793 | size_t len = 0; |
| 794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 795 | if ((size_t) (*p - start) < n_len) { |
| 796 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 797 | } |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 798 | |
| 799 | len = n_len; |
| 800 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 801 | memmove(*p, start, len); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 802 | |
| 803 | /* ASN.1 DER encoding requires minimal length, so skip leading 0s. |
| 804 | * Neither r nor s should be 0, but as a failsafe measure, still detect |
| 805 | * that rather than overflowing the buffer in case of a PSA error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 806 | while (len > 0 && **p == 0x00) { |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 807 | ++(*p); |
| 808 | --len; |
| 809 | } |
| 810 | |
| 811 | /* this is only reached if the signature was invalid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 812 | if (len == 0) { |
| 813 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
| 814 | } |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 815 | |
| 816 | /* if the msb is 1, ASN.1 requires that we prepend a 0. |
| 817 | * Neither r nor s can be 0, so we can assume len > 0 at all times. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 818 | if (**p & 0x80) { |
| 819 | if (*p - start < 1) { |
| 820 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 821 | } |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 822 | |
| 823 | *--(*p) = 0x00; |
| 824 | len += 1; |
| 825 | } |
| 826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 827 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 828 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 829 | MBEDTLS_ASN1_INTEGER)); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | return (int) len; |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /* Transcode signature from PSA format to ASN.1 sequence. |
| 835 | * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of |
| 836 | * MPIs, and in-place. |
| 837 | * |
| 838 | * [in/out] sig: the signature pre- and post-transcoding |
| 839 | * [in/out] sig_len: signature length pre- and post-transcoding |
| 840 | * [int] buf_len: the available size the in/out buffer |
| 841 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 842 | static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len, |
| 843 | size_t buf_len) |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 844 | { |
| 845 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 846 | size_t len = 0; |
| 847 | const size_t rs_len = *sig_len / 2; |
| 848 | unsigned char *p = sig + buf_len; |
| 849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 850 | MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len)); |
| 851 | MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len)); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 853 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len)); |
| 854 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig, |
| 855 | MBEDTLS_ASN1_CONSTRUCTED | |
| 856 | MBEDTLS_ASN1_SEQUENCE)); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 858 | memmove(sig, p, len); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 859 | *sig_len = len; |
| 860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 861 | return 0; |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 862 | } |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 864 | static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg, |
| 865 | const unsigned char *hash, size_t hash_len, |
| 866 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 867 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 868 | { |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 869 | mbedtls_ecp_keypair *ctx = ctx_arg; |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 870 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 871 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 872 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 873 | psa_status_t status; |
Valerio Setti | 1337a4f | 2023-01-30 15:54:55 +0100 | [diff] [blame] | 874 | unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH]; |
Manuel Pégourié-Gonnard | 79ae7eb | 2022-12-05 12:55:51 +0100 | [diff] [blame] | 875 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Valerio Setti | 5bc5224 | 2023-01-30 15:48:28 +0100 | [diff] [blame] | 876 | psa_algorithm_t psa_sig_md = |
Valerio Setti | b761b15 | 2023-01-31 14:56:04 +0100 | [diff] [blame] | 877 | PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_hash_info_psa_from_md(md_alg)); |
Manuel Pégourié-Gonnard | 79ae7eb | 2022-12-05 12:55:51 +0100 | [diff] [blame] | 878 | #else |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 879 | psa_algorithm_t psa_sig_md = |
Valerio Setti | b761b15 | 2023-01-31 14:56:04 +0100 | [diff] [blame] | 880 | PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg)); |
Manuel Pégourié-Gonnard | 79ae7eb | 2022-12-05 12:55:51 +0100 | [diff] [blame] | 881 | #endif |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 882 | size_t curve_bits; |
| 883 | psa_ecc_family_t curve = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 884 | mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits); |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 885 | size_t key_len = PSA_BITS_TO_BYTES(curve_bits); |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 886 | |
| 887 | /* PSA has its own RNG */ |
| 888 | ((void) f_rng); |
| 889 | ((void) p_rng); |
| 890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 891 | if (curve == 0) { |
| 892 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 893 | } |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 894 | |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 895 | if (key_len > sizeof(buf)) { |
Valerio Setti | b761b15 | 2023-01-31 14:56:04 +0100 | [diff] [blame] | 896 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 897 | } |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 898 | ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 899 | if (ret != 0) { |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 900 | goto cleanup; |
| 901 | } |
| 902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 903 | psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve)); |
| 904 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 905 | psa_set_key_algorithm(&attributes, psa_sig_md); |
| 906 | |
| 907 | status = psa_import_key(&attributes, |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 908 | buf, key_len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 909 | &key_id); |
| 910 | if (status != PSA_SUCCESS) { |
| 911 | ret = mbedtls_pk_error_from_psa(status); |
| 912 | goto cleanup; |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 913 | } |
| 914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 915 | status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len, |
| 916 | sig, sig_size, sig_len); |
| 917 | if (status != PSA_SUCCESS) { |
| 918 | ret = mbedtls_pk_error_from_psa_ecdsa(status); |
| 919 | goto cleanup; |
| 920 | } |
| 921 | |
| 922 | ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size); |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 923 | |
| 924 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 925 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
| 926 | status = psa_destroy_key(key_id); |
| 927 | if (ret == 0 && status != PSA_SUCCESS) { |
| 928 | ret = mbedtls_pk_error_from_psa(status); |
| 929 | } |
Neil Armstrong | ff70f0b | 2022-03-03 14:31:17 +0100 | [diff] [blame] | 930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 931 | return ret; |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 932 | } |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 933 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 934 | static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 935 | const unsigned char *hash, size_t hash_len, |
| 936 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 937 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 938 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 939 | return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx, |
| 940 | md_alg, hash, hash_len, |
| 941 | sig, sig_size, sig_len, |
| 942 | f_rng, p_rng); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 943 | } |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 944 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 945 | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 946 | |
Valerio Setti | 80d0798 | 2023-02-08 13:49:17 +0100 | [diff] [blame] | 947 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 948 | /* Forward declarations */ |
| 949 | static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 950 | const unsigned char *hash, size_t hash_len, |
| 951 | const unsigned char *sig, size_t sig_len, |
| 952 | void *rs_ctx); |
| 953 | |
| 954 | static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 955 | const unsigned char *hash, size_t hash_len, |
| 956 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 957 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 958 | void *rs_ctx); |
| 959 | |
| 960 | /* |
| 961 | * Restart context for ECDSA operations with ECKEY context |
| 962 | * |
| 963 | * We need to store an actual ECDSA context, as we need to pass the same to |
| 964 | * the underlying ecdsa function, so we can't create it on the fly every time. |
| 965 | */ |
| 966 | typedef struct { |
| 967 | mbedtls_ecdsa_restart_ctx ecdsa_rs; |
| 968 | mbedtls_ecdsa_context ecdsa_ctx; |
| 969 | } eckey_restart_ctx; |
| 970 | |
| 971 | static void *eckey_rs_alloc(void) |
| 972 | { |
| 973 | eckey_restart_ctx *rs_ctx; |
| 974 | |
| 975 | void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx)); |
| 976 | |
| 977 | if (ctx != NULL) { |
| 978 | rs_ctx = ctx; |
| 979 | mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs); |
| 980 | mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx); |
| 981 | } |
| 982 | |
| 983 | return ctx; |
| 984 | } |
| 985 | |
| 986 | static void eckey_rs_free(void *ctx) |
| 987 | { |
| 988 | eckey_restart_ctx *rs_ctx; |
| 989 | |
| 990 | if (ctx == NULL) { |
| 991 | return; |
| 992 | } |
| 993 | |
| 994 | rs_ctx = ctx; |
| 995 | mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs); |
| 996 | mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx); |
| 997 | |
| 998 | mbedtls_free(ctx); |
| 999 | } |
| 1000 | |
| 1001 | static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1002 | const unsigned char *hash, size_t hash_len, |
| 1003 | const unsigned char *sig, size_t sig_len, |
| 1004 | void *rs_ctx) |
| 1005 | { |
| 1006 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1007 | eckey_restart_ctx *rs = rs_ctx; |
| 1008 | |
| 1009 | /* Should never happen */ |
| 1010 | if (rs == NULL) { |
| 1011 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1012 | } |
| 1013 | |
| 1014 | /* set up our own sub-context if needed (that is, on first run) */ |
| 1015 | if (rs->ecdsa_ctx.grp.pbits == 0) { |
| 1016 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx)); |
| 1017 | } |
| 1018 | |
| 1019 | MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx, |
| 1020 | md_alg, hash, hash_len, |
| 1021 | sig, sig_len, &rs->ecdsa_rs)); |
| 1022 | |
| 1023 | cleanup: |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1028 | const unsigned char *hash, size_t hash_len, |
| 1029 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1030 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 1031 | void *rs_ctx) |
| 1032 | { |
| 1033 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1034 | eckey_restart_ctx *rs = rs_ctx; |
| 1035 | |
| 1036 | /* Should never happen */ |
| 1037 | if (rs == NULL) { |
| 1038 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1039 | } |
| 1040 | |
| 1041 | /* set up our own sub-context if needed (that is, on first run) */ |
| 1042 | if (rs->ecdsa_ctx.grp.pbits == 0) { |
| 1043 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx)); |
| 1044 | } |
| 1045 | |
| 1046 | MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg, |
| 1047 | hash, hash_len, sig, sig_size, sig_len, |
| 1048 | f_rng, p_rng, &rs->ecdsa_rs)); |
| 1049 | |
| 1050 | cleanup: |
| 1051 | return ret; |
| 1052 | } |
Valerio Setti | 80d0798 | 2023-02-08 13:49:17 +0100 | [diff] [blame] | 1053 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1054 | |
| 1055 | static int eckey_check_pair(const void *pub, const void *prv, |
| 1056 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1057 | void *p_rng) |
| 1058 | { |
| 1059 | return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub, |
| 1060 | (const mbedtls_ecp_keypair *) prv, |
| 1061 | f_rng, p_rng); |
| 1062 | } |
| 1063 | |
| 1064 | static void *eckey_alloc_wrap(void) |
| 1065 | { |
| 1066 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair)); |
| 1067 | |
| 1068 | if (ctx != NULL) { |
| 1069 | mbedtls_ecp_keypair_init(ctx); |
| 1070 | } |
| 1071 | |
| 1072 | return ctx; |
| 1073 | } |
| 1074 | |
| 1075 | static void eckey_free_wrap(void *ctx) |
| 1076 | { |
| 1077 | mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx); |
| 1078 | mbedtls_free(ctx); |
| 1079 | } |
| 1080 | |
| 1081 | static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items) |
| 1082 | { |
| 1083 | items->type = MBEDTLS_PK_DEBUG_ECP; |
| 1084 | items->name = "eckey.Q"; |
| 1085 | items->value = &(((mbedtls_ecp_keypair *) ctx)->Q); |
| 1086 | } |
| 1087 | |
| 1088 | const mbedtls_pk_info_t mbedtls_eckey_info = { |
| 1089 | MBEDTLS_PK_ECKEY, |
| 1090 | "EC", |
| 1091 | eckey_get_bitlen, |
| 1092 | eckey_can_do, |
| 1093 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
| 1094 | ecdsa_verify_wrap, /* Compatible key structures */ |
| 1095 | #else |
| 1096 | NULL, |
| 1097 | #endif |
| 1098 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
| 1099 | ecdsa_sign_wrap, /* Compatible key structures */ |
| 1100 | #else |
| 1101 | NULL, |
| 1102 | #endif |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1103 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1104 | eckey_verify_rs_wrap, |
| 1105 | eckey_sign_rs_wrap, |
| 1106 | #endif |
| 1107 | NULL, |
| 1108 | NULL, |
| 1109 | eckey_check_pair, |
| 1110 | eckey_alloc_wrap, |
| 1111 | eckey_free_wrap, |
| 1112 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1113 | eckey_rs_alloc, |
| 1114 | eckey_rs_free, |
| 1115 | #endif |
| 1116 | eckey_debug, |
| 1117 | }; |
| 1118 | |
| 1119 | /* |
| 1120 | * EC key restricted to ECDH |
| 1121 | */ |
| 1122 | static int eckeydh_can_do(mbedtls_pk_type_t type) |
| 1123 | { |
| 1124 | return type == MBEDTLS_PK_ECKEY || |
| 1125 | type == MBEDTLS_PK_ECKEY_DH; |
| 1126 | } |
| 1127 | |
| 1128 | const mbedtls_pk_info_t mbedtls_eckeydh_info = { |
| 1129 | MBEDTLS_PK_ECKEY_DH, |
| 1130 | "EC_DH", |
| 1131 | eckey_get_bitlen, /* Same underlying key structure */ |
| 1132 | eckeydh_can_do, |
| 1133 | NULL, |
| 1134 | NULL, |
| 1135 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1136 | NULL, |
| 1137 | NULL, |
| 1138 | #endif |
| 1139 | NULL, |
| 1140 | NULL, |
| 1141 | eckey_check_pair, |
| 1142 | eckey_alloc_wrap, /* Same underlying key structure */ |
| 1143 | eckey_free_wrap, /* Same underlying key structure */ |
| 1144 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1145 | NULL, |
| 1146 | NULL, |
| 1147 | #endif |
| 1148 | eckey_debug, /* Same underlying key structure */ |
| 1149 | }; |
| 1150 | #endif /* MBEDTLS_ECP_C */ |
| 1151 | |
| 1152 | #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) |
| 1153 | static int ecdsa_can_do(mbedtls_pk_type_t type) |
| 1154 | { |
| 1155 | return type == MBEDTLS_PK_ECDSA; |
| 1156 | } |
| 1157 | |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1158 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1159 | static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1160 | const unsigned char *hash, size_t hash_len, |
| 1161 | const unsigned char *sig, size_t sig_len, |
| 1162 | void *rs_ctx) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1163 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1164 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1165 | ((void) md_alg); |
| 1166 | |
| 1167 | ret = mbedtls_ecdsa_read_signature_restartable( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1168 | (mbedtls_ecdsa_context *) ctx, |
| 1169 | hash, hash_len, sig, sig_len, |
| 1170 | (mbedtls_ecdsa_restart_ctx *) rs_ctx); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1172 | if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) { |
| 1173 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 1174 | } |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1176 | return ret; |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1177 | } |
| 1178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1180 | const unsigned char *hash, size_t hash_len, |
| 1181 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1182 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 1183 | void *rs_ctx) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1184 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1185 | return mbedtls_ecdsa_write_signature_restartable( |
| 1186 | (mbedtls_ecdsa_context *) ctx, |
| 1187 | md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng, |
| 1188 | (mbedtls_ecdsa_restart_ctx *) rs_ctx); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1189 | |
| 1190 | } |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1192 | static void *ecdsa_rs_alloc(void) |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1193 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1194 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx)); |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1196 | if (ctx != NULL) { |
| 1197 | mbedtls_ecdsa_restart_init(ctx); |
| 1198 | } |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1200 | return ctx; |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1201 | } |
| 1202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1203 | static void ecdsa_rs_free(void *ctx) |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1204 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1205 | mbedtls_ecdsa_restart_free(ctx); |
| 1206 | mbedtls_free(ctx); |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1207 | } |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1208 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1210 | const mbedtls_pk_info_t mbedtls_ecdsa_info = { |
| 1211 | MBEDTLS_PK_ECDSA, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 1212 | "ECDSA", |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 1213 | eckey_get_bitlen, /* Compatible key structures */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 1214 | ecdsa_can_do, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1215 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
| 1216 | ecdsa_verify_wrap, /* Compatible key structures */ |
| 1217 | #else |
| 1218 | NULL, |
| 1219 | #endif |
| 1220 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
| 1221 | ecdsa_sign_wrap, /* Compatible key structures */ |
| 1222 | #else |
| 1223 | NULL, |
| 1224 | #endif |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1225 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1226 | ecdsa_verify_rs_wrap, |
| 1227 | ecdsa_sign_rs_wrap, |
| 1228 | #endif |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1229 | NULL, |
| 1230 | NULL, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1231 | eckey_check_pair, /* Compatible key structures */ |
Valerio Setti | 24138d9 | 2023-01-27 14:24:09 +0100 | [diff] [blame] | 1232 | eckey_alloc_wrap, /* Compatible key structures */ |
| 1233 | eckey_free_wrap, /* Compatible key structures */ |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1234 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1235 | ecdsa_rs_alloc, |
| 1236 | ecdsa_rs_free, |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1237 | #endif |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 1238 | eckey_debug, /* Compatible key structures */ |
| 1239 | }; |
Valerio Setti | 7ca1318 | 2023-01-27 13:22:42 +0100 | [diff] [blame] | 1240 | #endif /* MBEDTLS_PK_CAN_ECDSA_SOME */ |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1242 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1243 | /* |
| 1244 | * Support for alternative RSA-private implementations |
| 1245 | */ |
| 1246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1247 | static int rsa_alt_can_do(mbedtls_pk_type_t type) |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1248 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1249 | return type == MBEDTLS_PK_RSA; |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1252 | static size_t rsa_alt_get_bitlen(const void *ctx) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1253 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1254 | const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1256 | return 8 * rsa_alt->key_len_func(rsa_alt->key); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1257 | } |
| 1258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1259 | static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1260 | const unsigned char *hash, size_t hash_len, |
| 1261 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1262 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1263 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1264 | mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1266 | if (UINT_MAX < hash_len) { |
| 1267 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1268 | } |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 1269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1270 | *sig_len = rsa_alt->key_len_func(rsa_alt->key); |
| 1271 | if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) { |
| 1272 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1273 | } |
| 1274 | if (*sig_len > sig_size) { |
| 1275 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
| 1276 | } |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1278 | return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng, |
| 1279 | md_alg, (unsigned int) hash_len, hash, sig); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1280 | } |
| 1281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1282 | static int rsa_alt_decrypt_wrap(void *ctx, |
| 1283 | const unsigned char *input, size_t ilen, |
| 1284 | unsigned char *output, size_t *olen, size_t osize, |
| 1285 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1286 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1287 | mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1288 | |
| 1289 | ((void) f_rng); |
| 1290 | ((void) p_rng); |
| 1291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1292 | if (ilen != rsa_alt->key_len_func(rsa_alt->key)) { |
| 1293 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1294 | } |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1296 | return rsa_alt->decrypt_func(rsa_alt->key, |
| 1297 | olen, input, output, osize); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1298 | } |
| 1299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1300 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1301 | static int rsa_alt_check_pair(const void *pub, const void *prv, |
| 1302 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1303 | void *p_rng) |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1304 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1305 | unsigned char sig[MBEDTLS_MPI_MAX_SIZE]; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1306 | unsigned char hash[32]; |
| 1307 | size_t sig_len = 0; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1308 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1310 | if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) { |
| 1311 | return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1312 | } |
| 1313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | memset(hash, 0x2a, sizeof(hash)); |
| 1315 | |
| 1316 | if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE, |
| 1317 | hash, sizeof(hash), |
| 1318 | sig, sizeof(sig), &sig_len, |
| 1319 | f_rng, p_rng)) != 0) { |
| 1320 | return ret; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1323 | if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE, |
| 1324 | hash, sizeof(hash), sig, sig_len) != 0) { |
| 1325 | return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 1326 | } |
| 1327 | |
| 1328 | return 0; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1329 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1330 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1332 | static void *rsa_alt_alloc_wrap(void) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1333 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1334 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context)); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1336 | if (ctx != NULL) { |
| 1337 | memset(ctx, 0, sizeof(mbedtls_rsa_alt_context)); |
| 1338 | } |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1340 | return ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1341 | } |
| 1342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1343 | static void rsa_alt_free_wrap(void *ctx) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1344 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1345 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context)); |
| 1346 | mbedtls_free(ctx); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1349 | const mbedtls_pk_info_t mbedtls_rsa_alt_info = { |
| 1350 | MBEDTLS_PK_RSA_ALT, |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1351 | "RSA-alt", |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 1352 | rsa_alt_get_bitlen, |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1353 | rsa_alt_can_do, |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1354 | NULL, |
| 1355 | rsa_alt_sign_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1356 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1357 | NULL, |
| 1358 | NULL, |
| 1359 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1360 | rsa_alt_decrypt_wrap, |
| 1361 | NULL, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1362 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1363 | rsa_alt_check_pair, |
Manuel Pégourié-Gonnard | 7c13d69 | 2014-11-12 00:01:34 +0100 | [diff] [blame] | 1364 | #else |
| 1365 | NULL, |
| 1366 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1367 | rsa_alt_alloc_wrap, |
| 1368 | rsa_alt_free_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1369 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1370 | NULL, |
| 1371 | NULL, |
| 1372 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1373 | NULL, |
| 1374 | }; |
Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 1375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1376 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 1377 | |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1378 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1380 | static void *pk_opaque_alloc_wrap(void) |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1381 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1382 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_svc_key_id_t)); |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1383 | |
Tom Cosgrove | ce7f18c | 2022-07-28 05:50:56 +0100 | [diff] [blame] | 1384 | /* no _init() function to call, as calloc() already zeroized */ |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1386 | return ctx; |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1387 | } |
| 1388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1389 | static void pk_opaque_free_wrap(void *ctx) |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1390 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1391 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_svc_key_id_t)); |
| 1392 | mbedtls_free(ctx); |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1393 | } |
| 1394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1395 | static size_t pk_opaque_get_bitlen(const void *ctx) |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1396 | { |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1397 | const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx; |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1398 | size_t bits; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 1399 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1401 | if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) { |
| 1402 | return 0; |
| 1403 | } |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1405 | bits = psa_get_key_bits(&attributes); |
| 1406 | psa_reset_key_attributes(&attributes); |
| 1407 | return bits; |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1408 | } |
| 1409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1410 | static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type) |
Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 1411 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1412 | return type == MBEDTLS_PK_ECKEY || |
| 1413 | type == MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 1414 | } |
| 1415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1416 | static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type) |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1417 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1418 | return type == MBEDTLS_PK_RSA || |
| 1419 | type == MBEDTLS_PK_RSASSA_PSS; |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1420 | } |
| 1421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1422 | static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1423 | const unsigned char *hash, size_t hash_len, |
| 1424 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1425 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1426 | { |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1427 | #if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C) |
John Durkop | f35069a | 2020-08-17 22:05:14 -0700 | [diff] [blame] | 1428 | ((void) ctx); |
| 1429 | ((void) md_alg); |
| 1430 | ((void) hash); |
| 1431 | ((void) hash_len); |
| 1432 | ((void) sig); |
Gilles Peskine | f00f152 | 2021-06-22 00:09:00 +0200 | [diff] [blame] | 1433 | ((void) sig_size); |
John Durkop | f35069a | 2020-08-17 22:05:14 -0700 | [diff] [blame] | 1434 | ((void) sig_len); |
| 1435 | ((void) f_rng); |
| 1436 | ((void) p_rng); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1437 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1438 | #else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */ |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1439 | const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx; |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1440 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1441 | psa_algorithm_t alg; |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1442 | psa_key_type_t type; |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1443 | psa_status_t status; |
| 1444 | |
| 1445 | /* PSA has its own RNG */ |
| 1446 | (void) f_rng; |
| 1447 | (void) p_rng; |
| 1448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1449 | status = psa_get_key_attributes(*key, &attributes); |
| 1450 | if (status != PSA_SUCCESS) { |
| 1451 | return mbedtls_pk_error_from_psa(status); |
| 1452 | } |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1454 | type = psa_get_key_type(&attributes); |
| 1455 | psa_reset_key_attributes(&attributes); |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1456 | |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1457 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1458 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
| 1459 | alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg)); |
| 1460 | } else |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1461 | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1462 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1463 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 1464 | alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg)); |
| 1465 | } else |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1466 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1467 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1468 | |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1469 | /* make the signature */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1470 | status = psa_sign_hash(*key, alg, hash, hash_len, |
| 1471 | sig, sig_size, sig_len); |
| 1472 | if (status != PSA_SUCCESS) { |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1473 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1474 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
| 1475 | return mbedtls_pk_error_from_psa_ecdsa(status); |
| 1476 | } else |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1477 | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1478 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1479 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 1480 | return mbedtls_pk_error_from_psa_rsa(status); |
| 1481 | } else |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1482 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1483 | return mbedtls_pk_error_from_psa(status); |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1484 | } |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1485 | |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1486 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1487 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1488 | /* transcode it to ASN.1 sequence */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1489 | return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size); |
| 1490 | } |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1491 | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1492 | |
| 1493 | return 0; |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1494 | #endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1495 | } |
| 1496 | |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1497 | const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = { |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 1498 | MBEDTLS_PK_OPAQUE, |
| 1499 | "Opaque", |
| 1500 | pk_opaque_get_bitlen, |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1501 | pk_opaque_ecdsa_can_do, |
| 1502 | NULL, /* verify - will be done later */ |
| 1503 | pk_opaque_sign_wrap, |
| 1504 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1505 | NULL, /* restartable verify - not relevant */ |
| 1506 | NULL, /* restartable sign - not relevant */ |
| 1507 | #endif |
Neil Armstrong | 95a8923 | 2022-04-08 15:13:51 +0200 | [diff] [blame] | 1508 | NULL, /* decrypt - not relevant */ |
| 1509 | NULL, /* encrypt - not relevant */ |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1510 | NULL, /* check_pair - could be done later or left NULL */ |
| 1511 | pk_opaque_alloc_wrap, |
| 1512 | pk_opaque_free_wrap, |
| 1513 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1514 | NULL, /* restart alloc - not relevant */ |
| 1515 | NULL, /* restart free - not relevant */ |
| 1516 | #endif |
| 1517 | NULL, /* debug - could be done later, or even left NULL */ |
| 1518 | }; |
| 1519 | |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 1520 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1521 | static int pk_opaque_rsa_decrypt(void *ctx, |
| 1522 | const unsigned char *input, size_t ilen, |
| 1523 | unsigned char *output, size_t *olen, size_t osize, |
| 1524 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Neil Armstrong | 1082818 | 2022-04-22 15:02:27 +0200 | [diff] [blame] | 1525 | { |
| 1526 | const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx; |
| 1527 | psa_status_t status; |
| 1528 | |
| 1529 | /* PSA has its own RNG */ |
| 1530 | (void) f_rng; |
| 1531 | (void) p_rng; |
| 1532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1533 | status = psa_asymmetric_decrypt(*key, PSA_ALG_RSA_PKCS1V15_CRYPT, |
| 1534 | input, ilen, |
| 1535 | NULL, 0, |
| 1536 | output, osize, olen); |
| 1537 | if (status != PSA_SUCCESS) { |
| 1538 | return mbedtls_pk_error_from_psa_rsa(status); |
Neil Armstrong | 1082818 | 2022-04-22 15:02:27 +0200 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | return 0; |
| 1542 | } |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 1543 | #endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */ |
Neil Armstrong | 1082818 | 2022-04-22 15:02:27 +0200 | [diff] [blame] | 1544 | |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1545 | const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = { |
| 1546 | MBEDTLS_PK_OPAQUE, |
| 1547 | "Opaque", |
| 1548 | pk_opaque_get_bitlen, |
| 1549 | pk_opaque_rsa_can_do, |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1550 | NULL, /* verify - will be done later */ |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 1551 | pk_opaque_sign_wrap, |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1552 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1553 | NULL, /* restartable verify - not relevant */ |
| 1554 | NULL, /* restartable sign - not relevant */ |
| 1555 | #endif |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 1556 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Neil Armstrong | 1082818 | 2022-04-22 15:02:27 +0200 | [diff] [blame] | 1557 | pk_opaque_rsa_decrypt, |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 1558 | #else |
| 1559 | NULL, /* decrypt - not available */ |
| 1560 | #endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */ |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1561 | NULL, /* encrypt - will be done later */ |
| 1562 | NULL, /* check_pair - could be done later or left NULL */ |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 1563 | pk_opaque_alloc_wrap, |
| 1564 | pk_opaque_free_wrap, |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1565 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1566 | NULL, /* restart alloc - not relevant */ |
| 1567 | NULL, /* restart free - not relevant */ |
| 1568 | #endif |
| 1569 | NULL, /* debug - could be done later, or even left NULL */ |
| 1570 | }; |
| 1571 | |
| 1572 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1573 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1574 | #endif /* MBEDTLS_PK_C */ |