Skip to content

Commit cca6870

Browse files
committed
ed25519 tool: Added O_BINARY flag to open() for windows compatibility
1 parent 9e775a6 commit cca6870

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

tools/ed25519/ed25519_keygen.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#include <wolfssl/wolfcrypt/asn_public.h>
2828

2929
#define PEMSIZE 1024
30+
#ifndef WIN32
31+
# define O_BINARY O_RDONLY
32+
#endif
3033

3134
void print_buf(uint8_t *buf, int len)
3235
{
@@ -56,7 +59,7 @@ void create_pubkey_cfile(const char *fname, uint8_t *key_in)
5659
char buf[4192] = { };
5760
char keybyte[5] = {};
5861
int i;
59-
int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0660);
62+
int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0660);
6063
if (fd < 0) {
6164
perror("creating c file");
6265
exit(1);
@@ -124,7 +127,7 @@ int main(int argc, char *argv[])
124127
print_key(full);
125128
print_key(full + 32);
126129

127-
fd = open("ed25519.der", O_WRONLY|O_CREAT|O_TRUNC, 0600);
130+
fd = open("ed25519.der", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600);
128131
if (fd < 0) {
129132
perror("exporting key (der)");
130133
}
@@ -133,7 +136,7 @@ int main(int argc, char *argv[])
133136
memset(outkey, 0, PEMSIZE);
134137
wc_DerToPem(priv, outlen, outkey, PEMSIZE, ED25519_TYPE);
135138
printf("%s\n", outkey);
136-
fd = open("ed25519.pem", O_WRONLY|O_CREAT|O_TRUNC, 0600);
139+
fd = open("ed25519.pem", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600);
137140
if (fd < 0) {
138141
perror("exporting key (pem)");
139142
}
@@ -149,7 +152,7 @@ int main(int argc, char *argv[])
149152
memset(outkey, 0, PEMSIZE);
150153
wc_DerToPem(pub, 32, outkey, PEMSIZE, PUBLICKEY_TYPE);
151154
printf("%s\n", outkey);
152-
fd = open("ed25519_pub.pem", O_WRONLY|O_CREAT|O_TRUNC, 0660);
155+
fd = open("ed25519_pub.pem", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0660);
153156
if (fd < 0) {
154157
perror("creating key\n");
155158
}

tools/ed25519/ed25519_sign.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
*
2020
*/
2121
#include <stdint.h>
22+
#include <stdio.h>
2223
#include <fcntl.h>
24+
#ifndef WIN32
25+
# define O_BINARY 0
26+
#endif
2327

2428
#include <wolfssl/wolfcrypt/settings.h>
2529
#include <wolfssl/wolfcrypt/ed25519.h>
@@ -98,18 +102,18 @@ int main(int argc, char *argv[])
98102
strcpy(in_name, argv[1]);
99103
snprintf(signed_name, PATH_MAX, "%s.v%s.signed", argv[1], argv[3]);
100104

101-
in_fd = open(in_name, O_RDONLY);
105+
in_fd = open(in_name, O_RDONLY|O_BINARY);
102106
if (in_fd < 0) {
103107
perror(in_name);
104108
exit(2);
105109
}
106-
out_fd = open(signed_name, O_WRONLY|O_CREAT|O_TRUNC, 0660);
110+
out_fd = open(signed_name, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0660);
107111
if (out_fd < 0) {
108112
perror(signed_name);
109113
exit(2);
110114
}
111115

112-
key_fd = open(argv[2], O_RDONLY);
116+
key_fd = open(argv[2], O_RDONLY|O_BINARY);
113117
if (key_fd < 0) {
114118
perror(argv[2]);
115119
exit(2);
@@ -195,7 +199,10 @@ int main(int argc, char *argv[])
195199
print_buf(hdr, IMAGE_HEADER_SIZE);
196200

197201
/* Write header */
198-
write(out_fd, hdr, IMAGE_HEADER_SIZE);
202+
if (write(out_fd, hdr, IMAGE_HEADER_SIZE) != IMAGE_HEADER_SIZE) {
203+
perror("write");
204+
exit(1);
205+
}
199206

200207
/* Write image payload */
201208
lseek(in_fd, 0, SEEK_SET);
@@ -215,13 +222,14 @@ int main(int argc, char *argv[])
215222
if ((r == 0) && st.st_size < padsize) {
216223
size_t fill = padsize - st.st_size;
217224
uint8_t padbyte = 0xFF;
218-
out_fd = open(signed_name, O_WRONLY|O_APPEND|O_EXCL);
225+
out_fd = open(signed_name, O_WRONLY|O_APPEND|O_EXCL|O_BINARY);
219226
if (out_fd > 0) {
220227
while(fill--)
221228
write(out_fd, &padbyte, 1);
222229
}
223230
close(out_fd);
224231
}
232+
close(in_fd);
225233
exit(0);
226234
}
227235

0 commit comments

Comments
 (0)