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