blob: 6e20b478f2cb0ef3b82a340fced0f032bdc7ed87 [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 */
30static const char _NR[] = {
31 0x4e,0x61,0x62,0x69,0x6c,0x20,0x53,0x2e,0x20,
32 0x41,0x6c,0x20,0x52,0x61,0x6d,0x6c,0x69,0x00 };
33
34#include <stddef.h>
35#include <time.h>
Ethan Yonker6c41bfd2014-11-08 15:12:25 -060036//#include <sys/timeb.h>
37#include "ftime.h"
Dees_Troy83bd4832013-05-04 12:39:56 +000038#include <malloc.h>
39#include <string.h>
40
41#ifdef WIN32
42#include <process.h>
43#endif
44
45#include "oaes_config.h"
46#include "oaes_lib.h"
47
48#ifdef OAES_HAVE_ISAAC
49#include "rand.h"
50#endif // OAES_HAVE_ISAAC
51
52#define OAES_RKEY_LEN 4
53#define OAES_COL_LEN 4
54#define OAES_ROUND_BASE 7
55
56// the block is padded
57#define OAES_FLAG_PAD 0x01
58
59#ifndef min
60# define min(a,b) (((a)<(b)) ? (a) : (b))
61#endif /* min */
62
63typedef struct _oaes_key
64{
65 size_t data_len;
66 uint8_t *data;
67 size_t exp_data_len;
68 uint8_t *exp_data;
69 size_t num_keys;
70 size_t key_base;
71} oaes_key;
72
73typedef struct _oaes_ctx
74{
75#ifdef OAES_HAVE_ISAAC
76 randctx * rctx;
77#endif // OAES_HAVE_ISAAC
78
79#ifdef OAES_DEBUG
80 oaes_step_cb step_cb;
81#endif // OAES_DEBUG
82
83 oaes_key * key;
84 OAES_OPTION options;
85 uint8_t iv[OAES_BLOCK_SIZE];
86} oaes_ctx;
87
88// "OAES<8-bit header version><8-bit type><16-bit options><8-bit flags><56-bit reserved>"
89static uint8_t oaes_header[OAES_BLOCK_SIZE] = {
90 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
91 /*0*/ 0x4f, 0x41, 0x45, 0x53, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92};
93static uint8_t oaes_gf_8[] = {
94 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
95
96static uint8_t oaes_sub_byte_value[16][16] = {
97 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
98 /*0*/ 0x63, 0x7c, 0x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 0x62, 0x91, 0x95, 0xe4, 0x79,
109 /*b*/ 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
110 /*c*/ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
111 /*d*/ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
112 /*e*/ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
113 /*f*/ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
114};
115
116static uint8_t oaes_inv_sub_byte_value[16][16] = {
117 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
118 /*0*/ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
119 /*1*/ 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
120 /*2*/ 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
121 /*3*/ 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
122 /*4*/ 0x72, 0xf8, 0xf6, 0x64, 0x86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 0x83, 0x53, 0x99, 0x61,
133 /*f*/ 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
134};
135
136static uint8_t oaes_gf_mul_2[16][16] = {
137 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
138 /*0*/ 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
139 /*1*/ 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
140 /*2*/ 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
141 /*3*/ 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
142 /*4*/ 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
143 /*5*/ 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
144 /*6*/ 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
145 /*7*/ 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
146 /*8*/ 0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05,
147 /*9*/ 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25,
148 /*a*/ 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45,
149 /*b*/ 0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65,
150 /*c*/ 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0x83, 0x81, 0x87, 0x85,
151 /*d*/ 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5, 0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5,
152 /*e*/ 0xdb, 0xd9, 0xdf, 0xdd, 0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5,
153 /*f*/ 0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5,
154};
155
156static uint8_t oaes_gf_mul_3[16][16] = {
157 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
158 /*0*/ 0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d, 0x14, 0x17, 0x12, 0x11,
159 /*1*/ 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39, 0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21,
160 /*2*/ 0x60, 0x63, 0x66,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0x43, 0x40, 0x45, 0x46, 0x4f, 0x4c, 0x49, 0x4a,
171 /*d*/ 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a,
172 /*e*/ 0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a,
173 /*f*/ 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a,
174};
175
176static uint8_t oaes_gf_mul_9[16][16] = {
177 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
178 /*0*/ 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77,
179 /*1*/ 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7,
180 /*2*/ 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c,
181 /*3*/ 0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8, 0xc7, 0xce, 0xd5, 0xdc,
182 /*4*/ 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49, 0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01,
183 /*5*/ 0xe6, 0xef, 0xf4, 0xfd, 0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91,
184 /*6*/ 0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e, 0x21, 0x28, 0x33, 0x3a,
185 /*7*/ 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2, 0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa,
186 /*8*/ 0xec, 0xe5, 0xfe, 0xf7, 0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b,
187 /*9*/ 0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f, 0x10, 0x19, 0x02, 0x0b,
188 /*a*/ 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8, 0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0,
189 /*b*/ 0x47, 0x4e, 0x55, 0x5c, 0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30,
190 /*c*/ 0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed,
191 /*d*/ 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d,
192 /*e*/ 0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6,
193 /*f*/ 0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46,
194};
195
196static uint8_t oaes_gf_mul_b[16][16] = {
197 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
198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6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5,
209 /*b*/ 0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55,
210 /*c*/ 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68,
211 /*d*/ 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8,
212 /*e*/ 0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13,
213 /*f*/ 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3,
214};
215
216static uint8_t oaes_gf_mul_d[16][16] = {
217 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
218 /*0*/ 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b,
219 /*1*/ 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b,
220 /*2*/ 0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0,
221 /*3*/ 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20,
222 /*4*/ 0x6d, 0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0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47,
233 /*f*/ 0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, 0x80, 0x8d, 0x9a, 0x97,
234};
235
236static uint8_t oaes_gf_mul_e[16][16] = {
237 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
238 /*0*/ 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a,
239 /*1*/ 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba,
240 /*2*/ 0xdb, 0xd5, 0xc7, 0xc9, 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81,
241 /*3*/ 0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, 0x73, 0x7d, 0x6f, 0x61,
242 /*4*/ 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7,
243 /*5*/ 0x4d, 0x43, 0x51, 0x5f, 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17,
244 /*6*/ 0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, 0x3e, 0x30, 0x22, 0x2c,
245 /*7*/ 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc,
246 /*8*/ 0x41, 0x4f, 0x5d, 0x53, 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b,
247 /*9*/ 0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, 0xe9, 0xe7, 0xf5, 0xfb,
248 /*a*/ 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0,
249 /*b*/ 0x7a, 0x74, 0x66, 0x68, 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20,
250 /*c*/ 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6,
251 /*d*/ 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56,
252 /*e*/ 0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d,
253 /*f*/ 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d,
254};
255
256static OAES_RET oaes_sub_byte( uint8_t * byte )
257{
258 size_t _x, _y;
259
260 if( NULL == byte )
261 return OAES_RET_ARG1;
262
263 _x = _y = *byte;
264 _x &= 0x0f;
265 _y &= 0xf0;
266 _y >>= 4;
267 *byte = oaes_sub_byte_value[_y][_x];
268
269 return OAES_RET_SUCCESS;
270}
271
272static OAES_RET oaes_inv_sub_byte( uint8_t * byte )
273{
274 size_t _x, _y;
275
276 if( NULL == byte )
277 return OAES_RET_ARG1;
278
279 _x = _y = *byte;
280 _x &= 0x0f;
281 _y &= 0xf0;
282 _y >>= 4;
283 *byte = oaes_inv_sub_byte_value[_y][_x];
284
285 return OAES_RET_SUCCESS;
286}
287
288static OAES_RET oaes_word_rot_right( uint8_t word[OAES_COL_LEN] )
289{
290 uint8_t _temp[OAES_COL_LEN];
291
292 if( NULL == word )
293 return OAES_RET_ARG1;
294
295 memcpy( _temp + 1, word, OAES_COL_LEN - 1 );
296 _temp[0] = word[OAES_COL_LEN - 1];
297 memcpy( word, _temp, OAES_COL_LEN );
298
299 return OAES_RET_SUCCESS;
300}
301
302static OAES_RET oaes_word_rot_left( uint8_t word[OAES_COL_LEN] )
303{
304 uint8_t _temp[OAES_COL_LEN];
305
306 if( NULL == word )
307 return OAES_RET_ARG1;
308
309 memcpy( _temp, word + 1, OAES_COL_LEN - 1 );
310 _temp[OAES_COL_LEN - 1] = word[0];
311 memcpy( word, _temp, OAES_COL_LEN );
312
313 return OAES_RET_SUCCESS;
314}
315
316static OAES_RET oaes_shift_rows( uint8_t block[OAES_BLOCK_SIZE] )
317{
318 uint8_t _temp[OAES_BLOCK_SIZE];
319
320 if( NULL == block )
321 return OAES_RET_ARG1;
322
323 _temp[0x00] = block[0x00];
324 _temp[0x01] = block[0x05];
325 _temp[0x02] = block[0x0a];
326 _temp[0x03] = block[0x0f];
327 _temp[0x04] = block[0x04];
328 _temp[0x05] = block[0x09];
329 _temp[0x06] = block[0x0e];
330 _temp[0x07] = block[0x03];
331 _temp[0x08] = block[0x08];
332 _temp[0x09] = block[0x0d];
333 _temp[0x0a] = block[0x02];
334 _temp[0x0b] = block[0x07];
335 _temp[0x0c] = block[0x0c];
336 _temp[0x0d] = block[0x01];
337 _temp[0x0e] = block[0x06];
338 _temp[0x0f] = block[0x0b];
339 memcpy( block, _temp, OAES_BLOCK_SIZE );
340
341 return OAES_RET_SUCCESS;
342}
343
344static OAES_RET oaes_inv_shift_rows( uint8_t block[OAES_BLOCK_SIZE] )
345{
346 uint8_t _temp[OAES_BLOCK_SIZE];
347
348 if( NULL == block )
349 return OAES_RET_ARG1;
350
351 _temp[0x00] = block[0x00];
352 _temp[0x01] = block[0x0d];
353 _temp[0x02] = block[0x0a];
354 _temp[0x03] = block[0x07];
355 _temp[0x04] = block[0x04];
356 _temp[0x05] = block[0x01];
357 _temp[0x06] = block[0x0e];
358 _temp[0x07] = block[0x0b];
359 _temp[0x08] = block[0x08];
360 _temp[0x09] = block[0x05];
361 _temp[0x0a] = block[0x02];
362 _temp[0x0b] = block[0x0f];
363 _temp[0x0c] = block[0x0c];
364 _temp[0x0d] = block[0x09];
365 _temp[0x0e] = block[0x06];
366 _temp[0x0f] = block[0x03];
367 memcpy( block, _temp, OAES_BLOCK_SIZE );
368
369 return OAES_RET_SUCCESS;
370}
371
372static uint8_t oaes_gf_mul(uint8_t left, uint8_t right)
373{
374 size_t _x, _y;
375
376 _x = _y = left;
377 _x &= 0x0f;
378 _y &= 0xf0;
379 _y >>= 4;
380
381 switch( right )
382 {
383 case 0x02:
384 return oaes_gf_mul_2[_y][_x];
385 break;
386 case 0x03:
387 return oaes_gf_mul_3[_y][_x];
388 break;
389 case 0x09:
390 return oaes_gf_mul_9[_y][_x];
391 break;
392 case 0x0b:
393 return oaes_gf_mul_b[_y][_x];
394 break;
395 case 0x0d:
396 return oaes_gf_mul_d[_y][_x];
397 break;
398 case 0x0e:
399 return oaes_gf_mul_e[_y][_x];
400 break;
401 default:
402 return left;
403 break;
404 }
405}
406
407static OAES_RET oaes_mix_cols( uint8_t word[OAES_COL_LEN] )
408{
409 uint8_t _temp[OAES_COL_LEN];
410
411 if( NULL == word )
412 return OAES_RET_ARG1;
413
414 _temp[0] = oaes_gf_mul(word[0], 0x02) ^ oaes_gf_mul( word[1], 0x03 ) ^
415 word[2] ^ word[3];
416 _temp[1] = word[0] ^ oaes_gf_mul( word[1], 0x02 ) ^
417 oaes_gf_mul( word[2], 0x03 ) ^ word[3];
418 _temp[2] = word[0] ^ word[1] ^
419 oaes_gf_mul( word[2], 0x02 ) ^ oaes_gf_mul( word[3], 0x03 );
420 _temp[3] = oaes_gf_mul( word[0], 0x03 ) ^ word[1] ^
421 word[2] ^ oaes_gf_mul( word[3], 0x02 );
422 memcpy( word, _temp, OAES_COL_LEN );
423
424 return OAES_RET_SUCCESS;
425}
426
427static OAES_RET oaes_inv_mix_cols( uint8_t word[OAES_COL_LEN] )
428{
429 uint8_t _temp[OAES_COL_LEN];
430
431 if( NULL == word )
432 return OAES_RET_ARG1;
433
434 _temp[0] = oaes_gf_mul( word[0], 0x0e ) ^ oaes_gf_mul( word[1], 0x0b ) ^
435 oaes_gf_mul( word[2], 0x0d ) ^ oaes_gf_mul( word[3], 0x09 );
436 _temp[1] = oaes_gf_mul( word[0], 0x09 ) ^ oaes_gf_mul( word[1], 0x0e ) ^
437 oaes_gf_mul( word[2], 0x0b ) ^ oaes_gf_mul( word[3], 0x0d );
438 _temp[2] = oaes_gf_mul( word[0], 0x0d ) ^ oaes_gf_mul( word[1], 0x09 ) ^
439 oaes_gf_mul( word[2], 0x0e ) ^ oaes_gf_mul( word[3], 0x0b );
440 _temp[3] = oaes_gf_mul( word[0], 0x0b ) ^ oaes_gf_mul( word[1], 0x0d ) ^
441 oaes_gf_mul( word[2], 0x09 ) ^ oaes_gf_mul( word[3], 0x0e );
442 memcpy( word, _temp, OAES_COL_LEN );
443
444 return OAES_RET_SUCCESS;
445}
446
447OAES_RET oaes_sprintf(
448 char * buf, size_t * buf_len, const uint8_t * data, size_t data_len )
449{
450 size_t _i, _buf_len_in;
451 char _temp[4];
452
453 if( NULL == buf_len )
454 return OAES_RET_ARG2;
455
456 _buf_len_in = *buf_len;
457 *buf_len = data_len * 3 + data_len / OAES_BLOCK_SIZE + 1;
458
459 if( NULL == buf )
460 return OAES_RET_SUCCESS;
461
462 if( *buf_len > _buf_len_in )
463 return OAES_RET_BUF;
464
465 if( NULL == data )
466 return OAES_RET_ARG3;
467
468 strcpy( buf, "" );
469
470 for( _i = 0; _i < data_len; _i++ )
471 {
472 sprintf( _temp, "%02x ", data[_i] );
473 strcat( buf, _temp );
474 if( _i && 0 == ( _i + 1 ) % OAES_BLOCK_SIZE )
475 strcat( buf, "\n" );
476 }
477
478 return OAES_RET_SUCCESS;
479}
480
481#ifdef OAES_HAVE_ISAAC
482static void oaes_get_seed( char buf[RANDSIZ + 1] )
483{
484 struct timeb timer;
485 struct tm *gmTimer;
486 char * _test = NULL;
487
488 ftime (&timer);
489 gmTimer = gmtime( &timer.time );
490 _test = (char *) calloc( sizeof( char ), timer.millitm );
491 sprintf( buf, "%04d%02d%02d%02d%02d%02d%03d%p%d",
492 gmTimer->tm_year + 1900, gmTimer->tm_mon + 1, gmTimer->tm_mday,
493 gmTimer->tm_hour, gmTimer->tm_min, gmTimer->tm_sec, timer.millitm,
494 _test + timer.millitm, getpid() );
495
496 if( _test )
497 free( _test );
498}
499#else
500static uint32_t oaes_get_seed()
501{
502 struct timeb timer;
503 struct tm *gmTimer;
504 char * _test = NULL;
505 uint32_t _ret = 0;
506
507 ftime (&timer);
508 gmTimer = gmtime( &timer.time );
509 _test = (char *) calloc( sizeof( char ), timer.millitm );
510 _ret = gmTimer->tm_year + 1900 + gmTimer->tm_mon + 1 + gmTimer->tm_mday +
511 gmTimer->tm_hour + gmTimer->tm_min + gmTimer->tm_sec + timer.millitm +
512 (uint32_t) ( _test + timer.millitm ) + getpid();
513
514 if( _test )
515 free( _test );
516
517 return _ret;
518}
519#endif // OAES_HAVE_ISAAC
520
521static OAES_RET oaes_key_destroy( oaes_key ** key )
522{
523 if( NULL == *key )
524 return OAES_RET_SUCCESS;
525
526 if( (*key)->data )
527 {
528 free( (*key)->data );
529 (*key)->data = NULL;
530 }
531
532 if( (*key)->exp_data )
533 {
534 free( (*key)->exp_data );
535 (*key)->exp_data = NULL;
536 }
537
538 (*key)->data_len = 0;
539 (*key)->exp_data_len = 0;
540 (*key)->num_keys = 0;
541 (*key)->key_base = 0;
542 free( *key );
543 *key = NULL;
544
545 return OAES_RET_SUCCESS;
546}
547
548static OAES_RET oaes_key_expand( OAES_CTX * ctx )
549{
550 size_t _i, _j;
551 oaes_ctx * _ctx = (oaes_ctx *) ctx;
552
553 if( NULL == _ctx )
554 return OAES_RET_ARG1;
555
556 if( NULL == _ctx->key )
557 return OAES_RET_NOKEY;
558
559 _ctx->key->key_base = _ctx->key->data_len / OAES_RKEY_LEN;
560 _ctx->key->num_keys = _ctx->key->key_base + OAES_ROUND_BASE;
561
562 _ctx->key->exp_data_len = _ctx->key->num_keys * OAES_RKEY_LEN * OAES_COL_LEN;
563 _ctx->key->exp_data = (uint8_t *)
564 calloc( _ctx->key->exp_data_len, sizeof( uint8_t ));
565
566 if( NULL == _ctx->key->exp_data )
567 return OAES_RET_MEM;
568
569 // the first _ctx->key->data_len are a direct copy
570 memcpy( _ctx->key->exp_data, _ctx->key->data, _ctx->key->data_len );
571
572 // apply ExpandKey algorithm for remainder
573 for( _i = _ctx->key->key_base; _i < _ctx->key->num_keys * OAES_RKEY_LEN; _i++ )
574 {
575 uint8_t _temp[OAES_COL_LEN];
576
577 memcpy( _temp,
578 _ctx->key->exp_data + ( _i - 1 ) * OAES_RKEY_LEN, OAES_COL_LEN );
579
580 // transform key column
581 if( 0 == _i % _ctx->key->key_base )
582 {
583 oaes_word_rot_left( _temp );
584
585 for( _j = 0; _j < OAES_COL_LEN; _j++ )
586 oaes_sub_byte( _temp + _j );
587
588 _temp[0] = _temp[0] ^ oaes_gf_8[ _i / _ctx->key->key_base - 1 ];
589 }
590 else if( _ctx->key->key_base > 6 && 4 == _i % _ctx->key->key_base )
591 {
592 for( _j = 0; _j < OAES_COL_LEN; _j++ )
593 oaes_sub_byte( _temp + _j );
594 }
595
596 for( _j = 0; _j < OAES_COL_LEN; _j++ )
597 {
598 _ctx->key->exp_data[ _i * OAES_RKEY_LEN + _j ] =
599 _ctx->key->exp_data[ ( _i - _ctx->key->key_base ) *
600 OAES_RKEY_LEN + _j ] ^ _temp[_j];
601 }
602 }
603
604 return OAES_RET_SUCCESS;
605}
606
607static OAES_RET oaes_key_gen( OAES_CTX * ctx, size_t key_size )
608{
609 size_t _i;
610 oaes_key * _key = NULL;
611 oaes_ctx * _ctx = (oaes_ctx *) ctx;
612 OAES_RET _rc = OAES_RET_SUCCESS;
613
614 if( NULL == _ctx )
615 return OAES_RET_ARG1;
616
617 _key = (oaes_key *) calloc( sizeof( oaes_key ), 1 );
618
619 if( NULL == _key )
620 return OAES_RET_MEM;
621
622 if( _ctx->key )
623 oaes_key_destroy( &(_ctx->key) );
624
625 _key->data_len = key_size;
626 _key->data = (uint8_t *) calloc( key_size, sizeof( uint8_t ));
627
628 if( NULL == _key->data )
629 return OAES_RET_MEM;
630
631 for( _i = 0; _i < key_size; _i++ )
632#ifdef OAES_HAVE_ISAAC
633 _key->data[_i] = (uint8_t) rand( _ctx->rctx );
634#else
635 _key->data[_i] = (uint8_t) rand();
636#endif // OAES_HAVE_ISAAC
637
638 _ctx->key = _key;
639 _rc = _rc || oaes_key_expand( ctx );
640
641 if( _rc != OAES_RET_SUCCESS )
642 {
643 oaes_key_destroy( &(_ctx->key) );
644 return _rc;
645 }
646
647 return OAES_RET_SUCCESS;
648}
649
650OAES_RET oaes_key_gen_128( OAES_CTX * ctx )
651{
652 return oaes_key_gen( ctx, 16 );
653}
654
655OAES_RET oaes_key_gen_192( OAES_CTX * ctx )
656{
657 return oaes_key_gen( ctx, 24 );
658}
659
660OAES_RET oaes_key_gen_256( OAES_CTX * ctx )
661{
662 return oaes_key_gen( ctx, 32 );
663}
664
665OAES_RET oaes_key_export( OAES_CTX * ctx,
666 uint8_t * data, size_t * data_len )
667{
668 size_t _data_len_in;
669 oaes_ctx * _ctx = (oaes_ctx *) ctx;
670
671 if( NULL == _ctx )
672 return OAES_RET_ARG1;
673
674 if( NULL == _ctx->key )
675 return OAES_RET_NOKEY;
676
677 if( NULL == data_len )
678 return OAES_RET_ARG3;
679
680 _data_len_in = *data_len;
681 // data + header
682 *data_len = _ctx->key->data_len + OAES_BLOCK_SIZE;
683
684 if( NULL == data )
685 return OAES_RET_SUCCESS;
686
687 if( _data_len_in < *data_len )
688 return OAES_RET_BUF;
689
690 // header
691 memcpy( data, oaes_header, OAES_BLOCK_SIZE );
692 data[5] = 0x01;
693 data[7] = _ctx->key->data_len;
694 memcpy( data + OAES_BLOCK_SIZE, _ctx->key->data, _ctx->key->data_len );
695
696 return OAES_RET_SUCCESS;
697}
698
699OAES_RET oaes_key_export_data( OAES_CTX * ctx,
700 uint8_t * data, size_t * data_len )
701{
702 size_t _data_len_in;
703 oaes_ctx * _ctx = (oaes_ctx *) ctx;
704
705 if( NULL == _ctx )
706 return OAES_RET_ARG1;
707
708 if( NULL == _ctx->key )
709 return OAES_RET_NOKEY;
710
711 if( NULL == data_len )
712 return OAES_RET_ARG3;
713
714 _data_len_in = *data_len;
715 *data_len = _ctx->key->data_len;
716
717 if( NULL == data )
718 return OAES_RET_SUCCESS;
719
720 if( _data_len_in < *data_len )
721 return OAES_RET_BUF;
722
723 memcpy( data, _ctx->key->data, *data_len );
724
725 return OAES_RET_SUCCESS;
726}
727
728OAES_RET oaes_key_import( OAES_CTX * ctx,
729 const uint8_t * data, size_t data_len )
730{
731 oaes_ctx * _ctx = (oaes_ctx *) ctx;
732 OAES_RET _rc = OAES_RET_SUCCESS;
733 int _key_length;
734
735 if( NULL == _ctx )
736 return OAES_RET_ARG1;
737
738 if( NULL == data )
739 return OAES_RET_ARG2;
740
741 switch( data_len )
742 {
743 case 16 + OAES_BLOCK_SIZE:
744 case 24 + OAES_BLOCK_SIZE:
745 case 32 + OAES_BLOCK_SIZE:
746 break;
747 default:
748 return OAES_RET_ARG3;
749 }
750
751 // header
752 if( 0 != memcmp( data, oaes_header, 4 ) )
753 return OAES_RET_HEADER;
754
755 // header version
756 switch( data[4] )
757 {
758 case 0x01:
759 break;
760 default:
761 return OAES_RET_HEADER;
762 }
763
764 // header type
765 switch( data[5] )
766 {
767 case 0x01:
768 break;
769 default:
770 return OAES_RET_HEADER;
771 }
772
773 // options
774 _key_length = data[7];
775 switch( _key_length )
776 {
777 case 16:
778 case 24:
779 case 32:
780 break;
781 default:
782 return OAES_RET_HEADER;
783 }
784
785 if( data_len != _key_length + OAES_BLOCK_SIZE )
786 return OAES_RET_ARG3;
787
788 if( _ctx->key )
789 oaes_key_destroy( &(_ctx->key) );
790
791 _ctx->key = (oaes_key *) calloc( sizeof( oaes_key ), 1 );
792
793 if( NULL == _ctx->key )
794 return OAES_RET_MEM;
795
796 _ctx->key->data_len = _key_length;
797 _ctx->key->data = (uint8_t *)
798 calloc( _key_length, sizeof( uint8_t ));
799
800 if( NULL == _ctx->key->data )
801 {
802 oaes_key_destroy( &(_ctx->key) );
803 return OAES_RET_MEM;
804 }
805
806 memcpy( _ctx->key->data, data + OAES_BLOCK_SIZE, _key_length );
807 _rc = _rc || oaes_key_expand( ctx );
808
809 if( _rc != OAES_RET_SUCCESS )
810 {
811 oaes_key_destroy( &(_ctx->key) );
812 return _rc;
813 }
814
815 return OAES_RET_SUCCESS;
816}
817
818OAES_RET oaes_key_import_data( OAES_CTX * ctx,
819 const uint8_t * data, size_t data_len )
820{
821 oaes_ctx * _ctx = (oaes_ctx *) ctx;
822 OAES_RET _rc = OAES_RET_SUCCESS;
823
824 if( NULL == _ctx )
825 return OAES_RET_ARG1;
826
827 if( NULL == data )
828 return OAES_RET_ARG2;
829
830 switch( data_len )
831 {
832 case 16:
833 case 24:
834 case 32:
835 break;
836 default:
837 return OAES_RET_ARG3;
838 }
839
840 if( _ctx->key )
841 oaes_key_destroy( &(_ctx->key) );
842
843 _ctx->key = (oaes_key *) calloc( sizeof( oaes_key ), 1 );
844
845 if( NULL == _ctx->key )
846 return OAES_RET_MEM;
847
848 _ctx->key->data_len = data_len;
849 _ctx->key->data = (uint8_t *)
850 calloc( data_len, sizeof( uint8_t ));
851
852 if( NULL == _ctx->key->data )
853 {
854 oaes_key_destroy( &(_ctx->key) );
855 return OAES_RET_MEM;
856 }
857
858 memcpy( _ctx->key->data, data, data_len );
859 _rc = _rc || oaes_key_expand( ctx );
860
861 if( _rc != OAES_RET_SUCCESS )
862 {
863 oaes_key_destroy( &(_ctx->key) );
864 return _rc;
865 }
866
867 return OAES_RET_SUCCESS;
868}
869
870OAES_CTX * oaes_alloc()
871{
872 oaes_ctx * _ctx = (oaes_ctx *) calloc( sizeof( oaes_ctx ), 1 );
873
874 if( NULL == _ctx )
875 return NULL;
876
877#ifdef OAES_HAVE_ISAAC
878 {
879 ub4 _i = 0;
880 char _seed[RANDSIZ + 1];
881
882 _ctx->rctx = (randctx *) calloc( sizeof( randctx ), 1 );
883
884 if( NULL == _ctx->rctx )
885 {
886 free( _ctx );
887 return NULL;
888 }
889
890 oaes_get_seed( _seed );
891 memset( _ctx->rctx->randrsl, 0, RANDSIZ );
892 memcpy( _ctx->rctx->randrsl, _seed, RANDSIZ );
893 randinit( _ctx->rctx, TRUE);
894 }
895#else
896 srand( oaes_get_seed() );
897#endif // OAES_HAVE_ISAAC
898
899 _ctx->key = NULL;
900 oaes_set_option( _ctx, OAES_OPTION_CBC, NULL );
901
902#ifdef OAES_DEBUG
903 _ctx->step_cb = NULL;
904 oaes_set_option( _ctx, OAES_OPTION_STEP_OFF, NULL );
905#endif // OAES_DEBUG
906
907 return (OAES_CTX *) _ctx;
908}
909
910OAES_RET oaes_free( OAES_CTX ** ctx )
911{
912 oaes_ctx ** _ctx = (oaes_ctx **) ctx;
913
914 if( NULL == _ctx )
915 return OAES_RET_ARG1;
916
917 if( NULL == *_ctx )
918 return OAES_RET_SUCCESS;
919
920 if( (*_ctx)->key )
921 oaes_key_destroy( &((*_ctx)->key) );
922
923#ifdef OAES_HAVE_ISAAC
924 if( (*_ctx)->rctx )
925 {
926 free( (*_ctx)->rctx );
927 (*_ctx)->rctx = NULL;
928 }
929#endif // OAES_HAVE_ISAAC
930
931 free( *_ctx );
932 *_ctx = NULL;
933
934 return OAES_RET_SUCCESS;
935}
936
937OAES_RET oaes_set_option( OAES_CTX * ctx,
938 OAES_OPTION option, const void * value )
939{
940 size_t _i;
941 oaes_ctx * _ctx = (oaes_ctx *) ctx;
942
943 if( NULL == _ctx )
944 return OAES_RET_ARG1;
945
946 switch( option )
947 {
948 case OAES_OPTION_ECB:
949 _ctx->options &= ~OAES_OPTION_CBC;
950 memset( _ctx->iv, 0, OAES_BLOCK_SIZE );
951 break;
952
953 case OAES_OPTION_CBC:
954 _ctx->options &= ~OAES_OPTION_ECB;
955 if( value )
956 memcpy( _ctx->iv, value, OAES_BLOCK_SIZE );
957 else
958 {
959 for( _i = 0; _i < OAES_BLOCK_SIZE; _i++ )
960#ifdef OAES_HAVE_ISAAC
961 _ctx->iv[_i] = (uint8_t) rand( _ctx->rctx );
962#else
963 _ctx->iv[_i] = (uint8_t) rand();
964#endif // OAES_HAVE_ISAAC
965 }
966 break;
967
968#ifdef OAES_DEBUG
969
970 case OAES_OPTION_STEP_ON:
971 if( value )
972 {
973 _ctx->options &= ~OAES_OPTION_STEP_OFF;
974 _ctx->step_cb = value;
975 }
976 else
977 {
978 _ctx->options &= ~OAES_OPTION_STEP_ON;
979 _ctx->options |= OAES_OPTION_STEP_OFF;
980 _ctx->step_cb = NULL;
981 return OAES_RET_ARG3;
982 }
983 break;
984
985 case OAES_OPTION_STEP_OFF:
986 _ctx->options &= ~OAES_OPTION_STEP_ON;
987 _ctx->step_cb = NULL;
988 break;
989
990#endif // OAES_DEBUG
991
992 default:
993 return OAES_RET_ARG2;
994 }
995
996 _ctx->options |= option;
997
998 return OAES_RET_SUCCESS;
999}
1000
1001static OAES_RET oaes_encrypt_block(
1002 OAES_CTX * ctx, uint8_t * c, size_t c_len )
1003{
1004 size_t _i, _j;
1005 oaes_ctx * _ctx = (oaes_ctx *) ctx;
1006
1007 if( NULL == _ctx )
1008 return OAES_RET_ARG1;
1009
1010 if( NULL == c )
1011 return OAES_RET_ARG2;
1012
1013 if( c_len != OAES_BLOCK_SIZE )
1014 return OAES_RET_ARG3;
1015
1016 if( NULL == _ctx->key )
1017 return OAES_RET_NOKEY;
1018
1019#ifdef OAES_DEBUG
1020 if( _ctx->step_cb )
1021 _ctx->step_cb( c, "input", 1, NULL );
1022#endif // OAES_DEBUG
1023
1024 // AddRoundKey(State, K0)
1025 for( _i = 0; _i < c_len; _i++ )
1026 c[_i] = c[_i] ^ _ctx->key->exp_data[_i];
1027
1028#ifdef OAES_DEBUG
1029 if( _ctx->step_cb )
1030 {
1031 _ctx->step_cb( _ctx->key->exp_data, "k_sch", 1, NULL );
1032 _ctx->step_cb( c, "k_add", 1, NULL );
1033 }
1034#endif // OAES_DEBUG
1035
1036 // for round = 1 step 1 to Nr–1
1037 for( _i = 1; _i < _ctx->key->num_keys - 1; _i++ )
1038 {
1039 // SubBytes(state)
1040 for( _j = 0; _j < c_len; _j++ )
1041 oaes_sub_byte( c + _j );
1042
1043#ifdef OAES_DEBUG
1044 if( _ctx->step_cb )
1045 _ctx->step_cb( c, "s_box", _i, NULL );
1046#endif // OAES_DEBUG
1047
1048 // ShiftRows(state)
1049 oaes_shift_rows( c );
1050
1051#ifdef OAES_DEBUG
1052 if( _ctx->step_cb )
1053 _ctx->step_cb( c, "s_row", _i, NULL );
1054#endif // OAES_DEBUG
1055
1056 // MixColumns(state)
1057 oaes_mix_cols( c );
1058 oaes_mix_cols( c + 4 );
1059 oaes_mix_cols( c + 8 );
1060 oaes_mix_cols( c + 12 );
1061
1062#ifdef OAES_DEBUG
1063 if( _ctx->step_cb )
1064 _ctx->step_cb( c, "m_col", _i, NULL );
1065#endif // OAES_DEBUG
1066
1067 // AddRoundKey(state, w[round*Nb, (round+1)*Nb-1])
1068 for( _j = 0; _j < c_len; _j++ )
1069 c[_j] = c[_j] ^
1070 _ctx->key->exp_data[_i * OAES_RKEY_LEN * OAES_COL_LEN + _j];
1071
1072#ifdef OAES_DEBUG
1073 if( _ctx->step_cb )
1074 {
1075 _ctx->step_cb( _ctx->key->exp_data + _i * OAES_RKEY_LEN * OAES_COL_LEN,
1076 "k_sch", _i, NULL );
1077 _ctx->step_cb( c, "k_add", _i, NULL );
1078 }
1079#endif // OAES_DEBUG
1080
1081 }
1082
1083 // SubBytes(state)
1084 for( _i = 0; _i < c_len; _i++ )
1085 oaes_sub_byte( c + _i );
1086
1087#ifdef OAES_DEBUG
1088 if( _ctx->step_cb )
1089 _ctx->step_cb( c, "s_box", _ctx->key->num_keys - 1, NULL );
1090#endif // OAES_DEBUG
1091
1092 // ShiftRows(state)
1093 oaes_shift_rows( c );
1094
1095#ifdef OAES_DEBUG
1096 if( _ctx->step_cb )
1097 _ctx->step_cb( c, "s_row", _ctx->key->num_keys - 1, NULL );
1098#endif // OAES_DEBUG
1099
1100 // AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
1101 for( _i = 0; _i < c_len; _i++ )
1102 c[_i] = c[_i] ^ _ctx->key->exp_data[
1103 ( _ctx->key->num_keys - 1 ) * OAES_RKEY_LEN * OAES_COL_LEN + _i ];
1104
1105#ifdef OAES_DEBUG
1106 if( _ctx->step_cb )
1107 {
1108 _ctx->step_cb( _ctx->key->exp_data +
1109 ( _ctx->key->num_keys - 1 ) * OAES_RKEY_LEN * OAES_COL_LEN,
1110 "k_sch", _ctx->key->num_keys - 1, NULL );
1111 _ctx->step_cb( c, "output", _ctx->key->num_keys - 1, NULL );
1112 }
1113#endif // OAES_DEBUG
1114
1115 return OAES_RET_SUCCESS;
1116}
1117
1118static OAES_RET oaes_decrypt_block(
1119 OAES_CTX * ctx, uint8_t * c, size_t c_len )
1120{
1121 size_t _i, _j;
1122 oaes_ctx * _ctx = (oaes_ctx *) ctx;
1123
1124 if( NULL == _ctx )
1125 return OAES_RET_ARG1;
1126
1127 if( NULL == c )
1128 return OAES_RET_ARG2;
1129
1130 if( c_len != OAES_BLOCK_SIZE )
1131 return OAES_RET_ARG3;
1132
1133 if( NULL == _ctx->key )
1134 return OAES_RET_NOKEY;
1135
1136#ifdef OAES_DEBUG
1137 if( _ctx->step_cb )
1138 _ctx->step_cb( c, "iinput", _ctx->key->num_keys - 1, NULL );
1139#endif // OAES_DEBUG
1140
1141 // AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
1142 for( _i = 0; _i < c_len; _i++ )
1143 c[_i] = c[_i] ^ _ctx->key->exp_data[
1144 ( _ctx->key->num_keys - 1 ) * OAES_RKEY_LEN * OAES_COL_LEN + _i ];
1145
1146#ifdef OAES_DEBUG
1147 if( _ctx->step_cb )
1148 {
1149 _ctx->step_cb( _ctx->key->exp_data +
1150 ( _ctx->key->num_keys - 1 ) * OAES_RKEY_LEN * OAES_COL_LEN,
1151 "ik_sch", _ctx->key->num_keys - 1, NULL );
1152 _ctx->step_cb( c, "ik_add", _ctx->key->num_keys - 1, NULL );
1153 }
1154#endif // OAES_DEBUG
1155
1156 for( _i = _ctx->key->num_keys - 2; _i > 0; _i-- )
1157 {
1158 // InvShiftRows(state)
1159 oaes_inv_shift_rows( c );
1160
1161#ifdef OAES_DEBUG
1162 if( _ctx->step_cb )
1163 _ctx->step_cb( c, "is_row", _i, NULL );
1164#endif // OAES_DEBUG
1165
1166 // InvSubBytes(state)
1167 for( _j = 0; _j < c_len; _j++ )
1168 oaes_inv_sub_byte( c + _j );
1169
1170#ifdef OAES_DEBUG
1171 if( _ctx->step_cb )
1172 _ctx->step_cb( c, "is_box", _i, NULL );
1173#endif // OAES_DEBUG
1174
1175 // AddRoundKey(state, w[round*Nb, (round+1)*Nb-1])
1176 for( _j = 0; _j < c_len; _j++ )
1177 c[_j] = c[_j] ^
1178 _ctx->key->exp_data[_i * OAES_RKEY_LEN * OAES_COL_LEN + _j];
1179
1180#ifdef OAES_DEBUG
1181 if( _ctx->step_cb )
1182 {
1183 _ctx->step_cb( _ctx->key->exp_data + _i * OAES_RKEY_LEN * OAES_COL_LEN,
1184 "ik_sch", _i, NULL );
1185 _ctx->step_cb( c, "ik_add", _i, NULL );
1186 }
1187#endif // OAES_DEBUG
1188
1189 // InvMixColums(state)
1190 oaes_inv_mix_cols( c );
1191 oaes_inv_mix_cols( c + 4 );
1192 oaes_inv_mix_cols( c + 8 );
1193 oaes_inv_mix_cols( c + 12 );
1194
1195#ifdef OAES_DEBUG
1196 if( _ctx->step_cb )
1197 _ctx->step_cb( c, "im_col", _i, NULL );
1198#endif // OAES_DEBUG
1199
1200 }
1201
1202 // InvShiftRows(state)
1203 oaes_inv_shift_rows( c );
1204
1205#ifdef OAES_DEBUG
1206 if( _ctx->step_cb )
1207 _ctx->step_cb( c, "is_row", 1, NULL );
1208#endif // OAES_DEBUG
1209
1210 // InvSubBytes(state)
1211 for( _i = 0; _i < c_len; _i++ )
1212 oaes_inv_sub_byte( c + _i );
1213
1214#ifdef OAES_DEBUG
1215 if( _ctx->step_cb )
1216 _ctx->step_cb( c, "is_box", 1, NULL );
1217#endif // OAES_DEBUG
1218
1219 // AddRoundKey(state, w[0, Nb-1])
1220 for( _i = 0; _i < c_len; _i++ )
1221 c[_i] = c[_i] ^ _ctx->key->exp_data[_i];
1222
1223#ifdef OAES_DEBUG
1224 if( _ctx->step_cb )
1225 {
1226 _ctx->step_cb( _ctx->key->exp_data, "ik_sch", 1, NULL );
1227 _ctx->step_cb( c, "ioutput", 1, NULL );
1228 }
1229#endif // OAES_DEBUG
1230
1231 return OAES_RET_SUCCESS;
1232}
1233
1234OAES_RET oaes_encrypt( OAES_CTX * ctx,
1235 const uint8_t * m, size_t m_len, uint8_t * c, size_t * c_len )
1236{
1237 size_t _i, _j, _c_len_in, _c_data_len;
1238 size_t _pad_len = m_len % OAES_BLOCK_SIZE == 0 ?
1239 0 : OAES_BLOCK_SIZE - m_len % OAES_BLOCK_SIZE;
1240 oaes_ctx * _ctx = (oaes_ctx *) ctx;
1241 OAES_RET _rc = OAES_RET_SUCCESS;
1242 uint8_t _flags = _pad_len ? OAES_FLAG_PAD : 0;
1243
1244 if( NULL == _ctx )
1245 return OAES_RET_ARG1;
1246
1247 if( NULL == m )
1248 return OAES_RET_ARG2;
1249
1250 if( NULL == c_len )
1251 return OAES_RET_ARG5;
1252
1253 _c_len_in = *c_len;
1254 // data + pad
1255 _c_data_len = m_len + _pad_len;
1256 // header + iv + data + pad
1257 *c_len = 2 * OAES_BLOCK_SIZE + m_len + _pad_len;
1258
1259 if( NULL == c )
1260 return OAES_RET_SUCCESS;
1261
1262 if( _c_len_in < *c_len )
1263 return OAES_RET_BUF;
1264
1265 if( NULL == _ctx->key )
1266 return OAES_RET_NOKEY;
1267
1268 // header
1269 memcpy(c, oaes_header, OAES_BLOCK_SIZE );
1270 memcpy(c + 6, &_ctx->options, sizeof(_ctx->options));
1271 memcpy(c + 8, &_flags, sizeof(_flags));
1272 // iv
1273 memcpy(c + OAES_BLOCK_SIZE, _ctx->iv, OAES_BLOCK_SIZE );
1274 // data
1275 memcpy(c + 2 * OAES_BLOCK_SIZE, m, m_len );
1276
1277 for( _i = 0; _i < _c_data_len; _i += OAES_BLOCK_SIZE )
1278 {
1279 uint8_t _block[OAES_BLOCK_SIZE];
1280 size_t _block_size = min( m_len - _i, OAES_BLOCK_SIZE );
1281
1282 memcpy( _block, c + 2 * OAES_BLOCK_SIZE + _i, _block_size );
1283
1284 // insert pad
1285 for( _j = 0; _j < OAES_BLOCK_SIZE - _block_size; _j++ )
1286 _block[ _block_size + _j ] = _j + 1;
1287
1288 // CBC
1289 if( _ctx->options & OAES_OPTION_CBC )
1290 {
1291 for( _j = 0; _j < OAES_BLOCK_SIZE; _j++ )
1292 _block[_j] = _block[_j] ^ _ctx->iv[_j];
1293 }
1294
1295 _rc = _rc ||
1296 oaes_encrypt_block( ctx, _block, OAES_BLOCK_SIZE );
1297 memcpy( c + 2 * OAES_BLOCK_SIZE + _i, _block, OAES_BLOCK_SIZE );
1298
1299 if( _ctx->options & OAES_OPTION_CBC )
1300 memcpy( _ctx->iv, _block, OAES_BLOCK_SIZE );
1301 }
1302
1303 return _rc;
1304}
1305
1306OAES_RET oaes_decrypt( OAES_CTX * ctx,
1307 const uint8_t * c, size_t c_len, uint8_t * m, size_t * m_len )
1308{
1309 size_t _i, _j, _m_len_in;
1310 oaes_ctx * _ctx = (oaes_ctx *) ctx;
1311 OAES_RET _rc = OAES_RET_SUCCESS;
1312 uint8_t _iv[OAES_BLOCK_SIZE];
1313 uint8_t _flags;
1314 OAES_OPTION _options;
1315
1316 if( NULL == ctx )
1317 return OAES_RET_ARG1;
1318
1319 if( NULL == c )
1320 return OAES_RET_ARG2;
1321
1322 if( c_len % OAES_BLOCK_SIZE )
1323 return OAES_RET_ARG3;
1324
1325 if( NULL == m_len )
1326 return OAES_RET_ARG5;
1327
1328 _m_len_in = *m_len;
1329 *m_len = c_len - 2 * OAES_BLOCK_SIZE;
1330
1331 if( NULL == m )
1332 return OAES_RET_SUCCESS;
1333
1334 if( _m_len_in < *m_len )
1335 return OAES_RET_BUF;
1336
1337 if( NULL == _ctx->key )
1338 return OAES_RET_NOKEY;
1339
1340 // header
1341 if( 0 != memcmp( c, oaes_header, 4 ) )
1342 return OAES_RET_HEADER;
1343
1344 // header version
1345 switch( c[4] )
1346 {
1347 case 0x01:
1348 break;
1349 default:
1350 return OAES_RET_HEADER;
1351 }
1352
1353 // header type
1354 switch( c[5] )
1355 {
1356 case 0x02:
1357 break;
1358 default:
1359 return OAES_RET_HEADER;
1360 }
1361
1362 // options
1363 memcpy(&_options, c + 6, sizeof(_options));
1364 // validate that all options are valid
1365 if( _options & ~(
1366 OAES_OPTION_ECB
1367 | OAES_OPTION_CBC
1368#ifdef OAES_DEBUG
1369 | OAES_OPTION_STEP_ON
1370 | OAES_OPTION_STEP_OFF
1371#endif // OAES_DEBUG
1372 ) )
1373 return OAES_RET_HEADER;
1374 if( ( _options & OAES_OPTION_ECB ) &&
1375 ( _options & OAES_OPTION_CBC ) )
1376 return OAES_RET_HEADER;
1377 if( _options == OAES_OPTION_NONE )
1378 return OAES_RET_HEADER;
1379
1380 // flags
1381 memcpy(&_flags, c + 8, sizeof(_flags));
1382 // validate that all flags are valid
1383 if( _flags & ~(
1384 OAES_FLAG_PAD
1385 ) )
1386 return OAES_RET_HEADER;
1387
1388 // iv
1389 memcpy( _iv, c + OAES_BLOCK_SIZE, OAES_BLOCK_SIZE);
1390 // data + pad
1391 memcpy( m, c + 2 * OAES_BLOCK_SIZE, *m_len );
1392
1393 for( _i = 0; _i < *m_len; _i += OAES_BLOCK_SIZE )
1394 {
1395 if( ( _options & OAES_OPTION_CBC ) && _i > 0 )
1396 memcpy( _iv, c + OAES_BLOCK_SIZE + _i, OAES_BLOCK_SIZE );
1397
1398 _rc = _rc ||
1399 oaes_decrypt_block( ctx, m + _i, min( *m_len - _i, OAES_BLOCK_SIZE ) );
1400
1401 // CBC
1402 if( _options & OAES_OPTION_CBC )
1403 {
1404 for( _j = 0; _j < OAES_BLOCK_SIZE; _j++ )
1405 m[ _i + _j ] = m[ _i + _j ] ^ _iv[_j];
1406 }
1407 }
1408
1409 // remove pad
1410 if( _flags & OAES_FLAG_PAD )
1411 {
1412 int _is_pad = 1;
1413 size_t _temp = (size_t) m[*m_len - 1];
1414
1415 if( _temp <= 0x00 || _temp > 0x0f )
1416 return OAES_RET_HEADER;
1417 for( _i = 0; _i < _temp; _i++ )
1418 if( m[*m_len - 1 - _i] != _temp - _i )
1419 _is_pad = 0;
1420 if( _is_pad )
1421 {
1422 memset( m + *m_len - _temp, 0, _temp );
1423 *m_len -= _temp;
1424 }
1425 else
1426 return OAES_RET_HEADER;
1427 }
1428
1429 return OAES_RET_SUCCESS;
1430}