From cca687054f9eeb88f2ef34afd2e40dc1fcb8e8a6 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Sat, 9 Feb 2019 10:54:09 +0100 Subject: [PATCH] ed25519 tool: Added O_BINARY flag to open() for windows compatibility --- tools/ed25519/ed25519_keygen.c | 11 +++++++---- tools/ed25519/ed25519_sign.c | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/tools/ed25519/ed25519_keygen.c b/tools/ed25519/ed25519_keygen.c index 5d613282..3f69f8b6 100644 --- a/tools/ed25519/ed25519_keygen.c +++ b/tools/ed25519/ed25519_keygen.c @@ -27,6 +27,9 @@ #include #define PEMSIZE 1024 +#ifndef WIN32 +# define O_BINARY O_RDONLY +#endif void print_buf(uint8_t *buf, int len) { @@ -56,7 +59,7 @@ void create_pubkey_cfile(const char *fname, uint8_t *key_in) char buf[4192] = { }; char keybyte[5] = {}; int i; - int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0660); + int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0660); if (fd < 0) { perror("creating c file"); exit(1); @@ -124,7 +127,7 @@ int main(int argc, char *argv[]) print_key(full); print_key(full + 32); - fd = open("ed25519.der", O_WRONLY|O_CREAT|O_TRUNC, 0600); + fd = open("ed25519.der", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600); if (fd < 0) { perror("exporting key (der)"); } @@ -133,7 +136,7 @@ int main(int argc, char *argv[]) memset(outkey, 0, PEMSIZE); wc_DerToPem(priv, outlen, outkey, PEMSIZE, ED25519_TYPE); printf("%s\n", outkey); - fd = open("ed25519.pem", O_WRONLY|O_CREAT|O_TRUNC, 0600); + fd = open("ed25519.pem", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600); if (fd < 0) { perror("exporting key (pem)"); } @@ -149,7 +152,7 @@ int main(int argc, char *argv[]) memset(outkey, 0, PEMSIZE); wc_DerToPem(pub, 32, outkey, PEMSIZE, PUBLICKEY_TYPE); printf("%s\n", outkey); - fd = open("ed25519_pub.pem", O_WRONLY|O_CREAT|O_TRUNC, 0660); + fd = open("ed25519_pub.pem", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0660); if (fd < 0) { perror("creating key\n"); } diff --git a/tools/ed25519/ed25519_sign.c b/tools/ed25519/ed25519_sign.c index 8676a7d1..fadfae26 100644 --- a/tools/ed25519/ed25519_sign.c +++ b/tools/ed25519/ed25519_sign.c @@ -19,7 +19,11 @@ * */ #include +#include #include +#ifndef WIN32 +# define O_BINARY 0 +#endif #include #include @@ -98,18 +102,18 @@ int main(int argc, char *argv[]) strcpy(in_name, argv[1]); snprintf(signed_name, PATH_MAX, "%s.v%s.signed", argv[1], argv[3]); - in_fd = open(in_name, O_RDONLY); + in_fd = open(in_name, O_RDONLY|O_BINARY); if (in_fd < 0) { perror(in_name); exit(2); } - out_fd = open(signed_name, O_WRONLY|O_CREAT|O_TRUNC, 0660); + out_fd = open(signed_name, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0660); if (out_fd < 0) { perror(signed_name); exit(2); } - key_fd = open(argv[2], O_RDONLY); + key_fd = open(argv[2], O_RDONLY|O_BINARY); if (key_fd < 0) { perror(argv[2]); exit(2); @@ -195,7 +199,10 @@ int main(int argc, char *argv[]) print_buf(hdr, IMAGE_HEADER_SIZE); /* Write header */ - write(out_fd, hdr, IMAGE_HEADER_SIZE); + if (write(out_fd, hdr, IMAGE_HEADER_SIZE) != IMAGE_HEADER_SIZE) { + perror("write"); + exit(1); + } /* Write image payload */ lseek(in_fd, 0, SEEK_SET); @@ -215,13 +222,14 @@ int main(int argc, char *argv[]) if ((r == 0) && st.st_size < padsize) { size_t fill = padsize - st.st_size; uint8_t padbyte = 0xFF; - out_fd = open(signed_name, O_WRONLY|O_APPEND|O_EXCL); + out_fd = open(signed_name, O_WRONLY|O_APPEND|O_EXCL|O_BINARY); if (out_fd > 0) { while(fill--) write(out_fd, &padbyte, 1); } close(out_fd); } + close(in_fd); exit(0); }