Debian Codegraph @

src/crypto/oaes_lib.c

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