Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curve DSA |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame^] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * References: |
| 25 | * |
| 26 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
| 27 | */ |
| 28 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 30 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #else |
| 32 | #include POLARSSL_CONFIG_FILE |
| 33 | #endif |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 34 | |
| 35 | #if defined(POLARSSL_ECDSA_C) |
| 36 | |
| 37 | #include "polarssl/ecdsa.h" |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 38 | #include "polarssl/asn1write.h" |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 39 | |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 40 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 7845fc0 | 2014-01-27 14:24:03 +0100 | [diff] [blame] | 41 | #include "polarssl/hmac_drbg.h" |
| 42 | #endif |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 43 | |
Manuel Pégourié-Gonnard | 7845fc0 | 2014-01-27 14:24:03 +0100 | [diff] [blame] | 44 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 5e6edcf | 2014-01-07 16:17:53 +0100 | [diff] [blame] | 45 | /* |
| 46 | * This a hopefully temporary compatibility function. |
| 47 | * |
| 48 | * Since we can't ensure the caller will pass a valid md_alg before the next |
| 49 | * interface change, try to pick up a decent md by size. |
| 50 | * |
| 51 | * Argument is the minimum size in bytes of the MD output. |
| 52 | */ |
Manuel Pégourié-Gonnard | 9592485 | 2014-03-21 10:54:55 +0100 | [diff] [blame] | 53 | static const md_info_t *md_info_by_size( size_t min_size ) |
Manuel Pégourié-Gonnard | 5e6edcf | 2014-01-07 16:17:53 +0100 | [diff] [blame] | 54 | { |
| 55 | const md_info_t *md_cur, *md_picked = NULL; |
| 56 | const int *md_alg; |
| 57 | |
| 58 | for( md_alg = md_list(); *md_alg != 0; md_alg++ ) |
| 59 | { |
| 60 | if( ( md_cur = md_info_from_type( *md_alg ) ) == NULL || |
Manuel Pégourié-Gonnard | 9592485 | 2014-03-21 10:54:55 +0100 | [diff] [blame] | 61 | (size_t) md_cur->size < min_size || |
Manuel Pégourié-Gonnard | 5e6edcf | 2014-01-07 16:17:53 +0100 | [diff] [blame] | 62 | ( md_picked != NULL && md_cur->size > md_picked->size ) ) |
| 63 | continue; |
| 64 | |
| 65 | md_picked = md_cur; |
| 66 | } |
| 67 | |
| 68 | return( md_picked ); |
| 69 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 70 | #endif /* POLARSSL_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 71 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 72 | /* |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 73 | * Derive a suitable integer for group grp from a buffer of length len |
| 74 | * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3 |
| 75 | */ |
| 76 | static int derive_mpi( const ecp_group *grp, mpi *x, |
| 77 | const unsigned char *buf, size_t blen ) |
| 78 | { |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 79 | int ret; |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 80 | size_t n_size = ( grp->nbits + 7 ) / 8; |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 81 | size_t use_size = blen > n_size ? n_size : blen; |
| 82 | |
| 83 | MPI_CHK( mpi_read_binary( x, buf, use_size ) ); |
| 84 | if( use_size * 8 > grp->nbits ) |
| 85 | MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) ); |
| 86 | |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 87 | /* While at it, reduce modulo N */ |
| 88 | if( mpi_cmp_mpi( x, &grp->N ) >= 0 ) |
| 89 | MPI_CHK( mpi_sub_mpi( x, x, &grp->N ) ); |
| 90 | |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 91 | cleanup: |
| 92 | return( ret ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 96 | * Compute ECDSA signature of a hashed message (SEC1 4.1.3) |
| 97 | * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message) |
| 98 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 99 | int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s, |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 100 | const mpi *d, const unsigned char *buf, size_t blen, |
| 101 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 102 | { |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 103 | int ret, key_tries, sign_tries, blind_tries; |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 104 | ecp_point R; |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 105 | mpi k, e, t; |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 106 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 107 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
| 108 | if( grp->N.p == NULL ) |
| 109 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 110 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 111 | ecp_point_init( &R ); |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 112 | mpi_init( &k ); mpi_init( &e ); mpi_init( &t ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 113 | |
| 114 | sign_tries = 0; |
| 115 | do |
| 116 | { |
| 117 | /* |
| 118 | * Steps 1-3: generate a suitable ephemeral keypair |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 119 | * and set r = xR mod n |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 120 | */ |
| 121 | key_tries = 0; |
| 122 | do |
| 123 | { |
| 124 | MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 125 | MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 126 | |
| 127 | if( key_tries++ > 10 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 128 | { |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 129 | ret = POLARSSL_ERR_ECP_RANDOM_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 130 | goto cleanup; |
| 131 | } |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 132 | } |
| 133 | while( mpi_cmp_int( r, 0 ) == 0 ); |
| 134 | |
| 135 | /* |
| 136 | * Step 5: derive MPI from hashed message |
| 137 | */ |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 138 | MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 139 | |
| 140 | /* |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 141 | * Generate a random value to blind inv_mod in next step, |
| 142 | * avoiding a potential timing leak. |
| 143 | */ |
| 144 | blind_tries = 0; |
| 145 | do |
| 146 | { |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 147 | size_t n_size = ( grp->nbits + 7 ) / 8; |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 148 | MPI_CHK( mpi_fill_random( &t, n_size, f_rng, p_rng ) ); |
| 149 | MPI_CHK( mpi_shift_r( &t, 8 * n_size - grp->nbits ) ); |
| 150 | |
| 151 | /* See ecp_gen_keypair() */ |
| 152 | if( ++blind_tries > 30 ) |
| 153 | return( POLARSSL_ERR_ECP_RANDOM_FAILED ); |
| 154 | } |
| 155 | while( mpi_cmp_int( &t, 1 ) < 0 || |
| 156 | mpi_cmp_mpi( &t, &grp->N ) >= 0 ); |
| 157 | |
| 158 | /* |
| 159 | * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 160 | */ |
| 161 | MPI_CHK( mpi_mul_mpi( s, r, d ) ); |
| 162 | MPI_CHK( mpi_add_mpi( &e, &e, s ) ); |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 163 | MPI_CHK( mpi_mul_mpi( &e, &e, &t ) ); |
| 164 | MPI_CHK( mpi_mul_mpi( &k, &k, &t ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 165 | MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) ); |
| 166 | MPI_CHK( mpi_mul_mpi( s, s, &e ) ); |
| 167 | MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) ); |
| 168 | |
| 169 | if( sign_tries++ > 10 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 170 | { |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 171 | ret = POLARSSL_ERR_ECP_RANDOM_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 172 | goto cleanup; |
| 173 | } |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 174 | } |
| 175 | while( mpi_cmp_int( s, 0 ) == 0 ); |
| 176 | |
| 177 | cleanup: |
| 178 | ecp_point_free( &R ); |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 179 | mpi_free( &k ); mpi_free( &e ); mpi_free( &t ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 180 | |
| 181 | return( ret ); |
| 182 | } |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 183 | |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 184 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
| 185 | /* |
| 186 | * Deterministic signature wrapper |
| 187 | */ |
| 188 | int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s, |
| 189 | const mpi *d, const unsigned char *buf, size_t blen, |
| 190 | md_type_t md_alg ) |
| 191 | { |
| 192 | int ret; |
| 193 | hmac_drbg_context rng_ctx; |
Manuel Pégourié-Gonnard | f42bca6 | 2014-01-06 15:05:01 +0100 | [diff] [blame] | 194 | unsigned char data[2 * POLARSSL_ECP_MAX_BYTES]; |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 195 | size_t grp_len = ( grp->nbits + 7 ) / 8; |
| 196 | const md_info_t *md_info; |
| 197 | mpi h; |
| 198 | |
Manuel Pégourié-Gonnard | 5e6edcf | 2014-01-07 16:17:53 +0100 | [diff] [blame] | 199 | /* Temporary fallback */ |
| 200 | if( md_alg == POLARSSL_MD_NONE ) |
| 201 | md_info = md_info_by_size( blen ); |
| 202 | else |
| 203 | md_info = md_info_from_type( md_alg ); |
| 204 | |
| 205 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 206 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 207 | |
| 208 | mpi_init( &h ); |
| 209 | memset( &rng_ctx, 0, sizeof( hmac_drbg_context ) ); |
| 210 | |
Manuel Pégourié-Gonnard | f42bca6 | 2014-01-06 15:05:01 +0100 | [diff] [blame] | 211 | /* Use private key and message hash (reduced) to initialize HMAC_DRBG */ |
| 212 | MPI_CHK( mpi_write_binary( d, data, grp_len ) ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 213 | MPI_CHK( derive_mpi( grp, &h, buf, blen ) ); |
Manuel Pégourié-Gonnard | f42bca6 | 2014-01-06 15:05:01 +0100 | [diff] [blame] | 214 | MPI_CHK( mpi_write_binary( &h, data + grp_len, grp_len ) ); |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 215 | hmac_drbg_init_buf( &rng_ctx, md_info, data, 2 * grp_len ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 216 | |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 217 | ret = ecdsa_sign( grp, r, s, d, buf, blen, |
| 218 | hmac_drbg_random, &rng_ctx ); |
| 219 | |
| 220 | cleanup: |
| 221 | hmac_drbg_free( &rng_ctx ); |
| 222 | mpi_free( &h ); |
| 223 | |
| 224 | return( ret ); |
| 225 | } |
Paul Bakker | 9f3c7d7 | 2014-01-23 16:11:14 +0100 | [diff] [blame] | 226 | #endif /* POLARSSL_ECDSA_DETERMINISTIC */ |
| 227 | |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 228 | /* |
| 229 | * Verify ECDSA signature of hashed message (SEC1 4.1.4) |
| 230 | * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message) |
| 231 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 232 | int ecdsa_verify( ecp_group *grp, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 233 | const unsigned char *buf, size_t blen, |
| 234 | const ecp_point *Q, const mpi *r, const mpi *s) |
| 235 | { |
| 236 | int ret; |
| 237 | mpi e, s_inv, u1, u2; |
| 238 | ecp_point R, P; |
| 239 | |
| 240 | ecp_point_init( &R ); ecp_point_init( &P ); |
| 241 | mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 ); |
| 242 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 243 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
| 244 | if( grp->N.p == NULL ) |
| 245 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 246 | |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 247 | /* |
| 248 | * Step 1: make sure r and s are in range 1..n-1 |
| 249 | */ |
| 250 | if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 || |
| 251 | mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 ) |
| 252 | { |
Manuel Pégourié-Gonnard | db77175 | 2013-08-27 15:11:23 +0200 | [diff] [blame] | 253 | ret = POLARSSL_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 254 | goto cleanup; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Additional precaution: make sure Q is valid |
| 259 | */ |
| 260 | MPI_CHK( ecp_check_pubkey( grp, Q ) ); |
| 261 | |
| 262 | /* |
| 263 | * Step 3: derive MPI from hashed message |
| 264 | */ |
| 265 | MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
| 266 | |
| 267 | /* |
| 268 | * Step 4: u1 = e / s mod n, u2 = r / s mod n |
| 269 | */ |
| 270 | MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) ); |
| 271 | |
| 272 | MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) ); |
| 273 | MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) ); |
| 274 | |
| 275 | MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) ); |
| 276 | MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) ); |
| 277 | |
| 278 | /* |
| 279 | * Step 5: R = u1 G + u2 Q |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 280 | * |
| 281 | * Since we're not using any secret data, no need to pass a RNG to |
| 282 | * ecp_mul() for countermesures. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 283 | */ |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 284 | MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) ); |
| 285 | MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 286 | MPI_CHK( ecp_add( grp, &R, &R, &P ) ); |
| 287 | |
| 288 | if( ecp_is_zero( &R ) ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 289 | { |
Manuel Pégourié-Gonnard | db77175 | 2013-08-27 15:11:23 +0200 | [diff] [blame] | 290 | ret = POLARSSL_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 291 | goto cleanup; |
| 292 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 293 | |
| 294 | /* |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 295 | * Step 6: convert xR to an integer (no-op) |
| 296 | * Step 7: reduce xR mod n (gives v) |
| 297 | */ |
| 298 | MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) ); |
| 299 | |
| 300 | /* |
| 301 | * Step 8: check if v (that is, R.X) is equal to r |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 302 | */ |
| 303 | if( mpi_cmp_mpi( &R.X, r ) != 0 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 304 | { |
Manuel Pégourié-Gonnard | db77175 | 2013-08-27 15:11:23 +0200 | [diff] [blame] | 305 | ret = POLARSSL_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 306 | goto cleanup; |
| 307 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 308 | |
| 309 | cleanup: |
| 310 | ecp_point_free( &R ); ecp_point_free( &P ); |
| 311 | mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 ); |
| 312 | |
| 313 | return( ret ); |
| 314 | } |
| 315 | |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 316 | /* |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 317 | * RFC 4492 page 20: |
| 318 | * |
| 319 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 320 | * r INTEGER, |
| 321 | * s INTEGER |
| 322 | * } |
| 323 | * |
| 324 | * Size is at most |
| 325 | * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s, |
| 326 | * twice that + 1 (tag) + 2 (len) for the sequence |
| 327 | * (assuming ECP_MAX_BYTES is less than 126 for r and s, |
| 328 | * and less than 124 (total len <= 255) for the sequence) |
| 329 | */ |
| 330 | #if POLARSSL_ECP_MAX_BYTES > 124 |
| 331 | #error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN" |
| 332 | #endif |
Manuel Pégourié-Gonnard | e959979 | 2014-11-10 13:43:55 +0100 | [diff] [blame] | 333 | #define MAX_SIG_LEN ( 3 + 2 * ( 3 + POLARSSL_ECP_MAX_BYTES ) ) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 334 | |
| 335 | /* |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 336 | * Convert a signature (given by context) to ASN.1 |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 337 | */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 338 | static int ecdsa_signature_to_asn1( ecdsa_context *ctx, |
| 339 | unsigned char *sig, size_t *slen ) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 340 | { |
| 341 | int ret; |
Manuel Pégourié-Gonnard | 4cf0686 | 2013-09-16 12:07:45 +0200 | [diff] [blame] | 342 | unsigned char buf[MAX_SIG_LEN]; |
| 343 | unsigned char *p = buf + sizeof( buf ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 344 | size_t len = 0; |
| 345 | |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 346 | ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) ); |
| 347 | ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) ); |
| 348 | |
| 349 | ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) ); |
| 350 | ASN1_CHK_ADD( len, asn1_write_tag( &p, buf, |
| 351 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 352 | |
| 353 | memcpy( sig, p, len ); |
| 354 | *slen = len; |
| 355 | |
| 356 | return( 0 ); |
| 357 | } |
| 358 | |
| 359 | /* |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 360 | * Compute and write signature |
| 361 | */ |
| 362 | int ecdsa_write_signature( ecdsa_context *ctx, |
| 363 | const unsigned char *hash, size_t hlen, |
| 364 | unsigned char *sig, size_t *slen, |
| 365 | int (*f_rng)(void *, unsigned char *, size_t), |
| 366 | void *p_rng ) |
| 367 | { |
| 368 | int ret; |
| 369 | |
| 370 | if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d, |
| 371 | hash, hlen, f_rng, p_rng ) ) != 0 ) |
| 372 | { |
| 373 | return( ret ); |
| 374 | } |
| 375 | |
| 376 | return( ecdsa_signature_to_asn1( ctx, sig, slen ) ); |
| 377 | } |
| 378 | |
Paul Bakker | 9f3c7d7 | 2014-01-23 16:11:14 +0100 | [diff] [blame] | 379 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 380 | /* |
| 381 | * Compute and write signature deterministically |
| 382 | */ |
| 383 | int ecdsa_write_signature_det( ecdsa_context *ctx, |
| 384 | const unsigned char *hash, size_t hlen, |
| 385 | unsigned char *sig, size_t *slen, |
| 386 | md_type_t md_alg ) |
| 387 | { |
| 388 | int ret; |
| 389 | |
| 390 | if( ( ret = ecdsa_sign_det( &ctx->grp, &ctx->r, &ctx->s, &ctx->d, |
| 391 | hash, hlen, md_alg ) ) != 0 ) |
| 392 | { |
| 393 | return( ret ); |
| 394 | } |
| 395 | |
| 396 | return( ecdsa_signature_to_asn1( ctx, sig, slen ) ); |
| 397 | } |
Paul Bakker | 9f3c7d7 | 2014-01-23 16:11:14 +0100 | [diff] [blame] | 398 | #endif /* POLARSSL_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 399 | |
| 400 | /* |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 401 | * Read and check signature |
| 402 | */ |
| 403 | int ecdsa_read_signature( ecdsa_context *ctx, |
| 404 | const unsigned char *hash, size_t hlen, |
| 405 | const unsigned char *sig, size_t slen ) |
| 406 | { |
| 407 | int ret; |
| 408 | unsigned char *p = (unsigned char *) sig; |
| 409 | const unsigned char *end = sig + slen; |
| 410 | size_t len; |
| 411 | |
| 412 | if( ( ret = asn1_get_tag( &p, end, &len, |
| 413 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
| 414 | { |
| 415 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret ); |
| 416 | } |
| 417 | |
| 418 | if( p + len != end ) |
| 419 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + |
| 420 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 421 | |
| 422 | if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 || |
| 423 | ( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 ) |
| 424 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret ); |
| 425 | |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 426 | if( ( ret = ecdsa_verify( &ctx->grp, hash, hlen, |
| 427 | &ctx->Q, &ctx->r, &ctx->s ) ) != 0 ) |
| 428 | return( ret ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 429 | |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 430 | if( p != end ) |
| 431 | return( POLARSSL_ERR_ECP_SIG_LEN_MISMATCH ); |
| 432 | |
| 433 | return( 0 ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 437 | * Generate key pair |
| 438 | */ |
| 439 | int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid, |
| 440 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 441 | { |
| 442 | return( ecp_use_known_dp( &ctx->grp, gid ) || |
| 443 | ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ); |
| 444 | } |
| 445 | |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 446 | /* |
| 447 | * Set context from an ecp_keypair |
| 448 | */ |
| 449 | int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key ) |
| 450 | { |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 451 | int ret; |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 452 | |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 453 | if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 || |
| 454 | ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 || |
| 455 | ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 ) |
| 456 | { |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 457 | ecdsa_free( ctx ); |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 458 | } |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 459 | |
| 460 | return( ret ); |
| 461 | } |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 462 | |
| 463 | /* |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 464 | * Initialize context |
| 465 | */ |
| 466 | void ecdsa_init( ecdsa_context *ctx ) |
| 467 | { |
| 468 | ecp_group_init( &ctx->grp ); |
| 469 | mpi_init( &ctx->d ); |
| 470 | ecp_point_init( &ctx->Q ); |
| 471 | mpi_init( &ctx->r ); |
| 472 | mpi_init( &ctx->s ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* |
| 476 | * Free context |
| 477 | */ |
| 478 | void ecdsa_free( ecdsa_context *ctx ) |
| 479 | { |
| 480 | ecp_group_free( &ctx->grp ); |
| 481 | mpi_free( &ctx->d ); |
| 482 | ecp_point_free( &ctx->Q ); |
| 483 | mpi_free( &ctx->r ); |
| 484 | mpi_free( &ctx->s ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 485 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 486 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 487 | #if defined(POLARSSL_SELF_TEST) |
| 488 | |
| 489 | /* |
| 490 | * Checkup routine |
| 491 | */ |
| 492 | int ecdsa_self_test( int verbose ) |
| 493 | { |
Manuel Pégourié-Gonnard | 7c59363 | 2014-01-20 10:27:13 +0100 | [diff] [blame] | 494 | ((void) verbose ); |
| 495 | return( 0 ); |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 498 | #endif /* POLARSSL_SELF_TEST */ |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 499 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 500 | #endif /* POLARSSL_ECDSA_C */ |