From 82e110815f4075c484ddaad42720ebc3157061ad Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Thu, 29 Dec 2022 10:57:28 -0800 Subject: [PATCH 1/2] add cmake example tie in --- cmake/CMakeLists.txt | 31 +++++++++++++++++++ cmake/README.md | 12 ++++++++ cmake/include/user_settings.h | 9 ++++++ cmake/myApp.c | 56 +++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 cmake/CMakeLists.txt create mode 100644 cmake/README.md create mode 100644 cmake/include/user_settings.h create mode 100644 cmake/myApp.c diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt new file mode 100644 index 00000000..c2e4e23b --- /dev/null +++ b/cmake/CMakeLists.txt @@ -0,0 +1,31 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.16) +project(wolfssl-example) + +message("Example cmake project including wolfSSL and using user_settings.h") + +# add global define to include user_settings.h +add_compile_definitions(WOLFSSL_USER_SETTINGS) + +set(BUILD_SHARED_LIBS OFF) +set(WOLFSSL_EXAMPLES OFF) +set(WOLFSSL_CRYPT_TESTS OFF) +set(WOLFSSL_USER_SETTINGS ON) + +if (CONFIG_BIG_ENDIAN) + set(CMAKE_C_BYTE_ORDER BIG_ENDIAN) + set(CMAKE_CXX_BYTE_OREDER BIG_ENDIAN) +else () + set(CMAKE_C_BYTE_ORDER LITTLE_ENDIAN) + set(CMAKE_CXX_BYTE_OREDER LITTLE_ENDIAN) +endif() + +include_directories(include) +add_subdirectory(wolfssl) + +target_link_libraries(wolfssl PRIVATE +) + +# add in our application +add_executable(hash myApp.c) +target_link_libraries(hash wolfssl) + diff --git a/cmake/README.md b/cmake/README.md new file mode 100644 index 00000000..acee773f --- /dev/null +++ b/cmake/README.md @@ -0,0 +1,12 @@ +This is an example of adding the wolfSSL library as a subdirectory to a project +and using cmake to build. + +## Steps to build: + +\# clone or download the wolfssl bundle and put it in the subdirectory wolfssl +git clone https://github.com/wolfssl/wolfssl +mkdir build +cd build +cmake .. -DCMAKE_C_FLAGS=-I../include/ +make +./hash example_string diff --git a/cmake/include/user_settings.h b/cmake/include/user_settings.h new file mode 100644 index 00000000..34c87314 --- /dev/null +++ b/cmake/include/user_settings.h @@ -0,0 +1,9 @@ +#ifndef _USER_SETTINGS_INCLUDE +#define _USER_SETTINGS_INCLUDE + +#define ECC_TIMING_RESISTANT +#define WC_RSA_BLINDING + +//add your target specific defines here + +#endif diff --git a/cmake/myApp.c b/cmake/myApp.c new file mode 100644 index 00000000..de1cbafb --- /dev/null +++ b/cmake/myApp.c @@ -0,0 +1,56 @@ +/* myApp.c + * + * Copyright (C) 2006-2022 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 + */ + +#include /* using user_settings.h for example */ +#include +#include + + +int main(int argc, char** argv) +{ + wc_Sha256 sha; + byte digest[WC_SHA256_DIGEST_SIZE]; + int i, ret; + + if (argc != 2) { + printf("%s \n", argv[0]); + return -1; + } + + ret = wc_InitSha256(&sha); + if (ret == 0) + ret = wc_Sha256Update(&sha, argv[1], (word32)XSTRLEN(argv[1])); + if (ret == 0) + ret = wc_Sha256Final(&sha, digest); + + if (ret != 0) + printf("Error %d\n", ret); + + if (ret == 0) { + printf("Hash in hex : "); + for (i = 0; i < WC_SHA256_DIGEST_SIZE; i++) { + printf("%02X", digest[i]); + } + printf("\n"); + } + + return 0; +} From 3283cf347f9e091bcccde5e2383201250951b50d Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Thu, 29 Dec 2022 13:35:27 -0800 Subject: [PATCH 2/2] Add init/cleanup and cast arg input --- cmake/myApp.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/cmake/myApp.c b/cmake/myApp.c index de1cbafb..cd06bc30 100644 --- a/cmake/myApp.c +++ b/cmake/myApp.c @@ -21,6 +21,7 @@ #include /* using user_settings.h for example */ #include +#include #include @@ -35,14 +36,21 @@ int main(int argc, char** argv) return -1; } - ret = wc_InitSha256(&sha); - if (ret == 0) - ret = wc_Sha256Update(&sha, argv[1], (word32)XSTRLEN(argv[1])); - if (ret == 0) + ret = wolfCrypt_Init(); + if (ret == 0) { + ret = wc_InitSha256(&sha); + } + if (ret == 0) { + ret = wc_Sha256Update(&sha, (const byte*)argv[1], + (word32)XSTRLEN(argv[1])); + } + if (ret == 0) { ret = wc_Sha256Final(&sha, digest); + } - if (ret != 0) + if (ret != 0) { printf("Error %d\n", ret); + } if (ret == 0) { printf("Hash in hex : "); @@ -52,5 +60,6 @@ int main(int argc, char** argv) printf("\n"); } + wolfCrypt_Cleanup(); return 0; }