mbedtls-alt/bignum: add bignum alternative implementation via /dev/mpi

Signed-off-by: makejian <makejian@xiaomi.com>
This commit is contained in:
makejian 2023-09-08 15:02:55 +08:00 committed by Xiang Xiao
parent 194269dbb8
commit e42de5d36d
6 changed files with 313 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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
*/

View file

@ -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;
}

View file

@ -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 <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/types.h>
#include <nuttx/math/math_ioctl.h>
#include <nuttx/math/mpi.h>
#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;
}

View file

@ -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;