Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file ecp.h |
| 3 | * |
| 4 | * \brief Elliptic curves over GF(p) |
| 5 | * |
Paul Bakker | cf4365f | 2013-01-16 17:00:43 +0100 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 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_ECP_H |
| 28 | #define POLARSSL_ECP_H |
| 29 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 30 | #include "bignum.h" |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 31 | |
| 32 | /* |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 33 | * ECP error codes |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 34 | */ |
Paul Bakker | cf4365f | 2013-01-16 17:00:43 +0100 | [diff] [blame] | 35 | #define POLARSSL_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */ |
Paul Bakker | fd3eac5 | 2013-06-29 23:31:33 +0200 | [diff] [blame] | 36 | #define POLARSSL_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */ |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 37 | #define POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< Requested curve not available. */ |
Manuel Pégourié-Gonnard | db77175 | 2013-08-27 15:11:23 +0200 | [diff] [blame] | 38 | #define POLARSSL_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */ |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 39 | #define POLARSSL_ERR_ECP_MALLOC_FAILED -0x4D80 /**< Memory allocation failed. */ |
| 40 | #define POLARSSL_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as (ephemeral) key, failed. */ |
| 41 | #define POLARSSL_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */ |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 42 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 43 | #ifdef __cplusplus |
| 44 | extern "C" { |
| 45 | #endif |
| 46 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 47 | /** |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 48 | * Domain parameters (curve, subgroup and generator) identifiers. |
| 49 | * |
| 50 | * Only curves over prime fields are supported. |
| 51 | * |
| 52 | * \warning This library does not support validation of arbitrary domain |
| 53 | * parameters. Therefore, only well-known domain parameters from trusted |
| 54 | * sources should be used. See ecp_use_known_dp(). |
| 55 | */ |
| 56 | typedef enum |
| 57 | { |
| 58 | POLARSSL_ECP_DP_NONE = 0, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 59 | POLARSSL_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */ |
| 60 | POLARSSL_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */ |
| 61 | POLARSSL_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */ |
| 62 | POLARSSL_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */ |
| 63 | POLARSSL_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */ |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 64 | POLARSSL_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */ |
| 65 | POLARSSL_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */ |
| 66 | POLARSSL_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */ |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 67 | } ecp_group_id; |
| 68 | |
| 69 | /** |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 70 | * Number of supported curves (plus one for NONE) |
| 71 | */ |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 72 | #define POLARSSL_ECP_DP_MAX 9 |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 73 | |
| 74 | /** |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 75 | * Curve information for use by other modules |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 76 | */ |
| 77 | typedef struct |
| 78 | { |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 79 | ecp_group_id grp_id; /*!< Internal identifier */ |
| 80 | uint16_t tls_id; /*!< TLS NamedCurve identifier */ |
| 81 | uint16_t size; /*!< Curve size in bits */ |
| 82 | const char *name; /*!< Human-friendly name */ |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 83 | } ecp_curve_info; |
| 84 | |
| 85 | /** |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 86 | * \brief ECP point structure (jacobian coordinates) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 87 | * |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 88 | * \note All functions expect and return points satisfying |
| 89 | * the following condition: Z == 0 or Z == 1. (Other |
| 90 | * values of Z are used by internal functions only.) |
| 91 | * The point is zero, or "at infinity", if Z == 0. |
| 92 | * Otherwise, X and Y are its standard (affine) coordinates. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 93 | */ |
| 94 | typedef struct |
| 95 | { |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 96 | mpi X; /*!< the point's X coordinate */ |
| 97 | mpi Y; /*!< the point's Y coordinate */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 98 | mpi Z; /*!< the point's Z coordinate */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 99 | } |
| 100 | ecp_point; |
| 101 | |
| 102 | /** |
| 103 | * \brief ECP group structure |
| 104 | * |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 105 | * The curves we consider are defined by y^2 = x^3 + A x + B mod P, |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 106 | * and a generator for a large subgroup of order N is fixed. |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 107 | * |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 108 | * pbits and nbits must be the size of P and N in bits. |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 109 | * |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 110 | * If modp is NULL, reduction modulo P is done using a generic algorithm. |
| 111 | * Otherwise, it must point to a function that takes an mpi in the range |
| 112 | * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more |
| 113 | * than pbits, so that the integer may be efficiently brought in the 0..P-1 |
| 114 | * range by a few additions or substractions. It must return 0 on success and |
| 115 | * non-zero on failure. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 116 | */ |
| 117 | typedef struct |
| 118 | { |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 119 | ecp_group_id id; /*!< internal group identifier */ |
| 120 | mpi P; /*!< prime modulus of the base field */ |
Manuel Pégourié-Gonnard | 0cd6f98 | 2013-10-10 15:55:39 +0200 | [diff] [blame] | 121 | mpi A; /*!< linear term in the equation */ |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 122 | mpi B; /*!< constant term in the equation */ |
| 123 | ecp_point G; /*!< generator of the subgroup used */ |
| 124 | mpi N; /*!< the order of G */ |
| 125 | size_t pbits; /*!< number of bits in P */ |
| 126 | size_t nbits; /*!< number of bits in N */ |
| 127 | unsigned int h; /*!< cofactor (unused now: assume 1) */ |
| 128 | int (*modp)(mpi *); /*!< function for fast reduction mod P */ |
| 129 | int (*t_pre)(ecp_point *, void *); /*!< currently unused */ |
| 130 | int (*t_post)(ecp_point *, void *); /*!< currently unused */ |
| 131 | void *t_data; /*!< currently unused */ |
| 132 | ecp_point *T; /*!< pre-computed points for ecp_mul() */ |
| 133 | size_t T_size; /*!< number for pre-computed points */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 134 | } |
| 135 | ecp_group; |
| 136 | |
| 137 | /** |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 138 | * \brief ECP key pair structure |
| 139 | * |
| 140 | * A generic key pair that could be used for ECDSA, fixed ECDH, etc. |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 141 | * |
| 142 | * \note Members purposefully in the same order as struc ecdsa_context. |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 143 | */ |
| 144 | typedef struct |
| 145 | { |
| 146 | ecp_group grp; /*!< Elliptic curve and base point */ |
| 147 | mpi d; /*!< our secret value */ |
| 148 | ecp_point Q; /*!< our public value */ |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 149 | } |
| 150 | ecp_keypair; |
| 151 | |
| 152 | /** |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 153 | * Maximum size of the groups (that is, of N and P) |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 154 | */ |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 155 | #define POLARSSL_ECP_MAX_BITS 521 |
| 156 | #define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 ) |
Manuel Pégourié-Gonnard | 3837dae | 2013-09-12 01:39:07 +0200 | [diff] [blame] | 157 | #define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 ) |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 158 | |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 159 | /* |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 160 | * Maximum "window" size used for point multiplication. |
| 161 | * Default: 6. |
| 162 | * Minimum value: 2. Maximum value: 7. |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 163 | * |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 164 | * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) ) |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 165 | * points used for point multiplication. |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 166 | * |
| 167 | * Reduction in size may reduce speed for big curves. |
| 168 | */ |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 169 | #define POLARSSL_ECP_WINDOW_SIZE 6 /**< Maximum window size used. */ |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 170 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 171 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 172 | * Point formats, from RFC 4492's enum ECPointFormat |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 173 | */ |
| 174 | #define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */ |
| 175 | #define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */ |
| 176 | |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 177 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 178 | * Some other constants from RFC 4492 |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 179 | */ |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 180 | #define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */ |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 181 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 182 | /** |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 183 | * \brief Return the list of supported curves with associated info |
| 184 | * |
| 185 | * \return A statically allocated array, the last entry is 0. |
| 186 | */ |
| 187 | const ecp_curve_info *ecp_curve_list( void ); |
| 188 | |
| 189 | /** |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 190 | * \brief Get curve information from an internal group identifier |
| 191 | * |
| 192 | * \param grp_id A POLARSSL_ECP_DP_XXX value |
| 193 | * |
| 194 | * \return The associated curve information or NULL |
| 195 | */ |
| 196 | const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id ); |
| 197 | |
| 198 | /** |
| 199 | * \brief Get curve information from a TLS NamedCurve value |
| 200 | * |
| 201 | * \param grp_id A POLARSSL_ECP_DP_XXX value |
| 202 | * |
| 203 | * \return The associated curve information or NULL |
| 204 | */ |
| 205 | const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id ); |
| 206 | |
| 207 | /** |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 208 | * \brief Initialize a point (as zero) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 209 | */ |
| 210 | void ecp_point_init( ecp_point *pt ); |
| 211 | |
| 212 | /** |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 213 | * \brief Initialize a group (to something meaningless) |
| 214 | */ |
| 215 | void ecp_group_init( ecp_group *grp ); |
| 216 | |
| 217 | /** |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 218 | * \brief Initialize a key pair (as an invalid one) |
| 219 | */ |
| 220 | void ecp_keypair_init( ecp_keypair *key ); |
| 221 | |
| 222 | /** |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 223 | * \brief Free the components of a point |
| 224 | */ |
| 225 | void ecp_point_free( ecp_point *pt ); |
| 226 | |
| 227 | /** |
| 228 | * \brief Free the components of an ECP group |
| 229 | */ |
| 230 | void ecp_group_free( ecp_group *grp ); |
| 231 | |
| 232 | /** |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 233 | * \brief Free the components of a key pair |
| 234 | */ |
| 235 | void ecp_keypair_free( ecp_keypair *key ); |
| 236 | |
| 237 | /** |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 238 | * \brief Copy the contents of point Q into P |
| 239 | * |
| 240 | * \param P Destination point |
| 241 | * \param Q Source point |
| 242 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 243 | * \return 0 if successful, |
| 244 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 245 | */ |
| 246 | int ecp_copy( ecp_point *P, const ecp_point *Q ); |
| 247 | |
| 248 | /** |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 249 | * \brief Copy the contents of a group object |
| 250 | * |
| 251 | * \param dst Destination group |
| 252 | * \param src Source group |
| 253 | * |
| 254 | * \return 0 if successful, |
| 255 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
| 256 | */ |
| 257 | int ecp_group_copy( ecp_group *dst, const ecp_group *src ); |
| 258 | |
| 259 | /** |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 260 | * \brief Set a point to zero |
| 261 | * |
| 262 | * \param pt Destination point |
| 263 | * |
| 264 | * \return 0 if successful, |
| 265 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
| 266 | */ |
| 267 | int ecp_set_zero( ecp_point *pt ); |
| 268 | |
| 269 | /** |
| 270 | * \brief Tell if a point is zero |
| 271 | * |
| 272 | * \param pt Point to test |
| 273 | * |
| 274 | * \return 1 if point is zero, 0 otherwise |
| 275 | */ |
| 276 | int ecp_is_zero( ecp_point *pt ); |
| 277 | |
| 278 | /** |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 279 | * \brief Import a non-zero point from two ASCII strings |
| 280 | * |
| 281 | * \param P Destination point |
| 282 | * \param radix Input numeric base |
| 283 | * \param x First affine coordinate as a null-terminated string |
| 284 | * \param y Second affine coordinate as a null-terminated string |
| 285 | * |
| 286 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
| 287 | */ |
| 288 | int ecp_point_read_string( ecp_point *P, int radix, |
| 289 | const char *x, const char *y ); |
| 290 | |
| 291 | /** |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 292 | * \brief Export a point into unsigned binary data |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 293 | * |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 294 | * \param grp Group to which the point should belong |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 295 | * \param P Point to export |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 296 | * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 297 | * \param olen Length of the actual output |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 298 | * \param buf Output buffer |
| 299 | * \param buflen Length of the output buffer |
| 300 | * |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 301 | * \return 0 if successful, |
| 302 | * or POLARSSL_ERR_ECP_BAD_INPUT_DATA |
| 303 | * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 304 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 305 | int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P, |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 306 | int format, size_t *olen, |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 307 | unsigned char *buf, size_t buflen ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 308 | |
| 309 | /** |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 310 | * \brief Import a point from unsigned binary data |
| 311 | * |
| 312 | * \param grp Group to which the point should belong |
| 313 | * \param P Point to import |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 314 | * \param buf Input buffer |
| 315 | * \param ilen Actual length of input |
| 316 | * |
| 317 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 318 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 319 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
| 320 | * |
| 321 | * \note This function does NOT check that the point actually |
| 322 | * belongs to the given group, see ecp_check_pubkey() for |
| 323 | * that. |
| 324 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 325 | int ecp_point_read_binary( const ecp_group *grp, ecp_point *P, |
| 326 | const unsigned char *buf, size_t ilen ); |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 327 | |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 328 | /** |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 329 | * \brief Import a point from a TLS ECPoint record |
| 330 | * |
| 331 | * \param grp ECP group used |
| 332 | * \param pt Destination point |
| 333 | * \param buf $(Start of input buffer) |
| 334 | * \param len Buffer length |
| 335 | * |
| 336 | * \return O if successful, |
| 337 | * POLARSSL_ERR_MPI_XXX if initialization failed |
| 338 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid |
| 339 | */ |
| 340 | int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt, |
| 341 | const unsigned char **buf, size_t len ); |
| 342 | |
| 343 | /** |
| 344 | * \brief Export a point as a TLS ECPoint record |
| 345 | * |
| 346 | * \param grp ECP group used |
| 347 | * \param pt Point to export |
| 348 | * \param format Export format |
| 349 | * \param olen length of data written |
| 350 | * \param buf Buffer to write to |
| 351 | * \param blen Buffer length |
| 352 | * |
| 353 | * \return 0 if successful, |
| 354 | * or POLARSSL_ERR_ECP_BAD_INPUT_DATA |
| 355 | * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL |
| 356 | */ |
| 357 | int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt, |
| 358 | int format, size_t *olen, |
| 359 | unsigned char *buf, size_t blen ); |
| 360 | |
| 361 | /** |
| 362 | * \brief Import an ECP group from null-terminated ASCII strings |
| 363 | * |
| 364 | * \param grp Destination group |
| 365 | * \param radix Input numeric base |
| 366 | * \param p Prime modulus of the base field |
| 367 | * \param b Constant term in the equation |
| 368 | * \param gx The generator's X coordinate |
| 369 | * \param gy The generator's Y coordinate |
| 370 | * \param n The generator's order |
| 371 | * |
| 372 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
| 373 | * |
| 374 | * \note Sets all fields except modp. |
| 375 | */ |
| 376 | int ecp_group_read_string( ecp_group *grp, int radix, |
| 377 | const char *p, const char *b, |
| 378 | const char *gx, const char *gy, const char *n); |
| 379 | |
| 380 | /** |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 381 | * \brief Set a group using well-known domain parameters |
| 382 | * |
| 383 | * \param grp Destination group |
| 384 | * \param index Index in the list of well-known domain parameters |
| 385 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 386 | * \return O if successful, |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 387 | * POLARSSL_ERR_MPI_XXX if initialization failed |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 388 | * POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 389 | * |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 390 | * \note Index should be a value of RFC 4492's enum NamdeCurve, |
| 391 | * possibly in the form of a POLARSSL_ECP_DP_XXX macro. |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 392 | */ |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 393 | int ecp_use_known_dp( ecp_group *grp, ecp_group_id index ); |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 394 | |
| 395 | /** |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 396 | * \brief Set a group from a TLS ECParameters record |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 397 | * |
| 398 | * \param grp Destination group |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 399 | * \param buf &(Start of input buffer) |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 400 | * \param len Buffer length |
| 401 | * |
| 402 | * \return O if successful, |
| 403 | * POLARSSL_ERR_MPI_XXX if initialization failed |
| 404 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid |
| 405 | */ |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 406 | int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len ); |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 407 | |
| 408 | /** |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 409 | * \brief Write the TLS ECParameters record for a group |
| 410 | * |
| 411 | * \param grp ECP group used |
| 412 | * \param olen Number of bytes actually written |
| 413 | * \param buf Buffer to write to |
| 414 | * \param blen Buffer length |
| 415 | * |
| 416 | * \return 0 if successful, |
| 417 | * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL |
| 418 | */ |
| 419 | int ecp_tls_write_group( const ecp_group *grp, size_t *olen, |
| 420 | unsigned char *buf, size_t blen ); |
| 421 | |
| 422 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 423 | * \brief Addition: R = P + Q |
| 424 | * |
| 425 | * \param grp ECP group |
| 426 | * \param R Destination point |
| 427 | * \param P Left-hand point |
| 428 | * \param Q Right-hand point |
| 429 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 430 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 431 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 432 | */ |
| 433 | int ecp_add( const ecp_group *grp, ecp_point *R, |
| 434 | const ecp_point *P, const ecp_point *Q ); |
| 435 | |
| 436 | /** |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 437 | * \brief Subtraction: R = P - Q |
| 438 | * |
| 439 | * \param grp ECP group |
| 440 | * \param R Destination point |
| 441 | * \param P Left-hand point |
| 442 | * \param Q Right-hand point |
| 443 | * |
| 444 | * \return 0 if successful, |
| 445 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
| 446 | */ |
| 447 | int ecp_sub( const ecp_group *grp, ecp_point *R, |
| 448 | const ecp_point *P, const ecp_point *Q ); |
| 449 | |
| 450 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 451 | * \brief Multiplication by an integer: R = m * P |
Paul Bakker | 6838bd1 | 2013-09-30 13:56:38 +0200 | [diff] [blame] | 452 | * (Not thread-safe to use same group in multiple threads) |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 453 | * |
| 454 | * \param grp ECP group |
| 455 | * \param R Destination point |
| 456 | * \param m Integer by which to multiply |
| 457 | * \param P Point to multiply |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 458 | * \param f_rng RNG function (see notes) |
| 459 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 460 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 461 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | ff27b7c | 2013-11-21 09:28:03 +0100 | [diff] [blame] | 462 | * POLARSSL_ERR_ECP_INVALID_KEY if m is not a valid privkey |
| 463 | * or P is not a valid pubkey, |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 464 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 465 | * |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 466 | * \note In order to prevent simple timing attacks, this function |
| 467 | * executes a constant number of operations (that is, point |
| 468 | * doubling and addition of distinct points) for random m in |
| 469 | * the allowed range. |
| 470 | * |
Manuel Pégourié-Gonnard | ff27b7c | 2013-11-21 09:28:03 +0100 | [diff] [blame] | 471 | * \note If f_rng is not NULL, it is used to randomize intermediate |
| 472 | * results in order to prevent potential attacks targetting |
| 473 | * these results. It is recommended to always provide a |
| 474 | * non-NULL f_rng (the overhead is negligible). |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 475 | */ |
Manuel Pégourié-Gonnard | 09ceaf4 | 2013-11-20 23:06:14 +0100 | [diff] [blame] | 476 | int ecp_mul( ecp_group *grp, ecp_point *R, |
| 477 | const mpi *m, const ecp_point *P, |
| 478 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 479 | |
| 480 | /** |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 481 | * \brief Check that a point is a valid public key on this curve |
| 482 | * |
| 483 | * \param grp Curve/group the point should belong to |
| 484 | * \param pt Point to check |
| 485 | * |
| 486 | * \return 0 if point is a valid public key, |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 487 | * POLARSSL_ERR_ECP_INVALID_KEY otherwise. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 488 | * |
| 489 | * \note This function only checks the point is non-zero, has valid |
| 490 | * coordinates and lies on the curve, but not that it is |
| 491 | * indeed a multiple of G. This is additional check is more |
| 492 | * expensive, isn't required by standards, and shouldn't be |
| 493 | * necessary if the group used has a small cofactor. In |
| 494 | * particular, it is useless for the NIST groups which all |
| 495 | * have a cofactor of 1. |
| 496 | * |
| 497 | * \note Uses bare components rather than an ecp_keypair structure |
| 498 | * in order to ease use with other structures such as |
| 499 | * ecdh_context of ecdsa_context. |
| 500 | */ |
| 501 | int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt ); |
| 502 | |
| 503 | /** |
| 504 | * \brief Check that an mpi is a valid private key for this curve |
| 505 | * |
| 506 | * \param grp Group used |
| 507 | * \param d Integer to check |
| 508 | * |
| 509 | * \return 0 if point is a valid private key, |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 510 | * POLARSSL_ERR_ECP_INVALID_KEY otherwise. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 511 | * |
| 512 | * \note Uses bare components rather than an ecp_keypair structure |
| 513 | * in order to ease use with other structures such as |
| 514 | * ecdh_context of ecdsa_context. |
| 515 | */ |
Manuel Pégourié-Gonnard | de44a4a | 2013-07-09 16:05:52 +0200 | [diff] [blame] | 516 | int ecp_check_privkey( const ecp_group *grp, const mpi *d ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 517 | |
| 518 | /** |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 519 | * \brief Generate a keypair |
| 520 | * |
| 521 | * \param grp ECP group |
| 522 | * \param d Destination MPI (secret part) |
| 523 | * \param Q Destination point (public part) |
| 524 | * \param f_rng RNG function |
| 525 | * \param p_rng RNG parameter |
| 526 | * |
| 527 | * \return 0 if successful, |
| 528 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 529 | * |
| 530 | * \note Uses bare components rather than an ecp_keypair structure |
| 531 | * in order to ease use with other structures such as |
| 532 | * ecdh_context of ecdsa_context. |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 533 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 534 | int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q, |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 535 | int (*f_rng)(void *, unsigned char *, size_t), |
| 536 | void *p_rng ); |
| 537 | |
| 538 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 539 | * \brief Checkup routine |
| 540 | * |
| 541 | * \return 0 if successful, or 1 if the test failed |
| 542 | */ |
| 543 | int ecp_self_test( int verbose ); |
| 544 | |
| 545 | #ifdef __cplusplus |
| 546 | } |
| 547 | #endif |
| 548 | |
| 549 | #endif |