Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: tcp: use PSA functions for ISN generation instead of legacy MbedTLS ones #71827

Merged
merged 7 commits into from
May 9, 2024
Merged
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
1 change: 1 addition & 0 deletions modules/mbedtls/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ config MBEDTLS_PROMPTLESS
mbed TLS menu prompt and instead handle the selection of MBEDTLS from
dependent sub-configurations and thus prevent stuck symbol behavior.

rsource "Kconfig.psa"

menuconfig MBEDTLS
bool "mbed TLS Support" if !MBEDTLS_PROMPTLESS
Expand Down
14 changes: 14 additions & 0 deletions modules/mbedtls/Kconfig.psa
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

config MBEDTLS_PSA_CRYPTO_CLIENT
bool
default y
depends on BUILD_WITH_TFM || MBEDTLS_PSA_CRYPTO_C

if MBEDTLS_PSA_CRYPTO_CLIENT

config PSA_WANT_ALG_SHA_256
bool "SHA-256 hash algorithm through PSA"

endif # MBEDTLS_PSA_CRYPTO_CLIENT
30 changes: 28 additions & 2 deletions modules/mbedtls/Kconfig.tls-generic
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,38 @@ config MBEDTLS_SSL_EXTENDED_MASTER_SECRET
which ensures that master secrets are different for every
connection and every session.

choice MBEDTLS_PSA_CRYPTO_RND_SOURCE
prompt "Select random source for built-in PSA crypto"
default MBEDTLS_PSA_CRYPTO_LEGACY_RNG

config MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
bool "Use a cryptographically secure driver as random source"
depends on CSPRNG_ENABLED
help
Use cryptographically secure random generator to provide random data
instead of legacy MbedTLS modules (ENTROPY + CTR_DRBG/HMAC_DRBG).

config MBEDTLS_PSA_CRYPTO_LEGACY_RNG
bool "Use legacy modules to generate random data"
select MBEDTLS_ENTROPY_ENABLED
select MBEDTLS_CTR_DRBG_ENABLED if !MBEDTLS_HMAC_DRBG_ENABLED
help
Use legacy MbedTLS modules (ENTROPY + CTR_DRBG/HMAC_DRBG) as random
source generators.

endchoice

config MBEDTLS_PSA_CRYPTO_C
bool "Platform Security Architecture cryptography API"
depends on MBEDTLS_ENTROPY_ENABLED
depends on MBEDTLS_CTR_DRBG_ENABLED || MBEDTLS_HMAC_DRBG_ENABLED
depends on MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG || MBEDTLS_PSA_CRYPTO_LEGACY_RNG
default y if UOSCORE || UEDHOC

config MBEDTLS_USE_PSA_CRYPTO
bool "Use PSA APIs instead of legacy MbedTLS when possible"
help
Use PSA APIs instead of legacy MbedTLS functions in TLS/DTLS and other
"intermediate" modules such as PK, MD and Cipher.

config MBEDTLS_LMS
bool "Support LMS signature schemes"
depends on MBEDTLS_PSA_CRYPTO_C
Expand Down
18 changes: 16 additions & 2 deletions modules/mbedtls/configs/config-tls-generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,18 @@
#define MBEDTLS_SSL_EXTENDED_MASTER_SECRET
#endif

#if defined(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
#define MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
#endif

#if defined(CONFIG_MBEDTLS_PSA_CRYPTO_C)
#define MBEDTLS_PSA_CRYPTO_C

#if defined(CONFIG_MBEDTLS_USE_PSA_CRYPTO)
#define MBEDTLS_USE_PSA_CRYPTO
#endif

#if defined(CONFIG_ARCH_POSIX)
#if defined(CONFIG_ARCH_POSIX) && !defined(CONFIG_PICOLIBC)
#define MBEDTLS_PSA_KEY_SLOT_COUNT 64
#define MBEDTLS_PSA_CRYPTO_STORAGE_C
#define MBEDTLS_PSA_ITS_FILE_C
Expand All @@ -496,8 +503,15 @@
#endif

#if defined(CONFIG_BUILD_WITH_TFM)
#define MBEDTLS_PSA_CRYPTO_CLIENT
#undef MBEDTLS_PSA_CRYPTO_C
#endif /* CONFIG_BUILD_WITH_TFM */

#if defined(CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT)
#define MBEDTLS_PSA_CRYPTO_CLIENT
#endif

#if defined(CONFIG_PSA_WANT_ALG_SHA_256)
#define PSA_WANT_ALG_SHA_256 1
#endif

#endif /* MBEDTLS_CONFIG_H */
23 changes: 23 additions & 0 deletions modules/mbedtls/zephyr_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,26 @@ mbedtls_ms_time_t mbedtls_ms_time(void)
{
return (mbedtls_ms_time_t)k_uptime_get();
}

#if defined(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
/* MBEDTLS_PSA_CRYPTO_C requires a random generator to work and this can
* be achieved through either legacy MbedTLS modules
* (ENTROPY + CTR_DRBG/HMAC_DRBG) or provided externally by enabling the
* CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG. In the latter case the following
* callback functions needs to be defined.
*/
psa_status_t mbedtls_psa_external_get_random(
mbedtls_psa_external_random_context_t *context,
uint8_t *output, size_t output_size, size_t *output_length)
{
(void) context;

if (sys_csrand_get(output, output_size) != 0) {
return PSA_ERROR_GENERIC_ERROR;
}

*output_length = output_size;

return PSA_SUCCESS;
}
#endif
1 change: 1 addition & 0 deletions samples/tfm_integration/psa_crypto/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ CONFIG_MBEDTLS_USER_CONFIG_ENABLE=y
CONFIG_MBEDTLS_USER_CONFIG_FILE="user-tls-conf.h"

CONFIG_MBEDTLS_PSA_CRYPTO_C=y
CONFIG_MBEDTLS_USE_PSA_CRYPTO=y
CONFIG_MBEDTLS_ENTROPY_ENABLED=y
CONFIG_MBEDTLS_ECP_C=y
CONFIG_MBEDTLS_ECP_DP_SECP256R1_ENABLED=y
Expand Down
1 change: 1 addition & 0 deletions subsys/bluetooth/mesh/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,7 @@ config BT_MESH_USES_MBEDTLS_PSA
select MBEDTLS
select MBEDTLS_ZEPHYR_ENTROPY
select MBEDTLS_PSA_CRYPTO_C
select MBEDTLS_USE_PSA_CRYPTO
select MBEDTLS_MAC_CMAC_ENABLED
select MBEDTLS_CIPHER_AES_ENABLED
select MBEDTLS_AES_ROM_TABLES
Expand Down
4 changes: 1 addition & 3 deletions subsys/net/ip/Kconfig.tcp
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ config NET_TCP_ISN_RFC6528
bool "Use ISN algorithm from RFC 6528"
default y
depends on NET_TCP
select MBEDTLS
select MBEDTLS_MD
select MBEDTLS_MAC_MD5_ENABLED
depends on PSA_WANT_ALG_SHA_256
help
Implement Initial Sequence Number calculation as described in
RFC 6528 chapter 3. https://tools.ietf.org/html/rfc6528
Expand Down
16 changes: 11 additions & 5 deletions subsys/net/ip/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LOG_MODULE_REGISTER(net_tcp, CONFIG_NET_TCP_LOG_LEVEL);
#include <zephyr/random/random.h>

#if defined(CONFIG_NET_TCP_ISN_RFC6528)
#include <mbedtls/md5.h>
#include <psa/crypto.h>
#endif
#include <zephyr/net/net_pkt.h>
#include <zephyr/net/net_context.h>
Expand Down Expand Up @@ -2291,14 +2291,17 @@ static uint32_t tcpv6_init_isn(struct in6_addr *saddr,
static bool once;

if (!once) {
sys_rand_get(unique_key, sizeof(unique_key));
sys_csrand_get(unique_key, sizeof(unique_key));
once = true;
}

memcpy(buf.key, unique_key, sizeof(buf.key));

#if defined(CONFIG_NET_TCP_ISN_RFC6528)
mbedtls_md5((const unsigned char *)&buf, sizeof(buf), hash);
size_t hash_len;

psa_hash_compute(PSA_ALG_SHA_256, (const unsigned char *)&buf, sizeof(buf),
hash, sizeof(hash), &hash_len);
#endif

return seq_scale(UNALIGNED_GET((uint32_t *)&hash[0]));
Expand Down Expand Up @@ -2326,14 +2329,17 @@ static uint32_t tcpv4_init_isn(struct in_addr *saddr,
static bool once;

if (!once) {
sys_rand_get(unique_key, sizeof(unique_key));
sys_csrand_get(unique_key, sizeof(unique_key));
once = true;
}

memcpy(buf.key, unique_key, sizeof(unique_key));

#if defined(CONFIG_NET_TCP_ISN_RFC6528)
mbedtls_md5((const unsigned char *)&buf, sizeof(buf), hash);
size_t hash_len;

psa_hash_compute(PSA_ALG_SHA_256, (const unsigned char *)&buf, sizeof(buf),
hash, sizeof(hash), &hash_len);
#endif

return seq_scale(UNALIGNED_GET((uint32_t *)&hash[0]));
Expand Down
4 changes: 4 additions & 0 deletions tests/crypto/mbedtls/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ CONFIG_MBEDTLS_BUILTIN=y
CONFIG_MBEDTLS_TEST=y
CONFIG_ZTEST=y
CONFIG_TEST_USERSPACE=y
CONFIG_MINIMAL_LIBC=y
CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS=y
CONFIG_MINIMAL_LIBC_RAND=y
CONFIG_ENTROPY_GENERATOR=y
CONFIG_MBEDTLS_ZEPHYR_ENTROPY=y
CONFIG_TEST_RANDOM_GENERATOR=y
Loading