]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/mcapi/crypto.h
Rename the CyaSSL directory to WolfSSL
[freertos] / FreeRTOS-Plus / Source / WolfSSL / mcapi / crypto.h
1 /* crypto.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
23 /* Defines Microchip CRYPTO API layer */
24
25
26 #ifndef MC_CRYPTO_API_H
27 #define MC_CRYPTO_API_H
28
29
30 #ifdef __cplusplus
31     extern "C" {
32 #endif
33
34 /* MD5 */
35 typedef struct CRYPT_MD5_CTX {
36     int holder[24];   /* big enough to hold internal, but check on init */
37 } CRYPT_MD5_CTX;
38
39 int CRYPT_MD5_Initialize(CRYPT_MD5_CTX*);
40 int CRYPT_MD5_DataAdd(CRYPT_MD5_CTX*, const unsigned char*, unsigned int);
41 int CRYPT_MD5_Finalize(CRYPT_MD5_CTX*, unsigned char*);
42
43 enum {
44     CRYPT_MD5_DIGEST_SIZE = 16 
45 };
46
47
48 /* SHA */
49 typedef struct CRYPT_SHA_CTX {
50     int holder[24];   /* big enough to hold internal, but check on init */
51 } CRYPT_SHA_CTX;
52
53 int CRYPT_SHA_Initialize(CRYPT_SHA_CTX*);
54 int CRYPT_SHA_DataAdd(CRYPT_SHA_CTX*, const unsigned char*, unsigned int);
55 int CRYPT_SHA_Finalize(CRYPT_SHA_CTX*, unsigned char*);
56
57 enum {
58     CRYPT_SHA_DIGEST_SIZE = 20
59 };
60
61
62 /* SHA-256 */
63 typedef struct CRYPT_SHA256_CTX {
64     int holder[28];   /* big enough to hold internal, but check on init */
65 } CRYPT_SHA256_CTX;
66
67 int CRYPT_SHA256_Initialize(CRYPT_SHA256_CTX*);
68 int CRYPT_SHA256_DataAdd(CRYPT_SHA256_CTX*, const unsigned char*, unsigned int);
69 int CRYPT_SHA256_Finalize(CRYPT_SHA256_CTX*, unsigned char*);
70
71 enum {
72     CRYPT_SHA256_DIGEST_SIZE = 32 
73 };
74
75
76 /* SHA-384 */
77 typedef struct CRYPT_SHA384_CTX {
78     long long holder[32];   /* big enough to hold internal, but check on init */
79 } CRYPT_SHA384_CTX;
80
81 int CRYPT_SHA384_Initialize(CRYPT_SHA384_CTX*);
82 int CRYPT_SHA384_DataAdd(CRYPT_SHA384_CTX*, const unsigned char*, unsigned int);
83 int CRYPT_SHA384_Finalize(CRYPT_SHA384_CTX*, unsigned char*);
84
85 enum {
86     CRYPT_SHA384_DIGEST_SIZE = 48
87 };
88
89
90 /* SHA-512 */
91 typedef struct CRYPT_SHA512_CTX {
92     long long holder[36];   /* big enough to hold internal, but check on init */
93 } CRYPT_SHA512_CTX;
94
95 int CRYPT_SHA512_Initialize(CRYPT_SHA512_CTX*);
96 int CRYPT_SHA512_DataAdd(CRYPT_SHA512_CTX*, const unsigned char*, unsigned int);
97 int CRYPT_SHA512_Finalize(CRYPT_SHA512_CTX*, unsigned char*);
98
99 enum {
100     CRYPT_SHA512_DIGEST_SIZE = 64 
101 };
102
103
104 /* HMAC */
105 typedef struct CRYPT_HMAC_CTX {
106     long long holder[67];   /* big enough to hold internal, but check on init */
107 } CRYPT_HMAC_CTX;
108
109 int CRYPT_HMAC_SetKey(CRYPT_HMAC_CTX*, int, const unsigned char*, unsigned int);
110 int CRYPT_HMAC_DataAdd(CRYPT_HMAC_CTX*, const unsigned char*, unsigned int);
111 int CRYPT_HMAC_Finalize(CRYPT_HMAC_CTX*, unsigned char*);
112
113 /* HMAC types */
114 enum {
115     CRYPT_HMAC_SHA    = 1, 
116     CRYPT_HMAC_SHA256 = 2, 
117     CRYPT_HMAC_SHA384 = 5, 
118     CRYPT_HMAC_SHA512 = 4 
119 };
120
121
122 /* Huffman */
123 int CRYPT_HUFFMAN_Compress(unsigned char*, unsigned int, const unsigned char*,
124                            unsigned int, unsigned int);
125 int CRYPT_HUFFMAN_DeCompress(unsigned char*, unsigned int, const unsigned char*,
126                              unsigned int);
127
128 /* flag to use static huffman */
129 enum {
130     CRYPT_HUFFMAN_COMPRESS_STATIC = 1 
131 };
132
133
134 /* RNG */
135 typedef struct CRYPT_RNG_CTX {
136     int holder[66];   /* big enough to hold internal, but check on init */
137 } CRYPT_RNG_CTX;
138
139 int CRYPT_RNG_Initialize(CRYPT_RNG_CTX*);
140 int CRYPT_RNG_Get(CRYPT_RNG_CTX*, unsigned char*);
141 int CRYPT_RNG_BlockGenerate(CRYPT_RNG_CTX*, unsigned char*, unsigned int);
142
143
144 /* TDES */
145 typedef struct CRYPT_TDES_CTX {
146     int holder[100];   /* big enough to hold internal, but check on init */
147 } CRYPT_TDES_CTX;
148
149 int CRYPT_TDES_KeySet(CRYPT_TDES_CTX*, const unsigned char*,
150                       const unsigned char*, int);
151 int CRYPT_TDES_IvSet(CRYPT_TDES_CTX*, const unsigned char*);
152 int CRYPT_TDES_CBC_Encrypt(CRYPT_TDES_CTX*, unsigned char*,
153                            const unsigned char*, unsigned int);
154 int CRYPT_TDES_CBC_Decrypt(CRYPT_TDES_CTX*, unsigned char*,
155                            const unsigned char*, unsigned int);
156
157 /* key direction flags for setup */
158 enum {
159     CRYPT_TDES_ENCRYPTION = 0,
160     CRYPT_TDES_DECRYPTION = 1 
161 };
162
163
164 /* AES */
165 typedef struct CRYPT_AES_CTX {
166     int holder[70];   /* big enough to hold internal, but check on init */
167 } CRYPT_AES_CTX;
168
169 /* key */
170 int CRYPT_AES_KeySet(CRYPT_AES_CTX*, const unsigned char*, unsigned int,
171                       const unsigned char*, int);
172 int CRYPT_AES_IvSet(CRYPT_AES_CTX*, const unsigned char*);
173
174 /* cbc */
175 int CRYPT_AES_CBC_Encrypt(CRYPT_AES_CTX*, unsigned char*,
176                            const unsigned char*, unsigned int);
177 int CRYPT_AES_CBC_Decrypt(CRYPT_AES_CTX*, unsigned char*,
178                            const unsigned char*, unsigned int);
179
180 /* ctr (counter), use Encrypt both ways with ENCRYPT key setup */
181 int CRYPT_AES_CTR_Encrypt(CRYPT_AES_CTX*, unsigned char*,
182                           const unsigned char*, unsigned int);
183
184 /* direct, one block at a time */
185 int CRYPT_AES_DIRECT_Encrypt(CRYPT_AES_CTX*, unsigned char*,
186                            const unsigned char*);
187 int CRYPT_AES_DIRECT_Decrypt(CRYPT_AES_CTX*, unsigned char*,
188                            const unsigned char*);
189
190 /* key direction flags for setup, ctr always uses ENCRYPT flag */
191 enum {
192     CRYPT_AES_ENCRYPTION = 0,
193     CRYPT_AES_DECRYPTION = 1,
194     CRYPT_AES_BLOCK_SIZE = 16
195 };
196
197
198
199 /* RSA */
200 typedef struct CRYPT_RSA_CTX {
201     void* holder;
202 } CRYPT_RSA_CTX;
203
204 /* init/free */
205 int CRYPT_RSA_Initialize(CRYPT_RSA_CTX*);
206 int CRYPT_RSA_Free(CRYPT_RSA_CTX*);
207
208 /* key decode */
209 int CRYPT_RSA_PublicKeyDecode(CRYPT_RSA_CTX*, const unsigned char*,
210                               unsigned int);
211 int CRYPT_RSA_PrivateKeyDecode(CRYPT_RSA_CTX*, const unsigned char*,
212                                unsigned int);
213
214 /* encrypt/decrypt */
215 int CRYPT_RSA_PublicEncrypt(CRYPT_RSA_CTX*, unsigned char*,
216                             unsigned int, const unsigned char*, unsigned int,
217                             CRYPT_RNG_CTX*);
218 int CRYPT_RSA_PrivateDecrypt(CRYPT_RSA_CTX*, unsigned char*,
219                              unsigned int, const unsigned char*, unsigned int);
220
221 /* helpers */
222 int CRYPT_RSA_EncryptSizeGet(CRYPT_RSA_CTX*);                             
223
224
225
226 /* ECC */
227 typedef struct CRYPT_ECC_CTX {
228     void* holder;
229 } CRYPT_ECC_CTX;
230
231 /* init/free */
232 int CRYPT_ECC_Initialize(CRYPT_ECC_CTX*);
233 int CRYPT_ECC_Free(CRYPT_ECC_CTX*);
234
235 /* key coders */
236 int CRYPT_ECC_PublicExport(CRYPT_ECC_CTX*, unsigned char*, unsigned int,
237                            unsigned int*);
238 int CRYPT_ECC_PublicImport(CRYPT_ECC_CTX*, const unsigned char*, unsigned int);
239 int CRYPT_ECC_PrivateImport(CRYPT_ECC_CTX*, const unsigned char*, unsigned int,
240                             const unsigned char*, unsigned int);
241
242 /* dhe */
243 int CRYPT_ECC_DHE_KeyMake(CRYPT_ECC_CTX*, CRYPT_RNG_CTX*, int);
244 int CRYPT_ECC_DHE_SharedSecretMake(CRYPT_ECC_CTX*, CRYPT_ECC_CTX*,
245                                    unsigned char*, unsigned int, unsigned int*);
246
247 /* dsa */
248 int CRYPT_ECC_DSA_HashSign(CRYPT_ECC_CTX*, CRYPT_RNG_CTX*, unsigned char*,
249         unsigned int, unsigned int*, const unsigned char*, unsigned int);
250 int CRYPT_ECC_DSA_HashVerify(CRYPT_ECC_CTX*, const unsigned char*,
251                      unsigned int, unsigned char*, unsigned int, int*);
252
253 /* helpers */
254 int CRYPT_ECC_KeySizeGet(CRYPT_ECC_CTX*);
255 int CRYPT_ECC_SignatureSizeGet(CRYPT_ECC_CTX*);
256
257
258 /* Error string helper, string needs to be >= 80 chars */
259 int CRYPT_ERROR_StringGet(int, char*);
260
261
262 #ifdef __cplusplus
263     }  /* extern "C" */ 
264 #endif
265
266
267 #endif /* MC_CRYPTO_API_H */
268