blob: 9a5d2684945f0dab87c2c735a71f450865633574 [file] [log] [blame]
Dees_Troy83bd4832013-05-04 12:39:56 +00001/*
2 * ---------------------------------------------------------------------------
3 * OpenAES License
4 * ---------------------------------------------------------------------------
5 * Copyright (c) 2012, Nabil S. Al Ramli, www.nalramli.com
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 * ---------------------------------------------------------------------------
29 */
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#define OAES_DEBUG 1
36#include "../inc/oaes_lib.h"
37
38#if defined(_WIN32) && !defined(__SYMBIAN32__)
39#include <io.h>
40#else
41__inline static int setmode(int a, int b)
42{
43 return 0;
44}
45#endif
46
47#ifndef __max
48 #define __max(a,b) (((a) > (b)) ? (a) : (b))
49#endif // __max
50
51#ifndef __min
52 #define __min(a,b) (((a) < (b)) ? (a) : (b))
53#endif // __min
54
55#define OAES_BUF_LEN_ENC 4096 - 2 * OAES_BLOCK_SIZE
56#define OAES_BUF_LEN_DEC 4096
57
58static void usage( const char * exe_name )
59{
60 if( NULL == exe_name )
61 return;
62
63 fprintf( stderr,
64 "Usage:\n"
65 " %s <command> --key <key_data> [options]\n"
66 "\n"
67 " command:\n"
68 " enc: encrypt\n"
69 " dec: decrypt\n"
70 "\n"
71 " options:\n"
72 " --ecb: use ecb mode instead of cbc\n"
73 " --in <path_in>\n"
74 " --out <path_out>\n"
75 "\n",
76 exe_name
77 );
78}
79
80int main(int argc, char** argv)
81{
82 size_t _i = 0, _j = 0;
83 OAES_CTX * ctx = NULL;
84 uint8_t _buf_in[OAES_BUF_LEN_DEC];
85 uint8_t *_buf_out = NULL, _key_data[32] = "";
86 size_t _buf_in_len = 0, _buf_out_len = 0, _read_len = 0;
87 size_t _key_data_len = 0;
88 short _is_ecb = 0;
89 char *_file_in = NULL, *_file_out = NULL;
90 int _op = 0;
91 FILE *_f_in = stdin, *_f_out = stdout;
92
93 fprintf( stderr, "\n"
94 "*******************************************************************************\n"
95 "* OpenAES %-10s *\n"
96 "* Copyright (c) 2012, Nabil S. Al Ramli, www.nalramli.com *\n"
97 "*******************************************************************************\n\n",
98 OAES_VERSION );
99
100 // pad the key
101 for( _j = 0; _j < 32; _j++ )
102 _key_data[_j] = _j + 1;
103
104 if( argc < 2 )
105 {
106 usage( argv[0] );
107 return EXIT_FAILURE;
108 }
109
110 if( 0 == strcmp( argv[1], "enc" ) )
111 {
112 _op = 0;
113 _read_len = OAES_BUF_LEN_ENC;
114 }
115 else if( 0 == strcmp( argv[1], "dec" ) )
116 {
117 _op = 1;
118 _read_len = OAES_BUF_LEN_DEC;
119 }
120 else
121 {
122 fprintf(stderr, "Error: Unknown command '%s'.", argv[1]);
123 usage( argv[0] );
124 return EXIT_FAILURE;
125 }
126
127 for( _i = 2; _i < argc; _i++ )
128 {
129 int _found = 0;
130
131 if( 0 == strcmp( argv[_i], "--ecb" ) )
132 {
133 _found = 1;
134 _is_ecb = 1;
135 }
136
137 if( 0 == strcmp( argv[_i], "--key" ) )
138 {
139 _found = 1;
140 _i++; // key_data
141 if( _i >= argc )
142 {
143 fprintf(stderr, "Error: No value specified for '%s'.\n",
144 "--key");
145 usage( argv[0] );
146 return EXIT_FAILURE;
147 }
148 _key_data_len = strlen(argv[_i]);
149 if( 16 >= _key_data_len )
150 _key_data_len = 16;
151 else if( 24 >= _key_data_len )
152 _key_data_len = 24;
153 else
154 _key_data_len = 32;
155 memcpy(_key_data, argv[_i], __min(32, strlen(argv[_i])));
156 }
157
158 if( 0 == strcmp( argv[_i], "--in" ) )
159 {
160 _found = 1;
161 _i++; // path_in
162 if( _i >= argc )
163 {
164 fprintf(stderr, "Error: No value specified for '%s'.\n",
165 "--in");
166 usage( argv[0] );
167 return EXIT_FAILURE;
168 }
169 _file_in = argv[_i];
170 }
171
172 if( 0 == strcmp( argv[_i], "--out" ) )
173 {
174 _found = 1;
175 _i++; // path_out
176 if( _i >= argc )
177 {
178 fprintf(stderr, "Error: No value specified for '%s'.\n",
179 "--out");
180 usage( argv[0] );
181 return EXIT_FAILURE;
182 }
183 _file_out = argv[_i];
184 }
185
186 if( 0 == _found )
187 {
188 fprintf(stderr, "Error: Invalid option '%s'.\n", argv[_i]);
189 usage( argv[0] );
190 return EXIT_FAILURE;
191 }
192 }
193
194 if( 0 == _key_data_len )
195 {
196 fprintf(stderr, "Error: --key must be specified.\n");
197 return EXIT_FAILURE;
198 }
199
200 if( _file_in )
201 {
202 _f_in = fopen(_file_in, "rb");
203 if( NULL == _f_in )
204 {
205 fprintf(stderr,
206 "Error: Failed to open '-%s' for reading.\n", _file_in);
207 return EXIT_FAILURE;
208 }
209 }
210 else
211 {
212 if( setmode(fileno(stdin), 0x8000) < 0 )
213 fprintf(stderr,"Error: Failed in setmode().\n");
214 _f_in = stdin;
215 }
216
217 if( _file_out )
218 {
219 _f_out = fopen(_file_out, "wb");
220 if( NULL == _f_out )
221 {
222 fprintf(stderr,
223 "Error: Failed to open '-%s' for writing.\n", _file_out);
224 if( _file_in )
225 fclose(_f_in);
226 return EXIT_FAILURE;
227 }
228 }
229 else
230 {
231 if( setmode(fileno(stdout), 0x8000) < 0 )
232 fprintf(stderr, "Error: Failed in setmode().\n");
233 _f_out = stdout;
234 }
235
236 ctx = oaes_alloc();
237 if( NULL == ctx )
238 {
239 fprintf(stderr, "Error: Failed to initialize OAES.\n");
240 if( _file_in )
241 fclose(_f_in);
242 if( _file_out )
243 fclose(_f_out);
244 return EXIT_FAILURE;
245 }
246 if( _is_ecb )
247 if( OAES_RET_SUCCESS != oaes_set_option( ctx, OAES_OPTION_ECB, NULL ) )
248 fprintf(stderr, "Error: Failed to set OAES options.\n");
249
250 oaes_key_import_data( ctx, _key_data, _key_data_len );
251
252 while( _buf_in_len =
253 fread(_buf_in, sizeof(uint8_t), _read_len, _f_in) )
254 {
255 switch(_op)
256 {
257 case 0:
258 if( OAES_RET_SUCCESS != oaes_encrypt( ctx,
259 _buf_in, _buf_in_len, NULL, &_buf_out_len ) )
260 fprintf( stderr,
261 "Error: Failed to retrieve required buffer size for "
262 "encryption.\n" );
263 _buf_out = (uint8_t *) calloc( _buf_out_len, sizeof( char ) );
264 if( NULL == _buf_out )
265 {
266 fprintf(stderr, "Error: Failed to allocate memory.\n" );
267 if( _file_in )
268 fclose(_f_in);
269 if( _file_out )
270 fclose(_f_out);
271 return EXIT_FAILURE;
272 }
273 if( OAES_RET_SUCCESS != oaes_encrypt( ctx,
274 _buf_in, _buf_in_len, _buf_out, &_buf_out_len ) )
275 fprintf(stderr, "Error: Encryption failed.\n");
276 fwrite(_buf_out, sizeof(uint8_t), _buf_out_len, _f_out);
277 free(_buf_out);
278 break;
279 case 1:
280 if( OAES_RET_SUCCESS != oaes_decrypt( ctx,
281 _buf_in, _buf_in_len, NULL, &_buf_out_len ) )
282 fprintf( stderr,
283 "Error: Failed to retrieve required buffer size for "
284 "encryption.\n" );
285 _buf_out = (uint8_t *) calloc( _buf_out_len, sizeof( char ) );
286 if( NULL == _buf_out )
287 {
288 fprintf(stderr, "Error: Failed to allocate memory.\n" );
289 free( _buf_out );
290 if( _file_in )
291 fclose(_f_in);
292 if( _file_out )
293 fclose(_f_out);
294 return EXIT_FAILURE;
295 }
296 if( OAES_RET_SUCCESS != oaes_decrypt( ctx,
297 _buf_in, _buf_in_len, _buf_out, &_buf_out_len ) )
298 fprintf(stderr, "Error: Decryption failed.\n");
299 fwrite(_buf_out, sizeof(uint8_t), _buf_out_len, _f_out);
300 free(_buf_out);
301 break;
302 default:
303 break;
304 }
305 }
306
307
308 if( OAES_RET_SUCCESS != oaes_free( &ctx ) )
309 fprintf(stderr, "Error: Failed to uninitialize OAES.\n");
310
311 if( _file_in )
312 fclose(_f_in);
313 if( _file_out )
314 fclose(_f_out);
315
316 return (EXIT_SUCCESS);
317}