blob: 6a8d6256dc53c449c41106f22a42ca5c8084a977 [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
34#include "oaes_lib.h"
35
36/*
37 *
38 */
39int main(int argc, char** argv) {
40
41 OAES_CTX * ctx = NULL;
42 uint8_t * _buf;
43 size_t _data_len;
44 FILE * f = NULL;
45 OAES_RET _rc;
46
47 if( NULL == ( ctx = oaes_alloc() ) )
48 {
49 printf( "Error: Initialization failed.\n" );
50 return 1;
51 }
52
53 /* ************** Generate 128-bit key and export it **************
54 * ****************************************************************/
55 if( OAES_RET_SUCCESS != ( _rc = oaes_key_gen_128(ctx) ) )
56 {
57 printf( "Error: Failed to generate 128-bit key [%d].\n", _rc );
58 oaes_free(&ctx);
59 return 1;
60 }
61
62 if( OAES_RET_SUCCESS != ( _rc = oaes_key_export(ctx, NULL, &_data_len) ) )
63 {
64 printf( "Error: Failed to retrieve key length [%d].\n", _rc );
65 oaes_free(&ctx);
66 return 1;
67 }
68
69 _buf = (uint8_t *) calloc(_data_len, sizeof(uint8_t));
70 if( _buf )
71 {
72 if( OAES_RET_SUCCESS != ( _rc = oaes_key_export(ctx, _buf, &_data_len) ) )
73 {
74 printf("Error: Failed to export key [%d].\n", _rc);
75 free(_buf);
76 oaes_free(&ctx);
77 return 1;
78 }
79
80 f = fopen( "key_128", "wb" );
81 if( f )
82 {
83 fwrite(_buf, _data_len, sizeof(uint8_t), f);
84 fclose(f);
85 }
86 free(_buf);
87 }
88
89 /* ************** Generate 192-bit key and export it **************
90 * ****************************************************************/
91 if( OAES_RET_SUCCESS != ( _rc = oaes_key_gen_192(ctx) ) )
92 {
93 printf( "Error: Failed to generate 192-bit key [%d].\n", _rc );
94 oaes_free(&ctx);
95 return 1;
96 }
97
98 if( OAES_RET_SUCCESS != ( _rc = oaes_key_export(ctx, NULL, &_data_len) ) )
99 {
100 printf( "Error: Failed to retrieve key length [%d].\n", _rc );
101 oaes_free(&ctx);
102 return 1;
103 }
104
105 _buf = (uint8_t *) calloc(_data_len, sizeof(uint8_t));
106 if( _buf )
107 {
108 if( OAES_RET_SUCCESS != ( _rc = oaes_key_export(ctx, _buf, &_data_len) ) )
109 {
110 printf("Error: Failed to export key [%d].\n", _rc);
111 free(_buf);
112 oaes_free(&ctx);
113 return 1;
114 }
115
116 f = fopen("key_192", "wb");
117 if( f )
118 {
119 fwrite(_buf, _data_len, sizeof(uint8_t), f);
120 fclose(f);
121 }
122 free(_buf);
123 }
124
125 /* ************** Generate 256-bit key and export it **************
126 * ****************************************************************/
127 if( OAES_RET_SUCCESS != ( _rc = oaes_key_gen_256(ctx) ) )
128 {
129 printf("Error: Failed to generate 256-bit key [%d].\n", _rc);
130 oaes_free(&ctx);
131 return 1;
132 }
133
134 if( OAES_RET_SUCCESS != ( _rc = oaes_key_export(ctx, NULL, &_data_len) ) )
135 {
136 printf("Error: Failed to retrieve key length [%d].\n", _rc);
137 oaes_free(&ctx);
138 return 1;
139 }
140
141 _buf = (uint8_t *) calloc(_data_len, sizeof(uint8_t));
142 if( _buf )
143 {
144 if( OAES_RET_SUCCESS != ( _rc = oaes_key_export(ctx, _buf, &_data_len) ) )
145 {
146 printf("Error: Failed to export key [%d].\n", _rc);
147 free(_buf);
148 oaes_free(&ctx);
149 return 1;
150 }
151
152 f = fopen("key_256", "wb");
153 if( f )
154 {
155 fwrite(_buf, _data_len, sizeof(uint8_t), f);
156 fclose(f);
157 }
158 free(_buf);
159 }
160
161 /* ********************** Import 128-bit key **********************
162 * ****************************************************************/
163 f = fopen("key_128", "rb");
164 if( f )
165 {
166 fseek(f, 0L, SEEK_END);
167 _data_len = ftell(f);
168 fseek(f, 0L, SEEK_SET);
169 _buf = (uint8_t *) calloc(_data_len, sizeof(uint8_t));
170 if( _buf )
171 {
172 fread(_buf, _data_len, sizeof(uint8_t), f);
173
174 if( OAES_RET_SUCCESS !=
175 ( _rc = oaes_key_import(ctx, _buf, _data_len) ) )
176 {
177 printf( "Error: Failed to import key [%d].\n", _rc );
178 free(_buf);
179 fclose(f);
180 oaes_free(&ctx);
181 return 1;
182 }
183
184 free(_buf);
185 }
186 fclose(f);
187 }
188
189 /* ********************** Import 192-bit key **********************
190 * ****************************************************************/
191 f = fopen("key_192", "rb");
192 if( f )
193 {
194 fseek(f, 0L, SEEK_END);
195 _data_len = ftell(f);
196 fseek(f, 0L, SEEK_SET);
197 _buf = (uint8_t *) calloc(_data_len, sizeof(uint8_t));
198 if( _buf )
199 {
200 fread(_buf, _data_len, sizeof(uint8_t), f);
201
202 if( OAES_RET_SUCCESS !=
203 ( _rc = oaes_key_import(ctx, _buf, _data_len) ) )
204 {
205 printf("Error: Failed to import key [%d].\n", _rc);
206 free(_buf);
207 fclose(f);
208 oaes_free(&ctx);
209 return 1;
210 }
211
212 free(_buf);
213 }
214 fclose(f);
215 }
216
217 /* ********************** Import 256-bit key **********************
218 * ****************************************************************/
219 f = fopen("key_256", "rb");
220 if( f )
221 {
222 fseek(f, 0L, SEEK_END);
223 _data_len = ftell(f);
224 fseek(f, 0L, SEEK_SET);
225 _buf = (uint8_t *) calloc(_data_len, sizeof(uint8_t));
226 if( _buf )
227 {
228 fread(_buf, _data_len, sizeof(uint8_t), f);
229
230 if( OAES_RET_SUCCESS !=
231 ( _rc = oaes_key_import(ctx, _buf, _data_len) ) )
232 {
233 printf("Error: Failed to import key [%d].\n", _rc);
234 free(_buf);
235 fclose(f);
236 oaes_free(&ctx);
237 return 1;
238 }
239
240 free(_buf);
241 }
242 fclose(f);
243 }
244
245 oaes_free(&ctx);
246
247 return (EXIT_SUCCESS);
248}