| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Public Key abstraction layer | 
|  | 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 | 12e0ed9 | 2013-07-04 13:31:32 +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 | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_PK_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/pk.h" | 
| Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 24 | #include "pk_wrap.h" | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 25 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 26 | #include "mbedtls/platform_util.h" | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 27 | #include "mbedtls/error.h" | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 28 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_RSA_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/rsa.h" | 
| Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 31 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 33 | #include "mbedtls/ecp.h" | 
| Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 34 | #endif | 
| 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 | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 37 | #endif | 
| Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 38 |  | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 39 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 40 | #include "mbedtls/psa_util.h" | 
|  | 41 | #endif | 
|  | 42 |  | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 43 | #include <limits.h> | 
| Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 44 | #include <stdint.h> | 
| Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 45 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 46 | /* Parameter validation macros based on platform_util.h */ | 
|  | 47 | #define PK_VALIDATE_RET( cond )    \ | 
|  | 48 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA ) | 
|  | 49 | #define PK_VALIDATE( cond )        \ | 
|  | 50 | MBEDTLS_INTERNAL_VALIDATE( cond ) | 
|  | 51 |  | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 52 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | * Initialise a mbedtls_pk_context | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 54 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | void mbedtls_pk_init( mbedtls_pk_context *ctx ) | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 56 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 57 | PK_VALIDATE( ctx != NULL ); | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 58 |  | 
| Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 59 | ctx->pk_info = NULL; | 
|  | 60 | ctx->pk_ctx = NULL; | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
|  | 63 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | * Free (the components of) a mbedtls_pk_context | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 65 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | void mbedtls_pk_free( mbedtls_pk_context *ctx ) | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 67 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 68 | if( ctx == NULL ) | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 69 | return; | 
|  | 70 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 71 | if ( ctx->pk_info != NULL ) | 
|  | 72 | ctx->pk_info->ctx_free_func( ctx->pk_ctx ); | 
| Manuel Pégourié-Gonnard | 1f73a65 | 2013-07-09 10:26:41 +0200 | [diff] [blame] | 73 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 74 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 77 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 78 | /* | 
|  | 79 | * Initialize a restart context | 
|  | 80 | */ | 
|  | 81 | void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx ) | 
|  | 82 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 83 | PK_VALIDATE( ctx != NULL ); | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 84 | ctx->pk_info = NULL; | 
|  | 85 | ctx->rs_ctx = NULL; | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
|  | 88 | /* | 
|  | 89 | * Free the components of a restart context | 
|  | 90 | */ | 
|  | 91 | void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx ) | 
|  | 92 | { | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 93 | if( ctx == NULL || ctx->pk_info == NULL || | 
|  | 94 | ctx->pk_info->rs_free_func == NULL ) | 
|  | 95 | { | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 96 | return; | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 97 | } | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 98 |  | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 99 | ctx->pk_info->rs_free_func( ctx->rs_ctx ); | 
|  | 100 |  | 
|  | 101 | ctx->pk_info = NULL; | 
|  | 102 | ctx->rs_ctx = NULL; | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 103 | } | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 104 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 105 |  | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 106 | /* | 
|  | 107 | * Get pk_info structure from type | 
|  | 108 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type ) | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 110 | { | 
|  | 111 | switch( pk_type ) { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | #if defined(MBEDTLS_RSA_C) | 
|  | 113 | case MBEDTLS_PK_RSA: | 
|  | 114 | return( &mbedtls_rsa_info ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 115 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | #if defined(MBEDTLS_ECP_C) | 
|  | 117 | case MBEDTLS_PK_ECKEY: | 
|  | 118 | return( &mbedtls_eckey_info ); | 
|  | 119 | case MBEDTLS_PK_ECKEY_DH: | 
|  | 120 | return( &mbedtls_eckeydh_info ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 121 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 123 | case MBEDTLS_PK_ECDSA: | 
|  | 124 | return( &mbedtls_ecdsa_info ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 125 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | /* MBEDTLS_PK_RSA_ALT omitted on purpose */ | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 127 | default: | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 128 | return( NULL ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 129 | } | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
|  | 132 | /* | 
| Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 133 | * Initialise context | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 134 | */ | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 135 | int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info ) | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 136 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 137 | PK_VALIDATE_RET( ctx != NULL ); | 
|  | 138 | if( info == NULL || ctx->pk_info != NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 140 |  | 
| Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 141 | if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 142 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 143 |  | 
| Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 144 | ctx->pk_info = info; | 
| Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 145 |  | 
|  | 146 | return( 0 ); | 
|  | 147 | } | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 148 |  | 
| Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 149 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 150 | /* | 
|  | 151 | * Initialise a PSA-wrapping context | 
|  | 152 | */ | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 153 | int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx, | 
|  | 154 | const psa_key_id_t key ) | 
| Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 155 | { | 
| Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 156 | const mbedtls_pk_info_t * const info = &mbedtls_pk_opaque_info; | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 157 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 158 | psa_key_id_t *pk_ctx; | 
| Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 159 | psa_key_type_t type; | 
| Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 160 |  | 
|  | 161 | if( ctx == NULL || ctx->pk_info != NULL ) | 
|  | 162 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 163 |  | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 164 | if( PSA_SUCCESS != psa_get_key_attributes( key, &attributes ) ) | 
| Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 165 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 166 | type = psa_get_key_type( &attributes ); | 
|  | 167 | psa_reset_key_attributes( &attributes ); | 
| Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 168 |  | 
|  | 169 | /* Current implementation of can_do() relies on this. */ | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 170 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) ) | 
| Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 171 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE) ; | 
|  | 172 |  | 
| Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 173 | if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) | 
|  | 174 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); | 
|  | 175 |  | 
| Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 176 | ctx->pk_info = info; | 
|  | 177 |  | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 178 | pk_ctx = (psa_key_id_t *) ctx->pk_ctx; | 
| Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 179 | *pk_ctx = key; | 
|  | 180 |  | 
| Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 181 | return( 0 ); | 
|  | 182 | } | 
|  | 183 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 184 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) | 
| Manuel Pégourié-Gonnard | b4fae57 | 2014-01-20 11:22:25 +0100 | [diff] [blame] | 186 | /* | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 187 | * Initialize an RSA-alt context | 
|  | 188 | */ | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 189 | int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | mbedtls_pk_rsa_alt_decrypt_func decrypt_func, | 
|  | 191 | mbedtls_pk_rsa_alt_sign_func sign_func, | 
|  | 192 | mbedtls_pk_rsa_alt_key_len_func key_len_func ) | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 193 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | mbedtls_rsa_alt_context *rsa_alt; | 
|  | 195 | const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info; | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 196 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 197 | PK_VALIDATE_RET( ctx != NULL ); | 
|  | 198 | if( ctx->pk_info != NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 200 |  | 
|  | 201 | if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 202 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 203 |  | 
|  | 204 | ctx->pk_info = info; | 
|  | 205 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx; | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 207 |  | 
|  | 208 | rsa_alt->key = key; | 
|  | 209 | rsa_alt->decrypt_func = decrypt_func; | 
|  | 210 | rsa_alt->sign_func = sign_func; | 
|  | 211 | rsa_alt->key_len_func = key_len_func; | 
|  | 212 |  | 
|  | 213 | return( 0 ); | 
|  | 214 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 216 |  | 
|  | 217 | /* | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 218 | * Tell if a PK can do the operations of the given type | 
|  | 219 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type ) | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 221 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 222 | /* A context with null pk_info is not set up yet and can't do anything. | 
|  | 223 | * For backward compatibility, also accept NULL instead of a context | 
|  | 224 | * pointer. */ | 
| Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 225 | if( ctx == NULL || ctx->pk_info == NULL ) | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 226 | return( 0 ); | 
|  | 227 |  | 
| Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 228 | return( ctx->pk_info->can_do( type ) ); | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 229 | } | 
|  | 230 |  | 
|  | 231 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | * Helper for mbedtls_pk_sign and mbedtls_pk_verify | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 233 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len ) | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 235 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | const mbedtls_md_info_t *md_info; | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 237 |  | 
|  | 238 | if( *hash_len != 0 ) | 
|  | 239 | return( 0 ); | 
|  | 240 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 242 | return( -1 ); | 
|  | 243 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | *hash_len = mbedtls_md_get_size( md_info ); | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 245 | return( 0 ); | 
|  | 246 | } | 
|  | 247 |  | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 248 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 249 | /* | 
|  | 250 | * Helper to set up a restart context if needed | 
|  | 251 | */ | 
|  | 252 | static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx, | 
| Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 253 | const mbedtls_pk_info_t *info ) | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 254 | { | 
| Manuel Pégourié-Gonnard | c8c12b6 | 2018-07-02 13:09:39 +0200 | [diff] [blame] | 255 | /* Don't do anything if already set up or invalid */ | 
|  | 256 | if( ctx == NULL || ctx->pk_info != NULL ) | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 257 | return( 0 ); | 
|  | 258 |  | 
|  | 259 | /* Should never happen when we're called */ | 
|  | 260 | if( info->rs_alloc_func == NULL || info->rs_free_func == NULL ) | 
|  | 261 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 262 |  | 
|  | 263 | if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL ) | 
|  | 264 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); | 
|  | 265 |  | 
|  | 266 | ctx->pk_info = info; | 
|  | 267 |  | 
|  | 268 | return( 0 ); | 
|  | 269 | } | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 270 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 271 |  | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 272 | /* | 
| Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 273 | * Verify a signature (restartable) | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 274 | */ | 
| Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 275 | int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx, | 
|  | 276 | mbedtls_md_type_t md_alg, | 
| Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 277 | const unsigned char *hash, size_t hash_len, | 
| Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 278 | const unsigned char *sig, size_t sig_len, | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 279 | mbedtls_pk_restart_ctx *rs_ctx ) | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 280 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 281 | PK_VALIDATE_RET( ctx != NULL ); | 
|  | 282 | PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) || | 
|  | 283 | hash != NULL ); | 
|  | 284 | PK_VALIDATE_RET( sig != NULL ); | 
|  | 285 |  | 
|  | 286 | if( ctx->pk_info == NULL || | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 287 | pk_hashlen_helper( md_alg, &hash_len ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 288 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 289 |  | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 290 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | d55f776 | 2017-08-18 17:40:15 +0200 | [diff] [blame] | 291 | /* optimization: use non-restartable version if restart disabled */ | 
|  | 292 | if( rs_ctx != NULL && | 
| Manuel Pégourié-Gonnard | b843b15 | 2018-10-16 10:41:31 +0200 | [diff] [blame] | 293 | mbedtls_ecp_restart_is_enabled() && | 
| Manuel Pégourié-Gonnard | d55f776 | 2017-08-18 17:40:15 +0200 | [diff] [blame] | 294 | ctx->pk_info->verify_rs_func != NULL ) | 
| Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 295 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 296 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 297 |  | 
|  | 298 | if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 ) | 
|  | 299 | return( ret ); | 
|  | 300 |  | 
|  | 301 | ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx, | 
|  | 302 | md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx ); | 
|  | 303 |  | 
|  | 304 | if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 305 | mbedtls_pk_restart_free( rs_ctx ); | 
|  | 306 |  | 
|  | 307 | return( ret ); | 
| Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 308 | } | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 309 | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ | 
| Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 310 | (void) rs_ctx; | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 311 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ | 
| Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 312 |  | 
| Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 313 | if( ctx->pk_info->verify_func == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 315 |  | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 316 | return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len, | 
| Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 317 | sig, sig_len ) ); | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 318 | } | 
|  | 319 |  | 
|  | 320 | /* | 
| Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 321 | * Verify a signature | 
|  | 322 | */ | 
|  | 323 | int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, | 
|  | 324 | const unsigned char *hash, size_t hash_len, | 
|  | 325 | const unsigned char *sig, size_t sig_len ) | 
|  | 326 | { | 
|  | 327 | return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len, | 
|  | 328 | sig, sig_len, NULL ) ); | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | /* | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 332 | * Verify a signature with options | 
|  | 333 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 334 | int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options, | 
|  | 335 | mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 336 | const unsigned char *hash, size_t hash_len, | 
|  | 337 | const unsigned char *sig, size_t sig_len ) | 
|  | 338 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 339 | PK_VALIDATE_RET( ctx != NULL ); | 
|  | 340 | PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) || | 
|  | 341 | hash != NULL ); | 
|  | 342 | PK_VALIDATE_RET( sig != NULL ); | 
|  | 343 |  | 
|  | 344 | if( ctx->pk_info == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 346 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | if( ! mbedtls_pk_can_do( ctx, type ) ) | 
|  | 348 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 349 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | if( type == MBEDTLS_PK_RSASSA_PSS ) | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 351 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 353 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 354 | const mbedtls_pk_rsassa_pss_options *pss_opts; | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 355 |  | 
| Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 356 | #if SIZE_MAX > UINT_MAX | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 357 | if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) | 
|  | 358 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 359 | #endif /* SIZE_MAX > UINT_MAX */ | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 360 |  | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 361 | if( options == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 363 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 364 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) options; | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 365 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 366 | if( sig_len < mbedtls_pk_get_len( ctx ) ) | 
|  | 367 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 368 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 369 | ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ), | 
| Thomas Daubney | 782a7f5 | 2021-05-19 12:27:35 +0100 | [diff] [blame] | 370 | md_alg, (unsigned int) hash_len, hash, | 
|  | 371 | pss_opts->mgf1_hash_id, | 
|  | 372 | pss_opts->expected_salt_len, | 
|  | 373 | sig ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 374 | if( ret != 0 ) | 
|  | 375 | return( ret ); | 
|  | 376 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 377 | if( sig_len > mbedtls_pk_get_len( ctx ) ) | 
|  | 378 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 379 |  | 
|  | 380 | return( 0 ); | 
|  | 381 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 382 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 383 | #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */ | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 384 | } | 
|  | 385 |  | 
|  | 386 | /* General case: no options */ | 
|  | 387 | if( options != NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 389 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 390 | return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 391 | } | 
|  | 392 |  | 
|  | 393 | /* | 
| Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 394 | * Make a signature (restartable) | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 395 | */ | 
| Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 396 | int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx, | 
|  | 397 | mbedtls_md_type_t md_alg, | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 398 | const unsigned char *hash, size_t hash_len, | 
|  | 399 | unsigned char *sig, size_t *sig_len, | 
| Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 400 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 401 | mbedtls_pk_restart_ctx *rs_ctx ) | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 402 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 403 | PK_VALIDATE_RET( ctx != NULL ); | 
|  | 404 | PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) || | 
|  | 405 | hash != NULL ); | 
|  | 406 | PK_VALIDATE_RET( sig != NULL ); | 
|  | 407 |  | 
|  | 408 | if( ctx->pk_info == NULL || | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 409 | pk_hashlen_helper( md_alg, &hash_len ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 410 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 411 |  | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 412 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | d55f776 | 2017-08-18 17:40:15 +0200 | [diff] [blame] | 413 | /* optimization: use non-restartable version if restart disabled */ | 
|  | 414 | if( rs_ctx != NULL && | 
| Manuel Pégourié-Gonnard | b843b15 | 2018-10-16 10:41:31 +0200 | [diff] [blame] | 415 | mbedtls_ecp_restart_is_enabled() && | 
| Manuel Pégourié-Gonnard | d55f776 | 2017-08-18 17:40:15 +0200 | [diff] [blame] | 416 | ctx->pk_info->sign_rs_func != NULL ) | 
| Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 417 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 418 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 419 |  | 
|  | 420 | if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 ) | 
|  | 421 | return( ret ); | 
|  | 422 |  | 
|  | 423 | ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg, | 
|  | 424 | hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx ); | 
|  | 425 |  | 
|  | 426 | if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 427 | mbedtls_pk_restart_free( rs_ctx ); | 
|  | 428 |  | 
|  | 429 | return( ret ); | 
| Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 430 | } | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 431 | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ | 
| Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 432 | (void) rs_ctx; | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 433 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ | 
| Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 434 |  | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 435 | if( ctx->pk_info->sign_func == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 436 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 437 |  | 
|  | 438 | return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len, | 
|  | 439 | sig, sig_len, f_rng, p_rng ) ); | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | /* | 
| Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 443 | * Make a signature | 
|  | 444 | */ | 
|  | 445 | int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, | 
|  | 446 | const unsigned char *hash, size_t hash_len, | 
|  | 447 | unsigned char *sig, size_t *sig_len, | 
|  | 448 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | 
|  | 449 | { | 
|  | 450 | return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len, | 
|  | 451 | sig, sig_len, f_rng, p_rng, NULL ) ); | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | /* | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 455 | * Decrypt message | 
|  | 456 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 457 | int mbedtls_pk_decrypt( mbedtls_pk_context *ctx, | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 458 | const unsigned char *input, size_t ilen, | 
|  | 459 | unsigned char *output, size_t *olen, size_t osize, | 
|  | 460 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | 
|  | 461 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 462 | PK_VALIDATE_RET( ctx != NULL ); | 
|  | 463 | PK_VALIDATE_RET( input != NULL || ilen == 0 ); | 
|  | 464 | PK_VALIDATE_RET( output != NULL || osize == 0 ); | 
|  | 465 | PK_VALIDATE_RET( olen != NULL ); | 
|  | 466 |  | 
|  | 467 | if( ctx->pk_info == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 468 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 469 |  | 
|  | 470 | if( ctx->pk_info->decrypt_func == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 471 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 472 |  | 
|  | 473 | return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen, | 
|  | 474 | output, olen, osize, f_rng, p_rng ) ); | 
|  | 475 | } | 
|  | 476 |  | 
|  | 477 | /* | 
|  | 478 | * Encrypt message | 
|  | 479 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | int mbedtls_pk_encrypt( mbedtls_pk_context *ctx, | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 481 | const unsigned char *input, size_t ilen, | 
|  | 482 | unsigned char *output, size_t *olen, size_t osize, | 
|  | 483 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | 
|  | 484 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 485 | PK_VALIDATE_RET( ctx != NULL ); | 
|  | 486 | PK_VALIDATE_RET( input != NULL || ilen == 0 ); | 
|  | 487 | PK_VALIDATE_RET( output != NULL || osize == 0 ); | 
|  | 488 | PK_VALIDATE_RET( olen != NULL ); | 
|  | 489 |  | 
|  | 490 | if( ctx->pk_info == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 491 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 492 |  | 
|  | 493 | if( ctx->pk_info->encrypt_func == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 494 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 495 |  | 
|  | 496 | return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen, | 
|  | 497 | output, olen, osize, f_rng, p_rng ) ); | 
|  | 498 | } | 
|  | 499 |  | 
|  | 500 | /* | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 501 | * Check public-private key pair | 
|  | 502 | */ | 
| Manuel Pégourié-Gonnard | 39be141 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 503 | int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, | 
|  | 504 | const mbedtls_pk_context *prv, | 
|  | 505 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 506 | void *p_rng ) | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 507 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 508 | PK_VALIDATE_RET( pub != NULL ); | 
|  | 509 | PK_VALIDATE_RET( prv != NULL ); | 
|  | 510 |  | 
|  | 511 | if( pub->pk_info == NULL || | 
| Andrzej Kurek | efed323 | 2019-02-05 05:09:05 -0500 | [diff] [blame] | 512 | prv->pk_info == NULL ) | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 513 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 514 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 515 | } | 
|  | 516 |  | 
| Manuel Pégourié-Gonnard | 39be141 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 517 | if( f_rng == NULL ) | 
|  | 518 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 519 |  | 
| Manuel Pégourié-Gonnard | eaeb7b2 | 2018-10-24 12:37:44 +0200 | [diff] [blame] | 520 | if( prv->pk_info->check_pair_func == NULL ) | 
|  | 521 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); | 
|  | 522 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 523 | if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT ) | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 524 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 525 | if( pub->pk_info->type != MBEDTLS_PK_RSA ) | 
|  | 526 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 527 | } | 
|  | 528 | else | 
|  | 529 | { | 
|  | 530 | if( pub->pk_info != prv->pk_info ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 532 | } | 
|  | 533 |  | 
| Manuel Pégourié-Gonnard | 39be141 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 534 | return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx, f_rng, p_rng ) ); | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 535 | } | 
|  | 536 |  | 
|  | 537 | /* | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 538 | * Get key size in bits | 
|  | 539 | */ | 
| Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 540 | size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx ) | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 541 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 542 | /* For backward compatibility, accept NULL or a context that | 
|  | 543 | * isn't set up yet, and return a fake value that should be safe. */ | 
| Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 544 | if( ctx == NULL || ctx->pk_info == NULL ) | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 545 | return( 0 ); | 
|  | 546 |  | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 547 | return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) ); | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 548 | } | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 549 |  | 
|  | 550 | /* | 
|  | 551 | * Export debug information | 
|  | 552 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 553 | int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items ) | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 554 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 555 | PK_VALIDATE_RET( ctx != NULL ); | 
|  | 556 | if( ctx->pk_info == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 557 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 558 |  | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 559 | if( ctx->pk_info->debug_func == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 560 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 561 |  | 
| Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 562 | ctx->pk_info->debug_func( ctx->pk_ctx, items ); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 563 | return( 0 ); | 
|  | 564 | } | 
| Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 565 |  | 
|  | 566 | /* | 
|  | 567 | * Access the PK type name | 
|  | 568 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 569 | const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx ) | 
| Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 570 | { | 
|  | 571 | if( ctx == NULL || ctx->pk_info == NULL ) | 
|  | 572 | return( "invalid PK" ); | 
|  | 573 |  | 
|  | 574 | return( ctx->pk_info->name ); | 
|  | 575 | } | 
| Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 576 |  | 
| Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 577 | /* | 
|  | 578 | * Access the PK type | 
|  | 579 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 580 | mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx ) | 
| Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 581 | { | 
|  | 582 | if( ctx == NULL || ctx->pk_info == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 583 | return( MBEDTLS_PK_NONE ); | 
| Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 584 |  | 
|  | 585 | return( ctx->pk_info->type ); | 
|  | 586 | } | 
|  | 587 |  | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 588 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 589 | /* | 
|  | 590 | * Load the key to a PSA key slot, | 
|  | 591 | * then turn the PK context into a wrapper for that key slot. | 
|  | 592 | * | 
|  | 593 | * Currently only works for EC private keys. | 
|  | 594 | */ | 
|  | 595 | int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk, | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 596 | psa_key_id_t *key, | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 597 | psa_algorithm_t hash_alg ) | 
|  | 598 | { | 
|  | 599 | #if !defined(MBEDTLS_ECP_C) | 
| John Durkop | f35069a | 2020-08-17 22:05:14 -0700 | [diff] [blame] | 600 | ((void) pk); | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 601 | ((void) key); | 
| John Durkop | f35069a | 2020-08-17 22:05:14 -0700 | [diff] [blame] | 602 | ((void) hash_alg); | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 603 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
|  | 604 | #else | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 605 | const mbedtls_ecp_keypair *ec; | 
|  | 606 | unsigned char d[MBEDTLS_ECP_MAX_BYTES]; | 
|  | 607 | size_t d_len; | 
| Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 608 | psa_ecc_family_t curve_id; | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 609 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 610 | psa_key_type_t key_type; | 
| Gilles Peskine | 4080c91 | 2019-12-18 20:43:03 +0100 | [diff] [blame] | 611 | size_t bits; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 612 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 613 |  | 
|  | 614 | /* export the private key material in the format PSA wants */ | 
|  | 615 | if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY ) | 
|  | 616 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
|  | 617 |  | 
|  | 618 | ec = mbedtls_pk_ec( *pk ); | 
|  | 619 | d_len = ( ec->grp.nbits + 7 ) / 8; | 
|  | 620 | if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 ) | 
|  | 621 | return( ret ); | 
|  | 622 |  | 
| Gilles Peskine | 4080c91 | 2019-12-18 20:43:03 +0100 | [diff] [blame] | 623 | curve_id = mbedtls_ecc_group_to_psa( ec->grp.id, &bits ); | 
|  | 624 | key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve_id ); | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 625 |  | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 626 | /* prepare the key attributes */ | 
|  | 627 | psa_set_key_type( &attributes, key_type ); | 
| Gilles Peskine | 4080c91 | 2019-12-18 20:43:03 +0100 | [diff] [blame] | 628 | psa_set_key_bits( &attributes, bits ); | 
| Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 629 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 630 | psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(hash_alg) ); | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 631 |  | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 632 | /* import private key into PSA */ | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 633 | if( PSA_SUCCESS != psa_import_key( &attributes, d, d_len, key ) ) | 
| TRodziewicz | b579ccd | 2021-04-13 14:28:28 +0200 | [diff] [blame] | 634 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 635 |  | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 636 | /* make PK context wrap the key slot */ | 
|  | 637 | mbedtls_pk_free( pk ); | 
|  | 638 | mbedtls_pk_init( pk ); | 
|  | 639 |  | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 640 | return( mbedtls_pk_setup_opaque( pk, *key ) ); | 
| Manuel Pégourié-Gonnard | 347a00e | 2018-11-19 12:25:37 +0100 | [diff] [blame] | 641 | #endif /* MBEDTLS_ECP_C */ | 
|  | 642 | } | 
|  | 643 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 644 | #endif /* MBEDTLS_PK_C */ |