1
- #ifdef _MSC_VER
1
+ #ifdef _WIN32
2
2
#define _CRT_SECURE_NO_WARNINGS
3
- #endif
3
+
4
+ #include <io.h>
5
+ #include <fcntl.h>
6
+
7
+ #else
4
8
5
9
#ifndef _FILE_OFFSET_BITS
6
10
#define _FILE_OFFSET_BITS 64
7
11
#endif
8
12
9
- #define _CRT_SECURE_NO_WARNINGS
10
-
11
- #ifdef _WIN32
12
- #define xstat _stat64
13
- #else
14
- #define xstat stat
15
-
16
13
#endif
17
14
15
+
18
16
#define ADsize 20
19
17
20
18
#include <stdio.h>
@@ -57,11 +55,6 @@ int main(int argc, char **argv) {
57
55
58
56
int retcode ;
59
57
60
-
61
- struct xstat sbk ;
62
- struct xstat sbpt ;
63
- struct xstat sbct ;
64
-
65
58
// encryption mode args:{plaintext filename}
66
59
if (argc == 2 ) {
67
60
strcpy (ptstr , argv [1 ]);
@@ -72,15 +65,25 @@ int main(int argc, char **argv) {
72
65
73
66
#if defined _WIN32
74
67
68
+ int fd ;
69
+ _sopen_s (& fd , "foo.bin" , _O_RDONLY , _SH_DENYRW , _S_IREAD );
70
+
75
71
err = fopen_s (& key_file , kstr , "wb" );
76
72
if (err != 0 ) perror ("The key file was not opened" );
77
73
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
+
78
80
err = fopen_s (& input_file , ptstr , "rb" );
79
81
if (err != 0 ) perror ("The input file was not opened" );
80
82
81
83
err = fopen_s (& output_file , ctstr , "wb" );
82
84
if (err != 0 ) perror ("The output was not opened" );
83
85
86
+
84
87
#else
85
88
86
89
key_file = fopen (kstr , "wb" );
@@ -93,21 +96,12 @@ int main(int argc, char **argv) {
93
96
if (!output_file ) perror ("The output was not opened" );
94
97
if (!key_file || !input_file || !output_file ) return -1 ;
95
98
96
- #endif
99
+ off_t ptsz = ftello ( input_file );
97
100
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
108
102
109
- plaintext = malloc (sbpt . st_size );
110
- ciphertext = malloc (sbpt . st_size + tagsize );
103
+ plaintext = malloc (ptsz );
104
+ ciphertext = malloc (ptsz + tagsize );
111
105
// keyfile is: key||nonce
112
106
113
107
size_t wholekeysize = keysize + noncesize ;
@@ -121,14 +115,14 @@ int main(int argc, char **argv) {
121
115
memcpy (nonce , key + keysize , 16 );
122
116
123
117
#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 ),
125
119
input_file );
126
120
#else
127
- fread (plaintext , sizeof (unsigned char ), sbpt . st_size , input_file );
121
+ fread (plaintext , sizeof (unsigned char ), ptsz , input_file );
128
122
#endif
129
123
130
124
unsigned long long clen ;
131
- retcode = crypto_aead_encrypt (ciphertext , & clen , plaintext , sbpt . st_size ,
125
+ retcode = crypto_aead_encrypt (ciphertext , & clen , plaintext , ptsz ,
132
126
AD , ADsize , 0 , nonce , key );
133
127
if (retcode != 0 ) {
134
128
perror ("!!! crypto_aead_encrypt() did not return 0.\n" );
@@ -143,6 +137,14 @@ int main(int argc, char **argv) {
143
137
strncpy (ptstr , ctstr , strlen (argv [2 ]) + 1 - sizeof (".Keyak" ));
144
138
145
139
#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
+
146
148
err = fopen_s (& key_file , kstr , "rb" );
147
149
if (err != 0 ) perror ("Key file was not opened" );
148
150
@@ -161,44 +163,36 @@ int main(int argc, char **argv) {
161
163
output_file = fopen (ptstr , "wb" );
162
164
if (!output_file ) perror ("The output was not opened" );
163
165
if (!key_file || !input_file || !output_file ) return -1 ;
164
- #endif
165
166
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 );
171
169
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
176
171
177
- plaintext = malloc (sbct . st_size - tagsize );
178
- ciphertext = malloc (sbct . st_size );
172
+ plaintext = malloc (ctsz - tagsize );
173
+ ciphertext = malloc (ctsz );
179
174
// keyfile is: key||nonce
180
175
key = malloc (keysize + noncesize );
181
176
182
177
#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 );
186
180
#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 );
189
183
#endif
190
184
191
185
memcpy (nonce , key + keysize , 16 );
192
186
193
187
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 ,
195
189
AD , ADsize , nonce , key );
196
190
if (retcode != 0 ) {
197
191
perror ("!!! crypto_aead_decrypt() did not return 0" );
198
192
exit (EXIT_FAILURE );
199
193
}
200
194
201
- if (mlen != sbct . st_size - tagsize ) {
195
+ if (mlen != ctsz - tagsize ) {
202
196
perror ("!!! plaintext length mistach." );
203
197
exit (EXIT_FAILURE );
204
198
}
0 commit comments