Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ AC_CONFIG_MACRO_DIR([m4])
m4_include([m4/ax_pthread.m4])
AX_PTHREAD()

m4_include([m4/ax_openssl_version.m4])

if test x"${cross_compiling}" = "xno" ; then
# Bring additional directories where things might be found into our
# search path. I don't know why autoconf doesn't do this by default
Expand Down Expand Up @@ -213,15 +215,37 @@ AM_CONDITIONAL(HAVE_PYTHON, test "$enable_python" = yes)
## Always check for headers, then libs, then functions
###

AC_CHECK_HEADERS([openssl/aes.h openssl/bio.h openssl/evp.h openssl/hmac.h openssl/md5.h openssl/rand.h openssl/rsa.h openssl/sha.h openssl/pem.h openssl/x509.h])
AC_CHECK_HEADERS([openssl/opensslv.h openssl/bio.h openssl/evp.h openssl/hmac.h openssl/rand.h openssl/rsa.h openssl/pem.h openssl/x509.h])

AC_CHECK_LIB([crypto],[EVP_get_digestbyname],,
AC_MSG_ERROR([OpenSSL developer library 'libssl-dev' or 'openssl-devel' not installed; cannot continue.]))

AC_CHECK_LIB([md],[MD5]) # if libmd is available, get it

AC_CHECK_FUNCS([MD5 SHA1 AES_encrypt RAND_bytes des_read_pw_string EVP_read_pw_string EVP_MD_size])
AC_CHECK_FUNCS([PEM_read_bio_RSA_PUBKEY])
AC_CHECK_FUNCS([PEM_read_bio_RSA_PUBKEY des_read_pw_string EVP_read_pw_string EVP_MD_size])

dnl Detect the OpenSSL version in use (works with OpenSSL 1.0.2, 1.1.1,
dnl 3.x, 4.x and LibreSSL, including when cross-compiling).
AX_OPENSSL_VERSION

dnl The crypto code is written against the stable EVP API (EVP_MD_CTX,
dnl EVP_CIPHER_CTX), which is supported on OpenSSL 1.0.2, 1.1.1, 3.x, 4.x
dnl and LibreSSL.
dnl Define HAVE_MD5 / HAVE_SHA1 / HAVE_AES_ENCRYPT -- previously derived from
dnl the now-deprecated low-level MD5/SHA1/AES symbols -- from probes of the
dnl EVP symbols instead. This keeps the build clean on OpenSSL 3.x and 4.x
dnl (including no-deprecated builds) and working on OpenSSL 1.0.2/1.1.1
dnl (where EVP_MD_CTX_create/destroy are used) and LibreSSL.
AC_CHECK_FUNCS([EVP_MD_CTX_new EVP_MD_CTX_free EVP_MD_CTX_create EVP_MD_CTX_destroy EVP_DigestInit_ex EVP_DigestUpdate EVP_DigestFinal_ex])
AC_CHECK_FUNCS([EVP_CIPHER_CTX_new EVP_CIPHER_CTX_free EVP_CIPHER_CTX_copy EVP_EncryptInit_ex EVP_EncryptUpdate EVP_EncryptFinal_ex EVP_DecryptInit_ex EVP_DecryptUpdate EVP_DecryptFinal_ex EVP_CIPHER_CTX_set_padding])
AC_CHECK_FUNCS([EVP_md5 EVP_sha1 EVP_sha256 EVP_aes_128_cbc EVP_aes_256_cbc EVP_aes_256_ecb RAND_bytes])

AS_IF([test "$ac_cv_func_EVP_md5" = "yes" && (test "$ac_cv_func_EVP_MD_CTX_new" = "yes" || test "$ac_cv_func_EVP_MD_CTX_create" = "yes")],
[AC_DEFINE([HAVE_MD5],1,[Define to 1 if OpenSSL provides MD5 through the EVP API])])
AS_IF([test "$ac_cv_func_EVP_sha1" = "yes" && (test "$ac_cv_func_EVP_MD_CTX_new" = "yes" || test "$ac_cv_func_EVP_MD_CTX_create" = "yes")],
[AC_DEFINE([HAVE_SHA1],1,[Define to 1 if OpenSSL provides SHA1 through the EVP API])])
AS_IF([test "$ac_cv_func_EVP_aes_256_cbc" = "yes" && test "$ac_cv_func_EVP_CIPHER_CTX_new" = "yes" && test "$ac_cv_func_EVP_CIPHER_CTX_copy" = "yes"],
[AC_DEFINE([HAVE_AES_ENCRYPT],1,[Define to 1 if OpenSSL provides AES-256 through the EVP API])])

################################################################
## FUSE: Filesystem in Userspace
Expand Down Expand Up @@ -296,6 +320,7 @@ AC_MSG_NOTICE([PYTHON Bindings: ${enable_python}])
AC_MSG_NOTICE([QEMU Image Drivers: ${enable_qemu}])
AC_MSG_NOTICE([FUSE: ${enable_fuse}])
AC_MSG_NOTICE([LIBEXPAT: ${have_expat} (needed for AFF signatures)])
AC_MSG_NOTICE([OpenSSL: ${OPENSSL_VERSION} (EVP-based digest and cipher APIs)])
AC_MSG_NOTICE([])
AC_MSG_NOTICE([CFLAGS: ${CFLAGS}])
AC_MSG_NOTICE([CPPFLAGS: ${CPPFLAGS}])
Expand Down
2 changes: 2 additions & 0 deletions include/afflib/afflib.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ int af_display_as_hex(const char *segname); // afflib recommends displaying this

/* Crypto */
/* AFF Base Encryption */
int af_MD5(const unsigned char *buf,size_t buflen,unsigned char md[16]); // return 0 if success, -1 if no cipher
int af_sha1(const unsigned char *buf,size_t buflen,unsigned char md[20]); // return 0 if success, -1 if no cipher
int af_SHA256(const unsigned char *buf,size_t buflen,unsigned char md[32]); // return 0 if success, -1 if no cipher
int af_set_aes_key(AFFILE *af,const unsigned char *userKey,const int bits);
int af_cannot_decrypt(AFFILE *af); // encrypted pages are present which cannot be decrypted
Expand Down
30 changes: 24 additions & 6 deletions include/afflib/afflib_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,29 @@ void __cdecl AFDbgPrint (PCSTR Format,...);
#endif

#ifdef HAVE_LIBCRYPTO
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <openssl/rsa.h> // a standard part of OpenSSL
#include <openssl/rand.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <openssl/x509.h> // X509 is used by struct af_crypto
/* The AES block size (16 bytes). Formerly taken from <openssl/aes.h>;
* that header is deprecated/removed in OpenSSL 3.x/4.x no-deprecated builds,
* so define it locally (AES blocks are always 16 bytes).
*/
#define AFF_AES_BLOCK_SIZE 16

/* Support OpenSSL before 1.1.0 and LibreSSL before 2.7.0
* (renamed EVP_MD_CTX_create/destroy to EVP_MD_CTX_new/free).
* Note: EVP_CIPHER_CTX_new/free already exist under those names in all versions.
*/
#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)) || \
(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
#ifndef EVP_MD_CTX_new
#define EVP_MD_CTX_new EVP_MD_CTX_create
#endif
#ifndef EVP_MD_CTX_free
#define EVP_MD_CTX_free EVP_MD_CTX_destroy
#endif
#endif
#endif

#ifdef HAVE_PTHREAD
Expand Down Expand Up @@ -432,9 +450,9 @@ struct af_crypto {
uint32_t sealing_key_set:1; // encryption key has been set
uint32_t auto_encrypt:1; // encrypt segments when we write
uint32_t auto_decrypt:1; // automatically decrypto when we read
#ifdef AES_BLOCK_SIZE
AES_KEY ekey; // encrypt key
AES_KEY dkey; // decrypt key
#ifdef HAVE_AES_ENCRYPT
EVP_CIPHER_CTX *ekey; // EVP cipher context holding the encryption key (AES-256-CBC); NULL until set
EVP_CIPHER_CTX *dkey; // EVP cipher context holding the decryption key (AES-256-CBC); NULL until set
#endif
#ifdef HAVE_OPENSSL_EVP_H
EVP_PKEY *sign_privkey; // signing private key (to write signatures)
Expand Down
1 change: 0 additions & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ libafflib_la_SOURCES = $(AFFLIB_SOURCES)
QEMU_SOURCES = \
vnode_qemu.cpp \
vnode_qemu.h \
qemu/aes.h \
qemu/aff-block-vmdk.h \
qemu/block-bochs.c \
qemu/block-cloop.c \
Expand Down
Loading
Loading