Skip to content

Commit 5daed72

Browse files
authored
Configure which crypto library to use (#437)
* Configure which crypto library to use * Fix crypto makefile * incorporate code review suggestions * fix information text * Fix crypto makefile
1 parent 7b04357 commit 5daed72

File tree

4 files changed

+155
-66
lines changed

4 files changed

+155
-66
lines changed

config/efr32/efr32-chip.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ CHIP_CONFIGURE_OPTIONS = \
114114
--disable-docs \
115115
--disable-java \
116116
--disable-device-manager \
117-
--with-mbedtls=internal
117+
--with-crypto=mbedtls
118118

119119
# Enable / disable optimization.
120120
ifeq ($(OPT),1)

config/nrf5/nrf5-chip.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ CHIP_CONFIGURE_OPTIONS = \
113113
--disable-docs \
114114
--disable-java \
115115
--disable-device-manager \
116-
--with-mbedtls=internal
116+
--with-crypto=mbedtls
117117

118118
# Enable / disable optimization.
119119
ifeq ($(OPT),1)

configure.ac

+147-63
Original file line numberDiff line numberDiff line change
@@ -1300,20 +1300,96 @@ fi
13001300

13011301
AC_LANG_POP([C++])
13021302

1303+
#
1304+
# Check for crypto implementation
1305+
#
1306+
CHIP_CRYPTO=
1307+
CHIP_CRYPTO_MBEDTLS=0
1308+
CHIP_CRYPTO_OPENSSL=0
1309+
1310+
AC_MSG_CHECKING([for crypto implementation])
1311+
1312+
# The user may have attempted to explicitly specify the crypto
1313+
# implementation. Sanity check it or default to 'auto'.
1314+
1315+
AC_ARG_WITH(crypto,
1316+
[AS_HELP_STRING([--with-crypto=CRYPTO],
1317+
[Specify the crypto implementation from one of: auto, mbedtls, or openssl @<:@default=auto@:>@.])],
1318+
[
1319+
case "${with_crypto}" in
1320+
auto|mbedtls|openssl)
1321+
;;
1322+
*)
1323+
AC_MSG_ERROR([Invalid value ${with_crypto} for --with-crypto])
1324+
;;
1325+
esac
1326+
],
1327+
[with_crypto=auto])
1328+
1329+
# At this point, the crypto implementation is one of the allowed
1330+
# values. If it's 'auto' we autodetect it.
1331+
1332+
if test "${with_crypto}" = "auto"; then
1333+
case ${target_os} in
1334+
1335+
*cygwin*|*darwin*|*linux*|*freebsd*|*netbsd*|*openbsd*)
1336+
with_crypto=openssl
1337+
;;
1338+
1339+
*freertos*)
1340+
with_crypto=mbedtls
1341+
;;
1342+
1343+
*)
1344+
AC_MSG_RESULT([unknown])
1345+
AC_MSG_ERROR([Unsupported target OS ${target_os}])
1346+
;;
1347+
1348+
esac
1349+
fi
1350+
1351+
CHIP_CRYPTO=${with_crypto}
1352+
1353+
case ${with_crypto} in
1354+
1355+
mbedtls)
1356+
CHIP_CRYPTO_MBEDTLS=1
1357+
;;
1358+
1359+
openssl)
1360+
CHIP_CRYPTO_OPENSSL=1
1361+
;;
1362+
1363+
esac
1364+
1365+
AC_MSG_RESULT(${CHIP_CRYPTO})
1366+
1367+
AC_SUBST(CHIP_CRYPTO)
1368+
AC_DEFINE_UNQUOTED([CHIP_CRYPTO],[${CHIP_CRYPTO}],[CHIP crypto implementation])
1369+
1370+
AC_SUBST(CHIP_CRYPTO_MBEDTLS)
1371+
AM_CONDITIONAL([CHIP_CRYPTO_MBEDTLS], [test "${CHIP_CRYPTO}" = "mbedtls"])
1372+
AC_DEFINE_UNQUOTED([CHIP_CRYPTO_MBEDTLS],[${CHIP_CRYPTO_MBEDTLS}],[Define to 1 if you want to use CHIP with a mbedTLS crypto implementation])
1373+
1374+
AC_SUBST(CHIP_CRYPTO_OPENSSL)
1375+
AM_CONDITIONAL([CHIP_CRYPTO_OPENSSL], [test "${CHIP_CRYPTO}" = "openssl"])
1376+
AC_DEFINE_UNQUOTED([CHIP_CRYPTO_OPENSSL],[${CHIP_CRYPTO_OPENSSL}],[Define to 1 if you want to use CHIP with an OpenSSL crypto implementation])
1377+
13031378
#
13041379
# Checks for libraries and packages.
13051380
#
13061381
# At minimum, the following are required:
13071382
#
13081383
# * nlassert
13091384
# * nlio
1310-
# * openssl
13111385
#
13121386
# The following are optional, depending on configuration:
13131387
#
13141388
# * lwip
1389+
# * mbedtls
13151390
# * nlunit-test
13161391
# * nlfaultinjection
1392+
# * openssl
13171393
#
13181394
# Most of these are supplied "in package"; however, they may be also
13191395
# supplied out of package.
@@ -1324,71 +1400,66 @@ AC_MSG_NOTICE([checking package dependencies])
13241400

13251401
AC_PATH_PROG([PKG_CONFIG],[pkg-config])
13261402

1327-
#
13281403
#
13291404
# OpenSSL
13301405
#
13311406

1332-
NL_WITH_REQUIRED_EXTERNAL_PACKAGE([OpenSSL],
1333-
[OPENSSL],
1334-
[openssl],
1335-
[-lcrypto],
1336-
[
1337-
# Check for required OpenSSL headers.
1338-
1339-
AC_CHECK_HEADERS([openssl/aes.h] [openssl/bn.h] [openssl/crypto.h] [openssl/ec.h] [openssl/err.h] [openssl/evp.h] [openssl/hmac.h] [openssl/kdf.h] [openssl/rand.h] [openssl/sha.h] [openssl/srp.h],
1340-
1341-
[],
1342-
[
1343-
AC_MSG_ERROR(The OpenSSL header "$ac_header" is required but cannot be found.)
1344-
]
1345-
)
1346-
]
1347-
)
1407+
if test "${with_crypto}" = "openssl"; then
1408+
NL_WITH_REQUIRED_EXTERNAL_PACKAGE([OpenSSL],
1409+
[OPENSSL],
1410+
[openssl],
1411+
[-lcrypto],
1412+
[
1413+
# Check for required OpenSSL headers.
1414+
AC_CHECK_HEADERS([openssl/aes.h] [openssl/bn.h] [openssl/crypto.h] [openssl/ec.h] [openssl/err.h] [openssl/evp.h] [openssl/hmac.h] [openssl/kdf.h] [openssl/rand.h] [openssl/sha.h] [openssl/srp.h],
1415+
[],
1416+
[
1417+
AC_MSG_ERROR(The OpenSSL header "$ac_header" is required but cannot be found.)
1418+
]
1419+
)
1420+
]
1421+
)
1422+
fi
13481423

13491424
AM_CONDITIONAL([CHIP_WITH_OPENSSL], [test "${nl_with_openssl}" != "no"])
13501425

13511426
if test "${nl_with_openssl}" = "no"; then
1352-
AC_DEFINE([CHIP_WITH_OPENSSL], [0], [Define to 1 to build CHIP with OpenSSL features])
1427+
AC_DEFINE([CHIP_WITH_OPENSSL], [0], [Define to 1 to build CHIP with OpenSSL])
13531428
else
1354-
AC_DEFINE([CHIP_WITH_OPENSSL], [1], [Define to 1 to build CHIP with OpenSSL features])
1429+
AC_DEFINE([CHIP_WITH_OPENSSL], [1], [Define to 1 to build CHIP with OpenSSL])
13551430
fi
13561431

1357-
#
13581432
#
13591433
# mbedTLS
13601434
#
1361-
if test "${with_mbedtls}" != "internal"; then
1362-
with_mbedtls="no"
1363-
fi
13641435

1365-
NL_WITH_OPTIONAL_INTERNAL_PACKAGE(
1366-
[mbedTLS],
1367-
[MBEDTLS],
1368-
[mbedtls],
1369-
[],
1370-
[
1371-
# At this point, the internal mbedTLS package will be neither
1372-
# configured nor built, so the normal checks we undertake for an
1373-
# external package cannot be run here. Simply set the appropriate
1374-
# variables and trust all will be well.
1375-
1376-
MBEDTLS_CPPFLAGS="-I\${abs_top_srcdir}/third_party/mbedtls/repo/include"
1377-
MBEDTLS_LDFLAGS="-L${ac_pwd}/third_party/mbedtls"
1378-
MBEDTLS_LIBS="-lmbedtls"
1379-
],
1380-
[
1381-
# Check for required mbedTLS headers.
1382-
1383-
AC_CHECK_HEADERS([mbedtls/sha1.h] [mbedtls/sha256.h] [mbedtls/sha512.h] [mbedtls/md.h] [mbedtls/hkdf.h] [mbedtls/pkcs5.h] [mbedtls/chachapoly.h] [mbedtls/aes.h] [mbedtls/ecdh.h] [mbedtls/bignum.h],
1436+
if test "${with_crypto}" = "mbedtls"; then
1437+
NL_WITH_REQUIRED_INTERNAL_PACKAGE(
1438+
[mbedTLS],
1439+
[MBEDTLS],
1440+
[mbedtls],
1441+
[],
1442+
[
1443+
# At this point, the internal mbedTLS package will be neither
1444+
# configured nor built, so the normal checks we undertake for an
1445+
# external package cannot be run here. Simply set the appropriate
1446+
# variables and trust all will be well.
13841447
1385-
[],
1386-
[
1387-
AC_MSG_ERROR(The mbedTLS header "$ac_header" is required but cannot be found.)
1388-
]
1389-
)
1390-
]
1391-
)
1448+
MBEDTLS_CPPFLAGS="-I\${abs_top_srcdir}/third_party/mbedtls/repo/include"
1449+
MBEDTLS_LDFLAGS="-L${ac_pwd}/third_party/mbedtls"
1450+
MBEDTLS_LIBS="-lmbedtls"
1451+
],
1452+
[
1453+
# Check for required mbedTLS headers.
1454+
AC_CHECK_HEADERS([mbedtls/sha1.h] [mbedtls/sha256.h] [mbedtls/sha512.h] [mbedtls/md.h] [mbedtls/hkdf.h] [mbedtls/pkcs5.h] [mbedtls/chachapoly.h] [mbedtls/aes.h] [mbedtls/ecdh.h] [mbedtls/bignum.h],
1455+
[],
1456+
[
1457+
AC_MSG_ERROR(The mbedTLS header "$ac_header" is required but cannot be found.)
1458+
]
1459+
)
1460+
]
1461+
)
1462+
fi
13921463

13931464
# Depending on whether mbedTLS has been configured for an internal
13941465
# location, its directory stem within this package needs to be set
@@ -1413,6 +1484,14 @@ fi
14131484
AC_SUBST(MBEDTLS_SUBDIRS, [${maybe_mbedtls_dirstem}])
14141485
AM_CONDITIONAL([CHIP_WITH_MBEDTLS_INTERNAL], [test "${nl_with_mbedtls}" = "internal"])
14151486

1487+
AM_CONDITIONAL([CHIP_WITH_MBEDTLS], [test "${nl_with_mbedtls}" != "no"])
1488+
1489+
if test "${nl_with_mbedtls}" = "no"; then
1490+
AC_DEFINE([CHIP_WITH_MBEDTLS], [0], [Define to 1 to build CHIP with mbedTLS])
1491+
else
1492+
AC_DEFINE([CHIP_WITH_MBEDTLS], [1], [Define to 1 to build CHIP with mbedTLS])
1493+
fi
1494+
14161495
#
14171496
# LwIP
14181497
#
@@ -1843,18 +1922,19 @@ if test "${nl_cv_build_tests}" = "yes"; then
18431922
fi
18441923
fi
18451924

1925+
# Add any crypto-implementation CPPFLAGS, LDFLAGS, and LIBS
18461926

1847-
# Add any OpenSSL CPPFLAGS, LDFLAGS, and LIBS
1848-
1849-
CPPFLAGS="${CPPFLAGS} ${OPENSSL_CPPFLAGS}"
1850-
LDFLAGS="${LDFLAGS} ${OPENSSL_LDFLAGS}"
1851-
LIBS="${LIBS} ${OPENSSL_LIBS}"
1927+
CRYPTO_CPPFLAGS="${CRYPTO_CPPFLAGS} ${MBEDTLS_CPPFLAGS} ${OPENSSL_CPPFLAGS}"
1928+
CRYPTO_LDFLAGS="${CRYPTO_LDFLAGS} ${MBEDTLS_LDFLAGS} ${OPENSSL_LDFLAGS}"
1929+
CRYPTO_LIBS="${CRYPTO_LIBS} ${MBEDTLS_LIBS} ${OPENSSL_LIBS}"
18521930

1853-
# Add any mbedTLS CPPFLAGS, LDFLAGS, and LIBS
1931+
AC_SUBST(CRYPTO_CPPFLAGS)
1932+
AC_SUBST(CRYPTO_LDFLAGS)
1933+
AC_SUBST(CRYPTO_LIBS)
18541934

1855-
CPPFLAGS="${CPPFLAGS} ${MBEDTLS_CPPFLAGS}"
1856-
LDFLAGS="${LDFLAGS} ${MBEDTLS_LDFLAGS}"
1857-
LIBS="${LIBS} ${MBEDTLS_LIBS}"
1935+
CPPFLAGS="${CPPFLAGS} ${CRYPTO_CPPFLAGS}"
1936+
LDFLAGS="${LDFLAGS} ${CRYPTO_LDFLAGS}"
1937+
LIBS="${LIBS} ${CRYPTO_LIBS}"
18581938

18591939
# Add any code coverage CPPFLAGS, LDFLAGS, and LIBS
18601940

@@ -1964,6 +2044,7 @@ AC_MSG_NOTICE([
19642044
Target architecture : ${target_cpu}
19652045
Target OS : ${target_os}
19662046
Target style : ${CHIP_TARGET_STYLE}
2047+
Cryptographic implementation : ${CHIP_CRYPTO}
19672048
Target network layer : ${with_network_layer}
19682049
Target network system(s) : ${CONFIG_TARGET_NETWORKS}
19692050
IPv4 enabled : ${enable_ipv4}
@@ -2012,14 +2093,17 @@ AC_MSG_NOTICE([
20122093
LwIP compile flags : ${LWIP_CPPFLAGS:--}
20132094
LwIP link flags : ${LWIP_LDFLAGS:--}
20142095
LwIP link libraries : ${LWIP_LIBS:--}
2015-
OpenSSL source : ${nl_with_openssl}
2016-
OpenSSL compile flags : ${OPENSSL_CPPFLAGS:--}
2017-
OpenSSL link flags : ${OPENSSL_LDFLAGS:--}
2018-
OpenSSL link libraries : ${OPENSSL_LIBS:--}
20192096
mbedTLS source : ${nl_with_mbedtls}
20202097
mbedTLS compile flags : ${MBEDTLS_CPPFLAGS:--}
20212098
mbedTLS link flags : ${MBEDTLS_LDFLAGS:--}
20222099
mbedTLS link libraries : ${MBEDTLS_LIBS:--}
2100+
OpenSSL source : ${nl_with_openssl}
2101+
OpenSSL compile flags : ${OPENSSL_CPPFLAGS:--}
2102+
OpenSSL link flags : ${OPENSSL_LDFLAGS:--}
2103+
OpenSSL link libraries : ${OPENSSL_LIBS:--}
2104+
Crypto implementation compile flags : ${CRYPTO_CPPFLAGS:--}
2105+
Crypto implementation link flags : ${CRYPTO_LDFLAGS:--}
2106+
Crypto implementation link libraries : ${CRYPTO_LIBS:--}
20232107
Nlunit-test source : ${nl_with_nlunit_test:--}
20242108
Nlunit-test compile flags : ${NLUNIT_TEST_CPPFLAGS:--}
20252109
Nlunit-test link flags : ${NLUNIT_TEST_LDFLAGS:--}

src/crypto/Makefile.am

+6-1
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ libChipCrypto_a_CPPFLAGS = \
4040
$(NULL)
4141

4242
libChipCrypto_a_SOURCES = \
43-
CHIPOpenSSL.c \
4443
CHIPBase+Crypto.c \
44+
$(NULL)
45+
46+
if CHIP_CRYPTO_OPENSSL
47+
libChipCrypto_a_SOURCES += \
48+
CHIPOpenSSL.c \
4549
CHIPCryptoPALOpenSSL.cpp \
4650
$(NULL)
51+
endif
4752

4853
dist_libChipCrypto_a_HEADERS = \
4954
CHIPCrypto.h \

0 commit comments

Comments
 (0)