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