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