]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/CyaSSL/cyassl/ctaocrypt/ecc.h
Commit 3 RX100 low power demos.
[freertos] / FreeRTOS-Plus / CyaSSL / cyassl / ctaocrypt / ecc.h
1 /* ecc.h
2  *
3  * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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* B;        /* 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, byte* hash, word32 hashlen, 
95                     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
101
102 /* ASN key helpers */
103 CYASSL_API
104 int ecc_export_x963(ecc_key*, byte* out, word32* outLen);
105 CYASSL_API
106 int ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
107 CYASSL_API
108 int ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
109                            word32 pubSz, ecc_key* key);
110
111 /* size helper */
112 CYASSL_API
113 int ecc_size(ecc_key* key);
114 CYASSL_API
115 int ecc_sig_size(ecc_key* key);
116
117 /* TODO: fix mutex types */
118 #define MUTEX_GLOBAL(x) int (x);
119 #define MUTEX_LOCK(x)
120 #define MUTEX_UNLOCK(x)
121
122
123
124 #ifdef __cplusplus
125     }    /* extern "C" */    
126 #endif
127
128 #endif /* CTAO_CRYPT_ECC_H */
129 #endif /* HAVE_ECC */