Skip to content

Commit

Permalink
Factorize nifs for OTP crypto module
Browse files Browse the repository at this point in the history
Move implementation from ESP32 to otp_crypto.c and make it available to all
platforms using mbedtls.

Alter Pico's mbedtls configuration to handle the ciphers we support

Also fix crypto implementation to accept iv for ciphers that do not require one,
following OTP's API.

Signed-off-by: Paul Guyot <pguyot@kallisys.net>
  • Loading branch information
pguyot committed Nov 6, 2023
1 parent 71cc082 commit 1ab57e3
Show file tree
Hide file tree
Showing 16 changed files with 765 additions and 617 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test-other.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ jobs:
${{ matrix.install_deps }}
else
apt update &&
apt install -y file gcc g++ binutils cmake make doxygen gperf zlib1g-dev libssl-dev
apt install -y file gcc g++ binutils cmake make doxygen gperf zlib1g-dev libssl-dev libmbedtls-dev
fi &&
file /bin/bash &&
uname -a &&
Expand Down
593 changes: 593 additions & 0 deletions src/libAtomVM/otp_crypto.c

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions src/libAtomVM/otp_crypto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of AtomVM.
*
* Copyright 2023 Paul Guyot <pguyot@kallisys.net>
*
* Licensed 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.
*
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
*/

#ifndef _OTP_CRYPTO_H_
#define _OTP_CRYPTO_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <nifs.h>

const struct Nif *otp_crypto_nif_get_nif(const char *nifname);

#ifdef __cplusplus
}
#endif

#endif
3 changes: 2 additions & 1 deletion src/platforms/esp32/components/avm_builtins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(AVM_BUILTIN_COMPONENT_SRCS
"socket_driver.c"
"spi_driver.c"
"uart_driver.c"
"otp_crypto_platform.c"
"otp_net_platform.c"
"otp_socket_platform.c"
"otp_ssl_platform.c"
Expand All @@ -39,7 +40,7 @@ else()
set(ADDITIONAL_PRIV_REQUIRES "")
endif()

if(CONFIG_AVM_ENABLE_OTP_SSL_NIFS)
if(CONFIG_AVM_ENABLE_OTP_SSL_NIFS OR CONFIG_AVM_ENABLE_OTP_CRYPTO_NIFS)
set(ADDITIONAL_PRIV_REQUIRES ${ADDITIONAL_PRIV_REQUIRES} "mbedtls")
endif()

Expand Down
4 changes: 4 additions & 0 deletions src/platforms/esp32/components/avm_builtins/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ config AVM_ENABLE_UART_PORT_DRIVER
bool "Enable UART port driver"
default y

config AVM_ENABLE_OTP_CRYPTO_NIFS
bool "Enable OTP Crypto NIFs"
default y

config AVM_ENABLE_OTP_SOCKET_NIFS
bool "Enable OTP Socket NIFs"
default y
Expand Down
30 changes: 30 additions & 0 deletions src/platforms/esp32/components/avm_builtins/otp_crypto_platform.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is part of AtomVM.
*
* Copyright 2023 by Paul Guyot <pguyot@kallisys.net>
*
* Licensed 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.
*
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
*/

#include <esp32_sys.h>
#include <nifs.h>
#include <otp_crypto.h>
#include <sdkconfig.h>

#ifdef CONFIG_AVM_ENABLE_OTP_CRYPTO_NIFS

REGISTER_NIF_COLLECTION(otp_crypto, NULL, NULL, otp_crypto_nif_get_nif)

#endif
1 change: 1 addition & 0 deletions src/platforms/esp32/components/avm_sys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(AVM_SYS_COMPONENT_SRCS
"platform_nifs.c"
"platform_defaultatoms.c"
"../../../../libAtomVM/inet.c"
"../../../../libAtomVM/otp_crypto.c"
"../../../../libAtomVM/otp_net.c"
"../../../../libAtomVM/otp_socket.c"
"../../../../libAtomVM/otp_ssl.c"
Expand Down
Loading

0 comments on commit 1ab57e3

Please sign in to comment.