From 56f23b850548b949a751284a1ed5db801dbb167d Mon Sep 17 00:00:00 2001 From: dianjixz <18637716021@163.com> Date: Mon, 17 Mar 2025 11:29:50 +0800 Subject: [PATCH] [add] mbedtls_component --- components/mbedtls_component/Kconfig | 43 ++++ components/mbedtls_component/SConstruct | 282 ++++++++++++++++++++++ examples/mbedtls_demo/.gitignore | 6 + examples/mbedtls_demo/SConstruct | 4 + examples/mbedtls_demo/config_defaults.mk | 12 + examples/mbedtls_demo/main/Kconfig | 0 examples/mbedtls_demo/main/SConstruct | 33 +++ examples/mbedtls_demo/main/include/main.h | 3 + examples/mbedtls_demo/main/src/main.c | 63 +++++ github_source/source-list.sh | 1 + 10 files changed, 447 insertions(+) create mode 100644 components/mbedtls_component/Kconfig create mode 100644 components/mbedtls_component/SConstruct create mode 100644 examples/mbedtls_demo/.gitignore create mode 100644 examples/mbedtls_demo/SConstruct create mode 100644 examples/mbedtls_demo/config_defaults.mk create mode 100644 examples/mbedtls_demo/main/Kconfig create mode 100644 examples/mbedtls_demo/main/SConstruct create mode 100644 examples/mbedtls_demo/main/include/main.h create mode 100644 examples/mbedtls_demo/main/src/main.c diff --git a/components/mbedtls_component/Kconfig b/components/mbedtls_component/Kconfig new file mode 100644 index 0000000..a5b7255 --- /dev/null +++ b/components/mbedtls_component/Kconfig @@ -0,0 +1,43 @@ + +menuconfig MBEDTLS_COMPONENT_ENABLED + bool "Enable mbedtls component" + default n + + config EVEREST_ENABLED + bool "compile mbedtls libeverest" + default n + depends on MBEDTLS_COMPONENT_ENABLED + + config P256M_ENABLED + bool "compile mbedtls libp256m" + default n + depends on MBEDTLS_COMPONENT_ENABLED + + config MBEDCRYPTO_ENABLED + bool "compile mbedtls libmbedcrypto" + default n + depends on MBEDTLS_COMPONENT_ENABLED + + config MBEDX509_ENABLED + bool "compile mbedtls libmbedx509" + default n + depends on MBEDTLS_COMPONENT_ENABLED + + config MBEDTLS_ENABLED + bool "compile mbedtls libmbedtls" + default n + depends on MBEDTLS_COMPONENT_ENABLED + + + + + + + + + + + + + + diff --git a/components/mbedtls_component/SConstruct b/components/mbedtls_component/SConstruct new file mode 100644 index 0000000..fe98e52 --- /dev/null +++ b/components/mbedtls_component/SConstruct @@ -0,0 +1,282 @@ +# component/SConscript +Import("env") +import os + +with open(env["PROJECT_TOOL_S"]) as f: + exec(f.read()) + + +if "CONFIG_MBEDTLS_COMPONENT_ENABLED" in os.environ: + check_component("mbedtls") + SRCS = [] + INCLUDE = [ADir("../../github_source/mbedtls/include")] + PRIVATE_INCLUDE = [ADir("../../github_source/mbedtls/library")] + REQUIREMENTS = [] + STATIC_LIB = [] + DYNAMIC_LIB = [] + DEFINITIONS = [] + DEFINITIONS_PRIVATE = ["-std=c99", "-O2"] + LDFLAGS = [] + LINK_SEARCH_PATH = [] + + if "CONFIG_EVEREST_ENABLED" in os.environ: + everest_src = ["everest.c", "x25519.c", "Hacl_Curve25519_joined.c"] + _SRCS = [ + AFile("../../github_source/mbedtls/3rdparty/everest/library/" + src) + for src in everest_src + ] + _INCLUDE = [] + _PRIVATE_INCLUDE = [ + ADir("../../github_source/mbedtls/3rdparty/everest/include"), + ADir("../../github_source/mbedtls/3rdparty/everest/include/everest"), + ADir( + "../../github_source/mbedtls/3rdparty/everest/include/everest/kremlib" + ), + ] + _DEFINITIONS_PRIVATE = [] + env["COMPONENTS"].append( + { + "target": "everest", + "SRCS": SRCS + _SRCS, + "INCLUDE": INCLUDE + _INCLUDE, + "PRIVATE_INCLUDE": PRIVATE_INCLUDE + _PRIVATE_INCLUDE, + "REQUIREMENTS": REQUIREMENTS, + "STATIC_LIB": STATIC_LIB, + "DYNAMIC_LIB": DYNAMIC_LIB, + "DEFINITIONS": DEFINITIONS, + "DEFINITIONS_PRIVATE": DEFINITIONS_PRIVATE + _DEFINITIONS_PRIVATE, + "LDFLAGS": LDFLAGS, + "LINK_SEARCH_PATH": LINK_SEARCH_PATH, + "REGISTER": "static", + } + ) + + if "CONFIG_P256M_ENABLED" in os.environ: + p256m_src = ["p256-m_driver_entrypoints.c", "p256-m/p256-m.c"] + _SRCS = [ + AFile("../../github_source/mbedtls/3rdparty/p256-m/" + src) + for src in p256m_src + ] + _INCLUDE = [] + _PRIVATE_INCLUDE = [ + ADir("../../github_source/mbedtls/3rdparty/p256-m"), + ADir("../../github_source/mbedtls/3rdparty/p256-m/p256-m"), + ] + _DEFINITIONS_PRIVATE = [] + env["COMPONENTS"].append( + { + "target": "p256m", + "SRCS": SRCS + _SRCS, + "INCLUDE": INCLUDE + _INCLUDE, + "PRIVATE_INCLUDE": PRIVATE_INCLUDE + _PRIVATE_INCLUDE, + "REQUIREMENTS": REQUIREMENTS, + "STATIC_LIB": STATIC_LIB, + "DYNAMIC_LIB": DYNAMIC_LIB, + "DEFINITIONS": DEFINITIONS, + "DEFINITIONS_PRIVATE": DEFINITIONS_PRIVATE + _DEFINITIONS_PRIVATE, + "LDFLAGS": LDFLAGS, + "LINK_SEARCH_PATH": LINK_SEARCH_PATH, + "REGISTER": "static", + } + ) + + if "CONFIG_MBEDCRYPTO_ENABLED" in os.environ: + mbedcrypto_src = [ + "aes.c", + "aesni.c", + "aesce.c", + "aria.c", + "asn1parse.c", + "asn1write.c", + "base64.c", + "bignum.c", + "bignum_core.c", + "bignum_mod.c", + "bignum_mod_raw.c", + "block_cipher.c", + "camellia.c", + "ccm.c", + "chacha20.c", + "chachapoly.c", + "cipher.c", + "cipher_wrap.c", + "constant_time.c", + "cmac.c", + "ctr_drbg.c", + "des.c", + "dhm.c", + "ecdh.c", + "ecdsa.c", + "ecjpake.c", + "ecp.c", + "ecp_curves.c", + "ecp_curves_new.c", + "entropy.c", + "entropy_poll.c", + "error.c", + "gcm.c", + "hkdf.c", + "hmac_drbg.c", + "lmots.c", + "lms.c", + "md.c", + "md5.c", + "memory_buffer_alloc.c", + "nist_kw.c", + "oid.c", + "padlock.c", + "pem.c", + "pk.c", + "pk_ecc.c", + "pk_wrap.c", + "pkcs12.c", + "pkcs5.c", + "pkparse.c", + "pkwrite.c", + "platform.c", + "platform_util.c", + "poly1305.c", + "psa_crypto.c", + "psa_crypto_aead.c", + "psa_crypto_cipher.c", + "psa_crypto_client.c", + "psa_crypto_driver_wrappers_no_static.c", + "psa_crypto_ecp.c", + "psa_crypto_ffdh.c", + "psa_crypto_hash.c", + "psa_crypto_mac.c", + "psa_crypto_pake.c", + "psa_crypto_rsa.c", + "psa_crypto_se.c", + "psa_crypto_slot_management.c", + "psa_crypto_storage.c", + "psa_its_file.c", + "psa_util.c", + "ripemd160.c", + "rsa.c", + "rsa_alt_helpers.c", + "sha1.c", + "sha256.c", + "sha512.c", + "sha3.c", + "threading.c", + "timing.c", + "version.c", + "version_features.c", + ] + _SRCS = [ + AFile("../../github_source/mbedtls/library/" + src) + for src in mbedcrypto_src + ] + _INCLUDE = [] + _DEFINITIONS_PRIVATE = [] + env["COMPONENTS"].append( + { + "target": "mbedcrypto", + "SRCS": SRCS + _SRCS, + "INCLUDE": INCLUDE + _INCLUDE, + "PRIVATE_INCLUDE": PRIVATE_INCLUDE, + "REQUIREMENTS": REQUIREMENTS, + "STATIC_LIB": STATIC_LIB, + "DYNAMIC_LIB": DYNAMIC_LIB, + "DEFINITIONS": DEFINITIONS, + "DEFINITIONS_PRIVATE": DEFINITIONS_PRIVATE + _DEFINITIONS_PRIVATE, + "LDFLAGS": LDFLAGS, + "LINK_SEARCH_PATH": LINK_SEARCH_PATH, + "REGISTER": "static", + } + ) + + if "CONFIG_MBEDX509_ENABLED" in os.environ: + mbedx509_src = [ + "pkcs7.c", + "x509.c", + "x509_create.c", + "x509_crl.c", + "x509_crt.c", + "x509_csr.c", + "x509write.c", + "x509write_crt.c", + "x509write_csr.c", + ] + _SRCS = [ + AFile("../../github_source/mbedtls/library/" + src) for src in mbedx509_src + ] + _INCLUDE = [] + _DEFINITIONS_PRIVATE = [] + env["COMPONENTS"].append( + { + "target": "mbedx509", + "SRCS": SRCS + _SRCS, + "INCLUDE": INCLUDE + _INCLUDE, + "PRIVATE_INCLUDE": PRIVATE_INCLUDE, + "REQUIREMENTS": REQUIREMENTS, + "STATIC_LIB": STATIC_LIB, + "DYNAMIC_LIB": DYNAMIC_LIB, + "DEFINITIONS": DEFINITIONS, + "DEFINITIONS_PRIVATE": DEFINITIONS_PRIVATE + _DEFINITIONS_PRIVATE, + "LDFLAGS": LDFLAGS, + "LINK_SEARCH_PATH": LINK_SEARCH_PATH, + "REGISTER": "static", + } + ) + + if "CONFIG_MBEDTLS_ENABLED" in os.environ: + mbedtls_src = [ + "debug.c", + "mps_reader.c", + "mps_trace.c", + "net_sockets.c", + "ssl_cache.c", + "ssl_ciphersuites.c", + "ssl_client.c", + "ssl_cookie.c", + "ssl_debug_helpers_generated.c", + "ssl_msg.c", + "ssl_ticket.c", + "ssl_tls.c", + "ssl_tls12_client.c", + "ssl_tls12_server.c", + "ssl_tls13_keys.c", + "ssl_tls13_server.c", + "ssl_tls13_client.c", + "ssl_tls13_generic.c", + ] + _SRCS = [ + AFile("../../github_source/mbedtls/library/" + src) for src in mbedtls_src + ] + _INCLUDE = [] + _DEFINITIONS_PRIVATE = [] + env["COMPONENTS"].append( + { + "target": "mbedtls", + "SRCS": SRCS + _SRCS, + "INCLUDE": INCLUDE + _INCLUDE, + "PRIVATE_INCLUDE": PRIVATE_INCLUDE, + "REQUIREMENTS": REQUIREMENTS, + "STATIC_LIB": STATIC_LIB, + "DYNAMIC_LIB": DYNAMIC_LIB, + "DEFINITIONS": DEFINITIONS, + "DEFINITIONS_PRIVATE": DEFINITIONS_PRIVATE + _DEFINITIONS_PRIVATE, + "LDFLAGS": LDFLAGS, + "LINK_SEARCH_PATH": LINK_SEARCH_PATH, + "REGISTER": "static", + } + ) + + env["COMPONENTS"].append( + { + "target": os.path.basename(env["component_dir"]), + "SRCS": SRCS, + "INCLUDE": INCLUDE, + "PRIVATE_INCLUDE": PRIVATE_INCLUDE, + "REQUIREMENTS": REQUIREMENTS, + "STATIC_LIB": STATIC_LIB, + "DYNAMIC_LIB": DYNAMIC_LIB, + "DEFINITIONS": DEFINITIONS, + "DEFINITIONS_PRIVATE": DEFINITIONS_PRIVATE, + "LDFLAGS": LDFLAGS, + "LINK_SEARCH_PATH": LINK_SEARCH_PATH, + "REGISTER": "static", + } + ) diff --git a/examples/mbedtls_demo/.gitignore b/examples/mbedtls_demo/.gitignore new file mode 100644 index 0000000..76b743d --- /dev/null +++ b/examples/mbedtls_demo/.gitignore @@ -0,0 +1,6 @@ + +dist +build +.config.mk +.flash.conf.json + diff --git a/examples/mbedtls_demo/SConstruct b/examples/mbedtls_demo/SConstruct new file mode 100644 index 0000000..076d65c --- /dev/null +++ b/examples/mbedtls_demo/SConstruct @@ -0,0 +1,4 @@ +from pathlib import Path +import os +with open(str(Path(os.getcwd())/'..'/'..'/'tools'/'scons'/'project.py')) as f: + exec(f.read()) diff --git a/examples/mbedtls_demo/config_defaults.mk b/examples/mbedtls_demo/config_defaults.mk new file mode 100644 index 0000000..11e127d --- /dev/null +++ b/examples/mbedtls_demo/config_defaults.mk @@ -0,0 +1,12 @@ +# unix +# CONFIG_TOOLCHAIN_PATH="/opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin" +# win +# CONFIG_TOOLCHAIN_PATH="..\\gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf\\bin" + +# CONFIG_TOOLCHAIN_PREFIX="arm-linux-gnueabihf-" +CONFIG_MBEDTLS_COMPONENT_ENABLED=y +CONFIG_EVEREST_ENABLED=y +CONFIG_P256M_ENABLED=y +CONFIG_MBEDCRYPTO_ENABLED=y +CONFIG_MBEDX509_ENABLED=y +CONFIG_MBEDTLS_ENABLED=y \ No newline at end of file diff --git a/examples/mbedtls_demo/main/Kconfig b/examples/mbedtls_demo/main/Kconfig new file mode 100644 index 0000000..e69de29 diff --git a/examples/mbedtls_demo/main/SConstruct b/examples/mbedtls_demo/main/SConstruct new file mode 100644 index 0000000..1cfd711 --- /dev/null +++ b/examples/mbedtls_demo/main/SConstruct @@ -0,0 +1,33 @@ +# project_root/src/SConscript +import os +# Import the environment from the SConstruct file +Import('env') +with open(env['PROJECT_TOOL_S']) as f: + exec(f.read()) + + +SRCS = Glob('src/*.c*') +INCLUDE = [ADir('include'), ADir('.')] +PRIVATE_INCLUDE = [] +REQUIREMENTS = ['pthread', 'mbedtls_component', 'mbedtls', 'mbedcrypto', 'mbedx509'] +STATIC_LIB = [] +DYNAMIC_LIB = [] +DEFINITIONS = [] +DEFINITIONS_PRIVATE = [] +LDFLAGS = [] +LINK_SEARCH_PATH = [] + + +env['COMPONENTS'].append({'target':env['PROJECT_NAME'], + 'SRCS':SRCS, + 'INCLUDE':INCLUDE, + 'PRIVATE_INCLUDE':PRIVATE_INCLUDE, + 'REQUIREMENTS':REQUIREMENTS, + 'STATIC_LIB':STATIC_LIB, + 'DYNAMIC_LIB':DYNAMIC_LIB, + 'DEFINITIONS':DEFINITIONS, + 'DEFINITIONS_PRIVATE':DEFINITIONS_PRIVATE, + 'LDFLAGS':LDFLAGS, + 'LINK_SEARCH_PATH':LINK_SEARCH_PATH, + 'REGISTER':'project' + }) \ No newline at end of file diff --git a/examples/mbedtls_demo/main/include/main.h b/examples/mbedtls_demo/main/include/main.h new file mode 100644 index 0000000..45dcbb0 --- /dev/null +++ b/examples/mbedtls_demo/main/include/main.h @@ -0,0 +1,3 @@ +#pragma once + + diff --git a/examples/mbedtls_demo/main/src/main.c b/examples/mbedtls_demo/main/src/main.c new file mode 100644 index 0000000..db6afad --- /dev/null +++ b/examples/mbedtls_demo/main/src/main.c @@ -0,0 +1,63 @@ +/* + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +#include +#include +#include + +int main() { + // Define AES key and input data for encryption + const unsigned char key[16] = "1234567890abcdef"; // 128-bit key (16 bytes) + unsigned char input[16] = "HelloAESExample!"; // 16-byte data to be encrypted + unsigned char output[16]; // Buffer to store encryption result + unsigned char decrypted[16]; // Buffer to store decryption result + + // Initialize AES encryption context + mbedtls_aes_context aes; + mbedtls_aes_init(&aes); + + // Set the encryption key + if (mbedtls_aes_setkey_enc(&aes, key, 128) != 0) { + printf("Failed to set AES encryption key\n"); + return -1; + } + + // Perform AES encryption + if (mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, input, output) != 0) { + printf("AES encryption failed\n"); + return -1; + } + + // Print the encrypted data + printf("Encrypted data: "); + for (int i = 0; i < 16; i++) { + printf("%02X ", output[i]); + } + printf("\n"); + + // Set the decryption key + if (mbedtls_aes_setkey_dec(&aes, key, 128) != 0) { + printf("Failed to set AES decryption key\n"); + return -1; + } + + // Perform AES decryption + if (mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, output, decrypted) != 0) { + printf("AES decryption failed\n"); + return -1; + } + + // Print the decrypted data + printf("Decrypted data: "); + for (int i = 0; i < 16; i++) { + printf("%c", decrypted[i]); + } + printf("\n"); + + // Free AES context + mbedtls_aes_free(&aes); + + return 0; +} diff --git a/github_source/source-list.sh b/github_source/source-list.sh index e26d943..cdf65d6 100755 --- a/github_source/source-list.sh +++ b/github_source/source-list.sh @@ -50,6 +50,7 @@ git_clone_and_checkout_commit https://github.com/simdjson/simdjson.git 9b314922a git_clone_and_checkout_commit https://github.com/abseil/abseil-cpp.git ce986ae569c3b11ce2f2e53ed00c0b597bdfed58 git_clone_and_checkout_commit https://github.com/google/flatbuffers.git 1c514626e83c20fffa8557e75641848e1e15cd5e git_clone_and_checkout_commit https://github.com/mongodb/mongo-c-driver.git 915277362a01f7304a6e119fb7c2030f5401dca9 +git_clone_and_checkout_commit https://github.com/Mbed-TLS/mbedtls.git 107ea89daaefb9867ea9121002fbbdf926780e98 # opencv-mobile-2.4.13.7-armlinux https://github.com/nihui/opencv-mobile/releases/download/v26/opencv-mobile-2.4.13.7-armlinux.zip