Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curve DSA |
| 3 | * |
| 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * References: |
| 28 | * |
| 29 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
| 30 | */ |
| 31 | |
| 32 | #include "polarssl/config.h" |
| 33 | |
| 34 | #if defined(POLARSSL_ECDSA_C) |
| 35 | |
| 36 | #include "polarssl/ecdsa.h" |
| 37 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame^] | 38 | /* |
| 39 | * Compute ECDSA signature of a hashed message (SEC1 4.1.3) |
| 40 | * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message) |
| 41 | */ |
| 42 | int ecdsa_sign( const ecp_group *grp, mpi *r, mpi *s, |
| 43 | const mpi *d, const unsigned char *buf, size_t blen, |
| 44 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 45 | { |
| 46 | int ret, key_tries, sign_tries; |
| 47 | size_t n_size; |
| 48 | ecp_point R; |
| 49 | mpi k, e; |
| 50 | |
| 51 | ecp_point_init( &R ); |
| 52 | mpi_init( &k ); |
| 53 | mpi_init( &e ); |
| 54 | |
| 55 | sign_tries = 0; |
| 56 | do |
| 57 | { |
| 58 | /* |
| 59 | * Steps 1-3: generate a suitable ephemeral keypair |
| 60 | */ |
| 61 | key_tries = 0; |
| 62 | do |
| 63 | { |
| 64 | MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) ); |
| 65 | MPI_CHK( mpi_copy( r, &R.X ) ); |
| 66 | |
| 67 | if( key_tries++ > 10 ) |
| 68 | return( POLARSSL_ERR_ECP_GENERIC ); |
| 69 | } |
| 70 | while( mpi_cmp_int( r, 0 ) == 0 ); |
| 71 | |
| 72 | /* |
| 73 | * Step 5: derive MPI from hashed message |
| 74 | */ |
| 75 | n_size = (grp->nbits + 7) / 8; |
| 76 | MPI_CHK( mpi_read_binary( &e, buf, blen > n_size ? n_size : blen ) ); |
| 77 | |
| 78 | /* |
| 79 | * Step 6: compute s = (e + r * d) / k mod n |
| 80 | */ |
| 81 | MPI_CHK( mpi_mul_mpi( s, r, d ) ); |
| 82 | MPI_CHK( mpi_add_mpi( &e, &e, s ) ); |
| 83 | MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) ); |
| 84 | MPI_CHK( mpi_mul_mpi( s, s, &e ) ); |
| 85 | MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) ); |
| 86 | |
| 87 | if( sign_tries++ > 10 ) |
| 88 | return( POLARSSL_ERR_ECP_GENERIC ); |
| 89 | } |
| 90 | while( mpi_cmp_int( s, 0 ) == 0 ); |
| 91 | |
| 92 | cleanup: |
| 93 | ecp_point_free( &R ); |
| 94 | mpi_free( &k ); |
| 95 | mpi_free( &e ); |
| 96 | |
| 97 | return( ret ); |
| 98 | } |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 99 | |
| 100 | #if defined(POLARSSL_SELF_TEST) |
| 101 | |
| 102 | /* |
| 103 | * Checkup routine |
| 104 | */ |
| 105 | int ecdsa_self_test( int verbose ) |
| 106 | { |
| 107 | return( verbose++ ); |
| 108 | } |
| 109 | |
| 110 | #endif |
| 111 | |
| 112 | #endif /* defined(POLARSSL_ECDSA_C) */ |