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