| 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 |  | 
| Andres Amaya Garcia | e32df08 | 2017-10-25 09:37:04 +0100 | [diff] [blame] | 44 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 45 | #include "mbedtls/platform_util.h" | 
| Andres Amaya Garcia | e32df08 | 2017-10-25 09:37:04 +0100 | [diff] [blame] | 46 | #endif | 
|  | 47 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 49 | #include "mbedtls/platform.h" | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 50 | #else | 
|  | 51 | #include <stdlib.h> | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 52 | #define mbedtls_calloc    calloc | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #define mbedtls_free       free | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 54 | #endif | 
|  | 55 |  | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 56 | #include <limits.h> | 
| Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 57 | #include <stdint.h> | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 58 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_RSA_C) | 
|  | 60 | static int rsa_can_do( mbedtls_pk_type_t type ) | 
| Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 61 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | return( type == MBEDTLS_PK_RSA || | 
|  | 63 | type == MBEDTLS_PK_RSASSA_PSS ); | 
| Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 66 | static size_t rsa_get_bitlen( const void *ctx ) | 
| Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 67 | { | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 68 | const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx; | 
|  | 69 | return( 8 * mbedtls_rsa_get_len( rsa ) ); | 
| Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | 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] | 73 | const unsigned char *hash, size_t hash_len, | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 74 | const unsigned char *sig, size_t sig_len ) | 
|  | 75 | { | 
| Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 76 | int ret; | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 77 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; | 
|  | 78 | size_t rsa_len = mbedtls_rsa_get_len( rsa ); | 
| Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 79 |  | 
| Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 80 | #if SIZE_MAX > UINT_MAX | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 81 | if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) | 
|  | 82 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 83 | #endif /* SIZE_MAX > UINT_MAX */ | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 84 |  | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 85 | if( sig_len < rsa_len ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 87 |  | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 88 | if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | MBEDTLS_RSA_PUBLIC, md_alg, | 
| Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 90 | (unsigned int) hash_len, hash, sig ) ) != 0 ) | 
|  | 91 | return( ret ); | 
|  | 92 |  | 
| Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 93 | /* The buffer contains a valid signature followed by extra data. | 
|  | 94 | * We have a special error code for that so that so that callers can | 
|  | 95 | * use mbedtls_pk_verify() to check "Does the buffer start with a | 
|  | 96 | * valid signature?" and not just "Does the buffer contain a valid | 
|  | 97 | * signature?". */ | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 98 | if( sig_len > rsa_len ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); | 
| Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 100 |  | 
|  | 101 | return( 0 ); | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | 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] | 105 | const unsigned char *hash, size_t hash_len, | 
|  | 106 | unsigned char *sig, size_t *sig_len, | 
|  | 107 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | 
|  | 108 | { | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 109 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; | 
|  | 110 |  | 
| Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 111 | #if SIZE_MAX > UINT_MAX | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 112 | if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) | 
|  | 113 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 114 | #endif /* SIZE_MAX > UINT_MAX */ | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 115 |  | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 116 | *sig_len = mbedtls_rsa_get_len( rsa ); | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 117 |  | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 118 | 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] | 119 | md_alg, (unsigned int) hash_len, hash, sig ) ); | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 122 | static int rsa_decrypt_wrap( void *ctx, | 
|  | 123 | const unsigned char *input, size_t ilen, | 
|  | 124 | unsigned char *output, size_t *olen, size_t osize, | 
|  | 125 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | 
|  | 126 | { | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 127 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; | 
|  | 128 |  | 
|  | 129 | if( ilen != mbedtls_rsa_get_len( rsa ) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 131 |  | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 132 | return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) ); | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
|  | 136 | static int rsa_encrypt_wrap( void *ctx, | 
|  | 137 | const unsigned char *input, size_t ilen, | 
|  | 138 | unsigned char *output, size_t *olen, size_t osize, | 
|  | 139 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | 
|  | 140 | { | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 141 | mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx; | 
|  | 142 | *olen = mbedtls_rsa_get_len( rsa ); | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 143 |  | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 144 | if( *olen > osize ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE ); | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 146 |  | 
| Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 147 | return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC, | 
|  | 148 | ilen, input, output ) ); | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 151 | static int rsa_check_pair_wrap( const void *pub, const void *prv ) | 
|  | 152 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub, | 
|  | 154 | (const mbedtls_rsa_context *) prv ) ); | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 157 | static void *rsa_alloc_wrap( void ) | 
|  | 158 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 159 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 160 |  | 
|  | 161 | if( ctx != NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 163 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 164 | return( ctx ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
|  | 167 | static void rsa_free_wrap( void *ctx ) | 
|  | 168 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | mbedtls_rsa_free( (mbedtls_rsa_context *) ctx ); | 
|  | 170 | mbedtls_free( ctx ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 171 | } | 
|  | 172 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | 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] | 174 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | items->type = MBEDTLS_PK_DEBUG_MPI; | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 176 | items->name = "rsa.N"; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | items->value = &( ((mbedtls_rsa_context *) ctx)->N ); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 178 |  | 
|  | 179 | items++; | 
|  | 180 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | items->type = MBEDTLS_PK_DEBUG_MPI; | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 182 | items->name = "rsa.E"; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | items->value = &( ((mbedtls_rsa_context *) ctx)->E ); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 184 | } | 
|  | 185 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | const mbedtls_pk_info_t mbedtls_rsa_info = { | 
|  | 187 | MBEDTLS_PK_RSA, | 
| Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 188 | "RSA", | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 189 | rsa_get_bitlen, | 
| Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 190 | rsa_can_do, | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 191 | rsa_verify_wrap, | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 192 | rsa_sign_wrap, | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 193 | rsa_decrypt_wrap, | 
|  | 194 | rsa_encrypt_wrap, | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 195 | rsa_check_pair_wrap, | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 196 | rsa_alloc_wrap, | 
|  | 197 | rsa_free_wrap, | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 198 | rsa_debug, | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 199 | }; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | #endif /* MBEDTLS_RSA_C */ | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 201 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 203 | /* | 
|  | 204 | * Generic EC key | 
|  | 205 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | static int eckey_can_do( mbedtls_pk_type_t type ) | 
| Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 207 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | return( type == MBEDTLS_PK_ECKEY || | 
|  | 209 | type == MBEDTLS_PK_ECKEY_DH || | 
|  | 210 | type == MBEDTLS_PK_ECDSA ); | 
| Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 213 | static size_t eckey_get_bitlen( const void *ctx ) | 
| Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 214 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits ); | 
| Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 216 | } | 
|  | 217 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | #if defined(MBEDTLS_ECDSA_C) | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 219 | /* Forward declarations */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | 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] | 221 | const unsigned char *hash, size_t hash_len, | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 222 | const unsigned char *sig, size_t sig_len ); | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 223 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | 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] | 225 | const unsigned char *hash, size_t hash_len, | 
|  | 226 | unsigned char *sig, size_t *sig_len, | 
|  | 227 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); | 
|  | 228 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | 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] | 230 | const unsigned char *hash, size_t hash_len, | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 231 | const unsigned char *sig, size_t sig_len ) | 
|  | 232 | { | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 233 | int ret; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | mbedtls_ecdsa_context ecdsa; | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 235 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | mbedtls_ecdsa_init( &ecdsa ); | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 237 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) | 
| Manuel Pégourié-Gonnard | 583b608 | 2013-08-20 16:58:13 +0200 | [diff] [blame] | 239 | 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] | 240 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | mbedtls_ecdsa_free( &ecdsa ); | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 242 |  | 
|  | 243 | return( ret ); | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 244 | } | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 245 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 246 | 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] | 247 | const unsigned char *hash, size_t hash_len, | 
|  | 248 | unsigned char *sig, size_t *sig_len, | 
|  | 249 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | 
|  | 250 | { | 
|  | 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 | 8df2769 | 2013-08-21 10:34:38 +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 | 8df2769 | 2013-08-21 10:34:38 +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 | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 257 | ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len, | 
|  | 258 | f_rng, p_rng ); | 
|  | 259 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | mbedtls_ecdsa_free( &ecdsa ); | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 261 |  | 
|  | 262 | return( ret ); | 
|  | 263 | } | 
|  | 264 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | #endif /* MBEDTLS_ECDSA_C */ | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 266 |  | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 267 | static int eckey_check_pair( const void *pub, const void *prv ) | 
|  | 268 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 269 | return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub, | 
|  | 270 | (const mbedtls_ecp_keypair *) prv ) ); | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 271 | } | 
|  | 272 |  | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 273 | static void *eckey_alloc_wrap( void ) | 
|  | 274 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 275 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 276 |  | 
|  | 277 | if( ctx != NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 278 | mbedtls_ecp_keypair_init( ctx ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 279 |  | 
|  | 280 | return( ctx ); | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | static void eckey_free_wrap( void *ctx ) | 
|  | 284 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx ); | 
|  | 286 | mbedtls_free( ctx ); | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 287 | } | 
|  | 288 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | 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] | 290 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | items->type = MBEDTLS_PK_DEBUG_ECP; | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 292 | items->name = "eckey.Q"; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q ); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 294 | } | 
|  | 295 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 296 | const mbedtls_pk_info_t mbedtls_eckey_info = { | 
|  | 297 | MBEDTLS_PK_ECKEY, | 
| Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 298 | "EC", | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 299 | eckey_get_bitlen, | 
| Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 300 | eckey_can_do, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | #if defined(MBEDTLS_ECDSA_C) | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 302 | eckey_verify_wrap, | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 303 | eckey_sign_wrap, | 
| Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 304 | #else | 
|  | 305 | NULL, | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 306 | NULL, | 
| Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 307 | #endif | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 308 | NULL, | 
|  | 309 | NULL, | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 310 | eckey_check_pair, | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 311 | eckey_alloc_wrap, | 
|  | 312 | eckey_free_wrap, | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 313 | eckey_debug, | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 314 | }; | 
| Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 315 |  | 
|  | 316 | /* | 
| Paul Bakker | 75342a6 | 2014-04-08 17:35:40 +0200 | [diff] [blame] | 317 | * EC key restricted to ECDH | 
| Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 318 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 319 | static int eckeydh_can_do( mbedtls_pk_type_t type ) | 
| Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 320 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | return( type == MBEDTLS_PK_ECKEY || | 
|  | 322 | type == MBEDTLS_PK_ECKEY_DH ); | 
| Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 325 | const mbedtls_pk_info_t mbedtls_eckeydh_info = { | 
|  | 326 | MBEDTLS_PK_ECKEY_DH, | 
| Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 327 | "EC_DH", | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 328 | eckey_get_bitlen,         /* Same underlying key structure */ | 
| Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 329 | eckeydh_can_do, | 
| Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 330 | NULL, | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 331 | NULL, | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 332 | NULL, | 
|  | 333 | NULL, | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 334 | eckey_check_pair, | 
| Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 335 | eckey_alloc_wrap,       /* Same underlying key structure */ | 
|  | 336 | eckey_free_wrap,        /* Same underlying key structure */ | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 337 | eckey_debug,            /* Same underlying key structure */ | 
| Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 338 | }; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 339 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 340 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 341 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 342 | static int ecdsa_can_do( mbedtls_pk_type_t type ) | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 343 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 344 | return( type == MBEDTLS_PK_ECDSA ); | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 345 | } | 
|  | 346 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | 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] | 348 | const unsigned char *hash, size_t hash_len, | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 349 | const unsigned char *sig, size_t sig_len ) | 
|  | 350 | { | 
| Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 351 | int ret; | 
| Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 352 | ((void) md_alg); | 
|  | 353 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 354 | ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx, | 
| Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 355 | hash, hash_len, sig, sig_len ); | 
|  | 356 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH ) | 
|  | 358 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); | 
| Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 359 |  | 
|  | 360 | return( ret ); | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 361 | } | 
|  | 362 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 363 | 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] | 364 | const unsigned char *hash, size_t hash_len, | 
|  | 365 | unsigned char *sig, size_t *sig_len, | 
|  | 366 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | 
|  | 367 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx, | 
| Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 369 | 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] | 370 | } | 
|  | 371 |  | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 372 | static void *ecdsa_alloc_wrap( void ) | 
|  | 373 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 374 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) ); | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 375 |  | 
|  | 376 | if( ctx != NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 377 | mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx ); | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 378 |  | 
|  | 379 | return( ctx ); | 
|  | 380 | } | 
|  | 381 |  | 
|  | 382 | static void ecdsa_free_wrap( void *ctx ) | 
|  | 383 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 384 | mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx ); | 
|  | 385 | mbedtls_free( ctx ); | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 386 | } | 
|  | 387 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | const mbedtls_pk_info_t mbedtls_ecdsa_info = { | 
|  | 389 | MBEDTLS_PK_ECDSA, | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 390 | "ECDSA", | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 391 | eckey_get_bitlen,     /* Compatible key structures */ | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 392 | ecdsa_can_do, | 
|  | 393 | ecdsa_verify_wrap, | 
| Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 394 | ecdsa_sign_wrap, | 
| Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 395 | NULL, | 
|  | 396 | NULL, | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 397 | eckey_check_pair,   /* Compatible key structures */ | 
| Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 398 | ecdsa_alloc_wrap, | 
|  | 399 | ecdsa_free_wrap, | 
|  | 400 | eckey_debug,        /* Compatible key structures */ | 
|  | 401 | }; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 402 | #endif /* MBEDTLS_ECDSA_C */ | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 403 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 405 | /* | 
|  | 406 | * Support for alternative RSA-private implementations | 
|  | 407 | */ | 
|  | 408 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 409 | 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] | 410 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | return( type == MBEDTLS_PK_RSA ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 412 | } | 
|  | 413 |  | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 414 | static size_t rsa_alt_get_bitlen( const void *ctx ) | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 415 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | 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] | 417 |  | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 418 | return( 8 * rsa_alt->key_len_func( rsa_alt->key ) ); | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 419 | } | 
|  | 420 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | 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] | 422 | const unsigned char *hash, size_t hash_len, | 
|  | 423 | unsigned char *sig, size_t *sig_len, | 
|  | 424 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | 
|  | 425 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | 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] | 427 |  | 
| Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 428 | #if SIZE_MAX > UINT_MAX | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 429 | if( UINT_MAX < hash_len ) | 
|  | 430 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 431 | #endif /* SIZE_MAX > UINT_MAX */ | 
| Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 432 |  | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 433 | *sig_len = rsa_alt->key_len_func( rsa_alt->key ); | 
|  | 434 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 435 | 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] | 436 | md_alg, (unsigned int) hash_len, hash, sig ) ); | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 437 | } | 
|  | 438 |  | 
|  | 439 | static int rsa_alt_decrypt_wrap( void *ctx, | 
|  | 440 | const unsigned char *input, size_t ilen, | 
|  | 441 | unsigned char *output, size_t *olen, size_t osize, | 
|  | 442 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | 
|  | 443 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 444 | 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] | 445 |  | 
|  | 446 | ((void) f_rng); | 
|  | 447 | ((void) p_rng); | 
|  | 448 |  | 
|  | 449 | if( ilen != rsa_alt->key_len_func( rsa_alt->key ) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 450 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 451 |  | 
|  | 452 | return( rsa_alt->decrypt_func( rsa_alt->key, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 453 | MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) ); | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 454 | } | 
|  | 455 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 456 | #if defined(MBEDTLS_RSA_C) | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 457 | static int rsa_alt_check_pair( const void *pub, const void *prv ) | 
|  | 458 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | unsigned char sig[MBEDTLS_MPI_MAX_SIZE]; | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 460 | unsigned char hash[32]; | 
|  | 461 | size_t sig_len = 0; | 
|  | 462 | int ret; | 
|  | 463 |  | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 464 | if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 465 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 466 |  | 
|  | 467 | memset( hash, 0x2a, sizeof( hash ) ); | 
|  | 468 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 469 | 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] | 470 | hash, sizeof( hash ), | 
|  | 471 | sig, &sig_len, NULL, NULL ) ) != 0 ) | 
|  | 472 | { | 
|  | 473 | return( ret ); | 
|  | 474 | } | 
|  | 475 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE, | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 477 | hash, sizeof( hash ), sig, sig_len ) != 0 ) | 
|  | 478 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 479 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 480 | } | 
|  | 481 |  | 
|  | 482 | return( 0 ); | 
|  | 483 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 484 | #endif /* MBEDTLS_RSA_C */ | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 485 |  | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 486 | static void *rsa_alt_alloc_wrap( void ) | 
|  | 487 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 488 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) ); | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 489 |  | 
|  | 490 | if( ctx != NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 491 | memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) ); | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 492 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 493 | return( ctx ); | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 494 | } | 
|  | 495 |  | 
|  | 496 | static void rsa_alt_free_wrap( void *ctx ) | 
|  | 497 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 498 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 499 | mbedtls_free( ctx ); | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 500 | } | 
|  | 501 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 502 | const mbedtls_pk_info_t mbedtls_rsa_alt_info = { | 
|  | 503 | MBEDTLS_PK_RSA_ALT, | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 504 | "RSA-alt", | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 505 | rsa_alt_get_bitlen, | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 506 | rsa_alt_can_do, | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 507 | NULL, | 
|  | 508 | rsa_alt_sign_wrap, | 
|  | 509 | rsa_alt_decrypt_wrap, | 
|  | 510 | NULL, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 511 | #if defined(MBEDTLS_RSA_C) | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 512 | rsa_alt_check_pair, | 
| Manuel Pégourié-Gonnard | 7c13d69 | 2014-11-12 00:01:34 +0100 | [diff] [blame] | 513 | #else | 
|  | 514 | NULL, | 
|  | 515 | #endif | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 516 | rsa_alt_alloc_wrap, | 
|  | 517 | rsa_alt_free_wrap, | 
|  | 518 | NULL, | 
|  | 519 | }; | 
| Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 520 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 521 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ | 
| Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 522 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 523 | #endif /* MBEDTLS_PK_C */ |