mbed TLS v2.16.9
ecp.h
Go to the documentation of this file.
1
17/*
18 * Copyright The Mbed TLS Contributors
19 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
20 *
21 * This file is provided under the Apache License 2.0, or the
22 * GNU General Public License v2.0 or later.
23 *
24 * **********
25 * Apache License 2.0:
26 *
27 * Licensed under the Apache License, Version 2.0 (the "License"); you may
28 * not use this file except in compliance with the License.
29 * You may obtain a copy of the License at
30 *
31 * http://www.apache.org/licenses/LICENSE-2.0
32 *
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 * See the License for the specific language governing permissions and
37 * limitations under the License.
38 *
39 * **********
40 *
41 * **********
42 * GNU General Public License v2.0 or later:
43 *
44 * This program is free software; you can redistribute it and/or modify
45 * it under the terms of the GNU General Public License as published by
46 * the Free Software Foundation; either version 2 of the License, or
47 * (at your option) any later version.
48 *
49 * This program is distributed in the hope that it will be useful,
50 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 * GNU General Public License for more details.
53 *
54 * You should have received a copy of the GNU General Public License along
55 * with this program; if not, write to the Free Software Foundation, Inc.,
56 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
57 *
58 * **********
59 */
60
61#ifndef MBEDTLS_ECP_H
62#define MBEDTLS_ECP_H
63
64#if !defined(MBEDTLS_CONFIG_FILE)
65#include "config.h"
66#else
67#include MBEDTLS_CONFIG_FILE
68#endif
69
70#include "bignum.h"
71
72/*
73 * ECP error codes
74 */
75#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80
76#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00
77#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80
78#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00
79#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80
80#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00
81#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80
82#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00
84/* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */
85#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80
87#define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00
89#ifdef __cplusplus
90extern "C" {
91#endif
92
102typedef enum
103{
119
125#define MBEDTLS_ECP_DP_MAX 12
126
131{
133 uint16_t tls_id;
134 uint16_t bit_size;
135 const char *name;
137
149typedef struct mbedtls_ecp_point
150{
154}
156
157#if !defined(MBEDTLS_ECP_ALT)
158/*
159 * default mbed TLS elliptic curve arithmetic implementation
160 *
161 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
162 * alternative implementation for the whole module and it will replace this
163 * one.)
164 */
165
198typedef struct mbedtls_ecp_group
199{
208 size_t pbits;
209 size_t nbits;
212 unsigned int h;
213 int (*modp)(mbedtls_mpi *);
215 int (*t_pre)(mbedtls_ecp_point *, void *);
216 int (*t_post)(mbedtls_ecp_point *, void *);
217 void *t_data;
219 size_t T_size;
220}
222
231#if !defined(MBEDTLS_ECP_MAX_BITS)
235#define MBEDTLS_ECP_MAX_BITS 521
236#endif
237
238#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
239#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
240
241#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
242/*
243 * Maximum "window" size used for point multiplication.
244 * Default: 6.
245 * Minimum value: 2. Maximum value: 7.
246 *
247 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
248 * points used for point multiplication. This value is directly tied to EC
249 * peak memory usage, so decreasing it by one should roughly cut memory usage
250 * by two (if large curves are in use).
251 *
252 * Reduction in size may reduce speed, but larger curves are impacted first.
253 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
254 * w-size: 6 5 4 3 2
255 * 521 145 141 135 120 97
256 * 384 214 209 198 177 146
257 * 256 320 320 303 262 226
258 * 224 475 475 453 398 342
259 * 192 640 640 633 587 476
260 */
261#define MBEDTLS_ECP_WINDOW_SIZE 6
262#endif /* MBEDTLS_ECP_WINDOW_SIZE */
263
264#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
265/*
266 * Trade memory for speed on fixed-point multiplication.
267 *
268 * This speeds up repeated multiplication of the generator (that is, the
269 * multiplication in ECDSA signatures, and half of the multiplications in
270 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
271 *
272 * The cost is increasing EC peak memory usage by a factor roughly 2.
273 *
274 * Change this value to 0 to reduce peak memory usage.
275 */
276#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1
277#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
278
279/* \} name SECTION: Module settings */
280
281#else /* MBEDTLS_ECP_ALT */
282#include "ecp_alt.h"
283#endif /* MBEDTLS_ECP_ALT */
284
285#if defined(MBEDTLS_ECP_RESTARTABLE)
286
292typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
293
299typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
300
304typedef struct
305{
306 unsigned ops_done;
307 unsigned depth;
308 mbedtls_ecp_restart_mul_ctx *rsm;
309 mbedtls_ecp_restart_muladd_ctx *ma;
311
312/*
313 * Operation counts for restartable functions
314 */
315#define MBEDTLS_ECP_OPS_CHK 3
316#define MBEDTLS_ECP_OPS_DBL 8
317#define MBEDTLS_ECP_OPS_ADD 11
318#define MBEDTLS_ECP_OPS_INV 120
331int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
333 unsigned ops );
334
335/* Utility macro for checking and updating ops budget */
336#define MBEDTLS_ECP_BUDGET( ops ) \
337 MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, \
338 (unsigned) (ops) ) );
339
340#else /* MBEDTLS_ECP_RESTARTABLE */
341
342#define MBEDTLS_ECP_BUDGET( ops ) /* no-op; for compatibility */
343
344/* We want to declare restartable versions of existing functions anyway */
346
347#endif /* MBEDTLS_ECP_RESTARTABLE */
348
358{
362}
364
365/*
366 * Point formats, from RFC 4492's enum ECPointFormat
367 */
368#define MBEDTLS_ECP_PF_UNCOMPRESSED 0
369#define MBEDTLS_ECP_PF_COMPRESSED 1
371/*
372 * Some other constants from RFC 4492
373 */
374#define MBEDTLS_ECP_TLS_NAMED_CURVE 3
376#if defined(MBEDTLS_ECP_RESTARTABLE)
434void mbedtls_ecp_set_max_ops( unsigned max_ops );
435
442int mbedtls_ecp_restart_is_enabled( void );
443#endif /* MBEDTLS_ECP_RESTARTABLE */
444
453
463
474
485
496
503
514
521
528
537
546
547#if defined(MBEDTLS_ECP_RESTARTABLE)
554void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx );
555
563void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx );
564#endif /* MBEDTLS_ECP_RESTARTABLE */
565
578
591 const mbedtls_ecp_group *src );
592
603
614
628 const mbedtls_ecp_point *Q );
629
643 const char *x, const char *y );
644
666 int format, size_t *olen,
667 unsigned char *buf, size_t buflen );
668
693 const unsigned char *buf, size_t ilen );
694
715 const unsigned char **buf, size_t len );
716
740 const mbedtls_ecp_point *pt,
741 int format, size_t *olen,
742 unsigned char *buf, size_t blen );
743
762
781 const unsigned char **buf, size_t len );
782
802 const unsigned char **buf,
803 size_t len );
823 size_t *olen,
824 unsigned char *buf, size_t blen );
825
863 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
864 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
865
897 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
898 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
899 mbedtls_ecp_restart_ctx *rs_ctx );
900
931 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
932 const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
933
970 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
971 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
972 mbedtls_ecp_restart_ctx *rs_ctx );
973
1002 const mbedtls_ecp_point *pt );
1003
1024 const mbedtls_mpi *d );
1025
1042 mbedtls_mpi *d,
1043 int (*f_rng)(void *, unsigned char *, size_t),
1044 void *p_rng );
1045
1074 const mbedtls_ecp_point *G,
1076 int (*f_rng)(void *, unsigned char *, size_t),
1077 void *p_rng );
1078
1104 int (*f_rng)(void *, unsigned char *, size_t),
1105 void *p_rng );
1106
1121 int (*f_rng)(void *, unsigned char *, size_t),
1122 void *p_rng );
1123
1142 const mbedtls_ecp_keypair *prv );
1143
1144#if defined(MBEDTLS_SELF_TEST)
1145
1152int mbedtls_ecp_self_test( int verbose );
1153
1154#endif /* MBEDTLS_SELF_TEST */
1155
1156#ifdef __cplusplus
1157}
1158#endif
1159
1160#endif /* ecp.h */
Multi-precision integer library.
Configuration options (set of defines)
void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
This function frees the components of a key pair.
int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P, const unsigned char *buf, size_t ilen)
This function imports a point from unsigned binary data.
int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, const mbedtls_mpi *m, const mbedtls_ecp_point *P, const mbedtls_mpi *n, const mbedtls_ecp_point *Q)
This function performs multiplication and addition of two points by integers: R = m * P + n * Q.
int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates an ECP key.
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
This function checks if a point is the point at infinity.
int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen, unsigned char *buf, size_t blen)
This function exports an elliptic curve as a TLS ECParameters record as defined in RFC 4492,...
int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
This function copies the contents of group src into group dst.
void mbedtls_ecp_restart_ctx
Definition ecp.h:345
int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, const mbedtls_mpi *m, const mbedtls_ecp_point *P, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function performs a scalar multiplication of a point by an integer: R = m * P.
const mbedtls_ecp_group_id * mbedtls_ecp_grp_id_list(void)
This function retrieves the list of internal group identifiers of all supported curves in the order o...
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
This function initializes an ECP group context without loading any domain parameters.
int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp, const mbedtls_ecp_point *G, mbedtls_mpi *d, mbedtls_ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates a keypair with a configurable base point.
int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv)
This function checks that the keypair objects pub and prv have the same group and the same public poi...
int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, const mbedtls_mpi *m, const mbedtls_ecp_point *P, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, mbedtls_ecp_restart_ctx *rs_ctx)
This function performs multiplication of a point by an integer: R = m * P in a restartable way.
int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
This function compares two points.
int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, const mbedtls_mpi *d)
This function checks that an mbedtls_mpi is a valid private key for this curve.
int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix, const char *x, const char *y)
This function imports a non-zero point from two ASCII strings.
int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id)
This function sets up an ECP group context from a standardized set of domain parameters.
void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
This function initializes a key pair as an invalid one.
int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp, const unsigned char **buf, size_t len)
This function extracts an elliptic curve group ID from a TLS ECParameters record as defined in RFC 44...
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_info_from_name(const char *name)
This function retrieves curve information from a human-readable name.
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
This function frees the components of a point.
int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, const unsigned char **buf, size_t len)
This function imports a point from a TLS ECPoint record.
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
This function frees the components of an ECP group.
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
This function copies the contents of point Q into point P.
int mbedtls_ecp_muladd_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, const mbedtls_mpi *m, const mbedtls_ecp_point *P, const mbedtls_mpi *n, const mbedtls_ecp_point *Q, mbedtls_ecp_restart_ctx *rs_ctx)
This function performs multiplication and addition of two points by integers: R = m * P + n * Q in a ...
int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp, mbedtls_mpi *d, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates a private key.
int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates an ECP keypair.
int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt, int format, size_t *olen, unsigned char *buf, size_t blen)
This function exports a point as a TLS ECPoint record defined in RFC 4492, Section 5....
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id)
This function retrieves curve information from an internal group identifier.
int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
This function sets a point to the point at infinity.
int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, const unsigned char **buf, size_t len)
This function sets up an ECP group context from a TLS ECParameters record as defined in RFC 4492,...
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_list(void)
This function retrieves the information defined in mbedtls_ecp_curve_info() for all supported curves ...
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
This function initializes a point as zero.
int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
This function checks that a point is a valid public key on this curve.
mbedtls_ecp_group_id
Definition ecp.h:103
@ MBEDTLS_ECP_DP_SECP192K1
Definition ecp.h:114
@ MBEDTLS_ECP_DP_SECP384R1
Definition ecp.h:108
@ MBEDTLS_ECP_DP_CURVE448
Definition ecp.h:117
@ MBEDTLS_ECP_DP_CURVE25519
Definition ecp.h:113
@ MBEDTLS_ECP_DP_NONE
Definition ecp.h:104
@ MBEDTLS_ECP_DP_SECP256K1
Definition ecp.h:116
@ MBEDTLS_ECP_DP_BP512R1
Definition ecp.h:112
@ MBEDTLS_ECP_DP_SECP224R1
Definition ecp.h:106
@ MBEDTLS_ECP_DP_SECP521R1
Definition ecp.h:109
@ MBEDTLS_ECP_DP_BP384R1
Definition ecp.h:111
@ MBEDTLS_ECP_DP_SECP224K1
Definition ecp.h:115
@ MBEDTLS_ECP_DP_BP256R1
Definition ecp.h:110
@ MBEDTLS_ECP_DP_SECP192R1
Definition ecp.h:105
@ MBEDTLS_ECP_DP_SECP256R1
Definition ecp.h:107
int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P, int format, size_t *olen, unsigned char *buf, size_t buflen)
This function exports a point into unsigned binary data.
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
This function retrieves curve information from a TLS NamedCurve value.
int mbedtls_ecp_self_test(int verbose)
The ECP checkup routine.
const char * name
Definition ecp.h:135
mbedtls_ecp_group_id grp_id
Definition ecp.h:132
uint16_t bit_size
Definition ecp.h:134
uint16_t tls_id
Definition ecp.h:133
The ECP group structure.
Definition ecp.h:199
size_t pbits
Definition ecp.h:208
int(* t_pre)(mbedtls_ecp_point *, void *)
Definition ecp.h:215
unsigned int h
Definition ecp.h:212
mbedtls_ecp_group_id id
Definition ecp.h:200
size_t T_size
Definition ecp.h:219
mbedtls_ecp_point * T
Definition ecp.h:218
mbedtls_mpi N
Definition ecp.h:207
void * t_data
Definition ecp.h:217
mbedtls_ecp_point G
Definition ecp.h:206
int(* t_post)(mbedtls_ecp_point *, void *)
Definition ecp.h:216
mbedtls_mpi B
Definition ecp.h:204
int(* modp)(mbedtls_mpi *)
Definition ecp.h:213
mbedtls_mpi P
Definition ecp.h:201
size_t nbits
Definition ecp.h:209
mbedtls_mpi A
Definition ecp.h:202
The ECP key-pair structure.
Definition ecp.h:358
mbedtls_ecp_point Q
Definition ecp.h:361
mbedtls_mpi d
Definition ecp.h:360
mbedtls_ecp_group grp
Definition ecp.h:359
The ECP point structure, in Jacobian coordinates.
Definition ecp.h:150
mbedtls_mpi Z
Definition ecp.h:153
mbedtls_mpi X
Definition ecp.h:151
mbedtls_mpi Y
Definition ecp.h:152
MPI structure.
Definition bignum.h:211