Skip to content
Open
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
32 changes: 32 additions & 0 deletions crypto/mbedtls/0004-mbedtls-add-PBKDF2-ALT-guard-to-pkcs5.patch
Original file line number Diff line number Diff line change
@@ -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)

16 changes: 16 additions & 0 deletions crypto/mbedtls/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ if(CONFIG_CRYPTO_MBEDTLS)
list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/source/cmac_alt.c)
endif()

if(CONFIG_MBEDTLS_GCM_ALT)
list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/source/gcm_alt.c)
endif()

if(CONFIG_MBEDTLS_MD5_ALT)
list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/source/md5_alt.c)
endif()
Expand Down Expand Up @@ -118,6 +122,18 @@ 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()

if(CONFIG_MBEDTLS_ECDH_ALT)
list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/source/ecdh_alt.c)
endif()

endif()

# ############################################################################
Expand Down
27 changes: 27 additions & 0 deletions crypto/mbedtls/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,11 @@ config MBEDTLS_CMAC_ALT
select MBEDTLS_ALT
default n

config MBEDTLS_GCM_ALT
bool "Enable Mbedt TLS GCM module alted by nuttx crypto"
select MBEDTLS_ALT
default n

config MBEDTLS_MD5_ALT
bool "Enable Mbedt TLS MD5 module alted by nuttx crypto"
select MBEDTLS_ALT
Expand Down Expand Up @@ -743,6 +748,28 @@ config MBEDTLS_BIGNUM_ALT
select MBEDTLS_ALT
default n

config MBEDTLS_ECDSA_ALT
bool "Enable Mbedt TLS ECDSA module alted by nuttx crypto"
select MBEDTLS_ALT
default n

config MBEDTLS_ECDH_ALT
bool "Enable Mbedt TLS ECDH module alted by nuttx crypto"
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
Expand Down
17 changes: 17 additions & 0 deletions crypto/mbedtls/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -170,6 +171,10 @@ ifeq ($(CONFIG_MBEDTLS_CMAC_ALT),y)
CSRCS += $(APPDIR)/crypto/mbedtls/source/cmac_alt.c
endif

ifeq ($(CONFIG_MBEDTLS_GCM_ALT),y)
CSRCS += $(APPDIR)/crypto/mbedtls/source/gcm_alt.c
endif

ifeq ($(CONFIG_MBEDTLS_MD5_ALT),y)
CSRCS += $(APPDIR)/crypto/mbedtls/source/md5_alt.c
endif
Expand Down Expand Up @@ -198,6 +203,18 @@ 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

ifeq ($(CONFIG_MBEDTLS_ECDH_ALT),y)
CSRCS += $(APPDIR)/crypto/mbedtls/source/ecdh_alt.c
endif

endif

include $(APPDIR)/Application.mk
19 changes: 19 additions & 0 deletions crypto/mbedtls/include/aes_alt.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,23 @@ mbedtls_aes_context;

#define mbedtls_aes_xts_context mbedtls_aes_context

int mbedtls_aes_set128key_enc_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits);
int mbedtls_aes_set192key_enc_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits);
int mbedtls_aes_set256key_enc_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits);
int mbedtls_aes_set128key_dec_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits);
int mbedtls_aes_set192key_dec_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits);
int mbedtls_aes_set256key_dec_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits);

#endif /* __APPS_CRYPTO_MBEDTLS_INCLUDE_AES_ALT_H */
2 changes: 2 additions & 0 deletions crypto/mbedtls/include/dev_alt.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef struct cryptodev_context_s
int fd;
struct session_op session;
struct crypt_op crypt;
struct crypt_kop cryptk;
}
cryptodev_context_t;

Expand All @@ -43,5 +44,6 @@ void cryptodev_free(FAR cryptodev_context_t *ctx);
int cryptodev_get_session(FAR cryptodev_context_t *ctx);
void cryptodev_free_session(FAR cryptodev_context_t *ctx);
int cryptodev_crypt(FAR cryptodev_context_t *ctx);
int cryptodev_cryptk(FAR cryptodev_context_t *ctx);

#endif /* __APPS_CRYPTO_MBEDTLS_INCLUDE_DEV_ALT_H */
56 changes: 56 additions & 0 deletions crypto/mbedtls/include/gcm_alt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/****************************************************************************
* apps/crypto/mbedtls/include/gcm_alt.h
*
* 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.
****************************************************************************/

#ifndef __APPS_CRYPTO_MBEDTLS_INCLUDE_GCM_ALT_H
#define __APPS_CRYPTO_MBEDTLS_INCLUDE_GCM_ALT_H

/****************************************************************************
* Included Files
****************************************************************************/

#include <stdint.h>

#include "dev_alt.h"

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define GCM_KEY_MAX_SIZE 32
#define GCM_IV_MAX_SIZE 128

/****************************************************************************
* Public Types
****************************************************************************/

typedef struct mbedtls_gcm_context
{
cryptodev_context_t dev;
unsigned char key[GCM_KEY_MAX_SIZE];
unsigned char iv[GCM_IV_MAX_SIZE];
unsigned int keybits;
unsigned int ivlen;
uint64_t aadlen;
uint64_t len;
}
mbedtls_gcm_context;

#endif /* __APPS_CRYPTO_MBEDTLS_INCLUDE_GCM_ALT_H */
15 changes: 15 additions & 0 deletions crypto/mbedtls/include/mbedtls/mbedtls_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@
#ifdef CONFIG_MBEDTLS_CMAC_ALT
#define MBEDTLS_CMAC_ALT
#endif
#ifdef CONFIG_MBEDTLS_GCM_ALT
#define MBEDTLS_GCM_ALT
#endif
/* #define MBEDTLS_DES_ALT
* #define MBEDTLS_DHM_ALT
* #define MBEDTLS_ECJPAKE_ALT
Expand Down Expand Up @@ -479,6 +482,18 @@
#ifdef CONFIG_MBEDTLS_BIGNUM_ALT
#define MBEDTLS_BIGNUM_ALT
#endif
#ifdef CONFIG_MBEDTLS_ECDSA_ALT
#define MBEDTLS_ECDSA_VERIFY_ALT
#define MBEDTLS_ECDSA_SIGN_ALT
#define MBEDTLS_ECDSA_GENKEY_ALT
#endif
#ifdef CONFIG_MBEDTLS_ECDH_ALT
#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
*/

Expand Down
78 changes: 78 additions & 0 deletions crypto/mbedtls/source/aes_alt.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,41 @@
#define ECB_BLOCK_SIZE 16
#define NONCE_LENGTH 4

/****************************************************************************
* Private Functions
****************************************************************************/

/* Set the session key by key id instead of key material: the key
* buffer carries the 32-bit key id in its first bytes, padded to the
* full key size (the kernel reads the id from the first four bytes;
* the size only decides which AES variant the session uses). The
* key itself must already live in a key store the backend can reach,
* e.g. one allocated through CIOCKEY CRK_ALLOCATE_KEY.
*/

static int aes_setkey_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits)
{
switch (keybits)
{
case 128:
break;
case 192:
break;
case 256:
break;
default:
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
}

memcpy(ctx->key, key, keybits / 8);
ctx->dev.session.key = (caddr_t)ctx->key;
ctx->dev.session.keylen = keybits / 8;
ctx->dev.session.flags |= SOP_F_KEYID;
return 0;
}

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -68,6 +103,7 @@ int mbedtls_aes_setkey_enc(FAR mbedtls_aes_context *ctx,
memcpy(ctx->key, key, keybits / 8);
ctx->dev.session.key = (caddr_t)ctx->key;
ctx->dev.session.keylen = keybits / 8;
ctx->dev.session.flags &= ~SOP_F_KEYID;
return 0;
}

Expand All @@ -78,6 +114,48 @@ int mbedtls_aes_setkey_dec(FAR mbedtls_aes_context *ctx,
return mbedtls_aes_setkey_enc(ctx, key, keybits);
}

int mbedtls_aes_set128key_enc_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits)
{
return aes_setkey_keyid(ctx, key, keybits);
}

int mbedtls_aes_set192key_enc_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits)
{
return aes_setkey_keyid(ctx, key, keybits);
}

int mbedtls_aes_set256key_enc_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits)
{
return aes_setkey_keyid(ctx, key, keybits);
}

int mbedtls_aes_set128key_dec_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits)
{
return aes_setkey_keyid(ctx, key, keybits);
}

int mbedtls_aes_set192key_dec_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits)
{
return aes_setkey_keyid(ctx, key, keybits);
}

int mbedtls_aes_set256key_dec_keyid(FAR mbedtls_aes_context *ctx,
FAR const unsigned char *key,
unsigned int keybits)
{
return aes_setkey_keyid(ctx, key, keybits);
}

/* AES-ECB block encryption/decryption */

int mbedtls_aes_crypt_ecb(FAR mbedtls_aes_context *ctx,
Expand Down
Loading
Loading