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 |
| 30 | */ |
| 31 | |
| 32 | #include "polarssl/config.h" |
| 33 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame^] | 34 | #if defined(POLARSSL_ECDH_C) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 35 | |
| 36 | #include "polarssl/ecdh.h" |
| 37 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 38 | /* |
| 39 | * Generate public key: simple wrapper around ecp_gen_keypair |
| 40 | */ |
| 41 | int ecdh_gen_public( const ecp_group *grp, mpi *d, ecp_point *Q, |
| 42 | int (*f_rng)(void *, unsigned char *, size_t), |
| 43 | void *p_rng ) |
| 44 | { |
| 45 | return ecp_gen_keypair( grp, d, Q, f_rng, p_rng ); |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * Compute shared secret (SEC1 3.3.1) |
| 50 | */ |
| 51 | int ecdh_compute_shared( const ecp_group *grp, mpi *z, |
| 52 | const ecp_point *Q, const mpi *d ) |
| 53 | { |
| 54 | int ret; |
| 55 | ecp_point P; |
| 56 | |
| 57 | ecp_point_init( &P ); |
| 58 | |
| 59 | /* |
| 60 | * Make sure Q is a valid pubkey before using it |
| 61 | */ |
| 62 | MPI_CHK( ecp_check_pubkey( grp, Q ) ); |
| 63 | |
| 64 | MPI_CHK( ecp_mul( grp, &P, d, Q ) ); |
| 65 | |
| 66 | if( ecp_is_zero( &P ) ) |
| 67 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 68 | |
| 69 | MPI_CHK( mpi_copy( z, &P.X ) ); |
| 70 | |
| 71 | cleanup: |
| 72 | ecp_point_free( &P ); |
| 73 | |
| 74 | return( ret ); |
| 75 | } |
| 76 | |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 77 | |
| 78 | #if defined(POLARSSL_SELF_TEST) |
| 79 | |
| 80 | /* |
| 81 | * Checkup routine |
| 82 | */ |
| 83 | int ecdh_self_test( int verbose ) |
| 84 | { |
| 85 | return( verbose++ ); |
| 86 | } |
| 87 | |
| 88 | #endif |
| 89 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame^] | 90 | #endif /* defined(POLARSSL_ECDH_C) */ |