1 /*
2  *  Example ECDSA program
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #include <stdlib.h>
33 #define mbedtls_printf          printf
34 #define mbedtls_exit            exit
35 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
36 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
37 #endif /* MBEDTLS_PLATFORM_C */
38 
39 #if defined(MBEDTLS_ECDSA_C) && \
40     defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
41 #include "mbedtls/entropy.h"
42 #include "mbedtls/ctr_drbg.h"
43 #include "mbedtls/ecdsa.h"
44 #include "mbedtls/sha256.h"
45 
46 #include <string.h>
47 #endif
48 
49 /*
50  * Uncomment to show key and signature details
51  */
52 #define VERBOSE
53 
54 /*
55  * Uncomment to force use of a specific curve
56  */
57 #define ECPARAMS    MBEDTLS_ECP_DP_SECP192R1
58 
59 #if !defined(ECPARAMS)
60 #define ECPARAMS    mbedtls_ecp_curve_list()->grp_id
61 #endif
62 
63 #if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
64     !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
main(void)65 int main( void )
66 {
67     mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
68            "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
69     return( 0 );
70 }
71 #else
72 #if defined(VERBOSE)
dump_buf(const char * title,unsigned char * buf,size_t len)73 static void dump_buf( const char *title, unsigned char *buf, size_t len )
74 {
75     size_t i;
76 
77     mbedtls_printf( "%s", title );
78     for( i = 0; i < len; i++ )
79         mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
80                        "0123456789ABCDEF" [buf[i] % 16] );
81     mbedtls_printf( "\n" );
82 }
83 
dump_pubkey(const char * title,mbedtls_ecdsa_context * key)84 static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
85 {
86     unsigned char buf[300];
87     size_t len;
88 
89     if( mbedtls_ecp_point_write_binary( &key->grp, &key->Q,
90                 MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
91     {
92         mbedtls_printf("internal error\n");
93         return;
94     }
95 
96     dump_buf( title, buf, len );
97 }
98 #else
99 #define dump_buf( a, b, c )
100 #define dump_pubkey( a, b )
101 #endif
102 
103 #if defined(MBEDTLS_CHECK_PARAMS)
104 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)105 void mbedtls_param_failed( const char *failure_condition,
106                            const char *file,
107                            int line )
108 {
109     mbedtls_printf( "%s:%i: Input param failed - %s\n",
110                     file, line, failure_condition );
111     mbedtls_exit( MBEDTLS_EXIT_FAILURE );
112 }
113 #endif
114 
main(int argc,char * argv[])115 int main( int argc, char *argv[] )
116 {
117     int ret = 1;
118     int exit_code = MBEDTLS_EXIT_FAILURE;
119     mbedtls_ecdsa_context ctx_sign, ctx_verify;
120     mbedtls_entropy_context entropy;
121     mbedtls_ctr_drbg_context ctr_drbg;
122     unsigned char message[100];
123     unsigned char hash[32];
124     unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
125     size_t sig_len;
126     const char *pers = "ecdsa";
127     ((void) argv);
128 
129     mbedtls_ecdsa_init( &ctx_sign );
130     mbedtls_ecdsa_init( &ctx_verify );
131     mbedtls_ctr_drbg_init( &ctr_drbg );
132 
133     memset( sig, 0, sizeof( sig ) );
134     memset( message, 0x25, sizeof( message ) );
135 
136     if( argc != 1 )
137     {
138         mbedtls_printf( "usage: ecdsa\n" );
139 
140 #if defined(_WIN32)
141         mbedtls_printf( "\n" );
142 #endif
143 
144         goto exit;
145     }
146 
147     /*
148      * Generate a key pair for signing
149      */
150     mbedtls_printf( "\n  . Seeding the random number generator..." );
151     fflush( stdout );
152 
153     mbedtls_entropy_init( &entropy );
154     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
155                                (const unsigned char *) pers,
156                                strlen( pers ) ) ) != 0 )
157     {
158         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
159         goto exit;
160     }
161 
162     mbedtls_printf( " ok\n  . Generating key pair..." );
163     fflush( stdout );
164 
165     if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS,
166                               mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
167     {
168         mbedtls_printf( " failed\n  ! mbedtls_ecdsa_genkey returned %d\n", ret );
169         goto exit;
170     }
171 
172     mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
173 
174     dump_pubkey( "  + Public key: ", &ctx_sign );
175 
176     /*
177      * Compute message hash
178      */
179     mbedtls_printf( "  . Computing message hash..." );
180     fflush( stdout );
181 
182     if( ( ret = mbedtls_sha256_ret( message, sizeof( message ), hash, 0 ) ) != 0 )
183     {
184         mbedtls_printf( " failed\n  ! mbedtls_sha256_ret returned %d\n", ret );
185         goto exit;
186     }
187 
188     mbedtls_printf( " ok\n" );
189 
190     dump_buf( "  + Hash: ", hash, sizeof( hash ) );
191 
192     /*
193      * Sign message hash
194      */
195     mbedtls_printf( "  . Signing message hash..." );
196     fflush( stdout );
197 
198     if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256,
199                                        hash, sizeof( hash ),
200                                        sig, &sig_len,
201                                        mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
202     {
203         mbedtls_printf( " failed\n  ! mbedtls_ecdsa_genkey returned %d\n", ret );
204         goto exit;
205     }
206     mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
207 
208     dump_buf( "  + Signature: ", sig, sig_len );
209 
210     /*
211      * Transfer public information to verifying context
212      *
213      * We could use the same context for verification and signatures, but we
214      * chose to use a new one in order to make it clear that the verifying
215      * context only needs the public key (Q), and not the private key (d).
216      */
217     mbedtls_printf( "  . Preparing verification context..." );
218     fflush( stdout );
219 
220     if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
221     {
222         mbedtls_printf( " failed\n  ! mbedtls_ecp_group_copy returned %d\n", ret );
223         goto exit;
224     }
225 
226     if( ( ret = mbedtls_ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
227     {
228         mbedtls_printf( " failed\n  ! mbedtls_ecp_copy returned %d\n", ret );
229         goto exit;
230     }
231 
232     /*
233      * Verify signature
234      */
235     mbedtls_printf( " ok\n  . Verifying signature..." );
236     fflush( stdout );
237 
238     if( ( ret = mbedtls_ecdsa_read_signature( &ctx_verify,
239                                       hash, sizeof( hash ),
240                                       sig, sig_len ) ) != 0 )
241     {
242         mbedtls_printf( " failed\n  ! mbedtls_ecdsa_read_signature returned %d\n", ret );
243         goto exit;
244     }
245 
246     mbedtls_printf( " ok\n" );
247 
248     exit_code = MBEDTLS_EXIT_SUCCESS;
249 
250 exit:
251 
252 #if defined(_WIN32)
253     mbedtls_printf( "  + Press Enter to exit this program.\n" );
254     fflush( stdout ); getchar();
255 #endif
256 
257     mbedtls_ecdsa_free( &ctx_verify );
258     mbedtls_ecdsa_free( &ctx_sign );
259     mbedtls_ctr_drbg_free( &ctr_drbg );
260     mbedtls_entropy_free( &entropy );
261 
262     return( exit_code );
263 }
264 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
265           ECPARAMS */
266