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 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 967a2a5 | 2015-01-22 14:28:16 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (http://www.polarssl.org) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 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 | #ifndef POLARSSL_ECDH_H |
| 26 | #define POLARSSL_ECDH_H |
| 27 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 28 | #include "ecp.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 29 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 34 | /** |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 35 | * When importing from an EC key, select if it is our key or the peer's key |
| 36 | */ |
| 37 | typedef enum |
| 38 | { |
| 39 | POLARSSL_ECDH_OURS, |
| 40 | POLARSSL_ECDH_THEIRS, |
| 41 | } ecdh_side; |
| 42 | |
| 43 | /** |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 44 | * \brief ECDH context structure |
| 45 | */ |
| 46 | typedef struct |
| 47 | { |
Paul Bakker | 237a847 | 2014-06-25 14:45:24 +0200 | [diff] [blame] | 48 | ecp_group grp; /*!< elliptic curve used */ |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 49 | mpi d; /*!< our secret value (private key) */ |
| 50 | ecp_point Q; /*!< our public value (public key) */ |
| 51 | ecp_point Qp; /*!< peer's public value (public key) */ |
| 52 | mpi z; /*!< shared secret */ |
| 53 | int point_format; /*!< format for point export in TLS messages */ |
| 54 | ecp_point Vi; /*!< blinding value (for later) */ |
| 55 | ecp_point Vf; /*!< un-blinding value (for later) */ |
| 56 | mpi _d; /*!< previous d (for later) */ |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 57 | } |
| 58 | ecdh_context; |
| 59 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 60 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 61 | * \brief Generate a public key. |
| 62 | * Raw function that only does the core computation. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 63 | * |
| 64 | * \param grp ECP group |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 65 | * \param d Destination MPI (secret exponent, aka private key) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 66 | * \param Q Destination point (public key) |
| 67 | * \param f_rng RNG function |
| 68 | * \param p_rng RNG parameter |
| 69 | * |
| 70 | * \return 0 if successful, |
| 71 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
| 72 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 73 | 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] | 74 | int (*f_rng)(void *, unsigned char *, size_t), |
| 75 | void *p_rng ); |
| 76 | |
| 77 | /** |
| 78 | * \brief Compute shared secret |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 79 | * Raw function that only does the core computation. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 80 | * |
| 81 | * \param grp ECP group |
| 82 | * \param z Destination MPI (shared secret) |
| 83 | * \param Q Public key from other party |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 84 | * \param d Our secret exponent (private key) |
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 | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 115 | * \brief Generate a public key and a TLS ServerKeyExchange payload. |
| 116 | * (First function used by a TLS server for ECDHE.) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 117 | * |
| 118 | * \param ctx ECDH context |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 119 | * \param olen number of chars written |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 120 | * \param buf destination buffer |
| 121 | * \param blen length of buffer |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 122 | * \param f_rng RNG function |
| 123 | * \param p_rng RNG parameter |
| 124 | * |
| 125 | * \note This function assumes that ctx->grp has already been |
| 126 | * properly set (for example using ecp_use_known_dp). |
| 127 | * |
| 128 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 129 | */ |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 130 | int ecdh_make_params( ecdh_context *ctx, size_t *olen, |
| 131 | unsigned char *buf, size_t blen, |
| 132 | int (*f_rng)(void *, unsigned char *, size_t), |
| 133 | void *p_rng ); |
| 134 | |
| 135 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 136 | * \brief Parse and procress a TLS ServerKeyExhange payload. |
| 137 | * (First function used by a TLS client for ECDHE.) |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 138 | * |
| 139 | * \param ctx ECDH context |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 140 | * \param buf pointer to start of input buffer |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 141 | * \param end one past end of buffer |
| 142 | * |
| 143 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 144 | */ |
| 145 | int ecdh_read_params( ecdh_context *ctx, |
| 146 | const unsigned char **buf, const unsigned char *end ); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 147 | |
| 148 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 149 | * \brief Setup an ECDH context from an EC key. |
| 150 | * (Used by clients and servers in place of the |
| 151 | * ServerKeyEchange for static ECDH: import ECDH parameters |
| 152 | * from a certificate's EC key information.) |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 153 | * |
| 154 | * \param ctx ECDH constext to set |
| 155 | * \param key EC key to use |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 156 | * \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] | 157 | * |
| 158 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 159 | */ |
| 160 | int ecdh_get_params( ecdh_context *ctx, const ecp_keypair *key, |
| 161 | ecdh_side side ); |
| 162 | |
| 163 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 164 | * \brief Generate a public key and a TLS ClientKeyExchange payload. |
| 165 | * (Second function used by a TLS client for ECDH(E).) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 166 | * |
| 167 | * \param ctx ECDH context |
| 168 | * \param olen number of bytes actually written |
| 169 | * \param buf destination buffer |
| 170 | * \param blen size of destination buffer |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 171 | * \param f_rng RNG function |
| 172 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 173 | * |
| 174 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 175 | */ |
| 176 | int ecdh_make_public( ecdh_context *ctx, size_t *olen, |
| 177 | unsigned char *buf, size_t blen, |
| 178 | int (*f_rng)(void *, unsigned char *, size_t), |
| 179 | void *p_rng ); |
| 180 | |
| 181 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 182 | * \brief Parse and process a TLS ClientKeyExchange payload. |
| 183 | * (Second function used by a TLS server for ECDH(E).) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 184 | * |
| 185 | * \param ctx ECDH context |
| 186 | * \param buf start of input buffer |
| 187 | * \param blen length of input buffer |
| 188 | * |
| 189 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 190 | */ |
| 191 | int ecdh_read_public( ecdh_context *ctx, |
| 192 | const unsigned char *buf, size_t blen ); |
| 193 | |
| 194 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 195 | * \brief Derive and export the shared secret. |
| 196 | * (Last function used by both TLS client en servers.) |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 197 | * |
| 198 | * \param ctx ECDH context |
| 199 | * \param olen number of bytes written |
| 200 | * \param buf destination buffer |
| 201 | * \param blen buffer length |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 202 | * \param f_rng RNG function, see notes for \c ecdh_compute_shared() |
| 203 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 204 | * |
| 205 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 206 | */ |
| 207 | int ecdh_calc_secret( ecdh_context *ctx, size_t *olen, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 208 | unsigned char *buf, size_t blen, |
| 209 | int (*f_rng)(void *, unsigned char *, size_t), |
| 210 | void *p_rng ); |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 211 | |
| 212 | /** |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 213 | * \brief Checkup routine |
| 214 | * |
| 215 | * \return 0 if successful, or 1 if the test failed |
| 216 | */ |
| 217 | int ecdh_self_test( int verbose ); |
| 218 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 219 | #ifdef __cplusplus |
| 220 | } |
| 221 | #endif |
| 222 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 223 | #endif /* ecdh.h */ |