3 * Copyright (C) 2013 wolfSSL Inc.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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;
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,
114 DES_EDE3_CBC_TYPE = 8,
116 NULL_CIPHER_TYPE = 10,
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;
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);
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,
141 CYASSL_API int CyaSSL_EVP_DigestFinal(CYASSL_EVP_MD_CTX* ctx, unsigned char* md,
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*,
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);
153 CYASSL_API int CyaSSL_EVP_CIPHER_CTX_iv_length(const CYASSL_EVP_CIPHER_CTX*);
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,
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,
163 CYASSL_API int CyaSSL_EVP_Cipher(CYASSL_EVP_CIPHER_CTX* ctx,
164 unsigned char* dst, unsigned char* src,
167 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_get_digestbynid(int);
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*);
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);
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);
181 CYASSL_API int CyaSSL_StoreExternalIV(CYASSL_EVP_CIPHER_CTX* ctx);
182 CYASSL_API int CyaSSL_SetInternalIV(CYASSL_EVP_CIPHER_CTX* ctx);
185 /* end OpenSSH compat */
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;
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
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
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
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
227 #define EVP_get_digestbynid CyaSSL_EVP_get_digestbynid
229 #define EVP_PKEY_get1_RSA CyaSSL_EVP_PKEY_get1_RSA
230 #define EVP_PKEY_get1_DSA CyaSSL_EVP_PKEY_get1_DSA
232 #ifndef EVP_MAX_MD_SIZE
233 #define EVP_MAX_MD_SIZE 64 /* sha512 */
241 #endif /* CYASSL_EVP_H_ */