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 | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 30 | #include "polarssl/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. */ |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 36 | #define POLARSSL_ERR_ECP_BUFFER_TOO_SMALL -0x4F80 /**< The buffer is too small to write to. */ |
Paul Bakker | cf4365f | 2013-01-16 17:00:43 +0100 | [diff] [blame] | 37 | #define POLARSSL_ERR_ECP_GENERIC -0x4F00 /**< Generic ECP error */ |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 38 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame^] | 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 43 | /** |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 44 | * \brief ECP point structure (jacobian coordinates) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 45 | * |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 46 | * \note All functions expect and return points satisfying |
| 47 | * the following condition: Z == 0 or Z == 1. (Other |
| 48 | * values of Z are used by internal functions only.) |
| 49 | * The point is zero, or "at infinity", if Z == 0. |
| 50 | * Otherwise, X and Y are its standard (affine) coordinates. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 51 | */ |
| 52 | typedef struct |
| 53 | { |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 54 | mpi X; /*!< the point's X coordinate */ |
| 55 | mpi Y; /*!< the point's Y coordinate */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 56 | mpi Z; /*!< the point's Z coordinate */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 57 | } |
| 58 | ecp_point; |
| 59 | |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 60 | /* |
| 61 | * RFC 4492 defines an enum NamedCurve with two-bytes values |
| 62 | */ |
| 63 | typedef uint16_t ecp_group_id; |
| 64 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 65 | /** |
| 66 | * \brief ECP group structure |
| 67 | * |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 68 | * The curves we consider are defined by y^2 = x^3 - 3x + B mod P, |
| 69 | * 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] | 70 | * |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 71 | * 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] | 72 | * |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 73 | * If modp is NULL, reduction modulo P is done using a generic |
| 74 | * algorithm. Otherwise, it must point to a function that takes an mpi |
| 75 | * in the range 0..2^(2*pbits) and transforms it in-place in an integer |
| 76 | * of little more than pbits, so that the integer may be efficiently |
| 77 | * brought in the 0..P range by a few additions or substractions. It |
| 78 | * must return 0 on success and a POLARSSL_ERR_ECP_XXX error on failure. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 79 | */ |
| 80 | typedef struct |
| 81 | { |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 82 | ecp_group_id id; /*!< RFC 4492 group ID */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 83 | mpi P; /*!< prime modulus of the base field */ |
| 84 | mpi B; /*!< constant term in the equation */ |
| 85 | ecp_point G; /*!< generator of the subgroup used */ |
| 86 | mpi N; /*!< the order of G */ |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 87 | size_t pbits; /*!< number of bits in P */ |
| 88 | size_t nbits; /*!< number of bits in N */ |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 89 | int (*modp)(mpi *); /*!< function for fast reduction mod P */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 90 | } |
| 91 | ecp_group; |
| 92 | |
| 93 | /** |
| 94 | * RFC 5114 defines a number of standardized ECP groups for use with TLS. |
| 95 | * |
| 96 | * These also are the NIST-recommended ECP groups, are the random ECP groups |
| 97 | * recommended by SECG, and include the two groups used by NSA Suite B. |
Manuel Pégourié-Gonnard | d7e4570 | 2012-10-31 18:57:05 +0100 | [diff] [blame] | 98 | * There are known as secpLLLr1 with LLL = 192, 224, 256, 384, 521. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 99 | * |
| 100 | * \warning This library does not support validation of arbitrary domain |
| 101 | * parameters. Therefore, only well-known domain parameters from trusted |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 102 | * sources should be used. See ecp_use_known_dp(). |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 103 | * |
| 104 | * \note The values are taken from RFC 4492's enum NamedCurve. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 105 | */ |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 106 | #define POLARSSL_ECP_DP_SECP192R1 19 |
| 107 | #define POLARSSL_ECP_DP_SECP224R1 21 |
| 108 | #define POLARSSL_ECP_DP_SECP256R1 23 |
| 109 | #define POLARSSL_ECP_DP_SECP384R1 24 |
| 110 | #define POLARSSL_ECP_DP_SECP521R1 25 |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 111 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 112 | /** |
| 113 | * Maximum bit size of the groups (that is, of N) |
| 114 | */ |
| 115 | #define POLARSSL_ECP_MAX_N_BITS 521 |
| 116 | |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 117 | /* |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 118 | * Maximum window size (actually, NAF width) used for point multipliation. |
| 119 | * Default: 7. |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 120 | * Minimum value: 2. Maximum value: 8. |
| 121 | * |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 122 | * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) ) |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 123 | * points used for point multiplication, so at most 64 by default. |
| 124 | * In practice, most curves will use less precomputed points. |
| 125 | * |
| 126 | * Reduction in size may reduce speed for big curves. |
| 127 | */ |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 128 | #define POLARSSL_ECP_WINDOW_SIZE 7 /**< Maximum NAF width used. */ |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 129 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 130 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 131 | * Point formats, from RFC 4492's enum ECPointFormat |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 132 | */ |
| 133 | #define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */ |
| 134 | #define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */ |
| 135 | |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 136 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 137 | * Some other constants from RFC 4492 |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 138 | */ |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 139 | #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] | 140 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 141 | /** |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 142 | * \brief Initialize a point (as zero) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 143 | */ |
| 144 | void ecp_point_init( ecp_point *pt ); |
| 145 | |
| 146 | /** |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 147 | * \brief Initialize a group (to something meaningless) |
| 148 | */ |
| 149 | void ecp_group_init( ecp_group *grp ); |
| 150 | |
| 151 | /** |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 152 | * \brief Free the components of a point |
| 153 | */ |
| 154 | void ecp_point_free( ecp_point *pt ); |
| 155 | |
| 156 | /** |
| 157 | * \brief Free the components of an ECP group |
| 158 | */ |
| 159 | void ecp_group_free( ecp_group *grp ); |
| 160 | |
| 161 | /** |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 162 | * \brief Set a point to zero |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 163 | * |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 164 | * \param pt Destination point |
| 165 | * |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 166 | * \return 0 if successful, |
| 167 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 168 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 169 | int ecp_set_zero( ecp_point *pt ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 170 | |
| 171 | /** |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 172 | * \brief Tell if a point is zero |
| 173 | * |
| 174 | * \param pt Point to test |
| 175 | * |
| 176 | * \return 1 if point is zero, 0 otherwise |
| 177 | */ |
| 178 | int ecp_is_zero( ecp_point *pt ); |
| 179 | |
| 180 | /** |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 181 | * \brief Copy the contents of point Q into P |
| 182 | * |
| 183 | * \param P Destination point |
| 184 | * \param Q Source point |
| 185 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 186 | * \return 0 if successful, |
| 187 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 188 | */ |
| 189 | int ecp_copy( ecp_point *P, const ecp_point *Q ); |
| 190 | |
| 191 | /** |
Manuel Pégourié-Gonnard | 1c33057 | 2012-11-24 12:05:44 +0100 | [diff] [blame] | 192 | * \brief Check that a point is a valid public key on this curve |
| 193 | * |
| 194 | * \param grp Curve/group the point should belong to |
| 195 | * \param pt Point to check |
| 196 | * |
| 197 | * \return 0 if point is a valid public key, |
| 198 | * POLARSSL_ERR_ECP_GENERIC otherwise. |
| 199 | * |
| 200 | * \note This function only checks the point is non-zero, has valid |
| 201 | * coordinates and lies on the curve, but not that it is |
| 202 | * indeed a multiple of G. This is additional check is more |
| 203 | * expensive, isn't required by standards, and shouldn't be |
| 204 | * necessary if the group used has a small cofactor. In |
| 205 | * particular, it is useless for the NIST groups which all |
| 206 | * have a cofactor of 1. |
| 207 | */ |
| 208 | int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt ); |
| 209 | |
| 210 | /** |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 211 | * \brief Import a non-zero point from two ASCII strings |
| 212 | * |
| 213 | * \param P Destination point |
| 214 | * \param radix Input numeric base |
| 215 | * \param x First affine coordinate as a null-terminated string |
| 216 | * \param y Second affine coordinate as a null-terminated string |
| 217 | * |
| 218 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
| 219 | */ |
| 220 | int ecp_point_read_string( ecp_point *P, int radix, |
| 221 | const char *x, const char *y ); |
| 222 | |
| 223 | /** |
| 224 | * \brief Import an ECP group from null-terminated ASCII strings |
| 225 | * |
| 226 | * \param grp Destination group |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 227 | * \param radix Input numeric base |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 228 | * \param p Prime modulus of the base field |
| 229 | * \param b Constant term in the equation |
| 230 | * \param gx The generator's X coordinate |
| 231 | * \param gy The generator's Y coordinate |
| 232 | * \param n The generator's order |
| 233 | * |
| 234 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 235 | * |
| 236 | * \note Sets all fields except modp. |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 237 | */ |
| 238 | int ecp_group_read_string( ecp_group *grp, int radix, |
| 239 | const char *p, const char *b, |
| 240 | const char *gx, const char *gy, const char *n); |
| 241 | |
| 242 | /** |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 243 | * \brief Export a point into unsigned binary data |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 244 | * |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 245 | * \param grp Group to which the point should belong |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 246 | * \param P Point to export |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 247 | * \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] | 248 | * \param olen Length of the actual output |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 249 | * \param buf Output buffer |
| 250 | * \param buflen Length of the output buffer |
| 251 | * |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 252 | * \return 0 if successful, |
| 253 | * or POLARSSL_ERR_ECP_BAD_INPUT_DATA |
| 254 | * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 255 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 256 | 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] | 257 | int format, size_t *olen, |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 258 | unsigned char *buf, size_t buflen ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 259 | |
| 260 | /** |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 261 | * \brief Import a point from unsigned binary data |
| 262 | * |
| 263 | * \param grp Group to which the point should belong |
| 264 | * \param P Point to import |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 265 | * \param buf Input buffer |
| 266 | * \param ilen Actual length of input |
| 267 | * |
| 268 | * \return 0 if successful, |
| 269 | * POLARSSL_ERR_ECP_GENERIC if input is invalid |
| 270 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
| 271 | * |
| 272 | * \note This function does NOT check that the point actually |
| 273 | * belongs to the given group, see ecp_check_pubkey() for |
| 274 | * that. |
| 275 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 276 | int ecp_point_read_binary( const ecp_group *grp, ecp_point *P, |
| 277 | const unsigned char *buf, size_t ilen ); |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 278 | |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 279 | /** |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 280 | * \brief Set a group using well-known domain parameters |
| 281 | * |
| 282 | * \param grp Destination group |
| 283 | * \param index Index in the list of well-known domain parameters |
| 284 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 285 | * \return O if successful, |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 286 | * POLARSSL_ERR_MPI_XXX if initialization failed |
| 287 | * POLARSSL_ERR_ECP_GENERIC if index is out of range |
| 288 | * |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 289 | * \note Index should be a value of RFC 4492's enum NamdeCurve, |
| 290 | * 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] | 291 | */ |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 292 | int ecp_use_known_dp( ecp_group *grp, ecp_group_id id ); |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 293 | |
| 294 | /** |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 295 | * \brief Set a group from a TLS ECParameters record |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 296 | * |
| 297 | * \param grp Destination group |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 298 | * \param buf &(Start of input buffer) |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 299 | * \param len Buffer length |
| 300 | * |
| 301 | * \return O if successful, |
| 302 | * POLARSSL_ERR_MPI_XXX if initialization failed |
| 303 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid |
| 304 | */ |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 305 | 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] | 306 | |
| 307 | /** |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 308 | * \brief Write the TLS ECParameters record for a group |
| 309 | * |
| 310 | * \param grp ECP group used |
| 311 | * \param olen Number of bytes actually written |
| 312 | * \param buf Buffer to write to |
| 313 | * \param blen Buffer length |
| 314 | * |
| 315 | * \return 0 if successful, |
| 316 | * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL |
| 317 | */ |
| 318 | int ecp_tls_write_group( const ecp_group *grp, size_t *olen, |
| 319 | unsigned char *buf, size_t blen ); |
| 320 | |
| 321 | /** |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 322 | * \brief Import a point from a TLS ECPoint record |
| 323 | * |
| 324 | * \param grp ECP group used |
| 325 | * \param pt Destination point |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 326 | * \param buf $(Start of input buffer) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 327 | * \param len Buffer length |
| 328 | * |
| 329 | * \return O if successful, |
| 330 | * POLARSSL_ERR_MPI_XXX if initialization failed |
| 331 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid |
| 332 | */ |
| 333 | int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt, |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 334 | const unsigned char **buf, size_t len ); |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * \brief Export a point as a TLS ECPoint record |
| 338 | * |
| 339 | * \param grp ECP group used |
| 340 | * \param pt Point to export |
| 341 | * \param format Export format |
| 342 | * \param buf Buffer to write to |
| 343 | * \param len Buffer length |
| 344 | * |
| 345 | * \return 0 if successful, |
| 346 | * or POLARSSL_ERR_ECP_BAD_INPUT_DATA |
| 347 | * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL |
| 348 | */ |
| 349 | int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt, |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 350 | int format, size_t *olen, |
| 351 | unsigned char *buf, size_t blen ); |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 352 | |
| 353 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 354 | * \brief Addition: R = P + Q |
| 355 | * |
| 356 | * \param grp ECP group |
| 357 | * \param R Destination point |
| 358 | * \param P Left-hand point |
| 359 | * \param Q Right-hand point |
| 360 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 361 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 362 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 363 | */ |
| 364 | int ecp_add( const ecp_group *grp, ecp_point *R, |
| 365 | const ecp_point *P, const ecp_point *Q ); |
| 366 | |
| 367 | /** |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 368 | * \brief Subtraction: R = P - Q |
| 369 | * |
| 370 | * \param grp ECP group |
| 371 | * \param R Destination point |
| 372 | * \param P Left-hand point |
| 373 | * \param Q Right-hand point |
| 374 | * |
| 375 | * \return 0 if successful, |
| 376 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
| 377 | */ |
| 378 | int ecp_sub( const ecp_group *grp, ecp_point *R, |
| 379 | const ecp_point *P, const ecp_point *Q ); |
| 380 | |
| 381 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 382 | * \brief Multiplication by an integer: R = m * P |
| 383 | * |
| 384 | * \param grp ECP group |
| 385 | * \param R Destination point |
| 386 | * \param m Integer by which to multiply |
| 387 | * \param P Point to multiply |
| 388 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 389 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 390 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 391 | * POLARSSL_ERR_ECP_GENERIC if m < 0 of m has greater bit |
| 392 | * length than N, the number of points in the group. |
| 393 | * |
| 394 | * \note This function executes a constant number of operations |
| 395 | * for random m in the allowed range. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 396 | */ |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 397 | int ecp_mul( const ecp_group *grp, ecp_point *R, |
| 398 | const mpi *m, const ecp_point *P ); |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 399 | |
| 400 | /** |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 401 | * \brief Generate a keypair |
| 402 | * |
| 403 | * \param grp ECP group |
| 404 | * \param d Destination MPI (secret part) |
| 405 | * \param Q Destination point (public part) |
| 406 | * \param f_rng RNG function |
| 407 | * \param p_rng RNG parameter |
| 408 | * |
| 409 | * \return 0 if successful, |
| 410 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
| 411 | */ |
| 412 | int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q, |
| 413 | int (*f_rng)(void *, unsigned char *, size_t), |
| 414 | void *p_rng ); |
| 415 | |
| 416 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 417 | * \brief Checkup routine |
| 418 | * |
| 419 | * \return 0 if successful, or 1 if the test failed |
| 420 | */ |
| 421 | int ecp_self_test( int verbose ); |
| 422 | |
| 423 | #ifdef __cplusplus |
| 424 | } |
| 425 | #endif |
| 426 | |
| 427 | #endif |