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