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