1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/**
* \file
* \author Egor Tensin <Egor.Tensin@gmail.com>
* \date 2015
* \copyright This file is licensed under the terms of the MIT License.
* See LICENSE.txt for details.
*/
#include <aesni/all.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
AesNI_StatusCode aesni_aes_format_block(
AesNI_Aes_BlockString* str,
const AesNI_Aes_Block* block,
AesNI_ErrorDetails* err_details)
{
assert(str);
assert(block);
if (str == NULL)
return aesni_make_null_argument_error(err_details, "str");
if (block == NULL)
return aesni_make_null_argument_error(err_details, "block");
char* cursor = str->str;
__declspec(align(16)) unsigned char bytes[16];
aesni_store_block128_aligned(bytes, *block);
for (int i = 0; i < 16; ++i, cursor += 2)
sprintf(cursor, "%02x", bytes[i]);
*cursor = '\0';
return AESNI_SUCCESS;
}
AesNI_StatusCode aesni_aes_format_block_as_matrix(
AesNI_Aes_BlockMatrixString* str,
const AesNI_Aes_Block* block,
AesNI_ErrorDetails* err_details)
{
assert(str);
assert(block);
if (str == NULL)
return aesni_make_null_argument_error(err_details, "str");
if (block == NULL)
return aesni_make_null_argument_error(err_details, "block");
char* cursor = str->str;
__declspec(align(16)) unsigned char bytes[4][4];
aesni_store_block128_aligned(bytes, *block);
for (int i = 0; i < 4; ++i, cursor += 3)
{
for (int j = 0; j < 3; ++j, cursor += 3)
sprintf(cursor, "%02x ", bytes[j][i]);
sprintf(cursor, "%02x\n", bytes[3][i]);
}
*cursor = '\0';
return AESNI_SUCCESS;
}
AesNI_StatusCode aesni_aes_print_block(
const AesNI_Aes_Block* block,
AesNI_ErrorDetails* err_details)
{
assert(block);
if (block == NULL)
return aesni_make_null_argument_error(err_details, "block");
AesNI_StatusCode ec = AESNI_SUCCESS;
AesNI_Aes_BlockString str;
if (aesni_is_error(ec = aesni_aes_format_block(&str, block, err_details)))
return ec;
printf("%s\n", str.str);
return ec;
}
AesNI_StatusCode aesni_aes_print_block_as_matrix(
const AesNI_Aes_Block* block,
AesNI_ErrorDetails* err_details)
{
assert(block);
if (block == NULL)
return aesni_make_null_argument_error(err_details, "block");
AesNI_StatusCode ec = AESNI_SUCCESS;
AesNI_Aes_BlockMatrixString str;
if (aesni_is_error(ec = aesni_aes_format_block_as_matrix(&str, block, err_details)))
return ec;
printf("%s", str.str);
return ec;
}
AesNI_StatusCode aesni_aes_parse_block(
AesNI_Aes_Block* dest,
const char* src,
AesNI_ErrorDetails* err_details)
{
assert(dest);
assert(src);
if (dest == NULL)
return aesni_make_null_argument_error(err_details, "dest");
if (src == NULL)
return aesni_make_null_argument_error(err_details, "src");
__declspec(align(16)) unsigned char bytes[16];
for (int i = 0; i < 16; ++i)
{
int n;
unsigned int byte;
if (sscanf(src, "%2x%n", &byte, &n) != 1)
return aesni_make_parse_error(err_details, src);
bytes[i] = (unsigned char) byte;
src += n;
}
*dest = aesni_load_block128_aligned(bytes);
return AESNI_SUCCESS;
}
AesNI_StatusCode aesni_aes128_format_key(
AesNI_Aes128_KeyString* str,
const AesNI_Aes128_Key* key,
AesNI_ErrorDetails* err_details)
{
return aesni_aes_format_block(str, &key->key, err_details);
}
AesNI_StatusCode aesni_aes192_format_key(
AesNI_Aes192_KeyString* str,
const AesNI_Aes192_Key* key,
AesNI_ErrorDetails* err_details)
{
assert(str);
assert(key);
if (str == NULL)
return aesni_make_null_argument_error(err_details, "str");
if (key == NULL)
return aesni_make_null_argument_error(err_details, "key");
char* cursor = str->str;
{
__declspec(align(16)) unsigned char bytes[16];
aesni_store_block128_aligned(bytes, key->lo);
for (int i = 0; i < 16; ++i, cursor += 2)
sprintf(cursor, "%02x", bytes[i]);
}
{
__declspec(align(16)) unsigned char bytes[16];
aesni_store_block128_aligned(bytes, key->hi);
for (int i = 0; i < 8; ++i, cursor += 2)
sprintf(cursor, "%02x", bytes[i]);
}
*cursor = '\0';
return AESNI_SUCCESS;
}
AesNI_StatusCode aesni_aes256_format_key(
AesNI_Aes256_KeyString* str,
const AesNI_Aes256_Key* key,
AesNI_ErrorDetails* err_details)
{
assert(str);
assert(key);
if (str == NULL)
return aesni_make_null_argument_error(err_details, "str");
if (key == NULL)
return aesni_make_null_argument_error(err_details, "key");
char* cursor = str->str;
{
__declspec(align(16)) unsigned char bytes[16];
aesni_store_block128_aligned(bytes, key->lo);
for (int i = 0; i < 16; ++i, cursor += 2)
sprintf(cursor, "%02x", bytes[i]);
}
{
__declspec(align(16)) unsigned char bytes[16];
aesni_store_block128_aligned(bytes, key->hi);
for (int i = 0; i < 16; ++i, cursor += 2)
sprintf(cursor, "%02x", bytes[i]);
}
*cursor = '\0';
return AESNI_SUCCESS;
}
AesNI_StatusCode aesni_aes128_print_key(
const AesNI_Aes128_Key* key,
AesNI_ErrorDetails* err_details)
{
return aesni_aes_print_block(&key->key, err_details);
}
AesNI_StatusCode aesni_aes192_print_key(
const AesNI_Aes192_Key* key,
AesNI_ErrorDetails* err_details)
{
assert(key);
if (key == NULL)
return aesni_make_null_argument_error(err_details, "key");
AesNI_StatusCode ec = AESNI_SUCCESS;
AesNI_Aes192_KeyString str;
if (aesni_is_error(ec = aesni_aes192_format_key(&str, key, err_details)))
return ec;
printf("%s\n", str.str);
return ec;
}
AesNI_StatusCode aesni_aes256_print_key(
const AesNI_Aes256_Key* key,
AesNI_ErrorDetails* err_details)
{
assert(key);
if (key == NULL)
return aesni_make_null_argument_error(err_details, "key");
AesNI_StatusCode ec = AESNI_SUCCESS;
AesNI_Aes256_KeyString str;
if (aesni_is_error(ec = aesni_aes256_format_key(&str, key, err_details)))
return ec;
printf("%s\n", str.str);
return ec;
}
AesNI_StatusCode aesni_aes128_parse_key(
AesNI_Aes128_Key* dest,
const char* src,
AesNI_ErrorDetails* err_details)
{
return aesni_aes_parse_block(&dest->key, src, err_details);
}
AesNI_StatusCode aesni_aes192_parse_key(
AesNI_Aes192_Key* dest,
const char* src,
AesNI_ErrorDetails* err_details)
{
assert(dest);
assert(src);
if (dest == NULL)
return aesni_make_null_argument_error(err_details, "dest");
if (src == NULL)
return aesni_make_null_argument_error(err_details, "src");
{
__declspec(align(16)) unsigned char bytes[16];
for (int i = 0; i < 16; ++i)
{
int n;
unsigned int byte;
if (sscanf(src, "%2x%n", &byte, &n) != 1)
return aesni_make_parse_error(err_details, src);
bytes[i] = (unsigned char) byte;
src += n;
}
dest->lo = aesni_load_block128_aligned(bytes);
}
{
__declspec(align(16)) unsigned char bytes[16];
for (int i = 0; i < 8; ++i)
{
int n;
unsigned int byte;
if (sscanf(src, "%2x%n", &byte, &n) != 1)
return aesni_make_parse_error(err_details, src);
bytes[i] = (unsigned char) byte;
src += n;
}
memset(bytes + 8, 0x00, 8);
dest->hi = aesni_load_block128_aligned(bytes);
}
return AESNI_SUCCESS;
}
AesNI_StatusCode aesni_aes256_parse_key(
AesNI_Aes256_Key* dest,
const char* src,
AesNI_ErrorDetails* err_details)
{
assert(dest);
assert(src);
if (dest == NULL)
return aesni_make_null_argument_error(err_details, "dest");
if (src == NULL)
return aesni_make_null_argument_error(err_details, "src");
{
__declspec(align(16)) unsigned char bytes[16];
for (int i = 0; i < 16; ++i)
{
int n;
unsigned int byte;
if (sscanf(src, "%2x%n", &byte, &n) != 1)
return aesni_make_parse_error(err_details, src);
bytes[i] = (unsigned char) byte;
src += n;
}
dest->lo = aesni_load_block128_aligned(bytes);
}
{
__declspec(align(16)) unsigned char bytes[16];
for (int i = 0; i < 16; ++i)
{
int n;
unsigned int byte;
if (sscanf(src, "%2x%n", &byte, &n) != 1)
return aesni_make_parse_error(err_details, src);
bytes[i] = (unsigned char) byte;
src += n;
}
dest->hi = aesni_load_block128_aligned(bytes);
}
return AESNI_SUCCESS;
}
|