Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Espressif examples run with local wolfSSL (no setup!) #6018

Merged
merged 13 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
#
# Copyright (C) 2006-2023 wolfSSL Inc.
#
# This file is part of wolfSSL.
#
# wolfSSL is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# wolfSSL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
#
# cmake for wolfssl
#
cmake_minimum_required(VERSION 3.5)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWOLFSSL_USER_SETTINGS")
set(CMAKE_CURRENT_SOURCE_DIR ".")

# We are currently in [root]/IDE/Espressif/ESP-IDF/examples/wolfssl_test/components/wolfssl
# The root of wolfSSL is 7 directories up from here:
get_filename_component(WOLFSSL_ROOT "../../../../../../../" ABSOLUTE)

# Espressif may take several passes through this makefile. Check to see if we found IDF
string(COMPARE EQUAL "${PROJECT_SOURCE_DIR}" "" WOLFSSL_FOUND_IDF)

if($WOLFSSL_FOUND_IDF)
message(STATUS "IDF_PATH = $ENV{IDF_PATH}")
message(STATUS "WOLFSSL_ROOT = ${WOLFSSL_ROOT}")
message(STATUS "PROJECT_SOURCE_DIR = ${PROJECT_SOURCE_DIR}")
endif()

# get a list of all wolfcrypt assembly files; we'll exclude them as they don't target Xtensa
FILE(GLOB EXCLUDE_ASM *.S)
file(GLOB_RECURSE EXCLUDE_ASM ${CMAKE_SOURCE_DIR} "${WOLFSSL_ROOT}/wolfcrypt/src/*.S")

message(STATUS "EXCLUDE_ASM = ${EXCLUDE_ASM}")

set(INCLUDE_PATH ${WOLFSSL_ROOT})

set(COMPONENT_SRCDIRS "${WOLFSSL_ROOT}/src/"
"${WOLFSSL_ROOT}/wolfcrypt/src/"
"${WOLFSSL_ROOT}/wolfcrypt/src/port/Espressif/"
"${WOLFSSL_ROOT}/wolfcrypt/src/port/atmel/"
"${WOLFSSL_ROOT}/wolfcrypt/benchmark/"
"${WOLFSSL_ROOT}/wolfcrypt/test/"
)

set(COMPONENT_REQUIRES lwip)


# check to see if there's both a local copy and EDP-IDF copy of the wolfssl and/or wolfssh components
if( EXISTS "${CMAKE_HOME_DIRECTORY}/components/wolfssl/" AND EXISTS "$ENV{IDF_PATH}/components/wolfssl/" )
#
# wolfSSL found in both ESP-IDF and local project - needs to be resolved by user
#
message(STATUS "")
message(STATUS "WARNING: Found components/wolfssl in both local project and IDF_PATH")
message(STATUS "")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWOLFSSL_MULTI_INSTALL_WARNING")

else()
if(EXISTS "$ENV{IDF_PATH}/components/wolfssl/" )
#
# wolfSSL found in ESP-IDF components and is assumed to be already configured in user_settings.h via setup.
#
message(STATUS "")
message(STATUS "Using components/wolfssl in IDF_PATH = $ENV{IDF_PATH}")
message(STATUS "")
else()
#
# wolfSSL is not an ESP-IDF component. We need to now determine if it is local and if so if it is part of the wolfSSL repo
# or if wolfSSL is simply installed as a local component.
#
if( EXISTS "${CMAKE_HOME_DIRECTORY}/components/wolfssl/" )
#
# wolfSSL found in local project.
#
if( EXISTS "${CMAKE_HOME_DIRECTORY}/components/wolfssl/include/" )
message(STATUS "")
message(STATUS "Using installed project ./components/wolfssl in CMAKE_HOME_DIRECTORY = $ENV{CMAKE_HOME_DIRECTORY}")
message(STATUS "")
#
# Note we already checked above and confirmed there's not another wolfSSL installed in the ESP-IDF components.
#
# We won't do anything else here, as it will be assumed the original install completed successfully.
#
else()
#
# This is the developer repo mode. wolfSSL will be assume to be not installed to ESP-IDF nor local project
# In this configuration, we are likely running a wolfSSL example found directly in the repo.
#
message(STATUS "")
message(STATUS "Using developer repo ./components/wolfssl in CMAKE_HOME_DIRECTORY = $ENV{CMAKE_HOME_DIRECTORY}")
message(STATUS "")

message(STATUS "************************************************************************************************")
# When in developer mode, we are typically running wolfSSL examples such as benchmark or test directories.
# However, the as-cloned or distributed wolfSSL does not have the ./include/ directory, so we'll add it as needed.
#
# first check if there's a [root]/include/user_settings.h
if( EXISTS "${WOLFSSL_ROOT}/include/user_settings.h" )
message(STATUS "Found wolfSSL user_settings.h in ${WOLFSSL_ROOT}/include/user_settings.h")
else()
message(STATUS "Installing wolfSSL user_settings.h to ${WOLFSSL_ROOT}/include/user_settings.h")
file(COPY "${WOLFSSL_ROOT}/IDE/Espressif/ESP-IDF/user_settings.h" DESTINATION "${WOLFSSL_ROOT}/include/")
endif() # user_settings.h

# next check if there's a [root]/include/config.h
if( EXISTS "${WOLFSSL_ROOT}/include/config.h" )
message(STATUS "Found wolfSSL config.h in ${WOLFSSL_ROOT}/include/config.h")
else()
message(STATUS "Installing wolfSSL config.h to ${WOLFSSL_ROOT}/include/config.h")
file(COPY "${WOLFSSL_ROOT}/IDE/Espressif/ESP-IDF/dummy_config_h" DESTINATION "${WOLFSSL_ROOT}/include/")
file(RENAME "${WOLFSSL_ROOT}/include/dummy_config_h" "${WOLFSSL_ROOT}/include/config.h")
endif() # config.h
message(STATUS "************************************************************************************************")
message(STATUS "")
endif()

else()
# we did not find a ./components/wolfssl/include/ directory froim this pass of cmake.
if($WOLFSSL_FOUND_IDF)
message(STATUS "")
message(STATUS "WARNING: wolfSSL not found.")
message(STATUS "")
else()
# probably needs to be reparsed by Espressif
endif() # else we have not found ESP-IDF yet
endif() # else not a local wolfSSL component

endif() #else not an ESP-IDF component
endif() # else not local copy and EDP-IDF wolfSSL


# RTOS_IDF_PATH is typically:
# "/Users/{username}/Desktop/esp-idf/components/freertos/include/freertos"
# depending on the environment, we may need to swap backslashes with forward slashes
string(REPLACE "\\" "/" RTOS_IDF_PATH "$ENV{IDF_PATH}/components/freertos/include/freertos")

# ESP-IDF after version 4.4x has a different RTOS directory structure
string(REPLACE "\\" "/" RTOS_IDF_PATH5 "$ENV{IDF_PATH}/components/freertos/FreeRTOS-Kernel/include/freertos")

if(IS_DIRECTORY ${IDF_PATH}/components/freertos/FreeRTOS-Kernel/)
set(COMPONENT_ADD_INCLUDEDIRS
"."
"${WOLFSSL_ROOT}/include"
"${RTOS_IDF_PATH5}"
"${WOLFSSL_ROOT}"
)
else()

set(COMPONENT_ADD_INCLUDEDIRS
"."
"${WOLFSSL_ROOT}/include"
"${RTOS_IDF_PATH}"
"${WOLFSSL_ROOT}"
)
endif()

if(IS_DIRECTORY ${IDF_PATH}/components/cryptoauthlib)
list(APPEND COMPONENT_ADD_INCLUDEDIRS "../cryptoauthlib/lib")
endif()

set(COMPONENT_SRCEXCLUDE
"${WOLFSSL_ROOT}/src/bio.c"
"${WOLFSSL_ROOT}/src/conf.c"
"${WOLFSSL_ROOT}/src/misc.c"
"${WOLFSSL_ROOT}/src/pk.c"
"${WOLFSSL_ROOT}/src/ssl_misc.c" # included by ssl.c
"${WOLFSSL_ROOT}/src/x509.c"
"${WOLFSSL_ROOT}/src/x509_str.c"
"${WOLFSSL_ROOT}/wolfcrypt/src/evp.c"
"${WOLFSSL_ROOT}/wolfcrypt/src/misc.c"
"${EXCLUDE_ASM}"
)

register_component()

# some optional diagnostics
if (0)
get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
message(STATUS "")
message(STATUS "ALL VARIABLES BEGIN")
message(STATUS "")
foreach (_variableName ${_variableNames})
message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
message(STATUS "")
message(STATUS "ALL VARIABLES END")
message(STATUS "")
endif()

# check to see if there's both a local copy and EDP-IDF copy of the wolfssl components
if( EXISTS "${CMAKE_HOME_DIRECTORY}/components/wolfssl/" AND EXISTS "$ENV{IDF_PATH}/components/wolfssl/" )
message(STATUS "")
message(STATUS "WARNING: Found components/wolfssl in both local project and IDF_PATH")
message(STATUS "")
endif()
# end multiple component check


Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright (C) 2006-2023 wolfSSL Inc.
#
# This file is part of wolfSSL.
#
# wolfSSL is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# wolfSSL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
#
#
# Component Makefile
#

COMPONENT_ADD_INCLUDEDIRS := . ./include

COMPONENT_ADD_INCLUDEDIRS += "$ENV{IDF_PATH}/components/freertos/include/freertos"
# COMPONENT_ADD_INCLUDEDIRS += "$ENV{IDF_PATH}/soc/esp32s3/include/soc"

COMPONENT_SRCDIRS := src wolfcrypt/src
COMPONENT_SRCDIRS += wolfcrypt/src/port/Espressif
COMPONENT_SRCDIRS += wolfcrypt/src/port/atmel
COMPONENT_SRCDIRS += wolfcrypt/benchmark
COMPONENT_SRCDIRS += wolfcrypt/test

CFLAGS +=-DWOLFSSL_USER_SETTINGS

COMPONENT_OBJEXCLUDE := wolfcrypt/src/aes_asm.o
COMPONENT_OBJEXCLUDE += wolfcrypt/src/evp.o
COMPONENT_OBJEXCLUDE += wolfcrypt/src/misc.o
COMPONENT_OBJEXCLUDE += src/bio.o
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,57 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWOLFSSL_USER_SETTINGS")
set(COMPONENT_SRCS main.c)
set(COMPONENT_ADD_INCLUDEDIRS ".")

if( EXISTS "${CMAKE_HOME_DIRECTORY}/components/wolfssl/" AND EXISTS "$ENV{IDF_PATH}/components/wolfssl/" )
#
# wolfSSL found in both ESP-IDF and local project - needs to be resolved by user
#
message(STATUS "")
message(STATUS "WARNING: Found components/wolfssl in both local project and IDF_PATH")
message(STATUS "")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWOLFSSL_MULTI_INSTALL_WARNING")
endif()

register_component()

set (git_cmd "git")
set (git_rev_parse "rev-parse")
set (git_short "--short")
set (git_HEAD "HEAD")
set (git_show "show")
set (git_no_patch "--no-patch")
set (git_no_notes "--no-notes")
set (git_pretty "--pretty=\'\%cd\'" )

if(NOT CMAKE_BUILD_EARLY_EXPANSION)
# LIBWOLFSSL_VERSION_GIT_HASH
execute_process(COMMAND ${git_cmd} ${git_rev_parse} ${git_HEAD} OUTPUT_VARIABLE LIBWOLFSSL_VERSION_GIT_HASH_VAR RESULT_VARIABLE WOLFSSL_GIT_PEEK_RESULT ERROR_QUIET )
string(REPLACE "\n" "" LIBWOLFSSL_VERSION_GIT_HASH ${LIBWOLFSSL_VERSION_GIT_HASH_VAR})
string(COMPARE EQUAL "${WOLFSSL_GIT_PEEK_RESULT}" "0" WOLFSSL_FOUND_GIT_VALUE)
if(${WOLFSSL_FOUND_GIT_VALUE})
message(STATUS "Found LIBWOLFSSL_VERSION_GIT_HASH=${LIBWOLFSSL_VERSION_GIT_HASH}")
#idf_build_set_property(COMPILE_DEFINITIONS "LIBWOLFSSL_VERSION_GIT_HASH=\"${LIBWOLFSSL_VERSION_GIT_HASH_VAR1}\"" APPEND)
#idf_build_set_property(COMPILE_DEFINITIONS "LIBWOLFSSL_VERSION_GIT_HASH=\"${LIBWOLFSSL_VERSION_GIT_HASH_VAR}\"" APPEND)
add_definitions(-DLIBWOLFSSL_VERSION_GIT_HASH=\"${LIBWOLFSSL_VERSION_GIT_HASH}\")
endif()

# LIBWOLFSSL_VERSION_GIT_SHORT_HASH
execute_process(COMMAND ${git_cmd} ${git_rev_parse} ${git_short} ${git_HEAD} OUTPUT_VARIABLE LIBWOLFSSL_VERSION_GIT_SHORT_HASH_VAR RESULT_VARIABLE WOLFSSL_GIT_PEEK_RESULT ERROR_QUIET )
string(REPLACE "\n" "" LIBWOLFSSL_VERSION_GIT_SHORT_HASH ${LIBWOLFSSL_VERSION_GIT_SHORT_HASH_VAR})
string(COMPARE EQUAL "${WOLFSSL_GIT_PEEK_RESULT}" "0" WOLFSSL_FOUND_GIT_VALUE)
if(${WOLFSSL_FOUND_GIT_VALUE})
message(STATUS "Found LIBWOLFSSL_VERSION_GIT_SHORT_HASH=${LIBWOLFSSL_VERSION_GIT_SHORT_HASH}")
add_definitions(-DLIBWOLFSSL_VERSION_GIT_SHORT_HASH=\"${LIBWOLFSSL_VERSION_GIT_SHORT_HASH}\")
endif()

# LIBWOLFSSL_VERSION_GIT_SHORT_HASH
execute_process(COMMAND ${git_cmd} ${git_show} ${git_no_patch} ${git_no_notes} ${git_pretty} OUTPUT_VARIABLE LIBWOLFSSL_VERSION_GIT_HASH_DATE_VAR RESULT_VARIABLE WOLFSSL_GIT_PEEK_RESULT )
string(REPLACE "\n" "" LIBWOLFSSL_VERSION_GIT_HASH_DATE ${LIBWOLFSSL_VERSION_GIT_HASH_DATE_VAR})
string(COMPARE EQUAL "${WOLFSSL_GIT_PEEK_RESULT}" "0" WOLFSSL_FOUND_GIT_VALUE)
if(${WOLFSSL_FOUND_GIT_VALUE})
message(STATUS "Found LIBWOLFSSL_VERSION_GIT_HASH_DATE=${LIBWOLFSSL_VERSION_GIT_HASH_DATE}")
add_definitions(-DLIBWOLFSSL_VERSION_GIT_HASH_DATE=\"${LIBWOLFSSL_VERSION_GIT_HASH_DATE}\")
endif()
endif()

message(STATUS "")

51 changes: 49 additions & 2 deletions IDE/Espressif/ESP-IDF/examples/wolfssl_benchmark/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
#include "sdkconfig.h"

/* wolfSSL */
#include <wolfssl/wolfcrypt/settings.h>
#include <user_settings.h>
#include <wolfssl/version.h>
#ifndef WOLFSSL_ESPIDF
#warning "problem with wolfSSL user_settings. Check components/wolfssl/include"
#warning "problem with wolfSSL user_settings. Check components/wolfssl/include"
#endif

#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/types.h>
#include <wolfcrypt/benchmark/benchmark.h>

Expand Down Expand Up @@ -182,6 +183,51 @@ int construct_argv()
/* entry point */
void app_main(void)
{
ESP_LOGI(TAG, "--------------------------------------------------------");
ESP_LOGI(TAG, "--------------------------------------------------------");
ESP_LOGI(TAG, "---------------------- BEGIN MAIN ----------------------");
ESP_LOGI(TAG, "--------------------------------------------------------");
ESP_LOGI(TAG, "--------------------------------------------------------");
ESP_LOGI(TAG, "CONFIG_IDF_TARGET = %s", CONFIG_IDF_TARGET);
ESP_LOGI(TAG, "LIBWOLFSSL_VERSION_STRING = %s", LIBWOLFSSL_VERSION_STRING);

#if defined(WOLFSSL_MULTI_INSTALL_WARNING)
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "WARNING: Multiple wolfSSL installs found.");
ESP_LOGI(TAG, "Check ESP-IDF and local project [components] directory.");
ESP_LOGI(TAG, "");
#endif

#if defined(LIBWOLFSSL_VERSION_GIT_HASH)
ESP_LOGI(TAG, "LIBWOLFSSL_VERSION_GIT_HASH = %s", LIBWOLFSSL_VERSION_GIT_HASH);
#endif

#if defined(LIBWOLFSSL_VERSION_GIT_SHORT_HASH )
ESP_LOGI(TAG, "LIBWOLFSSL_VERSION_GIT_SHORT_HASH = %s", LIBWOLFSSL_VERSION_GIT_SHORT_HASH);
#endif

#if defined(LIBWOLFSSL_VERSION_GIT_HASH_DATE)
ESP_LOGI(TAG, "LIBWOLFSSL_VERSION_GIT_HASH_DATE = %s", LIBWOLFSSL_VERSION_GIT_HASH_DATE);
#endif


/* some interesting settings are target specific (ESP32, -C3, -S3, etc */
#if defined(CONFIG_IDF_TARGET_ESP32C3)
/* not available for C3 at this time */
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
ESP_LOGI(TAG, "CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ = %u MHz",
CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ
);
ESP_LOGI(TAG, "Xthal_have_ccount = %u", Xthal_have_ccount);
#else
ESP_LOGI(TAG, "CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ = %u MHz",
CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ
);
ESP_LOGI(TAG, "Xthal_have_ccount = %u", Xthal_have_ccount);
#endif

/* all platforms: stack high water mark check */
ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));
ESP_LOGI(TAG, "app_main CONFIG_BENCH_ARGV = %s", WOLFSSL_BENCH_ARGV);

/* when using atecc608a on esp32-wroom-32se */
Expand Down Expand Up @@ -209,6 +255,7 @@ void app_main(void)
/* wolfCrypt_Cleanup should always be called at completion,
** and is called in wolf_benchmark_task().
*/
ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));

/* after the test, we'll just wait */
while (1) {
Expand Down
Loading