xref: /freebsd/crypto/openssh/openbsd-compat/openssl-compat.h (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /* $Id: openssl-compat.h,v 1.26 2014/02/13 05:38:33 dtucker Exp $ */
2 
3 /*
4  * Copyright (c) 2005 Darren Tucker <dtucker@zip.com.au>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "includes.h"
20 #include <openssl/opensslv.h>
21 #include <openssl/evp.h>
22 #include <openssl/rsa.h>
23 #include <openssl/dsa.h>
24 
25 /* Only in 0.9.8 */
26 #ifndef OPENSSL_DSA_MAX_MODULUS_BITS
27 # define OPENSSL_DSA_MAX_MODULUS_BITS        10000
28 #endif
29 #ifndef OPENSSL_RSA_MAX_MODULUS_BITS
30 # define OPENSSL_RSA_MAX_MODULUS_BITS        16384
31 #endif
32 
33 /* OPENSSL_free() is Free() in versions before OpenSSL 0.9.6 */
34 #if !defined(OPENSSL_VERSION_NUMBER) || (OPENSSL_VERSION_NUMBER < 0x0090600f)
35 # define OPENSSL_free(x) Free(x)
36 #endif
37 
38 #if OPENSSL_VERSION_NUMBER < 0x00906000L
39 # define SSH_OLD_EVP
40 # define EVP_CIPHER_CTX_get_app_data(e)		((e)->app_data)
41 #endif
42 
43 #if OPENSSL_VERSION_NUMBER < 0x10000001L
44 # define LIBCRYPTO_EVP_INL_TYPE unsigned int
45 #else
46 # define LIBCRYPTO_EVP_INL_TYPE size_t
47 #endif
48 
49 #if (OPENSSL_VERSION_NUMBER < 0x00907000L) || defined(OPENSSL_LOBOTOMISED_AES)
50 # define USE_BUILTIN_RIJNDAEL
51 #endif
52 
53 #ifdef USE_BUILTIN_RIJNDAEL
54 # include "rijndael.h"
55 # define AES_KEY rijndael_ctx
56 # define AES_BLOCK_SIZE 16
57 # define AES_encrypt(a, b, c)		rijndael_encrypt(c, a, b)
58 # define AES_set_encrypt_key(a, b, c)	rijndael_set_key(c, (char *)a, b, 1)
59 # define EVP_aes_128_cbc evp_rijndael
60 # define EVP_aes_192_cbc evp_rijndael
61 # define EVP_aes_256_cbc evp_rijndael
62 const EVP_CIPHER *evp_rijndael(void);
63 void ssh_rijndael_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
64 #endif
65 
66 #ifndef OPENSSL_HAVE_EVPCTR
67 #define EVP_aes_128_ctr evp_aes_128_ctr
68 #define EVP_aes_192_ctr evp_aes_128_ctr
69 #define EVP_aes_256_ctr evp_aes_128_ctr
70 const EVP_CIPHER *evp_aes_128_ctr(void);
71 void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, size_t);
72 #endif
73 
74 /* Avoid some #ifdef. Code that uses these is unreachable without GCM */
75 #if !defined(OPENSSL_HAVE_EVPGCM) && !defined(EVP_CTRL_GCM_SET_IV_FIXED)
76 # define EVP_CTRL_GCM_SET_IV_FIXED -1
77 # define EVP_CTRL_GCM_IV_GEN -1
78 # define EVP_CTRL_GCM_SET_TAG -1
79 # define EVP_CTRL_GCM_GET_TAG -1
80 #endif
81 
82 /* Replace missing EVP_CIPHER_CTX_ctrl() with something that returns failure */
83 #ifndef HAVE_EVP_CIPHER_CTX_CTRL
84 # ifdef OPENSSL_HAVE_EVPGCM
85 #  error AES-GCM enabled without EVP_CIPHER_CTX_ctrl /* shouldn't happen */
86 # else
87 # define EVP_CIPHER_CTX_ctrl(a,b,c,d) (0)
88 # endif
89 #endif
90 
91 #if OPENSSL_VERSION_NUMBER < 0x00907000L
92 #define EVP_X_STATE(evp)	&(evp).c
93 #define EVP_X_STATE_LEN(evp)	sizeof((evp).c)
94 #else
95 #define EVP_X_STATE(evp)	(evp).cipher_data
96 #define EVP_X_STATE_LEN(evp)	(evp).cipher->ctx_size
97 #endif
98 
99 /* OpenSSL 0.9.8e returns cipher key len not context key len */
100 #if (OPENSSL_VERSION_NUMBER == 0x0090805fL)
101 # define EVP_CIPHER_CTX_key_length(c) ((c)->key_len)
102 #endif
103 
104 #ifndef HAVE_RSA_GET_DEFAULT_METHOD
105 RSA_METHOD *RSA_get_default_method(void);
106 #endif
107 
108 /*
109  * We overload some of the OpenSSL crypto functions with ssh_* equivalents
110  * which cater for older and/or less featureful OpenSSL version.
111  *
112  * In order for the compat library to call the real functions, it must
113  * define SSH_DONT_OVERLOAD_OPENSSL_FUNCS before including this file and
114  * implement the ssh_* equivalents.
115  */
116 #ifndef SSH_DONT_OVERLOAD_OPENSSL_FUNCS
117 
118 # ifdef SSH_OLD_EVP
119 #  ifdef EVP_Cipher
120 #   undef EVP_Cipher
121 #  endif
122 #  define EVP_CipherInit(a,b,c,d,e)	ssh_EVP_CipherInit((a),(b),(c),(d),(e))
123 #  define EVP_Cipher(a,b,c,d)		ssh_EVP_Cipher((a),(b),(c),(d))
124 #  define EVP_CIPHER_CTX_cleanup(a)	ssh_EVP_CIPHER_CTX_cleanup((a))
125 # endif /* SSH_OLD_EVP */
126 
127 # ifdef OPENSSL_EVP_DIGESTUPDATE_VOID
128 #  define EVP_DigestUpdate(a,b,c)	ssh_EVP_DigestUpdate((a),(b),(c))
129 #  endif
130 
131 # ifdef USE_OPENSSL_ENGINE
132 #  ifdef OpenSSL_add_all_algorithms
133 #   undef OpenSSL_add_all_algorithms
134 #  endif
135 #  define OpenSSL_add_all_algorithms()  ssh_OpenSSL_add_all_algorithms()
136 # endif
137 
138 # ifndef HAVE_BN_IS_PRIME_EX
139 int BN_is_prime_ex(const BIGNUM *, int, BN_CTX *, void *);
140 # endif
141 
142 # ifndef HAVE_DSA_GENERATE_PARAMETERS_EX
143 int DSA_generate_parameters_ex(DSA *, int, const unsigned char *, int, int *,
144     unsigned long *, void *);
145 # endif
146 
147 # ifndef HAVE_RSA_GENERATE_KEY_EX
148 int RSA_generate_key_ex(RSA *, int, BIGNUM *, void *);
149 # endif
150 
151 # ifndef HAVE_EVP_DIGESTINIT_EX
152 int EVP_DigestInit_ex(EVP_MD_CTX *, const EVP_MD *, void *);
153 # endif
154 
155 # ifndef HAVE_EVP_DISESTFINAL_EX
156 int EVP_DigestFinal_ex(EVP_MD_CTX *, unsigned char *, unsigned int *);
157 # endif
158 
159 # ifndef EVP_MD_CTX_COPY_EX
160 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *, const EVP_MD_CTX *);
161 # endif
162 
163 int ssh_EVP_CipherInit(EVP_CIPHER_CTX *, const EVP_CIPHER *, unsigned char *,
164     unsigned char *, int);
165 int ssh_EVP_Cipher(EVP_CIPHER_CTX *, char *, char *, int);
166 int ssh_EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);
167 void ssh_OpenSSL_add_all_algorithms(void);
168 
169 # ifndef HAVE_HMAC_CTX_INIT
170 #  define HMAC_CTX_init(a)
171 # endif
172 
173 # ifndef HAVE_EVP_MD_CTX_INIT
174 #  define EVP_MD_CTX_init(a)
175 # endif
176 
177 # ifndef HAVE_EVP_MD_CTX_CLEANUP
178 #  define EVP_MD_CTX_cleanup(a)
179 # endif
180 
181 #endif	/* SSH_DONT_OVERLOAD_OPENSSL_FUNCS */
182 
183