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