forked from unfrozen/stm8_libs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
aes_tables.c
115 lines (98 loc) · 2.17 KB
/
aes_tables.c
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
/*
* File name: aes_tables.c
* Date first: 12/13/2017
* Date last: 12/13/2017
*
* Description: Output AES mix tables.
*
* Author: Richard Hodges
*
* Copyright (C) 2017, Richard Hodges. All rights reserved.
* Permission is granted to use or copy for any purpose.
*
******************************************************************************
*
* Includes
*/
#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/******************************************************************************
*
* Types
*/
#ifndef _BYTE
#define _BYTE
typedef unsigned char BYTE;
#endif
static BYTE mix_mul2(BYTE);
static void print_table(char *, BYTE *);
/******************************************************************************
*
*/
int main(int argc, char *argv[])
{
BYTE x_2[256];
BYTE x_3[256];
BYTE x_9[256];
BYTE x_11[256];
BYTE x_13[256];
BYTE x_14[256];
int i, val;
for (i = 0; i < 256; i++) {
val = i;
x_3[i] = val; /* x 1 */
x_9[i] = val;
x_11[i] = val;
x_13[i] = val;
val = mix_mul2(val); /* x 2 */
x_2[i] = val;
x_3[i] ^= val;
x_11[i] ^= val;
x_14[i] = val;
val = mix_mul2(val); /* x 4 */
x_13[i] ^= val;
x_14[i] ^= val;
val = mix_mul2(val); /* x 8 */
x_9[i] ^= val;
x_11[i] ^= val;
x_13[i] ^= val;
x_14[i] ^= val;
}
print_table("mix_2", x_2);
print_table("mix_3", x_3);
print_table("mix_9", x_9);
print_table("mix_11", x_11);
print_table("mix_13", x_13);
print_table("mix_14", x_14);
return 0;
}
/******************************************************************************
*
* Print mix table
*/
static void print_table(char *tabname, BYTE *table)
{
int i, j;
printf("const BYTE %s[256] = {", tabname);
for (i = 0; i < 32; i++) {
printf("\n ");
for (j = 0; j < 8; j++)
printf("0x%02x, ", *table++);
}
printf("\n};\n");
}
/******************************************************************************
*
* Multiply by two for mix operation
*/
static BYTE mix_mul2(BYTE val)
{
BYTE retval;
retval = val << 1;
if (val & 0x80)
retval ^= 0x1b;
return (retval);
}