Skip to content

Commit 1167316

Browse files
committed
Improve user settings detection
1 parent c02c273 commit 1167316

File tree

7 files changed

+146
-21
lines changed

7 files changed

+146
-21
lines changed

include/user_settings.h

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,52 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2424
*/
25-
#ifndef _WOLFBOOT_USER_SETTINGS_H_
26-
#define _WOLFBOOT_USER_SETTINGS_H_
25+
#ifndef WOLFBOOT_USER_SETTINGS_H
26+
#define WOLFBOOT_USER_SETTINGS_H
27+
28+
/* This is the wolfBoot embedded target user settings.
29+
*
30+
* See also settings in [WOLFBOOT_ROOT]/tools/keytools
31+
*
32+
* When in question, define WOLFBOOT_SHOW_INCLUDE
33+
*/
34+
35+
#ifdef WOLFBOOT_SHOW_INCLUDE
36+
#ifdef __GNUC__ /* GCC compiler */
37+
#pragma message "===============include/user_settings.h"
38+
#elif defined(_MSC_VER) /* Microsoft Visual C++ compiler */
39+
#pragma message("===============include/user_settings.h")
40+
#else
41+
#warning "===============include/user_settings.h"
42+
#endif
43+
#endif
44+
45+
//#define DEBUG_SIGNTOOL
46+
//#define WOLFSSL_USE_ALIGN
47+
48+
#if defined(_MSC_VER)
49+
/* MSVC and clang-cl both define _MSC_VER */
50+
#ifndef WOLFSSL_HAVE_MIN
51+
#define WOLFSSL_HAVE_MIN
52+
#endif
53+
#ifndef WOLFSSL_HAVE_MAX
54+
#define WOLFSSL_HAVE_MAX
55+
#endif
56+
57+
/* Really keep Windows headers from redefining min/max */
58+
#ifndef NOMINMAX
59+
#define NOMINMAX 1
60+
#endif
61+
#endif
2762

2863
#ifdef WOLFBOOT_PKCS11_APP
2964
# include "test-app/wcs/user_settings.h"
3065
#else
3166

67+
/* The target.h is a device-specific, typically a generated file.
68+
* CMake configures from `include/target.h.in` into ${CMAKE_CURRENT_BINARY_DIR}
69+
*
70+
* See also the sample in [WOLFBOOT_ROOT]/tools/unit-tests/target.h */
3271
#include <target.h>
3372

3473
/* System */
@@ -330,7 +369,9 @@ extern int tolower(int c);
330369

331370
/* SP Math needs to understand long long */
332371
# ifndef ULLONG_MAX
333-
# define ULLONG_MAX 18446744073709551615ULL
372+
# ifndef _MSC_VER
373+
# define ULLONG_MAX 18446744073709551615ULL
374+
# endif
334375
# endif
335376
#endif
336377

@@ -495,7 +536,6 @@ extern int tolower(int c);
495536
# define WOLFSSL_HAVE_MAX
496537
#endif
497538

498-
499539
/* Memory model */
500540
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
501541
/* Disable VLAs */
@@ -586,4 +626,4 @@ extern int tolower(int c);
586626
# define WOLFSSL_PEM_TO_DER
587627
#endif
588628

589-
#endif /* !_WOLFBOOT_USER_SETTINGS_H_ */
629+
#endif /* !WOLFBOOT_USER_SETTINGS_H */

include/wolfboot/wolfboot.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ extern "C" {
163163
#if defined(__WOLFBOOT) || defined(UNIT_TEST_AUTH)
164164

165165
#include "wolfssl/wolfcrypt/settings.h"
166+
#ifdef WOLFBOOT_KEYTOOLS_USER_SETTINGS_H
167+
/* Encountered the user settings in [WOLFBOOT_ROOT]/tools/keytools/user_settings.h */
168+
#error "wolfBoot expects user settings from [WOLFBOOT_ROOT]/tools/keygen/user_settings.h"
169+
#endif
170+
#ifndef WOLFBOOT_USER_SETTINGS_H
171+
#error "wolfBoot expected user settings from [WOLFBOOT_ROOT]/include/user_settings.h"
172+
#endif
166173
#include "wolfssl/wolfcrypt/visibility.h"
167174
#include "wolfssl/wolfcrypt/wc_port.h"
168175
#include "wolfssl/wolfcrypt/types.h"
@@ -178,6 +185,7 @@ extern "C" {
178185

179186
/* Hashing configuration */
180187
#if defined(WOLFBOOT_HASH_SHA256)
188+
#define WOLFBOOT_KEYHASH_HAS_RESULT
181189
#include "wolfssl/wolfcrypt/sha256.h"
182190
# ifndef WOLFBOOT_SHA_BLOCK_SIZE
183191
# define WOLFBOOT_SHA_BLOCK_SIZE (256)

src/image.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
#endif
3232
#include <wolfssl/wolfcrypt/settings.h> /* for wolfCrypt hash/sign routines */
3333
#ifdef WOLFBOOT_KEYTOOLS
34-
/* this code needs to use the Use ./include/user_settings.h, not keytools */
34+
/* this code needs to use the local tools/keytools/user_settings.h
35+
* not [WOLFBOOT_ROOT]/include/user_settings.h */
3536
#error "The wrong user_settings.h has been included."
3637
#endif
3738

tools/keytools/keygen.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
//#define DEBUG_SIGNTOOL
2828

2929
#ifdef _WIN32
30-
#define _CRT_SECURE_NO_WARNINGS
31-
#define _CRT_NONSTDC_NO_DEPRECATE /* unlink */
30+
#define _CRT_SECURE_NO_WARNINGS
31+
#define _CRT_NONSTDC_NO_DEPRECATE /* unlink */
32+
#else
33+
#include <unistd.h>
3234
#endif
3335
#include <stdio.h>
3436
#include <stdint.h>
@@ -40,21 +42,32 @@
4042
#include <sys/types.h>
4143
#include <errno.h>
4244
#include <fcntl.h>
43-
#ifndef _WIN32
44-
#include <unistd.h>
45+
46+
/* wolfSSL */
47+
/* Always include wolfcrypt/settings.h before any other wolfSSL file. */
48+
/* Reminder: settings.h pulls in user_settings.h; don't include it here. */
49+
#include <wolfssl/wolfcrypt/settings.h>
50+
#ifdef WOLFBOOT_USER_SETTINGS_H
51+
#error "Keygen encountered unexpected user settings from [WOLFBOOT_ROOT]/include/user_settings.h"
52+
#endif
53+
#ifdef __WOLFBOOT
54+
/* wolfBoot otherwise uses a user_se*/
55+
#error "Keygen unexpectedly encountered __WOLFBOOT. Check your config"
56+
#endif
57+
#ifndef WOLFBOOT_KEYTOOLS_USER_SETTINGS_H
58+
#error "Keygen expects settings from [WOLFBOOT_ROOT]/tools/keygen/user_settings.h"
4559
#endif
4660

4761
#include <wolfssl/wolfcrypt/settings.h>
4862
#ifndef NO_RSA
49-
#include <wolfssl/wolfcrypt/rsa.h>
63+
#include <wolfssl/wolfcrypt/rsa.h>
5064
#endif
5165
#ifdef HAVE_ECC
52-
#include <wolfssl/wolfcrypt/ecc.h>
53-
#include <wolfssl/wolfcrypt/asn.h>
54-
66+
#include <wolfssl/wolfcrypt/ecc.h>
67+
#include <wolfssl/wolfcrypt/asn.h>
5568
#endif
5669
#ifdef HAVE_ED25519
57-
#include <wolfssl/wolfcrypt/ed25519.h>
70+
#include <wolfssl/wolfcrypt/ed25519.h>
5871
#endif
5972

6073
#ifdef HAVE_ED448
@@ -86,11 +99,11 @@
8699
#include <wolfssl/wolfcrypt/random.h>
87100
#include <wolfssl/wolfcrypt/error-crypt.h>
88101
#ifdef DEBUG_SIGNTOOL
89-
#include <wolfssl/wolfcrypt/logging.h>
102+
#include <wolfssl/wolfcrypt/logging.h>
90103
#endif
91104

92105
#if !defined(PATH_MAX)
93-
#define PATH_MAX 256
106+
#define PATH_MAX 256
94107
#endif
95108

96109
#include "wolfboot/wolfboot.h"

tools/keytools/otp/otp-keystore-gen.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@
3636
#endif
3737

3838
#include "wolfboot/wolfboot.h"
39+
#ifdef __WOLFBOOT
40+
#ifndef WOLFBOOT_USER_SETTINGS_H
41+
#error "otp-keystore-gen encountered unexpected user settings, expected [WOLFBOOT_ROOT]/include/user_settings.h"
42+
#endif
43+
#ifdef WOLFBOOT_KEYTOOLS_USER_SETTINGS_H
44+
#error "Detected keytools user settings, expected [WOLFBOOT_ROOT]/include/user_settings.hh"
45+
#endif
46+
#else
47+
#ifdef WOLFBOOT_KEYTOOLS_USER_SETTINGS_H
48+
#error "Detected keytools user settings, otp-keystore-gen does not expect any user_settings.h"
49+
#endif
50+
#ifdef WOLFBOOT_USER_SETTINGS_H
51+
#error "Detected wolfboot user settings, otp-keystore-gen does not expect any user_settings.h"
52+
#endif
53+
#endif
54+
3955
#include "keystore.h"
4056
#include "otp_keystore.h"
4157

tools/keytools/sign.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2323
*/
2424

25+
/* Beware of wolfCrypt user settings in [WOLFBOOT_ROOT]/include/user_settings.h */
26+
2527
/* Option to enable sign tool debugging */
26-
/* Must also define DEBUG_WOLFSSL in user_settings.h */
28+
/* Must also define DEBUG_WOLFSSL in /tools/keytools/user_settings.h */
2729
//#define DEBUG_SIGNTOOL
2830

2931
#ifdef _WIN32
@@ -79,11 +81,22 @@ static inline int fp_truncate(FILE *f, size_t len)
7981
#endif
8082

8183
#include <wolfssl/wolfcrypt/settings.h>
84+
#ifdef WOLFBOOT_USER_SETTINGS_H
85+
#error "signing tool encountered unexpected user settings from [WOLFBOOT_ROOT]/include/user_settings.h"
86+
#endif
87+
#ifdef __WOLFBOOT
88+
/* wolfBoot otherwise uses a different user_settings */
89+
#error "signing tool unexpectedly encountered __WOLFBOOT. Check your config"
90+
#endif
91+
#ifndef WOLFBOOT_KEYTOOLS_USER_SETTINGS_H
92+
#error "signing tool expects settings from [WOLFBOOT_ROOT]/tools/keygen/user_settings.h"
93+
#endif
94+
8295
#include <wolfssl/wolfcrypt/asn.h>
8396
#include <wolfssl/wolfcrypt/aes.h>
8497

8598
#ifdef HAVE_CHACHA
86-
#include <wolfssl/wolfcrypt/chacha.h>
99+
#include <wolfssl/wolfcrypt/chacha.h>
87100
#endif
88101

89102

tools/keytools/user_settings.h

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,45 @@
2323
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2424
*/
2525

26-
#ifndef H_USER_SETTINGS_
27-
#define H_USER_SETTINGS_
26+
#ifndef WOLFBOOT_KEYTOOLS_USER_SETTINGS_H
27+
#define WOLFBOOT_KEYTOOLS_USER_SETTINGS_H
28+
29+
/* This is the keytools user settings.
30+
*
31+
* See also settings in [WOLFBOOT_ROOT]/include/user_settings.h
32+
*
33+
* When in question, define WOLFBOOT_SHOW_INCLUDE
34+
*/
35+
36+
#ifdef WOLFBOOT_SHOW_INCLUDE
37+
#ifdef __GNUC__ /* GCC compiler */
38+
#pragma message "============= keytools/user_settings.h"
39+
#elif defined(_MSC_VER) /* Microsoft Visual C++ compiler */
40+
#pragma message("============= keytools/user_settings.h")
41+
#else
42+
#warning "============= keytools/user_settings"
43+
#endif
44+
#endif
2845

2946
#include <stdint.h>
3047

48+
/* #define DEBUG_SIGNTOOL */
49+
50+
/* #define WOLFBOOT_HASH_SHA256 */
51+
/* #define WOLFBOOT_SIGN_ECC256 */
52+
53+
/* Only the lib-test needs some image size macros in Visual Studio. */
54+
/* See project file to identify IS_TEST_LIB_APP */
55+
#if defined(_MSC_VER) && defined(IS_TEST_LIB_APP)
56+
/* These should be defined in preprocessor section, here for backup: */
57+
#ifndef LMS_IMAGE_SIGNATURE_SIZE
58+
#define LMS_IMAGE_SIGNATURE_SIZE 4096
59+
#endif
60+
#ifndef XMSS_IMAGE_SIGNATURE_SIZE
61+
#define XMSS_IMAGE_SIGNATURE_SIZE 4096
62+
#endif
63+
#endif
64+
3165
/* System */
3266
#ifndef WOLFBOOT_KEYTOOLS
3367
#define WOLFBOOT_KEYTOOLS

0 commit comments

Comments
 (0)