mirror of https://github.com/wolfSSL/wolfBoot.git
Merge pull request #344 from danielinux/fix_windows_keytools
Fixes to sign.c running on windowspull/348/head^2
commit
6bfc594a75
8
Makefile
8
Makefile
|
|
@ -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:=
|
||||
|
|
@ -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:
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.**
|
||||
|
|
|
|||
24
src/delta.c
24
src/delta.c
|
|
@ -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.
|
||||
|
|
@ -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.
|
||||
|
|
@ -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
|
||||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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 <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
|
@ -37,6 +41,9 @@
|
|||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <stddef.h>
|
||||
/* target.h is a generated file based on .config (see target.h.in)
|
||||
* Provides: WOLFBOOT_SECTOR_SIZE */
|
||||
#include <target.h>
|
||||
#include <delta.h>
|
||||
|
||||
#include "wolfboot/version.h"
|
||||
|
|
@ -45,11 +52,16 @@
|
|||
#include <io.h>
|
||||
#define HAVE_MMAP 0
|
||||
#define ftruncate(fd, len) _chsize(fd, len)
|
||||
static inline int fp_truncate(FILE *f, size_t len)
|
||||
{
|
||||
int fd;
|
||||
if (f == NULL)
|
||||
return -1;
|
||||
fd = _fileno(f);
|
||||
return _chsize_s(fd, len);
|
||||
}
|
||||
#else
|
||||
#define HAVE_MMAP 1
|
||||
#endif
|
||||
|
||||
#if HAVE_MMAP
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
|
@ -1009,7 +1021,7 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
|
|||
mp_clear(&r); mp_clear(&s);
|
||||
}
|
||||
#endif
|
||||
else if (CMD.sign == SIGN_RSA2048 ||
|
||||
else if (CMD.sign == SIGN_RSA2048 ||
|
||||
CMD.sign == SIGN_RSA3072 ||
|
||||
CMD.sign == SIGN_RSA4096) {
|
||||
|
||||
|
|
@ -1237,7 +1249,11 @@ static int make_header_delta(uint8_t *pubkey, uint32_t pubkey_sz,
|
|||
|
||||
static int base_diff(const char *f_base, uint8_t *pubkey, uint32_t pubkey_sz, int padding)
|
||||
{
|
||||
#if HAVE_MMAP
|
||||
int fd1 = -1, fd2 = -1, fd3 = -1;
|
||||
#else
|
||||
FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
|
||||
#endif
|
||||
int len1 = 0, len2 = 0, len3 = 0;
|
||||
struct stat st;
|
||||
void *base = NULL;
|
||||
|
|
@ -1266,31 +1282,35 @@ static int base_diff(const char *f_base, uint8_t *pubkey, uint32_t pubkey_sz, in
|
|||
goto cleanup;
|
||||
}
|
||||
|
||||
#if HAVE_MMAP
|
||||
/* Open base image */
|
||||
fd1 = open(f_base, O_RDWR);
|
||||
if (fd1 < 0) {
|
||||
printf("Cannot open file %s\n", f_base);
|
||||
goto cleanup;
|
||||
}
|
||||
#if HAVE_MMAP
|
||||
base = mmap(NULL, len1, PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);
|
||||
if (base == (void *)(-1)) {
|
||||
perror("mmap");
|
||||
goto cleanup;
|
||||
}
|
||||
#else
|
||||
f1 = fopen(f_base, "wb");
|
||||
if (f1 == NULL) {
|
||||
printf("Cannot open file %s\n", f_base);
|
||||
goto cleanup;
|
||||
}
|
||||
base = malloc(len1);
|
||||
if (base == NULL) {
|
||||
fprintf(stderr, "Error malloc for base %d\n", len1);
|
||||
goto cleanup;
|
||||
}
|
||||
if (len1 != read(fd1, base, len1)) {
|
||||
if (len1 != (int)fread(base, len1, 1, f1)) {
|
||||
perror("read of base");
|
||||
goto cleanup;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/* Check base image version */
|
||||
base_ver_p = strstr(f_base, "_v");
|
||||
if (base_ver_p) {
|
||||
|
|
@ -1313,6 +1333,7 @@ static int base_diff(const char *f_base, uint8_t *pubkey, uint32_t pubkey_sz, in
|
|||
printf("Delta base version: %u\n", delta_base_version);
|
||||
}
|
||||
|
||||
#if HAVE_MMAP
|
||||
/* Open second image file */
|
||||
fd2 = open(CMD.output_image_file, O_RDONLY);
|
||||
if (fd2 < 0) {
|
||||
|
|
@ -1325,23 +1346,11 @@ static int base_diff(const char *f_base, uint8_t *pubkey, uint32_t pubkey_sz, in
|
|||
goto cleanup;
|
||||
}
|
||||
len2 = st.st_size;
|
||||
#if HAVE_MMAP
|
||||
buffer = mmap(NULL, len2, PROT_READ, MAP_SHARED, fd2, 0);
|
||||
if (buffer == (void *)(-1)) {
|
||||
perror("mmap");
|
||||
goto cleanup;
|
||||
}
|
||||
#else
|
||||
buffer = malloc(len2);
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "Error malloc for buffer %d\n", len2);
|
||||
goto cleanup;
|
||||
}
|
||||
if (len2 != read(fd2, buffer, len2)) {
|
||||
perror("fread of buffer");
|
||||
goto cleanup;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Open output file */
|
||||
fd3 = open(wolfboot_delta_file, O_RDWR|O_CREAT|O_TRUNC, 0660);
|
||||
|
|
@ -1350,15 +1359,54 @@ static int base_diff(const char *f_base, uint8_t *pubkey, uint32_t pubkey_sz, in
|
|||
goto cleanup;
|
||||
}
|
||||
if (len2 <= 0) {
|
||||
printf("Invalid file size: %d\n", len2);
|
||||
goto cleanup;
|
||||
}
|
||||
lseek(fd3, MAX_SRC_SIZE -1, SEEK_SET);
|
||||
io_sz = write(fd3, &ff, 1);
|
||||
if (io_sz != 1) {
|
||||
printf("Could not write to output file: %s\n", strerror(errno));
|
||||
goto cleanup;
|
||||
}
|
||||
lseek(fd3, 0, SEEK_SET);
|
||||
len3 = 0;
|
||||
#else
|
||||
/* Open second image file */
|
||||
f2 = fopen(CMD.output_image_file, "rb");
|
||||
if (f2 == NULL) {
|
||||
printf("Cannot open file %s\n", CMD.output_image_file);
|
||||
goto cleanup;
|
||||
}
|
||||
/* Get second file size */
|
||||
fseek(f2, 0L, SEEK_END);
|
||||
len2 = ftell(f2);
|
||||
fseek(f2, 0L, SEEK_SET);
|
||||
buffer = malloc(len2);
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "Error malloc for buffer %d\n", len2);
|
||||
goto cleanup;
|
||||
}
|
||||
if (len2 != (int)fread(buffer, len2, 1, f2)) {
|
||||
perror("fread of buffer");
|
||||
goto cleanup;
|
||||
}
|
||||
/* Open output file */
|
||||
f3 = fopen(wolfboot_delta_file, "wb");
|
||||
if (f3 == NULL) {
|
||||
printf("Cannot open file %s for writing\n", wolfboot_delta_file);
|
||||
goto cleanup;
|
||||
}
|
||||
if (len2 <= 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
fseek(f3, MAX_SRC_SIZE -1, SEEK_SET);
|
||||
io_sz = (int)fwrite(&ff, 1, 1, f3);
|
||||
if (io_sz != 1) {
|
||||
goto cleanup;
|
||||
}
|
||||
fseek(f3, 0, SEEK_SET);
|
||||
len3 = 0;
|
||||
#endif
|
||||
|
||||
/* Direct base->second patch */
|
||||
if (wb_diff_init(&diff_ctx, base, len1, buffer, len2) < 0) {
|
||||
|
|
@ -1368,7 +1416,11 @@ static int base_diff(const char *f_base, uint8_t *pubkey, uint32_t pubkey_sz, in
|
|||
r = wb_diff(&diff_ctx, dest, blksz);
|
||||
if (r < 0)
|
||||
goto cleanup;
|
||||
#if HAVE_MMAP
|
||||
io_sz = write(fd3, dest, r);
|
||||
#else
|
||||
io_sz = (int)fwrite(dest, r, 1, f3);
|
||||
#endif
|
||||
if (io_sz != r) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
|
@ -1377,7 +1429,11 @@ static int base_diff(const char *f_base, uint8_t *pubkey, uint32_t pubkey_sz, in
|
|||
patch_sz = len3;
|
||||
while ((len3 % padding) != 0) {
|
||||
uint8_t zero = 0;
|
||||
#if HAVE_MMAP
|
||||
io_sz = write(fd3, &zero, 1);
|
||||
#else
|
||||
io_sz = (int)fwrite(&zero, 1, 1, f3);
|
||||
#endif
|
||||
if (io_sz != 1) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
|
@ -1394,57 +1450,75 @@ static int base_diff(const char *f_base, uint8_t *pubkey, uint32_t pubkey_sz, in
|
|||
r = wb_diff(&diff_ctx, dest, blksz);
|
||||
if (r < 0)
|
||||
goto cleanup;
|
||||
#if HAVE_MMAP
|
||||
io_sz = write(fd3, dest, r);
|
||||
#else
|
||||
io_sz = (int)fwrite(dest, r, 1, f3);
|
||||
#endif
|
||||
if (io_sz != r) {
|
||||
goto cleanup;
|
||||
}
|
||||
patch_inv_sz += r;
|
||||
len3 += r;
|
||||
} while (r > 0);
|
||||
ret = ftruncate(fd3, len3);
|
||||
#if HAVE_MMAP
|
||||
if (fd3 >= 0) {
|
||||
if (len3 > 0) {
|
||||
ret = ftruncate(fd3, len3);
|
||||
}
|
||||
close(fd3);
|
||||
fd3 = -1;
|
||||
}
|
||||
#else
|
||||
if (f3 != NULL) {
|
||||
if (len3 > 0) {
|
||||
ret = fp_truncate(f3, len3);
|
||||
}
|
||||
fclose(f3);
|
||||
f3 = NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
if (ret != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
close(fd3);
|
||||
fd3 = -1;
|
||||
printf("Successfully created output file %s\n", wolfboot_delta_file);
|
||||
|
||||
/* Create delta file, with header, from the resulting patch */
|
||||
ret = make_header_delta(pubkey, pubkey_sz, wolfboot_delta_file, CMD.output_diff_file,
|
||||
delta_base_version, patch_sz, patch_inv_off, patch_inv_sz);
|
||||
|
||||
cleanup:
|
||||
if (fd3 >= 0) {
|
||||
if (len3 > 0) {
|
||||
io_sz = ftruncate(fd3, len3);
|
||||
(void)io_sz; /* ignore failure */
|
||||
}
|
||||
close(fd3);
|
||||
fd3 = -1;
|
||||
}
|
||||
/* Unlink output file */
|
||||
unlink(wolfboot_delta_file);
|
||||
#if HAVE_MMAP
|
||||
/* Cleanup/close */
|
||||
if (fd2 >= 0) {
|
||||
if (len2 > 0) {
|
||||
#if HAVE_MMAP
|
||||
munmap(buffer, len2);
|
||||
#else
|
||||
free(buffer);
|
||||
#endif
|
||||
}
|
||||
close(fd2);
|
||||
}
|
||||
if (fd1 >= 0) {
|
||||
if (len1 > 0) {
|
||||
#if HAVE_MMAP
|
||||
munmap(base, len1);
|
||||
#else
|
||||
free(base);
|
||||
#endif
|
||||
}
|
||||
close(fd1);
|
||||
}
|
||||
#else
|
||||
if (f2 != NULL) {
|
||||
if (len2 > 0) {
|
||||
free(buffer);
|
||||
}
|
||||
fclose(f2);
|
||||
}
|
||||
if (f1 != NULL) {
|
||||
if (len1 > 0) {
|
||||
free(base);
|
||||
}
|
||||
fclose(f1);
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue