3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
7 * CyaSSL 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.
12 * CyaSSL 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.
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-1301, USA
24 #ifndef CTAO_CRYPT_ECC_H
25 #define CTAO_CRYPT_ECC_H
27 #include <cyassl/ctaocrypt/types.h>
28 #include <cyassl/ctaocrypt/integer.h>
29 #include <cyassl/ctaocrypt/random.h>
39 ECC_MAXNAME = 16, /* MAX CURVE NAME LENGTH */
40 SIG_HEADER_SZ = 6, /* ECC signature header size */
41 ECC_BUFSIZE = 256, /* for exported keys temp buffer */
42 ECC_MINSIZE = 20, /* MIN Private Key size */
43 ECC_MAXSIZE = 66 /* MAX Private Key size */
47 /* ECC set type defined a NIST GF(p) curve */
49 int size; /* The size of the curve in octets */
50 const char* name; /* name of this curve */
51 const char* prime; /* prime that defines the field, curve is in (hex) */
52 const char* Bf; /* fields B param (hex) */
53 const char* order; /* order of the curve (hex) */
54 const char* Gx; /* x coordinate of the base point on curve (hex) */
55 const char* Gy; /* y coordinate of the base point on curve (hex) */
59 /* A point on an ECC curve, stored in Jacbobian format such that (x,y,z) =>
60 (x/z^2, y/z^3, 1) when interpreted as affine */
62 mp_int x; /* The x coordinate */
63 mp_int y; /* The y coordinate */
64 mp_int z; /* The z coordinate */
70 int type; /* Public or Private */
71 int idx; /* Index into the ecc_sets[] for the parameters of
72 this curve if -1, this key is using user supplied
74 const ecc_set_type* dp; /* domain parameters, either points to NIST
75 curves (idx >= 0) or user supplied */
76 ecc_point pubkey; /* public key */
77 mp_int k; /* private key */
81 /* ECC predefined curve sets */
82 extern const ecc_set_type ecc_sets[];
86 int ecc_make_key(RNG* rng, int keysize, ecc_key* key);
88 int ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
91 int ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
92 RNG* rng, ecc_key* key);
94 int ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
95 word32 hashlen, int* stat, ecc_key* key);
97 void ecc_init(ecc_key* key);
99 void ecc_free(ecc_key* key);
101 void ecc_fp_free(void);
104 /* ASN key helpers */
106 int ecc_export_x963(ecc_key*, byte* out, word32* outLen);
108 int ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
110 int ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
111 word32 pubSz, ecc_key* key);
113 int ecc_export_private_only(ecc_key* key, byte* out, word32* outLen);
117 int ecc_size(ecc_key* key);
119 int ecc_sig_size(ecc_key* key);
122 #ifdef HAVE_ECC_ENCRYPT
126 ecAES_128_CBC = 1, /* default */
131 ecHKDF_SHA256 = 1, /* default */
136 ecHMAC_SHA256 = 1, /* default */
144 EXCHANGE_SALT_SZ = 16,
145 EXCHANGE_INFO_SZ = 23
154 typedef struct ecEncCtx ecEncCtx;
157 ecEncCtx* ecc_ctx_new(int flags, RNG* rng);
159 void ecc_ctx_free(ecEncCtx*);
161 int ecc_ctx_reset(ecEncCtx*, RNG*); /* reset for use again w/o alloc/free */
164 const byte* ecc_ctx_get_own_salt(ecEncCtx*);
166 int ecc_ctx_set_peer_salt(ecEncCtx*, const byte* salt);
168 int ecc_ctx_set_info(ecEncCtx*, const byte* info, int sz);
171 int ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
172 word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
174 int ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
175 word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
177 #endif /* HAVE_ECC_ENCRYPT */
183 #endif /* CTAO_CRYPT_ECC_H */
184 #endif /* HAVE_ECC */