Skip to content

Commit

Permalink
Merge pull request #344 from danielinux/fix_windows_keytools
Browse files Browse the repository at this point in the history
Fixes to sign.c running on windows
  • Loading branch information
dgarske authored Aug 18, 2023
2 parents 701674b + fce1d53 commit 6bfc594
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 52 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include tools/config.mk
## Initializers
WOLFBOOT_ROOT?=$(PWD)
CFLAGS:=-D"__WOLFBOOT"
CFLAGS+=-Werror -Wextra
CFLAGS+=-Werror -Wextra -Wno-array-bounds
LSCRIPT:=config/target.ld
LSCRIPT_FLAGS:=
LDFLAGS:=
Expand Down Expand Up @@ -159,7 +159,7 @@ keytools_check: keytools FORCE

$(PRIVATE_KEY):
$(Q)$(MAKE) keytools_check
$(Q)(test $(SIGN) = NONE) || ($(KEYGEN_TOOL) $(KEYGEN_OPTIONS) -g $(PRIVATE_KEY)) || true
$(Q)(test $(SIGN) = NONE) || ("$(KEYGEN_TOOL)" $(KEYGEN_OPTIONS) -g $(PRIVATE_KEY)) || true
$(Q)(test $(SIGN) = NONE) && (echo "// SIGN=NONE" > src/keystore.c) || true

keytools:
Expand All @@ -174,8 +174,8 @@ tpmtools:

test-app/image_v1_signed.bin: $(BOOT_IMG)
@echo "\t[SIGN] $(BOOT_IMG)"
$(Q)(test $(SIGN) = NONE) || $(SIGN_TOOL) $(SIGN_OPTIONS) $(BOOT_IMG) $(PRIVATE_KEY) 1
$(Q)(test $(SIGN) = NONE) && $(SIGN_TOOL) $(SIGN_OPTIONS) $(BOOT_IMG) 1 || true
$(Q)(test $(SIGN) = NONE) || "$(SIGN_TOOL)" $(SIGN_OPTIONS) $(BOOT_IMG) $(PRIVATE_KEY) 1
$(Q)(test $(SIGN) = NONE) && "$(SIGN_TOOL)" $(SIGN_OPTIONS) $(BOOT_IMG) 1 || true

test-app/image.elf: wolfboot.elf
$(Q)$(MAKE) -C test-app WOLFBOOT_ROOT="$(WOLFBOOT_ROOT)" image.elf
Expand Down
2 changes: 2 additions & 0 deletions docs/Signing.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ If the C version of the key tools exists they will be used by wolfBoot's makefil

Use the `wolfBootSignTool.vcxproj` Visual Studio project to build the `sign.exe` and `keygen.exe` tools for use on Windows.

If you see any error about missing `target.h` this is a generated file based on your .config using the make process. It is needed for `WOLFBOOT_SECTOR_SIZE` used in delta updates.

### Python key tools

**Please note that the Python tools are deprecated and will be removed in future versions.**
Expand Down
24 changes: 12 additions & 12 deletions src/delta.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,19 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
int found;
uint8_t *pa, *pb;
uint16_t match_len;
uint32_t blk_start;
uint32_t p_off = 0;
uintptr_t blk_start;
uintptr_t p_off = 0;
if (ctx->off_b >= ctx->size_b)
return 0;
if (len < BLOCK_HDR_SIZE)
return -1;

while ((ctx->off_b + BLOCK_HDR_SIZE < ctx->size_b) && (len > p_off + BLOCK_HDR_SIZE)) {
uint32_t page_start = ctx->off_b / WOLFBOOT_SECTOR_SIZE;
uint32_t pa_start;
uintptr_t page_start = ctx->off_b / WOLFBOOT_SECTOR_SIZE;
uintptr_t pa_start;
found = 0;
if (p_off + BLOCK_HDR_SIZE > len)
return p_off;
return (int)p_off;

/* 'A' Patch base is valid for addresses in blocks ahead.
* For matching previous blocks, 'B' is used as base instead.
Expand All @@ -211,15 +211,15 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)

pa_start = (WOLFBOOT_SECTOR_SIZE + 1) * page_start;
pa = ctx->src_a + pa_start;
while (((uint32_t)(pa - ctx->src_a) < ctx->size_a ) && (p_off < len)) {
if ((uint32_t)(ctx->size_a - (pa - ctx->src_a)) < BLOCK_HDR_SIZE)
while (((uintptr_t)(pa - ctx->src_a) < (uintptr_t)ctx->size_a) && (p_off < len)) {
if ((uintptr_t)(ctx->size_a - (pa - ctx->src_a)) < BLOCK_HDR_SIZE)
break;
if ((ctx->size_b - ctx->off_b) < BLOCK_HDR_SIZE)
break;
if ((WOLFBOOT_SECTOR_SIZE - (ctx->off_b % WOLFBOOT_SECTOR_SIZE)) < BLOCK_HDR_SIZE)
break;
if ((memcmp(pa, (ctx->src_b + ctx->off_b), BLOCK_HDR_SIZE) == 0)) {
uint32_t b_start;
uintptr_t b_start;
/* Identical areas of BLOCK_HDR_SIZE bytes match between the images.
* initialize match_len; blk_start is the relative offset within
* the src image.
Expand Down Expand Up @@ -261,13 +261,13 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
}
if (!found) {
/* Try matching an earlier section in the resulting image */
uint32_t pb_end = page_start * WOLFBOOT_SECTOR_SIZE;
uintptr_t pb_end = page_start * WOLFBOOT_SECTOR_SIZE;
pb = ctx->src_b;
while (((uint32_t)(pb - ctx->src_b) < pb_end) && (p_off < len)) {
while (((uintptr_t)(pb - ctx->src_b) < pb_end) && (p_off < len)) {
/* Check image boundary */
if ((ctx->size_b - ctx->off_b) < BLOCK_HDR_SIZE)
break;
if ((uint32_t)(ctx->size_b - (pb - ctx->src_b)) < BLOCK_HDR_SIZE)
if ((uintptr_t)(ctx->size_b - (pb - ctx->src_b)) < BLOCK_HDR_SIZE)
break;

/* Don't try matching backwards if the distance between the two
Expand Down Expand Up @@ -334,7 +334,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
}
ctx->off_b++;
}
return (p_off);
return (int)p_off;
}

#endif /* DELTA_UPDATES */
6 changes: 5 additions & 1 deletion tools/keytools/keygen.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
/* Must also define DEBUG_WOLFSSL in user_settings.h */
//#define DEBUG_SIGNTOOL

#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE /* unlink */
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
Expand Down Expand Up @@ -554,7 +558,7 @@ static void key_import(uint32_t ktype, const char *fname)
exit(6);
}

readLen = fread(buf, 1, sizeof(buf), file);
readLen = (int)fread(buf, 1, sizeof(buf), file);

if (readLen <= 0) {
printf("Fatal error: could not find valid key in file %s\n", fname);
Expand Down
Loading

0 comments on commit 6bfc594

Please sign in to comment.