Skip to content

Commit bacc024

Browse files
bbarenblatachernya
authored andcommitted
Go back to internal OCB implementation
After further discussion, the Mosh maintainers have decided to stick with the internal OCB implementation for this release. Restore support for using OpenSSL’s AES but internal OCB. To make this commit easy to audit, restore the code exactly, including calls to AES functions that are deprecated in OpenSSL 3; a future commit will update ocb_internal.cc to use EVP instead of directly calling the AES primitives. In anticipation of future changes, preserve support for OpenSSL’s AES-OCB, but don’t compile it in. Add --with-crypto-library=openssl-with-openssl-ocb and --with-crypto-library=openssl-with-internal-ocb options to configure so that developers can easily test Mosh using OpenSSL’s AES-OCB. These options are intended only for testing, are undocumented, and are not subject to any API stability guarantees. Rework configure to look for all possible cryptography libraries first and then dispatch on --with-crypto-library as appropriate.
1 parent 135a11a commit bacc024

3 files changed

Lines changed: 70 additions & 26 deletions

File tree

configure.ac

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -365,44 +365,58 @@ AC_ARG_WITH(
365365
[AS_HELP_STRING([--with-crypto-library=library], [build with the given crypto library, TYPE=openssl|nettle|apple-common-crypto @<:@default=openssl@:>@])],
366366
[
367367
case "${withval}" in
368-
openssl|nettle|apple-common-crypto) ;;
368+
openssl|openssl-with-internal-ocb|openssl-with-openssl-ocb|nettle|apple-common-crypto) ;;
369369
*) AC_MSG_ERROR([bad value ${withval} for --with-crypto-library]) ;;
370370
esac
371371
],
372372
[with_crypto_library="$default_crypto_library"]
373373
)
374374

375375
dnl Checks for chosen crypto library
376+
PKG_CHECK_MODULES([OpenSSL], [openssl],
377+
[have_openssl=yes
378+
AC_CHECK_LIB([crypto], [AES_encrypt], [have_deprecated_openssl_aes=yes])
379+
AC_CHECK_LIB([crypto], [EVP_aes_128_ocb], [have_evp_aes_ocb=yes])],
380+
[:])
381+
PKG_CHECK_MODULES([Nettle], [nettle], [have_nettle=yes], [:])
382+
AS_CASE([$with_crypto_library],
383+
[openssl*],
384+
[AS_IF([test "x$have_openssl" != xyes],
385+
[AC_MSG_ERROR([OpenSSL crypto library not found])])
386+
AC_DEFINE([USE_OPENSSL_AES], [1], [Use OpenSSL library])
387+
AC_SUBST([CRYPTO_CFLAGS], ["$OpenSSL_CFLAGS"])
388+
AC_SUBST([CRYPTO_LIBS], ["$OpenSSL_LDFLAGS -lcrypto"])])
376389
case "${with_crypto_library}" in
377-
openssl)
378-
PKG_CHECK_MODULES([CRYPTO], [openssl],
379-
[AC_DEFINE([USE_OPENSSL_AES], [1], [Use OpenSSL library])],
380-
[AX_CHECK_LIBRARY([CRYPTO], [openssl/aes.h], [crypto],
381-
[AC_DEFINE([USE_OPENSSL_AES], [1], [Use OpenSSL library])
382-
AC_SUBST([CRYPTO_CFLAGS], ["$CRYPTO_CPPFLAGS"])
383-
AC_SUBST([CRYPTO_LIBS], ["$CRYPTO_LDFLAGS -lcrypto"])],
384-
[AC_MSG_ERROR([OpenSSL crypto library not found])])])
385-
;;
390+
openssl|openssl-with-internal-ocb)
391+
AS_IF([test "x$have_deprecated_openssl_aes" != xyes],
392+
[AC_MSG_ERROR([found OpenSSL without AES support])])
393+
AM_CONDITIONAL([USE_AES_OCB_FROM_OPENSSL], [false])
394+
human_readable_cryptography_description='internal OCB, OpenSSL AES'
395+
;;
396+
openssl-with-openssl-ocb)
397+
AS_IF([test "x$have_evp_aes_ocb" != xyes],
398+
[AC_MSG_ERROR([found OpenSSL without AES-OCB support])])
399+
AM_CONDITIONAL([USE_AES_OCB_FROM_OPENSSL], [true])
400+
human_readable_cryptography_description='OpenSSL OCB, OpenSSL AES'
401+
;;
386402
nettle)
387-
PKG_CHECK_MODULES([CRYPTO], [nettle],
388-
[],
403+
AS_IF([test "x$have_nettle" != xyes],
389404
[AC_MSG_ERROR([Nettle crypto library not found])])
390405
AC_DEFINE([USE_NETTLE_AES], [1], [Use Nettle library])
406+
AC_SUBST([CRYPTO_CFLAGS], ["$Nettle_CFLAGS"])
407+
AC_SUBST([CRYPTO_LIBS], ["$Nettle_LDFLAGS"])
408+
AM_CONDITIONAL([USE_AES_OCB_FROM_OPENSSL], [false])
409+
human_readable_cryptography_description='internal OCB, Nettle AES'
391410
;;
392411
apple-common-crypto)
393-
dnl Common Crypto is in Apple's standard paths and base libraries.
394-
dnl So just check for presence of the header.
395-
AC_CHECK_HEADERS([CommonCrypto/CommonCrypto.h],
396-
[],
412+
AS_IF([test "x$ac_cv_header_CommonCrypto_CommonCrypto_h" != xyes],
397413
[AC_MSG_ERROR([Apple Common Crypto header not found])])
398414
AC_DEFINE([USE_APPLE_COMMON_CRYPTO_AES], [1], [Use Apple Common Crypto library])
415+
AM_CONDITIONAL([USE_AES_OCB_FROM_OPENSSL], [false])
416+
human_readable_cryptography_description='internal OCB, Apple Common Crypto AES'
399417
;;
400418
esac
401419

402-
AM_CONDITIONAL([CRYPTO_LIBRARY_OPENSSL], [test x$with_crypto_library = xopenssl])
403-
AM_CONDITIONAL([CRYPTO_LIBRARY_NETTLE], [test x$with_crypto_library = xnettle])
404-
AM_CONDITIONAL([CRYPTO_LIBRARY_APPLE], [test x$with_crypto_library = xapple-common-crypto])
405-
406420
AC_ARG_ENABLE([static-crypto],
407421
[AS_HELP_STRING([--enable-static-crypto], [Link crypto library statically @<:@no@:>@])],
408422
[], [enable_static_crypto="$enable_static_libraries"])
@@ -585,4 +599,5 @@ AC_MSG_NOTICE([c++ compiler: $CXX])
585599
AC_MSG_NOTICE([Warning CXXFLAGS: $WARNING_CXXFLAGS])
586600
AC_MSG_NOTICE([Picky CXXFLAGS: $PICKY_CXXFLAGS])
587601
AC_MSG_NOTICE([Harden CFLAGS: $HARDEN_CFLAGS])
602+
AC_MSG_NOTICE([Cryptography: $human_readable_cryptography_description])
588603
AC_MSG_NOTICE([ =============================])

src/crypto/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXF
44
noinst_LIBRARIES = libmoshcrypto.a
55

66
OCB_SRCS = ae.h
7-
if CRYPTO_LIBRARY_OPENSSL
7+
if USE_AES_OCB_FROM_OPENSSL
88
OCB_SRCS += ocb_openssl.cc
99
else
1010
OCB_SRCS += ocb_internal.cc

src/crypto/ocb_internal.cc

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626

2727
#include "config.h"
2828

29-
/* This module implements the ae.h interface for Apple Common Crypto and
30-
/ Nettle. */
31-
#if !defined(USE_APPLE_COMMON_CRYPTO_AES) && !defined(USE_NETTLE_AES)
32-
#error ocb_internal.cc only works with Apple Common Crypto or Nettle
29+
/* This module implements the ae.h interface for OpenSSL, Apple Common
30+
/ Crypto, and Nettle. */
31+
#if !defined(USE_OPENSSL_AES) && !defined(USE_APPLE_COMMON_CRYPTO_AES) && \
32+
!defined(USE_NETTLE_AES)
33+
#error ocb_internal.cc only works with OpenSSL, Apple Common Crypto, or Nettle
3334
#endif
3435

3536
/* ----------------------------------------------------------------------- */
@@ -62,6 +63,7 @@
6263
#if 0
6364
#define USE_APPLE_COMMON_CRYPTO_AES 0
6465
#define USE_NETTLE_AES 0
66+
#define USE_OPENSSL_AES 1 /* http://openssl.org */
6567
#endif
6668

6769
/* During encryption and decryption, various "L values" are required.
@@ -352,8 +354,31 @@
352354
/* AES - Code uses OpenSSL API. Other implementations get mapped to it. */
353355
/* ----------------------------------------------------------------------- */
354356

357+
/*---------------*/
358+
#if USE_OPENSSL_AES
359+
/*---------------*/
360+
361+
#include <openssl/aes.h> /* http://openssl.org/ */
362+
363+
/* How to ECB encrypt an array of blocks, in place */
364+
static inline void AES_ecb_encrypt_blks(block *blks, unsigned nblks, AES_KEY *key) {
365+
while (nblks) {
366+
--nblks;
367+
AES_encrypt((unsigned char *)(blks+nblks), (unsigned char *)(blks+nblks), key);
368+
}
369+
}
370+
371+
static inline void AES_ecb_decrypt_blks(block *blks, unsigned nblks, AES_KEY *key) {
372+
while (nblks) {
373+
--nblks;
374+
AES_decrypt((unsigned char *)(blks+nblks), (unsigned char *)(blks+nblks), key);
375+
}
376+
}
377+
378+
#define BPI 4 /* Number of blocks in buffer per ECB call */
379+
355380
/*-------------------*/
356-
#if USE_APPLE_COMMON_CRYPTO_AES
381+
#elif USE_APPLE_COMMON_CRYPTO_AES
357382
/*-------------------*/
358383

359384
#include <fatal_assert.h>
@@ -1320,3 +1345,7 @@ int main()
13201345
return 0;
13211346
}
13221347
#endif
1348+
1349+
#if USE_OPENSSL_AES
1350+
char infoString[] = "OCB3 (OpenSSL)";
1351+
#endif

0 commit comments

Comments
 (0)