diff --git a/crypto/mbedtls/Kconfig b/crypto/mbedtls/Kconfig index 39294ee55f..75a47b2269 100644 --- a/crypto/mbedtls/Kconfig +++ b/crypto/mbedtls/Kconfig @@ -149,6 +149,16 @@ config MBEDTLS_SHA1_ALT select MBEDTLS_ALT default n +config MBEDTLS_SHA256_ALT + bool "Enable Mbedt TLS SHA224/SHA256 module alted by nuttx crypto" + select MBEDTLS_ALT + default n + +config MBEDTLS_SHA512_ALT + bool "Enable Mbedt TLS SHA384/SHA512 module alted by nuttx crypto" + select MBEDTLS_ALT + default n + endif menuconfig MBEDTLS_APPS diff --git a/crypto/mbedtls/Makefile b/crypto/mbedtls/Makefile index 367d87bf9e..532a5a399c 100644 --- a/crypto/mbedtls/Makefile +++ b/crypto/mbedtls/Makefile @@ -105,6 +105,14 @@ ifeq ($(CONFIG_MBEDTLS_SHA1_ALT),y) CSRCS += $(APPDIR)/crypto/mbedtls/source/sha1_alt.c endif +ifeq ($(CONFIG_MBEDTLS_SHA256_ALT),y) +CSRCS += $(APPDIR)/crypto/mbedtls/source/sha256_alt.c +endif + +ifeq ($(CONFIG_MBEDTLS_SHA512_ALT),y) +CSRCS += $(APPDIR)/crypto/mbedtls/source/sha512_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 d4df42aae3..7840f7561e 100644 --- a/crypto/mbedtls/include/mbedtls/mbedtls_config.h +++ b/crypto/mbedtls/include/mbedtls/mbedtls_config.h @@ -373,9 +373,13 @@ #ifdef CONFIG_MBEDTLS_SHA1_ALT #define MBEDTLS_SHA1_ALT #endif -/* #define MBEDTLS_SHA256_ALT - * #define MBEDTLS_SHA512_ALT - * #define MBEDTLS_XTEA_ALT +#ifdef CONFIG_MBEDTLS_SHA256_ALT +#define MBEDTLS_SHA256_ALT +#endif +#ifdef CONFIG_MBEDTLS_SHA512_ALT +#define MBEDTLS_SHA512_ALT +#endif +/* #define MBEDTLS_XTEA_ALT */ /* When replacing the elliptic curve module, please consider, that it is diff --git a/crypto/mbedtls/include/sha256_alt.h b/crypto/mbedtls/include/sha256_alt.h new file mode 100644 index 0000000000..9e75530f08 --- /dev/null +++ b/crypto/mbedtls/include/sha256_alt.h @@ -0,0 +1,31 @@ +/**************************************************************************** + * apps/crypto/mbedtls/include/sha256_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_SHA256_ALT_H +#define __APPS_CRYPTO_MBEDTLS_INCLUDE_SHA256_ALT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "dev_alt.h" + +#define mbedtls_sha256_context cryptodev_context_t + +#endif /* __APPS_CRYPTO_MBEDTLS_INCLUDE_SHA256_ALT_H */ diff --git a/crypto/mbedtls/include/sha512_alt.h b/crypto/mbedtls/include/sha512_alt.h new file mode 100644 index 0000000000..663e755b69 --- /dev/null +++ b/crypto/mbedtls/include/sha512_alt.h @@ -0,0 +1,31 @@ +/**************************************************************************** + * apps/crypto/mbedtls/include/sha512_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_SHA512_ALT_H +#define __APPS_CRYPTO_MBEDTLS_INCLUDE_SHA512_ALT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "dev_alt.h" + +#define mbedtls_sha512_context cryptodev_context_t + +#endif /* __APPS_CRYPTO_MBEDTLS_INCLUDE_SHA512_ALT_H */ diff --git a/crypto/mbedtls/source/sha256_alt.c b/crypto/mbedtls/source/sha256_alt.c new file mode 100644 index 0000000000..8d154cae57 --- /dev/null +++ b/crypto/mbedtls/source/sha256_alt.c @@ -0,0 +1,82 @@ +/**************************************************************************** + * apps/crypto/mbedtls/source/sha256_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/sha256.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void mbedtls_sha256_clone(FAR mbedtls_sha256_context *dst, + FAR const mbedtls_sha256_context *src) +{ + cryptodev_clone(dst, src); +} + +void mbedtls_sha256_init(FAR mbedtls_sha256_context *ctx) +{ + cryptodev_init(ctx); +} + +void mbedtls_sha256_free(FAR mbedtls_sha256_context *ctx) +{ + cryptodev_free(ctx); +} + +int mbedtls_sha256_starts(FAR mbedtls_sha256_context *ctx, int is224) +{ + if (is224) + { + ctx->session.mac = CRYPTO_SHA2_224; + } + else + { + ctx->session.mac = CRYPTO_SHA2_256; + } + + return cryptodev_get_session(ctx); +} + +int mbedtls_sha256_update(FAR mbedtls_sha256_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_sha256_finish(FAR mbedtls_sha256_context *ctx, + FAR unsigned char *output) +{ + 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; +} diff --git a/crypto/mbedtls/source/sha512_alt.c b/crypto/mbedtls/source/sha512_alt.c new file mode 100644 index 0000000000..6c75fff5f9 --- /dev/null +++ b/crypto/mbedtls/source/sha512_alt.c @@ -0,0 +1,82 @@ +/**************************************************************************** + * apps/crypto/mbedtls/source/sha512_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/sha512.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void mbedtls_sha512_clone(FAR mbedtls_sha512_context *dst, + FAR const mbedtls_sha512_context *src) +{ + cryptodev_clone(dst, src); +} + +void mbedtls_sha512_init(FAR mbedtls_sha512_context *ctx) +{ + cryptodev_init(ctx); +} + +void mbedtls_sha512_free(FAR mbedtls_sha512_context *ctx) +{ + cryptodev_free(ctx); +} + +int mbedtls_sha512_starts(FAR mbedtls_sha512_context *ctx, int is384) +{ + if (is384) + { + ctx->session.mac = CRYPTO_SHA2_384; + } + else + { + ctx->session.mac = CRYPTO_SHA2_512; + } + + return cryptodev_get_session(ctx); +} + +int mbedtls_sha512_update(FAR mbedtls_sha512_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_sha512_finish(FAR mbedtls_sha512_context *ctx, + FAR unsigned char *output) +{ + 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; +}