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