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