3 * Copyright (C) 2012 Sawtooth Consulting Ltd.
5 * This file is part of CyaSSL.
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.
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.
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
23 /* evp.h defines mini evp openssl compatibility layer
31 #include <cyassl/ctaocrypt/settings.h>
34 #include "prefix_evp.h"
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>
43 #include <cyassl/ctaocrypt/aes.h>
44 #include <cyassl/ctaocrypt/des3.h>
45 #include <cyassl/ctaocrypt/arc4.h>
52 typedef char CYASSL_EVP_MD;
53 typedef char CYASSL_EVP_CIPHER;
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);
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);
77 CYASSL_SHA256_CTX sha256;
79 CYASSL_SHA384_CTX sha384;
82 CYASSL_SHA512_CTX sha512;
85 CYASSL_RIPEMD_CTX ripemd;
90 typedef struct CYASSL_EVP_MD_CTX {
91 unsigned char macType;
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,
112 DES_EDE3_CBC_TYPE = 8,
114 NULL_CIPHER_TYPE = 10,
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;
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);
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,
139 CYASSL_API int CyaSSL_EVP_DigestFinal(CYASSL_EVP_MD_CTX* ctx, unsigned char* md,
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*,
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);
151 CYASSL_API int CyaSSL_EVP_CIPHER_CTX_iv_length(const CYASSL_EVP_CIPHER_CTX*);
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,
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,
161 CYASSL_API int CyaSSL_EVP_Cipher(CYASSL_EVP_CIPHER_CTX* ctx,
162 unsigned char* dst, unsigned char* src,
165 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_get_digestbynid(int);
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*);
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);
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);
179 CYASSL_API int CyaSSL_StoreExternalIV(CYASSL_EVP_CIPHER_CTX* ctx);
180 CYASSL_API int CyaSSL_SetInternalIV(CYASSL_EVP_CIPHER_CTX* ctx);
183 /* end OpenSSH compat */
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;
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
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
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
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
225 #define EVP_get_digestbynid CyaSSL_EVP_get_digestbynid
227 #define EVP_PKEY_get1_RSA CyaSSL_EVP_PKEY_get1_RSA
228 #define EVP_PKEY_get1_DSA CyaSSL_EVP_PKEY_get1_DSA
230 #ifndef EVP_MAX_MD_SIZE
231 #define EVP_MAX_MD_SIZE 64 /* sha512 */
239 #endif /* CYASSL_EVP_H_ */