summaryrefslogtreecommitdiffstats
path: root/openaes/src/oaes.c
blob: 203da5227ca3fae1f113f29b49170f5ec2d4f3cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* 
 * ---------------------------------------------------------------------------
 * OpenAES License
 * ---------------------------------------------------------------------------
 * Copyright (c) 2012, Nabil S. Al Ramli, www.nalramli.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *   - Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * ---------------------------------------------------------------------------
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define OAES_DEBUG 1
#include "../inc/oaes_lib.h"

#if defined(_WIN32) && !defined(__SYMBIAN32__)
#include <io.h>
#else
__inline static int setmode(int a __unused, int b __unused)
{
	return 0;
}
#endif

#ifndef __max
	#define __max(a,b)  (((a) > (b)) ? (a) : (b))
#endif // __max

#ifndef __min
	#define __min(a,b)  (((a) < (b)) ? (a) : (b))
#endif // __min

#define OAES_BUF_LEN_ENC 4096 - 2 * OAES_BLOCK_SIZE
#define OAES_BUF_LEN_DEC 4096

static void usage( const char * exe_name )
{
	if( NULL == exe_name )
		return;
	
	fprintf( stderr,
			"Usage:\n"
			"  %s <command> --key <key_data> [options]\n"
			"\n"
			"    command:\n"
			"      enc: encrypt\n"
			"      dec:  decrypt\n"
			"\n"
			"    options:\n"
			"      --ecb: use ecb mode instead of cbc\n"
			"      --in <path_in>\n"
			"      --out <path_out>\n"
			"\n",
			exe_name
	);
}

int main(int argc, char** argv)
{
	int _i = 0, _j = 0;
	OAES_CTX * ctx = NULL;
	uint8_t _buf_in[OAES_BUF_LEN_DEC];
	uint8_t *_buf_out = NULL, _key_data[32] = "";
	size_t _buf_in_len = 0, _buf_out_len = 0, _read_len = 0;
	size_t _key_data_len = 0;
	short _is_ecb = 0;
	char *_file_in = NULL, *_file_out = NULL;
	int _op = 0;
	FILE *_f_in = stdin, *_f_out = stdout;
	
	fprintf( stderr, "\n"
		"*******************************************************************************\n"
		"* OpenAES %-10s                                                          *\n"
		"* Copyright (c) 2012, Nabil S. Al Ramli, www.nalramli.com                     *\n"
		"*******************************************************************************\n\n",
		OAES_VERSION );

	// pad the key
	for( _j = 0; _j < 32; _j++ )
		_key_data[_j] = _j + 1;

	if( argc < 2 )
	{
		usage( argv[0] );
		return EXIT_FAILURE;
	}

	if( 0 == strcmp( argv[1], "enc" ) )
	{
		_op = 0;
		_read_len = OAES_BUF_LEN_ENC;
	}
	else if( 0 == strcmp( argv[1], "dec" ) )
	{
		_op = 1;
		_read_len = OAES_BUF_LEN_DEC;
	}
	else
	{
		fprintf(stderr, "Error: Unknown command '%s'.", argv[1]);
		usage( argv[0] );
		return EXIT_FAILURE;
	}

	for( _i = 2; _i < argc; _i++ )
	{
		int _found = 0;

		if( 0 == strcmp( argv[_i], "--ecb" ) )
		{
			_found = 1;
			_is_ecb = 1;
		}
		
		if( 0 == strcmp( argv[_i], "--key" ) )
		{
			_found = 1;
			_i++; // key_data
			if( _i >= argc )
			{
				fprintf(stderr, "Error: No value specified for '%s'.\n",
						"--key");
				usage( argv[0] );
				return EXIT_FAILURE;
			}
			_key_data_len = strlen(argv[_i]);
			if( 16 >= _key_data_len )
				_key_data_len = 16;
			else if( 24 >= _key_data_len )
				_key_data_len = 24;
			else
				_key_data_len = 32;
			memcpy(_key_data, argv[_i], __min(32, strlen(argv[_i])));
		}
		
		if( 0 == strcmp( argv[_i], "--in" ) )
		{
			_found = 1;
			_i++; // path_in
			if( _i >= argc )
			{
				fprintf(stderr, "Error: No value specified for '%s'.\n",
						"--in");
				usage( argv[0] );
				return EXIT_FAILURE;
			}
			_file_in = argv[_i];
		}
		
		if( 0 == strcmp( argv[_i], "--out" ) )
		{
			_found = 1;
			_i++; // path_out
			if( _i >= argc )
			{
				fprintf(stderr, "Error: No value specified for '%s'.\n",
						"--out");
				usage( argv[0] );
				return EXIT_FAILURE;
			}
			_file_out = argv[_i];
		}
		
		if( 0 == _found )
		{
			fprintf(stderr, "Error: Invalid option '%s'.\n", argv[_i]);
			usage( argv[0] );
			return EXIT_FAILURE;
		}			
	}

	if( 0 == _key_data_len )
	{
		fprintf(stderr, "Error: --key must be specified.\n");
		return EXIT_FAILURE;
	}

	if( _file_in )
	{
		_f_in = fopen(_file_in, "rb");
		if( NULL == _f_in )
		{
			fprintf(stderr,
				"Error: Failed to open '-%s' for reading.\n", _file_in);
			return EXIT_FAILURE;
		}
	}
	else
	{
		if( setmode(fileno(stdin), 0x8000) < 0 )
			fprintf(stderr,"Error: Failed in setmode().\n");
		_f_in = stdin;
	}

	if( _file_out )
	{
		_f_out = fopen(_file_out, "wb");
		if( NULL == _f_out )
		{
			fprintf(stderr,
				"Error: Failed to open '-%s' for writing.\n", _file_out);
			if( _file_in )
				fclose(_f_in);
			return EXIT_FAILURE;
		}
	}
	else
	{
		if( setmode(fileno(stdout), 0x8000) < 0 )
			fprintf(stderr, "Error: Failed in setmode().\n");
		_f_out = stdout;
	}

	ctx = oaes_alloc();
	if( NULL == ctx )
	{
		fprintf(stderr, "Error: Failed to initialize OAES.\n");
		if( _file_in )
			fclose(_f_in);
		if( _file_out )
			fclose(_f_out);
		return EXIT_FAILURE;
	}
	if( _is_ecb )
		if( OAES_RET_SUCCESS != oaes_set_option( ctx, OAES_OPTION_ECB, NULL ) )
			fprintf(stderr, "Error: Failed to set OAES options.\n");

	oaes_key_import_data( ctx, _key_data, _key_data_len );

	while( (_buf_in_len =
		fread(_buf_in, sizeof(uint8_t), _read_len, _f_in)) )
	{
		switch(_op)
		{
		case 0:
			if( OAES_RET_SUCCESS != oaes_encrypt( ctx,
					_buf_in, _buf_in_len, NULL, &_buf_out_len ) )
				fprintf( stderr,
					"Error: Failed to retrieve required buffer size for "
					"encryption.\n" );
			_buf_out = (uint8_t *) calloc( _buf_out_len, sizeof( char ) );
			if( NULL == _buf_out )
			{
				fprintf(stderr,  "Error: Failed to allocate memory.\n" );
				if( _file_in )
					fclose(_f_in);
				if( _file_out )
					fclose(_f_out);
				return EXIT_FAILURE;
			}
			if( OAES_RET_SUCCESS != oaes_encrypt( ctx,
					_buf_in, _buf_in_len, _buf_out, &_buf_out_len ) )
				fprintf(stderr, "Error: Encryption failed.\n");
			fwrite(_buf_out, sizeof(uint8_t), _buf_out_len, _f_out);
			free(_buf_out);
			break;
		case 1:
			if( OAES_RET_SUCCESS != oaes_decrypt( ctx,
					_buf_in, _buf_in_len, NULL, &_buf_out_len ) )
				fprintf( stderr,
					"Error: Failed to retrieve required buffer size for "
					"encryption.\n" );
			_buf_out = (uint8_t *) calloc( _buf_out_len, sizeof( char ) );
			if( NULL == _buf_out )
			{
				fprintf(stderr,  "Error: Failed to allocate memory.\n" );
				free( _buf_out );
				if( _file_in )
					fclose(_f_in);
				if( _file_out )
					fclose(_f_out);
				return EXIT_FAILURE;
			}
			if( OAES_RET_SUCCESS != oaes_decrypt( ctx,
					_buf_in, _buf_in_len, _buf_out, &_buf_out_len ) )
				fprintf(stderr, "Error: Decryption failed.\n");
			fwrite(_buf_out, sizeof(uint8_t), _buf_out_len, _f_out);
			free(_buf_out);
			break;
		default:
			break;
		}
	}


	if( OAES_RET_SUCCESS !=  oaes_free( &ctx ) )
		fprintf(stderr, "Error: Failed to uninitialize OAES.\n");
	
	if( _file_in )
		fclose(_f_in);
	if( _file_out )
		fclose(_f_out);

	return (EXIT_SUCCESS);
}