Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key abstraction layer: wrapper functions |
| 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 | d73b3c1 | 2013-08-12 17:06:05 +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 | d73b3c1 | 2013-08-12 17:06:05 +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 | d73b3c1 | 2013-08-12 17:06:05 +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 | 50518f4 | 2015-05-26 11:04:15 +0200 | [diff] [blame] | 29 | #include "mbedtls/pk_internal.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | e511ffc | 2013-08-22 17:33:21 +0200 | [diff] [blame] | 31 | /* Even if RSA not activated, for the sake of RSA-alt */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 32 | #include "mbedtls/rsa.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 33 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/ecp.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 38 | #endif |
| 39 | |
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 | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 42 | #endif |
| 43 | |
Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 44 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 45 | #include "mbedtls/asn1write.h" |
| 46 | #endif |
| 47 | |
Andres Amaya Garcia | e32df08 | 2017-10-25 09:37:04 +0100 | [diff] [blame] | 48 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 49 | #include "mbedtls/platform_util.h" |
Andres Amaya Garcia | e32df08 | 2017-10-25 09:37:04 +0100 | [diff] [blame] | 50 | #endif |
| 51 | |
Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 52 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 53 | #include "mbedtls/psa_util.h" |
| 54 | #endif |
| 55 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 57 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 58 | #else |
| 59 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 60 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 62 | #endif |
| 63 | |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 64 | #include <limits.h> |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 65 | #include <stdint.h> |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 66 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | #if defined(MBEDTLS_RSA_C) |
| 68 | static int rsa_can_do( mbedtls_pk_type_t type ) |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 69 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | return( type == MBEDTLS_PK_RSA || |
| 71 | type == MBEDTLS_PK_RSASSA_PSS ); |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 74 | static size_t rsa_get_bitlen( const void *ctx ) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 75 | { |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 76 | const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx; |
| 77 | return( 8 * mbedtls_rsa_get_len( rsa ) ); |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 81 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 82 | const unsigned char *sig, size_t sig_len ) |
| 83 | { |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 84 | int ret; |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 85 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; |
| 86 | size_t rsa_len = mbedtls_rsa_get_len( rsa ); |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 87 | |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 88 | #if SIZE_MAX > UINT_MAX |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 89 | if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) |
| 90 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 91 | #endif /* SIZE_MAX > UINT_MAX */ |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 92 | |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 93 | if( sig_len < rsa_len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 95 | |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 96 | if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | MBEDTLS_RSA_PUBLIC, md_alg, |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 98 | (unsigned int) hash_len, hash, sig ) ) != 0 ) |
| 99 | return( ret ); |
| 100 | |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 101 | /* The buffer contains a valid signature followed by extra data. |
| 102 | * We have a special error code for that so that so that callers can |
| 103 | * use mbedtls_pk_verify() to check "Does the buffer start with a |
| 104 | * valid signature?" and not just "Does the buffer contain a valid |
| 105 | * signature?". */ |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 106 | if( sig_len > rsa_len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 108 | |
| 109 | return( 0 ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 113 | const unsigned char *hash, size_t hash_len, |
| 114 | unsigned char *sig, size_t *sig_len, |
| 115 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 116 | { |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 117 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; |
| 118 | |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 119 | #if SIZE_MAX > UINT_MAX |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 120 | if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) |
| 121 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 122 | #endif /* SIZE_MAX > UINT_MAX */ |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 123 | |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 124 | *sig_len = mbedtls_rsa_get_len( rsa ); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 125 | |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 126 | return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 127 | md_alg, (unsigned int) hash_len, hash, sig ) ); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 130 | static int rsa_decrypt_wrap( void *ctx, |
| 131 | const unsigned char *input, size_t ilen, |
| 132 | unsigned char *output, size_t *olen, size_t osize, |
| 133 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 134 | { |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 135 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; |
| 136 | |
| 137 | if( ilen != mbedtls_rsa_get_len( rsa ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 139 | |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 140 | return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) ); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int rsa_encrypt_wrap( void *ctx, |
| 145 | const unsigned char *input, size_t ilen, |
| 146 | unsigned char *output, size_t *olen, size_t osize, |
| 147 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 148 | { |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 149 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; |
| 150 | *olen = mbedtls_rsa_get_len( rsa ); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 151 | |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 152 | if( *olen > osize ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE ); |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 154 | |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 155 | return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC, |
| 156 | ilen, input, output ) ); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 159 | static int rsa_check_pair_wrap( const void *pub, const void *prv ) |
| 160 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub, |
| 162 | (const mbedtls_rsa_context *) prv ) ); |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 165 | static void *rsa_alloc_wrap( void ) |
| 166 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 167 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 168 | |
| 169 | if( ctx != NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 171 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 172 | return( ctx ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static void rsa_free_wrap( void *ctx ) |
| 176 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | mbedtls_rsa_free( (mbedtls_rsa_context *) ctx ); |
| 178 | mbedtls_free( ctx ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items ) |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 182 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | items->type = MBEDTLS_PK_DEBUG_MPI; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 184 | items->name = "rsa.N"; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | items->value = &( ((mbedtls_rsa_context *) ctx)->N ); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 186 | |
| 187 | items++; |
| 188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | items->type = MBEDTLS_PK_DEBUG_MPI; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 190 | items->name = "rsa.E"; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | items->value = &( ((mbedtls_rsa_context *) ctx)->E ); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | const mbedtls_pk_info_t mbedtls_rsa_info = { |
| 195 | MBEDTLS_PK_RSA, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 196 | "RSA", |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 197 | rsa_get_bitlen, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 198 | rsa_can_do, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 199 | rsa_verify_wrap, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 200 | rsa_sign_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 201 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 202 | NULL, |
| 203 | NULL, |
| 204 | #endif |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 205 | rsa_decrypt_wrap, |
| 206 | rsa_encrypt_wrap, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 207 | rsa_check_pair_wrap, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 208 | rsa_alloc_wrap, |
| 209 | rsa_free_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 210 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 211 | NULL, |
| 212 | NULL, |
| 213 | #endif |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 214 | rsa_debug, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 215 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 217 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 219 | /* |
| 220 | * Generic EC key |
| 221 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 222 | static int eckey_can_do( mbedtls_pk_type_t type ) |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 223 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | return( type == MBEDTLS_PK_ECKEY || |
| 225 | type == MBEDTLS_PK_ECKEY_DH || |
| 226 | type == MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 229 | static size_t eckey_get_bitlen( const void *ctx ) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 230 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits ); |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 235 | /* Forward declarations */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 237 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 238 | const unsigned char *sig, size_t sig_len ); |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 239 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 241 | const unsigned char *hash, size_t hash_len, |
| 242 | unsigned char *sig, size_t *sig_len, |
| 243 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 244 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 246 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 247 | const unsigned char *sig, size_t sig_len ) |
| 248 | { |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 249 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | mbedtls_ecdsa_context ecdsa; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 251 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 252 | mbedtls_ecdsa_init( &ecdsa ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
Manuel Pégourié-Gonnard | 583b608 | 2013-08-20 16:58:13 +0200 | [diff] [blame] | 255 | ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | mbedtls_ecdsa_free( &ecdsa ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 258 | |
| 259 | return( ret ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 260 | } |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 261 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 263 | const unsigned char *hash, size_t hash_len, |
| 264 | unsigned char *sig, size_t *sig_len, |
| 265 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 266 | { |
| 267 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | mbedtls_ecdsa_context ecdsa; |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 269 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | mbedtls_ecdsa_init( &ecdsa ); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 271 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 273 | ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len, |
| 274 | f_rng, p_rng ); |
| 275 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | mbedtls_ecdsa_free( &ecdsa ); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 277 | |
| 278 | return( ret ); |
| 279 | } |
| 280 | |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 281 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 282 | /* Forward declarations */ |
| 283 | static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 284 | const unsigned char *hash, size_t hash_len, |
| 285 | const unsigned char *sig, size_t sig_len, |
| 286 | void *rs_ctx ); |
| 287 | |
| 288 | static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 289 | const unsigned char *hash, size_t hash_len, |
| 290 | unsigned char *sig, size_t *sig_len, |
| 291 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 292 | void *rs_ctx ); |
| 293 | |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 294 | /* |
| 295 | * Restart context for ECDSA operations with ECKEY context |
| 296 | * |
| 297 | * We need to store an actual ECDSA context, as we need to pass the same to |
| 298 | * the underlying ecdsa function, so we can't create it on the fly every time. |
| 299 | */ |
| 300 | typedef struct |
| 301 | { |
| 302 | mbedtls_ecdsa_restart_ctx ecdsa_rs; |
| 303 | mbedtls_ecdsa_context ecdsa_ctx; |
| 304 | } eckey_restart_ctx; |
| 305 | |
| 306 | static void *eckey_rs_alloc( void ) |
| 307 | { |
| 308 | eckey_restart_ctx *rs_ctx; |
| 309 | |
| 310 | void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) ); |
| 311 | |
| 312 | if( ctx != NULL ) |
| 313 | { |
| 314 | rs_ctx = ctx; |
| 315 | mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs ); |
| 316 | mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx ); |
| 317 | } |
| 318 | |
| 319 | return( ctx ); |
| 320 | } |
| 321 | |
| 322 | static void eckey_rs_free( void *ctx ) |
| 323 | { |
| 324 | eckey_restart_ctx *rs_ctx; |
| 325 | |
| 326 | if( ctx == NULL) |
| 327 | return; |
| 328 | |
| 329 | rs_ctx = ctx; |
| 330 | mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs ); |
| 331 | mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx ); |
| 332 | |
| 333 | mbedtls_free( ctx ); |
| 334 | } |
| 335 | |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 336 | static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 337 | const unsigned char *hash, size_t hash_len, |
| 338 | const unsigned char *sig, size_t sig_len, |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 339 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 340 | { |
| 341 | int ret; |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 342 | eckey_restart_ctx *rs = rs_ctx; |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 343 | |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 344 | /* Should never happen */ |
| 345 | if( rs == NULL ) |
| 346 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 347 | |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 348 | /* set up our own sub-context if needed (that is, on first run) */ |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 349 | if( rs->ecdsa_ctx.grp.pbits == 0 ) |
| 350 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) ); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 351 | |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 352 | MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx, |
| 353 | md_alg, hash, hash_len, |
| 354 | sig, sig_len, &rs->ecdsa_rs ) ); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 355 | |
| 356 | cleanup: |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 357 | return( ret ); |
| 358 | } |
| 359 | |
| 360 | static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 361 | const unsigned char *hash, size_t hash_len, |
| 362 | unsigned char *sig, size_t *sig_len, |
| 363 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 364 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 365 | { |
| 366 | int ret; |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 367 | eckey_restart_ctx *rs = rs_ctx; |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 368 | |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 369 | /* Should never happen */ |
| 370 | if( rs == NULL ) |
| 371 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 372 | |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 373 | /* set up our own sub-context if needed (that is, on first run) */ |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 374 | if( rs->ecdsa_ctx.grp.pbits == 0 ) |
| 375 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) ); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 376 | |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 377 | MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg, |
| 378 | hash, hash_len, sig, sig_len, |
| 379 | f_rng, p_rng, &rs->ecdsa_rs ) ); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 380 | |
| 381 | cleanup: |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 382 | return( ret ); |
| 383 | } |
| 384 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 385 | #endif /* MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 386 | |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 387 | static int eckey_check_pair( const void *pub, const void *prv ) |
| 388 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 389 | return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub, |
| 390 | (const mbedtls_ecp_keypair *) prv ) ); |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 393 | static void *eckey_alloc_wrap( void ) |
| 394 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 395 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 396 | |
| 397 | if( ctx != NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 398 | mbedtls_ecp_keypair_init( ctx ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 399 | |
| 400 | return( ctx ); |
| 401 | } |
| 402 | |
| 403 | static void eckey_free_wrap( void *ctx ) |
| 404 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 405 | mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx ); |
| 406 | mbedtls_free( ctx ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 409 | static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items ) |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 410 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | items->type = MBEDTLS_PK_DEBUG_ECP; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 412 | items->name = "eckey.Q"; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 413 | items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q ); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | const mbedtls_pk_info_t mbedtls_eckey_info = { |
| 417 | MBEDTLS_PK_ECKEY, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 418 | "EC", |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 419 | eckey_get_bitlen, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 420 | eckey_can_do, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 422 | eckey_verify_wrap, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 423 | eckey_sign_wrap, |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 424 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 425 | eckey_verify_rs_wrap, |
| 426 | eckey_sign_rs_wrap, |
| 427 | #endif |
| 428 | #else /* MBEDTLS_ECDSA_C */ |
| 429 | NULL, |
| 430 | NULL, |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 431 | #endif /* MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 432 | NULL, |
| 433 | NULL, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 434 | eckey_check_pair, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 435 | eckey_alloc_wrap, |
| 436 | eckey_free_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 437 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 438 | eckey_rs_alloc, |
| 439 | eckey_rs_free, |
| 440 | #endif |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 441 | eckey_debug, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 442 | }; |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 443 | |
| 444 | /* |
Paul Bakker | 75342a6 | 2014-04-08 17:35:40 +0200 | [diff] [blame] | 445 | * EC key restricted to ECDH |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 446 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 447 | static int eckeydh_can_do( mbedtls_pk_type_t type ) |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 448 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 449 | return( type == MBEDTLS_PK_ECKEY || |
| 450 | type == MBEDTLS_PK_ECKEY_DH ); |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 453 | const mbedtls_pk_info_t mbedtls_eckeydh_info = { |
| 454 | MBEDTLS_PK_ECKEY_DH, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 455 | "EC_DH", |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 456 | eckey_get_bitlen, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 457 | eckeydh_can_do, |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 458 | NULL, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 459 | NULL, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 460 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 461 | NULL, |
| 462 | NULL, |
| 463 | #endif |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 464 | NULL, |
| 465 | NULL, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 466 | eckey_check_pair, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 467 | eckey_alloc_wrap, /* Same underlying key structure */ |
| 468 | eckey_free_wrap, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 469 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 470 | NULL, |
| 471 | NULL, |
| 472 | #endif |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 473 | eckey_debug, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 474 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 475 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 476 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | #if defined(MBEDTLS_ECDSA_C) |
| 478 | static int ecdsa_can_do( mbedtls_pk_type_t type ) |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 479 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | return( type == MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 483 | static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 484 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 485 | const unsigned char *sig, size_t sig_len ) |
| 486 | { |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 487 | int ret; |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 488 | ((void) md_alg); |
| 489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 490 | ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx, |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 491 | hash, hash_len, sig, sig_len ); |
| 492 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 493 | if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH ) |
| 494 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 495 | |
| 496 | return( ret ); |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 499 | static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 500 | const unsigned char *hash, size_t hash_len, |
| 501 | unsigned char *sig, size_t *sig_len, |
| 502 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 503 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 504 | return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx, |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 505 | md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 506 | } |
| 507 | |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 508 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 509 | static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 510 | const unsigned char *hash, size_t hash_len, |
| 511 | const unsigned char *sig, size_t sig_len, |
| 512 | void *rs_ctx ) |
| 513 | { |
| 514 | int ret; |
| 515 | ((void) md_alg); |
| 516 | |
| 517 | ret = mbedtls_ecdsa_read_signature_restartable( |
| 518 | (mbedtls_ecdsa_context *) ctx, |
| 519 | hash, hash_len, sig, sig_len, |
| 520 | (mbedtls_ecdsa_restart_ctx *) rs_ctx ); |
| 521 | |
| 522 | if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH ) |
| 523 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); |
| 524 | |
| 525 | return( ret ); |
| 526 | } |
| 527 | |
| 528 | static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg, |
| 529 | const unsigned char *hash, size_t hash_len, |
| 530 | unsigned char *sig, size_t *sig_len, |
| 531 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 532 | void *rs_ctx ) |
| 533 | { |
| 534 | return( mbedtls_ecdsa_write_signature_restartable( |
| 535 | (mbedtls_ecdsa_context *) ctx, |
| 536 | md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng, |
| 537 | (mbedtls_ecdsa_restart_ctx *) rs_ctx ) ); |
| 538 | |
| 539 | } |
| 540 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 541 | |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 542 | static void *ecdsa_alloc_wrap( void ) |
| 543 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 544 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) ); |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 545 | |
| 546 | if( ctx != NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 547 | mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx ); |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 548 | |
| 549 | return( ctx ); |
| 550 | } |
| 551 | |
| 552 | static void ecdsa_free_wrap( void *ctx ) |
| 553 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 554 | mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx ); |
| 555 | mbedtls_free( ctx ); |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 556 | } |
| 557 | |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 558 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 559 | static void *ecdsa_rs_alloc( void ) |
| 560 | { |
| 561 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) ); |
| 562 | |
| 563 | if( ctx != NULL ) |
| 564 | mbedtls_ecdsa_restart_init( ctx ); |
| 565 | |
| 566 | return( ctx ); |
| 567 | } |
| 568 | |
| 569 | static void ecdsa_rs_free( void *ctx ) |
| 570 | { |
| 571 | mbedtls_ecdsa_restart_free( ctx ); |
| 572 | mbedtls_free( ctx ); |
| 573 | } |
| 574 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 575 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 576 | const mbedtls_pk_info_t mbedtls_ecdsa_info = { |
| 577 | MBEDTLS_PK_ECDSA, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 578 | "ECDSA", |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 579 | eckey_get_bitlen, /* Compatible key structures */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 580 | ecdsa_can_do, |
| 581 | ecdsa_verify_wrap, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 582 | ecdsa_sign_wrap, |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 583 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 584 | ecdsa_verify_rs_wrap, |
| 585 | ecdsa_sign_rs_wrap, |
| 586 | #endif |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 587 | NULL, |
| 588 | NULL, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 589 | eckey_check_pair, /* Compatible key structures */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 590 | ecdsa_alloc_wrap, |
| 591 | ecdsa_free_wrap, |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 592 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 593 | ecdsa_rs_alloc, |
| 594 | ecdsa_rs_free, |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 595 | #endif |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 596 | eckey_debug, /* Compatible key structures */ |
| 597 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 598 | #endif /* MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 599 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 601 | /* |
| 602 | * Support for alternative RSA-private implementations |
| 603 | */ |
| 604 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 605 | static int rsa_alt_can_do( mbedtls_pk_type_t type ) |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 606 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 607 | return( type == MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 608 | } |
| 609 | |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 610 | static size_t rsa_alt_get_bitlen( const void *ctx ) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 611 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 612 | const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 613 | |
Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 614 | return( 8 * rsa_alt->key_len_func( rsa_alt->key ) ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 618 | const unsigned char *hash, size_t hash_len, |
| 619 | unsigned char *sig, size_t *sig_len, |
| 620 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 621 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 622 | mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 623 | |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 624 | #if SIZE_MAX > UINT_MAX |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 625 | if( UINT_MAX < hash_len ) |
| 626 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 627 | #endif /* SIZE_MAX > UINT_MAX */ |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 628 | |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 629 | *sig_len = rsa_alt->key_len_func( rsa_alt->key ); |
| 630 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 631 | return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 632 | md_alg, (unsigned int) hash_len, hash, sig ) ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static int rsa_alt_decrypt_wrap( void *ctx, |
| 636 | const unsigned char *input, size_t ilen, |
| 637 | unsigned char *output, size_t *olen, size_t osize, |
| 638 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 639 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 640 | mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 641 | |
| 642 | ((void) f_rng); |
| 643 | ((void) p_rng); |
| 644 | |
| 645 | if( ilen != rsa_alt->key_len_func( rsa_alt->key ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 646 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 647 | |
| 648 | return( rsa_alt->decrypt_func( rsa_alt->key, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 652 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 653 | static int rsa_alt_check_pair( const void *pub, const void *prv ) |
| 654 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 655 | unsigned char sig[MBEDTLS_MPI_MAX_SIZE]; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 656 | unsigned char hash[32]; |
| 657 | size_t sig_len = 0; |
| 658 | int ret; |
| 659 | |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 660 | if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 661 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 662 | |
| 663 | memset( hash, 0x2a, sizeof( hash ) ); |
| 664 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE, |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 666 | hash, sizeof( hash ), |
| 667 | sig, &sig_len, NULL, NULL ) ) != 0 ) |
| 668 | { |
| 669 | return( ret ); |
| 670 | } |
| 671 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 672 | if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE, |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 673 | hash, sizeof( hash ), sig, sig_len ) != 0 ) |
| 674 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | return( 0 ); |
| 679 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 681 | |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 682 | static void *rsa_alt_alloc_wrap( void ) |
| 683 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 684 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 685 | |
| 686 | if( ctx != NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 687 | memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 688 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 689 | return( ctx ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | static void rsa_alt_free_wrap( void *ctx ) |
| 693 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 694 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 695 | mbedtls_free( ctx ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 696 | } |
| 697 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 698 | const mbedtls_pk_info_t mbedtls_rsa_alt_info = { |
| 699 | MBEDTLS_PK_RSA_ALT, |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 700 | "RSA-alt", |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 701 | rsa_alt_get_bitlen, |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 702 | rsa_alt_can_do, |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 703 | NULL, |
| 704 | rsa_alt_sign_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 705 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 706 | NULL, |
| 707 | NULL, |
| 708 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 709 | rsa_alt_decrypt_wrap, |
| 710 | NULL, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 711 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 712 | rsa_alt_check_pair, |
Manuel Pégourié-Gonnard | 7c13d69 | 2014-11-12 00:01:34 +0100 | [diff] [blame] | 713 | #else |
| 714 | NULL, |
| 715 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 716 | rsa_alt_alloc_wrap, |
| 717 | rsa_alt_free_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 718 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 719 | NULL, |
| 720 | NULL, |
| 721 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 722 | NULL, |
| 723 | }; |
Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 724 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 725 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 726 | |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 727 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 728 | |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame^] | 729 | static void *pk_opaque_alloc_wrap( void ) |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 730 | { |
| 731 | void *ctx = mbedtls_calloc( 1, sizeof( psa_key_slot_t ) ); |
| 732 | |
| 733 | /* no _init() function to call, an calloc() already zeroized */ |
| 734 | |
| 735 | return( ctx ); |
| 736 | } |
| 737 | |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame^] | 738 | static void pk_opaque_free_wrap( void *ctx ) |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 739 | { |
| 740 | mbedtls_platform_zeroize( ctx, sizeof( psa_key_slot_t ) ); |
| 741 | mbedtls_free( ctx ); |
| 742 | } |
| 743 | |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame^] | 744 | static size_t pk_opaque_get_bitlen( const void *ctx ) |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 745 | { |
| 746 | const psa_key_slot_t *key = (const psa_key_slot_t *) ctx; |
| 747 | size_t bits; |
| 748 | |
| 749 | if( PSA_SUCCESS != psa_get_key_information( *key, NULL, &bits ) ) |
| 750 | return( 0 ); |
| 751 | |
| 752 | return( bits ); |
| 753 | } |
| 754 | |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame^] | 755 | static int pk_opaque_can_do( mbedtls_pk_type_t type ) |
Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 756 | { |
| 757 | /* For now opaque PSA keys can only wrap ECC keypairs, |
| 758 | * as checked by setup_psa(). |
| 759 | * Also, ECKEY_DH does not really make sense with the current API. */ |
| 760 | return( type == MBEDTLS_PK_ECKEY || |
| 761 | type == MBEDTLS_PK_ECDSA ); |
| 762 | } |
| 763 | |
Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 764 | /* Like mbedtls_asn1_write_mpi, but from a buffer */ |
| 765 | static int asn1_write_mpibuf( unsigned char **p, unsigned char *start, |
| 766 | const unsigned char *src, size_t slen ) |
| 767 | { |
| 768 | int ret; |
| 769 | size_t len = 0; |
| 770 | |
| 771 | if( (size_t)( *p - start ) < slen ) |
| 772 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 773 | |
| 774 | len = slen; |
| 775 | *p -= len; |
| 776 | memcpy( *p, src, len ); |
| 777 | |
| 778 | if( **p & 0x80 ) |
| 779 | { |
| 780 | if( *p - start < 1 ) |
| 781 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 782 | |
| 783 | *--(*p) = 0x00; |
| 784 | len += 1; |
| 785 | } |
| 786 | |
| 787 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 788 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) ); |
| 789 | |
| 790 | return( (int) len ); |
| 791 | } |
| 792 | |
| 793 | /* Transcode signature from PSA format to ASN.1 sequence. |
| 794 | * See ecdsa_signature_to_asn1 in ecdsa.c. |
| 795 | * |
| 796 | * [in] sig: the signature in PSA format |
| 797 | * [in/out] sig_len: signature length pre- and post-transcoding |
| 798 | * [out] dst: the signature in ASN.1 format |
| 799 | */ |
| 800 | static int pk_ecdsa_sig_asn1_from_psa( const unsigned char *sig, size_t *sig_len, |
| 801 | unsigned char *dst ) |
| 802 | { |
| 803 | int ret; |
| 804 | unsigned char buf[MBEDTLS_ECDSA_MAX_LEN]; |
| 805 | unsigned char *p = buf + sizeof( buf ); |
| 806 | size_t len = 0; |
| 807 | const size_t mpi_len = *sig_len / 2; |
| 808 | |
| 809 | MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, buf, sig + mpi_len, mpi_len ) ); |
| 810 | MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, buf, sig, mpi_len ) ); |
| 811 | |
| 812 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) ); |
| 813 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf, |
| 814 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); |
| 815 | |
| 816 | memcpy( dst, p, len ); |
| 817 | *sig_len = len; |
| 818 | |
| 819 | return( 0 ); |
| 820 | } |
| 821 | |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame^] | 822 | static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 823 | const unsigned char *hash, size_t hash_len, |
| 824 | unsigned char *sig, size_t *sig_len, |
| 825 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 826 | { |
| 827 | const psa_key_slot_t *key = (const psa_key_slot_t *) ctx; |
| 828 | psa_status_t status; |
| 829 | psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) ); |
| 830 | /* PSA needs a buffer of know size */ |
| 831 | unsigned char buf[2 * MBEDTLS_ECP_MAX_BYTES]; |
| 832 | const size_t buf_len = sizeof( buf ); |
| 833 | |
| 834 | /* PSA has its own RNG */ |
| 835 | (void) f_rng; |
| 836 | (void) p_rng; |
| 837 | |
| 838 | status = psa_asymmetric_sign( *key, alg, hash, hash_len, |
| 839 | buf, buf_len, sig_len ); |
| 840 | |
| 841 | /* translate errors to best approximation */ |
| 842 | switch( status ) |
| 843 | { |
| 844 | case PSA_SUCCESS: |
| 845 | break; /* don't return now */ |
| 846 | case PSA_ERROR_NOT_SUPPORTED: |
| 847 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); |
| 848 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 849 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); |
| 850 | case PSA_ERROR_COMMUNICATION_FAILURE: |
| 851 | case PSA_ERROR_HARDWARE_FAILURE: |
| 852 | case PSA_ERROR_TAMPERING_DETECTED: |
| 853 | return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED ); |
| 854 | case PSA_ERROR_INSUFFICIENT_ENTROPY: |
| 855 | return( MBEDTLS_ERR_ECP_RANDOM_FAILED ); |
| 856 | case PSA_ERROR_BAD_STATE: |
| 857 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
| 858 | default: /* should never happen */ |
| 859 | return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED ); |
| 860 | } |
| 861 | |
| 862 | pk_ecdsa_sig_asn1_from_psa( buf, sig_len, sig ); |
| 863 | |
| 864 | return( 0 ); |
| 865 | } |
| 866 | |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame^] | 867 | const mbedtls_pk_info_t mbedtls_pk_opaque_info = { |
| 868 | MBEDTLS_PK_OPAQUE, |
| 869 | "Opaque", |
| 870 | pk_opaque_get_bitlen, |
| 871 | pk_opaque_can_do, |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 872 | NULL, /* verify - will be done later */ |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame^] | 873 | pk_opaque_sign_wrap, |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 874 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 875 | NULL, /* restartable verify - not relevant */ |
| 876 | NULL, /* restartable sign - not relevant */ |
| 877 | #endif |
| 878 | NULL, /* decrypt - will be done later */ |
| 879 | NULL, /* encrypt - will be done later */ |
| 880 | NULL, /* check_pair - could be done later or left NULL */ |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame^] | 881 | pk_opaque_alloc_wrap, |
| 882 | pk_opaque_free_wrap, |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 883 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 884 | NULL, /* restart alloc - not relevant */ |
| 885 | NULL, /* restart free - not relevant */ |
| 886 | #endif |
| 887 | NULL, /* debug - could be done later, or even left NULL */ |
| 888 | }; |
| 889 | |
| 890 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 891 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 892 | #endif /* MBEDTLS_PK_C */ |