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 | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 9 | * |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 24 | #ifndef MBEDTLS_ECDH_H |
| 25 | #define MBEDTLS_ECDH_H |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 27 | #include "ecp.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 28 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 33 | /** |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 34 | * When importing from an EC key, select if it is our key or the peer's key |
| 35 | */ |
| 36 | typedef enum |
| 37 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 38 | MBEDTLS_ECDH_OURS, |
| 39 | MBEDTLS_ECDH_THEIRS, |
| 40 | } mbedtls_ecdh_side; |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 41 | |
| 42 | /** |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 43 | * \brief ECDH context structure |
| 44 | */ |
| 45 | typedef struct |
| 46 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 47 | mbedtls_ecp_group grp; /*!< elliptic curve used */ |
| 48 | mbedtls_mpi d; /*!< our secret value (private key) */ |
| 49 | mbedtls_ecp_point Q; /*!< our public value (public key) */ |
| 50 | mbedtls_ecp_point Qp; /*!< peer's public value (public key) */ |
| 51 | mbedtls_mpi z; /*!< shared secret */ |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 52 | int point_format; /*!< format for point export in TLS messages */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 53 | mbedtls_ecp_point Vi; /*!< blinding value (for later) */ |
| 54 | mbedtls_ecp_point Vf; /*!< un-blinding value (for later) */ |
| 55 | mbedtls_mpi _d; /*!< previous d (for later) */ |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 56 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 57 | mbedtls_ecdh_context; |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 59 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 60 | * \brief Generate a public key. |
| 61 | * Raw function that only does the core computation. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 62 | * |
| 63 | * \param grp ECP group |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 64 | * \param d Destination MPI (secret exponent, aka private key) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 65 | * \param Q Destination point (public key) |
| 66 | * \param f_rng RNG function |
| 67 | * \param p_rng RNG parameter |
| 68 | * |
| 69 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 70 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 71 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 72 | int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 73 | int (*f_rng)(void *, unsigned char *, size_t), |
| 74 | void *p_rng ); |
| 75 | |
| 76 | /** |
| 77 | * \brief Compute shared secret |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 78 | * Raw function that only does the core computation. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 79 | * |
| 80 | * \param grp ECP group |
| 81 | * \param z Destination MPI (shared secret) |
| 82 | * \param Q Public key from other party |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 83 | * \param d Our secret exponent (private key) |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 84 | * \param f_rng RNG function (see notes) |
| 85 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 86 | * |
| 87 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 88 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 89 | * |
| 90 | * \note If f_rng is not NULL, it is used to implement |
| 91 | * countermeasures against potential elaborate timing |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 92 | * attacks, see \c mbedtls_ecp_mul() for details. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 93 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 94 | int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, |
| 95 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 96 | int (*f_rng)(void *, unsigned char *, size_t), |
| 97 | void *p_rng ); |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 98 | |
| 99 | /** |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 100 | * \brief Initialize context |
| 101 | * |
| 102 | * \param ctx Context to initialize |
| 103 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 104 | void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * \brief Free context |
| 108 | * |
| 109 | * \param ctx Context to free |
| 110 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 111 | void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 112 | |
| 113 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 114 | * \brief Generate a public key and a TLS ServerKeyExchange payload. |
| 115 | * (First function used by a TLS server for ECDHE.) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 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 |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 125 | * properly set (for example using mbedtls_ecp_use_known_dp). |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 126 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 127 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 128 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 129 | int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen, |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 130 | unsigned char *buf, size_t blen, |
| 131 | int (*f_rng)(void *, unsigned char *, size_t), |
| 132 | void *p_rng ); |
| 133 | |
| 134 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 135 | * \brief Parse and procress a TLS ServerKeyExhange payload. |
| 136 | * (First function used by a TLS client for ECDHE.) |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 137 | * |
| 138 | * \param ctx ECDH context |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 139 | * \param buf pointer to start of input buffer |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 140 | * \param end one past end of buffer |
| 141 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 142 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 143 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 144 | int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 145 | const unsigned char **buf, const unsigned char *end ); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 146 | |
| 147 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 148 | * \brief Setup an ECDH context from an EC key. |
| 149 | * (Used by clients and servers in place of the |
| 150 | * ServerKeyEchange for static ECDH: import ECDH parameters |
| 151 | * from a certificate's EC key information.) |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 152 | * |
| 153 | * \param ctx ECDH constext to set |
| 154 | * \param key EC key to use |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 155 | * \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] | 156 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 157 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 158 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 159 | int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, |
| 160 | mbedtls_ecdh_side side ); |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 161 | |
| 162 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 163 | * \brief Generate a public key and a TLS ClientKeyExchange payload. |
| 164 | * (Second function used by a TLS client for ECDH(E).) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 165 | * |
| 166 | * \param ctx ECDH context |
| 167 | * \param olen number of bytes actually written |
| 168 | * \param buf destination buffer |
| 169 | * \param blen size of destination buffer |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 170 | * \param f_rng RNG function |
| 171 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 172 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 173 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 174 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 175 | int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 176 | unsigned char *buf, size_t blen, |
| 177 | int (*f_rng)(void *, unsigned char *, size_t), |
| 178 | void *p_rng ); |
| 179 | |
| 180 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 181 | * \brief Parse and process a TLS ClientKeyExchange payload. |
| 182 | * (Second function used by a TLS server for ECDH(E).) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 183 | * |
| 184 | * \param ctx ECDH context |
| 185 | * \param buf start of input buffer |
| 186 | * \param blen length of input buffer |
| 187 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 188 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 189 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 190 | int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 191 | const unsigned char *buf, size_t blen ); |
| 192 | |
| 193 | /** |
Manuel Pégourié-Gonnard | 2361746 | 2014-06-25 13:55:10 +0200 | [diff] [blame] | 194 | * \brief Derive and export the shared secret. |
| 195 | * (Last function used by both TLS client en servers.) |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 196 | * |
| 197 | * \param ctx ECDH context |
| 198 | * \param olen number of bytes written |
| 199 | * \param buf destination buffer |
| 200 | * \param blen buffer length |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 201 | * \param f_rng RNG function, see notes for \c mbedtls_ecdh_compute_shared() |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 202 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 203 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 204 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 205 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 206 | int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 207 | unsigned char *buf, size_t blen, |
| 208 | int (*f_rng)(void *, unsigned char *, size_t), |
| 209 | void *p_rng ); |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 210 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 211 | #ifdef __cplusplus |
| 212 | } |
| 213 | #endif |
| 214 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 215 | #endif /* ecdh.h */ |