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