]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/cyassl/ctaocrypt/ecc.h
Rename the CyaSSL directory to WolfSSL
[freertos] / FreeRTOS-Plus / Source / WolfSSL / cyassl / ctaocrypt / ecc.h
1 /* ecc.h
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
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.
11  *
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.
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-1301, USA
20  */
21
22 #ifdef HAVE_ECC
23
24 #ifndef CTAO_CRYPT_ECC_H
25 #define CTAO_CRYPT_ECC_H
26
27 #include <cyassl/ctaocrypt/types.h>
28 #include <cyassl/ctaocrypt/integer.h>
29 #include <cyassl/ctaocrypt/random.h>
30
31 #ifdef __cplusplus
32     extern "C" {
33 #endif
34
35
36 enum {
37     ECC_PUBLICKEY  = 1,
38     ECC_PRIVATEKEY = 2,
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 */
44 };
45
46
47 /* ECC set type defined a NIST GF(p) curve */
48 typedef struct {
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) */
56 } ecc_set_type;
57
58
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 */
61 typedef struct {
62     mp_int x;        /* The x coordinate */
63     mp_int y;        /* The y coordinate */
64     mp_int z;        /* The z coordinate */
65 } ecc_point;
66
67
68 /* An ECC Key */
69 typedef struct {
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
73                            curve in dp */
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 */
78 } ecc_key;
79
80
81 /* ECC predefined curve sets  */
82 extern const ecc_set_type ecc_sets[];
83
84
85 CYASSL_API
86 int ecc_make_key(RNG* rng, int keysize, ecc_key* key);
87 CYASSL_API
88 int ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
89                       word32* outlen);
90 CYASSL_API
91 int ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen, 
92                   RNG* rng, ecc_key* key);
93 CYASSL_API
94 int ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
95                     word32 hashlen, int* stat, ecc_key* key);
96 CYASSL_API
97 void ecc_init(ecc_key* key);
98 CYASSL_API
99 void ecc_free(ecc_key* key);
100 CYASSL_API
101 void ecc_fp_free(void);
102
103
104 /* ASN key helpers */
105 CYASSL_API
106 int ecc_export_x963(ecc_key*, byte* out, word32* outLen);
107 CYASSL_API
108 int ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
109 CYASSL_API
110 int ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
111                            word32 pubSz, ecc_key* key);
112 CYASSL_API
113 int ecc_export_private_only(ecc_key* key, byte* out, word32* outLen);
114
115 /* size helper */
116 CYASSL_API
117 int ecc_size(ecc_key* key);
118 CYASSL_API
119 int ecc_sig_size(ecc_key* key);
120
121
122 #ifdef HAVE_ECC_ENCRYPT
123 /* ecc encrypt */
124
125 enum ecEncAlgo {
126     ecAES_128_CBC = 1,  /* default */
127     ecAES_256_CBC = 2
128 };
129
130 enum ecKdfAlgo {
131     ecHKDF_SHA256 = 1,  /* default */
132     ecHKDF_SHA1   = 2
133 };
134
135 enum ecMacAlgo {
136     ecHMAC_SHA256 = 1,  /* default */
137     ecHMAC_SHA1   = 2
138 };
139
140 enum {
141     KEY_SIZE_128     = 16,   
142     KEY_SIZE_256     = 32,   
143     IV_SIZE_64       =  8,
144     EXCHANGE_SALT_SZ = 16,  
145     EXCHANGE_INFO_SZ = 23  
146 };
147
148 enum ecFlags {
149     REQ_RESP_CLIENT = 1,
150     REQ_RESP_SERVER = 2
151 };
152
153
154 typedef struct ecEncCtx ecEncCtx;
155
156 CYASSL_API
157 ecEncCtx* ecc_ctx_new(int flags, RNG* rng);
158 CYASSL_API
159 void ecc_ctx_free(ecEncCtx*);
160 CYASSL_API
161 int ecc_ctx_reset(ecEncCtx*, RNG*);   /* reset for use again w/o alloc/free */
162
163 CYASSL_API
164 const byte* ecc_ctx_get_own_salt(ecEncCtx*);
165 CYASSL_API
166 int ecc_ctx_set_peer_salt(ecEncCtx*, const byte* salt);
167 CYASSL_API
168 int ecc_ctx_set_info(ecEncCtx*, const byte* info, int sz);
169
170 CYASSL_API
171 int ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
172                 word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
173 CYASSL_API
174 int ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
175                 word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
176
177 #endif /* HAVE_ECC_ENCRYPT */
178
179 #ifdef __cplusplus
180     }    /* extern "C" */    
181 #endif
182
183 #endif /* CTAO_CRYPT_ECC_H */
184 #endif /* HAVE_ECC */