Debian Codegraph @

src/crypto/oaes_lib.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
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 #include <stddef.h>
31 #include <time.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35
36 // OS X, FreeBSD, OpenBSD and NetBSD don't need malloc.h
37 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) \
38 && !defined(__DragonFly__) && !defined(__NetBSD__)
39 #include <malloc.h>
40 #endif
41
42 // ANDROID, FreeBSD, OpenBSD and NetBSD also don't need timeb.h
43 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__ANDROID__) \
44 && !defined(__NetBSD__)
45 #include <sys/timeb.h>
46 #else
47 #include <sys/time.h>
48 #endif
49
50 #ifdef WIN32
51 #include <process.h>
52 #else
53 #include <sys/types.h>
54 #include <unistd.h>
55 #endif
56
57 #include "oaes_lib.h"
58
59 #define OAES_RKEY_LEN 4
60 #define OAES_COL_LEN 4
61 #define OAES_ROUND_BASE 7
62
63 static uint8_t oaes_gf_8[] = {
64 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
65
66 static uint8_t oaes_sub_byte_value[16][16] = {
67 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
68 /*0*/ { 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76 },
69 /*1*/ { 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0 },
70 /*2*/ { 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15 },
71 /*3*/ { 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75 },
72 /*4*/ { 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84 },
73 /*5*/ { 0x53, 0xd1, 0x00, 0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 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 },
84 };
85
86 static OAES_RET oaes_sub_byte( uint8_t * byte )
87 {
88 size_t _x, _y;
89
90 if( NULL == byte )
91 return OAES_RET_ARG1;
92
93 _x = _y = *byte;
94 _x &= 0x0f;
95 _y &= 0xf0;
96 _y >>= 4;
97 *byte = oaes_sub_byte_value[_y][_x];
98
99 return OAES_RET_SUCCESS;
100 }
101
102 static OAES_RET oaes_word_rot_left( uint8_t word[OAES_COL_LEN] )
103 {
104 uint8_t _temp[OAES_COL_LEN];
105
106 if( NULL == word )
107 return OAES_RET_ARG1;
108
109 memcpy( _temp, word + 1, OAES_COL_LEN - 1 );
110 _temp[OAES_COL_LEN - 1] = word[0];
111 memcpy( word, _temp, OAES_COL_LEN );
112
113 return OAES_RET_SUCCESS;
114 }
115
116 static OAES_RET oaes_key_destroy( oaes_key ** key )
117 {
118 if( NULL == *key )
119 return OAES_RET_SUCCESS;
120
121 if( (*key)->data )
122 {
123 free( (*key)->data );
124 (*key)->data = NULL;
125 }
126
127 if( (*key)->exp_data )
128 {
129 free( (*key)->exp_data );
130 (*key)->exp_data = NULL;
131 }
132
133 (*key)->data_len = 0;
134 (*key)->exp_data_len = 0;
135 (*key)->num_keys = 0;
136 (*key)->key_base = 0;
137 free( *key );
138 *key = NULL;
139
140 return OAES_RET_SUCCESS;
141 }
142
143 static OAES_RET oaes_key_expand( OAES_CTX * ctx )
144 {
145 size_t _i, _j;
146 oaes_ctx * _ctx = (oaes_ctx *) ctx;
147
148 if( NULL == _ctx )
149 return OAES_RET_ARG1;
150
151 if( NULL == _ctx->key )
152 return OAES_RET_NOKEY;
153
154 _ctx->key->key_base = _ctx->key->data_len / OAES_RKEY_LEN;
155 _ctx->key->num_keys = _ctx->key->key_base + OAES_ROUND_BASE;
156
157 _ctx->key->exp_data_len = _ctx->key->num_keys * OAES_RKEY_LEN * OAES_COL_LEN;
158 _ctx->key->exp_data = (uint8_t *)
159 calloc( _ctx->key->exp_data_len, sizeof( uint8_t ));
160
161 if( NULL == _ctx->key->exp_data )
162 return OAES_RET_MEM;
163
164 // the first _ctx->key->data_len are a direct copy
165 memcpy( _ctx->key->exp_data, _ctx->key->data, _ctx->key->data_len );
166
167 // apply ExpandKey algorithm for remainder
168 for( _i = _ctx->key->key_base; _i < _ctx->key->num_keys * OAES_RKEY_LEN; _i++ )
169 {
170 uint8_t _temp[OAES_COL_LEN];
171
172 memcpy( _temp,
173 _ctx->key->exp_data + ( _i - 1 ) * OAES_RKEY_LEN, OAES_COL_LEN );
174
175 // transform key column
176 if( 0 == _i % _ctx->key->key_base )
177 {
178 oaes_word_rot_left( _temp );
179
180 for( _j = 0; _j < OAES_COL_LEN; _j++ )
181 oaes_sub_byte( _temp + _j );
182
183 _temp[0] = _temp[0] ^ oaes_gf_8[ _i / _ctx->key->key_base - 1 ];
184 }
185 else if( _ctx->key->key_base > 6 && 4 == _i % _ctx->key->key_base )
186 {
187 for( _j = 0; _j < OAES_COL_LEN; _j++ )
188 oaes_sub_byte( _temp + _j );
189 }
190
191 for( _j = 0; _j < OAES_COL_LEN; _j++ )
192 {
193 _ctx->key->exp_data[ _i * OAES_RKEY_LEN + _j ] =
194 _ctx->key->exp_data[ ( _i - _ctx->key->key_base ) *
195 OAES_RKEY_LEN + _j ] ^ _temp[_j];
196 }
197 }
198
199 return OAES_RET_SUCCESS;
200 }
201
202 OAES_RET oaes_key_import_data( OAES_CTX * ctx,
203 const uint8_t * data, size_t data_len )
204 {
205 oaes_ctx * _ctx = (oaes_ctx *) ctx;
206 OAES_RET _rc = OAES_RET_SUCCESS;
207
208 if( NULL == _ctx )
209 return OAES_RET_ARG1;
210
211 if( NULL == data )
212 return OAES_RET_ARG2;
213
214 switch( data_len )
215 {
216 case 16:
217 case 24:
218 case 32:
219 break;
220 default:
221 return OAES_RET_ARG3;
222 }
223
224 if( _ctx->key )
225 oaes_key_destroy( &(_ctx->key) );
226
227 _ctx->key = (oaes_key *) calloc( sizeof( oaes_key ), 1 );
228
229 if( NULL == _ctx->key )
230 return OAES_RET_MEM;
231
232 _ctx->key->data_len = data_len;
233 _ctx->key->data = (uint8_t *)
234 calloc( data_len, sizeof( uint8_t ));
235
236 if( NULL == _ctx->key->data )
237 {
238 oaes_key_destroy( &(_ctx->key) );
239 return OAES_RET_MEM;
240 }
241
242 memcpy( _ctx->key->data, data, data_len );
243 _rc = _rc || oaes_key_expand( ctx );
244
245 if( _rc != OAES_RET_SUCCESS )
246 {
247 oaes_key_destroy( &(_ctx->key) );
248 return _rc;
249 }
250
251 return OAES_RET_SUCCESS;
252 }
253
254 OAES_CTX * oaes_alloc(void)
255 {
256 oaes_ctx * _ctx = (oaes_ctx *) calloc( sizeof( oaes_ctx ), 1 );
257
258 if( NULL == _ctx )
259 return NULL;
260
261 _ctx->key = NULL;
262
263 return (OAES_CTX *) _ctx;
264 }
265
266 OAES_RET oaes_free( OAES_CTX ** ctx )
267 {
268 oaes_ctx ** _ctx = (oaes_ctx **) ctx;
269
270 if( NULL == _ctx )
271 return OAES_RET_ARG1;
272
273 if( NULL == *_ctx )
274 return OAES_RET_SUCCESS;
275
276 if( (*_ctx)->key )
277 oaes_key_destroy( &((*_ctx)->key) );
278
279 free( *_ctx );
280 *_ctx = NULL;
281
282 return OAES_RET_SUCCESS;
283 }