| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Example ECDSA program | 
|  | 3 | * | 
| Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2013, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 5 | * | 
| Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 6 | *  This file is part of mbed TLS (https://polarssl.org) | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 7 | * | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 8 | *  This program is free software; you can redistribute it and/or modify | 
|  | 9 | *  it under the terms of the GNU General Public License as published by | 
|  | 10 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 11 | *  (at your option) any later version. | 
|  | 12 | * | 
|  | 13 | *  This program is distributed in the hope that it will be useful, | 
|  | 14 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 15 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 16 | *  GNU General Public License for more details. | 
|  | 17 | * | 
|  | 18 | *  You should have received a copy of the GNU General Public License along | 
|  | 19 | *  with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 20 | *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|  | 21 | */ | 
|  | 22 |  | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else | 
|  | 26 | #include POLARSSL_CONFIG_FILE | 
|  | 27 | #endif | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 28 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) | 
|  | 30 | #include "polarssl/platform.h" | 
|  | 31 | #else | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_printf     printf | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 34 | #endif | 
|  | 35 |  | 
| Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame] | 36 | #if defined(POLARSSL_ECDSA_C) && \ | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 37 | defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_CTR_DRBG_C) | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 38 | #include "polarssl/entropy.h" | 
|  | 39 | #include "polarssl/ctr_drbg.h" | 
|  | 40 | #include "polarssl/ecdsa.h" | 
|  | 41 |  | 
|  | 42 | #include <string.h> | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 43 | #endif | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 44 |  | 
|  | 45 | /* | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 46 | * Uncomment to show key and signature details | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 47 | */ | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 48 | #define VERBOSE | 
|  | 49 |  | 
|  | 50 | /* | 
|  | 51 | * Uncomment to force use of a specific curve | 
|  | 52 | */ | 
|  | 53 | #define ECPARAMS    POLARSSL_ECP_DP_SECP192R1 | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 54 |  | 
|  | 55 | #if !defined(ECPARAMS) | 
| Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 56 | #define ECPARAMS    ecp_curve_list()->grp_id | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 57 | #endif | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 58 |  | 
| Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 59 | #if !defined(POLARSSL_ECDSA_C) || \ | 
| Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 60 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 61 | int main( void ) | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 62 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 63 | polarssl_printf("POLARSSL_ECDSA_C and/or " | 
| Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 64 | "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C not defined\n"); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 65 | return( 0 ); | 
|  | 66 | } | 
|  | 67 | #else | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 68 | #if defined(VERBOSE) | 
| Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 69 | 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] | 70 | { | 
|  | 71 | size_t i; | 
|  | 72 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 73 | polarssl_printf( "%s", title ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 74 | for( i = 0; i < len; i++ ) | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 75 | polarssl_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16], | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 76 | "0123456789ABCDEF" [buf[i] % 16] ); | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 77 | polarssl_printf( "\n" ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
| Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 80 | static void dump_pubkey( const char *title, ecdsa_context *key ) | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 81 | { | 
|  | 82 | unsigned char buf[300]; | 
|  | 83 | size_t len; | 
|  | 84 |  | 
|  | 85 | if( ecp_point_write_binary( &key->grp, &key->Q, | 
|  | 86 | POLARSSL_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 ) | 
|  | 87 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 88 | polarssl_printf("internal error\n"); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 89 | return; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | dump_buf( title, buf, len ); | 
|  | 93 | } | 
|  | 94 | #else | 
|  | 95 | #define dump_buf( a, b, c ) | 
|  | 96 | #define dump_pubkey( a, b ) | 
|  | 97 | #endif | 
|  | 98 |  | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 99 | int main( int argc, char *argv[] ) | 
|  | 100 | { | 
|  | 101 | int ret; | 
|  | 102 | ecdsa_context ctx_sign, ctx_verify; | 
|  | 103 | entropy_context entropy; | 
|  | 104 | ctr_drbg_context ctr_drbg; | 
|  | 105 | unsigned char hash[] = "This should be the hash of a message."; | 
|  | 106 | unsigned char sig[512]; | 
|  | 107 | size_t sig_len; | 
|  | 108 | const char *pers = "ecdsa"; | 
|  | 109 | ((void) argv); | 
|  | 110 |  | 
|  | 111 | ecdsa_init( &ctx_sign ); | 
|  | 112 | ecdsa_init( &ctx_verify ); | 
|  | 113 |  | 
|  | 114 | memset(sig, 0, sizeof( sig ) ); | 
|  | 115 | ret = 1; | 
|  | 116 |  | 
|  | 117 | if( argc != 1 ) | 
|  | 118 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 119 | polarssl_printf( "usage: ecdsa\n" ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 120 |  | 
|  | 121 | #if defined(_WIN32) | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 122 | polarssl_printf( "\n" ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 123 | #endif | 
|  | 124 |  | 
|  | 125 | goto exit; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | /* | 
|  | 129 | * Generate a key pair for signing | 
|  | 130 | */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 131 | polarssl_printf( "\n  . Seeding the random number generator..." ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 132 | fflush( stdout ); | 
|  | 133 |  | 
|  | 134 | entropy_init( &entropy ); | 
|  | 135 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, | 
|  | 136 | (const unsigned char *) pers, | 
|  | 137 | strlen( pers ) ) ) != 0 ) | 
|  | 138 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 139 | polarssl_printf( " failed\n  ! ctr_drbg_init returned %d\n", ret ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 140 | goto exit; | 
|  | 141 | } | 
|  | 142 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 143 | polarssl_printf( " ok\n  . Generating key pair..." ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 144 | fflush( stdout ); | 
|  | 145 |  | 
|  | 146 | if( ( ret = ecdsa_genkey( &ctx_sign, ECPARAMS, | 
|  | 147 | ctr_drbg_random, &ctr_drbg ) ) != 0 ) | 
|  | 148 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 149 | polarssl_printf( " failed\n  ! ecdsa_genkey returned %d\n", ret ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 150 | goto exit; | 
|  | 151 | } | 
|  | 152 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 153 | polarssl_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] | 154 |  | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 155 | dump_pubkey( "  + Public key: ", &ctx_sign ); | 
|  | 156 |  | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 157 | /* | 
|  | 158 | * Sign some message hash | 
|  | 159 | */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 160 | polarssl_printf( "  . Signing message..." ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 161 | fflush( stdout ); | 
|  | 162 |  | 
|  | 163 | if( ( ret = ecdsa_write_signature( &ctx_sign, | 
|  | 164 | hash, sizeof( hash ), | 
|  | 165 | sig, &sig_len, | 
|  | 166 | ctr_drbg_random, &ctr_drbg ) ) != 0 ) | 
|  | 167 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 168 | polarssl_printf( " failed\n  ! ecdsa_genkey returned %d\n", ret ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 169 | goto exit; | 
|  | 170 | } | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 171 | polarssl_printf( " ok (signature length = %u)\n", (unsigned int) sig_len ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 172 |  | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 173 | dump_buf( "  + Hash: ", hash, sizeof hash ); | 
|  | 174 | dump_buf( "  + Signature: ", sig, sig_len ); | 
|  | 175 |  | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 176 | /* | 
|  | 177 | * Signature is serialized as defined by RFC 4492 p. 20, | 
|  | 178 | * but one can also access 'r' and 's' directly from the context | 
|  | 179 | */ | 
|  | 180 | #ifdef POLARSSL_FS_IO | 
|  | 181 | mpi_write_file( "    r = ", &ctx_sign.r, 16, NULL ); | 
|  | 182 | mpi_write_file( "    s = ", &ctx_sign.s, 16, NULL ); | 
|  | 183 | #endif | 
|  | 184 |  | 
|  | 185 | /* | 
|  | 186 | * Transfer public information to verifying context | 
| Manuel Pégourié-Gonnard | 4e3e7c2 | 2014-07-08 13:05:49 +0200 | [diff] [blame] | 187 | * | 
|  | 188 | * We could use the same context for verification and signatures, but we | 
|  | 189 | * chose to use a new one in order to make it clear that the verifying | 
|  | 190 | * 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] | 191 | */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 192 | polarssl_printf( "  . Preparing verification context..." ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 193 | fflush( stdout ); | 
|  | 194 |  | 
| Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 195 | if( ( ret = ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 196 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 197 | polarssl_printf( " failed\n  ! ecp_group_copy returned %d\n", ret ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 198 | goto exit; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | if( ( ret = ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 ) | 
|  | 202 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 203 | polarssl_printf( " failed\n  ! ecp_copy returned %d\n", ret ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 204 | goto exit; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | ret = 0; | 
|  | 208 |  | 
|  | 209 | /* | 
|  | 210 | * Verify signature | 
|  | 211 | */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 212 | polarssl_printf( " ok\n  . Verifying signature..." ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 213 | fflush( stdout ); | 
|  | 214 |  | 
|  | 215 | if( ( ret = ecdsa_read_signature( &ctx_verify, | 
|  | 216 | hash, sizeof( hash ), | 
|  | 217 | sig, sig_len ) ) != 0 ) | 
|  | 218 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 219 | polarssl_printf( " failed\n  ! ecdsa_read_signature returned %d\n", ret ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 220 | goto exit; | 
|  | 221 | } | 
|  | 222 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 223 | polarssl_printf( " ok\n" ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 224 |  | 
|  | 225 | exit: | 
|  | 226 |  | 
|  | 227 | #if defined(_WIN32) | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 228 | polarssl_printf( "  + Press Enter to exit this program.\n" ); | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 229 | fflush( stdout ); getchar(); | 
|  | 230 | #endif | 
|  | 231 |  | 
| Manuel Pégourié-Gonnard | bf3109f | 2013-08-14 21:36:01 +0200 | [diff] [blame] | 232 | ecdsa_free( &ctx_verify ); | 
|  | 233 | ecdsa_free( &ctx_sign ); | 
| Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 234 | ctr_drbg_free( &ctr_drbg ); | 
| Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 235 | entropy_free( &entropy ); | 
| Manuel Pégourié-Gonnard | bf3109f | 2013-08-14 21:36:01 +0200 | [diff] [blame] | 236 |  | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 237 | return( ret ); | 
|  | 238 | } | 
| Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 239 | #endif /* POLARSSL_ECDSA_C && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C && | 
| Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 240 | ECPARAMS */ |