This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit afcc07d3d5912284074ebc65171d5c78dae027fa Author: makejian <[email protected]> AuthorDate: Wed Nov 25 14:01:45 2026 +0800 crypto/mbedtls: add PKCS#5 PBKDF2 alt implementation Route mbedtls_pkcs5_pbkdf2_hmac[_ext] to /dev/crypto: the salt is submitted as the request payload, the password is the HMAC session key, the iteration count goes through crypt.iterations and the derived key length through crypt.olen. Only PBKDF2-HMAC-SHA1 and PBKDF2-HMAC-SHA256 are supported by the engine; every other digest returns FEATURE_UNAVAILABLE so the caller can fall back to software. mbed TLS 3.6 has no MBEDTLS_PKCS5_ALT guard in library/pkcs5.c, so the tree carries a small patch (0004) wrapping the software PBKDF2 path in the usual "#if !defined(MBEDTLS_xxx_ALT)" convention, next to the existing 0001-0003 carry-in patches. Verified on sim:crypto + software cryptodev with the alt enabled: the RFC 6070 HMAC-SHA1 vectors (1/2/4096 iterations) and two HMAC-SHA256 vectors match through mbedtls_pkcs5_pbkdf2_hmac_ext(). Signed-off-by: makejian <[email protected]> --- ...004-mbedtls-add-PBKDF2-ALT-guard-to-pkcs5.patch | 32 ++++ crypto/mbedtls/CMakeLists.txt | 4 + crypto/mbedtls/Kconfig | 12 ++ crypto/mbedtls/Makefile | 5 + crypto/mbedtls/include/mbedtls/mbedtls_config.h | 3 + crypto/mbedtls/source/pkcs5_alt.c | 162 +++++++++++++++++++++ 6 files changed, 218 insertions(+) diff --git a/crypto/mbedtls/0004-mbedtls-add-PBKDF2-ALT-guard-to-pkcs5.patch b/crypto/mbedtls/0004-mbedtls-add-PBKDF2-ALT-guard-to-pkcs5.patch new file mode 100644 index 000000000..79675c5af --- /dev/null +++ b/crypto/mbedtls/0004-mbedtls-add-PBKDF2-ALT-guard-to-pkcs5.patch @@ -0,0 +1,32 @@ +mbed TLS patch: guard the PBKDF2 implementation with MBEDTLS_PKCS5_ALT. + +mbed TLS 3.6 has no MBEDTLS_PKCS5_ALT module guard in library/pkcs5.c, +so an alternative implementation of mbedtls_pkcs5_pbkdf2_hmac[_ext] +cannot replace the built-in one: both would be compiled and the link +would see duplicate symbols. Wrap the software PBKDF2 path in the +usual "#if !defined(MBEDTLS_xxx_ALT)" guard, the same convention the +library already uses for MBEDTLS_GCM_ALT or MBEDTLS_ECDSA_SIGN_ALT. +Parsing (mbedtls_pkcs5_pbes2 etc.) stays in the library; only the key +derivation is replaceable. + +Upstream-Status: Inappropriate [NuttX carries this patch so the +cryptodev-backed pkcs5_alt.c can provide the PBKDF2 functions] + +--- a/library/pkcs5.c ++++ b/library/pkcs5.c +@@ -265,6 +265,7 @@ + } + #endif /* MBEDTLS_ASN1_PARSE_C && MBEDTLS_CIPHER_C */ + ++#if !defined(MBEDTLS_PKCS5_ALT) + static int pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx, + const unsigned char *password, + size_t plen, const unsigned char *salt, size_t slen, +@@ -392,6 +393,7 @@ + mbedtls_md_free(&md_ctx); + return ret; + } ++#endif /* !MBEDTLS_PKCS5_ALT */ + + #if defined(MBEDTLS_SELF_TEST) + diff --git a/crypto/mbedtls/CMakeLists.txt b/crypto/mbedtls/CMakeLists.txt index 2d72cd898..f82a67fe5 100644 --- a/crypto/mbedtls/CMakeLists.txt +++ b/crypto/mbedtls/CMakeLists.txt @@ -122,6 +122,10 @@ if(CONFIG_CRYPTO_MBEDTLS) list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/source/bignum_alt.c) endif() + if(CONFIG_MBEDTLS_PKCS5_ALT) + list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/source/pkcs5_alt.c) + endif() + if(CONFIG_MBEDTLS_ECDSA_ALT) list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/source/ecdsa_alt.c) endif() diff --git a/crypto/mbedtls/Kconfig b/crypto/mbedtls/Kconfig index 842f236bd..db1f1fbb3 100644 --- a/crypto/mbedtls/Kconfig +++ b/crypto/mbedtls/Kconfig @@ -767,6 +767,18 @@ config MBEDTLS_ECDH_ALT select MBEDTLS_ALT default n +config MBEDTLS_PKCS5_ALT + bool "Enable Mbed TLS PKCS#5 PBKDF2 alted by nuttx crypto" + depends on MBEDTLS_PKCS5_C + select MBEDTLS_ALT + default n + ---help--- + Route mbedtls_pkcs5_pbkdf2_hmac[_ext] to the NuttX crypto + device (/dev/crypto) so PBKDF2-HMAC-SHA1/SHA256 key derivation + runs on the hardware crypto engine. Only SHA1 and SHA256 are + supported; other digests return + MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE. + endif # CRYPTO_CRYPTODEV config MBEDTLS_HAVE_TIME diff --git a/crypto/mbedtls/Makefile b/crypto/mbedtls/Makefile index 7b3fc7f75..238536590 100644 --- a/crypto/mbedtls/Makefile +++ b/crypto/mbedtls/Makefile @@ -83,6 +83,7 @@ $(MBEDTLS_UNPACKNAME): $(MBEDTLS_ZIP) $(Q) patch -p1 -d $(MBEDTLS_UNPACKNAME) < 0001-mbedtls-entropy_poll-use-getrandom-to-get-the-system.patch $(Q) patch -p1 -d $(MBEDTLS_UNPACKNAME) < 0002-mbedtls-add-mbedtls-x509-crt-pool.patch $(Q) patch -p1 -d $(MBEDTLS_UNPACKNAME) < 0003-Fix-MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT-warning.patch + $(Q) patch -p1 -d $(MBEDTLS_UNPACKNAME) < 0004-mbedtls-add-PBKDF2-ALT-guard-to-pkcs5.patch $(Q) touch $(MBEDTLS_UNPACKNAME) $(MBEDTLS_FRAMEWORK_UNPACKNAME): $(MBEDTLS_FRAMEWORK_ZIP) @@ -202,6 +203,10 @@ ifeq ($(CONFIG_MBEDTLS_BIGNUM_ALT),y) CSRCS += $(APPDIR)/crypto/mbedtls/source/bignum_alt.c endif +ifeq ($(CONFIG_MBEDTLS_PKCS5_ALT),y) +CSRCS += $(APPDIR)/crypto/mbedtls/source/pkcs5_alt.c +endif + ifeq ($(CONFIG_MBEDTLS_ECDSA_ALT),y) CSRCS += $(APPDIR)/crypto/mbedtls/source/ecdsa_alt.c endif diff --git a/crypto/mbedtls/include/mbedtls/mbedtls_config.h b/crypto/mbedtls/include/mbedtls/mbedtls_config.h index 2d061182f..b351000c9 100644 --- a/crypto/mbedtls/include/mbedtls/mbedtls_config.h +++ b/crypto/mbedtls/include/mbedtls/mbedtls_config.h @@ -491,6 +491,9 @@ #define MBEDTLS_ECDH_GEN_PUBLIC_ALT #define MBEDTLS_ECDH_COMPUTE_SHARED_ALT #endif +#ifdef CONFIG_MBEDTLS_PKCS5_ALT +#define MBEDTLS_PKCS5_ALT +#endif /* #define MBEDTLS_XTEA_ALT */ diff --git a/crypto/mbedtls/source/pkcs5_alt.c b/crypto/mbedtls/source/pkcs5_alt.c new file mode 100644 index 000000000..dc654f6da --- /dev/null +++ b/crypto/mbedtls/source/pkcs5_alt.c @@ -0,0 +1,162 @@ +/**************************************************************************** + * apps/crypto/mbedtls/source/pkcs5_alt.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "mbedtls/pkcs5.h" +#include "mbedtls/md.h" +#include "mbedtls/error.h" + +#include <string.h> + +#include "dev_alt.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: pkcs5_alt_mac_from_md + * + * Description: + * Map an mbedtls digest type to the matching cryptodev PBKDF2 MAC + * algorithm. Only SHA1 and SHA256 are backed by the hardware engine; + * every other digest is reported as unsupported. + * + ****************************************************************************/ + +static int pkcs5_alt_mac_from_md(mbedtls_md_type_t md_alg, FAR uint32_t *mac) +{ + switch (md_alg) + { + case MBEDTLS_MD_SHA1: + *mac = CRYPTO_PBKDF2_HMAC_SHA1; + return 0; + case MBEDTLS_MD_SHA256: + *mac = CRYPTO_PBKDF2_HMAC_SHA256; + return 0; + default: + return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE; + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int mbedtls_pkcs5_pbkdf2_hmac_ext(mbedtls_md_type_t md_alg, + FAR const unsigned char *password, + size_t plen, + FAR const unsigned char *salt, size_t slen, + unsigned int iteration_count, + uint32_t key_length, + FAR unsigned char *output) +{ + uint8_t keybuf[HMAC_MAX_BLOCK_LEN]; + cryptodev_context_t ctx; + uint32_t mac; + int ret; + + ret = pkcs5_alt_mac_from_md(md_alg, &mac); + if (ret != 0) + { + return ret; + } + + /* The HMAC key (password) is loaded into a fixed CSS key slot, so it must + * fit the largest key the driver can accept. Longer passwords are not + * supported by this hardware path. + */ + + if (plen > sizeof(keybuf)) + { + return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE; + } + + ret = cryptodev_init(&ctx); + if (ret != 0) + { + return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA; + } + + /* Copy the (possibly const) password into a writable buffer: the kernel + * HMAC session setup XORs the key in place (ipad/opad). + */ + + memcpy(keybuf, password, plen); + + ctx.session.cipher = 0; + ctx.session.mac = mac; + ctx.session.mackey = (caddr_t)keybuf; + ctx.session.mackeylen = plen; + + ret = cryptodev_get_session(&ctx); + if (ret != 0) + { + ret = MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE; + goto errout; + } + + ctx.crypt.ses = ctx.session.ses; + ctx.crypt.op = COP_ENCRYPT; + ctx.crypt.src = (caddr_t)salt; + ctx.crypt.len = slen; + ctx.crypt.mac = (caddr_t)output; + ctx.crypt.olen = key_length; + ctx.crypt.iterations = iteration_count; + + ret = cryptodev_crypt(&ctx); + if (ret != 0) + { + ret = MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA; + } + + cryptodev_free_session(&ctx); + +errout: + memset(keybuf, 0, sizeof(keybuf)); + cryptodev_free(&ctx); + return ret; +} + +#if !defined(MBEDTLS_DEPRECATED_REMOVED) +int mbedtls_pkcs5_pbkdf2_hmac(FAR mbedtls_md_context_t *ctx, + FAR const unsigned char *password, + size_t plen, + FAR const unsigned char *salt, size_t slen, + unsigned int iteration_count, + uint32_t key_length, + FAR unsigned char *output) +{ + mbedtls_md_type_t md_alg; + + if (ctx == NULL || ctx->MBEDTLS_PRIVATE(md_info) == NULL) + { + return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA; + } + + md_alg = mbedtls_md_get_type(ctx->MBEDTLS_PRIVATE(md_info)); + return mbedtls_pkcs5_pbkdf2_hmac_ext(md_alg, password, plen, salt, slen, + iteration_count, key_length, output); +} +#endif /* !MBEDTLS_DEPRECATED_REMOVED */
