ecc.h
Go to the documentation of this file.
1 /* ecc.h
2  *
3  * Copyright (C) 2006-2020 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 
27 #ifndef WOLF_CRYPT_ECC_H
28 #define WOLF_CRYPT_ECC_H
29 
31 
32 #ifdef HAVE_ECC
33 
34 #if defined(HAVE_FIPS) && \
35  defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
36  #include <wolfssl/wolfcrypt/fips.h>
37 #endif /* HAVE_FIPS_VERSION >= 2 */
38 
39 #include <wolfssl/wolfcrypt/integer.h>
41 
42 #ifdef HAVE_X963_KDF
43  #include <wolfssl/wolfcrypt/hash.h>
44 #endif
45 
46 #ifdef WOLFSSL_ASYNC_CRYPT
47  #include <wolfssl/wolfcrypt/async.h>
48  #ifdef WOLFSSL_CERT_GEN
49  #include <wolfssl/wolfcrypt/asn.h>
50  #endif
51 #endif
52 
53 #ifdef WOLFSSL_ATECC508A
54  #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
55 #endif /* WOLFSSL_ATECC508A */
56 
57 #if defined(WOLFSSL_CRYPTOCELL)
58  #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
59 #endif
60 
61 #ifdef __cplusplus
62  extern "C" {
63 #endif
64 
65 
66 /* Enable curve B parameter if needed */
67 #if defined(HAVE_COMP_KEY) || defined(ECC_CACHE_CURVE)
68  #ifndef USE_ECC_B_PARAM /* Allow someone to force enable */
69  #define USE_ECC_B_PARAM
70  #endif
71 #endif
72 
73 
74 /* Use this as the key->idx if a custom ecc_set is used for key->dp */
75 #define ECC_CUSTOM_IDX (-1)
76 
77 
78 /* Determine max ECC bits based on enabled curves */
79 #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
80  #define MAX_ECC_BITS 521
81 #elif defined(HAVE_ECC512)
82  #define MAX_ECC_BITS 512
83 #elif defined(HAVE_ECC384)
84  #define MAX_ECC_BITS 384
85 #elif defined(HAVE_ECC320)
86  #define MAX_ECC_BITS 320
87 #elif !defined(NO_ECC256)
88  #define MAX_ECC_BITS 256
89 #elif defined(HAVE_ECC239)
90  #define MAX_ECC_BITS 239
91 #elif defined(HAVE_ECC224)
92  #define MAX_ECC_BITS 224
93 #elif defined(HAVE_ECC192)
94  #define MAX_ECC_BITS 192
95 #elif defined(HAVE_ECC160)
96  #define MAX_ECC_BITS 160
97 #elif defined(HAVE_ECC128)
98  #define MAX_ECC_BITS 128
99 #elif defined(HAVE_ECC112)
100  #define MAX_ECC_BITS 112
101 #endif
102 
103 /* calculate max ECC bytes */
104 #if ((MAX_ECC_BITS * 2) % 8) == 0
105  #define MAX_ECC_BYTES (MAX_ECC_BITS / 8)
106 #else
107  /* add byte if not aligned */
108  #define MAX_ECC_BYTES ((MAX_ECC_BITS / 8) + 1)
109 #endif
110 
111 #ifndef ECC_MAX_PAD_SZ
112  /* ECC maximum padding size (when MSB is set extra byte required for R and S) */
113  #define ECC_MAX_PAD_SZ 2
114 #endif
115 
116 enum {
117  ECC_PUBLICKEY = 1,
118  ECC_PRIVATEKEY = 2,
119  ECC_PRIVATEKEY_ONLY = 3,
120  ECC_MAXNAME = 16, /* MAX CURVE NAME LENGTH */
121  SIG_HEADER_SZ = 7, /* ECC signature header size (30 81 87 02 42 [R] 02 42 [S]) */
122  ECC_BUFSIZE = 256, /* for exported keys temp buffer */
123  ECC_MINSIZE = 20, /* MIN Private Key size */
124  ECC_MAXSIZE = 66, /* MAX Private Key size */
125  ECC_MAXSIZE_GEN = 74, /* MAX Buffer size required when generating ECC keys*/
126  ECC_MAX_OID_LEN = 16,
127  ECC_MAX_SIG_SIZE= ((MAX_ECC_BYTES * 2) + ECC_MAX_PAD_SZ + SIG_HEADER_SZ),
128 
129  /* max crypto hardware size */
130 #ifdef WOLFSSL_ATECC508A
131  ECC_MAX_CRYPTO_HW_SIZE = ATECC_KEY_SIZE, /* from port/atmel/atmel.h */
132  ECC_MAX_CRYPTO_HW_PUBKEY_SIZE = (ATECC_KEY_SIZE*2),
133 #elif defined(PLUTON_CRYPTO_ECC)
134  ECC_MAX_CRYPTO_HW_SIZE = 32,
135 #elif defined(WOLFSSL_CRYPTOCELL)
136  #ifndef CRYPTOCELL_KEY_SIZE
137  CRYPTOCELL_KEY_SIZE = ECC_MAXSIZE,
138  #endif
139  ECC_MAX_CRYPTO_HW_SIZE = CRYPTOCELL_KEY_SIZE,
140 #endif
141 
142  /* point compression type */
143  ECC_POINT_COMP_EVEN = 0x02,
144  ECC_POINT_COMP_ODD = 0x03,
145  ECC_POINT_UNCOMP = 0x04,
146 
147  /* Shamir's dual add constants */
148  SHAMIR_PRECOMP_SZ = 16,
149 
150 #ifdef HAVE_PKCS11
151  ECC_MAX_ID_LEN = 32,
152 #endif
153 };
154 
155 /* Curve Types */
156 typedef enum ecc_curve_id {
157  ECC_CURVE_INVALID = -1,
158  ECC_CURVE_DEF = 0, /* NIST or SECP */
159 
160  /* NIST Prime Curves */
161  ECC_SECP192R1,
162  ECC_PRIME192V2,
163  ECC_PRIME192V3,
164  ECC_PRIME239V1,
165  ECC_PRIME239V2,
166  ECC_PRIME239V3,
167  ECC_SECP256R1,
168 
169  /* SECP Curves */
170  ECC_SECP112R1,
171  ECC_SECP112R2,
172  ECC_SECP128R1,
173  ECC_SECP128R2,
174  ECC_SECP160R1,
175  ECC_SECP160R2,
176  ECC_SECP224R1,
177  ECC_SECP384R1,
178  ECC_SECP521R1,
179 
180  /* Koblitz */
181  ECC_SECP160K1,
182  ECC_SECP192K1,
183  ECC_SECP224K1,
184  ECC_SECP256K1,
185 
186  /* Brainpool Curves */
187  ECC_BRAINPOOLP160R1,
188  ECC_BRAINPOOLP192R1,
189  ECC_BRAINPOOLP224R1,
190  ECC_BRAINPOOLP256R1,
191  ECC_BRAINPOOLP320R1,
192  ECC_BRAINPOOLP384R1,
193  ECC_BRAINPOOLP512R1,
194 
195  /* Twisted Edwards Curves */
196 #ifdef HAVE_CURVE25519
197  ECC_X25519,
198 #endif
199 #ifdef HAVE_CURVE448
200  ECC_X448,
201 #endif
202 
203 #ifdef WOLFSSL_CUSTOM_CURVES
204  ECC_CURVE_CUSTOM,
205 #endif
206 } ecc_curve_id;
207 
208 #ifdef HAVE_OID_ENCODING
209 typedef word16 ecc_oid_t;
210 #else
211 typedef byte ecc_oid_t;
212  /* OID encoded with ASN scheme:
213  first element = (oid[0] * 40) + oid[1]
214  if any element > 127 then MSB 0x80 indicates additional byte */
215 #endif
216 
217 
218 #if !defined(WOLFSSL_ECC_CURVE_STATIC) && defined(USE_WINDOWS_API)
219  /* MSC does something different with the pointers to the arrays than GCC,
220  * and it causes the FIPS checksum to fail. In the case of windows builds,
221  * store everything as arrays instead of pointers to strings. */
222 
223  #define WOLFSSL_ECC_CURVE_STATIC
224 #endif
225 
226 /* ECC set type defined a GF(p) curve */
227 #ifndef WOLFSSL_ECC_CURVE_STATIC
228 typedef struct ecc_set_type {
229  int size; /* The size of the curve in octets */
230  int id; /* id of this curve */
231  const char* name; /* name of this curve */
232  const char* prime; /* prime that defines the field, curve is in (hex) */
233  const char* Af; /* fields A param (hex) */
234  const char* Bf; /* fields B param (hex) */
235  const char* order; /* order of the curve (hex) */
236  const char* Gx; /* x coordinate of the base point on curve (hex) */
237  const char* Gy; /* y coordinate of the base point on curve (hex) */
238  const ecc_oid_t* oid;
239  word32 oidSz;
240  word32 oidSum; /* sum of encoded OID bytes */
241  int cofactor;
242 } ecc_set_type;
243 #else
244 #define MAX_ECC_NAME 16
245 #define MAX_ECC_STRING ((MAX_ECC_BYTES * 2) + 1)
246  /* The values are stored as text strings. */
247 
248 typedef struct ecc_set_type {
249  int size; /* The size of the curve in octets */
250  int id; /* id of this curve */
251  const char name[MAX_ECC_NAME]; /* name of this curve */
252  const char prime[MAX_ECC_STRING]; /* prime that defines the field, curve is in (hex) */
253  const char Af[MAX_ECC_STRING]; /* fields A param (hex) */
254  const char Bf[MAX_ECC_STRING]; /* fields B param (hex) */
255  const char order[MAX_ECC_STRING]; /* order of the curve (hex) */
256  const char Gx[MAX_ECC_STRING]; /* x coordinate of the base point on curve (hex) */
257  const char Gy[MAX_ECC_STRING]; /* y coordinate of the base point on curve (hex) */
258  const ecc_oid_t oid[10];
259  word32 oidSz;
260  word32 oidSum; /* sum of encoded OID bytes */
261  int cofactor;
262 } ecc_set_type;
263 #endif
264 
265 
266 #ifdef ALT_ECC_SIZE
267 
268 /* Note on ALT_ECC_SIZE:
269  * The fast math code uses an array of a fixed size to store the big integers.
270  * By default, the array is big enough for RSA keys. There is a size,
271  * FP_MAX_BITS which can be used to make the array smaller when one wants ECC
272  * but not RSA. Some people want fast math sized for both RSA and ECC, where
273  * ECC won't use as much as RSA. The flag ALT_ECC_SIZE switches in an alternate
274  * ecc_point structure that uses an alternate fp_int that has a shorter array
275  * of fp_digits.
276  *
277  * Now, without ALT_ECC_SIZE, the ecc_point has three single item arrays of
278  * mp_ints for the components of the point. With ALT_ECC_SIZE, the components
279  * of the point are pointers that are set to each of a three item array of
280  * alt_fp_ints. While an mp_int will have 4096 bits of digit inside the
281  * structure, the alt_fp_int will only have 528 bits. A size value was added
282  * in the ALT case, as well, and is set by mp_init() and alt_fp_init(). The
283  * functions fp_zero() and fp_copy() use the size parameter. An int needs to
284  * be initialized before using it instead of just fp_zeroing it, the init will
285  * call zero. FP_MAX_BITS_ECC defaults to 528, but can be set to change the
286  * number of bits used in the alternate FP_INT.
287  *
288  * Do not enable ALT_ECC_SIZE and disable fast math in the configuration.
289  */
290 
291 #ifndef USE_FAST_MATH
292  #error USE_FAST_MATH must be defined to use ALT_ECC_SIZE
293 #endif
294 
295 /* determine max bits required for ECC math */
296 #ifndef FP_MAX_BITS_ECC
297  /* check alignment */
298  #if ((MAX_ECC_BITS * 2) % DIGIT_BIT) == 0
299  /* max bits is double */
300  #define FP_MAX_BITS_ECC (MAX_ECC_BITS * 2)
301  #else
302  /* max bits is doubled, plus one digit of fudge */
303  #define FP_MAX_BITS_ECC ((MAX_ECC_BITS * 2) + DIGIT_BIT)
304  #endif
305 #else
306  /* verify alignment */
307  #if FP_MAX_BITS_ECC % CHAR_BIT
308  #error FP_MAX_BITS_ECC must be a multiple of CHAR_BIT
309  #endif
310 #endif
311 
312 /* determine buffer size */
313 #define FP_SIZE_ECC (FP_MAX_BITS_ECC/DIGIT_BIT)
314 
315 
316 /* This needs to match the size of the fp_int struct, except the
317  * fp_digit array will be shorter. */
318 typedef struct alt_fp_int {
319  int used, sign, size;
320  mp_digit dp[FP_SIZE_ECC];
321 } alt_fp_int;
322 #endif /* ALT_ECC_SIZE */
323 
324 #ifndef WC_ECCKEY_TYPE_DEFINED
325  typedef struct ecc_key ecc_key;
326  #define WC_ECCKEY_TYPE_DEFINED
327 #endif
328 
329 
330 /* A point on an ECC curve, stored in Jacobian format such that (x,y,z) =>
331  (x/z^2, y/z^3, 1) when interpreted as affine */
332 typedef struct {
333 #ifndef ALT_ECC_SIZE
334  mp_int x[1]; /* The x coordinate */
335  mp_int y[1]; /* The y coordinate */
336  mp_int z[1]; /* The z coordinate */
337 #else
338  mp_int* x; /* The x coordinate */
339  mp_int* y; /* The y coordinate */
340  mp_int* z; /* The z coordinate */
341  alt_fp_int xyz[3];
342 #endif
343 #ifdef WOLFSSL_SMALL_STACK_CACHE
344  ecc_key* key;
345 #endif
346 } ecc_point;
347 
348 /* ECC Flags */
349 enum {
350  WC_ECC_FLAG_NONE = 0x00,
351 #ifdef HAVE_ECC_CDH
352  WC_ECC_FLAG_COFACTOR = 0x01,
353 #endif
354 };
355 
356 /* An ECC Key */
357 struct ecc_key {
358  int type; /* Public or Private */
359  int idx; /* Index into the ecc_sets[] for the parameters of
360  this curve if -1, this key is using user supplied
361  curve in dp */
362  int state;
363  word32 flags;
364  const ecc_set_type* dp; /* domain parameters, either points to NIST
365  curves (idx >= 0) or user supplied */
366 #ifdef WOLFSSL_CUSTOM_CURVES
367  int deallocSet;
368 #endif
369  void* heap; /* heap hint */
370  ecc_point pubkey; /* public key */
371  mp_int k; /* private key */
372 #ifdef WOLFSSL_ATECC508A
373  int slot; /* Key Slot Number (-1 unknown) */
374  byte pubkey_raw[ECC_MAX_CRYPTO_HW_PUBKEY_SIZE];
375 #endif
376 #if defined(PLUTON_CRYPTO_ECC) || defined(WOLF_CRYPTO_CB)
377  int devId;
378 #endif
379 #ifdef WOLFSSL_ASYNC_CRYPT
380  mp_int* r; /* sign/verify temps */
381  mp_int* s;
382  WC_ASYNC_DEV asyncDev;
383  #ifdef HAVE_CAVIUM_V
384  mp_int* e; /* Sign, Verify and Shared Secret */
385  mp_int* signK;
386  #endif
387  #ifdef WOLFSSL_CERT_GEN
388  CertSignCtx certSignCtx; /* context info for cert sign (MakeSignature) */
389  #endif
390 #endif /* WOLFSSL_ASYNC_CRYPT */
391 #ifdef HAVE_PKCS11
392  byte id[ECC_MAX_ID_LEN];
393  int idLen;
394 #endif
395 #if defined(WOLFSSL_CRYPTOCELL)
396  ecc_context_t ctx;
397 #endif
398 
399 #ifdef WOLFSSL_ECDSA_SET_K
400  mp_int *sign_k;
401 #endif
402 
403 #ifdef WOLFSSL_SMALL_STACK_CACHE
404  mp_int* t1;
405  mp_int* t2;
406 #ifdef ALT_ECC_SIZE
407  mp_int* x;
408  mp_int* y;
409  mp_int* z;
410 #endif
411 #endif
412 
413 #ifdef WOLFSSL_DSP
414  remote_handle64 handle;
415 #endif
416 };
417 
418 
419 WOLFSSL_ABI WOLFSSL_API ecc_key* wc_ecc_key_new(void*);
420 WOLFSSL_ABI WOLFSSL_API void wc_ecc_key_free(ecc_key*);
421 
422 
423 /* ECC predefined curve sets */
424 extern const ecc_set_type ecc_sets[];
425 extern const size_t ecc_sets_count;
426 
427 WOLFSSL_API
428 const char* wc_ecc_get_name(int curve_id);
429 
430 #ifndef WOLFSSL_ATECC508A
431 
432 #ifdef WOLFSSL_PUBLIC_ECC_ADD_DBL
433  #define ECC_API WOLFSSL_API
434 #else
435  #define ECC_API WOLFSSL_LOCAL
436 #endif
437 
438 ECC_API int ecc_mul2add(ecc_point* A, mp_int* kA,
439  ecc_point* B, mp_int* kB,
440  ecc_point* C, mp_int* a, mp_int* modulus, void* heap);
441 
442 ECC_API int ecc_map(ecc_point*, mp_int*, mp_digit);
443 ECC_API int ecc_map_ex(ecc_point*, mp_int*, mp_digit, int ct);
445  mp_int* a, mp_int* modulus, mp_digit mp);
446 ECC_API int ecc_projective_dbl_point(ecc_point* P, ecc_point* R, mp_int* a,
447  mp_int* modulus, mp_digit mp);
448 
449 #endif
450 
451 WOLFSSL_API
452 int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key);
453 WOLFSSL_ABI WOLFSSL_API
454 int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id);
455 WOLFSSL_API
456 int wc_ecc_make_pub(ecc_key* key, ecc_point* pubOut);
457 WOLFSSL_API
458 int wc_ecc_check_key(ecc_key* key);
459 WOLFSSL_API
460 int wc_ecc_is_point(ecc_point* ecp, mp_int* a, mp_int* b, mp_int* prime);
461 WOLFSSL_API
462 int wc_ecc_get_generator(ecc_point* ecp, int curve_idx);
463 
464 #ifdef HAVE_ECC_DHE
465 WOLFSSL_API
466 int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
467  word32* outlen);
468 WOLFSSL_LOCAL
469 int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point,
470  byte* out, word32 *outlen);
471 WOLFSSL_API
472 int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
473  byte* out, word32 *outlen);
474 
475 #if defined(WOLFSSL_ATECC508A) || defined(PLUTON_CRYPTO_ECC) || defined(WOLFSSL_CRYPTOCELL)
476 #define wc_ecc_shared_secret_ssh wc_ecc_shared_secret
477 #else
478 #define wc_ecc_shared_secret_ssh wc_ecc_shared_secret_ex /* For backwards compat */
479 #endif
480 
481 #endif /* HAVE_ECC_DHE */
482 
483 #ifdef HAVE_ECC_SIGN
484 WOLFSSL_ABI WOLFSSL_API
485 int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
486  WC_RNG* rng, ecc_key* key);
487 WOLFSSL_API
488 int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
489  ecc_key* key, mp_int *r, mp_int *s);
490 #ifdef WOLFSSL_ECDSA_SET_K
491 WOLFSSL_API
492 int wc_ecc_sign_set_k(const byte* k, word32 klen, ecc_key* key);
493 #endif
494 #endif /* HAVE_ECC_SIGN */
495 
496 #ifdef HAVE_ECC_VERIFY
497 WOLFSSL_API
498 int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
499  word32 hashlen, int* stat, ecc_key* key);
500 WOLFSSL_API
501 int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
502  word32 hashlen, int* stat, ecc_key* key);
503 #endif /* HAVE_ECC_VERIFY */
504 
505 WOLFSSL_API
506 int wc_ecc_init(ecc_key* key);
507 WOLFSSL_ABI WOLFSSL_API
508 int wc_ecc_init_ex(ecc_key* key, void* heap, int devId);
509 #ifdef HAVE_PKCS11
510 WOLFSSL_API
511 int wc_ecc_init_id(ecc_key* key, unsigned char* id, int len, void* heap,
512  int devId);
513 #endif
514 #ifdef WOLFSSL_CUSTOM_CURVES
515 WOLFSSL_LOCAL
516 void wc_ecc_free_curve(const ecc_set_type* curve, void* heap);
517 #endif
518 WOLFSSL_ABI WOLFSSL_API
519 int wc_ecc_free(ecc_key* key);
520 WOLFSSL_API
521 int wc_ecc_set_flags(ecc_key* key, word32 flags);
522 WOLFSSL_API
523 void wc_ecc_fp_free(void);
524 
525 WOLFSSL_API
526 int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id);
527 
528 WOLFSSL_API
529 int wc_ecc_is_valid_idx(int n);
530 WOLFSSL_API
531 int wc_ecc_get_curve_idx(int curve_id);
532 WOLFSSL_API
533 int wc_ecc_get_curve_id(int curve_idx);
534 #define wc_ecc_get_curve_name_from_id wc_ecc_get_name
535 WOLFSSL_API
536 int wc_ecc_get_curve_size_from_id(int curve_id);
537 
538 WOLFSSL_API
539 int wc_ecc_get_curve_idx_from_name(const char* curveName);
540 WOLFSSL_API
541 int wc_ecc_get_curve_size_from_name(const char* curveName);
542 WOLFSSL_API
543 int wc_ecc_get_curve_id_from_name(const char* curveName);
544 WOLFSSL_API
545 int wc_ecc_get_curve_id_from_params(int fieldSize,
546  const byte* prime, word32 primeSz, const byte* Af, word32 AfSz,
547  const byte* Bf, word32 BfSz, const byte* order, word32 orderSz,
548  const byte* Gx, word32 GxSz, const byte* Gy, word32 GySz, int cofactor);
549 WOLFSSL_API
550 int wc_ecc_get_curve_id_from_dp_params(const ecc_set_type* dp);
551 
552 WOLFSSL_API
553 int wc_ecc_get_curve_id_from_oid(const byte* oid, word32 len);
554 
555 WOLFSSL_API const ecc_set_type* wc_ecc_get_curve_params(int curve_idx);
556 
557 WOLFSSL_API
559 WOLFSSL_API
560 ecc_point* wc_ecc_new_point_h(void* h);
561 WOLFSSL_API
562 void wc_ecc_del_point(ecc_point* p);
563 WOLFSSL_API
564 void wc_ecc_del_point_h(ecc_point* p, void* h);
565 WOLFSSL_API
567 WOLFSSL_API
569 WOLFSSL_API
571 
572 #ifndef WOLFSSL_ATECC508A
573 WOLFSSL_API
574 int wc_ecc_mulmod(mp_int* k, ecc_point *G, ecc_point *R,
575  mp_int* a, mp_int* modulus, int map);
576 WOLFSSL_LOCAL
578  mp_int* a, mp_int* modulus, int map, void* heap);
579 #endif /* !WOLFSSL_ATECC508A */
580 
581 
582 #ifdef HAVE_ECC_KEY_EXPORT
583 /* ASN key helpers */
584 WOLFSSL_API
585 int wc_ecc_export_x963(ecc_key*, byte* out, word32* outLen);
586 WOLFSSL_API
587 int wc_ecc_export_x963_ex(ecc_key*, byte* out, word32* outLen, int compressed);
588  /* extended functionality with compressed option */
589 #endif /* HAVE_ECC_KEY_EXPORT */
590 
591 #ifdef HAVE_ECC_KEY_IMPORT
592 WOLFSSL_ABI WOLFSSL_API
593 int wc_ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
594 WOLFSSL_API
595 int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
596  int curve_id);
597 WOLFSSL_API
598 int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
599  word32 pubSz, ecc_key* key);
600 WOLFSSL_API
601 int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz,
602  const byte* pub, word32 pubSz, ecc_key* key, int curve_id);
603 WOLFSSL_API
604 int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen);
605 WOLFSSL_API
606 int wc_ecc_rs_raw_to_sig(const byte* r, word32 rSz, const byte* s, word32 sSz,
607  byte* out, word32* outlen);
608 WOLFSSL_API
609 int wc_ecc_sig_to_rs(const byte* sig, word32 sigLen, byte* r, word32* rLen,
610  byte* s, word32* sLen);
611 WOLFSSL_API
612 int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy,
613  const char* d, const char* curveName);
614 WOLFSSL_API
615 int wc_ecc_import_raw_ex(ecc_key* key, const char* qx, const char* qy,
616  const char* d, int curve_id);
617 WOLFSSL_API
618 int wc_ecc_import_unsigned(ecc_key* key, byte* qx, byte* qy,
619  byte* d, int curve_id);
620 #endif /* HAVE_ECC_KEY_IMPORT */
621 
622 #ifdef HAVE_ECC_KEY_EXPORT
623 WOLFSSL_API
624 int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen,
625  byte* qy, word32* qyLen, byte* d, word32* dLen,
626  int encType);
627 WOLFSSL_API
628 int wc_ecc_export_private_only(ecc_key* key, byte* out, word32* outLen);
629 WOLFSSL_API
630 int wc_ecc_export_public_raw(ecc_key* key, byte* qx, word32* qxLen,
631  byte* qy, word32* qyLen);
632 WOLFSSL_API
633 int wc_ecc_export_private_raw(ecc_key* key, byte* qx, word32* qxLen,
634  byte* qy, word32* qyLen, byte* d, word32* dLen);
635 #endif /* HAVE_ECC_KEY_EXPORT */
636 
637 #ifdef HAVE_ECC_KEY_EXPORT
638 WOLFSSL_API
639 int wc_ecc_export_point_der_ex(const int curve_idx, ecc_point* point, byte* out,
640  word32* outLen, int compressed);
641 WOLFSSL_API
642 int wc_ecc_export_point_der(const int curve_idx, ecc_point* point,
643  byte* out, word32* outLen);
644 WOLFSSL_LOCAL
645 int wc_ecc_export_point_der_compressed(const int curve_idx, ecc_point* point,
646  byte* out, word32* outLen);
647 #endif /* HAVE_ECC_KEY_EXPORT */
648 
649 
650 #ifdef HAVE_ECC_KEY_IMPORT
651 WOLFSSL_API
652 int wc_ecc_import_point_der_ex(byte* in, word32 inLen, const int curve_idx,
653  ecc_point* point, int shortKeySize);
654 WOLFSSL_API
655 int wc_ecc_import_point_der(byte* in, word32 inLen, const int curve_idx,
656  ecc_point* point);
657 #endif /* HAVE_ECC_KEY_IMPORT */
658 
659 /* size helper */
660 WOLFSSL_API
661 int wc_ecc_size(ecc_key* key);
662 WOLFSSL_API
663 int wc_ecc_sig_size_calc(int sz);
664 WOLFSSL_API
665 int wc_ecc_sig_size(ecc_key* key);
666 
667 WOLFSSL_API
668 int wc_ecc_get_oid(word32 oidSum, const byte** oid, word32* oidSz);
669 
670 #ifdef WOLFSSL_CUSTOM_CURVES
671  WOLFSSL_API
672  int wc_ecc_set_custom_curve(ecc_key* key, const ecc_set_type* dp);
673 #endif
674 
675 #ifdef HAVE_ECC_ENCRYPT
676 /* ecc encrypt */
677 
678 enum ecEncAlgo {
679  ecAES_128_CBC = 1, /* default */
680  ecAES_256_CBC = 2
681 };
682 
683 enum ecKdfAlgo {
684  ecHKDF_SHA256 = 1, /* default */
685  ecHKDF_SHA1 = 2
686 };
687 
688 enum ecMacAlgo {
689  ecHMAC_SHA256 = 1, /* default */
690  ecHMAC_SHA1 = 2
691 };
692 
693 enum {
694  KEY_SIZE_128 = 16,
695  KEY_SIZE_256 = 32,
696  IV_SIZE_64 = 8,
697  IV_SIZE_128 = 16,
698  EXCHANGE_SALT_SZ = 16,
699  EXCHANGE_INFO_SZ = 23
700 };
701 
702 enum ecFlags {
703  REQ_RESP_CLIENT = 1,
704  REQ_RESP_SERVER = 2
705 };
706 
707 
708 typedef struct ecEncCtx ecEncCtx;
709 
710 WOLFSSL_API
711 ecEncCtx* wc_ecc_ctx_new(int flags, WC_RNG* rng);
712 WOLFSSL_API
713 ecEncCtx* wc_ecc_ctx_new_ex(int flags, WC_RNG* rng, void* heap);
714 WOLFSSL_API
715 void wc_ecc_ctx_free(ecEncCtx*);
716 WOLFSSL_API
717 int wc_ecc_ctx_reset(ecEncCtx*, WC_RNG*); /* reset for use again w/o alloc/free */
718 
719 WOLFSSL_API
720 const byte* wc_ecc_ctx_get_own_salt(ecEncCtx*);
721 WOLFSSL_API
722 int wc_ecc_ctx_set_peer_salt(ecEncCtx*, const byte* salt);
723 WOLFSSL_API
724 int wc_ecc_ctx_set_info(ecEncCtx*, const byte* info, int sz);
725 
726 WOLFSSL_API
727 int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
728  word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
729 WOLFSSL_API
730 int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
731  word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
732 
733 #endif /* HAVE_ECC_ENCRYPT */
734 
735 #ifdef HAVE_X963_KDF
736 WOLFSSL_API int wc_X963_KDF(enum wc_HashType type, const byte* secret,
737  word32 secretSz, const byte* sinfo, word32 sinfoSz,
738  byte* out, word32 outSz);
739 #endif
740 
741 #ifdef ECC_CACHE_CURVE
742 WOLFSSL_API int wc_ecc_curve_cache_init(void);
743 WOLFSSL_API void wc_ecc_curve_cache_free(void);
744 #endif
745 
746 WOLFSSL_API
747 int wc_ecc_gen_k(WC_RNG* rng, int size, mp_int* k, mp_int* order);
748 
749 #ifdef WOLFSSL_DSP
750 WOLFSSL_API
751 int wc_ecc_set_handle(ecc_key* key, remote_handle64 handle);
752 WOLFSSL_LOCAL
753 int sp_dsp_ecc_verify_256(remote_handle64 handle, const byte* hash, word32 hashLen, mp_int* pX,
754  mp_int* pY, mp_int* pZ, mp_int* r, mp_int* sm, int* res, void* heap);
755 #endif
756 
757 #ifdef __cplusplus
758  } /* extern "C" */
759 #endif
760 
761 #endif /* HAVE_ECC */
762 #endif /* WOLF_CRYPT_ECC_H */
WOLFSSL_API int wc_ecc_export_private_only(ecc_key *key, byte *out, word32 *outLen)
This function exports only the private key from an ecc_key structure. It stores the private key in th...
Definition: ecc.c:7420
WOLFSSL_API int wc_ecc_is_valid_idx(int n)
Checks if an ECC idx is valid.
Definition: ecc.c:3145
WOLFSSL_API int wc_ecc_rs_raw_to_sig(const byte *r, word32 rSz, const byte *s, word32 sSz, byte *out, word32 *outlen)
Definition: ecc.c:7637
WOLFSSL_API int wc_ecc_rs_to_sig(const char *r, const char *s, byte *out, word32 *outlen)
This function converts the R and S portions of an ECC signature into a DER-encoded ECDSA signature...
Definition: ecc.c:7570
WOLFSSL_API int wc_ecc_ctx_set_info(ecEncCtx *, const byte *info, int sz)
This function can optionally be called before or after wc_ecc_ctx_set_peer_salt. It sets optional inf...
Definition: ecc.c:9718
WOLFSSL_API ecc_point * wc_ecc_new_point(void)
Allocate a new ECC point.
Definition: ecc.c:3060
WOLFSSL_API int wc_ecc_sign_hash_ex(const byte *in, word32 inlen, WC_RNG *rng, ecc_key *key, mp_int *r, mp_int *s)
Sign a message digest.
Definition: ecc.c:4848
Definition: ecc.h:318
WOLFSSL_API int wc_ecc_ctx_set_peer_salt(ecEncCtx *, const byte *salt)
This function sets the peer salt of an ecEncCtx object.
Definition: ecc.c:9732
WOLFSSL_API int wc_ecc_free(ecc_key *key)
This function frees an ecc_key object after it has been used.
Definition: ecc.c:5312
WOLFSSL_API int wc_ecc_export_x963(ecc_key *, byte *out, word32 *outLen)
This function exports the ECC key from the ecc_key structure, storing the result in out...
Definition: ecc.c:6610
ECC_API int ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, mp_int *a, mp_int *modulus, mp_digit mp)
Definition: ecc.c:1526
WOLFSSL_API const byte * wc_ecc_ctx_get_own_salt(ecEncCtx *)
This function returns the salt of an ecEncCtx object. This function should only be called when the ec...
Definition: ecc.c:9687
WOLFSSL_API int wc_ecc_sig_size(ecc_key *key)
This function returns the worst case size for an ECC signature, given by: (keySz * 2) + SIG_HEADER_SZ...
Definition: ecc.c:8033
WOLFSSL_API int wc_ecc_sig_to_rs(const byte *sig, word32 sigLen, byte *r, word32 *rLen, byte *s, word32 *sLen)
Definition: ecc.c:7705
Definition: integer.h:200
WOLFSSL_API int wc_ecc_export_point_der(const int curve_idx, ecc_point *point, byte *out, word32 *outLen)
Export point to der.
Definition: ecc.c:6479
WOLFSSL_API int wc_ecc_make_key(WC_RNG *rng, int keysize, ecc_key *key)
This function generates a new ecc_key and stores it in key.
Definition: ecc.c:4426
ECC_API int ecc_map_ex(ecc_point *, mp_int *, mp_digit, int ct)
Definition: ecc.c:2292
WOLFSSL_API int wc_ecc_decrypt(ecc_key *privKey, ecc_key *pubKey, const byte *msg, word32 msgSz, byte *out, word32 *outSz, ecEncCtx *ctx)
This function decrypts the ciphertext from msg to out. This function takes an optional ctx object as ...
Definition: ecc.c:10064
WOLFSSL_API int wc_ecc_ctx_reset(ecEncCtx *, WC_RNG *)
This function resets an ecEncCtx structure to avoid having to free and allocate a new context object...
Definition: ecc.c:9813
Definition: ecc.h:228
WOLFSSL_API int wc_ecc_shared_secret_ex(ecc_key *private_key, ecc_point *point, byte *out, word32 *outlen)
Create an ECC shared secret between private key and public point.
Definition: ecc.c:3815
WOLFSSL_API int wc_ecc_mulmod(mp_int *k, ecc_point *G, ecc_point *R, mp_int *a, mp_int *modulus, int map)
Perform ECC Fixed Point multiplication.
Definition: ecc.c:3014
WOLFSSL_API int wc_ecc_export_x963_ex(ecc_key *, byte *out, word32 *outLen, int compressed)
This function exports the ECC key from the ecc_key structure, storing the result in out...
Definition: ecc.c:6690
WOLFSSL_API int wc_ecc_import_raw_ex(ecc_key *key, const char *qx, const char *qy, const char *d, int curve_id)
Definition: ecc.c:7950
WOLFSSL_API int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte *hash, word32 hashlen, int *stat, ecc_key *key)
Verify an ECC signature. Result is written to stat. 1 is valid, 0 is invalid. Note: Do not use the re...
Definition: ecc.c:5835
WOLFSSL_API int wc_ecc_make_key_ex(WC_RNG *rng, int keysize, ecc_key *key, int curve_id)
This function generates a new ecc_key and stores it in key.
Definition: ecc.c:4126
WOLFSSL_API int wc_ecc_encrypt(ecc_key *privKey, ecc_key *pubKey, const byte *msg, word32 msgSz, byte *out, word32 *outSz, ecEncCtx *ctx)
This function encrypts the given input message from msg to out. This function takes an optional ctx o...
Definition: ecc.c:9895
ECC_API int ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int *a, mp_int *modulus, mp_digit mp)
Definition: ecc.c:1932
WOLFSSL_LOCAL int wc_ecc_mulmod_ex(mp_int *k, ecc_point *G, ecc_point *R, mp_int *a, mp_int *modulus, int map, void *heap)
Definition: ecc.c:9498
WOLFSSL_API int wc_ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, byte *out, word32 *outlen)
This function generates a new secret key using a local private key and a received public key...
Definition: ecc.c:3533
WOLFSSL_API ecEncCtx * wc_ecc_ctx_new(int flags, WC_RNG *rng)
This function allocates and initializes space for a new ECC context object to allow secure message ex...
Definition: ecc.c:9845
Definition: cryptoCell.h:91
WOLFSSL_API int wc_ecc_import_x963(const byte *in, word32 inLen, ecc_key *key)
This function imports a public ECC key from a buffer containing the key stored in ANSI X9...
Definition: ecc.c:7352
WOLFSSL_API int wc_ecc_check_key(ecc_key *key)
Perform sanity checks on ecc key validity.
Definition: ecc.c:7022
WOLFSSL_API int wc_ecc_import_private_key(const byte *priv, word32 privSz, const byte *pub, word32 pubSz, ecc_key *key)
This function imports a public/private ECC key pair from a buffer containing the raw private key...
Definition: ecc.c:7553
Definition: ecc.h:357
WOLFSSL_API void wc_ecc_fp_free(void)
This function frees the fixed-point cache, which can be used with ecc to speed up computation times...
Definition: ecc.c:9620
WOLFSSL_API int wc_ecc_verify_hash(const byte *sig, word32 siglen, const byte *hash, word32 hashlen, int *stat, ecc_key *key)
This function verifies the ECC signature of a hash to ensure authenticity. It returns the answer thro...
Definition: ecc.c:5704
Definition: random.h:153
WOLFSSL_API int wc_ecc_import_point_der(byte *in, word32 inLen, const int curve_idx, ecc_point *point)
Import point from der format.
Definition: ecc.c:6456
Definition: ecc.h:332
WOLFSSL_API int wc_ecc_point_is_at_infinity(ecc_point *p)
Checks if a point is at infinity. Returns 1 if point is at infinity, 0 if not, < 0 on error...
Definition: ecc.c:3887
WOLFSSL_API void wc_ecc_ctx_free(ecEncCtx *)
This function frees the ecEncCtx object used for encrypting and decrypting messages.
Definition: ecc.c:9852
WOLFSSL_API void wc_ecc_del_point(ecc_point *p)
Free an ECC point from memory.
Definition: ecc.c:3082
WOLFSSL_API int wc_ecc_import_raw(ecc_key *key, const char *qx, const char *qy, const char *d, const char *curveName)
This function fills an ecc_key structure with the raw components of an ECC signature.
Definition: ecc.c:7976
WOLFSSL_API int wc_ecc_cmp_point(ecc_point *a, ecc_point *b)
Compare the value of a point with another one.
Definition: ecc.c:3119
WOLFSSL_API int wc_ecc_sig_size_calc(int sz)
This function returns the worst case size for an ECC signature, given by: (keySz * 2) + SIG_HEADER_SZ...
Definition: ecc.c:8016
WOLFSSL_API int wc_ecc_size(ecc_key *key)
This function returns the key size of an ecc_key structure in octets.
Definition: ecc.c:8007
Definition: asn.h:758
WOLFSSL_API int wc_ecc_init(ecc_key *key)
This function initializes an ecc_key object for future use with message verification or key negotiati...
Definition: ecc.c:4495
WOLFSSL_API ecc_point * wc_ecc_new_point_h(void *h)
Definition: ecc.c:3026
ECC_API int ecc_mul2add(ecc_point *A, mp_int *kA, ecc_point *B, mp_int *kB, ecc_point *C, mp_int *a, mp_int *modulus, void *heap)
Definition: ecc.c:9375
WOLFSSL_API int wc_ecc_copy_point(ecc_point *p, ecc_point *r)
Copy the value of one point to another one.
Definition: ecc.c:3092
WOLFSSL_API int wc_ecc_sign_hash(const byte *in, word32 inlen, byte *out, word32 *outlen, WC_RNG *rng, ecc_key *key)
This function signs a message digest using an ecc_key object to guarantee authenticity.
Definition: ecc.c:4749