diff --git a/examples/Makefile-nrf52840 b/examples/Makefile-nrf52840 index 3fc029d9b..e78736956 100644 --- a/examples/Makefile-nrf52840 +++ b/examples/Makefile-nrf52840 @@ -124,6 +124,15 @@ ifeq ($(DISABLE_CC310), 1) NRF52840_MBEDTLS_CPPFLAGS += -DDISABLE_CC310=1 endif +ifeq ($(MBEDTLS_THREADING), 1) +NRF52840_MBEDTLS_CPPFLAGS += -DDISABLE_CC310=1 +NRF52840_MBEDTLS_CPPFLAGS += -DMBEDTLS_THREADING=1 +endif + +ifdef MBEDTLS_THREADING_MUTEX_DEF +NRF52840_MBEDTLS_CPPFLAGS += -DMBEDTLS_THREADING_MUTEX_DEF='\"$(MBEDTLS_THREADING_MUTEX_DEF)\"' +endif + ifeq ($(shell expr $(GCCVersion) \>= 7), 1) COMMONCFLAGS += -Wno-expansion-to-defined endif diff --git a/examples/platforms/nrf52840/README.md b/examples/platforms/nrf52840/README.md index 69f048955..c166a8953 100644 --- a/examples/platforms/nrf52840/README.md +++ b/examples/platforms/nrf52840/README.md @@ -121,6 +121,31 @@ By default, mbedTLS library is built with support for CryptoCell 310 hardware ac $ make -f examples/Makefile-nrf52840 DISABLE_CC310=1 ``` +### Optional mbedTLS threading support +By default, mbedTLS library is built without support for multiple threads. You can enable this built-in support by building OpenThread with the following parameter: + +``` +$ make -f examples/Makefile-nrf52840 MBEDTLS_THREADING=1 +``` + +The simple mutex definition is used as shown below: + +``` +typedef void * mbedtls_threading_mutex_t; +``` + +However, you can modify it, by providing a path to a header file with proper definition. To do that, build OpenThread with the following parameter: + +``` +$ make -f examples/Makefile-nrf52840 MBEDTLS_THREADING=1 MBEDTLS_THREADING_MUTEX_DEF="path_to_a_header_file_with_mutex_definition.h" +``` + +See [mbedTls Thread Safety and Multi Threading][mbedtls-thread-safety-and-multi-threading] for more details. + +Note that as a temporary limitation CryptoCell 310 hardware acceleration is disabled when using mbedTLS threading. + +[mbedtls-thread-safety-and-multi-threading]: https://tls.mbed.org/kb/development/thread-safety-and-multi-threading + ### IEEE EUI-64 address When the Thread device is configured to obtain the Thread Network security credentials with either Thread Commissioning or an out-of-band method, the extended MAC address should be constructed out of the globally unique IEEE EUI-64. diff --git a/third_party/NordicSemiconductor/libraries/crypto/nrf52840-mbedtls-config.h b/third_party/NordicSemiconductor/libraries/crypto/nrf52840-mbedtls-config.h index 6ae51322c..490badb1a 100644 --- a/third_party/NordicSemiconductor/libraries/crypto/nrf52840-mbedtls-config.h +++ b/third_party/NordicSemiconductor/libraries/crypto/nrf52840-mbedtls-config.h @@ -32,6 +32,11 @@ #define MBEDTLS_SHA256_ALT #endif // DISABLE_CC310 +#ifdef MBEDTLS_THREADING +#define MBEDTLS_THREADING_C +#define MBEDTLS_THREADING_ALT +#endif // MBEDTLS_THREADING + #if defined(__ICCARM__) _Pragma("diag_suppress=Pe550") #endif diff --git a/third_party/NordicSemiconductor/libraries/crypto/threading_alt.h b/third_party/NordicSemiconductor/libraries/crypto/threading_alt.h new file mode 100644 index 000000000..def50ba4a --- /dev/null +++ b/third_party/NordicSemiconductor/libraries/crypto/threading_alt.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef THREADING_ALT_H_ +#define THREADING_ALT_H_ + +#ifdef MBEDTLS_THREADING_MUTEX_DEF +#include MBEDTLS_THREADING_MUTEX_DEF +#else +typedef void * mbedtls_threading_mutex_t; +#endif // MBEDTLS_THREADING_MUTEX_DEF + +#endif // THREADING_ALT_H_ diff --git a/third_party/mbedtls/Makefile.am b/third_party/mbedtls/Makefile.am index 4b4158749..bd923c303 100644 --- a/third_party/mbedtls/Makefile.am +++ b/third_party/mbedtls/Makefile.am @@ -75,6 +75,7 @@ libmbedcrypto_a_SOURCES = \ repo/library/ssl_srv.c \ repo/library/ssl_ticket.c \ repo/library/ssl_tls.c \ + repo/library/threading.c \ repo/library/x509.c \ repo/library/x509_crt.c \ $(NULL)