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