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 | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.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 | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/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 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/ecdsa.h" |
| 38 | #include "mbedtls/asn1write.h" |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 39 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 40 | #include <string.h> |
| 41 | |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 42 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 43 | #include "mbedtls/hmac_drbg.h" |
Manuel Pégourié-Gonnard | 7845fc0 | 2014-01-27 14:24:03 +0100 | [diff] [blame] | 44 | #endif |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 45 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 46 | /* |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 47 | * Derive a suitable integer for group grp from a buffer of length len |
| 48 | * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3 |
| 49 | */ |
| 50 | static int derive_mpi( const ecp_group *grp, mpi *x, |
| 51 | const unsigned char *buf, size_t blen ) |
| 52 | { |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 53 | int ret; |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 54 | size_t n_size = ( grp->nbits + 7 ) / 8; |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 55 | size_t use_size = blen > n_size ? n_size : blen; |
| 56 | |
| 57 | MPI_CHK( mpi_read_binary( x, buf, use_size ) ); |
| 58 | if( use_size * 8 > grp->nbits ) |
| 59 | MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) ); |
| 60 | |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 61 | /* While at it, reduce modulo N */ |
| 62 | if( mpi_cmp_mpi( x, &grp->N ) >= 0 ) |
| 63 | MPI_CHK( mpi_sub_mpi( x, x, &grp->N ) ); |
| 64 | |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 65 | cleanup: |
| 66 | return( ret ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 70 | * Compute ECDSA signature of a hashed message (SEC1 4.1.3) |
| 71 | * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message) |
| 72 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 73 | int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s, |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 74 | const mpi *d, const unsigned char *buf, size_t blen, |
| 75 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 76 | { |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 77 | int ret, key_tries, sign_tries, blind_tries; |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 78 | ecp_point R; |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 79 | mpi k, e, t; |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 80 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 81 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
| 82 | if( grp->N.p == NULL ) |
| 83 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 84 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 85 | ecp_point_init( &R ); |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 86 | mpi_init( &k ); mpi_init( &e ); mpi_init( &t ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 87 | |
| 88 | sign_tries = 0; |
| 89 | do |
| 90 | { |
| 91 | /* |
| 92 | * Steps 1-3: generate a suitable ephemeral keypair |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 93 | * and set r = xR mod n |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 94 | */ |
| 95 | key_tries = 0; |
| 96 | do |
| 97 | { |
| 98 | 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] | 99 | MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 100 | |
| 101 | if( key_tries++ > 10 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 102 | { |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 103 | ret = POLARSSL_ERR_ECP_RANDOM_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 104 | goto cleanup; |
| 105 | } |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 106 | } |
| 107 | while( mpi_cmp_int( r, 0 ) == 0 ); |
| 108 | |
| 109 | /* |
| 110 | * Step 5: derive MPI from hashed message |
| 111 | */ |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 112 | MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 113 | |
| 114 | /* |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 115 | * Generate a random value to blind inv_mod in next step, |
| 116 | * avoiding a potential timing leak. |
| 117 | */ |
| 118 | blind_tries = 0; |
| 119 | do |
| 120 | { |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 121 | size_t n_size = ( grp->nbits + 7 ) / 8; |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 122 | MPI_CHK( mpi_fill_random( &t, n_size, f_rng, p_rng ) ); |
| 123 | MPI_CHK( mpi_shift_r( &t, 8 * n_size - grp->nbits ) ); |
| 124 | |
| 125 | /* See ecp_gen_keypair() */ |
| 126 | if( ++blind_tries > 30 ) |
| 127 | return( POLARSSL_ERR_ECP_RANDOM_FAILED ); |
| 128 | } |
| 129 | while( mpi_cmp_int( &t, 1 ) < 0 || |
| 130 | mpi_cmp_mpi( &t, &grp->N ) >= 0 ); |
| 131 | |
| 132 | /* |
| 133 | * 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] | 134 | */ |
| 135 | MPI_CHK( mpi_mul_mpi( s, r, d ) ); |
| 136 | MPI_CHK( mpi_add_mpi( &e, &e, s ) ); |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 137 | MPI_CHK( mpi_mul_mpi( &e, &e, &t ) ); |
| 138 | MPI_CHK( mpi_mul_mpi( &k, &k, &t ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 139 | MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) ); |
| 140 | MPI_CHK( mpi_mul_mpi( s, s, &e ) ); |
| 141 | MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) ); |
| 142 | |
| 143 | if( sign_tries++ > 10 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 144 | { |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 145 | ret = POLARSSL_ERR_ECP_RANDOM_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 146 | goto cleanup; |
| 147 | } |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 148 | } |
| 149 | while( mpi_cmp_int( s, 0 ) == 0 ); |
| 150 | |
| 151 | cleanup: |
| 152 | ecp_point_free( &R ); |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 153 | mpi_free( &k ); mpi_free( &e ); mpi_free( &t ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 154 | |
| 155 | return( ret ); |
| 156 | } |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 157 | |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 158 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
| 159 | /* |
| 160 | * Deterministic signature wrapper |
| 161 | */ |
| 162 | int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s, |
| 163 | const mpi *d, const unsigned char *buf, size_t blen, |
| 164 | md_type_t md_alg ) |
| 165 | { |
| 166 | int ret; |
| 167 | hmac_drbg_context rng_ctx; |
Manuel Pégourié-Gonnard | f42bca6 | 2014-01-06 15:05:01 +0100 | [diff] [blame] | 168 | unsigned char data[2 * POLARSSL_ECP_MAX_BYTES]; |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 169 | size_t grp_len = ( grp->nbits + 7 ) / 8; |
| 170 | const md_info_t *md_info; |
| 171 | mpi h; |
| 172 | |
Manuel Pégourié-Gonnard | b8cfe3f | 2015-03-31 11:04:45 +0200 | [diff] [blame] | 173 | if( ( md_info = md_info_from_type( md_alg ) ) == NULL ) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 174 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 175 | |
| 176 | mpi_init( &h ); |
| 177 | memset( &rng_ctx, 0, sizeof( hmac_drbg_context ) ); |
| 178 | |
Manuel Pégourié-Gonnard | f42bca6 | 2014-01-06 15:05:01 +0100 | [diff] [blame] | 179 | /* Use private key and message hash (reduced) to initialize HMAC_DRBG */ |
| 180 | MPI_CHK( mpi_write_binary( d, data, grp_len ) ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 181 | MPI_CHK( derive_mpi( grp, &h, buf, blen ) ); |
Manuel Pégourié-Gonnard | f42bca6 | 2014-01-06 15:05:01 +0100 | [diff] [blame] | 182 | 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] | 183 | 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] | 184 | |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 185 | ret = ecdsa_sign( grp, r, s, d, buf, blen, |
| 186 | hmac_drbg_random, &rng_ctx ); |
| 187 | |
| 188 | cleanup: |
| 189 | hmac_drbg_free( &rng_ctx ); |
| 190 | mpi_free( &h ); |
| 191 | |
| 192 | return( ret ); |
| 193 | } |
Paul Bakker | 9f3c7d7 | 2014-01-23 16:11:14 +0100 | [diff] [blame] | 194 | #endif /* POLARSSL_ECDSA_DETERMINISTIC */ |
| 195 | |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 196 | /* |
| 197 | * Verify ECDSA signature of hashed message (SEC1 4.1.4) |
| 198 | * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message) |
| 199 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 200 | int ecdsa_verify( ecp_group *grp, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 201 | const unsigned char *buf, size_t blen, |
| 202 | const ecp_point *Q, const mpi *r, const mpi *s) |
| 203 | { |
| 204 | int ret; |
| 205 | mpi e, s_inv, u1, u2; |
| 206 | ecp_point R, P; |
| 207 | |
| 208 | ecp_point_init( &R ); ecp_point_init( &P ); |
| 209 | mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 ); |
| 210 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 211 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
| 212 | if( grp->N.p == NULL ) |
| 213 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 214 | |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 215 | /* |
| 216 | * Step 1: make sure r and s are in range 1..n-1 |
| 217 | */ |
| 218 | if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 || |
| 219 | mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 ) |
| 220 | { |
Manuel Pégourié-Gonnard | db77175 | 2013-08-27 15:11:23 +0200 | [diff] [blame] | 221 | ret = POLARSSL_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 222 | goto cleanup; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Additional precaution: make sure Q is valid |
| 227 | */ |
| 228 | MPI_CHK( ecp_check_pubkey( grp, Q ) ); |
| 229 | |
| 230 | /* |
| 231 | * Step 3: derive MPI from hashed message |
| 232 | */ |
| 233 | MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
| 234 | |
| 235 | /* |
| 236 | * Step 4: u1 = e / s mod n, u2 = r / s mod n |
| 237 | */ |
| 238 | MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) ); |
| 239 | |
| 240 | MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) ); |
| 241 | MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) ); |
| 242 | |
| 243 | MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) ); |
| 244 | MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) ); |
| 245 | |
| 246 | /* |
| 247 | * Step 5: R = u1 G + u2 Q |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 248 | * |
| 249 | * Since we're not using any secret data, no need to pass a RNG to |
| 250 | * ecp_mul() for countermesures. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 251 | */ |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 252 | MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) ); |
| 253 | MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 254 | MPI_CHK( ecp_add( grp, &R, &R, &P ) ); |
| 255 | |
| 256 | if( ecp_is_zero( &R ) ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 257 | { |
Manuel Pégourié-Gonnard | db77175 | 2013-08-27 15:11:23 +0200 | [diff] [blame] | 258 | ret = POLARSSL_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 259 | goto cleanup; |
| 260 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 261 | |
| 262 | /* |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 263 | * Step 6: convert xR to an integer (no-op) |
| 264 | * Step 7: reduce xR mod n (gives v) |
| 265 | */ |
| 266 | MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) ); |
| 267 | |
| 268 | /* |
| 269 | * 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] | 270 | */ |
| 271 | if( mpi_cmp_mpi( &R.X, r ) != 0 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 272 | { |
Manuel Pégourié-Gonnard | db77175 | 2013-08-27 15:11:23 +0200 | [diff] [blame] | 273 | ret = POLARSSL_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 274 | goto cleanup; |
| 275 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 276 | |
| 277 | cleanup: |
| 278 | ecp_point_free( &R ); ecp_point_free( &P ); |
| 279 | mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 ); |
| 280 | |
| 281 | return( ret ); |
| 282 | } |
| 283 | |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 284 | /* |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 285 | * Convert a signature (given by context) to ASN.1 |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 286 | */ |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 287 | static int ecdsa_signature_to_asn1( const mpi *r, const mpi *s, |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 288 | unsigned char *sig, size_t *slen ) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 289 | { |
| 290 | int ret; |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 291 | unsigned char buf[POLARSSL_ECDSA_MAX_LEN]; |
Manuel Pégourié-Gonnard | 4cf0686 | 2013-09-16 12:07:45 +0200 | [diff] [blame] | 292 | unsigned char *p = buf + sizeof( buf ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 293 | size_t len = 0; |
| 294 | |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 295 | ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, s ) ); |
| 296 | ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, r ) ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 297 | |
| 298 | ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) ); |
| 299 | ASN1_CHK_ADD( len, asn1_write_tag( &p, buf, |
| 300 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 301 | |
| 302 | memcpy( sig, p, len ); |
| 303 | *slen = len; |
| 304 | |
| 305 | return( 0 ); |
| 306 | } |
| 307 | |
| 308 | /* |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 309 | * Compute and write signature |
| 310 | */ |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 311 | int ecdsa_write_signature( ecdsa_context *ctx, md_type_t md_alg, |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 312 | const unsigned char *hash, size_t hlen, |
| 313 | unsigned char *sig, size_t *slen, |
| 314 | int (*f_rng)(void *, unsigned char *, size_t), |
| 315 | void *p_rng ) |
| 316 | { |
| 317 | int ret; |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 318 | mpi r, s; |
| 319 | |
| 320 | mpi_init( &r ); |
| 321 | mpi_init( &s ); |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 322 | |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 323 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
| 324 | (void) f_rng; |
| 325 | (void) p_rng; |
| 326 | |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 327 | MPI_CHK( ecdsa_sign_det( &ctx->grp, &r, &s, &ctx->d, |
| 328 | hash, hlen, md_alg ) ); |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 329 | #else |
| 330 | (void) md_alg; |
| 331 | |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 332 | MPI_CHK( ecdsa_sign( &ctx->grp, &r, &s, &ctx->d, |
| 333 | hash, hlen, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 334 | #endif |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 335 | |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 336 | MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) ); |
| 337 | |
| 338 | cleanup: |
| 339 | mpi_free( &r ); |
| 340 | mpi_free( &s ); |
| 341 | |
| 342 | return( ret ); |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Manuel Pégourié-Gonnard | eadda3f | 2015-04-03 13:14:48 +0200 | [diff] [blame] | 345 | #if ! defined(POLARSSL_DEPRECATED_REMOVED) && \ |
| 346 | defined(POLARSSL_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 347 | int ecdsa_write_signature_det( ecdsa_context *ctx, |
| 348 | const unsigned char *hash, size_t hlen, |
| 349 | unsigned char *sig, size_t *slen, |
| 350 | md_type_t md_alg ) |
| 351 | { |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 352 | return( ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen, |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 353 | NULL, NULL ) ); |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 354 | } |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 355 | #endif |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 356 | |
| 357 | /* |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 358 | * Read and check signature |
| 359 | */ |
| 360 | int ecdsa_read_signature( ecdsa_context *ctx, |
| 361 | const unsigned char *hash, size_t hlen, |
| 362 | const unsigned char *sig, size_t slen ) |
| 363 | { |
| 364 | int ret; |
| 365 | unsigned char *p = (unsigned char *) sig; |
| 366 | const unsigned char *end = sig + slen; |
| 367 | size_t len; |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 368 | mpi r, s; |
| 369 | |
| 370 | mpi_init( &r ); |
| 371 | mpi_init( &s ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 372 | |
| 373 | if( ( ret = asn1_get_tag( &p, end, &len, |
| 374 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
| 375 | { |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 376 | ret += POLARSSL_ERR_ECP_BAD_INPUT_DATA; |
| 377 | goto cleanup; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | if( p + len != end ) |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 381 | { |
| 382 | ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA + |
| 383 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH; |
| 384 | goto cleanup; |
| 385 | } |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 386 | |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 387 | if( ( ret = asn1_get_mpi( &p, end, &r ) ) != 0 || |
| 388 | ( ret = asn1_get_mpi( &p, end, &s ) ) != 0 ) |
| 389 | { |
| 390 | ret += POLARSSL_ERR_ECP_BAD_INPUT_DATA; |
| 391 | goto cleanup; |
| 392 | } |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 393 | |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 394 | if( ( ret = ecdsa_verify( &ctx->grp, hash, hlen, |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 395 | &ctx->Q, &r, &s ) ) != 0 ) |
| 396 | goto cleanup; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 397 | |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 398 | if( p != end ) |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 399 | ret = POLARSSL_ERR_ECP_SIG_LEN_MISMATCH; |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 400 | |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 401 | cleanup: |
| 402 | mpi_free( &r ); |
| 403 | mpi_free( &s ); |
| 404 | |
| 405 | return( ret ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 409 | * Generate key pair |
| 410 | */ |
| 411 | int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid, |
| 412 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 413 | { |
| 414 | return( ecp_use_known_dp( &ctx->grp, gid ) || |
| 415 | ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ); |
| 416 | } |
| 417 | |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 418 | /* |
| 419 | * Set context from an ecp_keypair |
| 420 | */ |
| 421 | int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key ) |
| 422 | { |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 423 | int ret; |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 424 | |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 425 | if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 || |
| 426 | ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 || |
| 427 | ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 ) |
| 428 | { |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 429 | ecdsa_free( ctx ); |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 430 | } |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 431 | |
| 432 | return( ret ); |
| 433 | } |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 434 | |
| 435 | /* |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 436 | * Initialize context |
| 437 | */ |
| 438 | void ecdsa_init( ecdsa_context *ctx ) |
| 439 | { |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 440 | ecp_keypair_init( ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Free context |
| 445 | */ |
| 446 | void ecdsa_free( ecdsa_context *ctx ) |
| 447 | { |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 448 | ecp_keypair_free( ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 449 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 450 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 451 | #endif /* POLARSSL_ECDSA_C */ |