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