diff --git a/crypto/mbedtls/Kconfig b/crypto/mbedtls/Kconfig index 87c4e76a50e..c9876786d64 100644 --- a/crypto/mbedtls/Kconfig +++ b/crypto/mbedtls/Kconfig @@ -577,6 +577,16 @@ 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 + default n + config MBEDTLS_SHA1_ALT bool "Enable Mbedt TLS SHA1 module alted by nuttx crypto" select MBEDTLS_ALT @@ -592,6 +602,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 2b4331a96d2..62e310070f7 100644 --- a/crypto/mbedtls/Makefile +++ b/crypto/mbedtls/Makefile @@ -127,6 +127,14 @@ 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 + ifeq ($(CONFIG_MBEDTLS_SHA1_ALT),y) CSRCS += $(APPDIR)/crypto/mbedtls/source/sha1_alt.c endif @@ -139,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/aes_alt.h b/crypto/mbedtls/include/aes_alt.h index c920b2bd350..59077382935 100644 --- a/crypto/mbedtls/include/aes_alt.h +++ b/crypto/mbedtls/include/aes_alt.h @@ -30,7 +30,7 @@ * Pre-processor Definitions ****************************************************************************/ -#define MAX_KEY_SIZE 36 +#define MAX_KEY_SIZE 64 typedef struct mbedtls_aes_context { diff --git a/crypto/mbedtls/include/mbedtls/mbedtls_config.h b/crypto/mbedtls/include/mbedtls/mbedtls_config.h index 77489f8e675..97dbec93e49 100644 --- a/crypto/mbedtls/include/mbedtls/mbedtls_config.h +++ b/crypto/mbedtls/include/mbedtls/mbedtls_config.h @@ -374,9 +374,13 @@ #ifdef CONFIG_MBEDTLS_MD5_ALT #define MBEDTLS_MD5_ALT #endif -/* #define MBEDTLS_POLY1305_ALT - * #define MBEDTLS_RIPEMD160_ALT - * #define MBEDTLS_RSA_ALT +#ifdef CONFIG_MBEDTLS_POLY1305_ALT +#define MBEDTLS_POLY1305_ALT +#endif +#ifdef CONFIG_MBEDTLS_RIPEMD160_ALT +#define MBEDTLS_RIPEMD160_ALT +#endif +/* #define MBEDTLS_RSA_ALT */ #ifdef CONFIG_MBEDTLS_SHA1_ALT #define MBEDTLS_SHA1_ALT @@ -387,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/include/poly1305_alt.h b/crypto/mbedtls/include/poly1305_alt.h new file mode 100644 index 00000000000..fa6e29a41b0 --- /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/include/ripemd160_alt.h b/crypto/mbedtls/include/ripemd160_alt.h new file mode 100644 index 00000000000..03ba2a4b0ea --- /dev/null +++ b/crypto/mbedtls/include/ripemd160_alt.h @@ -0,0 +1,31 @@ +/**************************************************************************** + * apps/crypto/mbedtls/include/ripemd160_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_RIPEMD160_ALT_H +#define __APPS_CRYPTO_MBEDTLS_INCLUDE_RIPEMD160_ALT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "dev_alt.h" + +#define mbedtls_ripemd160_context cryptodev_context_t + +#endif /* __APPS_CRYPTO_MBEDTLS_INCLUDE_RIPEMD160_ALT_H */ diff --git a/crypto/mbedtls/source/aes_alt.c b/crypto/mbedtls/source/aes_alt.c index 34ec26604f7..f3e8b6e1ca9 100644 --- a/crypto/mbedtls/source/aes_alt.c +++ b/crypto/mbedtls/source/aes_alt.c @@ -51,6 +51,18 @@ int mbedtls_aes_setkey_enc(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; @@ -74,6 +86,11 @@ int mbedtls_aes_crypt_ecb(FAR mbedtls_aes_context *ctx, int ret; unsigned char iv[16]; + if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT) + { + return MBEDTLS_ERR_AES_BAD_INPUT_DATA; + } + ctx->dev.session.cipher = CRYPTO_AES_CBC; ret = cryptodev_get_session(&ctx->dev); if (ret != 0) @@ -107,6 +124,16 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx, { int ret; + if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT) + { + return MBEDTLS_ERR_AES_BAD_INPUT_DATA; + } + + if ((length % 16) != 0) + { + return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH; + } + ctx->dev.session.cipher = CRYPTO_AES_CBC; ret = cryptodev_get_session(&ctx->dev); if (ret != 0) @@ -141,6 +168,11 @@ int mbedtls_aes_crypt_ctr(FAR mbedtls_aes_context *ctx, { int ret; + if (*nc_off > 0x0f) + { + return MBEDTLS_ERR_AES_BAD_INPUT_DATA; + } + ctx->dev.session.cipher = CRYPTO_AES_CTR; memcpy(ctx->key + ctx->dev.session.keylen, nonce_counter, NONCE_LENGTH); @@ -183,14 +215,22 @@ int mbedtls_aes_xts_setkey_enc(FAR mbedtls_aes_xts_context *ctx, FAR const unsigned char *key, unsigned int keybits) { - return mbedtls_aes_setkey_enc(ctx, key, keybits); + if (keybits != 256 && keybits != 512) + { + 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; + return 0; } int mbedtls_aes_xts_setkey_dec(FAR mbedtls_aes_xts_context *ctx, FAR const unsigned char *key, unsigned int keybits) { - return mbedtls_aes_setkey_dec(ctx, key, keybits); + return mbedtls_aes_xts_setkey_enc(ctx, key, keybits); } int mbedtls_aes_crypt_xts(FAR mbedtls_aes_xts_context *ctx, @@ -203,6 +243,25 @@ int mbedtls_aes_crypt_xts(FAR mbedtls_aes_xts_context *ctx, int ret; unsigned char iv[16]; + if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT) + { + return MBEDTLS_ERR_AES_BAD_INPUT_DATA; + } + + /* Data units must be at least 16 bytes long. */ + + if (length < 16) + { + return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH; + } + + /* NIST SP 800-38E disallows data units larger than 2**20 blocks. */ + + if (length > (1 << 20) * 16) + { + return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH; + } + ctx->dev.session.cipher = CRYPTO_AES_XTS; ret = cryptodev_get_session(&ctx->dev); if (ret != 0) @@ -238,6 +297,16 @@ int mbedtls_aes_crypt_cfb128(FAR mbedtls_aes_context *ctx, { int ret; + if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT) + { + return MBEDTLS_ERR_AES_BAD_INPUT_DATA; + } + + if (*iv_off > 15) + { + return MBEDTLS_ERR_AES_BAD_INPUT_DATA; + } + ctx->dev.session.cipher = CRYPTO_AES_CFB_128; ret = cryptodev_get_session(&ctx->dev); if (ret != 0) @@ -273,6 +342,11 @@ int mbedtls_aes_crypt_cfb8(FAR mbedtls_aes_context *ctx, { int ret; + if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT) + { + return MBEDTLS_ERR_AES_BAD_INPUT_DATA; + } + ctx->dev.session.cipher = CRYPTO_AES_CFB_8; ret = cryptodev_get_session(&ctx->dev); if (ret != 0) @@ -306,6 +380,11 @@ int mbedtls_aes_crypt_ofb(FAR mbedtls_aes_context *ctx, { int ret; + if (*iv_off > 15) + { + return MBEDTLS_ERR_AES_BAD_INPUT_DATA; + } + ctx->dev.session.cipher = CRYPTO_AES_OFB; ret = cryptodev_get_session(&ctx->dev); if (ret != 0) diff --git a/crypto/mbedtls/source/bignum_alt.c b/crypto/mbedtls/source/bignum_alt.c new file mode 100644 index 00000000000..fe0fe8d51d5 --- /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/dev_alt.c b/crypto/mbedtls/source/dev_alt.c index adaada61913..9f9206445ef 100644 --- a/crypto/mbedtls/source/dev_alt.c +++ b/crypto/mbedtls/source/dev_alt.c @@ -94,5 +94,5 @@ int cryptodev_clone(FAR cryptodev_context_t *dst, { dst->session = src->session; dst->crypt = src->crypt; - return cryptodev_get_session(dst); + return dup2(src->fd, dst->fd); } diff --git a/crypto/mbedtls/source/poly1305_alt.c b/crypto/mbedtls/source/poly1305_alt.c new file mode 100644 index 00000000000..b956fc395f7 --- /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(FAR 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; +} diff --git a/crypto/mbedtls/source/ripemd160_alt.c b/crypto/mbedtls/source/ripemd160_alt.c new file mode 100644 index 00000000000..46db7474b38 --- /dev/null +++ b/crypto/mbedtls/source/ripemd160_alt.c @@ -0,0 +1,74 @@ +/**************************************************************************** + * apps/crypto/mbedtls/source/ripemd160_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/ripemd160.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void mbedtls_ripemd160_clone(FAR mbedtls_ripemd160_context *dst, + FAR const mbedtls_ripemd160_context *src) +{ + cryptodev_clone(dst, src); +} + +void mbedtls_ripemd160_init(FAR mbedtls_ripemd160_context *ctx) +{ + cryptodev_init(ctx); +} + +void mbedtls_ripemd160_free(FAR mbedtls_ripemd160_context *ctx) +{ + cryptodev_free(ctx); +} + +int mbedtls_ripemd160_starts(FAR mbedtls_ripemd160_context *ctx) +{ + ctx->session.mac = CRYPTO_RIPEMD160; + return cryptodev_get_session(ctx); +} + +int mbedtls_ripemd160_update(FAR mbedtls_ripemd160_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_ripemd160_finish(FAR mbedtls_ripemd160_context *ctx, + unsigned char output[20]) +{ + int ret; + + ctx->crypt.op = COP_ENCRYPT; + ctx->crypt.flags = 0; + ctx->crypt.mac = (caddr_t)output; + ret = cryptodev_crypt(ctx); + cryptodev_free_session(ctx); + return ret; +}