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