On Tue, 2 Jul 2024 at 21:30, Raymond Mao <raymond....@linaro.org> wrote:
>
> Add porting layer for public key on top of MbedTLS X509 library.
> Introduce _LEGACY and _MBEDTLS kconfigs for public key legacy and
> MbedTLS implementations respectively.
>
> Signed-off-by: Raymond Mao <raymond....@linaro.org>
> ---
> Changes in v2
> - Move the porting layer to MbedTLS dir.
> Changes in v3
> - None.
> Changes in v4
> - Introduce _LEGACY and _MBEDTLS kconfigs for public key legacy and
>   MbedTLS implementations respectively.
> - Move common functions to helper.
>
>  lib/mbedtls/Kconfig      | 50 ++++++++++++++++++++++++
>  lib/mbedtls/Makefile     |  7 +++-
>  lib/mbedtls/public_key.c | 82 ++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 138 insertions(+), 1 deletion(-)
>  create mode 100644 lib/mbedtls/public_key.c
>
> diff --git a/lib/mbedtls/Kconfig b/lib/mbedtls/Kconfig
> index 0cdf0135667..6f29b0c81a2 100644
> --- a/lib/mbedtls/Kconfig
> +++ b/lib/mbedtls/Kconfig
> @@ -122,9 +122,35 @@ endif # LEGACY_CRYPTO_BASIC
>
>  config LEGACY_CRYPTO_CERT
>         bool "legacy certificate libraries"
> +       select ASYMMETRIC_PUBLIC_KEY_SUBTYPE_LEGACY if \
> +               ASYMMETRIC_PUBLIC_KEY_SUBTYPE
> +       select SPL_ASYMMETRIC_PUBLIC_KEY_SUBTYPE_LEGACY if \
> +               ASYMMETRIC_PUBLIC_KEY_SUBTYPE && SPL
>         help
>           Enable legacy certificate libraries.
>
> +if LEGACY_CRYPTO_CERT
> +
> +config ASYMMETRIC_PUBLIC_KEY_SUBTYPE_LEGACY
> +       bool "Asymmetric public key crypto with legacy certificate library"
> +       depends on LEGACY_CRYPTO_CERT && ASYMMETRIC_PUBLIC_KEY_SUBTYPE
> +       help
> +         This option chooses legacy certificate library for asymmetric public
> +         key crypto algorithm.
> +
> +if SPL
> +
> +config SPL_ASYMMETRIC_PUBLIC_KEY_SUBTYPE_LEGACY

subtype is either legacy or mbedTLS? If yes I think you can get rid of
it and have
SPL_ASYMMETRIC_PUBLIC_KEY_LEGACY and ASYMMETRIC_PUBLIC_KEY_MBEDTLS

> +       bool "Asymmetric public key crypto with legacy certificate library in 
> SPL"
> +       depends on LEGACY_CRYPTO_CERT && SPL_ASYMMETRIC_PUBLIC_KEY_SUBTYPE
> +       help
> +         This option chooses legacy certificate library for asymmetric public
> +         key crypto algorithm in SPL.
> +
> +endif # SPL
> +
> +endif # LEGACY_CRYPTO_CERT
> +
>  endif # LEGACY_CRYPTO
>
>  if MBEDTLS_LIB
> @@ -232,7 +258,31 @@ endif # MBEDTLS_LIB_CRYPTO
>
>  config MBEDTLS_LIB_X509
>         bool "MbedTLS certificate libraries"
> +       select ASYMMETRIC_PUBLIC_KEY_SUBTYPE_MBEDTLS if \
> +               ASYMMETRIC_PUBLIC_KEY_SUBTYPE
> +       select SPL_ASYMMETRIC_PUBLIC_KEY_SUBTYPE_MBEDTLS if \
> +               ASYMMETRIC_PUBLIC_KEY_SUBTYPE && SPL

Why do we have to select SPL here and in the Kconfig above for the
legacy option?

>         help
>           Enable MbedTLS certificate libraries.
>
> +if MBEDTLS_LIB_X509
> +
> +config ASYMMETRIC_PUBLIC_KEY_SUBTYPE_MBEDTLS
> +       bool "Asymmetric public key crypto with MbedTLS certificate library"
> +       help
> +         This option chooses MbedTLS certificate library for asymmetric 
> public
> +         key crypto algorithm.
> +
> +if SPL
> +
> +config SPL_ASYMMETRIC_PUBLIC_KEY_SUBTYPE_MBEDTLS
> +       bool "Asymmetric public key crypto with MbedTLS certificate library 
> in SPL"
> +       help
> +         This option chooses MbedTLS certificate library for asymmetric 
> public
> +         key crypto algorithm in SPL.
> +
> +endif # SPL
> +
> +endif # MBEDTLS_LIB_X509
> +
>  endif # MBEDTLS_LIB
> diff --git a/lib/mbedtls/Makefile b/lib/mbedtls/Makefile
> index 32a98b7f4ca..f06d0704502 100644
> --- a/lib/mbedtls/Makefile
> +++ b/lib/mbedtls/Makefile
> @@ -20,6 +20,11 @@ hash_mbedtls-$(CONFIG_$(SPL_)SHA1_MBEDTLS) += sha1.o
>  hash_mbedtls-$(CONFIG_$(SPL_)SHA256_MBEDTLS) += sha256.o
>  hash_mbedtls-$(CONFIG_$(SPL_)SHA512_MBEDTLS) += sha512.o
>
> +# x509 libraries
> +obj-$(CONFIG_MBEDTLS_LIB_X509) += x509_mbedtls.o
> +x509_mbedtls-$(CONFIG_$(SPL_)ASYMMETRIC_PUBLIC_KEY_SUBTYPE_MBEDTLS) += \
> +       public_key.o
> +
>  # MbedTLS crypto library
>  obj-$(CONFIG_MBEDTLS_LIB_CRYPTO) += mbedtls_lib_crypto.o
>  mbedtls_lib_crypto-y += \
> @@ -45,7 +50,7 @@ mbedtls_lib_x509-$(CONFIG_$(SPL_)RSA_PUBLIC_KEY_PARSER) += \
>         $(MBEDTLS_LIB_DIR)/bignum_core.o \
>         $(MBEDTLS_LIB_DIR)/rsa.o \
>         $(MBEDTLS_LIB_DIR)/rsa_alt_helpers.o
> -mbedtls_lib_x509-$(CONFIG_$(SPL_)ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += \
> +mbedtls_lib_x509-$(CONFIG_$(SPL_)ASYMMETRIC_PUBLIC_KEY_SUBTYPE_MBEDTLS) += \
>         $(MBEDTLS_LIB_DIR)/pk.o \
>         $(MBEDTLS_LIB_DIR)/pk_wrap.o \
>         $(MBEDTLS_LIB_DIR)/pkparse.o
> diff --git a/lib/mbedtls/public_key.c b/lib/mbedtls/public_key.c
> new file mode 100644
> index 00000000000..076a61862cb
> --- /dev/null
> +++ b/lib/mbedtls/public_key.c
> @@ -0,0 +1,82 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Public key helper functions using MbedTLS X509 library
> + *
> + * Copyright (c) 2024 Linaro Limited
> + * Author: Raymond Mao <raymond....@linaro.org>
> + */
> +
> +#include <linux/compat.h>
> +#include <crypto/public_key.h>
> +
> +int public_key_verify_signature(const struct public_key *pkey,
> +                               const struct public_key_signature *sig)
> +{
> +       mbedtls_md_type_t mb_hash_algo;
> +       mbedtls_pk_context pk_ctx;
> +       int ret;
> +
> +       if (!pkey || !sig || pkey->key_is_private)
> +               return -EINVAL;
> +
> +       /*
> +        * ECRDSA (Elliptic Curve RedDSA) from Red Hat is not supported by
> +        * MbedTLS
> +        */

That's not Redhat AFAICT. That's Elliptic Curve (Russian) Digital
Signature Algorithm described in GOST 34.10-2018, GOST R 34.10-2012,
RFC 7091, ISO/IEC 14888-3:2018

> +       if (strcmp(pkey->pkey_algo, "rsa")) {
> +               pr_err("Encryption is not RSA: %s\n", sig->pkey_algo);
> +               return -EINVAL;
> +       }
> +
> +       /*
> +        * Can be pkcs1 or raw, but pkcs1 is expected.
> +        * This is just for argument checking, not necessarily passed to 
> MbedTLS,
> +        * For RSA signatures, MbedTLS typically supports the PKCS#1 v1.5
> +        * (aka. pkcs1) encoding by default.
> +        * The library internally handles the details of decoding and 
> verifying
> +        * the signature according to the expected encoding for the specified 
> algorithm.
> +        */
> +       if (strcmp(sig->encoding, "pkcs1")) {
> +               pr_err("Encoding %s is not supported, only supports pkcs1\n",
> +                      sig->encoding);
> +               return -EINVAL;
> +       }
> +
> +       if (!strcmp(sig->hash_algo, "sha1"))
> +               mb_hash_algo = MBEDTLS_MD_SHA1;
> +       else if (!strcmp(sig->hash_algo, "sha224"))
> +               mb_hash_algo = MBEDTLS_MD_SHA224;
> +       else if (!strcmp(sig->hash_algo, "sha256"))
> +               mb_hash_algo = MBEDTLS_MD_SHA256;
> +       else if (!strcmp(sig->hash_algo, "sha384"))
> +               mb_hash_algo = MBEDTLS_MD_SHA384;
> +       else if (!strcmp(sig->hash_algo, "sha512"))
> +               mb_hash_algo = MBEDTLS_MD_SHA512;
> +       else    /* Unknown or unsupported hash algorithm */
> +               return -EINVAL;
> +       /* Initialize the mbedtls_pk_context with RSA key type */
> +       mbedtls_pk_init(&pk_ctx);
> +
> +       /* Parse the DER-encoded public key */
> +       ret = mbedtls_pk_parse_public_key(&pk_ctx, pkey->key, pkey->keylen);
> +       if (ret) {
> +               pr_err("Failed to parse public key, ret:-0x%04x\n", -ret);
> +               ret = -EINVAL;
> +               goto err_key;
> +       }
> +
> +       /* Ensure that it is a RSA key */
> +       if (mbedtls_pk_get_type(&pk_ctx) != MBEDTLS_PK_RSA) {
> +               pr_err("Only RSA keys are supported\n");
> +               ret = -EKEYREJECTED;
> +               goto err_key;
> +       }
> +
> +       /* Verify the hash */
> +       ret = mbedtls_pk_verify(&pk_ctx, mb_hash_algo, sig->digest,
> +                               sig->digest_size, sig->s, sig->s_size);
> +
> +err_key:
> +       mbedtls_pk_free(&pk_ctx);
> +       return ret;
> +}
> --
> 2.25.1
>

Thanks
/Ilias

Reply via email to