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 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame^] | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 9 | * |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #ifndef MBEDTLS_ECP_H |
| 25 | #define MBEDTLS_ECP_H |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 27 | #include "bignum.h" |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 28 | |
| 29 | /* |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 30 | * ECP error codes |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 31 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */ |
| 33 | #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */ |
| 34 | #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< Requested curve not available. */ |
| 35 | #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */ |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 36 | #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as (ephemeral) key, failed. */ |
| 38 | #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */ |
| 39 | #define MBEDTLS_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] | 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 | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 46 | * Domain parameters (curve, subgroup and generator) identifiers. |
| 47 | * |
| 48 | * Only curves over prime fields are supported. |
| 49 | * |
| 50 | * \warning This library does not support validation of arbitrary domain |
| 51 | * parameters. Therefore, only well-known domain parameters from trusted |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 52 | * sources should be used. See mbedtls_ecp_group_load(). |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 53 | */ |
| 54 | typedef enum |
| 55 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | MBEDTLS_ECP_DP_NONE = 0, |
| 57 | MBEDTLS_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */ |
| 58 | MBEDTLS_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */ |
| 59 | MBEDTLS_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */ |
| 60 | MBEDTLS_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */ |
| 61 | MBEDTLS_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */ |
| 62 | MBEDTLS_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */ |
| 63 | MBEDTLS_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */ |
| 64 | MBEDTLS_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */ |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 65 | MBEDTLS_ECP_DP_CURVE25519, /*!< Curve25519 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | MBEDTLS_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */ |
| 67 | MBEDTLS_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */ |
| 68 | MBEDTLS_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */ |
| 69 | } mbedtls_ecp_group_id; |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 70 | |
| 71 | /** |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 72 | * Number of supported curves (plus one for NONE). |
| 73 | * |
| 74 | * (Montgomery curves excluded for now.) |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 75 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | #define MBEDTLS_ECP_DP_MAX 12 |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 77 | |
| 78 | /** |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 79 | * Curve information for use by other modules |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 80 | */ |
| 81 | typedef struct |
| 82 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | mbedtls_ecp_group_id grp_id; /*!< Internal identifier */ |
Manuel Pégourié-Gonnard | 797f48a | 2015-06-18 15:45:05 +0200 | [diff] [blame] | 84 | uint16_t tls_id; /*!< TLS NamedCurve identifier */ |
| 85 | uint16_t bit_size; /*!< Curve size in bits */ |
| 86 | const char *name; /*!< Human-friendly name */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | } mbedtls_ecp_curve_info; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 88 | |
| 89 | /** |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 90 | * \brief ECP point structure (jacobian coordinates) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 91 | * |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 92 | * \note All functions expect and return points satisfying |
| 93 | * the following condition: Z == 0 or Z == 1. (Other |
| 94 | * values of Z are used by internal functions only.) |
| 95 | * The point is zero, or "at infinity", if Z == 0. |
| 96 | * Otherwise, X and Y are its standard (affine) coordinates. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 97 | */ |
| 98 | typedef struct |
| 99 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | mbedtls_mpi X; /*!< the point's X coordinate */ |
| 101 | mbedtls_mpi Y; /*!< the point's Y coordinate */ |
| 102 | mbedtls_mpi Z; /*!< the point's Z coordinate */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 103 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | mbedtls_ecp_point; |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * \brief ECP group structure |
| 108 | * |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 109 | * We consider two types of curves equations: |
| 110 | * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492) |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 111 | * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft) |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 112 | * In both cases, a generator G for a prime-order subgroup is fixed. In the |
| 113 | * short weierstrass, this subgroup is actually the whole curve, and its |
| 114 | * cardinal is denoted by N. |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 115 | * |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 116 | * In the case of Short Weierstrass curves, our code requires that N is an odd |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | * prime. (Use odd in mbedtls_ecp_mul() and prime in mbedtls_ecdsa_sign() for blinding.) |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 118 | * |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 119 | * 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] | 120 | * 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] | 121 | * but the required size for private keys. |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 122 | * |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 123 | * If modp is NULL, reduction modulo P is done using a generic algorithm. |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | * Otherwise, it must point to a function that takes an mbedtls_mpi in the range |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 125 | * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more |
| 126 | * than pbits, so that the integer may be efficiently brought in the 0..P-1 |
| 127 | * range by a few additions or substractions. It must return 0 on success and |
| 128 | * non-zero on failure. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 129 | */ |
| 130 | typedef struct |
| 131 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | mbedtls_ecp_group_id id; /*!< internal group identifier */ |
| 133 | mbedtls_mpi P; /*!< prime modulus of the base field */ |
| 134 | mbedtls_mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */ |
| 135 | mbedtls_mpi B; /*!< 1. B in the equation, or 2. unused */ |
| 136 | mbedtls_ecp_point G; /*!< generator of the (sub)group used */ |
| 137 | mbedtls_mpi N; /*!< 1. the order of G, or 2. unused */ |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 138 | size_t pbits; /*!< number of bits in P */ |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 139 | 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] | 140 | unsigned int h; /*!< internal: 1 if the constants are static */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | int (*modp)(mbedtls_mpi *); /*!< function for fast reduction mod P */ |
| 142 | int (*t_pre)(mbedtls_ecp_point *, void *); /*!< unused */ |
| 143 | int (*t_post)(mbedtls_ecp_point *, void *); /*!< unused */ |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 144 | void *t_data; /*!< unused */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | mbedtls_ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */ |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 146 | size_t T_size; /*!< number for pre-computed points */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 147 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | mbedtls_ecp_group; |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 149 | |
| 150 | /** |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 151 | * \brief ECP key pair structure |
| 152 | * |
| 153 | * 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] | 154 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | * \note Members purposefully in the same order as struc mbedtls_ecdsa_context. |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 156 | */ |
| 157 | typedef struct |
| 158 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | mbedtls_ecp_group grp; /*!< Elliptic curve and base point */ |
| 160 | mbedtls_mpi d; /*!< our secret value */ |
| 161 | mbedtls_ecp_point Q; /*!< our public value */ |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 162 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | mbedtls_ecp_keypair; |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 164 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 165 | /** |
| 166 | * \name SECTION: Module settings |
| 167 | * |
| 168 | * The configuration options you can set for this module are in this section. |
| 169 | * Either change them in config.h or define them on the compiler command line. |
| 170 | * \{ |
| 171 | */ |
| 172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | #if !defined(MBEDTLS_ECP_MAX_BITS) |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 174 | /** |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 175 | * 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] | 176 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | #define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */ |
Paul Bakker | e1b665e | 2013-12-11 16:02:58 +0100 | [diff] [blame] | 178 | #endif |
| 179 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 180 | #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 ) |
| 181 | #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 ) |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | #if !defined(MBEDTLS_ECP_WINDOW_SIZE) |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 184 | /* |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 185 | * Maximum "window" size used for point multiplication. |
| 186 | * Default: 6. |
| 187 | * Minimum value: 2. Maximum value: 7. |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 188 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) ) |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 190 | * points used for point multiplication. This value is directly tied to EC |
| 191 | * peak memory usage, so decreasing it by one should roughly cut memory usage |
| 192 | * by two (if large curves are in use). |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 193 | * |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 194 | * Reduction in size may reduce speed, but larger curves are impacted first. |
| 195 | * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1): |
| 196 | * w-size: 6 5 4 3 2 |
| 197 | * 521 145 141 135 120 97 |
| 198 | * 384 214 209 198 177 146 |
| 199 | * 256 320 320 303 262 226 |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 200 | |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 201 | * 224 475 475 453 398 342 |
| 202 | * 192 640 640 633 587 476 |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 203 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | #define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */ |
| 205 | #endif /* MBEDTLS_ECP_WINDOW_SIZE */ |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 206 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 207 | #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM) |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 208 | /* |
| 209 | * Trade memory for speed on fixed-point multiplication. |
| 210 | * |
| 211 | * This speeds up repeated multiplication of the generator (that is, the |
| 212 | * multiplication in ECDSA signatures, and half of the multiplications in |
| 213 | * ECDSA verification and ECDHE) by a factor roughly 3 to 4. |
| 214 | * |
| 215 | * The cost is increasing EC peak memory usage by a factor roughly 2. |
| 216 | * |
| 217 | * Change this value to 0 to reduce peak memory usage. |
| 218 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */ |
| 220 | #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */ |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 221 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 222 | /* \} name SECTION: Module settings */ |
| 223 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 224 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 225 | * Point formats, from RFC 4492's enum ECPointFormat |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 226 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | #define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */ |
| 228 | #define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format */ |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 229 | |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 230 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 231 | * Some other constants from RFC 4492 |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 232 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | #define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */ |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 234 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 235 | /** |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 236 | * \brief Get the list of supported curves in order of preferrence |
| 237 | * (full information) |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 238 | * |
| 239 | * \return A statically allocated array, the last entry is 0. |
| 240 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void ); |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 242 | |
| 243 | /** |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 244 | * \brief Get the list of supported curves in order of preferrence |
| 245 | * (grp_id only) |
| 246 | * |
| 247 | * \return A statically allocated array, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | * terminated with MBEDTLS_ECP_DP_NONE. |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 249 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void ); |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 251 | |
| 252 | /** |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 253 | * \brief Get curve information from an internal group identifier |
| 254 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | * \param grp_id A MBEDTLS_ECP_DP_XXX value |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 256 | * |
| 257 | * \return The associated curve information or NULL |
| 258 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * \brief Get curve information from a TLS NamedCurve value |
| 263 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | * \param tls_id A MBEDTLS_ECP_DP_XXX value |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 265 | * |
| 266 | * \return The associated curve information or NULL |
| 267 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 269 | |
| 270 | /** |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 271 | * \brief Get curve information from a human-readable name |
| 272 | * |
| 273 | * \param name The name |
| 274 | * |
| 275 | * \return The associated curve information or NULL |
| 276 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 277 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name ); |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 278 | |
| 279 | /** |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 280 | * \brief Initialize a point (as zero) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 281 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | void mbedtls_ecp_point_init( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 283 | |
| 284 | /** |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 285 | * \brief Initialize a group (to something meaningless) |
| 286 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | void mbedtls_ecp_group_init( mbedtls_ecp_group *grp ); |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 288 | |
| 289 | /** |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 290 | * \brief Initialize a key pair (as an invalid one) |
| 291 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 293 | |
| 294 | /** |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 295 | * \brief Free the components of a point |
| 296 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | void mbedtls_ecp_point_free( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 298 | |
| 299 | /** |
| 300 | * \brief Free the components of an ECP group |
| 301 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | void mbedtls_ecp_group_free( mbedtls_ecp_group *grp ); |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 303 | |
| 304 | /** |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 305 | * \brief Free the components of a key pair |
| 306 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 307 | void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 308 | |
| 309 | /** |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 310 | * \brief Copy the contents of point Q into P |
| 311 | * |
| 312 | * \param P Destination point |
| 313 | * \param Q Source point |
| 314 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 315 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 316 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 317 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 318 | int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q ); |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 319 | |
| 320 | /** |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 321 | * \brief Copy the contents of a group object |
| 322 | * |
| 323 | * \param dst Destination group |
| 324 | * \param src Source group |
| 325 | * |
| 326 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 327 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 328 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 329 | int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src ); |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 330 | |
| 331 | /** |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 332 | * \brief Set a point to zero |
| 333 | * |
| 334 | * \param pt Destination point |
| 335 | * |
| 336 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 337 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 338 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 339 | int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 340 | |
| 341 | /** |
| 342 | * \brief Tell if a point is zero |
| 343 | * |
| 344 | * \param pt Point to test |
| 345 | * |
| 346 | * \return 1 if point is zero, 0 otherwise |
| 347 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 348 | int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 349 | |
| 350 | /** |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 351 | * \brief Import a non-zero point from two ASCII strings |
| 352 | * |
| 353 | * \param P Destination point |
| 354 | * \param radix Input numeric base |
| 355 | * \param x First affine coordinate as a null-terminated string |
| 356 | * \param y Second affine coordinate as a null-terminated string |
| 357 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 358 | * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 359 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 360 | int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix, |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 361 | const char *x, const char *y ); |
| 362 | |
| 363 | /** |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 364 | * \brief Export a point into unsigned binary data |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 365 | * |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 366 | * \param grp Group to which the point should belong |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 367 | * \param P Point to export |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | * \param format Point format, should be a MBEDTLS_ECP_PF_XXX macro |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 369 | * \param olen Length of the actual output |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 370 | * \param buf Output buffer |
| 371 | * \param buflen Length of the output buffer |
| 372 | * |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 373 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 374 | * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA |
| 375 | * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 376 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 377 | int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P, |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 378 | int format, size_t *olen, |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 379 | unsigned char *buf, size_t buflen ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 380 | |
| 381 | /** |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 382 | * \brief Import a point from unsigned binary data |
| 383 | * |
| 384 | * \param grp Group to which the point should belong |
| 385 | * \param P Point to import |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 386 | * \param buf Input buffer |
| 387 | * \param ilen Actual length of input |
| 388 | * |
| 389 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 390 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 391 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 392 | * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 393 | * is not implemented. |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 394 | * |
| 395 | * \note This function does NOT check that the point actually |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 396 | * belongs to the given group, see mbedtls_ecp_check_pubkey() for |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 397 | * that. |
| 398 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 399 | int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P, |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 400 | const unsigned char *buf, size_t ilen ); |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 401 | |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 402 | /** |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 403 | * \brief Import a point from a TLS ECPoint record |
| 404 | * |
| 405 | * \param grp ECP group used |
| 406 | * \param pt Destination point |
| 407 | * \param buf $(Start of input buffer) |
| 408 | * \param len Buffer length |
| 409 | * |
Manuel Pégourié-Gonnard | 150c4f6 | 2014-11-21 09:14:52 +0100 | [diff] [blame] | 410 | * \note buf is updated to point right after the ECPoint on exit |
| 411 | * |
Manuel Pégourié-Gonnard | eecb43c | 2015-05-12 12:56:41 +0200 | [diff] [blame] | 412 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 413 | * MBEDTLS_ERR_MPI_XXX if initialization failed |
| 414 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 415 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 417 | const unsigned char **buf, size_t len ); |
| 418 | |
| 419 | /** |
| 420 | * \brief Export a point as a TLS ECPoint record |
| 421 | * |
| 422 | * \param grp ECP group used |
| 423 | * \param pt Point to export |
| 424 | * \param format Export format |
| 425 | * \param olen length of data written |
| 426 | * \param buf Buffer to write to |
| 427 | * \param blen Buffer length |
| 428 | * |
| 429 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 430 | * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA |
| 431 | * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 432 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 433 | int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt, |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 434 | int format, size_t *olen, |
| 435 | unsigned char *buf, size_t blen ); |
| 436 | |
| 437 | /** |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 438 | * \brief Set a group using well-known domain parameters |
| 439 | * |
| 440 | * \param grp Destination group |
| 441 | * \param index Index in the list of well-known domain parameters |
| 442 | * |
Manuel Pégourié-Gonnard | eecb43c | 2015-05-12 12:56:41 +0200 | [diff] [blame] | 443 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 444 | * MBEDTLS_ERR_MPI_XXX if initialization failed |
| 445 | * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 446 | * |
Manuel Pégourié-Gonnard | aff37e5 | 2015-05-11 18:11:57 +0200 | [diff] [blame] | 447 | * \note Index should be a value of RFC 4492's enum NamedCurve, |
| 448 | * usually in the form of a MBEDTLS_ECP_DP_XXX macro. |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 449 | */ |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 450 | int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id index ); |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 451 | |
| 452 | /** |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 453 | * \brief Set a group from a TLS ECParameters record |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 454 | * |
| 455 | * \param grp Destination group |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 456 | * \param buf &(Start of input buffer) |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 457 | * \param len Buffer length |
| 458 | * |
Manuel Pégourié-Gonnard | 150c4f6 | 2014-11-21 09:14:52 +0100 | [diff] [blame] | 459 | * \note buf is updated to point right after ECParameters on exit |
| 460 | * |
Manuel Pégourié-Gonnard | eecb43c | 2015-05-12 12:56:41 +0200 | [diff] [blame] | 461 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 462 | * MBEDTLS_ERR_MPI_XXX if initialization failed |
| 463 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 464 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 465 | int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len ); |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 466 | |
| 467 | /** |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 468 | * \brief Write the TLS ECParameters record for a group |
| 469 | * |
| 470 | * \param grp ECP group used |
| 471 | * \param olen Number of bytes actually written |
| 472 | * \param buf Buffer to write to |
| 473 | * \param blen Buffer length |
| 474 | * |
| 475 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 477 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 478 | int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen, |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 479 | unsigned char *buf, size_t blen ); |
| 480 | |
| 481 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 482 | * \brief Multiplication by an integer: R = m * P |
Paul Bakker | 6838bd1 | 2013-09-30 13:56:38 +0200 | [diff] [blame] | 483 | * (Not thread-safe to use same group in multiple threads) |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 484 | * |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 485 | * \note In order to prevent timing attacks, this function |
| 486 | * executes the exact same sequence of (base field) |
| 487 | * operations for any valid m. It avoids any if-branch or |
| 488 | * array index depending on the value of m. |
| 489 | * |
| 490 | * \note If f_rng is not NULL, it is used to randomize intermediate |
| 491 | * results in order to prevent potential timing attacks |
| 492 | * targeting these results. It is recommended to always |
| 493 | * provide a non-NULL f_rng (the overhead is negligible). |
| 494 | * |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 495 | * \param grp ECP group |
| 496 | * \param R Destination point |
| 497 | * \param m Integer by which to multiply |
| 498 | * \param P Point to multiply |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 499 | * \param f_rng RNG function (see notes) |
| 500 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 501 | * |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 502 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 503 | * MBEDTLS_ERR_ECP_INVALID_KEY if m is not a valid privkey |
Manuel Pégourié-Gonnard | ff27b7c | 2013-11-21 09:28:03 +0100 | [diff] [blame] | 504 | * or P is not a valid pubkey, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 505 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 506 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 508 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
Manuel Pégourié-Gonnard | 09ceaf4 | 2013-11-20 23:06:14 +0100 | [diff] [blame] | 509 | 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] | 510 | |
| 511 | /** |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 512 | * \brief Multiplication and addition of two points by integers: |
| 513 | * R = m * P + n * Q |
| 514 | * (Not thread-safe to use same group in multiple threads) |
| 515 | * |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 516 | * \note In contrast to mbedtls_ecp_mul(), this function does not guarantee |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 517 | * a constant execution flow and timing. |
| 518 | * |
| 519 | * \param grp ECP group |
| 520 | * \param R Destination point |
| 521 | * \param m Integer by which to multiply P |
| 522 | * \param P Point to multiply by m |
| 523 | * \param n Integer by which to multiply Q |
| 524 | * \param Q Point to be multiplied by n |
| 525 | * |
| 526 | * \return 0 if successful, |
| 527 | * MBEDTLS_ERR_ECP_INVALID_KEY if m or n is not a valid privkey |
| 528 | * or P or Q is not a valid pubkey, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 529 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 530 | */ |
| 531 | int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 532 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 533 | const mbedtls_mpi *n, const mbedtls_ecp_point *Q ); |
| 534 | |
| 535 | /** |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 536 | * \brief Check that a point is a valid public key on this curve |
| 537 | * |
| 538 | * \param grp Curve/group the point should belong to |
| 539 | * \param pt Point to check |
| 540 | * |
| 541 | * \return 0 if point is a valid public key, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | * MBEDTLS_ERR_ECP_INVALID_KEY otherwise. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 543 | * |
| 544 | * \note This function only checks the point is non-zero, has valid |
| 545 | * coordinates and lies on the curve, but not that it is |
| 546 | * indeed a multiple of G. This is additional check is more |
| 547 | * expensive, isn't required by standards, and shouldn't be |
| 548 | * necessary if the group used has a small cofactor. In |
| 549 | * particular, it is useless for the NIST groups which all |
| 550 | * have a cofactor of 1. |
| 551 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 552 | * \note Uses bare components rather than an mbedtls_ecp_keypair structure |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 553 | * in order to ease use with other structures such as |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 554 | * mbedtls_ecdh_context of mbedtls_ecdsa_context. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 555 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 557 | |
| 558 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 559 | * \brief Check that an mbedtls_mpi is a valid private key for this curve |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 560 | * |
| 561 | * \param grp Group used |
| 562 | * \param d Integer to check |
| 563 | * |
| 564 | * \return 0 if point is a valid private key, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | * MBEDTLS_ERR_ECP_INVALID_KEY otherwise. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 566 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 567 | * \note Uses bare components rather than an mbedtls_ecp_keypair structure |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 568 | * in order to ease use with other structures such as |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 569 | * mbedtls_ecdh_context of mbedtls_ecdsa_context. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 570 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 571 | int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 572 | |
| 573 | /** |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 574 | * \brief Generate a keypair |
| 575 | * |
| 576 | * \param grp ECP group |
| 577 | * \param d Destination MPI (secret part) |
| 578 | * \param Q Destination point (public part) |
| 579 | * \param f_rng RNG function |
| 580 | * \param p_rng RNG parameter |
| 581 | * |
| 582 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 583 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 584 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 585 | * \note Uses bare components rather than an mbedtls_ecp_keypair structure |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 586 | * in order to ease use with other structures such as |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 587 | * mbedtls_ecdh_context of mbedtls_ecdsa_context. |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 588 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 590 | int (*f_rng)(void *, unsigned char *, size_t), |
| 591 | void *p_rng ); |
| 592 | |
| 593 | /** |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 594 | * \brief Generate a keypair |
| 595 | * |
| 596 | * \param grp_id ECP group identifier |
| 597 | * \param key Destination keypair |
| 598 | * \param f_rng RNG function |
| 599 | * \param p_rng RNG parameter |
| 600 | * |
| 601 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 603 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 604 | int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 605 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 606 | |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 607 | /** |
| 608 | * \brief Check a public-private key pair |
| 609 | * |
| 610 | * \param pub Keypair structure holding a public key |
| 611 | * \param prv Keypair structure holding a private (plus public) key |
| 612 | * |
Manuel Pégourié-Gonnard | 862d503 | 2015-04-15 11:30:46 +0200 | [diff] [blame] | 613 | * \return 0 if successful (keys are valid and match), or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 614 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or |
| 615 | * a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code. |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 616 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv ); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 619 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 620 | /** |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 621 | * \brief Checkup routine |
| 622 | * |
Manuel Pégourié-Gonnard | 13a1ef8 | 2014-03-27 20:12:44 +0100 | [diff] [blame] | 623 | * \return 0 if successful, or 1 if a test failed |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 624 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | int mbedtls_ecp_self_test( int verbose ); |
Manuel Pégourié-Gonnard | 13a1ef8 | 2014-03-27 20:12:44 +0100 | [diff] [blame] | 626 | #endif |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 627 | |
| 628 | #ifdef __cplusplus |
| 629 | } |
| 630 | #endif |
| 631 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 632 | #endif /* ecp.h */ |