Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file ecdh.h |
| 3 | * |
| 4 | * \brief Elliptic curve Diffie-Hellman |
| 5 | * |
| 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_ECDH_H |
| 28 | #define POLARSSL_ECDH_H |
| 29 | |
| 30 | #include "polarssl/ecp.h" |
| 31 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 32 | /** |
| 33 | * \brief ECDH context structure |
| 34 | */ |
| 35 | typedef struct |
| 36 | { |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 37 | ecp_group grp; /*!< ellipitic curve used */ |
| 38 | mpi d; /*!< our secret value */ |
| 39 | ecp_point Q; /*!< our public value */ |
| 40 | ecp_point Qp; /*!< peer's public value */ |
| 41 | mpi z; /*!< shared secret */ |
| 42 | int point_format; /*!< format for point export */ |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 43 | } |
| 44 | ecdh_context; |
| 45 | |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 50 | /** |
| 51 | * \brief Generate a public key |
| 52 | * |
| 53 | * \param grp ECP group |
| 54 | * \param d Destination MPI (secret exponent) |
| 55 | * \param Q Destination point (public key) |
| 56 | * \param f_rng RNG function |
| 57 | * \param p_rng RNG parameter |
| 58 | * |
| 59 | * \return 0 if successful, |
| 60 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
| 61 | */ |
| 62 | int ecdh_gen_public( const ecp_group *grp, mpi *d, ecp_point *Q, |
| 63 | int (*f_rng)(void *, unsigned char *, size_t), |
| 64 | void *p_rng ); |
| 65 | |
| 66 | /** |
| 67 | * \brief Compute shared secret |
| 68 | * |
| 69 | * \param grp ECP group |
| 70 | * \param z Destination MPI (shared secret) |
| 71 | * \param Q Public key from other party |
| 72 | * \param d Our secret exponent |
| 73 | * |
| 74 | * \return 0 if successful, |
| 75 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
| 76 | */ |
| 77 | int ecdh_compute_shared( const ecp_group *grp, mpi *z, |
| 78 | const ecp_point *Q, const mpi *d ); |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 79 | |
| 80 | /** |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 81 | * \brief Initialize context |
| 82 | * |
| 83 | * \param ctx Context to initialize |
| 84 | */ |
| 85 | void ecdh_init( ecdh_context *ctx ); |
| 86 | |
| 87 | /** |
| 88 | * \brief Free context |
| 89 | * |
| 90 | * \param ctx Context to free |
| 91 | */ |
| 92 | void ecdh_free( ecdh_context *ctx ); |
| 93 | |
| 94 | /** |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 95 | * \brief Setup and write the ServerKeyExhange parameters |
| 96 | * |
| 97 | * \param ctx ECDH context |
| 98 | * \param buf destination buffer |
| 99 | * \param olen number of chars written |
| 100 | * \param f_rng RNG function |
| 101 | * \param p_rng RNG parameter |
| 102 | * |
| 103 | * \note This function assumes that ctx->grp has already been |
| 104 | * properly set (for example using ecp_use_known_dp). |
| 105 | * |
| 106 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 107 | */ |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 108 | int ecdh_make_params( ecdh_context *ctx, size_t *olen, |
| 109 | unsigned char *buf, size_t blen, |
| 110 | int (*f_rng)(void *, unsigned char *, size_t), |
| 111 | void *p_rng ); |
| 112 | |
| 113 | /** |
| 114 | * \brief Parse the ServerKeyExhange parameters |
| 115 | * |
| 116 | * \param ctx ECDH context |
| 117 | * \param buf $(start of input buffer) |
| 118 | * \param end one past end of buffer |
| 119 | * |
| 120 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 121 | */ |
| 122 | int ecdh_read_params( ecdh_context *ctx, |
| 123 | const unsigned char **buf, const unsigned char *end ); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 124 | |
| 125 | /** |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 126 | * \brief Setup and export the client's public value |
| 127 | * |
| 128 | * \param ctx ECDH context |
| 129 | * \param olen number of bytes actually written |
| 130 | * \param buf destination buffer |
| 131 | * \param blen size of destination buffer |
| 132 | * |
| 133 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 134 | */ |
| 135 | int ecdh_make_public( ecdh_context *ctx, size_t *olen, |
| 136 | unsigned char *buf, size_t blen, |
| 137 | int (*f_rng)(void *, unsigned char *, size_t), |
| 138 | void *p_rng ); |
| 139 | |
| 140 | /** |
| 141 | * \brief Parse and import the client's public value |
| 142 | * |
| 143 | * \param ctx ECDH context |
| 144 | * \param buf start of input buffer |
| 145 | * \param blen length of input buffer |
| 146 | * |
| 147 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 148 | */ |
| 149 | int ecdh_read_public( ecdh_context *ctx, |
| 150 | const unsigned char *buf, size_t blen ); |
| 151 | |
| 152 | /** |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 153 | * \brief Derive and export the shared secret |
| 154 | * |
| 155 | * \param ctx ECDH context |
| 156 | * \param olen number of bytes written |
| 157 | * \param buf destination buffer |
| 158 | * \param blen buffer length |
| 159 | * |
| 160 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 161 | */ |
| 162 | int ecdh_calc_secret( ecdh_context *ctx, size_t *olen, |
| 163 | unsigned char *buf, size_t blen ); |
| 164 | |
| 165 | /** |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 166 | * \brief Checkup routine |
| 167 | * |
| 168 | * \return 0 if successful, or 1 if the test failed |
| 169 | */ |
| 170 | int ecdh_self_test( int verbose ); |
| 171 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 172 | #ifdef __cplusplus |
| 173 | } |
| 174 | #endif |
| 175 | |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 176 | #endif |