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