blob: 2e281594c086508d928ab25773359039abe79898 [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 */
Ethan Yonker58f21322018-08-24 11:17:36 -050030/*static const char _NR[] = {
Dees_Troy83bd4832013-05-04 12:39:56 +000031 0x4e,0x61,0x62,0x69,0x6c,0x20,0x53,0x2e,0x20,
Ethan Yonker58f21322018-08-24 11:17:36 -050032 0x41,0x6c,0x20,0x52,0x61,0x6d,0x6c,0x69,0x00 };*/
Dees_Troy83bd4832013-05-04 12:39:56 +000033
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 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
Ethan Yonker58f21322018-08-24 11:17:36 -050099 /*0*/{ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76},
100 /*1*/{ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0},
101 /*2*/{ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15},
102 /*3*/{ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75},
103 /*4*/{ 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84},
104 /*5*/{ 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf},
105 /*6*/{ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8},
106 /*7*/{ 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2},
107 /*8*/{ 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73},
108 /*9*/{ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb},
109 /*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},
Dees_Troy83bd4832013-05-04 12:39:56 +0000115};
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,
Ethan Yonker58f21322018-08-24 11:17:36 -0500119 /*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, 0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/*c*/{ 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f},
132 /*d*/{ 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef},
133 /*e*/{ 0xa0, 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},
Dees_Troy83bd4832013-05-04 12:39:56 +0000135};
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,
Ethan Yonker58f21322018-08-24 11:17:36 -0500139 /*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,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f7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5},
Dees_Troy83bd4832013-05-04 12:39:56 +0000155};
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,
Ethan Yonker58f21322018-08-24 11:17:36 -0500159 /*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, 0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21},
161 /*2*/{ 0x60, 0x63, 0x66, 0x65, 0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71},
162 /*3*/{ 0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d, 0x44, 0x47, 0x42, 0x41},
163 /*4*/{ 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9, 0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1},
164 /*5*/{ 0xf0, 0xf3, 0xf6, 0xf5, 0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1},
165 /*6*/{ 0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd, 0xb4, 0xb7, 0xb2, 0xb1},
166 /*7*/{ 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99, 0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81},
167 /*8*/{ 0x9b, 0x98, 0x9d, 0x9e, 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a},
168 /*9*/{ 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, 0xbf, 0xbc, 0xb9, 0xba},
169 /*a*/{ 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea},
170 /*b*/{ 0xcb, 0xc8, 0xcd, 0xce, 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 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},
Dees_Troy83bd4832013-05-04 12:39:56 +0000175};
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,
Ethan Yonker58f21322018-08-24 11:17:36 -0500179 /*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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22, 0x39, 0x30},
191 /*c*/{ 0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed},
192 /*d*/{ 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d},
193 /*e*/{ 0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6},
194 /*f*/{ 0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46},
Dees_Troy83bd4832013-05-04 12:39:56 +0000195};
196
197static uint8_t oaes_gf_mul_b[16][16] = {
198 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
Ethan Yonker58f21322018-08-24 11:17:36 -0500199 /*0*/{ 0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45, 0x74, 0x7f, 0x62, 0x69},
200 /*1*/{ 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81, 0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9},
201 /*2*/{ 0x7b, 0x70, 0x6d, 0x66, 0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12},
202 /*3*/{ 0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e, 0xbf, 0xb4, 0xa9, 0xa2},
203 /*4*/{ 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7, 0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89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d7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3},
Dees_Troy83bd4832013-05-04 12:39:56 +0000215};
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,
Ethan Yonker58f21322018-08-24 11:17:36 -0500219 /*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, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20},
223 /*4*/{ 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26},
224 /*5*/{ 0xbd, 0xb0, 0xa7, 0xaa, 0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6},
225 /*6*/{ 0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9, 0x8a, 0x87, 0x90, 0x9d},
226 /*7*/{ 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25, 0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d},
227 /*8*/{ 0xda, 0xd7, 0xc0, 0xcd, 0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91},
228 /*9*/{ 0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75, 0x56, 0x5b, 0x4c, 0x41},
229 /*a*/{ 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42, 0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a},
230 /*b*/{ 0xb1, 0xbc, 0xab, 0xa6, 0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa},
231 /*c*/{ 0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8, 0xeb, 0xe6, 0xf1, 0xfc},
232 /*d*/{ 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44, 0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c},
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},
Dees_Troy83bd4832013-05-04 12:39:56 +0000235};
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,
Ethan Yonker58f21322018-08-24 11:17:36 -0500239 /*0*/{ 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a},
240 /*1*/{ 0xe0, 0xee, 0xfc, 0xf2, 0xd8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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},
Dees_Troy83bd4832013-05-04 12:39:56 +0000255};
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
Ethan Yonker58f21322018-08-24 11:17:36 -0500289/*static OAES_RET oaes_word_rot_right( uint8_t word[OAES_COL_LEN] )
Dees_Troy83bd4832013-05-04 12:39:56 +0000290{
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;
Ethan Yonker58f21322018-08-24 11:17:36 -0500301}*/
Dees_Troy83bd4832013-05-04 12:39:56 +0000302
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
Ethan Yonker58f21322018-08-24 11:17:36 -0500786 if( data_len != (size_t)_key_length + OAES_BLOCK_SIZE )
Dees_Troy83bd4832013-05-04 12:39:56 +0000787 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 {
Dees_Troy83bd4832013-05-04 12:39:56 +0000880 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}