1*462c32cbSDag-Erling Smørgrav /* $Id: openssl-compat.h,v 1.20 2012/01/17 03:03:39 dtucker Exp $ */ 2043840dfSDag-Erling Smørgrav 3043840dfSDag-Erling Smørgrav /* 4043840dfSDag-Erling Smørgrav * Copyright (c) 2005 Darren Tucker <dtucker@zip.com.au> 5043840dfSDag-Erling Smørgrav * 6043840dfSDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any 7043840dfSDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above 8043840dfSDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies. 9043840dfSDag-Erling Smørgrav * 10043840dfSDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11043840dfSDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12043840dfSDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13043840dfSDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14043840dfSDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15043840dfSDag-Erling Smørgrav * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16043840dfSDag-Erling Smørgrav * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17043840dfSDag-Erling Smørgrav */ 18043840dfSDag-Erling Smørgrav 19043840dfSDag-Erling Smørgrav #include "includes.h" 204a421b63SDag-Erling Smørgrav #include <openssl/opensslv.h> 21043840dfSDag-Erling Smørgrav #include <openssl/evp.h> 22e2f6069cSDag-Erling Smørgrav #include <openssl/rsa.h> 23e2f6069cSDag-Erling Smørgrav #include <openssl/dsa.h> 24e2f6069cSDag-Erling Smørgrav 25e2f6069cSDag-Erling Smørgrav /* Only in 0.9.8 */ 26e2f6069cSDag-Erling Smørgrav #ifndef OPENSSL_DSA_MAX_MODULUS_BITS 27e2f6069cSDag-Erling Smørgrav # define OPENSSL_DSA_MAX_MODULUS_BITS 10000 28e2f6069cSDag-Erling Smørgrav #endif 29e2f6069cSDag-Erling Smørgrav #ifndef OPENSSL_RSA_MAX_MODULUS_BITS 30e2f6069cSDag-Erling Smørgrav # define OPENSSL_RSA_MAX_MODULUS_BITS 16384 31e2f6069cSDag-Erling Smørgrav #endif 32043840dfSDag-Erling Smørgrav 33d4af9e69SDag-Erling Smørgrav /* OPENSSL_free() is Free() in versions before OpenSSL 0.9.6 */ 34d4af9e69SDag-Erling Smørgrav #if !defined(OPENSSL_VERSION_NUMBER) || (OPENSSL_VERSION_NUMBER < 0x0090600f) 35d4af9e69SDag-Erling Smørgrav # define OPENSSL_free(x) Free(x) 36d4af9e69SDag-Erling Smørgrav #endif 37d4af9e69SDag-Erling Smørgrav 38043840dfSDag-Erling Smørgrav #if OPENSSL_VERSION_NUMBER < 0x00906000L 39043840dfSDag-Erling Smørgrav # define SSH_OLD_EVP 40043840dfSDag-Erling Smørgrav # define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data) 41043840dfSDag-Erling Smørgrav #endif 42043840dfSDag-Erling Smørgrav 434a421b63SDag-Erling Smørgrav #if OPENSSL_VERSION_NUMBER < 0x1000000fL 444a421b63SDag-Erling Smørgrav # define LIBCRYPTO_EVP_INL_TYPE unsigned int 454a421b63SDag-Erling Smørgrav #else 464a421b63SDag-Erling Smørgrav # define LIBCRYPTO_EVP_INL_TYPE size_t 474a421b63SDag-Erling Smørgrav #endif 484a421b63SDag-Erling Smørgrav 49021d409fSDag-Erling Smørgrav #if (OPENSSL_VERSION_NUMBER < 0x00907000L) || defined(OPENSSL_LOBOTOMISED_AES) 50021d409fSDag-Erling Smørgrav # define USE_BUILTIN_RIJNDAEL 51021d409fSDag-Erling Smørgrav #endif 52021d409fSDag-Erling Smørgrav 53021d409fSDag-Erling Smørgrav #ifdef USE_BUILTIN_RIJNDAEL 54d4af9e69SDag-Erling Smørgrav # include "rijndael.h" 55d4af9e69SDag-Erling Smørgrav # define AES_KEY rijndael_ctx 56d4af9e69SDag-Erling Smørgrav # define AES_BLOCK_SIZE 16 57d4af9e69SDag-Erling Smørgrav # define AES_encrypt(a, b, c) rijndael_encrypt(c, a, b) 58d4af9e69SDag-Erling Smørgrav # define AES_set_encrypt_key(a, b, c) rijndael_set_key(c, (char *)a, b, 1) 59043840dfSDag-Erling Smørgrav # define EVP_aes_128_cbc evp_rijndael 60043840dfSDag-Erling Smørgrav # define EVP_aes_192_cbc evp_rijndael 61043840dfSDag-Erling Smørgrav # define EVP_aes_256_cbc evp_rijndael 62043840dfSDag-Erling Smørgrav extern const EVP_CIPHER *evp_rijndael(void); 63043840dfSDag-Erling Smørgrav extern void ssh_rijndael_iv(EVP_CIPHER_CTX *, int, u_char *, u_int); 64043840dfSDag-Erling Smørgrav #endif 65043840dfSDag-Erling Smørgrav 66043840dfSDag-Erling Smørgrav #if !defined(EVP_CTRL_SET_ACSS_MODE) 67043840dfSDag-Erling Smørgrav # if (OPENSSL_VERSION_NUMBER >= 0x00907000L) 68043840dfSDag-Erling Smørgrav # define USE_CIPHER_ACSS 1 69043840dfSDag-Erling Smørgrav extern const EVP_CIPHER *evp_acss(void); 70043840dfSDag-Erling Smørgrav # define EVP_acss evp_acss 71043840dfSDag-Erling Smørgrav # else 72043840dfSDag-Erling Smørgrav # define EVP_acss NULL 73043840dfSDag-Erling Smørgrav # endif 74043840dfSDag-Erling Smørgrav #endif 75043840dfSDag-Erling Smørgrav 76d4af9e69SDag-Erling Smørgrav /* OpenSSL 0.9.8e returns cipher key len not context key len */ 77d4af9e69SDag-Erling Smørgrav #if (OPENSSL_VERSION_NUMBER == 0x0090805fL) 78d4af9e69SDag-Erling Smørgrav # define EVP_CIPHER_CTX_key_length(c) ((c)->key_len) 79d4af9e69SDag-Erling Smørgrav #endif 80d4af9e69SDag-Erling Smørgrav 814a421b63SDag-Erling Smørgrav #ifndef HAVE_RSA_GET_DEFAULT_METHOD 824a421b63SDag-Erling Smørgrav RSA_METHOD *RSA_get_default_method(void); 834a421b63SDag-Erling Smørgrav #endif 844a421b63SDag-Erling Smørgrav 85043840dfSDag-Erling Smørgrav /* 86021d409fSDag-Erling Smørgrav * We overload some of the OpenSSL crypto functions with ssh_* equivalents 87021d409fSDag-Erling Smørgrav * which cater for older and/or less featureful OpenSSL version. 88021d409fSDag-Erling Smørgrav * 89021d409fSDag-Erling Smørgrav * In order for the compat library to call the real functions, it must 90021d409fSDag-Erling Smørgrav * define SSH_DONT_OVERLOAD_OPENSSL_FUNCS before including this file and 91021d409fSDag-Erling Smørgrav * implement the ssh_* equivalents. 92043840dfSDag-Erling Smørgrav */ 93761efaa7SDag-Erling Smørgrav #ifndef SSH_DONT_OVERLOAD_OPENSSL_FUNCS 94761efaa7SDag-Erling Smørgrav 95043840dfSDag-Erling Smørgrav # ifdef SSH_OLD_EVP 96043840dfSDag-Erling Smørgrav # ifdef EVP_Cipher 97043840dfSDag-Erling Smørgrav # undef EVP_Cipher 98043840dfSDag-Erling Smørgrav # endif 99043840dfSDag-Erling Smørgrav # define EVP_CipherInit(a,b,c,d,e) ssh_EVP_CipherInit((a),(b),(c),(d),(e)) 100043840dfSDag-Erling Smørgrav # define EVP_Cipher(a,b,c,d) ssh_EVP_Cipher((a),(b),(c),(d)) 101043840dfSDag-Erling Smørgrav # define EVP_CIPHER_CTX_cleanup(a) ssh_EVP_CIPHER_CTX_cleanup((a)) 102761efaa7SDag-Erling Smørgrav # endif /* SSH_OLD_EVP */ 103761efaa7SDag-Erling Smørgrav 1047aee6ffeSDag-Erling Smørgrav # ifdef OPENSSL_EVP_DIGESTUPDATE_VOID 1057aee6ffeSDag-Erling Smørgrav # define EVP_DigestUpdate(a,b,c) ssh_EVP_DigestUpdate((a),(b),(c)) 1067aee6ffeSDag-Erling Smørgrav # endif 1077aee6ffeSDag-Erling Smørgrav 108761efaa7SDag-Erling Smørgrav # ifdef USE_OPENSSL_ENGINE 109e146993eSDag-Erling Smørgrav # ifdef OpenSSL_add_all_algorithms 110e146993eSDag-Erling Smørgrav # undef OpenSSL_add_all_algorithms 111761efaa7SDag-Erling Smørgrav # endif 112e146993eSDag-Erling Smørgrav # define OpenSSL_add_all_algorithms() ssh_OpenSSL_add_all_algorithms() 113043840dfSDag-Erling Smørgrav # endif 114043840dfSDag-Erling Smørgrav 1154a421b63SDag-Erling Smørgrav # ifndef HAVE_BN_IS_PRIME_EX 1164a421b63SDag-Erling Smørgrav int BN_is_prime_ex(const BIGNUM *, int, BN_CTX *, void *); 1174a421b63SDag-Erling Smørgrav # endif 1184a421b63SDag-Erling Smørgrav 1194a421b63SDag-Erling Smørgrav # ifndef HAVE_DSA_GENERATE_PARAMETERS_EX 1204a421b63SDag-Erling Smørgrav int DSA_generate_parameters_ex(DSA *, int, const unsigned char *, int, int *, 1214a421b63SDag-Erling Smørgrav unsigned long *, void *); 1224a421b63SDag-Erling Smørgrav # endif 1234a421b63SDag-Erling Smørgrav 1244a421b63SDag-Erling Smørgrav # ifndef HAVE_RSA_GENERATE_KEY_EX 1254a421b63SDag-Erling Smørgrav int RSA_generate_key_ex(RSA *, int, BIGNUM *, void *); 1264a421b63SDag-Erling Smørgrav # endif 1274a421b63SDag-Erling Smørgrav 128043840dfSDag-Erling Smørgrav int ssh_EVP_CipherInit(EVP_CIPHER_CTX *, const EVP_CIPHER *, unsigned char *, 129043840dfSDag-Erling Smørgrav unsigned char *, int); 130043840dfSDag-Erling Smørgrav int ssh_EVP_Cipher(EVP_CIPHER_CTX *, char *, char *, int); 131043840dfSDag-Erling Smørgrav int ssh_EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *); 132e146993eSDag-Erling Smørgrav void ssh_OpenSSL_add_all_algorithms(void); 133*462c32cbSDag-Erling Smørgrav 134*462c32cbSDag-Erling Smørgrav # ifndef HAVE_HMAC_CTX_INIT 135*462c32cbSDag-Erling Smørgrav # define HMAC_CTX_init(a) 136*462c32cbSDag-Erling Smørgrav # endif 137*462c32cbSDag-Erling Smørgrav 138761efaa7SDag-Erling Smørgrav #endif /* SSH_DONT_OVERLOAD_OPENSSL_FUNCS */ 139e2f6069cSDag-Erling Smørgrav 140