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