Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curve Diffie-Hellman |
| 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 |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 30 | * RFC 4492 |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #include "polarssl/config.h" |
| 34 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 35 | #if defined(POLARSSL_ECDH_C) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 36 | |
| 37 | #include "polarssl/ecdh.h" |
| 38 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 39 | /* |
| 40 | * Generate public key: simple wrapper around ecp_gen_keypair |
| 41 | */ |
| 42 | int ecdh_gen_public( const ecp_group *grp, mpi *d, ecp_point *Q, |
| 43 | int (*f_rng)(void *, unsigned char *, size_t), |
| 44 | void *p_rng ) |
| 45 | { |
| 46 | return ecp_gen_keypair( grp, d, Q, f_rng, p_rng ); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Compute shared secret (SEC1 3.3.1) |
| 51 | */ |
| 52 | int ecdh_compute_shared( const ecp_group *grp, mpi *z, |
| 53 | const ecp_point *Q, const mpi *d ) |
| 54 | { |
| 55 | int ret; |
| 56 | ecp_point P; |
| 57 | |
| 58 | ecp_point_init( &P ); |
| 59 | |
| 60 | /* |
| 61 | * Make sure Q is a valid pubkey before using it |
| 62 | */ |
| 63 | MPI_CHK( ecp_check_pubkey( grp, Q ) ); |
| 64 | |
| 65 | MPI_CHK( ecp_mul( grp, &P, d, Q ) ); |
| 66 | |
| 67 | if( ecp_is_zero( &P ) ) |
| 68 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 69 | |
| 70 | MPI_CHK( mpi_copy( z, &P.X ) ); |
| 71 | |
| 72 | cleanup: |
| 73 | ecp_point_free( &P ); |
| 74 | |
| 75 | return( ret ); |
| 76 | } |
| 77 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 78 | /* |
| 79 | * Initialize context |
| 80 | */ |
| 81 | void ecdh_init( ecdh_context *ctx ) |
| 82 | { |
| 83 | ecp_group_init( &ctx->grp ); |
| 84 | mpi_init ( &ctx->d ); |
| 85 | ecp_point_init( &ctx->Q ); |
| 86 | ecp_point_init( &ctx->Qp ); |
| 87 | mpi_init ( &ctx->z ); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame^] | 88 | ctx->point_format = POLARSSL_ECP_PF_UNCOMPRESSED; |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 91 | /* |
| 92 | * Free context |
| 93 | */ |
| 94 | void ecdh_free( ecdh_context *ctx ) |
| 95 | { |
| 96 | if( ctx == NULL ) |
| 97 | return; |
| 98 | |
| 99 | ecp_group_free( &ctx->grp ); |
| 100 | mpi_free ( &ctx->d ); |
| 101 | ecp_point_free( &ctx->Q ); |
| 102 | ecp_point_free( &ctx->Qp ); |
| 103 | mpi_free ( &ctx->z ); |
| 104 | } |
| 105 | |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame^] | 106 | /* |
| 107 | * Setup and write the ServerKeyExhange parameters |
| 108 | * struct { |
| 109 | * ECParameters curve_params; |
| 110 | * ECPoint public; |
| 111 | * } ServerECDHParams; |
| 112 | */ |
| 113 | int ecdh_make_server_params( ecdh_context *ctx, size_t *olen, |
| 114 | unsigned char *buf, size_t blen, |
| 115 | int (*f_rng)(void *, unsigned char *, size_t), |
| 116 | void *p_rng ) |
| 117 | { |
| 118 | int ret; |
| 119 | size_t grp_len, pt_len; |
| 120 | |
| 121 | if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ) |
| 122 | != 0 ) |
| 123 | return( ret ); |
| 124 | |
| 125 | if( ( ret = ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) ) |
| 126 | != 0 ) |
| 127 | return( ret ); |
| 128 | |
| 129 | buf += grp_len; |
| 130 | blen -= grp_len; |
| 131 | |
| 132 | if( ( ret = ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format, |
| 133 | &pt_len, buf, blen ) ) != 0 ) |
| 134 | return( ret ); |
| 135 | |
| 136 | *olen = grp_len + pt_len; |
| 137 | return 0; |
| 138 | } |
| 139 | |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 140 | |
| 141 | #if defined(POLARSSL_SELF_TEST) |
| 142 | |
| 143 | /* |
| 144 | * Checkup routine |
| 145 | */ |
| 146 | int ecdh_self_test( int verbose ) |
| 147 | { |
| 148 | return( verbose++ ); |
| 149 | } |
| 150 | |
| 151 | #endif |
| 152 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 153 | #endif /* defined(POLARSSL_ECDH_C) */ |