• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* BEGIN_HEADER */
2#include "mbedtls/ecdsa.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_ECDSA_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void ecdsa_prim_zero( int id )
12{
13    mbedtls_ecp_group grp;
14    mbedtls_ecp_point Q;
15    mbedtls_mpi d, r, s;
16    mbedtls_test_rnd_pseudo_info rnd_info;
17    unsigned char buf[MBEDTLS_MD_MAX_SIZE];
18
19    mbedtls_ecp_group_init( &grp );
20    mbedtls_ecp_point_init( &Q );
21    mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s );
22    memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
23    memset( buf, 0, sizeof( buf ) );
24
25    TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
26    TEST_ASSERT( mbedtls_ecp_gen_keypair( &grp, &d, &Q,
27                                          &mbedtls_test_rnd_pseudo_rand,
28                                          &rnd_info ) == 0 );
29
30    TEST_ASSERT( mbedtls_ecdsa_sign( &grp, &r, &s, &d, buf, sizeof( buf ),
31                                     &mbedtls_test_rnd_pseudo_rand,
32                                     &rnd_info ) == 0 );
33    TEST_ASSERT( mbedtls_ecdsa_verify( &grp, buf, sizeof( buf ), &Q, &r, &s ) == 0 );
34
35exit:
36    mbedtls_ecp_group_free( &grp );
37    mbedtls_ecp_point_free( &Q );
38    mbedtls_mpi_free( &d ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s );
39}
40/* END_CASE */
41
42/* BEGIN_CASE */
43void ecdsa_prim_random( int id )
44{
45    mbedtls_ecp_group grp;
46    mbedtls_ecp_point Q;
47    mbedtls_mpi d, r, s;
48    mbedtls_test_rnd_pseudo_info rnd_info;
49    unsigned char buf[MBEDTLS_MD_MAX_SIZE];
50
51    mbedtls_ecp_group_init( &grp );
52    mbedtls_ecp_point_init( &Q );
53    mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s );
54    memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
55    memset( buf, 0, sizeof( buf ) );
56
57    /* prepare material for signature */
58    TEST_ASSERT( mbedtls_test_rnd_pseudo_rand( &rnd_info,
59                                               buf, sizeof( buf ) ) == 0 );
60    TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
61    TEST_ASSERT( mbedtls_ecp_gen_keypair( &grp, &d, &Q,
62                                          &mbedtls_test_rnd_pseudo_rand,
63                                          &rnd_info ) == 0 );
64
65    TEST_ASSERT( mbedtls_ecdsa_sign( &grp, &r, &s, &d, buf, sizeof( buf ),
66                                     &mbedtls_test_rnd_pseudo_rand,
67                                     &rnd_info ) == 0 );
68    TEST_ASSERT( mbedtls_ecdsa_verify( &grp, buf, sizeof( buf ), &Q, &r, &s ) == 0 );
69
70exit:
71    mbedtls_ecp_group_free( &grp );
72    mbedtls_ecp_point_free( &Q );
73    mbedtls_mpi_free( &d ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s );
74}
75/* END_CASE */
76
77/* BEGIN_CASE */
78void ecdsa_prim_test_vectors( int id, char * d_str, char * xQ_str,
79                              char * yQ_str, data_t * rnd_buf,
80                              data_t * hash, char * r_str, char * s_str,
81                              int result )
82{
83    mbedtls_ecp_group grp;
84    mbedtls_ecp_point Q;
85    mbedtls_mpi d, r, s, r_check, s_check;
86    mbedtls_test_rnd_buf_info rnd_info;
87
88    mbedtls_ecp_group_init( &grp );
89    mbedtls_ecp_point_init( &Q );
90    mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s );
91    mbedtls_mpi_init( &r_check ); mbedtls_mpi_init( &s_check );
92
93    TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
94    TEST_ASSERT( mbedtls_ecp_point_read_string( &Q, 16, xQ_str, yQ_str ) == 0 );
95    TEST_ASSERT( mbedtls_test_read_mpi( &d, 16, d_str ) == 0 );
96    TEST_ASSERT( mbedtls_test_read_mpi( &r_check, 16, r_str ) == 0 );
97    TEST_ASSERT( mbedtls_test_read_mpi( &s_check, 16, s_str ) == 0 );
98    rnd_info.fallback_f_rng = mbedtls_test_rnd_std_rand;
99    rnd_info.fallback_p_rng = NULL;
100    rnd_info.buf = rnd_buf->x;
101    rnd_info.length = rnd_buf->len;
102
103    /* Fix rnd_buf->x by shifting it left if necessary */
104    if( grp.nbits % 8 != 0 )
105    {
106        unsigned char shift = 8 - ( grp.nbits % 8 );
107        size_t i;
108
109        for( i = 0; i < rnd_info.length - 1; i++ )
110            rnd_buf->x[i] = rnd_buf->x[i] << shift | rnd_buf->x[i+1] >> ( 8 - shift );
111
112        rnd_buf->x[rnd_info.length-1] <<= shift;
113    }
114
115    TEST_ASSERT( mbedtls_ecdsa_sign( &grp, &r, &s, &d, hash->x, hash->len,
116                 mbedtls_test_rnd_buffer_rand, &rnd_info ) == result );
117
118    if ( result == 0)
119    {
120        TEST_ASSERT( mbedtls_mpi_cmp_mpi( &r, &r_check ) == 0 );
121        TEST_ASSERT( mbedtls_mpi_cmp_mpi( &s, &s_check ) == 0 );
122
123        TEST_ASSERT( mbedtls_ecdsa_verify( &grp, hash->x, hash->len, &Q, &r_check, &s_check ) == 0 );
124
125        TEST_ASSERT( mbedtls_mpi_sub_int( &r, &r, 1 ) == 0 );
126        TEST_ASSERT( mbedtls_mpi_add_int( &s, &s, 1 ) == 0 );
127
128        TEST_ASSERT( mbedtls_ecdsa_verify( &grp, hash->x, hash->len,
129                     &Q, &r, &s_check ) == MBEDTLS_ERR_ECP_VERIFY_FAILED );
130        TEST_ASSERT( mbedtls_ecdsa_verify( &grp, hash->x, hash->len,
131                     &Q, &r_check, &s ) == MBEDTLS_ERR_ECP_VERIFY_FAILED );
132        TEST_ASSERT( mbedtls_ecdsa_verify( &grp, hash->x, hash->len,
133                     &grp.G, &r_check, &s_check ) == MBEDTLS_ERR_ECP_VERIFY_FAILED );
134    }
135
136exit:
137    mbedtls_ecp_group_free( &grp );
138    mbedtls_ecp_point_free( &Q );
139    mbedtls_mpi_free( &d ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s );
140    mbedtls_mpi_free( &r_check ); mbedtls_mpi_free( &s_check );
141}
142/* END_CASE */
143
144/* BEGIN_CASE depends_on:MBEDTLS_ECDSA_DETERMINISTIC */
145void ecdsa_det_test_vectors( int id, char * d_str, int md_alg, char * msg,
146                             char * r_str, char * s_str )
147{
148    mbedtls_ecp_group grp;
149    mbedtls_mpi d, r, s, r_check, s_check;
150    unsigned char hash[MBEDTLS_MD_MAX_SIZE];
151    size_t hlen;
152    const mbedtls_md_info_t *md_info;
153
154    mbedtls_ecp_group_init( &grp );
155    mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s );
156    mbedtls_mpi_init( &r_check ); mbedtls_mpi_init( &s_check );
157    memset( hash, 0, sizeof( hash ) );
158
159    TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
160    TEST_ASSERT( mbedtls_test_read_mpi( &d, 16, d_str ) == 0 );
161    TEST_ASSERT( mbedtls_test_read_mpi( &r_check, 16, r_str ) == 0 );
162    TEST_ASSERT( mbedtls_test_read_mpi( &s_check, 16, s_str ) == 0 );
163
164    md_info = mbedtls_md_info_from_type( md_alg );
165    TEST_ASSERT( md_info != NULL );
166    hlen = mbedtls_md_get_size( md_info );
167    TEST_ASSERT( mbedtls_md( md_info, (const unsigned char *) msg,
168                 strlen( msg ), hash ) == 0 );
169
170    TEST_ASSERT(
171                mbedtls_ecdsa_sign_det_ext( &grp, &r, &s, &d, hash, hlen,
172                                            md_alg, mbedtls_test_rnd_std_rand,
173                                            NULL )
174                == 0 );
175
176    TEST_ASSERT( mbedtls_mpi_cmp_mpi( &r, &r_check ) == 0 );
177    TEST_ASSERT( mbedtls_mpi_cmp_mpi( &s, &s_check ) == 0 );
178
179exit:
180    mbedtls_ecp_group_free( &grp );
181    mbedtls_mpi_free( &d ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s );
182    mbedtls_mpi_free( &r_check ); mbedtls_mpi_free( &s_check );
183}
184/* END_CASE */
185
186/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
187void ecdsa_write_read_zero( int id )
188{
189    mbedtls_ecdsa_context ctx;
190    mbedtls_test_rnd_pseudo_info rnd_info;
191    unsigned char hash[32];
192    unsigned char sig[200];
193    size_t sig_len, i;
194
195    mbedtls_ecdsa_init( &ctx );
196    memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
197    memset( hash, 0, sizeof( hash ) );
198    memset( sig, 0x2a, sizeof( sig ) );
199
200    /* generate signing key */
201    TEST_ASSERT( mbedtls_ecdsa_genkey( &ctx, id,
202                                       &mbedtls_test_rnd_pseudo_rand,
203                                       &rnd_info ) == 0 );
204
205    /* generate and write signature, then read and verify it */
206    TEST_ASSERT( mbedtls_ecdsa_write_signature( &ctx, MBEDTLS_MD_SHA256,
207                 hash, sizeof( hash ),
208                 sig, sizeof( sig ), &sig_len, &mbedtls_test_rnd_pseudo_rand,
209                 &rnd_info ) == 0 );
210    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
211                 sig, sig_len ) == 0 );
212
213    /* check we didn't write past the announced length */
214    for( i = sig_len; i < sizeof( sig ); i++ )
215        TEST_ASSERT( sig[i] == 0x2a );
216
217    /* try verification with invalid length */
218    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
219                 sig, sig_len - 1 ) != 0 );
220    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
221                 sig, sig_len + 1 ) != 0 );
222
223    /* try invalid sequence tag */
224    sig[0]++;
225    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
226                 sig, sig_len ) != 0 );
227    sig[0]--;
228
229    /* try modifying r */
230    sig[10]++;
231    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
232                 sig, sig_len ) == MBEDTLS_ERR_ECP_VERIFY_FAILED );
233    sig[10]--;
234
235    /* try modifying s */
236    sig[sig_len - 1]++;
237    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
238                 sig, sig_len ) == MBEDTLS_ERR_ECP_VERIFY_FAILED );
239    sig[sig_len - 1]--;
240
241exit:
242    mbedtls_ecdsa_free( &ctx );
243}
244/* END_CASE */
245
246/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
247void ecdsa_write_read_random( int id )
248{
249    mbedtls_ecdsa_context ctx;
250    mbedtls_test_rnd_pseudo_info rnd_info;
251    unsigned char hash[32];
252    unsigned char sig[200];
253    size_t sig_len, i;
254
255    mbedtls_ecdsa_init( &ctx );
256    memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
257    memset( hash, 0, sizeof( hash ) );
258    memset( sig, 0x2a, sizeof( sig ) );
259
260    /* prepare material for signature */
261    TEST_ASSERT( mbedtls_test_rnd_pseudo_rand( &rnd_info,
262                                               hash, sizeof( hash ) ) == 0 );
263
264    /* generate signing key */
265    TEST_ASSERT( mbedtls_ecdsa_genkey( &ctx, id,
266                                       &mbedtls_test_rnd_pseudo_rand,
267                                       &rnd_info ) == 0 );
268
269    /* generate and write signature, then read and verify it */
270    TEST_ASSERT( mbedtls_ecdsa_write_signature( &ctx, MBEDTLS_MD_SHA256,
271                 hash, sizeof( hash ),
272                 sig, sizeof( sig ), &sig_len, &mbedtls_test_rnd_pseudo_rand,
273                 &rnd_info ) == 0 );
274    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
275                 sig, sig_len ) == 0 );
276
277    /* check we didn't write past the announced length */
278    for( i = sig_len; i < sizeof( sig ); i++ )
279        TEST_ASSERT( sig[i] == 0x2a );
280
281    /* try verification with invalid length */
282    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
283                 sig, sig_len - 1 ) != 0 );
284    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
285                 sig, sig_len + 1 ) != 0 );
286
287    /* try invalid sequence tag */
288    sig[0]++;
289    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
290                 sig, sig_len ) != 0 );
291    sig[0]--;
292
293    /* try modifying r */
294    sig[10]++;
295    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
296                 sig, sig_len ) == MBEDTLS_ERR_ECP_VERIFY_FAILED );
297    sig[10]--;
298
299    /* try modifying s */
300    sig[sig_len - 1]++;
301    TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
302                 sig, sig_len ) == MBEDTLS_ERR_ECP_VERIFY_FAILED );
303    sig[sig_len - 1]--;
304
305exit:
306    mbedtls_ecdsa_free( &ctx );
307}
308/* END_CASE */
309
310/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
311void ecdsa_read_restart( int id, data_t *pk, data_t *hash, data_t *sig,
312                         int max_ops, int min_restart, int max_restart )
313{
314    mbedtls_ecdsa_context ctx;
315    mbedtls_ecdsa_restart_ctx rs_ctx;
316    int ret, cnt_restart;
317
318    mbedtls_ecdsa_init( &ctx );
319    mbedtls_ecdsa_restart_init( &rs_ctx );
320
321    TEST_ASSERT( mbedtls_ecp_group_load( &ctx.grp, id ) == 0 );
322    TEST_ASSERT( mbedtls_ecp_point_read_binary( &ctx.grp, &ctx.Q,
323                                                pk->x, pk->len ) == 0 );
324
325    mbedtls_ecp_set_max_ops( max_ops );
326
327    cnt_restart = 0;
328    do {
329        ret = mbedtls_ecdsa_read_signature_restartable( &ctx,
330                            hash->x, hash->len, sig->x, sig->len, &rs_ctx );
331    } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
332
333    TEST_ASSERT( ret == 0 );
334    TEST_ASSERT( cnt_restart >= min_restart );
335    TEST_ASSERT( cnt_restart <= max_restart );
336
337    /* try modifying r */
338
339    TEST_ASSERT( sig->len > 10 );
340    sig->x[10]++;
341    do {
342        ret = mbedtls_ecdsa_read_signature_restartable( &ctx,
343                            hash->x, hash->len, sig->x, sig->len, &rs_ctx );
344    } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
345    TEST_ASSERT( ret == MBEDTLS_ERR_ECP_VERIFY_FAILED );
346    sig->x[10]--;
347
348    /* try modifying s */
349    sig->x[sig->len - 1]++;
350    do {
351        ret = mbedtls_ecdsa_read_signature_restartable( &ctx,
352                            hash->x, hash->len, sig->x, sig->len, &rs_ctx );
353    } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
354    TEST_ASSERT( ret == MBEDTLS_ERR_ECP_VERIFY_FAILED );
355    sig->x[sig->len - 1]--;
356
357    /* Do we leak memory when aborting an operation?
358     * This test only makes sense when we actually restart */
359    if( min_restart > 0 )
360    {
361        ret = mbedtls_ecdsa_read_signature_restartable( &ctx,
362                            hash->x, hash->len, sig->x, sig->len, &rs_ctx );
363        TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
364    }
365
366exit:
367    mbedtls_ecdsa_free( &ctx );
368    mbedtls_ecdsa_restart_free( &rs_ctx );
369}
370/* END_CASE */
371
372/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_DETERMINISTIC */
373void ecdsa_write_restart( int id, char *d_str, int md_alg,
374                          char *msg, data_t *sig_check,
375                          int max_ops, int min_restart, int max_restart )
376{
377    int ret, cnt_restart;
378    mbedtls_ecdsa_restart_ctx rs_ctx;
379    mbedtls_ecdsa_context ctx;
380    unsigned char hash[MBEDTLS_MD_MAX_SIZE];
381    unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
382    size_t hlen, slen;
383    const mbedtls_md_info_t *md_info;
384
385    mbedtls_ecdsa_restart_init( &rs_ctx );
386    mbedtls_ecdsa_init( &ctx );
387    memset( hash, 0, sizeof( hash ) );
388    memset( sig, 0, sizeof( sig ) );
389
390    TEST_ASSERT( mbedtls_ecp_group_load( &ctx.grp, id ) == 0 );
391    TEST_ASSERT( mbedtls_test_read_mpi( &ctx.d, 16, d_str ) == 0 );
392
393    md_info = mbedtls_md_info_from_type( md_alg );
394    TEST_ASSERT( md_info != NULL );
395
396    hlen = mbedtls_md_get_size( md_info );
397    TEST_ASSERT( mbedtls_md( md_info,
398                             (const unsigned char *) msg, strlen( msg ),
399                             hash ) == 0 );
400
401    mbedtls_ecp_set_max_ops( max_ops );
402
403    slen = sizeof( sig );
404    cnt_restart = 0;
405    do {
406        ret = mbedtls_ecdsa_write_signature_restartable( &ctx,
407                md_alg, hash, hlen, sig, sizeof( sig ), &slen,
408                mbedtls_test_rnd_std_rand, NULL, &rs_ctx );
409    } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
410
411    TEST_ASSERT( ret == 0 );
412    TEST_ASSERT( slen == sig_check->len );
413    TEST_ASSERT( memcmp( sig, sig_check->x, slen ) == 0 );
414
415    TEST_ASSERT( cnt_restart >= min_restart );
416    TEST_ASSERT( cnt_restart <= max_restart );
417
418    /* Do we leak memory when aborting an operation?
419     * This test only makes sense when we actually restart */
420    if( min_restart > 0 )
421    {
422        ret = mbedtls_ecdsa_write_signature_restartable( &ctx,
423                md_alg, hash, hlen, sig, sizeof( sig ), &slen,
424                mbedtls_test_rnd_std_rand, NULL, &rs_ctx );
425        TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
426    }
427
428exit:
429    mbedtls_ecdsa_restart_free( &rs_ctx );
430    mbedtls_ecdsa_free( &ctx );
431}
432/* END_CASE */
433