source: azure_iot_hub_f767zi/trunk/wolfssl-4.4.0/wolfssl/wolfcrypt/ecc.h@ 457

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

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 22.8 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#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
116enum {
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 */
156typedef 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
209typedef word16 ecc_oid_t;
210#else
211typedef 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
228typedef 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
248typedef 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. */
318typedef 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 */
332typedef 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 */
349enum {
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 */
357struct 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
419WOLFSSL_ABI WOLFSSL_API ecc_key* wc_ecc_key_new(void*);
420WOLFSSL_ABI WOLFSSL_API void wc_ecc_key_free(ecc_key*);
421
422
423/* ECC predefined curve sets */
424extern const ecc_set_type ecc_sets[];
425extern const size_t ecc_sets_count;
426
427WOLFSSL_API
428const 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
438ECC_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
442ECC_API int ecc_map(ecc_point*, mp_int*, mp_digit);
443ECC_API int ecc_map_ex(ecc_point*, mp_int*, mp_digit, int ct);
444ECC_API int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
445 mp_int* a, mp_int* modulus, mp_digit mp);
446ECC_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
451WOLFSSL_API
452int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key);
453WOLFSSL_ABI WOLFSSL_API
454int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id);
455WOLFSSL_API
456int wc_ecc_make_pub(ecc_key* key, ecc_point* pubOut);
457WOLFSSL_API
458int wc_ecc_check_key(ecc_key* key);
459WOLFSSL_API
460int wc_ecc_is_point(ecc_point* ecp, mp_int* a, mp_int* b, mp_int* prime);
461WOLFSSL_API
462int wc_ecc_get_generator(ecc_point* ecp, int curve_idx);
463
464#ifdef HAVE_ECC_DHE
465WOLFSSL_API
466int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
467 word32* outlen);
468WOLFSSL_LOCAL
469int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point,
470 byte* out, word32 *outlen);
471WOLFSSL_API
472int 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
484WOLFSSL_ABI WOLFSSL_API
485int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
486 WC_RNG* rng, ecc_key* key);
487WOLFSSL_API
488int 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
491WOLFSSL_API
492int 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
497WOLFSSL_API
498int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
499 word32 hashlen, int* stat, ecc_key* key);
500WOLFSSL_API
501int 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
505WOLFSSL_API
506int wc_ecc_init(ecc_key* key);
507WOLFSSL_ABI WOLFSSL_API
508int wc_ecc_init_ex(ecc_key* key, void* heap, int devId);
509#ifdef HAVE_PKCS11
510WOLFSSL_API
511int wc_ecc_init_id(ecc_key* key, unsigned char* id, int len, void* heap,
512 int devId);
513#endif
514#ifdef WOLFSSL_CUSTOM_CURVES
515WOLFSSL_LOCAL
516void wc_ecc_free_curve(const ecc_set_type* curve, void* heap);
517#endif
518WOLFSSL_ABI WOLFSSL_API
519int wc_ecc_free(ecc_key* key);
520WOLFSSL_API
521int wc_ecc_set_flags(ecc_key* key, word32 flags);
522WOLFSSL_API
523void wc_ecc_fp_free(void);
524
525WOLFSSL_API
526int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id);
527
528WOLFSSL_API
529int wc_ecc_is_valid_idx(int n);
530WOLFSSL_API
531int wc_ecc_get_curve_idx(int curve_id);
532WOLFSSL_API
533int wc_ecc_get_curve_id(int curve_idx);
534#define wc_ecc_get_curve_name_from_id wc_ecc_get_name
535WOLFSSL_API
536int wc_ecc_get_curve_size_from_id(int curve_id);
537
538WOLFSSL_API
539int wc_ecc_get_curve_idx_from_name(const char* curveName);
540WOLFSSL_API
541int wc_ecc_get_curve_size_from_name(const char* curveName);
542WOLFSSL_API
543int wc_ecc_get_curve_id_from_name(const char* curveName);
544WOLFSSL_API
545int 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);
549WOLFSSL_API
550int wc_ecc_get_curve_id_from_dp_params(const ecc_set_type* dp);
551
552WOLFSSL_API
553int wc_ecc_get_curve_id_from_oid(const byte* oid, word32 len);
554
555WOLFSSL_API const ecc_set_type* wc_ecc_get_curve_params(int curve_idx);
556
557WOLFSSL_API
558ecc_point* wc_ecc_new_point(void);
559WOLFSSL_API
560ecc_point* wc_ecc_new_point_h(void* h);
561WOLFSSL_API
562void wc_ecc_del_point(ecc_point* p);
563WOLFSSL_API
564void wc_ecc_del_point_h(ecc_point* p, void* h);
565WOLFSSL_API
566int wc_ecc_copy_point(ecc_point* p, ecc_point *r);
567WOLFSSL_API
568int wc_ecc_cmp_point(ecc_point* a, ecc_point *b);
569WOLFSSL_API
570int wc_ecc_point_is_at_infinity(ecc_point *p);
571
572#ifndef WOLFSSL_ATECC508A
573WOLFSSL_API
574int wc_ecc_mulmod(mp_int* k, ecc_point *G, ecc_point *R,
575 mp_int* a, mp_int* modulus, int map);
576WOLFSSL_LOCAL
577int wc_ecc_mulmod_ex(mp_int* k, ecc_point *G, ecc_point *R,
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 */
584WOLFSSL_API
585int wc_ecc_export_x963(ecc_key*, byte* out, word32* outLen);
586WOLFSSL_API
587int 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
592WOLFSSL_ABI WOLFSSL_API
593int wc_ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
594WOLFSSL_API
595int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
596 int curve_id);
597WOLFSSL_API
598int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
599 word32 pubSz, ecc_key* key);
600WOLFSSL_API
601int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz,
602 const byte* pub, word32 pubSz, ecc_key* key, int curve_id);
603WOLFSSL_API
604int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen);
605WOLFSSL_API
606int wc_ecc_rs_raw_to_sig(const byte* r, word32 rSz, const byte* s, word32 sSz,
607 byte* out, word32* outlen);
608WOLFSSL_API
609int wc_ecc_sig_to_rs(const byte* sig, word32 sigLen, byte* r, word32* rLen,
610 byte* s, word32* sLen);
611WOLFSSL_API
612int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy,
613 const char* d, const char* curveName);
614WOLFSSL_API
615int wc_ecc_import_raw_ex(ecc_key* key, const char* qx, const char* qy,
616 const char* d, int curve_id);
617WOLFSSL_API
618int 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
623WOLFSSL_API
624int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen,
625 byte* qy, word32* qyLen, byte* d, word32* dLen,
626 int encType);
627WOLFSSL_API
628int wc_ecc_export_private_only(ecc_key* key, byte* out, word32* outLen);
629WOLFSSL_API
630int wc_ecc_export_public_raw(ecc_key* key, byte* qx, word32* qxLen,
631 byte* qy, word32* qyLen);
632WOLFSSL_API
633int 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
638WOLFSSL_API
639int wc_ecc_export_point_der_ex(const int curve_idx, ecc_point* point, byte* out,
640 word32* outLen, int compressed);
641WOLFSSL_API
642int wc_ecc_export_point_der(const int curve_idx, ecc_point* point,
643 byte* out, word32* outLen);
644WOLFSSL_LOCAL
645int 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
651WOLFSSL_API
652int wc_ecc_import_point_der_ex(byte* in, word32 inLen, const int curve_idx,
653 ecc_point* point, int shortKeySize);
654WOLFSSL_API
655int 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 */
660WOLFSSL_API
661int wc_ecc_size(ecc_key* key);
662WOLFSSL_API
663int wc_ecc_sig_size_calc(int sz);
664WOLFSSL_API
665int wc_ecc_sig_size(ecc_key* key);
666
667WOLFSSL_API
668int 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
678enum ecEncAlgo {
679 ecAES_128_CBC = 1, /* default */
680 ecAES_256_CBC = 2
681};
682
683enum ecKdfAlgo {
684 ecHKDF_SHA256 = 1, /* default */
685 ecHKDF_SHA1 = 2
686};
687
688enum ecMacAlgo {
689 ecHMAC_SHA256 = 1, /* default */
690 ecHMAC_SHA1 = 2
691};
692
693enum {
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
702enum ecFlags {
703 REQ_RESP_CLIENT = 1,
704 REQ_RESP_SERVER = 2
705};
706
707
708typedef struct ecEncCtx ecEncCtx;
709
710WOLFSSL_API
711ecEncCtx* wc_ecc_ctx_new(int flags, WC_RNG* rng);
712WOLFSSL_API
713ecEncCtx* wc_ecc_ctx_new_ex(int flags, WC_RNG* rng, void* heap);
714WOLFSSL_API
715void wc_ecc_ctx_free(ecEncCtx*);
716WOLFSSL_API
717int wc_ecc_ctx_reset(ecEncCtx*, WC_RNG*); /* reset for use again w/o alloc/free */
718
719WOLFSSL_API
720const byte* wc_ecc_ctx_get_own_salt(ecEncCtx*);
721WOLFSSL_API
722int wc_ecc_ctx_set_peer_salt(ecEncCtx*, const byte* salt);
723WOLFSSL_API
724int wc_ecc_ctx_set_info(ecEncCtx*, const byte* info, int sz);
725
726WOLFSSL_API
727int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
728 word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
729WOLFSSL_API
730int 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
736WOLFSSL_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
742WOLFSSL_API int wc_ecc_curve_cache_init(void);
743WOLFSSL_API void wc_ecc_curve_cache_free(void);
744#endif
745
746WOLFSSL_API
747int wc_ecc_gen_k(WC_RNG* rng, int size, mp_int* k, mp_int* order);
748
749#ifdef WOLFSSL_DSP
750WOLFSSL_API
751int wc_ecc_set_handle(ecc_key* key, remote_handle64 handle);
752WOLFSSL_LOCAL
753int 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 */
Note: See TracBrowser for help on using the repository browser.