source: azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfssl/wolfcrypt/ecc.h@ 464

Last change on this file since 464 was 464, checked in by coas-nagasima, 3 years ago

WolfSSLとAzure IoT SDKを更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 25.5 KB
Line 
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
22/*!
23 \file wolfssl/wolfcrypt/ecc.h
24*/
25
26
27#ifndef WOLF_CRYPT_ECC_H
28#define WOLF_CRYPT_ECC_H
29
30#include <wolfssl/wolfcrypt/types.h>
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>
40#include <wolfssl/wolfcrypt/random.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#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
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 WOLFSSL_SILABS_SE_ACCEL
62 #include <wolfssl/wolfcrypt/port/silabs/silabs_ecc.h>
63#endif
64
65#ifdef WOLFSSL_HAVE_SP_ECC
66 #include <wolfssl/wolfcrypt/sp_int.h>
67#endif
68
69
70#ifdef __cplusplus
71 extern "C" {
72#endif
73
74
75/* Enable curve B parameter if needed */
76#if defined(HAVE_COMP_KEY) || defined(ECC_CACHE_CURVE)
77 #ifndef USE_ECC_B_PARAM /* Allow someone to force enable */
78 #define USE_ECC_B_PARAM
79 #endif
80#endif
81
82
83/* Use this as the key->idx if a custom ecc_set is used for key->dp */
84#define ECC_CUSTOM_IDX (-1)
85
86
87/* Determine max ECC bits based on enabled curves */
88#if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
89 #define MAX_ECC_BITS 521
90#elif defined(HAVE_ECC512)
91 #define MAX_ECC_BITS 512
92#elif defined(HAVE_ECC384)
93 #define MAX_ECC_BITS 384
94#elif defined(HAVE_ECC320)
95 #define MAX_ECC_BITS 320
96#elif !defined(NO_ECC256)
97 #define MAX_ECC_BITS 256
98#elif defined(HAVE_ECC239)
99 #define MAX_ECC_BITS 239
100#elif defined(HAVE_ECC224)
101 #define MAX_ECC_BITS 224
102#elif defined(HAVE_ECC192)
103 #define MAX_ECC_BITS 192
104#elif defined(HAVE_ECC160)
105 #define MAX_ECC_BITS 160
106#elif defined(HAVE_ECC128)
107 #define MAX_ECC_BITS 128
108#elif defined(HAVE_ECC112)
109 #define MAX_ECC_BITS 112
110#endif
111
112/* calculate max ECC bytes */
113#if ((MAX_ECC_BITS * 2) % 8) == 0
114 #define MAX_ECC_BYTES (MAX_ECC_BITS / 8)
115#else
116 /* add byte if not aligned */
117 #define MAX_ECC_BYTES ((MAX_ECC_BITS / 8) + 1)
118#endif
119
120#ifndef ECC_MAX_PAD_SZ
121 /* ECC maximum padding size (when MSB is set extra byte required for R and S) */
122 #define ECC_MAX_PAD_SZ 2
123#endif
124
125enum {
126 ECC_PUBLICKEY = 1,
127 ECC_PRIVATEKEY = 2,
128 ECC_PRIVATEKEY_ONLY = 3,
129 ECC_MAXNAME = 16, /* MAX CURVE NAME LENGTH */
130 SIG_HEADER_SZ = 7, /* ECC signature header size (30 81 87 02 42 [R] 02 42 [S]) */
131 ECC_BUFSIZE = 256, /* for exported keys temp buffer */
132 ECC_MINSIZE = ECC_MIN_KEY_SZ/8, /* MIN Private Key size */
133 ECC_MAXSIZE = 66, /* MAX Private Key size */
134 ECC_MAXSIZE_GEN = 74, /* MAX Buffer size required when generating ECC keys*/
135 ECC_MAX_OID_LEN = 16,
136 ECC_MAX_SIG_SIZE= ((MAX_ECC_BYTES * 2) + ECC_MAX_PAD_SZ + SIG_HEADER_SZ),
137
138 /* max crypto hardware size */
139#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
140 ECC_MAX_CRYPTO_HW_SIZE = ATECC_KEY_SIZE, /* from port/atmel/atmel.h */
141 ECC_MAX_CRYPTO_HW_PUBKEY_SIZE = (ATECC_KEY_SIZE*2),
142#elif defined(PLUTON_CRYPTO_ECC)
143 ECC_MAX_CRYPTO_HW_SIZE = 32,
144#elif defined(WOLFSSL_SILABS_SE_ACCEL)
145 ECC_MAX_CRYPTO_HW_SIZE = 32,
146#elif defined(WOLFSSL_CRYPTOCELL)
147 #ifndef CRYPTOCELL_KEY_SIZE
148 CRYPTOCELL_KEY_SIZE = ECC_MAXSIZE,
149 #endif
150 ECC_MAX_CRYPTO_HW_SIZE = CRYPTOCELL_KEY_SIZE,
151#endif
152
153 /* point compression type */
154 ECC_POINT_COMP_EVEN = 0x02,
155 ECC_POINT_COMP_ODD = 0x03,
156 ECC_POINT_UNCOMP = 0x04,
157
158 /* Shamir's dual add constants */
159 SHAMIR_PRECOMP_SZ = 16,
160
161#ifdef WOLF_CRYPTO_CB
162 ECC_MAX_ID_LEN = 32,
163 ECC_MAX_LABEL_LEN = 32,
164#endif
165};
166
167/* Curve Types */
168typedef enum ecc_curve_id {
169 ECC_CURVE_INVALID = -1,
170 ECC_CURVE_DEF = 0, /* NIST or SECP */
171
172 /* NIST Prime Curves */
173 ECC_SECP192R1,
174 ECC_PRIME192V2,
175 ECC_PRIME192V3,
176 ECC_PRIME239V1,
177 ECC_PRIME239V2,
178 ECC_PRIME239V3,
179 ECC_SECP256R1,
180
181 /* SECP Curves */
182 ECC_SECP112R1,
183 ECC_SECP112R2,
184 ECC_SECP128R1,
185 ECC_SECP128R2,
186 ECC_SECP160R1,
187 ECC_SECP160R2,
188 ECC_SECP224R1,
189 ECC_SECP384R1,
190 ECC_SECP521R1,
191
192 /* Koblitz */
193 ECC_SECP160K1,
194 ECC_SECP192K1,
195 ECC_SECP224K1,
196 ECC_SECP256K1,
197
198 /* Brainpool Curves */
199 ECC_BRAINPOOLP160R1,
200 ECC_BRAINPOOLP192R1,
201 ECC_BRAINPOOLP224R1,
202 ECC_BRAINPOOLP256R1,
203 ECC_BRAINPOOLP320R1,
204 ECC_BRAINPOOLP384R1,
205 ECC_BRAINPOOLP512R1,
206
207 /* Twisted Edwards Curves */
208#ifdef HAVE_CURVE25519
209 ECC_X25519,
210#endif
211#ifdef HAVE_CURVE448
212 ECC_X448,
213#endif
214
215#ifdef WOLFSSL_CUSTOM_CURVES
216 ECC_CURVE_CUSTOM,
217#endif
218 ECC_CURVE_MAX
219} ecc_curve_id;
220
221#ifdef HAVE_OID_ENCODING
222typedef word16 ecc_oid_t;
223#else
224typedef byte ecc_oid_t;
225 /* OID encoded with ASN scheme:
226 first element = (oid[0] * 40) + oid[1]
227 if any element > 127 then MSB 0x80 indicates additional byte */
228#endif
229
230
231#if !defined(WOLFSSL_ECC_CURVE_STATIC) && defined(USE_WINDOWS_API)
232 /* MSC does something different with the pointers to the arrays than GCC,
233 * and it causes the FIPS checksum to fail. In the case of windows builds,
234 * store everything as arrays instead of pointers to strings. */
235
236 #define WOLFSSL_ECC_CURVE_STATIC
237#endif
238
239/* ECC set type defined a GF(p) curve */
240#ifndef WOLFSSL_ECC_CURVE_STATIC
241typedef struct ecc_set_type {
242 int size; /* The size of the curve in octets */
243 int id; /* id of this curve */
244 const char* name; /* name of this curve */
245 const char* prime; /* prime that defines the field, curve is in (hex) */
246 const char* Af; /* fields A param (hex) */
247 const char* Bf; /* fields B param (hex) */
248 const char* order; /* order of the curve (hex) */
249 const char* Gx; /* x coordinate of the base point on curve (hex) */
250 const char* Gy; /* y coordinate of the base point on curve (hex) */
251 const ecc_oid_t* oid;
252 word32 oidSz;
253 word32 oidSum; /* sum of encoded OID bytes */
254 int cofactor;
255} ecc_set_type;
256#else
257#define MAX_ECC_NAME 16
258#define MAX_ECC_STRING ((MAX_ECC_BYTES * 2) + 1)
259 /* The values are stored as text strings. */
260
261typedef struct ecc_set_type {
262 int size; /* The size of the curve in octets */
263 int id; /* id of this curve */
264 const char name[MAX_ECC_NAME]; /* name of this curve */
265 const char prime[MAX_ECC_STRING]; /* prime that defines the field, curve is in (hex) */
266 const char Af[MAX_ECC_STRING]; /* fields A param (hex) */
267 const char Bf[MAX_ECC_STRING]; /* fields B param (hex) */
268 const char order[MAX_ECC_STRING]; /* order of the curve (hex) */
269 const char Gx[MAX_ECC_STRING]; /* x coordinate of the base point on curve (hex) */
270 const char Gy[MAX_ECC_STRING]; /* y coordinate of the base point on curve (hex) */
271 const ecc_oid_t oid[10];
272 word32 oidSz;
273 word32 oidSum; /* sum of encoded OID bytes */
274 int cofactor;
275} ecc_set_type;
276#endif
277
278
279#ifdef ALT_ECC_SIZE
280
281/* Note on ALT_ECC_SIZE:
282 * The fast math code uses an array of a fixed size to store the big integers.
283 * By default, the array is big enough for RSA keys. There is a size,
284 * FP_MAX_BITS which can be used to make the array smaller when one wants ECC
285 * but not RSA. Some people want fast math sized for both RSA and ECC, where
286 * ECC won't use as much as RSA. The flag ALT_ECC_SIZE switches in an alternate
287 * ecc_point structure that uses an alternate fp_int that has a shorter array
288 * of fp_digits.
289 *
290 * Now, without ALT_ECC_SIZE, the ecc_point has three single item arrays of
291 * mp_ints for the components of the point. With ALT_ECC_SIZE, the components
292 * of the point are pointers that are set to each of a three item array of
293 * alt_fp_ints. While an mp_int will have 4096 bits of digit inside the
294 * structure, the alt_fp_int will only have 512 bits for ECC 256-bit and
295 * 1056-bits for ECC 521-bit. A size value was added in the ALT case, as well,
296 * and is set by mp_init() and alt_fp_init(). The functions fp_zero() and
297 * fp_copy() use the size parameter. An int needs to be initialized before
298 * using it instead of just fp_zeroing it, the init will call zero. The
299 * FP_MAX_BITS_ECC defaults to calculating based on MAX_ECC_BITS, but
300 * can be set to change the number of bits used in the alternate FP_INT.
301 *
302 * The ALT_ECC_SIZE option only applies to stack based fast math USE_FAST_MATH.
303 */
304
305#ifndef USE_FAST_MATH
306 #error USE_FAST_MATH must be defined to use ALT_ECC_SIZE
307#endif
308
309/* determine max bits required for ECC math */
310#ifndef FP_MAX_BITS_ECC
311 /* max bits rounded up by 8 then doubled */
312 /* (ROUND8(MAX_ECC_BITS) * 2) */
313 #define FP_MAX_BITS_ECC (2 * \
314 ((MAX_ECC_BITS + DIGIT_BIT - 1) / DIGIT_BIT) * DIGIT_BIT)
315
316 /* Note: For ECC verify only FP_MAX_BITS_ECC can be reduced to:
317 ROUND8(MAX_ECC_BITS) + ROUND8(DIGIT_BIT) */
318#endif
319
320/* verify alignment */
321#if FP_MAX_BITS_ECC % CHAR_BIT
322 #error FP_MAX_BITS_ECC must be a multiple of CHAR_BIT
323#endif
324
325/* determine buffer size */
326/* Add one to accommodate extra digit used by sp_mul(), sp_mulmod(), sp_sqr(), and sp_sqrmod(). */
327#define FP_SIZE_ECC ((FP_MAX_BITS_ECC/DIGIT_BIT) + 1)
328
329
330/* This needs to match the size of the fp_int struct, except the
331 * fp_digit array will be shorter. */
332typedef struct alt_fp_int {
333 int used, sign, size;
334 mp_digit dp[FP_SIZE_ECC];
335} alt_fp_int;
336#endif /* ALT_ECC_SIZE */
337
338#ifndef WC_ECCKEY_TYPE_DEFINED
339 typedef struct ecc_key ecc_key;
340 #define WC_ECCKEY_TYPE_DEFINED
341#endif
342
343
344/* A point on an ECC curve, stored in Jacobian format such that (x,y,z) =>
345 (x/z^2, y/z^3, 1) when interpreted as affine */
346typedef struct {
347#ifndef ALT_ECC_SIZE
348 mp_int x[1]; /* The x coordinate */
349 mp_int y[1]; /* The y coordinate */
350 mp_int z[1]; /* The z coordinate */
351#else
352 mp_int* x; /* The x coordinate */
353 mp_int* y; /* The y coordinate */
354 mp_int* z; /* The z coordinate */
355 alt_fp_int xyz[3];
356#endif
357#ifdef WOLFSSL_SMALL_STACK_CACHE
358 ecc_key* key;
359#endif
360} ecc_point;
361
362/* ECC Flags */
363enum {
364 WC_ECC_FLAG_NONE = 0x00,
365#ifdef HAVE_ECC_CDH
366 WC_ECC_FLAG_COFACTOR = 0x01,
367#endif
368 WC_ECC_FLAG_DEC_SIGN = 0x02,
369};
370
371/* ECC non-blocking */
372#ifdef WC_ECC_NONBLOCK
373 typedef struct ecc_nb_ctx {
374 #if defined(WOLFSSL_HAVE_SP_ECC) && defined(WOLFSSL_SP_NONBLOCK)
375 sp_ecc_ctx_t sp_ctx;
376 #else
377 /* build configuration not supported */
378 #error ECC non-blocking only supports SP (--enable-sp=nonblock)
379 #endif
380 } ecc_nb_ctx_t;
381#endif /* WC_ECC_NONBLOCK */
382
383
384/* An ECC Key */
385struct ecc_key {
386 int type; /* Public or Private */
387 int idx; /* Index into the ecc_sets[] for the parameters of
388 this curve if -1, this key is using user supplied
389 curve in dp */
390 int state;
391 word32 flags;
392 const ecc_set_type* dp; /* domain parameters, either points to NIST
393 curves (idx >= 0) or user supplied */
394#ifdef WOLFSSL_CUSTOM_CURVES
395 int deallocSet;
396#endif
397 void* heap; /* heap hint */
398 ecc_point pubkey; /* public key */
399 mp_int k; /* private key */
400#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
401 int slot; /* Key Slot Number (-1 unknown) */
402 byte pubkey_raw[ECC_MAX_CRYPTO_HW_PUBKEY_SIZE];
403#endif
404#if defined(PLUTON_CRYPTO_ECC) || defined(WOLF_CRYPTO_CB)
405 int devId;
406#endif
407#ifdef WOLFSSL_SILABS_SE_ACCEL
408 sl_se_command_context_t cmd_ctx;
409 sl_se_key_descriptor_t key;
410 /* Used for SiLabs "plaintext" with public X, public Y, and
411 * private D concatenated. These are respectively at offset `0`,
412 * offset `keysize`, and offset `2 * keysize`.
413 */
414 byte key_raw[3 * ECC_MAX_CRYPTO_HW_SIZE];
415#endif
416
417#ifdef WOLFSSL_ASYNC_CRYPT
418 mp_int* r; /* sign/verify temps */
419 mp_int* s;
420 WC_ASYNC_DEV asyncDev;
421 #ifdef HAVE_CAVIUM_V
422 mp_int* e; /* Sign, Verify and Shared Secret */
423 mp_int* signK;
424 #endif
425 #ifdef WOLFSSL_CERT_GEN
426 CertSignCtx certSignCtx; /* context info for cert sign (MakeSignature) */
427 #endif
428#endif /* WOLFSSL_ASYNC_CRYPT */
429#ifdef WOLF_CRYPTO_CB
430 byte id[ECC_MAX_ID_LEN];
431 int idLen;
432 char label[ECC_MAX_LABEL_LEN];
433 int labelLen;
434#endif
435#if defined(WOLFSSL_CRYPTOCELL)
436 ecc_context_t ctx;
437#endif
438
439#ifdef WOLFSSL_ECDSA_SET_K
440 mp_int *sign_k;
441#endif
442
443#ifdef WOLFSSL_SMALL_STACK_CACHE
444 mp_int* t1;
445 mp_int* t2;
446#ifdef ALT_ECC_SIZE
447 mp_int* x;
448 mp_int* y;
449 mp_int* z;
450#endif
451#endif
452
453#ifdef WOLFSSL_DSP
454 remote_handle64 handle;
455#endif
456#ifdef ECC_TIMING_RESISTANT
457 WC_RNG* rng;
458#endif
459#ifdef WC_ECC_NONBLOCK
460 ecc_nb_ctx_t* nb_ctx;
461#endif
462};
463
464
465WOLFSSL_ABI WOLFSSL_API ecc_key* wc_ecc_key_new(void*);
466WOLFSSL_ABI WOLFSSL_API void wc_ecc_key_free(ecc_key*);
467
468
469/* ECC predefined curve sets */
470extern const ecc_set_type ecc_sets[];
471extern const size_t ecc_sets_count;
472
473WOLFSSL_API
474const char* wc_ecc_get_name(int curve_id);
475
476#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
477
478#ifdef WOLFSSL_PUBLIC_ECC_ADD_DBL
479 #define ECC_API WOLFSSL_API
480#else
481 #define ECC_API WOLFSSL_LOCAL
482#endif
483
484ECC_API int ecc_mul2add(ecc_point* A, mp_int* kA,
485 ecc_point* B, mp_int* kB,
486 ecc_point* C, mp_int* a, mp_int* modulus, void* heap);
487
488ECC_API int ecc_map(ecc_point*, mp_int*, mp_digit);
489ECC_API int ecc_map_ex(ecc_point*, mp_int*, mp_digit, int ct);
490ECC_API int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
491 mp_int* a, mp_int* modulus, mp_digit mp);
492ECC_API int ecc_projective_dbl_point(ecc_point* P, ecc_point* R, mp_int* a,
493 mp_int* modulus, mp_digit mp);
494
495WOLFSSL_LOCAL
496int ecc_projective_add_point_safe(ecc_point* A, ecc_point* B, ecc_point* R,
497 mp_int* a, mp_int* modulus, mp_digit mp, int* infinity);
498WOLFSSL_LOCAL
499int ecc_projective_dbl_point_safe(ecc_point* P, ecc_point* R, mp_int* a,
500 mp_int* modulus, mp_digit mp);
501
502#endif
503
504WOLFSSL_API
505int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key);
506WOLFSSL_ABI WOLFSSL_API
507int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id);
508WOLFSSL_API
509int wc_ecc_make_key_ex2(WC_RNG* rng, int keysize, ecc_key* key, int curve_id,
510 int flags);
511WOLFSSL_API
512int wc_ecc_make_pub(ecc_key* key, ecc_point* pubOut);
513WOLFSSL_API
514int wc_ecc_make_pub_ex(ecc_key* key, ecc_point* pubOut, WC_RNG* rng);
515WOLFSSL_API
516int wc_ecc_check_key(ecc_key* key);
517WOLFSSL_API
518int wc_ecc_is_point(ecc_point* ecp, mp_int* a, mp_int* b, mp_int* prime);
519WOLFSSL_API
520int wc_ecc_get_generator(ecc_point* ecp, int curve_idx);
521
522#ifdef HAVE_ECC_DHE
523WOLFSSL_API
524int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
525 word32* outlen);
526WOLFSSL_LOCAL
527int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point,
528 byte* out, word32 *outlen);
529WOLFSSL_API
530int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
531 byte* out, word32 *outlen);
532
533#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
534 defined(PLUTON_CRYPTO_ECC) || defined(WOLFSSL_CRYPTOCELL)
535#define wc_ecc_shared_secret_ssh wc_ecc_shared_secret
536#else
537#define wc_ecc_shared_secret_ssh wc_ecc_shared_secret_ex /* For backwards compat */
538#endif
539
540#endif /* HAVE_ECC_DHE */
541
542#ifdef HAVE_ECC_SIGN
543WOLFSSL_ABI WOLFSSL_API
544int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
545 WC_RNG* rng, ecc_key* key);
546WOLFSSL_API
547int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
548 ecc_key* key, mp_int *r, mp_int *s);
549#ifdef WOLFSSL_ECDSA_SET_K
550WOLFSSL_API
551int wc_ecc_sign_set_k(const byte* k, word32 klen, ecc_key* key);
552#endif
553#endif /* HAVE_ECC_SIGN */
554
555#ifdef HAVE_ECC_VERIFY
556WOLFSSL_API
557int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
558 word32 hashlen, int* stat, ecc_key* key);
559WOLFSSL_API
560int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
561 word32 hashlen, int* stat, ecc_key* key);
562#endif /* HAVE_ECC_VERIFY */
563
564WOLFSSL_API
565int wc_ecc_init(ecc_key* key);
566WOLFSSL_ABI WOLFSSL_API
567int wc_ecc_init_ex(ecc_key* key, void* heap, int devId);
568#ifdef WOLF_CRYPTO_CB
569WOLFSSL_API
570int wc_ecc_init_id(ecc_key* key, unsigned char* id, int len, void* heap,
571 int devId);
572WOLFSSL_API
573int wc_ecc_init_label(ecc_key* key, const char* label, void* heap, int devId);
574#endif
575#ifdef WOLFSSL_CUSTOM_CURVES
576WOLFSSL_LOCAL
577void wc_ecc_free_curve(const ecc_set_type* curve, void* heap);
578#endif
579WOLFSSL_ABI WOLFSSL_API
580int wc_ecc_free(ecc_key* key);
581WOLFSSL_API
582int wc_ecc_set_flags(ecc_key* key, word32 flags);
583WOLFSSL_API
584void wc_ecc_fp_free(void);
585WOLFSSL_LOCAL
586void wc_ecc_fp_init(void);
587#ifdef ECC_TIMING_RESISTANT
588WOLFSSL_API
589int wc_ecc_set_rng(ecc_key* key, WC_RNG* rng);
590#endif
591
592WOLFSSL_API
593int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id);
594
595WOLFSSL_API
596int wc_ecc_is_valid_idx(int n);
597WOLFSSL_API
598int wc_ecc_get_curve_idx(int curve_id);
599WOLFSSL_API
600int wc_ecc_get_curve_id(int curve_idx);
601#define wc_ecc_get_curve_name_from_id wc_ecc_get_name
602WOLFSSL_API
603int wc_ecc_get_curve_size_from_id(int curve_id);
604
605WOLFSSL_API
606int wc_ecc_get_curve_idx_from_name(const char* curveName);
607WOLFSSL_API
608int wc_ecc_get_curve_size_from_name(const char* curveName);
609WOLFSSL_API
610int wc_ecc_get_curve_id_from_name(const char* curveName);
611WOLFSSL_API
612int wc_ecc_get_curve_id_from_params(int fieldSize,
613 const byte* prime, word32 primeSz, const byte* Af, word32 AfSz,
614 const byte* Bf, word32 BfSz, const byte* order, word32 orderSz,
615 const byte* Gx, word32 GxSz, const byte* Gy, word32 GySz, int cofactor);
616WOLFSSL_API
617int wc_ecc_get_curve_id_from_dp_params(const ecc_set_type* dp);
618
619WOLFSSL_API
620int wc_ecc_get_curve_id_from_oid(const byte* oid, word32 len);
621
622WOLFSSL_API const ecc_set_type* wc_ecc_get_curve_params(int curve_idx);
623
624WOLFSSL_API
625ecc_point* wc_ecc_new_point(void);
626WOLFSSL_API
627ecc_point* wc_ecc_new_point_h(void* h);
628WOLFSSL_API
629void wc_ecc_del_point(ecc_point* p);
630WOLFSSL_API
631void wc_ecc_del_point_h(ecc_point* p, void* h);
632WOLFSSL_API
633int wc_ecc_copy_point(ecc_point* p, ecc_point *r);
634WOLFSSL_API
635int wc_ecc_cmp_point(ecc_point* a, ecc_point *b);
636WOLFSSL_API
637int wc_ecc_point_is_at_infinity(ecc_point *p);
638WOLFSSL_API
639int wc_ecc_point_is_on_curve(ecc_point *p, int curve_idx);
640
641#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
642WOLFSSL_API
643int wc_ecc_mulmod(mp_int* k, ecc_point *G, ecc_point *R,
644 mp_int* a, mp_int* modulus, int map);
645WOLFSSL_LOCAL
646int wc_ecc_mulmod_ex(mp_int* k, ecc_point *G, ecc_point *R,
647 mp_int* a, mp_int* modulus, int map, void* heap);
648WOLFSSL_LOCAL
649int wc_ecc_mulmod_ex2(mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
650 mp_int* modulus, mp_int* order, WC_RNG* rng, int map,
651 void* heap);
652#endif /* !WOLFSSL_ATECC508A */
653
654
655#ifdef HAVE_ECC_KEY_EXPORT
656/* ASN key helpers */
657WOLFSSL_API
658int wc_ecc_export_x963(ecc_key*, byte* out, word32* outLen);
659WOLFSSL_API
660int wc_ecc_export_x963_ex(ecc_key*, byte* out, word32* outLen, int compressed);
661 /* extended functionality with compressed option */
662#endif /* HAVE_ECC_KEY_EXPORT */
663
664#ifdef HAVE_ECC_KEY_IMPORT
665WOLFSSL_ABI WOLFSSL_API
666int wc_ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
667WOLFSSL_API
668int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
669 int curve_id);
670WOLFSSL_API
671int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
672 word32 pubSz, ecc_key* key);
673WOLFSSL_API
674int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz,
675 const byte* pub, word32 pubSz, ecc_key* key, int curve_id);
676WOLFSSL_API
677int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen);
678WOLFSSL_API
679int wc_ecc_rs_raw_to_sig(const byte* r, word32 rSz, const byte* s, word32 sSz,
680 byte* out, word32* outlen);
681WOLFSSL_API
682int wc_ecc_sig_to_rs(const byte* sig, word32 sigLen, byte* r, word32* rLen,
683 byte* s, word32* sLen);
684WOLFSSL_API
685int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy,
686 const char* d, const char* curveName);
687WOLFSSL_API
688int wc_ecc_import_raw_ex(ecc_key* key, const char* qx, const char* qy,
689 const char* d, int curve_id);
690WOLFSSL_API
691int wc_ecc_import_unsigned(ecc_key* key, byte* qx, byte* qy,
692 byte* d, int curve_id);
693#endif /* HAVE_ECC_KEY_IMPORT */
694
695#ifdef HAVE_ECC_KEY_EXPORT
696WOLFSSL_API
697int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen,
698 byte* qy, word32* qyLen, byte* d, word32* dLen,
699 int encType);
700WOLFSSL_API
701int wc_ecc_export_private_only(ecc_key* key, byte* out, word32* outLen);
702WOLFSSL_API
703int wc_ecc_export_public_raw(ecc_key* key, byte* qx, word32* qxLen,
704 byte* qy, word32* qyLen);
705WOLFSSL_API
706int wc_ecc_export_private_raw(ecc_key* key, byte* qx, word32* qxLen,
707 byte* qy, word32* qyLen, byte* d, word32* dLen);
708#endif /* HAVE_ECC_KEY_EXPORT */
709
710#ifdef HAVE_ECC_KEY_EXPORT
711WOLFSSL_API
712int wc_ecc_export_point_der_ex(const int curve_idx, ecc_point* point, byte* out,
713 word32* outLen, int compressed);
714WOLFSSL_API
715int wc_ecc_export_point_der(const int curve_idx, ecc_point* point,
716 byte* out, word32* outLen);
717WOLFSSL_LOCAL
718int wc_ecc_export_point_der_compressed(const int curve_idx, ecc_point* point,
719 byte* out, word32* outLen);
720#endif /* HAVE_ECC_KEY_EXPORT */
721
722
723#ifdef HAVE_ECC_KEY_IMPORT
724WOLFSSL_API
725int wc_ecc_import_point_der_ex(byte* in, word32 inLen, const int curve_idx,
726 ecc_point* point, int shortKeySize);
727WOLFSSL_API
728int wc_ecc_import_point_der(byte* in, word32 inLen, const int curve_idx,
729 ecc_point* point);
730#endif /* HAVE_ECC_KEY_IMPORT */
731
732/* size helper */
733WOLFSSL_API
734int wc_ecc_size(ecc_key* key);
735WOLFSSL_API
736int wc_ecc_sig_size_calc(int sz);
737WOLFSSL_API
738int wc_ecc_sig_size(ecc_key* key);
739
740WOLFSSL_API
741int wc_ecc_get_oid(word32 oidSum, const byte** oid, word32* oidSz);
742
743#ifdef WOLFSSL_CUSTOM_CURVES
744 WOLFSSL_API
745 int wc_ecc_set_custom_curve(ecc_key* key, const ecc_set_type* dp);
746#endif
747
748#ifdef HAVE_ECC_ENCRYPT
749/* ecc encrypt */
750
751enum ecEncAlgo {
752 ecAES_128_CBC = 1, /* default */
753 ecAES_256_CBC = 2
754};
755
756enum ecKdfAlgo {
757 ecHKDF_SHA256 = 1, /* default */
758 ecHKDF_SHA1 = 2
759};
760
761enum ecMacAlgo {
762 ecHMAC_SHA256 = 1, /* default */
763 ecHMAC_SHA1 = 2
764};
765
766enum {
767 KEY_SIZE_128 = 16,
768 KEY_SIZE_256 = 32,
769 IV_SIZE_64 = 8,
770 IV_SIZE_128 = 16,
771 EXCHANGE_SALT_SZ = 16,
772 EXCHANGE_INFO_SZ = 23
773};
774
775enum ecFlags {
776 REQ_RESP_CLIENT = 1,
777 REQ_RESP_SERVER = 2
778};
779
780
781typedef struct ecEncCtx ecEncCtx;
782
783WOLFSSL_API
784ecEncCtx* wc_ecc_ctx_new(int flags, WC_RNG* rng);
785WOLFSSL_API
786ecEncCtx* wc_ecc_ctx_new_ex(int flags, WC_RNG* rng, void* heap);
787WOLFSSL_API
788void wc_ecc_ctx_free(ecEncCtx*);
789WOLFSSL_API
790int wc_ecc_ctx_reset(ecEncCtx*, WC_RNG*); /* reset for use again w/o alloc/free */
791
792WOLFSSL_API
793const byte* wc_ecc_ctx_get_own_salt(ecEncCtx*);
794WOLFSSL_API
795int wc_ecc_ctx_set_peer_salt(ecEncCtx*, const byte* salt);
796WOLFSSL_API
797int wc_ecc_ctx_set_info(ecEncCtx*, const byte* info, int sz);
798
799WOLFSSL_API
800int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
801 word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
802WOLFSSL_API
803int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
804 word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
805
806#endif /* HAVE_ECC_ENCRYPT */
807
808#ifdef HAVE_X963_KDF
809WOLFSSL_API int wc_X963_KDF(enum wc_HashType type, const byte* secret,
810 word32 secretSz, const byte* sinfo, word32 sinfoSz,
811 byte* out, word32 outSz);
812#endif
813
814#ifdef ECC_CACHE_CURVE
815WOLFSSL_API int wc_ecc_curve_cache_init(void);
816WOLFSSL_API void wc_ecc_curve_cache_free(void);
817#endif
818
819WOLFSSL_API
820int wc_ecc_gen_k(WC_RNG* rng, int size, mp_int* k, mp_int* order);
821
822#ifdef WOLFSSL_DSP
823WOLFSSL_API
824int wc_ecc_set_handle(ecc_key* key, remote_handle64 handle);
825WOLFSSL_LOCAL
826int sp_dsp_ecc_verify_256(remote_handle64 handle, const byte* hash, word32 hashLen, mp_int* pX,
827 mp_int* pY, mp_int* pZ, mp_int* r, mp_int* sm, int* res, void* heap);
828#endif
829
830#ifdef WC_ECC_NONBLOCK
831 WOLFSSL_API int wc_ecc_set_nonblock(ecc_key *key, ecc_nb_ctx_t* ctx);
832#endif
833
834#ifdef __cplusplus
835 } /* extern "C" */
836#endif
837
838#endif /* HAVE_ECC */
839#endif /* WOLF_CRYPT_ECC_H */
Note: See TracBrowser for help on using the repository browser.