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 | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 42 | #define POLARSSL_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< Signature is valid but shorter than the user-supplied length. */ |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 43 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 48 | /** |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 49 | * Domain parameters (curve, subgroup and generator) identifiers. |
| 50 | * |
| 51 | * Only curves over prime fields are supported. |
| 52 | * |
| 53 | * \warning This library does not support validation of arbitrary domain |
| 54 | * parameters. Therefore, only well-known domain parameters from trusted |
| 55 | * sources should be used. See ecp_use_known_dp(). |
| 56 | */ |
| 57 | typedef enum |
| 58 | { |
| 59 | POLARSSL_ECP_DP_NONE = 0, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 60 | POLARSSL_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */ |
| 61 | POLARSSL_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */ |
| 62 | POLARSSL_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */ |
| 63 | POLARSSL_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */ |
| 64 | POLARSSL_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */ |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 65 | POLARSSL_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */ |
| 66 | POLARSSL_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */ |
| 67 | POLARSSL_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */ |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 68 | POLARSSL_ECP_DP_M221, /*!< (not implemented yet) */ |
| 69 | POLARSSL_ECP_DP_M255, /*!< Curve25519 */ |
| 70 | POLARSSL_ECP_DP_M383, /*!< (not implemented yet) */ |
| 71 | POLARSSL_ECP_DP_M511, /*!< (not implemented yet) */ |
Manuel Pégourié-Gonnard | ad3fab6 | 2014-01-28 20:05:16 +0100 | [diff] [blame] | 72 | POLARSSL_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */ |
| 73 | POLARSSL_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */ |
| 74 | POLARSSL_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */ |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 75 | } ecp_group_id; |
| 76 | |
| 77 | /** |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 78 | * Number of supported curves (plus one for NONE). |
| 79 | * |
| 80 | * (Montgomery curves excluded for now.) |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 81 | */ |
Manuel Pégourié-Gonnard | 9bcff39 | 2014-01-10 18:26:48 +0100 | [diff] [blame] | 82 | #define POLARSSL_ECP_DP_MAX 12 |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 83 | |
| 84 | /** |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 85 | * Curve information for use by other modules |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 86 | */ |
| 87 | typedef struct |
| 88 | { |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 89 | ecp_group_id grp_id; /*!< Internal identifier */ |
| 90 | uint16_t tls_id; /*!< TLS NamedCurve identifier */ |
| 91 | uint16_t size; /*!< Curve size in bits */ |
| 92 | const char *name; /*!< Human-friendly name */ |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 93 | } ecp_curve_info; |
| 94 | |
| 95 | /** |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 96 | * \brief ECP point structure (jacobian coordinates) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 97 | * |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 98 | * \note All functions expect and return points satisfying |
| 99 | * the following condition: Z == 0 or Z == 1. (Other |
| 100 | * values of Z are used by internal functions only.) |
| 101 | * The point is zero, or "at infinity", if Z == 0. |
| 102 | * Otherwise, X and Y are its standard (affine) coordinates. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 103 | */ |
| 104 | typedef struct |
| 105 | { |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 106 | mpi X; /*!< the point's X coordinate */ |
| 107 | mpi Y; /*!< the point's Y coordinate */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 108 | mpi Z; /*!< the point's Z coordinate */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 109 | } |
| 110 | ecp_point; |
| 111 | |
| 112 | /** |
| 113 | * \brief ECP group structure |
| 114 | * |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 115 | * We consider two types of curves equations: |
| 116 | * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492) |
| 117 | * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (M255 + draft) |
| 118 | * In both cases, a generator G for a prime-order subgroup is fixed. In the |
| 119 | * short weierstrass, this subgroup is actually the whole curve, and its |
| 120 | * cardinal is denoted by N. |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 121 | * |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 122 | * In the case of Short Weierstrass curves, our code requires that N is an odd |
| 123 | * prime. (Use odd in ecp_mul() and prime in ecdsa_sign() for blinding.) |
| 124 | * |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 125 | * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is |
Paul Bakker | 75342a6 | 2014-04-08 17:35:40 +0200 | [diff] [blame] | 126 | * the quantity actually used in the formulas. Also, nbits is not the size of N |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 127 | * but the required size for private keys. |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 128 | * |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 129 | * If modp is NULL, reduction modulo P is done using a generic algorithm. |
| 130 | * Otherwise, it must point to a function that takes an mpi in the range |
| 131 | * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more |
| 132 | * than pbits, so that the integer may be efficiently brought in the 0..P-1 |
| 133 | * range by a few additions or substractions. It must return 0 on success and |
| 134 | * non-zero on failure. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 135 | */ |
| 136 | typedef struct |
| 137 | { |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 138 | ecp_group_id id; /*!< internal group identifier */ |
| 139 | mpi P; /*!< prime modulus of the base field */ |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 140 | mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */ |
| 141 | mpi B; /*!< 1. B in the equation, or 2. unused */ |
| 142 | ecp_point G; /*!< generator of the (sub)group used */ |
| 143 | mpi N; /*!< 1. the order of G, or 2. unused */ |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 144 | size_t pbits; /*!< number of bits in P */ |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 145 | size_t nbits; /*!< number of bits in 1. P, or 2. private keys */ |
Manuel Pégourié-Gonnard | 1f82b04 | 2013-12-06 12:51:50 +0100 | [diff] [blame] | 146 | unsigned int h; /*!< internal: 1 if the constants are static */ |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 147 | int (*modp)(mpi *); /*!< function for fast reduction mod P */ |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 148 | int (*t_pre)(ecp_point *, void *); /*!< unused */ |
| 149 | int (*t_post)(ecp_point *, void *); /*!< unused */ |
| 150 | void *t_data; /*!< unused */ |
| 151 | ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */ |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 152 | size_t T_size; /*!< number for pre-computed points */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 153 | } |
| 154 | ecp_group; |
| 155 | |
| 156 | /** |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 157 | * \brief ECP key pair structure |
| 158 | * |
| 159 | * 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] | 160 | * |
| 161 | * \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] | 162 | */ |
| 163 | typedef struct |
| 164 | { |
| 165 | ecp_group grp; /*!< Elliptic curve and base point */ |
| 166 | mpi d; /*!< our secret value */ |
| 167 | ecp_point Q; /*!< our public value */ |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 168 | } |
| 169 | ecp_keypair; |
| 170 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 171 | /** |
| 172 | * \name SECTION: Module settings |
| 173 | * |
| 174 | * The configuration options you can set for this module are in this section. |
| 175 | * Either change them in config.h or define them on the compiler command line. |
| 176 | * \{ |
| 177 | */ |
| 178 | |
| 179 | #if !defined(POLARSSL_ECP_MAX_BITS) |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 180 | /** |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 181 | * 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] | 182 | */ |
Paul Bakker | e1b665e | 2013-12-11 16:02:58 +0100 | [diff] [blame] | 183 | #define POLARSSL_ECP_MAX_BITS 521 /**< Maximum bit size of groups */ |
| 184 | #endif |
| 185 | |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 186 | #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] | 187 | #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] | 188 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 189 | #if !defined(POLARSSL_ECP_WINDOW_SIZE) |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 190 | /* |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 191 | * Maximum "window" size used for point multiplication. |
| 192 | * Default: 6. |
| 193 | * Minimum value: 2. Maximum value: 7. |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 194 | * |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 195 | * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) ) |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 196 | * points used for point multiplication. This value is directly tied to EC |
| 197 | * peak memory usage, so decreasing it by one should roughly cut memory usage |
| 198 | * by two (if large curves are in use). |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 199 | * |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 200 | * Reduction in size may reduce speed, but larger curves are impacted first. |
| 201 | * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1): |
| 202 | * w-size: 6 5 4 3 2 |
| 203 | * 521 145 141 135 120 97 |
| 204 | * 384 214 209 198 177 146 |
| 205 | * 256 320 320 303 262 226 |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 206 | |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 207 | * 224 475 475 453 398 342 |
| 208 | * 192 640 640 633 587 476 |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 209 | */ |
Paul Bakker | e1b665e | 2013-12-11 16:02:58 +0100 | [diff] [blame] | 210 | #define POLARSSL_ECP_WINDOW_SIZE 6 /**< Maximum window size used */ |
Paul Bakker | db20c10 | 2014-06-17 14:34:44 +0200 | [diff] [blame] | 211 | #endif /* POLARSSL_ECP_WINDOW_SIZE */ |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 212 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 213 | #if !defined(POLARSSL_ECP_FIXED_POINT_OPTIM) |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 214 | /* |
| 215 | * Trade memory for speed on fixed-point multiplication. |
| 216 | * |
| 217 | * This speeds up repeated multiplication of the generator (that is, the |
| 218 | * multiplication in ECDSA signatures, and half of the multiplications in |
| 219 | * ECDSA verification and ECDHE) by a factor roughly 3 to 4. |
| 220 | * |
| 221 | * The cost is increasing EC peak memory usage by a factor roughly 2. |
| 222 | * |
| 223 | * Change this value to 0 to reduce peak memory usage. |
| 224 | */ |
| 225 | #define POLARSSL_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */ |
Paul Bakker | db20c10 | 2014-06-17 14:34:44 +0200 | [diff] [blame] | 226 | #endif /* POLARSSL_ECP_FIXED_POINT_OPTIM */ |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 227 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 228 | /* \} name SECTION: Module settings */ |
| 229 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 230 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 231 | * Point formats, from RFC 4492's enum ECPointFormat |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 232 | */ |
| 233 | #define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */ |
| 234 | #define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */ |
| 235 | |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 236 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 237 | * Some other constants from RFC 4492 |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 238 | */ |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 239 | #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] | 240 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 241 | /** |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 242 | * \brief Get the list of supported curves in order of preferrence |
| 243 | * (full information) |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 244 | * |
| 245 | * \return A statically allocated array, the last entry is 0. |
| 246 | */ |
| 247 | const ecp_curve_info *ecp_curve_list( void ); |
| 248 | |
| 249 | /** |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 250 | * \brief Get the list of supported curves in order of preferrence |
| 251 | * (grp_id only) |
| 252 | * |
| 253 | * \return A statically allocated array, |
| 254 | * terminated with POLARSSL_ECP_DP_NONE. |
| 255 | */ |
| 256 | const ecp_group_id *ecp_grp_id_list( void ); |
| 257 | |
| 258 | /** |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 259 | * \brief Get curve information from an internal group identifier |
| 260 | * |
| 261 | * \param grp_id A POLARSSL_ECP_DP_XXX value |
| 262 | * |
| 263 | * \return The associated curve information or NULL |
| 264 | */ |
| 265 | const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id ); |
| 266 | |
| 267 | /** |
| 268 | * \brief Get curve information from a TLS NamedCurve value |
| 269 | * |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 270 | * \param tls_id A POLARSSL_ECP_DP_XXX value |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 271 | * |
| 272 | * \return The associated curve information or NULL |
| 273 | */ |
| 274 | const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id ); |
| 275 | |
| 276 | /** |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 277 | * \brief Get curve information from a human-readable name |
| 278 | * |
| 279 | * \param name The name |
| 280 | * |
| 281 | * \return The associated curve information or NULL |
| 282 | */ |
| 283 | const ecp_curve_info *ecp_curve_info_from_name( const char *name ); |
| 284 | |
| 285 | /** |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 286 | * \brief Initialize a point (as zero) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 287 | */ |
| 288 | void ecp_point_init( ecp_point *pt ); |
| 289 | |
| 290 | /** |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 291 | * \brief Initialize a group (to something meaningless) |
| 292 | */ |
| 293 | void ecp_group_init( ecp_group *grp ); |
| 294 | |
| 295 | /** |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 296 | * \brief Initialize a key pair (as an invalid one) |
| 297 | */ |
| 298 | void ecp_keypair_init( ecp_keypair *key ); |
| 299 | |
| 300 | /** |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 301 | * \brief Free the components of a point |
| 302 | */ |
| 303 | void ecp_point_free( ecp_point *pt ); |
| 304 | |
| 305 | /** |
| 306 | * \brief Free the components of an ECP group |
| 307 | */ |
| 308 | void ecp_group_free( ecp_group *grp ); |
| 309 | |
| 310 | /** |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 311 | * \brief Free the components of a key pair |
| 312 | */ |
| 313 | void ecp_keypair_free( ecp_keypair *key ); |
| 314 | |
| 315 | /** |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 316 | * \brief Copy the contents of point Q into P |
| 317 | * |
| 318 | * \param P Destination point |
| 319 | * \param Q Source point |
| 320 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 321 | * \return 0 if successful, |
| 322 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 323 | */ |
| 324 | int ecp_copy( ecp_point *P, const ecp_point *Q ); |
| 325 | |
| 326 | /** |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 327 | * \brief Copy the contents of a group object |
| 328 | * |
| 329 | * \param dst Destination group |
| 330 | * \param src Source group |
| 331 | * |
| 332 | * \return 0 if successful, |
| 333 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
| 334 | */ |
| 335 | int ecp_group_copy( ecp_group *dst, const ecp_group *src ); |
| 336 | |
| 337 | /** |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 338 | * \brief Set a point to zero |
| 339 | * |
| 340 | * \param pt Destination point |
| 341 | * |
| 342 | * \return 0 if successful, |
| 343 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
| 344 | */ |
| 345 | int ecp_set_zero( ecp_point *pt ); |
| 346 | |
| 347 | /** |
| 348 | * \brief Tell if a point is zero |
| 349 | * |
| 350 | * \param pt Point to test |
| 351 | * |
| 352 | * \return 1 if point is zero, 0 otherwise |
| 353 | */ |
| 354 | int ecp_is_zero( ecp_point *pt ); |
| 355 | |
| 356 | /** |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 357 | * \brief Import a non-zero point from two ASCII strings |
| 358 | * |
| 359 | * \param P Destination point |
| 360 | * \param radix Input numeric base |
| 361 | * \param x First affine coordinate as a null-terminated string |
| 362 | * \param y Second affine coordinate as a null-terminated string |
| 363 | * |
| 364 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
| 365 | */ |
| 366 | int ecp_point_read_string( ecp_point *P, int radix, |
| 367 | const char *x, const char *y ); |
| 368 | |
| 369 | /** |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 370 | * \brief Export a point into unsigned binary data |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 371 | * |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 372 | * \param grp Group to which the point should belong |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 373 | * \param P Point to export |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 374 | * \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] | 375 | * \param olen Length of the actual output |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 376 | * \param buf Output buffer |
| 377 | * \param buflen Length of the output buffer |
| 378 | * |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 379 | * \return 0 if successful, |
| 380 | * or POLARSSL_ERR_ECP_BAD_INPUT_DATA |
| 381 | * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 382 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 383 | 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] | 384 | int format, size_t *olen, |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 385 | unsigned char *buf, size_t buflen ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 386 | |
| 387 | /** |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 388 | * \brief Import a point from unsigned binary data |
| 389 | * |
| 390 | * \param grp Group to which the point should belong |
| 391 | * \param P Point to import |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 392 | * \param buf Input buffer |
| 393 | * \param ilen Actual length of input |
| 394 | * |
| 395 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 396 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid, |
| 397 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
| 398 | * POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE if the point format |
| 399 | * is not implemented. |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 400 | * |
| 401 | * \note This function does NOT check that the point actually |
| 402 | * belongs to the given group, see ecp_check_pubkey() for |
| 403 | * that. |
| 404 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 405 | int ecp_point_read_binary( const ecp_group *grp, ecp_point *P, |
| 406 | const unsigned char *buf, size_t ilen ); |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 407 | |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 408 | /** |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 409 | * \brief Import a point from a TLS ECPoint record |
| 410 | * |
| 411 | * \param grp ECP group used |
| 412 | * \param pt Destination point |
| 413 | * \param buf $(Start of input buffer) |
| 414 | * \param len Buffer length |
| 415 | * |
| 416 | * \return O if successful, |
| 417 | * POLARSSL_ERR_MPI_XXX if initialization failed |
| 418 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid |
| 419 | */ |
| 420 | int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt, |
| 421 | const unsigned char **buf, size_t len ); |
| 422 | |
| 423 | /** |
| 424 | * \brief Export a point as a TLS ECPoint record |
| 425 | * |
| 426 | * \param grp ECP group used |
| 427 | * \param pt Point to export |
| 428 | * \param format Export format |
| 429 | * \param olen length of data written |
| 430 | * \param buf Buffer to write to |
| 431 | * \param blen Buffer length |
| 432 | * |
| 433 | * \return 0 if successful, |
| 434 | * or POLARSSL_ERR_ECP_BAD_INPUT_DATA |
| 435 | * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL |
| 436 | */ |
| 437 | int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt, |
| 438 | int format, size_t *olen, |
| 439 | unsigned char *buf, size_t blen ); |
| 440 | |
| 441 | /** |
| 442 | * \brief Import an ECP group from null-terminated ASCII strings |
| 443 | * |
| 444 | * \param grp Destination group |
| 445 | * \param radix Input numeric base |
| 446 | * \param p Prime modulus of the base field |
| 447 | * \param b Constant term in the equation |
| 448 | * \param gx The generator's X coordinate |
| 449 | * \param gy The generator's Y coordinate |
| 450 | * \param n The generator's order |
| 451 | * |
| 452 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
| 453 | * |
| 454 | * \note Sets all fields except modp. |
| 455 | */ |
| 456 | int ecp_group_read_string( ecp_group *grp, int radix, |
| 457 | const char *p, const char *b, |
| 458 | const char *gx, const char *gy, const char *n); |
| 459 | |
| 460 | /** |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 461 | * \brief Set a group using well-known domain parameters |
| 462 | * |
| 463 | * \param grp Destination group |
| 464 | * \param index Index in the list of well-known domain parameters |
| 465 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 466 | * \return O if successful, |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 467 | * POLARSSL_ERR_MPI_XXX if initialization failed |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 468 | * POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 469 | * |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 470 | * \note Index should be a value of RFC 4492's enum NamdeCurve, |
| 471 | * 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] | 472 | */ |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 473 | 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] | 474 | |
| 475 | /** |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 476 | * \brief Set a group from a TLS ECParameters record |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 477 | * |
| 478 | * \param grp Destination group |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 479 | * \param buf &(Start of input buffer) |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 480 | * \param len Buffer length |
| 481 | * |
| 482 | * \return O if successful, |
| 483 | * POLARSSL_ERR_MPI_XXX if initialization failed |
| 484 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid |
| 485 | */ |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 486 | 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] | 487 | |
| 488 | /** |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 489 | * \brief Write the TLS ECParameters record for a group |
| 490 | * |
| 491 | * \param grp ECP group used |
| 492 | * \param olen Number of bytes actually written |
| 493 | * \param buf Buffer to write to |
| 494 | * \param blen Buffer length |
| 495 | * |
| 496 | * \return 0 if successful, |
| 497 | * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL |
| 498 | */ |
| 499 | int ecp_tls_write_group( const ecp_group *grp, size_t *olen, |
| 500 | unsigned char *buf, size_t blen ); |
| 501 | |
| 502 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 503 | * \brief Addition: R = P + Q |
| 504 | * |
| 505 | * \param grp ECP group |
| 506 | * \param R Destination point |
| 507 | * \param P Left-hand point |
| 508 | * \param Q Right-hand point |
| 509 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 510 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 511 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 512 | * |
| 513 | * \note This function does not support Montgomery curves, such as |
| 514 | * Curve25519. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 515 | */ |
| 516 | int ecp_add( const ecp_group *grp, ecp_point *R, |
| 517 | const ecp_point *P, const ecp_point *Q ); |
| 518 | |
| 519 | /** |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 520 | * \brief Subtraction: R = P - Q |
| 521 | * |
| 522 | * \param grp ECP group |
| 523 | * \param R Destination point |
| 524 | * \param P Left-hand point |
| 525 | * \param Q Right-hand point |
| 526 | * |
| 527 | * \return 0 if successful, |
| 528 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 529 | * |
| 530 | * \note This function does not support Montgomery curves, such as |
| 531 | * Curve25519. |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 532 | */ |
| 533 | int ecp_sub( const ecp_group *grp, ecp_point *R, |
| 534 | const ecp_point *P, const ecp_point *Q ); |
| 535 | |
| 536 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 537 | * \brief Multiplication by an integer: R = m * P |
Paul Bakker | 6838bd1 | 2013-09-30 13:56:38 +0200 | [diff] [blame] | 538 | * (Not thread-safe to use same group in multiple threads) |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 539 | * |
| 540 | * \param grp ECP group |
| 541 | * \param R Destination point |
| 542 | * \param m Integer by which to multiply |
| 543 | * \param P Point to multiply |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 544 | * \param f_rng RNG function (see notes) |
| 545 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 546 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 547 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | ff27b7c | 2013-11-21 09:28:03 +0100 | [diff] [blame] | 548 | * POLARSSL_ERR_ECP_INVALID_KEY if m is not a valid privkey |
| 549 | * or P is not a valid pubkey, |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 550 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 551 | * |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 552 | * \note In order to prevent timing attacks, this function |
| 553 | * executes the exact same sequence of (base field) |
| 554 | * operations for any valid m. It avoids any if-branch or |
| 555 | * array index depending on the value of m. |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 556 | * |
Manuel Pégourié-Gonnard | ff27b7c | 2013-11-21 09:28:03 +0100 | [diff] [blame] | 557 | * \note If f_rng is not NULL, it is used to randomize intermediate |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 558 | * results in order to prevent potential timing attacks |
Paul Bakker | 75342a6 | 2014-04-08 17:35:40 +0200 | [diff] [blame] | 559 | * targeting these results. It is recommended to always |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 560 | * provide a non-NULL f_rng (the overhead is negligible). |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 561 | */ |
Manuel Pégourié-Gonnard | 09ceaf4 | 2013-11-20 23:06:14 +0100 | [diff] [blame] | 562 | int ecp_mul( ecp_group *grp, ecp_point *R, |
| 563 | const mpi *m, const ecp_point *P, |
| 564 | 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] | 565 | |
| 566 | /** |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 567 | * \brief Check that a point is a valid public key on this curve |
| 568 | * |
| 569 | * \param grp Curve/group the point should belong to |
| 570 | * \param pt Point to check |
| 571 | * |
| 572 | * \return 0 if point is a valid public key, |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 573 | * POLARSSL_ERR_ECP_INVALID_KEY otherwise. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 574 | * |
| 575 | * \note This function only checks the point is non-zero, has valid |
| 576 | * coordinates and lies on the curve, but not that it is |
| 577 | * indeed a multiple of G. This is additional check is more |
| 578 | * expensive, isn't required by standards, and shouldn't be |
| 579 | * necessary if the group used has a small cofactor. In |
| 580 | * particular, it is useless for the NIST groups which all |
| 581 | * have a cofactor of 1. |
| 582 | * |
| 583 | * \note Uses bare components rather than an ecp_keypair structure |
| 584 | * in order to ease use with other structures such as |
| 585 | * ecdh_context of ecdsa_context. |
| 586 | */ |
| 587 | int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt ); |
| 588 | |
| 589 | /** |
| 590 | * \brief Check that an mpi is a valid private key for this curve |
| 591 | * |
| 592 | * \param grp Group used |
| 593 | * \param d Integer to check |
| 594 | * |
| 595 | * \return 0 if point is a valid private key, |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 596 | * POLARSSL_ERR_ECP_INVALID_KEY otherwise. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 597 | * |
| 598 | * \note Uses bare components rather than an ecp_keypair structure |
| 599 | * in order to ease use with other structures such as |
| 600 | * ecdh_context of ecdsa_context. |
| 601 | */ |
Manuel Pégourié-Gonnard | de44a4a | 2013-07-09 16:05:52 +0200 | [diff] [blame] | 602 | 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] | 603 | |
| 604 | /** |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 605 | * \brief Generate a keypair |
| 606 | * |
| 607 | * \param grp ECP group |
| 608 | * \param d Destination MPI (secret part) |
| 609 | * \param Q Destination point (public part) |
| 610 | * \param f_rng RNG function |
| 611 | * \param p_rng RNG parameter |
| 612 | * |
| 613 | * \return 0 if successful, |
| 614 | * 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] | 615 | * |
| 616 | * \note Uses bare components rather than an ecp_keypair structure |
| 617 | * in order to ease use with other structures such as |
| 618 | * ecdh_context of ecdsa_context. |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 619 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 620 | 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] | 621 | int (*f_rng)(void *, unsigned char *, size_t), |
| 622 | void *p_rng ); |
| 623 | |
| 624 | /** |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 625 | * \brief Generate a keypair |
| 626 | * |
| 627 | * \param grp_id ECP group identifier |
| 628 | * \param key Destination keypair |
| 629 | * \param f_rng RNG function |
| 630 | * \param p_rng RNG parameter |
| 631 | * |
| 632 | * \return 0 if successful, |
| 633 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
| 634 | */ |
| 635 | int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key, |
| 636 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 637 | |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame^] | 638 | /** |
| 639 | * \brief Check a public-private key pair |
| 640 | * |
| 641 | * \param pub Keypair structure holding a public key |
| 642 | * \param prv Keypair structure holding a private (plus public) key |
| 643 | * |
| 644 | * \return 0 if successfull (keys are valid and match), or |
| 645 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA, or |
| 646 | * a POLARSSL_ERR_ECP_XXX or POLARSSL_ERR_MPI_XXX code. |
| 647 | */ |
| 648 | int ecp_check_pub_priv( const ecp_keypair *pub, const ecp_keypair *prv ); |
| 649 | |
Manuel Pégourié-Gonnard | 13a1ef8 | 2014-03-27 20:12:44 +0100 | [diff] [blame] | 650 | #if defined(POLARSSL_SELF_TEST) |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 651 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 652 | * \brief Checkup routine |
| 653 | * |
Manuel Pégourié-Gonnard | 13a1ef8 | 2014-03-27 20:12:44 +0100 | [diff] [blame] | 654 | * \return 0 if successful, or 1 if a test failed |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 655 | */ |
| 656 | int ecp_self_test( int verbose ); |
Manuel Pégourié-Gonnard | 13a1ef8 | 2014-03-27 20:12:44 +0100 | [diff] [blame] | 657 | #endif |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 658 | |
| 659 | #ifdef __cplusplus |
| 660 | } |
| 661 | #endif |
| 662 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 663 | #endif /* ecp.h */ |