From 194269dbb88a438c6e2a33e85dfa52803e3d77e7 Mon Sep 17 00:00:00 2001 From: makejian Date: Mon, 30 Oct 2023 15:27:07 +0800 Subject: [PATCH] mbedtls-alt/poly1305-alt: add poly1305 alternative implementation Signed-off-by: makejian --- crypto/mbedtls/Kconfig | 5 + crypto/mbedtls/Makefile | 4 + .../mbedtls/include/mbedtls/mbedtls_config.h | 5 +- crypto/mbedtls/include/poly1305_alt.h | 31 ++++++ crypto/mbedtls/source/poly1305_alt.c | 101 ++++++++++++++++++ 5 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 crypto/mbedtls/include/poly1305_alt.h create mode 100644 crypto/mbedtls/source/poly1305_alt.c diff --git a/crypto/mbedtls/Kconfig b/crypto/mbedtls/Kconfig index 136aedad7..4b329fb11 100644 --- a/crypto/mbedtls/Kconfig +++ b/crypto/mbedtls/Kconfig @@ -578,6 +578,11 @@ config MBEDTLS_MD5_ALT select MBEDTLS_ALT default n +config MBEDTLS_POLY1305_ALT + bool "Enable Mbedt TLS POLY1305 module alted by nuttx crypto" + select MBEDTLS_ALT + default n + config MBEDTLS_RIPEMD160_ALT bool "Enable Mbedt TLS RIPEMD160 module alted by nuttx crypto" select MBEDTLS_ALT diff --git a/crypto/mbedtls/Makefile b/crypto/mbedtls/Makefile index 595755b68..ccfa606bf 100644 --- a/crypto/mbedtls/Makefile +++ b/crypto/mbedtls/Makefile @@ -127,6 +127,10 @@ ifeq ($(CONFIG_MBEDTLS_MD5_ALT),y) CSRCS += $(APPDIR)/crypto/mbedtls/source/md5_alt.c endif +ifeq ($(CONFIG_MBEDTLS_POLY1305_ALT),y) +CSRCS += $(APPDIR)/crypto/mbedtls/source/poly1305_alt.c +endif + ifeq ($(CONFIG_MBEDTLS_RIPEMD160_ALT),y) CSRCS += $(APPDIR)/crypto/mbedtls/source/ripemd160_alt.c endif diff --git a/crypto/mbedtls/include/mbedtls/mbedtls_config.h b/crypto/mbedtls/include/mbedtls/mbedtls_config.h index af7eb7265..741fe376b 100644 --- a/crypto/mbedtls/include/mbedtls/mbedtls_config.h +++ b/crypto/mbedtls/include/mbedtls/mbedtls_config.h @@ -374,8 +374,9 @@ #ifdef CONFIG_MBEDTLS_MD5_ALT #define MBEDTLS_MD5_ALT #endif -/* #define MBEDTLS_POLY1305_ALT - */ +#ifdef CONFIG_MBEDTLS_POLY1305_ALT +#define MBEDTLS_POLY1305_ALT +#endif #ifdef CONFIG_MBEDTLS_RIPEMD160_ALT #define MBEDTLS_RIPEMD160_ALT #endif diff --git a/crypto/mbedtls/include/poly1305_alt.h b/crypto/mbedtls/include/poly1305_alt.h new file mode 100644 index 000000000..fa6e29a41 --- /dev/null +++ b/crypto/mbedtls/include/poly1305_alt.h @@ -0,0 +1,31 @@ +/**************************************************************************** + * apps/crypto/mbedtls/include/poly1305_alt.h + * + * 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_POLY1305_ALT_H +#define __APPS_CRYPTO_MBEDTLS_INCLUDE_POLY1305_ALT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "dev_alt.h" + +#define mbedtls_poly1305_context cryptodev_context_t + +#endif /* __APPS_CRYPTO_MBEDTLS_INCLUDE_POLY1305_ALT_H */ diff --git a/crypto/mbedtls/source/poly1305_alt.c b/crypto/mbedtls/source/poly1305_alt.c new file mode 100644 index 000000000..4326d19ff --- /dev/null +++ b/crypto/mbedtls/source/poly1305_alt.c @@ -0,0 +1,101 @@ +/**************************************************************************** + * apps/crypto/mbedtls/source/poly1305_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 "mbedtls/error.h" +#include "mbedtls/poly1305.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void mbedtls_poly1305_init(FAR mbedtls_poly1305_context *ctx) +{ + cryptodev_init(ctx); +} + +void mbedtls_poly1305_free(FAR mbedtls_poly1305_context *ctx) +{ + cryptodev_free(ctx); +} + +int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx, + const unsigned char key[32]) +{ + ctx->session.mac = CRYPTO_POLY1305; + ctx->session.mackey = (caddr_t)key; + ctx->session.mackeylen = 32; + return cryptodev_get_session(ctx); +} + +int mbedtls_poly1305_update(FAR mbedtls_poly1305_context *ctx, + FAR const unsigned char *input, + size_t ilen) +{ + ctx->crypt.op = COP_ENCRYPT; + ctx->crypt.flags |= COP_FLAG_UPDATE; + ctx->crypt.src = (caddr_t)input; + ctx->crypt.len = ilen; + return cryptodev_crypt(ctx); +} + +int mbedtls_poly1305_finish(FAR mbedtls_poly1305_context *ctx, + unsigned char mac[16]) +{ + int ret; + + ctx->crypt.op = COP_ENCRYPT; + ctx->crypt.flags = 0; + ctx->crypt.mac = (caddr_t)mac; + ret = cryptodev_crypt(ctx); + cryptodev_free_session(ctx); + return ret; +} + +int mbedtls_poly1305_mac(const unsigned char key[32], + FAR const unsigned char *input, + size_t ilen, + unsigned char mac[16]) +{ + mbedtls_poly1305_context ctx; + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + + mbedtls_poly1305_init(&ctx); + + ret = mbedtls_poly1305_starts(&ctx, key); + if (ret != 0) + { + goto cleanup; + } + + ret = mbedtls_poly1305_update(&ctx, input, ilen); + if (ret != 0) + { + goto cleanup; + } + + ret = mbedtls_poly1305_finish(&ctx, mac); + +cleanup: + mbedtls_poly1305_free(&ctx); + return ret; +}