blob: 89c756b37b9af6996bb567b62f55fd1820a96b1f [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/**
2 * \file ecp.h
3 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01004 * \brief This file contains ECP definitions and functions.
5 *
6 * The Elliptic Curve over P (ECP) is defined in <em>Standards for Efficient
7 * Cryptography Group (SECG): SEC1 Elliptic Curve Cryptography</em> and
8 * <em>RFC-4492: Elliptic Curve Cryptography (ECC) Cipher Suites
9 * for Transport Layer Security (TLS)</em>.
10 *
11 * <em>RFC-2409: The Internet Key Exchange (IKE)</em> defines ECP
12 * group types.
13 *
Darryl Greena40a1012018-01-05 15:33:17 +000014 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010015
16/*
17 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020018 * SPDX-License-Identifier: Apache-2.0
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010031 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010032 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010033 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#ifndef MBEDTLS_ECP_H
36#define MBEDTLS_ECP_H
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010037
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020038#include "bignum.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010039
40/*
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +010041 * ECP error codes
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
44#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010045#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< The requested curve not available. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020047#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010048#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as ephemeral key, failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
Gilles Peskine5114d3e2018-03-30 07:12:15 +020050#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< The buffer contains a valid signature followed by more data. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010051#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< The ECP hardware accelerator failed. */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010052
Janos Follathb0697532016-08-18 12:38:46 +010053#if !defined(MBEDTLS_ECP_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +000054/*
55 * default mbed TLS elliptic curve arithmetic implementation
56 *
57 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
58 * alternative implementation for the whole module and it will replace this
59 * one.)
60 */
Janos Follathb0697532016-08-18 12:38:46 +010061
Paul Bakker407a0da2013-06-27 14:29:21 +020062#ifdef __cplusplus
63extern "C" {
64#endif
65
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010066/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010067 * Definition of domain parameter identifiers: curve, subgroup and generator.
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020068 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010069 * \note Only curves over prime fields are supported.
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020070 *
71 * \warning This library does not support validation of arbitrary domain
72 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +020073 * sources should be used. See mbedtls_ecp_group_load().
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020074 */
75typedef enum
76{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010077 MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */
78 MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for 192-bit NIST curve. */
79 MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for 224-bit NIST curve. */
80 MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for 256-bit NIST curve. */
81 MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for 384-bit NIST curve. */
82 MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for 521-bit NIST curve. */
83 MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */
84 MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */
85 MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */
86 MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for a Curve25519 curve. */
87 MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for a Curve448 curve. */
88 MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */
89 MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */
90 MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091} mbedtls_ecp_group_id;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020092
93/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010094 * The number of supported curves, plus one for none.
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +010095 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010096 * \note Montgomery curves are currently excluded.
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#define MBEDTLS_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020099
100/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100101 * Curve information, for use by other modules.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200102 */
103typedef struct
104{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100105 mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */
106 uint16_t tls_id; /*!< The TLS NamedCurve identifier. */
107 uint16_t bit_size; /*!< The size of the curve in bits. */
108 const char *name; /*!< A human-friendly name. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109} mbedtls_ecp_curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200110
111/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100112 * \brief The ECP point structure, in jacobian coordinates.
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100113 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100114 * \note All functions expect and return points satisfying
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100115 * the following condition: \p Z == 0 or \p Z == 1. Other
116 * values of \p Z are used only by internal functions.
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100117 * The point is zero, or "at infinity", if Z == 0.
118 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100119 */
120typedef struct
121{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100122 mbedtls_mpi X; /*!< The X coordinate of the ECP point. */
123 mbedtls_mpi Y; /*!< The Y coordinate of the ECP point. */
124 mbedtls_mpi Z; /*!< The Z coordinate of the ECP point. */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126mbedtls_ecp_point;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100127
128/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100129 * \brief The ECP group structure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100130 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100131 * We consider two types of curve equations:
132 * <ul><li>Short Weierstrass: y^2 = x^3 + \p A x + \p B mod P
133 * (SEC1 + RFC-4492)</li>
134 * <li>Montgomery: y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft)</li></ul>
135 * In both cases, the generator (G) for a prime-order subgroup is fixed.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100136 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100137 * For Short Weierstrass, this subgroup is the whole curve, and its
138 * cardinal is denoted by \p N. Our code requires that \p N is an odd prime.
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200139 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100140 * \note For blinding, use odd in mbedtls_ecp_mul() and prime in
141 * mbedtls_ecdsa_sign().
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100142 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100143 * For Montgomery curves, we do not store \p A, but (A + 2) / 4, which is
144 * the quantity used in the formulas. Additionally, \p nbits is not the
145 * size of \p N but the required size for private keys.
146 *
147 * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
148 * Otherwise, it must point to a function that takes an \p mbedtls_mpi in the
149 * range of 0..2^(2*pbits)-1, and transforms it in-place in an integer of
150 * little more than \p pbits, so that the integer may be efficiently brought
151 * in the 0..P-1 range by a few additions or substractions.
152 *
153 * \return \c 0 on success
154 * \return Non-zero on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100155 */
156typedef struct
157{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100158 mbedtls_ecp_group_id id; /*!< An internal group identifier. */
159 mbedtls_mpi P; /*!< A prime modulus of the base field. */
160 mbedtls_mpi A; /*!< \p A in the equation or <code>(A + 2) / 4</code>. */
161 mbedtls_mpi B; /*!< \p B in the equation or unused. */
162 mbedtls_ecp_point G; /*!< The generator of the (sub)group used. */
163 mbedtls_mpi N; /*!< The order of \p G. */
164 size_t pbits; /*!< The number of bits in \p P.*/
165 size_t nbits; /*!< The number of bits in \p P, or the private
166 keys. */
167 unsigned int h; /*!< \internal 1 if the constants are static. */
168 int (*modp)(mbedtls_mpi *); /*!< The function for fast reduction mod P.*/
169 int (*t_pre)(mbedtls_ecp_point *, void *); /*!< Unused. */
170 int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */
171 void *t_data; /*!< Unused. */
172 mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */
173 size_t T_size; /*!< The number for pre-computed points. */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175mbedtls_ecp_group;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100176
177/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100178 * \brief The ECP key-pair structure.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200179 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100180 * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200181 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100182 * \note Members are deliberately in the same order as in the
183 * #mbedtls_ecdsa_context structure.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200184 */
185typedef struct
186{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100187 mbedtls_ecp_group grp; /*!< The elliptic curve and base point. */
188 mbedtls_mpi d; /*!< Our secret value. */
189 mbedtls_ecp_point Q; /*!< Our public value. */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191mbedtls_ecp_keypair;
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200192
Paul Bakker088c5c52014-04-25 11:11:10 +0200193/**
194 * \name SECTION: Module settings
195 *
196 * The configuration options you can set for this module are in this section.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100197 * Either change them in config.h, or define them using the compiler command line.
Paul Bakker088c5c52014-04-25 11:11:10 +0200198 * \{
199 */
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if !defined(MBEDTLS_ECP_MAX_BITS)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200202/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100203 * The maximum size of the groups, that is, of N and P.
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100204 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100205#define MBEDTLS_ECP_MAX_BITS 521 /**< The maximum size of groups, in bits. */
Paul Bakkere1b665e2013-12-11 16:02:58 +0100206#endif
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
209#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100212/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +0100213 * Maximum "window" size used for point multiplication.
214 * Default: 6.
215 * Minimum value: 2. Maximum value: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100216 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100218 * points used for point multiplication. This value is directly tied to EC
219 * peak memory usage, so decreasing it by one should roughly cut memory usage
220 * by two (if large curves are in use).
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100221 *
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100222 * Reduction in size may reduce speed, but larger curves are impacted first.
223 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
224 * w-size: 6 5 4 3 2
225 * 521 145 141 135 120 97
226 * 384 214 209 198 177 146
227 * 256 320 320 303 262 226
Paul Bakker088c5c52014-04-25 11:11:10 +0200228
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100229 * 224 475 475 453 398 342
230 * 192 640 640 633 587 476
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100231 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100232#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< The maximum window size used. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#endif /* MBEDTLS_ECP_WINDOW_SIZE */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100236/*
237 * Trade memory for speed on fixed-point multiplication.
238 *
239 * This speeds up repeated multiplication of the generator (that is, the
240 * multiplication in ECDSA signatures, and half of the multiplications in
241 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
242 *
243 * The cost is increasing EC peak memory usage by a factor roughly 2.
244 *
245 * Change this value to 0 to reduce peak memory usage.
246 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100247#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100249
Paul Bakker088c5c52014-04-25 11:11:10 +0200250/* \} name SECTION: Module settings */
251
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100252/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100253 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100254 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100255#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */
256#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format. */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100257
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100258/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100259 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100260 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100261#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100262
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100263/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100264 * \brief This function retrieves the information defined in
265 * mbedtls_ecp_curve_info()for all supported curves in order
266 * of preference.
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200267 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100268 * \return A statically allocated array. The last entry is 0.
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200271
272/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100273 * \brief This function retrieves the grp_id of all supported curves
274 * in order of preference.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100275 *
276 * \return A statically allocated array,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 * terminated with MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100280
281/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100282 * \brief This function retrieves curve information from an internal
283 * group identifier.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200284 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100285 * \param grp_id An \c MBEDTLS_ECP_DP_XXX value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200286 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100287 * \return The associated curve information, or NULL.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200290
291/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100292 * \brief This function retrieves curve information from a TLS
293 * NamedCurve value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200294 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100295 * \param tls_id An \c MBEDTLS_ECP_DP_XXX value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200296 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100297 * \return The associated curve information, or NULL.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200300
301/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100302 * \brief This function retrieves curve information from a
303 * human-readable name.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100304 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100305 * \param name The human-readable name.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100306 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100307 * \return The associated curve information, or NULL.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100310
311/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100312 * \brief This function initializes a point as zero.
313 *
314 * \param pt The point to initialize.
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100317
318/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100319 * \brief This function initializes a group to something meaningless.
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100322
323/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100324 * \brief This function initializes a key pair as an invalid one.
325 *
326 * \param key The key pair to initialize.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200329
330/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100331 * \brief This function frees the components of a point.
332 *
333 * \param pt The point to free.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100336
337/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100338 * \brief This function frees the components of an ECP group.
339 * \param grp The group to free.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100342
343/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100344 * \brief This function frees the components of a key pair.
345 * \param key The key pair to free.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200348
349/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100350 * \brief This function copies the contents of point \p Q into
351 * point \p P.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100352 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100353 * \param P The destination point.
354 * \param Q The source point.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100355 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100356 * \return \c 0 on success.
357 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation fails.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100360
361/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100362 * \brief This function copies the contents of group \p src into
363 * group \p dst.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200364 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100365 * \param dst The destination group.
366 * \param src The source group.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200367 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100368 * \return \c 0 on success.
369 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation fails.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200372
373/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100374 * \brief This function sets a point to zero.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200375 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100376 * \param pt The point to set.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200377 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100378 * \return \c 0 on success.
379 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation fails.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200382
383/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100384 * \brief This function checks if a point is zero.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200385 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100386 * \param pt The point to test.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200387 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100388 * \return \c 1 if point is zero.
389 * \return \c 0 if point is non-zero.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200392
393/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100394 * \brief This function compares two points.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200395 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100396 * \note This assumes that the points are normalized. Otherwise,
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200397 * they may compare as "not equal" even if they are.
398 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100399 * \param P The first point to compare.
400 * \param Q The second point to compare.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200401 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100402 * \return \c 0 if the points are equal.
403 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200404 */
405int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
406 const mbedtls_ecp_point *Q );
407
408/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100409 * \brief This function imports a non-zero point from two ASCII
410 * strings.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100411 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100412 * \param P The destination point.
413 * \param radix The numeric base of the input.
414 * \param x The first affine coordinate, as a null-terminated string.
415 * \param y The second affine coordinate, as a null-terminated string.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100416 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100417 * \return \c 0 on success.
418 * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100421 const char *x, const char *y );
422
423/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100424 * \brief This function exports a point into unsigned binary data.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100425 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100426 * \param grp The group to which the point should belong.
427 * \param P The point to export.
428 * \param format The point format. Should be an \c MBEDTLS_ECP_PF_XXX macro.
429 * \param olen The length of the output.
430 * \param buf The output buffer.
431 * \param buflen The length of the output buffer.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100432 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100433 * \return \c 0 on success.
434 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA
435 * or #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100438 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100439 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100440
441/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100442 * \brief This function imports a point from unsigned binary data.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100443 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100444 * \note This function does not check that the point actually
445 * belongs to the given group, see mbedtls_ecp_check_pubkey()
446 * for that.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100447 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100448 * \param grp The group to which the point should belong.
449 * \param P The point to import.
450 * \param buf The input buffer.
451 * \param ilen The length of the input.
452 *
453 * \return \c 0 on success.
454 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
455 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
456 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100457 * is not implemented.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100458 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100461 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100462
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100463/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100464 * \brief This function imports a point from a TLS ECPoint record.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200465 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100466 * \note On function return, \p buf is updated to point to immediately
467 * after the ECPoint.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200468 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100469 * \param grp The ECP group used.
470 * \param pt The destination point.
471 * \param buf The address of the pointer to the start of input buffer.
472 * \param len The length of the buffer.
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100473 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100474 * \return \c 0 on success.
475 * \return An \c MBEDTLS_ERR_MPI_XXX error code if initialization failed.
476 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200479 const unsigned char **buf, size_t len );
480
481/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100482 * \brief This function exports a point as a TLS ECPoint record.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200483 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100484 * \param grp The ECP group used.
485 * \param pt The point to export.
486 * \param format The export format.
487 * \param olen The length of data written.
488 * \param buf The Buffer to write to.
489 * \param blen The length of the Buffer.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200490 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100491 * \return \c 0 on success.
492 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA or
493 * #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200496 int format, size_t *olen,
497 unsigned char *buf, size_t blen );
498
499/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100500 * \brief This function sets a group using well-known domain parameters.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100501 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100502 * \note The index should be a value of the NamedCurve enum,
503 * as defined in <em>RFC-4492: Elliptic Curve Cryptography
504 * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
505 * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100506 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100507 * \param grp The destination group.
508 * \param id The index in the list of well-known domain parameters.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100509 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100510 * \return \c 0 on success,
511 * \return An \c MBEDTLS_ERR_MPI_XXX error code if initialization fails.
512 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups.
513
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100514 */
Hanno Becker61937d42017-04-26 15:01:23 +0100515int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100516
517/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100518 * \brief This function sets a group from a TLS ECParameters record.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100519 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100520 * \note \p buf is updated to point right after ECParameters on exit.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100521 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100522 * \param grp The destination group.
523 * \param buf The address of the pointer to the start of input buffer.
524 * \param len The length of the buffer.
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100525 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100526 * \return \c 0 on success.
527 * \return An \c MBEDTLS_ERR_MPI_XXX error code if initialization fails.
528 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100531
532/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100533 * \brief This function writes the TLS ECParameters record for a group.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100534 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100535 * \param grp The ECP group used.
536 * \param olen The number of Bytes written.
537 * \param buf The buffer to write to.
538 * \param blen The length of the buffer.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100539 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100540 * \return \c 0 on success.
541 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100544 unsigned char *buf, size_t blen );
545
546/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100547 * \brief This function performs multiplication of a point by
548 * an integer: \p R = \p m * \p P.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100549 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100550 * It is not thread-safe to use same group in multiple threads.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200551 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100552 * \note To prevent timing attacks, this function
553 * executes the exact same sequence of base-field
554 * operations for any valid \p m. It avoids any if-branch or
555 * array index depending on the value of \p m.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200556 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100557 * \note If \p f_rng is not NULL, it is used to randomize
558 * intermediate results to prevent potential timing attacks
559 * targeting these results. We recommend always providing
560 * a non-NULL \p f_rng. The overhead is negligible.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100561 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100562 * \param grp The ECP group.
563 * \param R The destination point.
564 * \param m The integer by which to multiply.
565 * \param P The point to multiply.
566 * \param f_rng The RNG function.
567 * \param p_rng The RNG context.
568 *
569 * \return \c 0 on success.
570 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid privkey,
571 * or \p P is not a valid pubkey.
572 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
575 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +0100576 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100577
578/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100579 * \brief This function performs multiplication and addition of two
580 * points by integers: \p R = \p m * \p P + \p n * \p Q
581
582 * It is not thread-safe to use same group in multiple threads.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200583 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100584 * \note In contrast to mbedtls_ecp_mul(), this function does not
585 * guarantee a constant execution flow and timing.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200586 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100587 * \param grp The ECP group.
588 * \param R The destination point.
589 * \param m The integer by which to multiply \p P.
590 * \param P The point to multiply by \p m.
591 * \param n The integer by which to multiply \p Q.
592 * \param Q The point to be multiplied by \p n.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200593 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100594 * \return \c 0 on success.
595 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
596 * valid private keys, or \p P or \p Q are not valid public
597 * keys.
598 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200599 */
600int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
601 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
602 const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
603
604/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100605 * \brief This function checks that a point is a valid public key
606 * on this curve.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200607 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100608 * It only checks that the point is non-zero, has
609 * valid coordinates and lies on the curve. It does not verify
610 * that it is indeed a multiple of \p G. This additional
611 * check is computationally more expensive, is not required
612 * by standards, and should not be necessary if the group
613 * used has a small cofactor. In particular, it is useless for
614 * the NIST groups which all have a cofactor of 1.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200615 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100616 * \note This function uses bare components rather than an
617 * mbedtls_ecp_keypair() structure, to ease use with other
618 * structures. For example, mbedtls_ecdh_context() or
619 * mbedtls_ecdsa_context().
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200620 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100621 * \param grp The curve or group the point should belong to.
622 * \param pt The point to check.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200623 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100624 * \return \c 0 if the point is a valid public key.
625 * \return #MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200628
629/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100630 * \brief This function checks that an \p mbedtls_mpi is a valid private
631 * key for this curve.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200632 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100633 * \note This function uses bare components rather than an
634 * mbedtls_ecp_keypair() structure to ease use with other
635 * structures such as mbedtls_ecdh_context() or
636 * mbedtls_ecdsa_context().
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200637 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100638 * \param grp The group used.
639 * \param d The integer to check.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200640 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100641 * \return \c 0 if the point is a valid private key.
642 * \return #MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200645
646/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100647 * \brief This function generates a keypair with a configurable base
648 * point.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +0200649 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100650 * \note This function uses bare components rather than an
651 * mbedtls_ecp_keypair() structure to ease use with other
652 * structures such as mbedtls_ecdh_context() or
653 * mbedtls_ecdsa_context().
654*
655 * \param grp The ECP group.
656 * \param G The chosen base point.
657 * \param d The destination MPI (secret part).
658 * \param Q The destination point (public part).
659 * \param f_rng The RNG function.
660 * \param p_rng The RNG context.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +0200661 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100662 * \return \c 0 on success.
663 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
664 * on failure.
665 */
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +0200666int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
667 const mbedtls_ecp_point *G,
668 mbedtls_mpi *d, mbedtls_ecp_point *Q,
669 int (*f_rng)(void *, unsigned char *, size_t),
670 void *p_rng );
671
672/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100673 * \brief This function generates a keypair.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100674 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100675 * \note This function uses bare components rather than an
676 * mbedtls_ecp_keypair() structure to ease use with other
677 * structures such as mbedtls_ecdh_context() or
678 * mbedtls_ecdsa_context().
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100679 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100680 * \param grp The ECP group.
681 * \param d The destination MPI (secret part).
682 * \param Q The destination point (public part).
683 * \param f_rng The RNG function.
684 * \param p_rng The RNG context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200685 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100686 * \return \c 0 on success.
687 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
688 * on failure.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100691 int (*f_rng)(void *, unsigned char *, size_t),
692 void *p_rng );
693
694/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100695 * \brief This function generates a key.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100696 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100697 * \param grp_id The ECP group identifier.
698 * \param key The destination key.
699 * \param f_rng The RNG function.
700 * \param p_rng The RNG context.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100701 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100702 * \return \c 0 on success.
703 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
704 * on failure.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100707 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
708
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100709/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100710 * \brief This function checks a public-private key pair.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100711 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100712 * \param pub The keypair structure holding the public key.
713 * \param prv The keypair structure holding the private key.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100714 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100715 * \note The both are keypairs, and may optionally hold the corresponding other key, but the public key passed in thee pub is checked against the private key passed in prv.
716 *
717 * \return \c 0 on success - the keys are valid and match.
718 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or an \c
719 * MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
720 * error code on failure.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#if defined(MBEDTLS_SELF_TEST)
Janos Follathb0697532016-08-18 12:38:46 +0100725
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100726/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100727 * \brief The ECP checkup routine.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100728 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100729 * \return \c 0 on success.
730 * \return \c 1 on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732int mbedtls_ecp_self_test( int verbose );
Janos Follathb0697532016-08-18 12:38:46 +0100733
Janos Follath372697b2016-10-28 16:53:11 +0100734#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100735
736#ifdef __cplusplus
737}
738#endif
739
Janos Follathb0697532016-08-18 12:38:46 +0100740#else /* MBEDTLS_ECP_ALT */
741#include "ecp_alt.h"
742#endif /* MBEDTLS_ECP_ALT */
743
Paul Bakker9af723c2014-05-01 13:03:14 +0200744#endif /* ecp.h */