]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/CyaSSL/cyassl/openssl/evp.h
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS-Plus / CyaSSL / cyassl / openssl / evp.h
1 /* evp.h
2  *
3  * Copyright (C) 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
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     Des  des;
99     Des3 des3;
100     Arc4 arc4;
101 } CYASSL_Cipher;
102
103
104 enum {
105     AES_128_CBC_TYPE  = 1,
106     AES_192_CBC_TYPE  = 2,
107     AES_256_CBC_TYPE  = 3,
108     AES_128_CTR_TYPE  = 4,
109     AES_192_CTR_TYPE  = 5,
110     AES_256_CTR_TYPE  = 6,
111     DES_CBC_TYPE      = 7,
112     DES_EDE3_CBC_TYPE = 8,
113     ARC4_TYPE         = 9,
114     NULL_CIPHER_TYPE  = 10,
115     EVP_PKEY_RSA      = 11,
116     EVP_PKEY_DSA      = 12,
117     NID_sha1          = 64,
118     NID_md5           =  4
119 };
120
121
122 typedef struct CYASSL_EVP_CIPHER_CTX {
123     int            keyLen;         /* user may set for variable */
124     unsigned char  enc;            /* if encrypt side, then true */
125     unsigned char  cipherType;
126     unsigned char  iv[64];         /* working iv pointer into cipher */
127     CYASSL_Cipher  cipher;
128 } CYASSL_EVP_CIPHER_CTX;
129
130
131 CYASSL_API int  CyaSSL_EVP_MD_size(const CYASSL_EVP_MD* md);
132 CYASSL_API void CyaSSL_EVP_MD_CTX_init(CYASSL_EVP_MD_CTX* ctx);
133 CYASSL_API int  CyaSSL_EVP_MD_CTX_cleanup(CYASSL_EVP_MD_CTX* ctx);
134
135 CYASSL_API int CyaSSL_EVP_DigestInit(CYASSL_EVP_MD_CTX* ctx,
136                                      const CYASSL_EVP_MD* type);
137 CYASSL_API int CyaSSL_EVP_DigestUpdate(CYASSL_EVP_MD_CTX* ctx, const void* data,
138                                        unsigned long sz);
139 CYASSL_API int CyaSSL_EVP_DigestFinal(CYASSL_EVP_MD_CTX* ctx, unsigned char* md,
140                                       unsigned int* s);
141 CYASSL_API int CyaSSL_EVP_DigestFinal_ex(CYASSL_EVP_MD_CTX* ctx,
142                                             unsigned char* md, unsigned int* s);
143 CYASSL_API int CyaSSL_EVP_BytesToKey(const CYASSL_EVP_CIPHER*,
144                               const CYASSL_EVP_MD*, const unsigned char*,
145                               const unsigned char*, int, int, unsigned char*,
146                               unsigned char*);
147
148 CYASSL_API void CyaSSL_EVP_CIPHER_CTX_init(CYASSL_EVP_CIPHER_CTX* ctx);
149 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_cleanup(CYASSL_EVP_CIPHER_CTX* ctx);
150
151 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_iv_length(const CYASSL_EVP_CIPHER_CTX*);
152
153
154 CYASSL_API int  CyaSSL_EVP_CipherInit(CYASSL_EVP_CIPHER_CTX* ctx,
155                                     const CYASSL_EVP_CIPHER* type,
156                                     unsigned char* key, unsigned char* iv,
157                                     int enc);
158 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_key_length(CYASSL_EVP_CIPHER_CTX* ctx);
159 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_set_key_length(CYASSL_EVP_CIPHER_CTX* ctx,
160                                                      int keylen);
161 CYASSL_API int  CyaSSL_EVP_Cipher(CYASSL_EVP_CIPHER_CTX* ctx,
162                           unsigned char* dst, unsigned char* src,
163                           unsigned int len);
164
165 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_get_digestbynid(int);
166
167 CYASSL_API CYASSL_RSA* CyaSSL_EVP_PKEY_get1_RSA(CYASSL_EVP_PKEY*);
168 CYASSL_API CYASSL_DSA* CyaSSL_EVP_PKEY_get1_DSA(CYASSL_EVP_PKEY*);
169
170 /* these next ones don't need real OpenSSL type, for OpenSSH compat only */
171 CYASSL_API void* CyaSSL_EVP_X_STATE(const CYASSL_EVP_CIPHER_CTX* ctx);
172 CYASSL_API int   CyaSSL_EVP_X_STATE_LEN(const CYASSL_EVP_CIPHER_CTX* ctx);
173
174 CYASSL_API void  CyaSSL_3des_iv(CYASSL_EVP_CIPHER_CTX* ctx, int doset,
175                                 unsigned char* iv, int len);
176 CYASSL_API void  CyaSSL_aes_ctr_iv(CYASSL_EVP_CIPHER_CTX* ctx, int doset,
177                                 unsigned char* iv, int len);
178
179 CYASSL_API int  CyaSSL_StoreExternalIV(CYASSL_EVP_CIPHER_CTX* ctx);
180 CYASSL_API int  CyaSSL_SetInternalIV(CYASSL_EVP_CIPHER_CTX* ctx);
181
182
183 /* end OpenSSH compat */
184
185 typedef CYASSL_EVP_MD         EVP_MD;
186 typedef CYASSL_EVP_CIPHER     EVP_CIPHER;
187 typedef CYASSL_EVP_MD_CTX     EVP_MD_CTX;
188 typedef CYASSL_EVP_CIPHER_CTX EVP_CIPHER_CTX;
189
190 #define EVP_md5       CyaSSL_EVP_md5
191 #define EVP_sha1      CyaSSL_EVP_sha1
192 #define EVP_sha256    CyaSSL_EVP_sha256
193 #define EVP_sha384    CyaSSL_EVP_sha384
194 #define EVP_sha512    CyaSSL_EVP_sha512
195 #define EVP_ripemd160 CyaSSL_EVP_ripemd160
196
197 #define EVP_aes_128_cbc  CyaSSL_EVP_aes_128_cbc
198 #define EVP_aes_192_cbc  CyaSSL_EVP_aes_192_cbc
199 #define EVP_aes_256_cbc  CyaSSL_EVP_aes_256_cbc
200 #define EVP_aes_128_ctr  CyaSSL_EVP_aes_128_ctr
201 #define EVP_aes_192_ctr  CyaSSL_EVP_aes_192_ctr
202 #define EVP_aes_256_ctr  CyaSSL_EVP_aes_256_ctr
203 #define EVP_des_cbc      CyaSSL_EVP_des_cbc
204 #define EVP_des_ede3_cbc CyaSSL_EVP_des_ede3_cbc
205 #define EVP_rc4          CyaSSL_EVP_rc4
206 #define EVP_enc_null     CyaSSL_EVP_enc_null
207
208 #define EVP_MD_size        CyaSSL_EVP_MD_size
209 #define EVP_MD_CTX_init    CyaSSL_EVP_MD_CTX_init
210 #define EVP_MD_CTX_cleanup CyaSSL_EVP_MD_CTX_cleanup
211 #define EVP_DigestInit     CyaSSL_EVP_DigestInit
212 #define EVP_DigestUpdate   CyaSSL_EVP_DigestUpdate
213 #define EVP_DigestFinal    CyaSSL_EVP_DigestFinal
214 #define EVP_DigestFinal_ex CyaSSL_EVP_DigestFinal_ex
215 #define EVP_BytesToKey     CyaSSL_EVP_BytesToKey
216
217 #define EVP_CIPHER_CTX_init           CyaSSL_EVP_CIPHER_CTX_init
218 #define EVP_CIPHER_CTX_cleanup        CyaSSL_EVP_CIPHER_CTX_cleanup
219 #define EVP_CIPHER_CTX_iv_length      CyaSSL_EVP_CIPHER_CTX_iv_length
220 #define EVP_CIPHER_CTX_key_length     CyaSSL_EVP_CIPHER_CTX_key_length
221 #define EVP_CIPHER_CTX_set_key_length CyaSSL_EVP_CIPHER_CTX_set_key_length
222 #define EVP_CipherInit                CyaSSL_EVP_CipherInit
223 #define EVP_Cipher                    CyaSSL_EVP_Cipher
224
225 #define EVP_get_digestbynid           CyaSSL_EVP_get_digestbynid
226
227 #define EVP_PKEY_get1_RSA   CyaSSL_EVP_PKEY_get1_RSA
228 #define EVP_PKEY_get1_DSA   CyaSSL_EVP_PKEY_get1_DSA
229
230 #ifndef EVP_MAX_MD_SIZE
231     #define EVP_MAX_MD_SIZE   64     /* sha512 */
232 #endif
233
234 #ifdef __cplusplus
235     } /* extern "C" */
236 #endif
237
238
239 #endif /* CYASSL_EVP_H_ */