Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curve Diffie-Hellman |
| 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 | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame^] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +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 |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 27 | * RFC 4492 |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 28 | */ |
| 29 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 31 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
| 33 | #include POLARSSL_CONFIG_FILE |
| 34 | #endif |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 36 | #if defined(POLARSSL_ECDH_C) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 37 | |
| 38 | #include "polarssl/ecdh.h" |
| 39 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 40 | /* |
| 41 | * Generate public key: simple wrapper around ecp_gen_keypair |
| 42 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 43 | int ecdh_gen_public( ecp_group *grp, mpi *d, ecp_point *Q, |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 44 | int (*f_rng)(void *, unsigned char *, size_t), |
| 45 | void *p_rng ) |
| 46 | { |
| 47 | return ecp_gen_keypair( grp, d, Q, f_rng, p_rng ); |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Compute shared secret (SEC1 3.3.1) |
| 52 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 53 | int ecdh_compute_shared( ecp_group *grp, mpi *z, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 54 | const ecp_point *Q, const mpi *d, |
| 55 | int (*f_rng)(void *, unsigned char *, size_t), |
| 56 | void *p_rng ) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 57 | { |
| 58 | int ret; |
| 59 | ecp_point P; |
| 60 | |
| 61 | ecp_point_init( &P ); |
| 62 | |
| 63 | /* |
| 64 | * Make sure Q is a valid pubkey before using it |
| 65 | */ |
| 66 | MPI_CHK( ecp_check_pubkey( grp, Q ) ); |
| 67 | |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 68 | MPI_CHK( ecp_mul( grp, &P, d, Q, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 69 | |
| 70 | if( ecp_is_zero( &P ) ) |
Paul Bakker | b548d77 | 2013-07-26 14:21:34 +0200 | [diff] [blame] | 71 | { |
| 72 | ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA; |
| 73 | goto cleanup; |
| 74 | } |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 75 | |
| 76 | MPI_CHK( mpi_copy( z, &P.X ) ); |
| 77 | |
| 78 | cleanup: |
| 79 | ecp_point_free( &P ); |
| 80 | |
| 81 | return( ret ); |
| 82 | } |
| 83 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 84 | /* |
| 85 | * Initialize context |
| 86 | */ |
| 87 | void ecdh_init( ecdh_context *ctx ) |
| 88 | { |
Manuel Pégourié-Gonnard | c83e418 | 2013-09-17 10:48:41 +0200 | [diff] [blame] | 89 | memset( ctx, 0, sizeof( ecdh_context ) ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 92 | /* |
| 93 | * Free context |
| 94 | */ |
| 95 | void ecdh_free( ecdh_context *ctx ) |
| 96 | { |
| 97 | if( ctx == NULL ) |
| 98 | return; |
| 99 | |
| 100 | ecp_group_free( &ctx->grp ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 101 | ecp_point_free( &ctx->Q ); |
| 102 | ecp_point_free( &ctx->Qp ); |
Manuel Pégourié-Gonnard | c83e418 | 2013-09-17 10:48:41 +0200 | [diff] [blame] | 103 | ecp_point_free( &ctx->Vi ); |
| 104 | ecp_point_free( &ctx->Vf ); |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 105 | mpi_free( &ctx->d ); |
| 106 | mpi_free( &ctx->z ); |
| 107 | mpi_free( &ctx->_d ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 110 | /* |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 111 | * Setup and write the ServerKeyExhange parameters (RFC 4492) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 112 | * struct { |
| 113 | * ECParameters curve_params; |
| 114 | * ECPoint public; |
| 115 | * } ServerECDHParams; |
| 116 | */ |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 117 | int ecdh_make_params( ecdh_context *ctx, size_t *olen, |
| 118 | unsigned char *buf, size_t blen, |
| 119 | int (*f_rng)(void *, unsigned char *, size_t), |
| 120 | void *p_rng ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 121 | { |
| 122 | int ret; |
| 123 | size_t grp_len, pt_len; |
| 124 | |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 125 | if( ctx == NULL || ctx->grp.pbits == 0 ) |
| 126 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 127 | |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 128 | if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ) |
| 129 | != 0 ) |
| 130 | return( ret ); |
| 131 | |
| 132 | if( ( ret = ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) ) |
| 133 | != 0 ) |
| 134 | return( ret ); |
| 135 | |
| 136 | buf += grp_len; |
| 137 | blen -= grp_len; |
| 138 | |
| 139 | if( ( ret = ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format, |
| 140 | &pt_len, buf, blen ) ) != 0 ) |
| 141 | return( ret ); |
| 142 | |
| 143 | *olen = grp_len + pt_len; |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 144 | return( 0 ); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 147 | /* |
| 148 | * Read the ServerKeyExhange parameters (RFC 4492) |
| 149 | * struct { |
| 150 | * ECParameters curve_params; |
| 151 | * ECPoint public; |
| 152 | * } ServerECDHParams; |
| 153 | */ |
| 154 | int ecdh_read_params( ecdh_context *ctx, |
| 155 | const unsigned char **buf, const unsigned char *end ) |
| 156 | { |
| 157 | int ret; |
| 158 | |
| 159 | if( ( ret = ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 ) |
| 160 | return( ret ); |
| 161 | |
| 162 | if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) ) |
| 163 | != 0 ) |
| 164 | return( ret ); |
| 165 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 166 | return( 0 ); |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 167 | } |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 168 | |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 169 | /* |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 170 | * Get parameters from a keypair |
| 171 | */ |
| 172 | int ecdh_get_params( ecdh_context *ctx, const ecp_keypair *key, |
| 173 | ecdh_side side ) |
| 174 | { |
| 175 | int ret; |
| 176 | |
| 177 | if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ) |
| 178 | return( ret ); |
| 179 | |
| 180 | /* If it's not our key, just import the public part as Qp */ |
| 181 | if( side == POLARSSL_ECDH_THEIRS ) |
| 182 | return( ecp_copy( &ctx->Qp, &key->Q ) ); |
| 183 | |
| 184 | /* Our key: import public (as Q) and private parts */ |
| 185 | if( side != POLARSSL_ECDH_OURS ) |
| 186 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 187 | |
| 188 | if( ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 || |
| 189 | ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 ) |
| 190 | return( ret ); |
| 191 | |
| 192 | return( 0 ); |
| 193 | } |
| 194 | |
| 195 | /* |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 196 | * Setup and export the client public value |
| 197 | */ |
| 198 | int ecdh_make_public( ecdh_context *ctx, size_t *olen, |
| 199 | unsigned char *buf, size_t blen, |
| 200 | int (*f_rng)(void *, unsigned char *, size_t), |
| 201 | void *p_rng ) |
| 202 | { |
| 203 | int ret; |
| 204 | |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 205 | if( ctx == NULL || ctx->grp.pbits == 0 ) |
| 206 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 207 | |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 208 | if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ) |
| 209 | != 0 ) |
| 210 | return( ret ); |
| 211 | |
| 212 | return ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format, |
| 213 | olen, buf, blen ); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Parse and import the client's public value |
| 218 | */ |
| 219 | int ecdh_read_public( ecdh_context *ctx, |
| 220 | const unsigned char *buf, size_t blen ) |
| 221 | { |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 222 | int ret; |
| 223 | const unsigned char *p = buf; |
| 224 | |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 225 | if( ctx == NULL ) |
| 226 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 227 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 228 | if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 ) |
| 229 | return( ret ); |
| 230 | |
| 231 | if( (size_t)( p - buf ) != blen ) |
| 232 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 233 | |
| 234 | return( 0 ); |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 237 | /* |
| 238 | * Derive and export the shared secret |
| 239 | */ |
| 240 | int ecdh_calc_secret( ecdh_context *ctx, size_t *olen, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 241 | unsigned char *buf, size_t blen, |
| 242 | int (*f_rng)(void *, unsigned char *, size_t), |
| 243 | void *p_rng ) |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 244 | { |
| 245 | int ret; |
| 246 | |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 247 | if( ctx == NULL ) |
| 248 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 249 | |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 250 | if( ( ret = ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d, |
| 251 | f_rng, p_rng ) ) != 0 ) |
| 252 | { |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 253 | return( ret ); |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 254 | } |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 255 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 256 | if( mpi_size( &ctx->z ) > blen ) |
| 257 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 258 | |
Manuel Pégourié-Gonnard | 0a56c2c | 2014-01-17 21:24:04 +0100 | [diff] [blame] | 259 | *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 260 | return mpi_write_binary( &ctx->z, buf, *olen ); |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 264 | #if defined(POLARSSL_SELF_TEST) |
| 265 | |
| 266 | /* |
| 267 | * Checkup routine |
| 268 | */ |
| 269 | int ecdh_self_test( int verbose ) |
| 270 | { |
Manuel Pégourié-Gonnard | 7c59363 | 2014-01-20 10:27:13 +0100 | [diff] [blame] | 271 | ((void) verbose ); |
| 272 | return( 0 ); |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 275 | #endif /* POLARSSL_SELF_TEST */ |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 276 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 277 | #endif /* POLARSSL_ECDH_C */ |