Skip to content

Commit a8e07e0

Browse files
committed
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 <makejian@xiaomi.com>
1 parent 5feccef commit a8e07e0

6 files changed

Lines changed: 218 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
mbed TLS patch: guard the PBKDF2 implementation with MBEDTLS_PKCS5_ALT.
2+
3+
mbed TLS 3.6 has no MBEDTLS_PKCS5_ALT module guard in library/pkcs5.c,
4+
so an alternative implementation of mbedtls_pkcs5_pbkdf2_hmac[_ext]
5+
cannot replace the built-in one: both would be compiled and the link
6+
would see duplicate symbols. Wrap the software PBKDF2 path in the
7+
usual "#if !defined(MBEDTLS_xxx_ALT)" guard, the same convention the
8+
library already uses for MBEDTLS_GCM_ALT or MBEDTLS_ECDSA_SIGN_ALT.
9+
Parsing (mbedtls_pkcs5_pbes2 etc.) stays in the library; only the key
10+
derivation is replaceable.
11+
12+
Upstream-Status: Inappropriate [NuttX carries this patch so the
13+
cryptodev-backed pkcs5_alt.c can provide the PBKDF2 functions]
14+
15+
--- a/library/pkcs5.c
16+
+++ b/library/pkcs5.c
17+
@@ -265,6 +265,7 @@
18+
}
19+
#endif /* MBEDTLS_ASN1_PARSE_C && MBEDTLS_CIPHER_C */
20+
21+
+#if !defined(MBEDTLS_PKCS5_ALT)
22+
static int pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx,
23+
const unsigned char *password,
24+
size_t plen, const unsigned char *salt, size_t slen,
25+
@@ -392,6 +393,7 @@
26+
mbedtls_md_free(&md_ctx);
27+
return ret;
28+
}
29+
+#endif /* !MBEDTLS_PKCS5_ALT */
30+
31+
#if defined(MBEDTLS_SELF_TEST)
32+

‎crypto/mbedtls/CMakeLists.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ if(CONFIG_CRYPTO_MBEDTLS)
122122
list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/source/bignum_alt.c)
123123
endif()
124124

125+
if(CONFIG_MBEDTLS_PKCS5_ALT)
126+
list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/source/pkcs5_alt.c)
127+
endif()
128+
125129
if(CONFIG_MBEDTLS_ECDSA_ALT)
126130
list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/source/ecdsa_alt.c)
127131
endif()

‎crypto/mbedtls/Kconfig‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,18 @@ config MBEDTLS_ECDH_ALT
758758
select MBEDTLS_ALT
759759
default n
760760

761+
config MBEDTLS_PKCS5_ALT
762+
bool "Enable Mbed TLS PKCS#5 PBKDF2 alted by nuttx crypto"
763+
depends on MBEDTLS_PKCS5_C
764+
select MBEDTLS_ALT
765+
default n
766+
---help---
767+
Route mbedtls_pkcs5_pbkdf2_hmac[_ext] to the NuttX crypto
768+
device (/dev/crypto) so PBKDF2-HMAC-SHA1/SHA256 key derivation
769+
runs on the hardware crypto engine. Only SHA1 and SHA256 are
770+
supported; other digests return
771+
MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE.
772+
761773
endif # CRYPTO_CRYPTODEV
762774

763775
config MBEDTLS_HAVE_TIME

‎crypto/mbedtls/Makefile‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ $(MBEDTLS_UNPACKNAME): $(MBEDTLS_ZIP)
8383
$(Q) patch -p1 -d $(MBEDTLS_UNPACKNAME) < 0001-mbedtls-entropy_poll-use-getrandom-to-get-the-system.patch
8484
$(Q) patch -p1 -d $(MBEDTLS_UNPACKNAME) < 0002-mbedtls-add-mbedtls-x509-crt-pool.patch
8585
$(Q) patch -p1 -d $(MBEDTLS_UNPACKNAME) < 0003-Fix-MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT-warning.patch
86+
$(Q) patch -p1 -d $(MBEDTLS_UNPACKNAME) < 0004-mbedtls-add-PBKDF2-ALT-guard-to-pkcs5.patch
8687
$(Q) touch $(MBEDTLS_UNPACKNAME)
8788

8889
$(MBEDTLS_FRAMEWORK_UNPACKNAME): $(MBEDTLS_FRAMEWORK_ZIP)
@@ -202,6 +203,10 @@ ifeq ($(CONFIG_MBEDTLS_BIGNUM_ALT),y)
202203
CSRCS += $(APPDIR)/crypto/mbedtls/source/bignum_alt.c
203204
endif
204205

206+
ifeq ($(CONFIG_MBEDTLS_PKCS5_ALT),y)
207+
CSRCS += $(APPDIR)/crypto/mbedtls/source/pkcs5_alt.c
208+
endif
209+
205210
ifeq ($(CONFIG_MBEDTLS_ECDSA_ALT),y)
206211
CSRCS += $(APPDIR)/crypto/mbedtls/source/ecdsa_alt.c
207212
endif

‎crypto/mbedtls/include/mbedtls/mbedtls_config.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@
491491
#define MBEDTLS_ECDH_GEN_PUBLIC_ALT
492492
#define MBEDTLS_ECDH_COMPUTE_SHARED_ALT
493493
#endif
494+
#ifdef CONFIG_MBEDTLS_PKCS5_ALT
495+
#define MBEDTLS_PKCS5_ALT
496+
#endif
494497
/* #define MBEDTLS_XTEA_ALT
495498
*/
496499

‎crypto/mbedtls/source/pkcs5_alt.c‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/****************************************************************************
2+
* apps/crypto/mbedtls/source/pkcs5_alt.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
****************************************************************************/
21+
22+
/****************************************************************************
23+
* Included Files
24+
****************************************************************************/
25+
26+
#include "mbedtls/pkcs5.h"
27+
#include "mbedtls/md.h"
28+
#include "mbedtls/error.h"
29+
30+
#include <string.h>
31+
32+
#include "dev_alt.h"
33+
34+
/****************************************************************************
35+
* Private Functions
36+
****************************************************************************/
37+
38+
/****************************************************************************
39+
* Name: pkcs5_alt_mac_from_md
40+
*
41+
* Description:
42+
* Map an mbedtls digest type to the matching cryptodev PBKDF2 MAC
43+
* algorithm. Only SHA1 and SHA256 are backed by the hardware engine;
44+
* every other digest is reported as unsupported.
45+
*
46+
****************************************************************************/
47+
48+
static int pkcs5_alt_mac_from_md(mbedtls_md_type_t md_alg, FAR uint32_t *mac)
49+
{
50+
switch (md_alg)
51+
{
52+
case MBEDTLS_MD_SHA1:
53+
*mac = CRYPTO_PBKDF2_HMAC_SHA1;
54+
return 0;
55+
case MBEDTLS_MD_SHA256:
56+
*mac = CRYPTO_PBKDF2_HMAC_SHA256;
57+
return 0;
58+
default:
59+
return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
60+
}
61+
}
62+
63+
/****************************************************************************
64+
* Public Functions
65+
****************************************************************************/
66+
67+
int mbedtls_pkcs5_pbkdf2_hmac_ext(mbedtls_md_type_t md_alg,
68+
FAR const unsigned char *password,
69+
size_t plen,
70+
FAR const unsigned char *salt, size_t slen,
71+
unsigned int iteration_count,
72+
uint32_t key_length,
73+
FAR unsigned char *output)
74+
{
75+
uint8_t keybuf[HMAC_MAX_BLOCK_LEN];
76+
cryptodev_context_t ctx;
77+
uint32_t mac;
78+
int ret;
79+
80+
ret = pkcs5_alt_mac_from_md(md_alg, &mac);
81+
if (ret != 0)
82+
{
83+
return ret;
84+
}
85+
86+
/* The HMAC key (password) is loaded into a fixed CSS key slot, so it must
87+
* fit the largest key the driver can accept. Longer passwords are not
88+
* supported by this hardware path.
89+
*/
90+
91+
if (plen > sizeof(keybuf))
92+
{
93+
return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
94+
}
95+
96+
ret = cryptodev_init(&ctx);
97+
if (ret != 0)
98+
{
99+
return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
100+
}
101+
102+
/* Copy the (possibly const) password into a writable buffer: the kernel
103+
* HMAC session setup XORs the key in place (ipad/opad).
104+
*/
105+
106+
memcpy(keybuf, password, plen);
107+
108+
ctx.session.cipher = 0;
109+
ctx.session.mac = mac;
110+
ctx.session.mackey = (caddr_t)keybuf;
111+
ctx.session.mackeylen = plen;
112+
113+
ret = cryptodev_get_session(&ctx);
114+
if (ret != 0)
115+
{
116+
ret = MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
117+
goto errout;
118+
}
119+
120+
ctx.crypt.ses = ctx.session.ses;
121+
ctx.crypt.op = COP_ENCRYPT;
122+
ctx.crypt.src = (caddr_t)salt;
123+
ctx.crypt.len = slen;
124+
ctx.crypt.mac = (caddr_t)output;
125+
ctx.crypt.olen = key_length;
126+
ctx.crypt.iterations = iteration_count;
127+
128+
ret = cryptodev_crypt(&ctx);
129+
if (ret != 0)
130+
{
131+
ret = MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
132+
}
133+
134+
cryptodev_free_session(&ctx);
135+
136+
errout:
137+
memset(keybuf, 0, sizeof(keybuf));
138+
cryptodev_free(&ctx);
139+
return ret;
140+
}
141+
142+
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
143+
int mbedtls_pkcs5_pbkdf2_hmac(FAR mbedtls_md_context_t *ctx,
144+
FAR const unsigned char *password,
145+
size_t plen,
146+
FAR const unsigned char *salt, size_t slen,
147+
unsigned int iteration_count,
148+
uint32_t key_length,
149+
FAR unsigned char *output)
150+
{
151+
mbedtls_md_type_t md_alg;
152+
153+
if (ctx == NULL || ctx->MBEDTLS_PRIVATE(md_info) == NULL)
154+
{
155+
return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
156+
}
157+
158+
md_alg = mbedtls_md_get_type(ctx->MBEDTLS_PRIVATE(md_info));
159+
return mbedtls_pkcs5_pbkdf2_hmac_ext(md_alg, password, plen, salt, slen,
160+
iteration_count, key_length, output);
161+
}
162+
#endif /* !MBEDTLS_DEPRECATED_REMOVED */

0 commit comments

Comments
 (0)