]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/cyassl/openssl/evp.h
Update CyaSSL to latest version.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / cyassl / openssl / evp.h
1 /* evp.h
2  *
3  * Copyright (C) 2013 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 /*  evp.h defines mini evp openssl compatibility layer 
24  *
25  */
26
27
28 #ifndef CYASSL_EVP_H_
29 #define CYASSL_EVP_H_
30
31 #include <cyassl/ctaocrypt/settings.h>
32
33 #ifdef YASSL_PREFIX
34 #include "prefix_evp.h"
35 #endif
36
37 #include <cyassl/openssl/md5.h>
38 #include <cyassl/openssl/sha.h>
39 #include <cyassl/openssl/ripemd.h>
40 #include <cyassl/openssl/rsa.h>
41 #include <cyassl/openssl/dsa.h>
42
43 #include <cyassl/ctaocrypt/aes.h>
44 #include <cyassl/ctaocrypt/des3.h>
45 #include <cyassl/ctaocrypt/arc4.h>
46
47
48 #ifdef __cplusplus
49     extern "C" {
50 #endif
51
52 typedef char CYASSL_EVP_MD;
53 typedef char CYASSL_EVP_CIPHER;
54
55 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_md5(void);
56 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_sha1(void);
57 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_sha256(void);
58 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_sha384(void);
59 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_sha512(void);
60 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_ripemd160(void);
61
62 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_128_cbc(void);
63 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_192_cbc(void);
64 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_256_cbc(void);
65 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_128_ctr(void);
66 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_192_ctr(void);
67 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_256_ctr(void);
68 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_des_cbc(void);
69 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_des_ede3_cbc(void);
70 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_rc4(void);
71 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_enc_null(void);
72
73
74 typedef union {
75     CYASSL_MD5_CTX    md5;
76     CYASSL_SHA_CTX    sha;
77     CYASSL_SHA256_CTX sha256;
78     #ifdef CYASSL_SHA384
79         CYASSL_SHA384_CTX sha384;
80     #endif
81     #ifdef CYASSL_SHA512
82         CYASSL_SHA512_CTX sha512;
83     #endif
84     #ifdef CYASSL_RIPEMD
85         CYASSL_RIPEMD_CTX ripemd;
86     #endif
87 } CYASSL_Hasher;
88
89
90 typedef struct CYASSL_EVP_MD_CTX {
91     unsigned char macType;
92     CYASSL_Hasher hash;
93 } CYASSL_EVP_MD_CTX;
94
95
96 typedef union {
97     Aes  aes;
98 #ifndef NO_DES3
99     Des  des;
100     Des3 des3;
101 #endif
102     Arc4 arc4;
103 } CYASSL_Cipher;
104
105
106 enum {
107     AES_128_CBC_TYPE  = 1,
108     AES_192_CBC_TYPE  = 2,
109     AES_256_CBC_TYPE  = 3,
110     AES_128_CTR_TYPE  = 4,
111     AES_192_CTR_TYPE  = 5,
112     AES_256_CTR_TYPE  = 6,
113     DES_CBC_TYPE      = 7,
114     DES_EDE3_CBC_TYPE = 8,
115     ARC4_TYPE         = 9,
116     NULL_CIPHER_TYPE  = 10,
117     EVP_PKEY_RSA      = 11,
118     EVP_PKEY_DSA      = 12,
119     NID_sha1          = 64,
120     NID_md5           =  4
121 };
122
123
124 typedef struct CYASSL_EVP_CIPHER_CTX {
125     int            keyLen;         /* user may set for variable */
126     unsigned char  enc;            /* if encrypt side, then true */
127     unsigned char  cipherType;
128     unsigned char  iv[AES_BLOCK_SIZE];    /* working iv pointer into cipher */
129     CYASSL_Cipher  cipher;
130 } CYASSL_EVP_CIPHER_CTX;
131
132
133 CYASSL_API int  CyaSSL_EVP_MD_size(const CYASSL_EVP_MD* md);
134 CYASSL_API void CyaSSL_EVP_MD_CTX_init(CYASSL_EVP_MD_CTX* ctx);
135 CYASSL_API int  CyaSSL_EVP_MD_CTX_cleanup(CYASSL_EVP_MD_CTX* ctx);
136
137 CYASSL_API int CyaSSL_EVP_DigestInit(CYASSL_EVP_MD_CTX* ctx,
138                                      const CYASSL_EVP_MD* type);
139 CYASSL_API int CyaSSL_EVP_DigestUpdate(CYASSL_EVP_MD_CTX* ctx, const void* data,
140                                        unsigned long sz);
141 CYASSL_API int CyaSSL_EVP_DigestFinal(CYASSL_EVP_MD_CTX* ctx, unsigned char* md,
142                                       unsigned int* s);
143 CYASSL_API int CyaSSL_EVP_DigestFinal_ex(CYASSL_EVP_MD_CTX* ctx,
144                                             unsigned char* md, unsigned int* s);
145 CYASSL_API int CyaSSL_EVP_BytesToKey(const CYASSL_EVP_CIPHER*,
146                               const CYASSL_EVP_MD*, const unsigned char*,
147                               const unsigned char*, int, int, unsigned char*,
148                               unsigned char*);
149
150 CYASSL_API void CyaSSL_EVP_CIPHER_CTX_init(CYASSL_EVP_CIPHER_CTX* ctx);
151 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_cleanup(CYASSL_EVP_CIPHER_CTX* ctx);
152
153 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_iv_length(const CYASSL_EVP_CIPHER_CTX*);
154
155
156 CYASSL_API int  CyaSSL_EVP_CipherInit(CYASSL_EVP_CIPHER_CTX* ctx,
157                                     const CYASSL_EVP_CIPHER* type,
158                                     unsigned char* key, unsigned char* iv,
159                                     int enc);
160 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_key_length(CYASSL_EVP_CIPHER_CTX* ctx);
161 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_set_key_length(CYASSL_EVP_CIPHER_CTX* ctx,
162                                                      int keylen);
163 CYASSL_API int  CyaSSL_EVP_Cipher(CYASSL_EVP_CIPHER_CTX* ctx,
164                           unsigned char* dst, unsigned char* src,
165                           unsigned int len);
166
167 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_get_digestbynid(int);
168
169 CYASSL_API CYASSL_RSA* CyaSSL_EVP_PKEY_get1_RSA(CYASSL_EVP_PKEY*);
170 CYASSL_API CYASSL_DSA* CyaSSL_EVP_PKEY_get1_DSA(CYASSL_EVP_PKEY*);
171
172 /* these next ones don't need real OpenSSL type, for OpenSSH compat only */
173 CYASSL_API void* CyaSSL_EVP_X_STATE(const CYASSL_EVP_CIPHER_CTX* ctx);
174 CYASSL_API int   CyaSSL_EVP_X_STATE_LEN(const CYASSL_EVP_CIPHER_CTX* ctx);
175
176 CYASSL_API void  CyaSSL_3des_iv(CYASSL_EVP_CIPHER_CTX* ctx, int doset,
177                                 unsigned char* iv, int len);
178 CYASSL_API void  CyaSSL_aes_ctr_iv(CYASSL_EVP_CIPHER_CTX* ctx, int doset,
179                                 unsigned char* iv, int len);
180
181 CYASSL_API int  CyaSSL_StoreExternalIV(CYASSL_EVP_CIPHER_CTX* ctx);
182 CYASSL_API int  CyaSSL_SetInternalIV(CYASSL_EVP_CIPHER_CTX* ctx);
183
184
185 /* end OpenSSH compat */
186
187 typedef CYASSL_EVP_MD         EVP_MD;
188 typedef CYASSL_EVP_CIPHER     EVP_CIPHER;
189 typedef CYASSL_EVP_MD_CTX     EVP_MD_CTX;
190 typedef CYASSL_EVP_CIPHER_CTX EVP_CIPHER_CTX;
191
192 #define EVP_md5       CyaSSL_EVP_md5
193 #define EVP_sha1      CyaSSL_EVP_sha1
194 #define EVP_sha256    CyaSSL_EVP_sha256
195 #define EVP_sha384    CyaSSL_EVP_sha384
196 #define EVP_sha512    CyaSSL_EVP_sha512
197 #define EVP_ripemd160 CyaSSL_EVP_ripemd160
198
199 #define EVP_aes_128_cbc  CyaSSL_EVP_aes_128_cbc
200 #define EVP_aes_192_cbc  CyaSSL_EVP_aes_192_cbc
201 #define EVP_aes_256_cbc  CyaSSL_EVP_aes_256_cbc
202 #define EVP_aes_128_ctr  CyaSSL_EVP_aes_128_ctr
203 #define EVP_aes_192_ctr  CyaSSL_EVP_aes_192_ctr
204 #define EVP_aes_256_ctr  CyaSSL_EVP_aes_256_ctr
205 #define EVP_des_cbc      CyaSSL_EVP_des_cbc
206 #define EVP_des_ede3_cbc CyaSSL_EVP_des_ede3_cbc
207 #define EVP_rc4          CyaSSL_EVP_rc4
208 #define EVP_enc_null     CyaSSL_EVP_enc_null
209
210 #define EVP_MD_size        CyaSSL_EVP_MD_size
211 #define EVP_MD_CTX_init    CyaSSL_EVP_MD_CTX_init
212 #define EVP_MD_CTX_cleanup CyaSSL_EVP_MD_CTX_cleanup
213 #define EVP_DigestInit     CyaSSL_EVP_DigestInit
214 #define EVP_DigestUpdate   CyaSSL_EVP_DigestUpdate
215 #define EVP_DigestFinal    CyaSSL_EVP_DigestFinal
216 #define EVP_DigestFinal_ex CyaSSL_EVP_DigestFinal_ex
217 #define EVP_BytesToKey     CyaSSL_EVP_BytesToKey
218
219 #define EVP_CIPHER_CTX_init           CyaSSL_EVP_CIPHER_CTX_init
220 #define EVP_CIPHER_CTX_cleanup        CyaSSL_EVP_CIPHER_CTX_cleanup
221 #define EVP_CIPHER_CTX_iv_length      CyaSSL_EVP_CIPHER_CTX_iv_length
222 #define EVP_CIPHER_CTX_key_length     CyaSSL_EVP_CIPHER_CTX_key_length
223 #define EVP_CIPHER_CTX_set_key_length CyaSSL_EVP_CIPHER_CTX_set_key_length
224 #define EVP_CipherInit                CyaSSL_EVP_CipherInit
225 #define EVP_Cipher                    CyaSSL_EVP_Cipher
226
227 #define EVP_get_digestbynid           CyaSSL_EVP_get_digestbynid
228
229 #define EVP_PKEY_get1_RSA   CyaSSL_EVP_PKEY_get1_RSA
230 #define EVP_PKEY_get1_DSA   CyaSSL_EVP_PKEY_get1_DSA
231
232 #ifndef EVP_MAX_MD_SIZE
233     #define EVP_MAX_MD_SIZE   64     /* sha512 */
234 #endif
235
236 #ifdef __cplusplus
237     } /* extern "C" */
238 #endif
239
240
241 #endif /* CYASSL_EVP_H_ */