27#ifndef WOLF_CRYPT_ECC_H
28#define WOLF_CRYPT_ECC_H
34#if defined(HAVE_FIPS) && \
35 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
36 #include <wolfssl/wolfcrypt/fips.h>
39#include <wolfssl/wolfcrypt/integer.h>
46#ifdef WOLFSSL_ASYNC_CRYPT
47 #include <wolfssl/wolfcrypt/async.h>
48 #ifdef WOLFSSL_CERT_GEN
53#ifdef WOLFSSL_ATECC508A
54 #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
57#if defined(WOLFSSL_CRYPTOCELL)
58 #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
67#if defined(HAVE_COMP_KEY) || defined(ECC_CACHE_CURVE)
68 #ifndef USE_ECC_B_PARAM
69 #define USE_ECC_B_PARAM
75#define ECC_CUSTOM_IDX (-1)
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
104#if ((MAX_ECC_BITS * 2) % 8) == 0
105 #define MAX_ECC_BYTES (MAX_ECC_BITS / 8)
108 #define MAX_ECC_BYTES ((MAX_ECC_BITS / 8) + 1)
111#ifndef ECC_MAX_PAD_SZ
113 #define ECC_MAX_PAD_SZ 2
119 ECC_PRIVATEKEY_ONLY = 3,
125 ECC_MAXSIZE_GEN = 74,
126 ECC_MAX_OID_LEN = 16,
127 ECC_MAX_SIG_SIZE= ((MAX_ECC_BYTES * 2) + ECC_MAX_PAD_SZ + SIG_HEADER_SZ),
130#ifdef WOLFSSL_ATECC508A
131 ECC_MAX_CRYPTO_HW_SIZE = ATECC_KEY_SIZE,
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,
139 ECC_MAX_CRYPTO_HW_SIZE = CRYPTOCELL_KEY_SIZE,
143 ECC_POINT_COMP_EVEN = 0x02,
144 ECC_POINT_COMP_ODD = 0x03,
145 ECC_POINT_UNCOMP = 0x04,
148 SHAMIR_PRECOMP_SZ = 16,
156typedef enum ecc_curve_id {
157 ECC_CURVE_INVALID = -1,
196#ifdef HAVE_CURVE25519
203#ifdef WOLFSSL_CUSTOM_CURVES
208#ifdef HAVE_OID_ENCODING
209typedef word16 ecc_oid_t;
211typedef byte ecc_oid_t;
218#if !defined(WOLFSSL_ECC_CURVE_STATIC) && defined(USE_WINDOWS_API)
223 #define WOLFSSL_ECC_CURVE_STATIC
227#ifndef WOLFSSL_ECC_CURVE_STATIC
238 const ecc_oid_t* oid;
244#define MAX_ECC_NAME 16
245#define MAX_ECC_STRING ((MAX_ECC_BYTES * 2) + 1)
251 const char name[MAX_ECC_NAME];
252 const char prime[MAX_ECC_STRING];
253 const char Af[MAX_ECC_STRING];
254 const char Bf[MAX_ECC_STRING];
255 const char order[MAX_ECC_STRING];
256 const char Gx[MAX_ECC_STRING];
257 const char Gy[MAX_ECC_STRING];
258 const ecc_oid_t oid[10];
292 #error USE_FAST_MATH must be defined to use ALT_ECC_SIZE
296#ifndef FP_MAX_BITS_ECC
298 #if ((MAX_ECC_BITS * 2) % DIGIT_BIT) == 0
300 #define FP_MAX_BITS_ECC (MAX_ECC_BITS * 2)
303 #define FP_MAX_BITS_ECC ((MAX_ECC_BITS * 2) + DIGIT_BIT)
307 #if FP_MAX_BITS_ECC % CHAR_BIT
308 #error FP_MAX_BITS_ECC must be a multiple of CHAR_BIT
313#define FP_SIZE_ECC (FP_MAX_BITS_ECC/DIGIT_BIT)
319 int used, sign, size;
320 mp_digit dp[FP_SIZE_ECC];
324#ifndef WC_ECCKEY_TYPE_DEFINED
326 #define WC_ECCKEY_TYPE_DEFINED
343#ifdef WOLFSSL_SMALL_STACK_CACHE
350 WC_ECC_FLAG_NONE = 0x00,
352 WC_ECC_FLAG_COFACTOR = 0x01,
366#ifdef WOLFSSL_CUSTOM_CURVES
372#ifdef WOLFSSL_ATECC508A
374 byte pubkey_raw[ECC_MAX_CRYPTO_HW_PUBKEY_SIZE];
376#if defined(PLUTON_CRYPTO_ECC) || defined(WOLF_CRYPTO_CB)
379#ifdef WOLFSSL_ASYNC_CRYPT
382 WC_ASYNC_DEV asyncDev;
387 #ifdef WOLFSSL_CERT_GEN
392 byte id[ECC_MAX_ID_LEN];
395#if defined(WOLFSSL_CRYPTOCELL)
399#ifdef WOLFSSL_ECDSA_SET_K
403#ifdef WOLFSSL_SMALL_STACK_CACHE
414 remote_handle64 handle;
419WOLFSSL_ABI WOLFSSL_API
ecc_key* wc_ecc_key_new(
void*);
420WOLFSSL_ABI WOLFSSL_API
void wc_ecc_key_free(
ecc_key*);
425extern const size_t ecc_sets_count;
428const char* wc_ecc_get_name(
int curve_id);
430#ifndef WOLFSSL_ATECC508A
432#ifdef WOLFSSL_PUBLIC_ECC_ADD_DBL
433 #define ECC_API WOLFSSL_API
435 #define ECC_API WOLFSSL_LOCAL
447 mp_int* modulus, mp_digit mp);
453WOLFSSL_ABI WOLFSSL_API
462int wc_ecc_get_generator(
ecc_point* ecp,
int curve_idx);
470 byte* out, word32 *outlen);
473 byte* out, word32 *outlen);
475#if defined(WOLFSSL_ATECC508A) || defined(PLUTON_CRYPTO_ECC) || defined(WOLFSSL_CRYPTOCELL)
476#define wc_ecc_shared_secret_ssh wc_ecc_shared_secret
478#define wc_ecc_shared_secret_ssh wc_ecc_shared_secret_ex
484WOLFSSL_ABI WOLFSSL_API
485int wc_ecc_sign_hash(
const byte* in, word32 inlen,
byte* out, word32 *outlen,
490#ifdef WOLFSSL_ECDSA_SET_K
492int wc_ecc_sign_set_k(
const byte* k, word32 klen,
ecc_key* key);
496#ifdef HAVE_ECC_VERIFY
499 word32 hashlen,
int* stat,
ecc_key* key);
501int wc_ecc_verify_hash_ex(
mp_int *r,
mp_int *s,
const byte* hash,
502 word32 hashlen,
int* stat,
ecc_key* key);
507WOLFSSL_ABI WOLFSSL_API
508int wc_ecc_init_ex(
ecc_key* key,
void* heap,
int devId);
511int wc_ecc_init_id(
ecc_key* key,
unsigned char*
id,
int len,
void* heap,
514#ifdef WOLFSSL_CUSTOM_CURVES
516void wc_ecc_free_curve(
const ecc_set_type* curve,
void* heap);
518WOLFSSL_ABI WOLFSSL_API
521int wc_ecc_set_flags(
ecc_key* key, word32 flags);
526int wc_ecc_set_curve(
ecc_key* key,
int keysize,
int curve_id);
531int wc_ecc_get_curve_idx(
int curve_id);
533int wc_ecc_get_curve_id(
int curve_idx);
534#define wc_ecc_get_curve_name_from_id wc_ecc_get_name
536int wc_ecc_get_curve_size_from_id(
int curve_id);
539int wc_ecc_get_curve_idx_from_name(
const char* curveName);
541int wc_ecc_get_curve_size_from_name(
const char* curveName);
543int wc_ecc_get_curve_id_from_name(
const char* curveName);
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);
550int wc_ecc_get_curve_id_from_dp_params(
const ecc_set_type* dp);
553int wc_ecc_get_curve_id_from_oid(
const byte* oid, word32 len);
555WOLFSSL_API
const ecc_set_type* wc_ecc_get_curve_params(
int curve_idx);
564void wc_ecc_del_point_h(
ecc_point* p,
void* h);
572#ifndef WOLFSSL_ATECC508A
582#ifdef HAVE_ECC_KEY_EXPORT
591#ifdef HAVE_ECC_KEY_IMPORT
592WOLFSSL_ABI WOLFSSL_API
595int wc_ecc_import_x963_ex(
const byte* in, word32 inLen,
ecc_key* key,
598int wc_ecc_import_private_key(
const byte* priv, word32 privSz,
const byte* pub,
601int wc_ecc_import_private_key_ex(
const byte* priv, word32 privSz,
602 const byte* pub, word32 pubSz,
ecc_key* key,
int curve_id);
604int wc_ecc_rs_to_sig(
const char* r,
const char* s,
byte* out, word32* outlen);
607 byte* out, word32* outlen);
610 byte* s, word32* sLen);
613 const char* d,
const char* curveName);
616 const char* d,
int curve_id);
618int wc_ecc_import_unsigned(
ecc_key* key,
byte* qx,
byte* qy,
619 byte* d,
int curve_id);
622#ifdef HAVE_ECC_KEY_EXPORT
624int wc_ecc_export_ex(
ecc_key* key,
byte* qx, word32* qxLen,
625 byte* qy, word32* qyLen,
byte* d, word32* dLen,
630int wc_ecc_export_public_raw(
ecc_key* key,
byte* qx, word32* qxLen,
631 byte* qy, word32* qyLen);
633int wc_ecc_export_private_raw(
ecc_key* key,
byte* qx, word32* qxLen,
634 byte* qy, word32* qyLen,
byte* d, word32* dLen);
637#ifdef HAVE_ECC_KEY_EXPORT
639int wc_ecc_export_point_der_ex(
const int curve_idx,
ecc_point* point,
byte* out,
640 word32* outLen,
int compressed);
643 byte* out, word32* outLen);
645int wc_ecc_export_point_der_compressed(
const int curve_idx,
ecc_point* point,
646 byte* out, word32* outLen);
650#ifdef HAVE_ECC_KEY_IMPORT
652int wc_ecc_import_point_der_ex(
byte* in, word32 inLen,
const int curve_idx,
668int wc_ecc_get_oid(word32 oidSum,
const byte** oid, word32* oidSz);
670#ifdef WOLFSSL_CUSTOM_CURVES
675#ifdef HAVE_ECC_ENCRYPT
698 EXCHANGE_SALT_SZ = 16,
699 EXCHANGE_INFO_SZ = 23
708typedef struct ecEncCtx ecEncCtx;
713ecEncCtx* wc_ecc_ctx_new_ex(
int flags,
WC_RNG* rng,
void* heap);
728 word32 msgSz,
byte* out, word32* outSz, ecEncCtx* ctx);
731 word32 msgSz,
byte* out, word32* outSz, ecEncCtx* ctx);
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);
741#ifdef ECC_CACHE_CURVE
742WOLFSSL_API
int wc_ecc_curve_cache_init(
void);
743WOLFSSL_API
void wc_ecc_curve_cache_free(
void);
751int wc_ecc_set_handle(
ecc_key* key, remote_handle64 handle);
753int sp_dsp_ecc_verify_256(remote_handle64 handle,
const byte* hash, word32 hashLen,
mp_int* pX,
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_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
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_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 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_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_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_del_point(ecc_point *p)
Free an ECC point from memory.
Definition ecc.c:3082
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_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_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 ecc_point * wc_ecc_new_point(void)
Allocate a new ECC point.
Definition ecc.c:3060
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_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_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_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
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
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_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
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 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
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
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_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 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 void wc_ecc_ctx_free(ecEncCtx *)
This function frees the ecEncCtx object used for encrypting and decrypting messages.
Definition ecc.c:9852
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
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 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_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_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
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_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_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
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_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
Definition cryptoCell.h:91
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
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
ECC_API int ecc_map_ex(ecc_point *, mp_int *, mp_digit, int ct)
Definition ecc.c:2292
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_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_sig_to_rs(const byte *sig, word32 sigLen, byte *r, word32 *rLen, byte *s, word32 *sLen)
Definition ecc.c:7705
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 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