Skip to content

Commit 7cd33a3

Browse files
authored
Merge pull request #358 from JacobBarthelmeh/cmake
add cmake example tie in
2 parents 1b85515 + 3283cf3 commit 7cd33a3

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

cmake/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
CMAKE_MINIMUM_REQUIRED(VERSION 3.16)
2+
project(wolfssl-example)
3+
4+
message("Example cmake project including wolfSSL and using user_settings.h")
5+
6+
# add global define to include user_settings.h
7+
add_compile_definitions(WOLFSSL_USER_SETTINGS)
8+
9+
set(BUILD_SHARED_LIBS OFF)
10+
set(WOLFSSL_EXAMPLES OFF)
11+
set(WOLFSSL_CRYPT_TESTS OFF)
12+
set(WOLFSSL_USER_SETTINGS ON)
13+
14+
if (CONFIG_BIG_ENDIAN)
15+
set(CMAKE_C_BYTE_ORDER BIG_ENDIAN)
16+
set(CMAKE_CXX_BYTE_OREDER BIG_ENDIAN)
17+
else ()
18+
set(CMAKE_C_BYTE_ORDER LITTLE_ENDIAN)
19+
set(CMAKE_CXX_BYTE_OREDER LITTLE_ENDIAN)
20+
endif()
21+
22+
include_directories(include)
23+
add_subdirectory(wolfssl)
24+
25+
target_link_libraries(wolfssl PRIVATE
26+
)
27+
28+
# add in our application
29+
add_executable(hash myApp.c)
30+
target_link_libraries(hash wolfssl)
31+

cmake/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
This is an example of adding the wolfSSL library as a subdirectory to a project
2+
and using cmake to build.
3+
4+
## Steps to build:
5+
6+
\# clone or download the wolfssl bundle and put it in the subdirectory wolfssl
7+
git clone https://github.com/wolfssl/wolfssl
8+
mkdir build
9+
cd build
10+
cmake .. -DCMAKE_C_FLAGS=-I../include/
11+
make
12+
./hash example_string

cmake/include/user_settings.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _USER_SETTINGS_INCLUDE
2+
#define _USER_SETTINGS_INCLUDE
3+
4+
#define ECC_TIMING_RESISTANT
5+
#define WC_RSA_BLINDING
6+
7+
//add your target specific defines here
8+
9+
#endif

cmake/myApp.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* myApp.c
2+
*
3+
* Copyright (C) 2006-2022 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
#include <wolfssl/wolfcrypt/settings.h> /* using user_settings.h for example */
23+
#include <wolfssl/wolfcrypt/sha256.h>
24+
#include <wolfssl/wolfcrypt/wc_port.h>
25+
#include <stdio.h>
26+
27+
28+
int main(int argc, char** argv)
29+
{
30+
wc_Sha256 sha;
31+
byte digest[WC_SHA256_DIGEST_SIZE];
32+
int i, ret;
33+
34+
if (argc != 2) {
35+
printf("%s <to be hashed>\n", argv[0]);
36+
return -1;
37+
}
38+
39+
ret = wolfCrypt_Init();
40+
if (ret == 0) {
41+
ret = wc_InitSha256(&sha);
42+
}
43+
if (ret == 0) {
44+
ret = wc_Sha256Update(&sha, (const byte*)argv[1],
45+
(word32)XSTRLEN(argv[1]));
46+
}
47+
if (ret == 0) {
48+
ret = wc_Sha256Final(&sha, digest);
49+
}
50+
51+
if (ret != 0) {
52+
printf("Error %d\n", ret);
53+
}
54+
55+
if (ret == 0) {
56+
printf("Hash in hex : ");
57+
for (i = 0; i < WC_SHA256_DIGEST_SIZE; i++) {
58+
printf("%02X", digest[i]);
59+
}
60+
printf("\n");
61+
}
62+
63+
wolfCrypt_Cleanup();
64+
return 0;
65+
}

0 commit comments

Comments
 (0)