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 | * |
| 6 | * Copyright (C) 2012, Brainspark B.V. |
| 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_ECP_H |
| 28 | #define POLARSSL_ECP_H |
| 29 | |
| 30 | #include "bignum.h" |
| 31 | |
| 32 | /* |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 33 | * ECP error codes |
| 34 | * |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 35 | * (Only one error code available...) |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 36 | */ |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 37 | #define POLARSSL_ERR_ECP_GENERIC -0x007E /**< Generic ECP error */ |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 38 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 39 | /** |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 40 | * \brief ECP point structure (jacobian coordinates) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 41 | * |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 42 | * \note All functions expect and return points satisfying |
| 43 | * the following condition: Z == 0 or Z == 1. (Other |
| 44 | * values of Z are used by internal functions only.) |
| 45 | * The point is zero, or "at infinity", if Z == 0. |
| 46 | * Otherwise, X and Y are its standard (affine) coordinates. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 47 | */ |
| 48 | typedef struct |
| 49 | { |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 50 | mpi X; /*!< the point's X coordinate */ |
| 51 | mpi Y; /*!< the point's Y coordinate */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 52 | mpi Z; /*!< the point's Z coordinate */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 53 | } |
| 54 | ecp_point; |
| 55 | |
| 56 | /** |
| 57 | * \brief ECP group structure |
| 58 | * |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 59 | * The curves we consider are defined by y^2 = x^3 - 3x + B mod P, |
| 60 | * 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] | 61 | * |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 62 | * 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] | 63 | * |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 64 | * If modp is NULL, reduction modulo P is done using a generic |
| 65 | * algorithm. Otherwise, it must point to a function that takes an mpi |
| 66 | * in the range 0..2^(2*pbits) and transforms it in-place in an integer |
| 67 | * of little more than pbits, so that the integer may be efficiently |
| 68 | * brought in the 0..P range by a few additions or substractions. It |
| 69 | * 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] | 70 | */ |
| 71 | typedef struct |
| 72 | { |
| 73 | mpi P; /*!< prime modulus of the base field */ |
| 74 | mpi B; /*!< constant term in the equation */ |
| 75 | ecp_point G; /*!< generator of the subgroup used */ |
| 76 | mpi N; /*!< the order of G */ |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 77 | size_t pbits; /*!< number of bits in P */ |
| 78 | size_t nbits; /*!< number of bits in N */ |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 79 | int (*modp)(mpi *); /*!< function for fast reduction mod P */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 80 | } |
| 81 | ecp_group; |
| 82 | |
| 83 | /** |
| 84 | * RFC 5114 defines a number of standardized ECP groups for use with TLS. |
| 85 | * |
| 86 | * These also are the NIST-recommended ECP groups, are the random ECP groups |
| 87 | * 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] | 88 | * 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] | 89 | * |
| 90 | * \warning This library does not support validation of arbitrary domain |
| 91 | * parameters. Therefore, only well-known domain parameters from trusted |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 92 | * sources should be used. See ecp_use_known_dp(). |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 93 | */ |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 94 | #define POLARSSL_ECP_DP_SECP192R1 0 |
| 95 | #define POLARSSL_ECP_DP_SECP224R1 1 |
| 96 | #define POLARSSL_ECP_DP_SECP256R1 2 |
| 97 | #define POLARSSL_ECP_DP_SECP384R1 3 |
| 98 | #define POLARSSL_ECP_DP_SECP521R1 4 |
| 99 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 100 | #ifdef __cplusplus |
| 101 | extern "C" { |
| 102 | #endif |
| 103 | |
| 104 | /** |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 105 | * \brief Initialize a point (as zero) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 106 | */ |
| 107 | void ecp_point_init( ecp_point *pt ); |
| 108 | |
| 109 | /** |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 110 | * \brief Initialize a group (to something meaningless) |
| 111 | */ |
| 112 | void ecp_group_init( ecp_group *grp ); |
| 113 | |
| 114 | /** |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 115 | * \brief Free the components of a point |
| 116 | */ |
| 117 | void ecp_point_free( ecp_point *pt ); |
| 118 | |
| 119 | /** |
| 120 | * \brief Free the components of an ECP group |
| 121 | */ |
| 122 | void ecp_group_free( ecp_group *grp ); |
| 123 | |
| 124 | /** |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 125 | * \brief Set a point to zero |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 126 | * |
| 127 | * \return 0 if successful, |
| 128 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 129 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 130 | int ecp_set_zero( ecp_point *pt ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 131 | |
| 132 | /** |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 133 | * \brief Copy the contents of point Q into P |
| 134 | * |
| 135 | * \param P Destination point |
| 136 | * \param Q Source point |
| 137 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 138 | * \return 0 if successful, |
| 139 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 140 | */ |
| 141 | int ecp_copy( ecp_point *P, const ecp_point *Q ); |
| 142 | |
| 143 | /** |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 144 | * \brief Import a non-zero point from two ASCII strings |
| 145 | * |
| 146 | * \param P Destination point |
| 147 | * \param radix Input numeric base |
| 148 | * \param x First affine coordinate as a null-terminated string |
| 149 | * \param y Second affine coordinate as a null-terminated string |
| 150 | * |
| 151 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
| 152 | */ |
| 153 | int ecp_point_read_string( ecp_point *P, int radix, |
| 154 | const char *x, const char *y ); |
| 155 | |
| 156 | /** |
| 157 | * \brief Import an ECP group from null-terminated ASCII strings |
| 158 | * |
| 159 | * \param grp Destination group |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 160 | * \param radix Input numeric base |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 161 | * \param p Prime modulus of the base field |
| 162 | * \param b Constant term in the equation |
| 163 | * \param gx The generator's X coordinate |
| 164 | * \param gy The generator's Y coordinate |
| 165 | * \param n The generator's order |
| 166 | * |
| 167 | * \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] | 168 | * |
| 169 | * \note Sets all fields except modp. |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 170 | */ |
| 171 | int ecp_group_read_string( ecp_group *grp, int radix, |
| 172 | const char *p, const char *b, |
| 173 | const char *gx, const char *gy, const char *n); |
| 174 | |
| 175 | /** |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 176 | * \brief Set a group using well-known domain parameters |
| 177 | * |
| 178 | * \param grp Destination group |
| 179 | * \param index Index in the list of well-known domain parameters |
| 180 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 181 | * \return O if successful, |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 182 | * POLARSSL_ERR_MPI_XXX if initialization failed |
| 183 | * POLARSSL_ERR_ECP_GENERIC if index is out of range |
| 184 | * |
| 185 | * \note Index should be a POLARSSL_ECP_DP_XXX macro. |
| 186 | */ |
| 187 | int ecp_use_known_dp( ecp_group *grp, size_t index ); |
| 188 | |
| 189 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 190 | * \brief Addition: R = P + Q |
| 191 | * |
| 192 | * \param grp ECP group |
| 193 | * \param R Destination point |
| 194 | * \param P Left-hand point |
| 195 | * \param Q Right-hand point |
| 196 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 197 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 198 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 199 | */ |
| 200 | int ecp_add( const ecp_group *grp, ecp_point *R, |
| 201 | const ecp_point *P, const ecp_point *Q ); |
| 202 | |
| 203 | /** |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame^] | 204 | * \brief Subtraction: R = P - Q |
| 205 | * |
| 206 | * \param grp ECP group |
| 207 | * \param R Destination point |
| 208 | * \param P Left-hand point |
| 209 | * \param Q Right-hand point |
| 210 | * |
| 211 | * \return 0 if successful, |
| 212 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
| 213 | */ |
| 214 | int ecp_sub( const ecp_group *grp, ecp_point *R, |
| 215 | const ecp_point *P, const ecp_point *Q ); |
| 216 | |
| 217 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 218 | * \brief Multiplication by an integer: R = m * P |
| 219 | * |
| 220 | * \param grp ECP group |
| 221 | * \param R Destination point |
| 222 | * \param m Integer by which to multiply |
| 223 | * \param P Point to multiply |
| 224 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 225 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 226 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 4bdd47d | 2012-11-11 14:33:59 +0100 | [diff] [blame] | 227 | * POLARSSL_ERR_ECP_GENERIC if m < 0 |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 228 | */ |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 229 | int ecp_mul( const ecp_group *grp, ecp_point *R, |
| 230 | const mpi *m, const ecp_point *P ); |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 231 | |
| 232 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 233 | * \brief Checkup routine |
| 234 | * |
| 235 | * \return 0 if successful, or 1 if the test failed |
| 236 | */ |
| 237 | int ecp_self_test( int verbose ); |
| 238 | |
| 239 | #ifdef __cplusplus |
| 240 | } |
| 241 | #endif |
| 242 | |
| 243 | #endif |