ed25519 tool: Added O_BINARY flag to open() for windows compatibility

pull/5/head
Daniele Lacamera 2019-02-09 10:54:09 +01:00
parent 9e775a637a
commit cca687054f
2 changed files with 20 additions and 9 deletions

View File

@ -27,6 +27,9 @@
#include <wolfssl/wolfcrypt/asn_public.h>
#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");
}

View File

@ -19,7 +19,11 @@
*
*/
#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#ifndef WIN32
# define O_BINARY 0
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/ed25519.h>
@ -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);
}