From e42de5d36d0792adbdd4551a2ef5c031e10b6648 Mon Sep 17 00:00:00 2001 From: makejian Date: Fri, 8 Sep 2023 15:02:55 +0800 Subject: [PATCH] mbedtls-alt/bignum: add bignum alternative implementation via /dev/mpi Signed-off-by: makejian --- crypto/mbedtls/Kconfig | 5 + crypto/mbedtls/Makefile | 4 + .../mbedtls/include/mbedtls/mbedtls_config.h | 3 + crypto/mbedtls/source/aes_alt.c | 2 +- crypto/mbedtls/source/bignum_alt.c | 299 ++++++++++++++++++ crypto/mbedtls/source/poly1305_alt.c | 2 +- 6 files changed, 313 insertions(+), 2 deletions(-) create mode 100644 crypto/mbedtls/source/bignum_alt.c diff --git a/crypto/mbedtls/Kconfig b/crypto/mbedtls/Kconfig index 4b329fb11..8a040ca93 100644 --- a/crypto/mbedtls/Kconfig +++ b/crypto/mbedtls/Kconfig @@ -603,6 +603,11 @@ config MBEDTLS_SHA512_ALT select MBEDTLS_ALT default n +config MBEDTLS_BIGNUM_ALT + bool "Enable Mbedt TLS Bignum module alted by nuttx mpi" + select MBEDTLS_ALT + default n + endif # CRYPTO_CRYPTODEV menuconfig MBEDTLS_APPS diff --git a/crypto/mbedtls/Makefile b/crypto/mbedtls/Makefile index ccfa606bf..62e310070 100644 --- a/crypto/mbedtls/Makefile +++ b/crypto/mbedtls/Makefile @@ -147,6 +147,10 @@ ifeq ($(CONFIG_MBEDTLS_SHA512_ALT),y) CSRCS += $(APPDIR)/crypto/mbedtls/source/sha512_alt.c endif +ifeq ($(CONFIG_MBEDTLS_BIGNUM_ALT),y) +CSRCS += $(APPDIR)/crypto/mbedtls/source/bignum_alt.c +endif + endif include $(APPDIR)/Application.mk diff --git a/crypto/mbedtls/include/mbedtls/mbedtls_config.h b/crypto/mbedtls/include/mbedtls/mbedtls_config.h index 741fe376b..97dbec93e 100644 --- a/crypto/mbedtls/include/mbedtls/mbedtls_config.h +++ b/crypto/mbedtls/include/mbedtls/mbedtls_config.h @@ -391,6 +391,9 @@ #ifdef CONFIG_MBEDTLS_SHA512_ALT #define MBEDTLS_SHA512_ALT #endif +#ifdef CONFIG_MBEDTLS_BIGNUM_ALT +#define MBEDTLS_BIGNUM_ALT +#endif /* #define MBEDTLS_XTEA_ALT */ diff --git a/crypto/mbedtls/source/aes_alt.c b/crypto/mbedtls/source/aes_alt.c index 57d6cc80e..f3e8b6e1c 100644 --- a/crypto/mbedtls/source/aes_alt.c +++ b/crypto/mbedtls/source/aes_alt.c @@ -129,7 +129,7 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx, return MBEDTLS_ERR_AES_BAD_INPUT_DATA; } - if (length % 16) + if ((length % 16) != 0) { return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH; } diff --git a/crypto/mbedtls/source/bignum_alt.c b/crypto/mbedtls/source/bignum_alt.c new file mode 100644 index 000000000..fe0fe8d51 --- /dev/null +++ b/crypto/mbedtls/source/bignum_alt.c @@ -0,0 +1,299 @@ +/**************************************************************************** + * apps/crypto/mbedtls/source/bignum_alt.c + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "mbedtls/bignum.h" +#include "mbedtls/platform.h" + +#define MBEDTLS_ROUNDUP(v, size) (((v) + (size - 1)) & ~(size - 1)) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static inline +void mbedtls_mpi_to_mpiparam(FAR struct mpiparam *a, + FAR const mbedtls_mpi *A) +{ + a->n = A->n * sizeof(mbedtls_mpi_uint); + a->s = A->s; + a->p = (FAR uint8_t *)A->p; +} + +static inline +void mpiparam_to_mbedtls_mpi(FAR mbedtls_mpi *A, + FAR const struct mpiparam *a) +{ + A->n = a->n / sizeof(mbedtls_mpi_uint); + A->s = a->s; + A->p = (FAR mbedtls_mpi_uint *)a->p; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int mbedtls_mpi_add_mpi(FAR mbedtls_mpi *X, FAR const mbedtls_mpi *A, + FAR const mbedtls_mpi *B) +{ + int ret; + int fd; + struct mpi_calc_s mpi; + + fd = open("/dev/mpi0", O_RDWR); + if (fd < 0) + { + return -errno; + } + + mpi.op = MPI_CALC_FUNC_ADD; + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); + + mbedtls_mpi_grow(X, MBEDTLS_ROUNDUP(MAX(A->n, B->n) + 1, + sizeof(mbedtls_mpi_uint))); + mbedtls_mpi_to_mpiparam(&mpi.param[2], X); + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); + if (ret >= 0) + { + mpiparam_to_mbedtls_mpi(X, &mpi.param[2]); + } + + close(fd); + return ret; +} + +int mbedtls_mpi_sub_mpi(FAR mbedtls_mpi *X, FAR const mbedtls_mpi *A, + FAR const mbedtls_mpi *B) +{ + int ret; + int fd; + struct mpi_calc_s mpi; + + fd = open("/dev/mpi0", O_RDWR); + if (fd < 0) + { + return -errno; + } + + mpi.op = MPI_CALC_FUNC_SUB; + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); + + mbedtls_mpi_grow(X, MBEDTLS_ROUNDUP(MAX(A->n, B->n) + 1, + sizeof(mbedtls_mpi_uint))); + mbedtls_mpi_to_mpiparam(&mpi.param[2], X); + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); + if (ret >= 0) + { + mpiparam_to_mbedtls_mpi(X, &mpi.param[2]); + } + + close(fd); + return ret; +} + +int mbedtls_mpi_mul_mpi(FAR mbedtls_mpi *X, FAR const mbedtls_mpi *A, + FAR const mbedtls_mpi *B) +{ + int ret; + int fd; + struct mpi_calc_s mpi; + + fd = open("/dev/mpi0", O_RDWR); + if (fd < 0) + { + return -errno; + } + + mpi.op = MPI_CALC_FUNC_MUL; + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); + + mbedtls_mpi_grow(X, MBEDTLS_ROUNDUP(A->n + B->n, + sizeof(mbedtls_mpi_uint))); + mbedtls_mpi_to_mpiparam(&mpi.param[2], X); + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); + if (ret >= 0) + { + mpiparam_to_mbedtls_mpi(X, &mpi.param[2]); + } + + close(fd); + return ret; +} + +int mbedtls_mpi_div_mpi(FAR mbedtls_mpi *Q, FAR mbedtls_mpi *R, + FAR const mbedtls_mpi *A, FAR const mbedtls_mpi *B) +{ + int ret; + int fd; + struct mpi_calc_s mpi; + + fd = open("/dev/mpi0", O_RDWR); + if (fd < 0) + { + return -errno; + } + + mpi.op = MPI_CALC_FUNC_DIV; + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); + mbedtls_mpi_grow(Q, A->n); + mbedtls_mpi_grow(R, B->n); + mbedtls_mpi_to_mpiparam(&mpi.param[2], Q); + mbedtls_mpi_to_mpiparam(&mpi.param[3], R); + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); + if (ret >= 0) + { + mpiparam_to_mbedtls_mpi(Q, &mpi.param[2]); + mpiparam_to_mbedtls_mpi(R, &mpi.param[3]); + } + + close(fd); + return ret; +} + +int mbedtls_mpi_mod_mpi(FAR mbedtls_mpi *R, FAR const mbedtls_mpi *A, + FAR const mbedtls_mpi *B) +{ + int ret; + int fd; + struct mpi_calc_s mpi; + + fd = open("/dev/mpi0", O_RDWR); + if (fd < 0) + { + return -errno; + } + + mpi.op = MPI_CALC_FUNC_MOD; + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); + mbedtls_mpi_grow(R, B->n); + mbedtls_mpi_to_mpiparam(&mpi.param[2], R); + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); + if (ret >= 0) + { + mpiparam_to_mbedtls_mpi(R, &mpi.param[2]); + } + + close(fd); + return ret; +} + +int mbedtls_mpi_exp_mod(FAR mbedtls_mpi *X, FAR const mbedtls_mpi *A, + FAR const mbedtls_mpi *E, FAR const mbedtls_mpi *N, + FAR mbedtls_mpi *) +{ + int ret; + int fd; + struct mpi_calc_s mpi; + + fd = open("/dev/mpi0", O_RDWR); + if (fd < 0) + { + return -errno; + } + + mpi.op = MPI_CALC_FUNC_EXP_MOD; + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); + mbedtls_mpi_to_mpiparam(&mpi.param[1], E); + mbedtls_mpi_to_mpiparam(&mpi.param[2], N); + mbedtls_mpi_grow(X, N->n); + mbedtls_mpi_to_mpiparam(&mpi.param[3], X); + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); + if (ret >= 0) + { + mpiparam_to_mbedtls_mpi(X, &mpi.param[3]); + } + + close(fd); + return ret; +} + +int mbedtls_mpi_gcd(FAR mbedtls_mpi *G, FAR const mbedtls_mpi *A, + FAR const mbedtls_mpi *B) +{ + int ret; + int fd; + struct mpi_calc_s mpi; + + fd = open("/dev/mpi0", O_RDWR); + if (fd < 0) + { + return -errno; + } + + mpi.op = MPI_CALC_FUNC_GCD; + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); + mbedtls_mpi_to_mpiparam(&mpi.param[1], B); + mbedtls_mpi_grow(G, MIN(A->n, B->n)); + mbedtls_mpi_to_mpiparam(&mpi.param[2], G); + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); + if (ret >= 0) + { + mpiparam_to_mbedtls_mpi(G, &mpi.param[2]); + } + + close(fd); + return ret; +} + +int mbedtls_mpi_inv_mod(FAR mbedtls_mpi *X, FAR const mbedtls_mpi *A, + FAR const mbedtls_mpi *N) +{ + int ret; + int fd; + struct mpi_calc_s mpi; + + fd = open("/dev/mpi0", O_RDWR); + if (fd < 0) + { + return -errno; + } + + mpi.op = MPI_CALC_FUNC_INV_MOD; + mbedtls_mpi_to_mpiparam(&mpi.param[0], A); + mbedtls_mpi_to_mpiparam(&mpi.param[1], N); + mbedtls_mpi_grow(X, N->n); + mbedtls_mpi_to_mpiparam(&mpi.param[2], X); + ret = ioctl(fd, MATHIOC_MPI_CALC, (unsigned long)((uintptr_t)&mpi)); + if (ret >= 0) + { + mpiparam_to_mbedtls_mpi(X, &mpi.param[2]); + } + + close(fd); + return ret; +} diff --git a/crypto/mbedtls/source/poly1305_alt.c b/crypto/mbedtls/source/poly1305_alt.c index 4326d19ff..b956fc395 100644 --- a/crypto/mbedtls/source/poly1305_alt.c +++ b/crypto/mbedtls/source/poly1305_alt.c @@ -38,7 +38,7 @@ void mbedtls_poly1305_free(FAR mbedtls_poly1305_context *ctx) cryptodev_free(ctx); } -int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx, +int mbedtls_poly1305_starts(FAR mbedtls_poly1305_context *ctx, const unsigned char key[32]) { ctx->session.mac = CRYPTO_POLY1305;