Skip to content

Commit cd0ba2c

Browse files
committed
Follow the CERT Secure C Coding Standard
1 parent ebfdbd2 commit cd0ba2c

File tree

1 file changed

+43
-49
lines changed

1 file changed

+43
-49
lines changed

src/main.c

+43-49
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
#ifdef _MSC_VER
1+
#ifdef _WIN32
22
#define _CRT_SECURE_NO_WARNINGS
3-
#endif
3+
4+
#include <io.h>
5+
#include <fcntl.h>
6+
7+
#else
48

59
#ifndef _FILE_OFFSET_BITS
610
#define _FILE_OFFSET_BITS 64
711
#endif
812

9-
#define _CRT_SECURE_NO_WARNINGS
10-
11-
#ifdef _WIN32
12-
#define xstat _stat64
13-
#else
14-
#define xstat stat
15-
1613
#endif
1714

15+
1816
#define ADsize 20
1917

2018
#include <stdio.h>
@@ -57,11 +55,6 @@ int main(int argc, char **argv) {
5755

5856
int retcode;
5957

60-
61-
struct xstat sbk;
62-
struct xstat sbpt;
63-
struct xstat sbct;
64-
6558
// encryption mode args:{plaintext filename}
6659
if (argc == 2) {
6760
strcpy(ptstr, argv[1]);
@@ -72,15 +65,25 @@ int main(int argc, char **argv) {
7265

7366
#if defined _WIN32
7467

68+
int fd;
69+
_sopen_s(&fd, "foo.bin", _O_RDONLY, _SH_DENYRW, _S_IREAD);
70+
7571
err = fopen_s(&key_file, kstr, "wb");
7672
if (err != 0) perror("The key file was not opened");
7773

74+
err = _sopen_s(&fd, ptstr, _O_RDONLY, _SH_DENYRW, _S_IREAD);
75+
if (err != 0) perror("The input file was not opened");
76+
__int64 ptsz = _filelengthi64(fd);
77+
err = _close(fd);
78+
if (err != 0) perror("Failed to closed the input file");
79+
7880
err = fopen_s(&input_file, ptstr, "rb");
7981
if (err != 0) perror("The input file was not opened");
8082

8183
err = fopen_s(&output_file, ctstr, "wb");
8284
if (err != 0) perror("The output was not opened");
8385

86+
8487
#else
8588

8689
key_file = fopen(kstr, "wb");
@@ -93,21 +96,12 @@ int main(int argc, char **argv) {
9396
if (!output_file) perror("The output was not opened");
9497
if (!key_file || !input_file || !output_file) return -1;
9598

96-
#endif
99+
off_t ptsz = ftello(input_file);
97100

98-
if (xstat(kstr, &sbk) == -1 || xstat(ptstr, &sbpt) == -1 ||
99-
xstat(ctstr, &sbct) == -1) {
100-
perror("stat");
101-
exit(EXIT_FAILURE);
102-
}
103-
104-
if (sbk.st_size != 0 || sbpt.st_size == 0 || sbct.st_size != 0) {
105-
perror("The key/ciphertext is not empty or the plaintext file is empty");
106-
exit(EXIT_FAILURE);
107-
}
101+
#endif
108102

109-
plaintext = malloc(sbpt.st_size);
110-
ciphertext = malloc(sbpt.st_size + tagsize);
103+
plaintext = malloc(ptsz);
104+
ciphertext = malloc(ptsz + tagsize);
111105
// keyfile is: key||nonce
112106

113107
size_t wholekeysize = keysize + noncesize;
@@ -121,14 +115,14 @@ int main(int argc, char **argv) {
121115
memcpy(nonce, key + keysize, 16);
122116

123117
#if defined _WIN32
124-
fread_s(plaintext, sbpt.st_size, sbpt.st_size, sizeof(unsigned char),
118+
fread_s(plaintext, ptsz, ptsz, sizeof(unsigned char),
125119
input_file);
126120
#else
127-
fread(plaintext, sizeof(unsigned char), sbpt.st_size, input_file);
121+
fread(plaintext, sizeof(unsigned char), ptsz, input_file);
128122
#endif
129123

130124
unsigned long long clen;
131-
retcode = crypto_aead_encrypt(ciphertext, &clen, plaintext, sbpt.st_size,
125+
retcode = crypto_aead_encrypt(ciphertext, &clen, plaintext, ptsz,
132126
AD, ADsize, 0, nonce, key);
133127
if (retcode != 0) {
134128
perror("!!! crypto_aead_encrypt() did not return 0.\n");
@@ -143,6 +137,14 @@ int main(int argc, char **argv) {
143137
strncpy(ptstr, ctstr, strlen(argv[2]) + 1 - sizeof(".Keyak"));
144138

145139
#if defined _WIN32
140+
141+
int fd;
142+
err = _sopen_s(&fd, ctstr, _O_RDONLY, _SH_DENYRW, _S_IREAD);
143+
if (err != 0) perror("The input file was not opened");
144+
__int64 ctsz = _filelengthi64(fd);
145+
err = _close(fd);
146+
if (err != 0) perror("Failed to closed the input file");
147+
146148
err = fopen_s(&key_file, kstr, "rb");
147149
if (err != 0) perror("Key file was not opened");
148150

@@ -161,44 +163,36 @@ int main(int argc, char **argv) {
161163
output_file = fopen(ptstr, "wb");
162164
if (!output_file) perror("The output was not opened");
163165
if (!key_file || !input_file || !output_file) return -1;
164-
#endif
165166

166-
if (stat(kstr, &sbk) == -1 || stat(ptstr, &sbpt) == -1 ||
167-
stat(ctstr, &sbct) == -1) {
168-
perror("stat");
169-
exit(EXIT_FAILURE);
170-
}
167+
off_t ksz = ftello(key_file);
168+
off_t ctsz = ftello(input_file);
171169

172-
if (sbk.st_size == 0 || sbct.st_size == 0 || sbpt.st_size != 0) {
173-
perror("The key/ciphertext is empty or the plaintext file is not empty");
174-
exit(EXIT_FAILURE);
175-
}
170+
#endif
176171

177-
plaintext = malloc(sbct.st_size - tagsize);
178-
ciphertext = malloc(sbct.st_size);
172+
plaintext = malloc(ctsz - tagsize);
173+
ciphertext = malloc(ctsz);
179174
// keyfile is: key||nonce
180175
key = malloc(keysize + noncesize);
181176

182177
#if defined _WIN32
183-
fread_s(key, sbk.st_size, sizeof(unsigned char), sbk.st_size, key_file);
184-
fread_s(ciphertext, sbct.st_size, sizeof(unsigned char), sbct.st_size,
185-
input_file);
178+
fread_s(key, 32, sizeof(unsigned char), 32, key_file);
179+
fread_s(ciphertext, ctsz, sizeof(unsigned char), ctsz, input_file);
186180
#else
187-
fread(key, sizeof(unsigned char), sbk.st_size, key_file);
188-
fread(ciphertext, sizeof(unsigned char), sbct.st_size, input_file);
181+
fread(key, sizeof(unsigned char), ksz, key_file);
182+
fread(ciphertext, sizeof(unsigned char), ctsz, input_file);
189183
#endif
190184

191185
memcpy(nonce, key + keysize, 16);
192186

193187
unsigned long long mlen;
194-
retcode = crypto_aead_decrypt(plaintext, &mlen, 0, ciphertext, sbct.st_size,
188+
retcode = crypto_aead_decrypt(plaintext, &mlen, 0, ciphertext, ctsz,
195189
AD, ADsize, nonce, key);
196190
if (retcode != 0) {
197191
perror("!!! crypto_aead_decrypt() did not return 0");
198192
exit(EXIT_FAILURE);
199193
}
200194

201-
if (mlen != sbct.st_size - tagsize) {
195+
if (mlen != ctsz - tagsize) {
202196
perror("!!! plaintext length mistach.");
203197
exit(EXIT_FAILURE);
204198
}

0 commit comments

Comments
 (0)