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