Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Example ECDSA program |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame^] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 21 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 22 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #endif |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 27 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 28 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
Andres Amaya Garcia | 2602a1f | 2018-04-29 19:45:25 +0100 | [diff] [blame] | 30 | #include <stdlib.h> |
| 31 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 32 | #define mbedtls_exit exit |
Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 33 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | 2602a1f | 2018-04-29 19:45:25 +0100 | [diff] [blame] | 34 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 35 | #endif /* MBEDTLS_PLATFORM_C */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_ECDSA_C) && \ |
| 38 | defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 39 | #include "mbedtls/entropy.h" |
| 40 | #include "mbedtls/ctr_drbg.h" |
| 41 | #include "mbedtls/ecdsa.h" |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 42 | #include "mbedtls/sha256.h" |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 43 | |
| 44 | #include <string.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 45 | #endif |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 46 | |
| 47 | /* |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 48 | * Uncomment to show key and signature details |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 49 | */ |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 50 | #define VERBOSE |
| 51 | |
| 52 | /* |
| 53 | * Uncomment to force use of a specific curve |
| 54 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #define ECPARAMS MBEDTLS_ECP_DP_SECP192R1 |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 56 | |
| 57 | #if !defined(ECPARAMS) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | #define ECPARAMS mbedtls_ecp_curve_list()->grp_id |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 59 | #endif |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 60 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \ |
| 62 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 63 | int main( void ) |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 64 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or " |
| 66 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n"); |
Krzysztof Stachowiak | 5e1b195 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 67 | mbedtls_exit( 0 ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 68 | } |
| 69 | #else |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 70 | #if defined(VERBOSE) |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 71 | static void dump_buf( const char *title, unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 72 | { |
| 73 | size_t i; |
| 74 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | mbedtls_printf( "%s", title ); |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 76 | for( i = 0; i < len; i++ ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 77 | mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16], |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 78 | "0123456789ABCDEF" [buf[i] % 16] ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | mbedtls_printf( "\n" ); |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key ) |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 83 | { |
| 84 | unsigned char buf[300]; |
| 85 | size_t len; |
| 86 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | if( mbedtls_ecp_point_write_binary( &key->grp, &key->Q, |
| 88 | MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 ) |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 89 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | mbedtls_printf("internal error\n"); |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
| 94 | dump_buf( title, buf, len ); |
| 95 | } |
| 96 | #else |
| 97 | #define dump_buf( a, b, c ) |
| 98 | #define dump_pubkey( a, b ) |
| 99 | #endif |
| 100 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 101 | |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 102 | int main( int argc, char *argv[] ) |
| 103 | { |
Andres Amaya Garcia | 2602a1f | 2018-04-29 19:45:25 +0100 | [diff] [blame] | 104 | int ret = 1; |
| 105 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | mbedtls_ecdsa_context ctx_sign, ctx_verify; |
| 107 | mbedtls_entropy_context entropy; |
| 108 | mbedtls_ctr_drbg_context ctr_drbg; |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 109 | unsigned char message[100]; |
| 110 | unsigned char hash[32]; |
| 111 | unsigned char sig[MBEDTLS_ECDSA_MAX_LEN]; |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 112 | size_t sig_len; |
| 113 | const char *pers = "ecdsa"; |
| 114 | ((void) argv); |
| 115 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | mbedtls_ecdsa_init( &ctx_sign ); |
| 117 | mbedtls_ecdsa_init( &ctx_verify ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 118 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 119 | |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 120 | memset( sig, 0, sizeof( sig ) ); |
| 121 | memset( message, 0x25, sizeof( message ) ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 122 | |
| 123 | if( argc != 1 ) |
| 124 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | mbedtls_printf( "usage: ecdsa\n" ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 126 | |
| 127 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | mbedtls_printf( "\n" ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 129 | #endif |
| 130 | |
| 131 | goto exit; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Generate a key pair for signing |
| 136 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | mbedtls_printf( "\n . Seeding the random number generator..." ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 138 | fflush( stdout ); |
| 139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | mbedtls_entropy_init( &entropy ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 141 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 142 | (const unsigned char *) pers, |
| 143 | strlen( pers ) ) ) != 0 ) |
| 144 | { |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 145 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 146 | goto exit; |
| 147 | } |
| 148 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | mbedtls_printf( " ok\n . Generating key pair..." ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 150 | fflush( stdout ); |
| 151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS, |
| 153 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 154 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 156 | goto exit; |
| 157 | } |
| 158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 160 | |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 161 | dump_pubkey( " + Public key: ", &ctx_sign ); |
| 162 | |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 163 | /* |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 164 | * Compute message hash |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 165 | */ |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 166 | mbedtls_printf( " . Computing message hash..." ); |
| 167 | fflush( stdout ); |
| 168 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 169 | if( ( ret = mbedtls_sha256_ret( message, sizeof( message ), hash, 0 ) ) != 0 ) |
Andres Amaya Garcia | 1ff60f4 | 2017-06-28 13:26:36 +0100 | [diff] [blame] | 170 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 171 | mbedtls_printf( " failed\n ! mbedtls_sha256_ret returned %d\n", ret ); |
Andres Amaya Garcia | 1ff60f4 | 2017-06-28 13:26:36 +0100 | [diff] [blame] | 172 | goto exit; |
| 173 | } |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 174 | |
| 175 | mbedtls_printf( " ok\n" ); |
| 176 | |
| 177 | dump_buf( " + Hash: ", hash, sizeof( hash ) ); |
| 178 | |
| 179 | /* |
| 180 | * Sign message hash |
| 181 | */ |
| 182 | mbedtls_printf( " . Signing message hash..." ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 183 | fflush( stdout ); |
| 184 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256, |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 186 | hash, sizeof( hash ), |
| 187 | sig, &sig_len, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 189 | { |
Ercan Ozturk | d437309 | 2020-01-28 21:51:04 -0800 | [diff] [blame] | 190 | mbedtls_printf( " failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 191 | goto exit; |
| 192 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 194 | |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 195 | dump_buf( " + Signature: ", sig, sig_len ); |
| 196 | |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 197 | /* |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 198 | * Transfer public information to verifying context |
Manuel Pégourié-Gonnard | 4e3e7c2 | 2014-07-08 13:05:49 +0200 | [diff] [blame] | 199 | * |
| 200 | * We could use the same context for verification and signatures, but we |
| 201 | * chose to use a new one in order to make it clear that the verifying |
| 202 | * context only needs the public key (Q), and not the private key (d). |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 203 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | mbedtls_printf( " . Preparing verification context..." ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 205 | fflush( stdout ); |
| 206 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 207 | if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 ) |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 208 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | mbedtls_printf( " failed\n ! mbedtls_ecp_group_copy returned %d\n", ret ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 210 | goto exit; |
| 211 | } |
| 212 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | if( ( ret = mbedtls_ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 ) |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 214 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | mbedtls_printf( " failed\n ! mbedtls_ecp_copy returned %d\n", ret ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 216 | goto exit; |
| 217 | } |
| 218 | |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 219 | /* |
| 220 | * Verify signature |
| 221 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 222 | mbedtls_printf( " ok\n . Verifying signature..." ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 223 | fflush( stdout ); |
| 224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | if( ( ret = mbedtls_ecdsa_read_signature( &ctx_verify, |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 226 | hash, sizeof( hash ), |
| 227 | sig, sig_len ) ) != 0 ) |
| 228 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | mbedtls_printf( " failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 230 | goto exit; |
| 231 | } |
| 232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 234 | |
Andres Amaya Garcia | 2602a1f | 2018-04-29 19:45:25 +0100 | [diff] [blame] | 235 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 236 | |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 237 | exit: |
| 238 | |
| 239 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 241 | fflush( stdout ); getchar(); |
| 242 | #endif |
| 243 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | mbedtls_ecdsa_free( &ctx_verify ); |
| 245 | mbedtls_ecdsa_free( &ctx_sign ); |
| 246 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 247 | mbedtls_entropy_free( &entropy ); |
Manuel Pégourié-Gonnard | bf3109f | 2013-08-14 21:36:01 +0200 | [diff] [blame] | 248 | |
Krzysztof Stachowiak | 5e1b195 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 249 | mbedtls_exit( exit_code ); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 250 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 252 | ECPARAMS */ |