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