mbed TLS v2.16.12
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  */
76 #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80
77 
78 #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00
79 
80 #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80
81 
82 #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00
83 
84 #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80
85 
86 #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00
87 
88 #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80
89 
90 #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00
91 
92 /* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */
94 #define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80
95 
97 #define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00
98 
99 #ifdef __cplusplus
100 extern "C" {
101 #endif
102 
112 typedef enum
113 {
129 
135 #define MBEDTLS_ECP_DP_MAX 12
136 
141 {
143  uint16_t tls_id;
144  uint16_t bit_size;
145  const char *name;
147 
159 typedef struct mbedtls_ecp_point
160 {
164 }
166 
167 /* Determine the minimum safe value of MBEDTLS_ECP_MAX_BITS. */
168 #if !defined(MBEDTLS_ECP_C)
169 #define MBEDTLS_ECP_MAX_BITS_MIN 0
170 /* Note: the curves must be listed in DECREASING size! */
171 #elif defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
172 #define MBEDTLS_ECP_MAX_BITS_MIN 521
173 #elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
174 #define MBEDTLS_ECP_MAX_BITS_MIN 512
175 #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
176 #define MBEDTLS_ECP_MAX_BITS_MIN 448
177 #elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
178 #define MBEDTLS_ECP_MAX_BITS_MIN 384
179 #elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
180 #define MBEDTLS_ECP_MAX_BITS_MIN 384
181 #elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
182 #define MBEDTLS_ECP_MAX_BITS_MIN 256
183 #elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
184 #define MBEDTLS_ECP_MAX_BITS_MIN 256
185 #elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
186 #define MBEDTLS_ECP_MAX_BITS_MIN 256
187 #elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
188 #define MBEDTLS_ECP_MAX_BITS_MIN 255
189 #elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
190 #define MBEDTLS_ECP_MAX_BITS_MIN 225 // n is slightly above 2^224
191 #elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
192 #define MBEDTLS_ECP_MAX_BITS_MIN 224
193 #elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
194 #define MBEDTLS_ECP_MAX_BITS_MIN 192
195 #elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
196 #define MBEDTLS_ECP_MAX_BITS_MIN 192
197 #else
198 #error "MBEDTLS_ECP_C enabled, but no curve?"
199 #endif
200 
201 #if !defined(MBEDTLS_ECP_ALT)
202 /*
203  * default mbed TLS elliptic curve arithmetic implementation
204  *
205  * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
206  * alternative implementation for the whole module and it will replace this
207  * one.)
208  */
209 
242 typedef struct mbedtls_ecp_group
243 {
252  size_t pbits;
253  size_t nbits;
256  unsigned int h;
257  int (*modp)(mbedtls_mpi *);
259  int (*t_pre)(mbedtls_ecp_point *, void *);
260  int (*t_post)(mbedtls_ecp_point *, void *);
261  void *t_data;
263  size_t T_size;
264 }
266 
275 #if defined(MBEDTLS_ECP_MAX_BITS)
276 
277 #if MBEDTLS_ECP_MAX_BITS < MBEDTLS_ECP_MAX_BITS_MIN
278 #error "MBEDTLS_ECP_MAX_BITS is smaller than the largest supported curve"
279 #endif
280 
281 #else
282 
285 #define MBEDTLS_ECP_MAX_BITS 521
286 #endif
287 
288 #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
289 #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
290 
291 #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
292 /*
293  * Maximum "window" size used for point multiplication.
294  * Default: 6.
295  * Minimum value: 2. Maximum value: 7.
296  *
297  * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
298  * points used for point multiplication. This value is directly tied to EC
299  * peak memory usage, so decreasing it by one should roughly cut memory usage
300  * by two (if large curves are in use).
301  *
302  * Reduction in size may reduce speed, but larger curves are impacted first.
303  * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
304  * w-size: 6 5 4 3 2
305  * 521 145 141 135 120 97
306  * 384 214 209 198 177 146
307  * 256 320 320 303 262 226
308  * 224 475 475 453 398 342
309  * 192 640 640 633 587 476
310  */
311 #define MBEDTLS_ECP_WINDOW_SIZE 6
312 #endif /* MBEDTLS_ECP_WINDOW_SIZE */
313 
314 #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
315 /*
316  * Trade memory for speed on fixed-point multiplication.
317  *
318  * This speeds up repeated multiplication of the generator (that is, the
319  * multiplication in ECDSA signatures, and half of the multiplications in
320  * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
321  *
322  * The cost is increasing EC peak memory usage by a factor roughly 2.
323  *
324  * Change this value to 0 to reduce peak memory usage.
325  */
326 #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1
327 #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
328 
329 /* \} name SECTION: Module settings */
330 
331 #else /* MBEDTLS_ECP_ALT */
332 #include "ecp_alt.h"
333 #endif /* MBEDTLS_ECP_ALT */
334 
335 #if defined(MBEDTLS_ECP_RESTARTABLE)
336 
342 typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
343 
349 typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
350 
354 typedef struct
355 {
356  unsigned ops_done;
357  unsigned depth;
358  mbedtls_ecp_restart_mul_ctx *rsm;
359  mbedtls_ecp_restart_muladd_ctx *ma;
361 
362 /*
363  * Operation counts for restartable functions
364  */
365 #define MBEDTLS_ECP_OPS_CHK 3
366 #define MBEDTLS_ECP_OPS_DBL 8
367 #define MBEDTLS_ECP_OPS_ADD 11
368 #define MBEDTLS_ECP_OPS_INV 120
381 int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
382  mbedtls_ecp_restart_ctx *rs_ctx,
383  unsigned ops );
384 
385 /* Utility macro for checking and updating ops budget */
386 #define MBEDTLS_ECP_BUDGET( ops ) \
387  MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, \
388  (unsigned) (ops) ) );
389 
390 #else /* MBEDTLS_ECP_RESTARTABLE */
391 
392 #define MBEDTLS_ECP_BUDGET( ops ) /* no-op; for compatibility */
393 
394 /* We want to declare restartable versions of existing functions anyway */
396 
397 #endif /* MBEDTLS_ECP_RESTARTABLE */
398 
407 typedef struct mbedtls_ecp_keypair
408 {
412 }
414 
415 /*
416  * Point formats, from RFC 4492's enum ECPointFormat
417  */
418 #define MBEDTLS_ECP_PF_UNCOMPRESSED 0
419 #define MBEDTLS_ECP_PF_COMPRESSED 1
421 /*
422  * Some other constants from RFC 4492
423  */
424 #define MBEDTLS_ECP_TLS_NAMED_CURVE 3
426 #if defined(MBEDTLS_ECP_RESTARTABLE)
427 
484 void mbedtls_ecp_set_max_ops( unsigned max_ops );
485 
492 int mbedtls_ecp_restart_is_enabled( void );
493 #endif /* MBEDTLS_ECP_RESTARTABLE */
494 
503 
513 
524 
535 
546 
553 
564 
571 
578 
587 
596 
597 #if defined(MBEDTLS_ECP_RESTARTABLE)
598 
604 void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx );
605 
613 void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx );
614 #endif /* MBEDTLS_ECP_RESTARTABLE */
615 
628 
641  const mbedtls_ecp_group *src );
642 
653 
664 
678  const mbedtls_ecp_point *Q );
679 
693  const char *x, const char *y );
694 
716  int format, size_t *olen,
717  unsigned char *buf, size_t buflen );
718 
743  const unsigned char *buf, size_t ilen );
744 
764  mbedtls_ecp_point *pt,
765  const unsigned char **buf, size_t len );
766 
790  const mbedtls_ecp_point *pt,
791  int format, size_t *olen,
792  unsigned char *buf, size_t blen );
793 
812 
831  const unsigned char **buf, size_t len );
832 
852  const unsigned char **buf,
853  size_t len );
873  size_t *olen,
874  unsigned char *buf, size_t blen );
875 
913  const mbedtls_mpi *m, const mbedtls_ecp_point *P,
914  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
915 
947  const mbedtls_mpi *m, const mbedtls_ecp_point *P,
948  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
949  mbedtls_ecp_restart_ctx *rs_ctx );
950 
981  const mbedtls_mpi *m, const mbedtls_ecp_point *P,
982  const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
983 
1020  const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1021  const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
1022  mbedtls_ecp_restart_ctx *rs_ctx );
1023 
1052  const mbedtls_ecp_point *pt );
1053 
1074  const mbedtls_mpi *d );
1075 
1092  mbedtls_mpi *d,
1093  int (*f_rng)(void *, unsigned char *, size_t),
1094  void *p_rng );
1095 
1124  const mbedtls_ecp_point *G,
1126  int (*f_rng)(void *, unsigned char *, size_t),
1127  void *p_rng );
1128 
1153  mbedtls_ecp_point *Q,
1154  int (*f_rng)(void *, unsigned char *, size_t),
1155  void *p_rng );
1156 
1171  int (*f_rng)(void *, unsigned char *, size_t),
1172  void *p_rng );
1173 
1192  const mbedtls_ecp_keypair *prv );
1193 
1194 #if defined(MBEDTLS_SELF_TEST)
1195 
1202 int mbedtls_ecp_self_test( int verbose );
1203 
1204 #endif /* MBEDTLS_SELF_TEST */
1205 
1206 #ifdef __cplusplus
1207 }
1208 #endif
1209 
1210 #endif /* ecp.h */
uint16_t tls_id
Definition: ecp.h:143
int(* modp)(mbedtls_mpi *)
Definition: ecp.h:257
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
This function checks if a point is the point at infinity.
mbedtls_mpi N
Definition: ecp.h:251
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(* t_post)(mbedtls_ecp_point *, void *)
Definition: ecp.h:260
mbedtls_mpi Z
Definition: ecp.h:163
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_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_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.
mbedtls_mpi Y
Definition: ecp.h:162
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 ...
mbedtls_ecp_group grp
Definition: ecp.h:409
struct mbedtls_ecp_curve_info mbedtls_ecp_curve_info
The ECP key-pair structure.
Definition: ecp.h:407
int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
This function sets a point to the point at infinity.
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
This function copies the contents of point Q into point 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...
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.
Configuration options (set of defines)
size_t nbits
Definition: ecp.h:253
struct mbedtls_ecp_group mbedtls_ecp_group
The ECP group structure.
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.
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
This function frees the components of a point.
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.
void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
This function initializes a key pair as an invalid one.
size_t pbits
Definition: ecp.h:252
mbedtls_mpi X
Definition: ecp.h:161
Multi-precision integer library.
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.
The ECP group structure.
Definition: ecp.h:242
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.
mbedtls_ecp_group_id id
Definition: ecp.h:244
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_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...
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
This function frees the components of an ECP group.
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.
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_mpi A
Definition: ecp.h:246
mbedtls_mpi P
Definition: ecp.h:245
void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
This function frees the components of a key pair.
struct mbedtls_ecp_keypair mbedtls_ecp_keypair
The ECP key-pair structure.
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...
void mbedtls_ecp_restart_ctx
Definition: ecp.h:395
mbedtls_ecp_point Q
Definition: ecp.h:411
void * t_data
Definition: ecp.h:261
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
This function initializes a point as zero.
mbedtls_ecp_group_id
Definition: ecp.h:112
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_self_test(int verbose)
The ECP checkup routine.
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_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.
mbedtls_ecp_group_id grp_id
Definition: ecp.h:142
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...
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.
mbedtls_mpi d
Definition: ecp.h:410
unsigned int h
Definition: ecp.h:256
int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
This function compares two points.
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...
size_t T_size
Definition: ecp.h:263
mbedtls_ecp_point * T
Definition: ecp.h:262
mbedtls_ecp_point G
Definition: ecp.h:250
MPI structure.
Definition: bignum.h:218
struct mbedtls_ecp_point mbedtls_ecp_point
The ECP point structure, in Jacobian coordinates.
The ECP point structure, in Jacobian coordinates.
Definition: ecp.h:159
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_info_from_name(const char *name)
This function retrieves curve information from a human-readable name.
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 ...
const char * name
Definition: ecp.h:145
int(* t_pre)(mbedtls_ecp_point *, void *)
Definition: ecp.h:259
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.
uint16_t bit_size
Definition: ecp.h:144
mbedtls_mpi B
Definition: ecp.h:248
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
This function initializes an ECP group context without loading any domain parameters.