mbed TLS v2.14.1
ecp.h
Go to the documentation of this file.
1 
17 /*
18  * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
19  * SPDX-License-Identifier: Apache-2.0
20  *
21  * Licensed under the Apache License, Version 2.0 (the "License"); you may
22  * not use this file except in compliance with the License.
23  * You may obtain a copy of the License at
24  *
25  * http://www.apache.org/licenses/LICENSE-2.0
26  *
27  * Unless required by applicable law or agreed to in writing, software
28  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30  * See the License for the specific language governing permissions and
31  * limitations under the License.
32  *
33  * This file is part of Mbed TLS (https://tls.mbed.org)
34  */
35 
36 #ifndef MBEDTLS_ECP_H
37 #define MBEDTLS_ECP_H
38 
39 #include "bignum.h"
40 
41 /*
42  * ECP error codes
43  */
44 #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80
45 #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00
46 #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80
47 #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00
48 #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80
49 #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00
50 #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80
51 #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00
53 /* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */
54 #define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80
56 #define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
71 typedef enum
72 {
88 
94 #define MBEDTLS_ECP_DP_MAX 12
95 
99 typedef struct mbedtls_ecp_curve_info
100 {
102  uint16_t tls_id;
103  uint16_t bit_size;
104  const char *name;
106 
118 typedef struct mbedtls_ecp_point
119 {
123 }
125 
126 #if !defined(MBEDTLS_ECP_ALT)
127 /*
128  * default mbed TLS elliptic curve arithmetic implementation
129  *
130  * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
131  * alternative implementation for the whole module and it will replace this
132  * one.)
133  */
134 
163 typedef struct mbedtls_ecp_group
164 {
173  size_t pbits;
174  size_t nbits;
177  unsigned int h;
178  int (*modp)(mbedtls_mpi *);
180  int (*t_pre)(mbedtls_ecp_point *, void *);
181  int (*t_post)(mbedtls_ecp_point *, void *);
182  void *t_data;
184  size_t T_size;
185 }
187 
188 #if defined(MBEDTLS_ECP_RESTARTABLE)
189 
195 typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
196 
202 typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
203 
207 typedef struct
208 {
209  unsigned ops_done;
210  unsigned depth;
211  mbedtls_ecp_restart_mul_ctx *rsm;
212  mbedtls_ecp_restart_muladd_ctx *ma;
214 
215 /*
216  * Operation counts for restartable functions
217  */
218 #define MBEDTLS_ECP_OPS_CHK 3
219 #define MBEDTLS_ECP_OPS_DBL 8
220 #define MBEDTLS_ECP_OPS_ADD 11
221 #define MBEDTLS_ECP_OPS_INV 120
234 int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
235  mbedtls_ecp_restart_ctx *rs_ctx,
236  unsigned ops );
237 
238 /* Utility macro for checking and updating ops budget */
239 #define MBEDTLS_ECP_BUDGET( ops ) \
240  MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, \
241  (unsigned) (ops) ) );
242 
243 #else /* MBEDTLS_ECP_RESTARTABLE */
244 
245 #define MBEDTLS_ECP_BUDGET( ops ) /* no-op; for compatibility */
246 
247 /* We want to declare restartable versions of existing functions anyway */
249 
250 #endif /* MBEDTLS_ECP_RESTARTABLE */
251 
260 #if !defined(MBEDTLS_ECP_MAX_BITS)
261 
264 #define MBEDTLS_ECP_MAX_BITS 521
265 #endif
266 
267 #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
268 #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
269 
270 #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
271 /*
272  * Maximum "window" size used for point multiplication.
273  * Default: 6.
274  * Minimum value: 2. Maximum value: 7.
275  *
276  * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
277  * points used for point multiplication. This value is directly tied to EC
278  * peak memory usage, so decreasing it by one should roughly cut memory usage
279  * by two (if large curves are in use).
280  *
281  * Reduction in size may reduce speed, but larger curves are impacted first.
282  * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
283  * w-size: 6 5 4 3 2
284  * 521 145 141 135 120 97
285  * 384 214 209 198 177 146
286  * 256 320 320 303 262 226
287  * 224 475 475 453 398 342
288  * 192 640 640 633 587 476
289  */
290 #define MBEDTLS_ECP_WINDOW_SIZE 6
291 #endif /* MBEDTLS_ECP_WINDOW_SIZE */
292 
293 #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
294 /*
295  * Trade memory for speed on fixed-point multiplication.
296  *
297  * This speeds up repeated multiplication of the generator (that is, the
298  * multiplication in ECDSA signatures, and half of the multiplications in
299  * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
300  *
301  * The cost is increasing EC peak memory usage by a factor roughly 2.
302  *
303  * Change this value to 0 to reduce peak memory usage.
304  */
305 #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1
306 #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
307 
308 /* \} name SECTION: Module settings */
309 
310 #else /* MBEDTLS_ECP_ALT */
311 #include "ecp_alt.h"
312 #endif /* MBEDTLS_ECP_ALT */
313 
322 typedef struct mbedtls_ecp_keypair
323 {
327 }
329 
330 /*
331  * Point formats, from RFC 4492's enum ECPointFormat
332  */
333 #define MBEDTLS_ECP_PF_UNCOMPRESSED 0
334 #define MBEDTLS_ECP_PF_COMPRESSED 1
336 /*
337  * Some other constants from RFC 4492
338  */
339 #define MBEDTLS_ECP_TLS_NAMED_CURVE 3
341 #if defined(MBEDTLS_ECP_RESTARTABLE)
342 
399 void mbedtls_ecp_set_max_ops( unsigned max_ops );
400 
407 int mbedtls_ecp_restart_is_enabled( void );
408 #endif /* MBEDTLS_ECP_RESTARTABLE */
409 
418 
428 
439 
450 
461 
468 
479 
486 
493 
499 
505 
506 #if defined(MBEDTLS_ECP_RESTARTABLE)
507 
510 void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx );
511 
515 void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx );
516 #endif /* MBEDTLS_ECP_RESTARTABLE */
517 
529 
541 
551 
561 
575  const mbedtls_ecp_point *Q );
576 
590  const char *x, const char *y );
591 
607  int format, size_t *olen,
608  unsigned char *buf, size_t buflen );
609 
630  const unsigned char *buf, size_t ilen );
631 
648  const unsigned char **buf, size_t len );
649 
666  int format, size_t *olen,
667  unsigned char *buf, size_t blen );
668 
686 
701 int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
702 
714 int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
715  unsigned char *buf, size_t blen );
716 
746  const mbedtls_mpi *m, const mbedtls_ecp_point *P,
747  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
748 
775  const mbedtls_mpi *m, const mbedtls_ecp_point *P,
776  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
777  mbedtls_ecp_restart_ctx *rs_ctx );
778 
802  const mbedtls_mpi *m, const mbedtls_ecp_point *P,
803  const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
804 
834  const mbedtls_mpi *m, const mbedtls_ecp_point *P,
835  const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
836  mbedtls_ecp_restart_ctx *rs_ctx );
837 
862 
878 int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
879 
893  mbedtls_mpi *d,
894  int (*f_rng)(void *, unsigned char *, size_t),
895  void *p_rng );
896 
918  const mbedtls_ecp_point *G,
920  int (*f_rng)(void *, unsigned char *, size_t),
921  void *p_rng );
922 
942  int (*f_rng)(void *, unsigned char *, size_t),
943  void *p_rng );
944 
958  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
959 
976 
977 #if defined(MBEDTLS_SELF_TEST)
978 
985 int mbedtls_ecp_self_test( int verbose );
986 
987 #endif /* MBEDTLS_SELF_TEST */
988 
989 #ifdef __cplusplus
990 }
991 #endif
992 
993 #endif /* ecp.h */