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