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 | /* |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 39 | * Derive a suitable integer for group grp from a buffer of length len |
| 40 | * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3 |
| 41 | */ |
| 42 | static int derive_mpi( const ecp_group *grp, mpi *x, |
| 43 | const unsigned char *buf, size_t blen ) |
| 44 | { |
| 45 | size_t n_size = (grp->nbits + 7) / 8; |
| 46 | return( mpi_read_binary( x, buf, blen > n_size ? n_size : blen ) ); |
| 47 | } |
| 48 | |
| 49 | /* |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 50 | * Compute ECDSA signature of a hashed message (SEC1 4.1.3) |
| 51 | * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message) |
| 52 | */ |
| 53 | int ecdsa_sign( const ecp_group *grp, mpi *r, mpi *s, |
| 54 | const mpi *d, const unsigned char *buf, size_t blen, |
| 55 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 56 | { |
| 57 | int ret, key_tries, sign_tries; |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 58 | ecp_point R; |
| 59 | mpi k, e; |
| 60 | |
| 61 | ecp_point_init( &R ); |
| 62 | mpi_init( &k ); |
| 63 | mpi_init( &e ); |
| 64 | |
| 65 | sign_tries = 0; |
| 66 | do |
| 67 | { |
| 68 | /* |
| 69 | * Steps 1-3: generate a suitable ephemeral keypair |
| 70 | */ |
| 71 | key_tries = 0; |
| 72 | do |
| 73 | { |
| 74 | MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) ); |
| 75 | MPI_CHK( mpi_copy( r, &R.X ) ); |
| 76 | |
| 77 | if( key_tries++ > 10 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame^] | 78 | { |
| 79 | ret = POLARSSL_ERR_ECP_GENERIC; |
| 80 | goto cleanup; |
| 81 | } |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 82 | } |
| 83 | while( mpi_cmp_int( r, 0 ) == 0 ); |
| 84 | |
| 85 | /* |
| 86 | * Step 5: derive MPI from hashed message |
| 87 | */ |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 88 | MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Step 6: compute s = (e + r * d) / k mod n |
| 92 | */ |
| 93 | MPI_CHK( mpi_mul_mpi( s, r, d ) ); |
| 94 | MPI_CHK( mpi_add_mpi( &e, &e, s ) ); |
| 95 | MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) ); |
| 96 | MPI_CHK( mpi_mul_mpi( s, s, &e ) ); |
| 97 | MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) ); |
| 98 | |
| 99 | if( sign_tries++ > 10 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame^] | 100 | { |
| 101 | ret = POLARSSL_ERR_ECP_GENERIC; |
| 102 | goto cleanup; |
| 103 | } |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 104 | } |
| 105 | while( mpi_cmp_int( s, 0 ) == 0 ); |
| 106 | |
| 107 | cleanup: |
| 108 | ecp_point_free( &R ); |
| 109 | mpi_free( &k ); |
| 110 | mpi_free( &e ); |
| 111 | |
| 112 | return( ret ); |
| 113 | } |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 114 | |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 115 | /* |
| 116 | * Verify ECDSA signature of hashed message (SEC1 4.1.4) |
| 117 | * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message) |
| 118 | */ |
| 119 | int ecdsa_verify( const ecp_group *grp, |
| 120 | const unsigned char *buf, size_t blen, |
| 121 | const ecp_point *Q, const mpi *r, const mpi *s) |
| 122 | { |
| 123 | int ret; |
| 124 | mpi e, s_inv, u1, u2; |
| 125 | ecp_point R, P; |
| 126 | |
| 127 | ecp_point_init( &R ); ecp_point_init( &P ); |
| 128 | mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 ); |
| 129 | |
| 130 | /* |
| 131 | * Step 1: make sure r and s are in range 1..n-1 |
| 132 | */ |
| 133 | if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 || |
| 134 | mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 ) |
| 135 | { |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame^] | 136 | ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA; |
| 137 | goto cleanup; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Additional precaution: make sure Q is valid |
| 142 | */ |
| 143 | MPI_CHK( ecp_check_pubkey( grp, Q ) ); |
| 144 | |
| 145 | /* |
| 146 | * Step 3: derive MPI from hashed message |
| 147 | */ |
| 148 | MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
| 149 | |
| 150 | /* |
| 151 | * Step 4: u1 = e / s mod n, u2 = r / s mod n |
| 152 | */ |
| 153 | MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) ); |
| 154 | |
| 155 | MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) ); |
| 156 | MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) ); |
| 157 | |
| 158 | MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) ); |
| 159 | MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) ); |
| 160 | |
| 161 | /* |
| 162 | * Step 5: R = u1 G + u2 Q |
| 163 | */ |
| 164 | MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G ) ); |
| 165 | MPI_CHK( ecp_mul( grp, &P, &u2, Q ) ); |
| 166 | MPI_CHK( ecp_add( grp, &R, &R, &P ) ); |
| 167 | |
| 168 | if( ecp_is_zero( &R ) ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame^] | 169 | { |
| 170 | ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA; |
| 171 | goto cleanup; |
| 172 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * Step 6: check that xR == r |
| 176 | */ |
| 177 | if( mpi_cmp_mpi( &R.X, r ) != 0 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame^] | 178 | { |
| 179 | ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA; |
| 180 | goto cleanup; |
| 181 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 182 | |
| 183 | cleanup: |
| 184 | ecp_point_free( &R ); ecp_point_free( &P ); |
| 185 | mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 ); |
| 186 | |
| 187 | return( ret ); |
| 188 | } |
| 189 | |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 190 | /* |
| 191 | * Initialize context |
| 192 | */ |
| 193 | void ecdsa_init( ecdsa_context *ctx ) |
| 194 | { |
| 195 | ecp_group_init( &ctx->grp ); |
| 196 | mpi_init( &ctx->d ); |
| 197 | ecp_point_init( &ctx->Q ); |
| 198 | mpi_init( &ctx->r ); |
| 199 | mpi_init( &ctx->s ); |
| 200 | mpi_init( &ctx->d ); |
| 201 | ctx->point_format = POLARSSL_ECP_PF_UNCOMPRESSED; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Free context |
| 206 | */ |
| 207 | void ecdsa_free( ecdsa_context *ctx ) |
| 208 | { |
| 209 | ecp_group_free( &ctx->grp ); |
| 210 | mpi_free( &ctx->d ); |
| 211 | ecp_point_free( &ctx->Q ); |
| 212 | mpi_free( &ctx->r ); |
| 213 | mpi_free( &ctx->s ); |
| 214 | mpi_free( &ctx->d ); |
| 215 | ctx->point_format = POLARSSL_ECP_PF_UNCOMPRESSED; |
| 216 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 217 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 218 | #if defined(POLARSSL_SELF_TEST) |
| 219 | |
| 220 | /* |
| 221 | * Checkup routine |
| 222 | */ |
| 223 | int ecdsa_self_test( int verbose ) |
| 224 | { |
| 225 | return( verbose++ ); |
| 226 | } |
| 227 | |
| 228 | #endif |
| 229 | |
| 230 | #endif /* defined(POLARSSL_ECDSA_C) */ |