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