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 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 30 | #include "ecp.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 31 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 36 | /** |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 37 | * When importing from an EC key, select if it is our key or the peer's key |
| 38 | */ |
| 39 | typedef enum |
| 40 | { |
| 41 | POLARSSL_ECDH_OURS, |
| 42 | POLARSSL_ECDH_THEIRS, |
| 43 | } ecdh_side; |
| 44 | |
| 45 | /** |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 46 | * \brief ECDH context structure |
| 47 | */ |
| 48 | typedef struct |
| 49 | { |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 50 | ecp_group grp; /*!< ellipitic curve used */ |
| 51 | mpi d; /*!< our secret value */ |
| 52 | ecp_point Q; /*!< our public value */ |
| 53 | ecp_point Qp; /*!< peer's public value */ |
| 54 | mpi z; /*!< shared secret */ |
| 55 | int point_format; /*!< format for point export */ |
Manuel Pégourié-Gonnard | c83e418 | 2013-09-17 10:48:41 +0200 | [diff] [blame] | 56 | ecp_point Vi; /*!< blinding value (for later) */ |
| 57 | ecp_point Vf; /*!< un-blinding value (for later) */ |
| 58 | mpi _d; /*!< previous d */ |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 59 | } |
| 60 | ecdh_context; |
| 61 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 62 | /** |
| 63 | * \brief Generate a public key |
| 64 | * |
| 65 | * \param grp ECP group |
| 66 | * \param d Destination MPI (secret exponent) |
| 67 | * \param Q Destination point (public key) |
| 68 | * \param f_rng RNG function |
| 69 | * \param p_rng RNG parameter |
| 70 | * |
| 71 | * \return 0 if successful, |
| 72 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
| 73 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 74 | 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] | 75 | int (*f_rng)(void *, unsigned char *, size_t), |
| 76 | void *p_rng ); |
| 77 | |
| 78 | /** |
| 79 | * \brief Compute shared secret |
| 80 | * |
| 81 | * \param grp ECP group |
| 82 | * \param z Destination MPI (shared secret) |
| 83 | * \param Q Public key from other party |
| 84 | * \param d Our secret exponent |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 85 | * \param f_rng RNG function (see notes) |
| 86 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 87 | * |
| 88 | * \return 0 if successful, |
| 89 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 90 | * |
| 91 | * \note If f_rng is not NULL, it is used to implement |
| 92 | * countermeasures against potential elaborate timing |
| 93 | * attacks, see \c ecp_mul() for details. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 94 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 95 | int ecdh_compute_shared( ecp_group *grp, mpi *z, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 96 | const ecp_point *Q, const mpi *d, |
| 97 | int (*f_rng)(void *, unsigned char *, size_t), |
| 98 | void *p_rng ); |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 99 | |
| 100 | /** |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 101 | * \brief Initialize context |
| 102 | * |
| 103 | * \param ctx Context to initialize |
| 104 | */ |
| 105 | void ecdh_init( ecdh_context *ctx ); |
| 106 | |
| 107 | /** |
| 108 | * \brief Free context |
| 109 | * |
| 110 | * \param ctx Context to free |
| 111 | */ |
| 112 | void ecdh_free( ecdh_context *ctx ); |
| 113 | |
| 114 | /** |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 115 | * \brief Setup and write the ServerKeyExhange parameters |
| 116 | * |
| 117 | * \param ctx ECDH context |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 118 | * \param olen number of chars written |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 119 | * \param buf destination buffer |
| 120 | * \param blen length of buffer |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 121 | * \param f_rng RNG function |
| 122 | * \param p_rng RNG parameter |
| 123 | * |
| 124 | * \note This function assumes that ctx->grp has already been |
| 125 | * properly set (for example using ecp_use_known_dp). |
| 126 | * |
| 127 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 128 | */ |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 129 | int ecdh_make_params( ecdh_context *ctx, size_t *olen, |
| 130 | unsigned char *buf, size_t blen, |
| 131 | int (*f_rng)(void *, unsigned char *, size_t), |
| 132 | void *p_rng ); |
| 133 | |
| 134 | /** |
| 135 | * \brief Parse the ServerKeyExhange parameters |
| 136 | * |
| 137 | * \param ctx ECDH context |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 138 | * \param buf pointer to start of input buffer |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 139 | * \param end one past end of buffer |
| 140 | * |
| 141 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 142 | */ |
| 143 | int ecdh_read_params( ecdh_context *ctx, |
| 144 | const unsigned char **buf, const unsigned char *end ); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 145 | |
| 146 | /** |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 147 | * \brief Setup an ECDH context from an EC key |
| 148 | * |
| 149 | * \param ctx ECDH constext to set |
| 150 | * \param key EC key to use |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame^] | 151 | * \param side Is it our key (1) or the peer's key (0) ? |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 152 | * |
| 153 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 154 | */ |
| 155 | int ecdh_get_params( ecdh_context *ctx, const ecp_keypair *key, |
| 156 | ecdh_side side ); |
| 157 | |
| 158 | /** |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 159 | * \brief Setup and export the client's public value |
| 160 | * |
| 161 | * \param ctx ECDH context |
| 162 | * \param olen number of bytes actually written |
| 163 | * \param buf destination buffer |
| 164 | * \param blen size of destination buffer |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 165 | * \param f_rng RNG function |
| 166 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 167 | * |
| 168 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 169 | */ |
| 170 | int ecdh_make_public( ecdh_context *ctx, size_t *olen, |
| 171 | unsigned char *buf, size_t blen, |
| 172 | int (*f_rng)(void *, unsigned char *, size_t), |
| 173 | void *p_rng ); |
| 174 | |
| 175 | /** |
| 176 | * \brief Parse and import the client's public value |
| 177 | * |
| 178 | * \param ctx ECDH context |
| 179 | * \param buf start of input buffer |
| 180 | * \param blen length of input buffer |
| 181 | * |
| 182 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 183 | */ |
| 184 | int ecdh_read_public( ecdh_context *ctx, |
| 185 | const unsigned char *buf, size_t blen ); |
| 186 | |
| 187 | /** |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 188 | * \brief Derive and export the shared secret |
| 189 | * |
| 190 | * \param ctx ECDH context |
| 191 | * \param olen number of bytes written |
| 192 | * \param buf destination buffer |
| 193 | * \param blen buffer length |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 194 | * \param f_rng RNG function, see notes for \c ecdh_compute_shared() |
| 195 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 196 | * |
| 197 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 198 | */ |
| 199 | int ecdh_calc_secret( ecdh_context *ctx, size_t *olen, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 200 | unsigned char *buf, size_t blen, |
| 201 | int (*f_rng)(void *, unsigned char *, size_t), |
| 202 | void *p_rng ); |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 203 | |
| 204 | /** |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 205 | * \brief Checkup routine |
| 206 | * |
| 207 | * \return 0 if successful, or 1 if the test failed |
| 208 | */ |
| 209 | int ecdh_self_test( int verbose ); |
| 210 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 211 | #ifdef __cplusplus |
| 212 | } |
| 213 | #endif |
| 214 | |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 215 | #endif |