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 #define mbedtls_printf     printf
33 #endif
34 
35 #if defined(MBEDTLS_ECDSA_C) && \
36     defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
37 #include "mbedtls/entropy.h"
38 #include "mbedtls/ctr_drbg.h"
39 #include "mbedtls/ecdsa.h"
40 
41 #include <string.h>
42 #endif
43 
44 /*
45  * Uncomment to show key and signature details
46  */
47 #define VERBOSE
48 
49 /*
50  * Uncomment to force use of a specific curve
51  */
52 #define ECPARAMS    MBEDTLS_ECP_DP_SECP192R1
53 
54 #if !defined(ECPARAMS)
55 #define ECPARAMS    mbedtls_ecp_curve_list()->grp_id
56 #endif
57 
58 #if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
59     !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
main(void)60 int main( void )
61 {
62     mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
63            "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
64     return( 0 );
65 }
66 #else
67 #if defined(VERBOSE)
dump_buf(const char * title,unsigned char * buf,size_t len)68 static void dump_buf( const char *title, unsigned char *buf, size_t len )
69 {
70     size_t i;
71 
72     mbedtls_printf( "%s", title );
73     for( i = 0; i < len; i++ )
74         mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
75                        "0123456789ABCDEF" [buf[i] % 16] );
76     mbedtls_printf( "\n" );
77 }
78 
dump_pubkey(const char * title,mbedtls_ecdsa_context * key)79 static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
80 {
81     unsigned char buf[300];
82     size_t len;
83 
84     if( mbedtls_ecp_point_write_binary( &key->grp, &key->Q,
85                 MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
86     {
87         mbedtls_printf("internal error\n");
88         return;
89     }
90 
91     dump_buf( title, buf, len );
92 }
93 #else
94 #define dump_buf( a, b, c )
95 #define dump_pubkey( a, b )
96 #endif
97 
main(int argc,char * argv[])98 int main( int argc, char *argv[] )
99 {
100     int ret;
101     mbedtls_ecdsa_context ctx_sign, ctx_verify;
102     mbedtls_entropy_context entropy;
103     mbedtls_ctr_drbg_context ctr_drbg;
104     unsigned char hash[] = "This should be the hash of a message.";
105     unsigned char sig[512];
106     size_t sig_len;
107     const char *pers = "ecdsa";
108     ((void) argv);
109 
110     mbedtls_ecdsa_init( &ctx_sign );
111     mbedtls_ecdsa_init( &ctx_verify );
112     mbedtls_ctr_drbg_init( &ctr_drbg );
113 
114     memset(sig, 0, sizeof( sig ) );
115     ret = 1;
116 
117     if( argc != 1 )
118     {
119         mbedtls_printf( "usage: ecdsa\n" );
120 
121 #if defined(_WIN32)
122         mbedtls_printf( "\n" );
123 #endif
124 
125         goto exit;
126     }
127 
128     /*
129      * Generate a key pair for signing
130      */
131     mbedtls_printf( "\n  . Seeding the random number generator..." );
132     fflush( stdout );
133 
134     mbedtls_entropy_init( &entropy );
135     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
136                                (const unsigned char *) pers,
137                                strlen( pers ) ) ) != 0 )
138     {
139         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
140         goto exit;
141     }
142 
143     mbedtls_printf( " ok\n  . Generating key pair..." );
144     fflush( stdout );
145 
146     if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS,
147                               mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
148     {
149         mbedtls_printf( " failed\n  ! mbedtls_ecdsa_genkey returned %d\n", ret );
150         goto exit;
151     }
152 
153     mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
154 
155     dump_pubkey( "  + Public key: ", &ctx_sign );
156 
157     /*
158      * Sign some message hash
159      */
160     mbedtls_printf( "  . Signing message..." );
161     fflush( stdout );
162 
163     if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256,
164                                        hash, sizeof( hash ),
165                                        sig, &sig_len,
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     mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
172 
173     dump_buf( "  + Hash: ", hash, sizeof hash );
174     dump_buf( "  + Signature: ", sig, sig_len );
175 
176     /*
177      * Transfer public information to verifying context
178      *
179      * We could use the same context for verification and signatures, but we
180      * chose to use a new one in order to make it clear that the verifying
181      * context only needs the public key (Q), and not the private key (d).
182      */
183     mbedtls_printf( "  . Preparing verification context..." );
184     fflush( stdout );
185 
186     if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
187     {
188         mbedtls_printf( " failed\n  ! mbedtls_ecp_group_copy returned %d\n", ret );
189         goto exit;
190     }
191 
192     if( ( ret = mbedtls_ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
193     {
194         mbedtls_printf( " failed\n  ! mbedtls_ecp_copy returned %d\n", ret );
195         goto exit;
196     }
197 
198     ret = 0;
199 
200     /*
201      * Verify signature
202      */
203     mbedtls_printf( " ok\n  . Verifying signature..." );
204     fflush( stdout );
205 
206     if( ( ret = mbedtls_ecdsa_read_signature( &ctx_verify,
207                                       hash, sizeof( hash ),
208                                       sig, sig_len ) ) != 0 )
209     {
210         mbedtls_printf( " failed\n  ! mbedtls_ecdsa_read_signature returned %d\n", ret );
211         goto exit;
212     }
213 
214     mbedtls_printf( " ok\n" );
215 
216 exit:
217 
218 #if defined(_WIN32)
219     mbedtls_printf( "  + Press Enter to exit this program.\n" );
220     fflush( stdout ); getchar();
221 #endif
222 
223     mbedtls_ecdsa_free( &ctx_verify );
224     mbedtls_ecdsa_free( &ctx_sign );
225     mbedtls_ctr_drbg_free( &ctr_drbg );
226     mbedtls_entropy_free( &entropy );
227 
228     return( ret );
229 }
230 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
231           ECPARAMS */
232