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