-
Notifications
You must be signed in to change notification settings - Fork 11
/
sboxalg.c
179 lines (153 loc) · 4.02 KB
/
sboxalg.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
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
/* sbox.c
*
* by: David Canright
*
* illustrates compact implementation of AES S-box via subfield operations
* case # 4 : [d^16, d], [alpha^8, alpha^2], [Omega^2, Omega]
* nu = beta^8 = N^2*alpha^2, N = w^2
*/
#include <stdio.h>
#include <sys/types.h>
/* to convert between polynomial (A^7...1) basis A & normal basis X */
/* or to basis S which incorporates bit matrix of Sbox */
static int A2X[8] = {0x98, 0xF3, 0xF2, 0x48, 0x09, 0x81, 0xA9, 0xFF},
X2A[8] = {0x64, 0x78, 0x6E, 0x8C, 0x68, 0x29, 0xDE, 0x60},
X2S[8] = {0x58, 0x2D, 0x9E, 0x0B, 0xDC, 0x04, 0x03, 0x24},
S2X[8] = {0x8C, 0x79, 0x05, 0xEB, 0x12, 0x04, 0x51, 0x53};
/* multiply in GF(2^2), using normal basis (Omega^2,Omega) */
int G4_mul(int x, int y) {
int a, b, c, d, e, p, q;
a = (x & 0x2) >> 1;
b = (x & 0x1);
c = (y & 0x2) >> 1;
d = (y & 0x1);
e = (a ^ b) & (c ^ d);
p = (a & c) ^ e;
q = (b & d) ^ e;
return ((p << 1) | q);
}
/* scale by N = Omega^2 in GF(2^2), using normal basis (Omega^2,Omega) */
int G4_scl_N(int x) {
int a, b, p, q;
a = (x & 0x2) >> 1;
b = (x & 0x1);
p = b;
q = a ^ b;
return ((p << 1) | q);
}
/* scale by N^2 = Omega in GF(2^2), using normal basis (Omega^2,Omega) */
int G4_scl_N2(int x) {
int a, b, p, q;
a = (x & 0x2) >> 1;
b = (x & 0x1);
p = a ^ b;
q = a;
return ((p << 1) | q);
}
/* square in GF(2^2), using normal basis (Omega^2,Omega) */
/* NOTE: inverse is identical */
int G4_sq(int x) {
int a, b;
a = (x & 0x2) >> 1;
b = (x & 0x1);
return ((b << 1) | a);
}
/* multiply in GF(2^4), using normal basis (alpha^8,alpha^2) */
int G16_mul(int x, int y) {
int a, b, c, d, e, p, q;
a = (x & 0xC) >> 2;
b = (x & 0x3);
c = (y & 0xC) >> 2;
d = (y & 0x3);
e = G4_mul(a ^ b, c ^ d);
e = G4_scl_N(e);
p = G4_mul(a, c) ^ e;
q = G4_mul(b, d) ^ e;
return ((p << 2) | q);
}
/* square & scale by nu in GF(2^4)/GF(2^2), normal basis (alpha^8,alpha^2) */
/* nu = beta^8 = N^2*alpha^2, N = w^2 */
int G16_sq_scl(int x) {
int a, b, p, q;
a = (x & 0xC) >> 2;
b = (x & 0x3);
p = G4_sq(a ^ b);
q = G4_scl_N2(G4_sq(b));
return ((p << 2) | q);
}
/* inverse in GF(2^4), using normal basis (alpha^8,alpha^2) */
int G16_inv(int x) {
int a, b, c, d, e, p, q;
a = (x & 0xC) >> 2;
b = (x & 0x3);
c = G4_scl_N(G4_sq(a ^ b));
d = G4_mul(a, b);
e = G4_sq(c ^ d); // really inverse, but same as square
p = G4_mul(e, b);
q = G4_mul(e, a);
return ((p << 2) | q);
}
/* inverse in GF(2^8), using normal basis (d^16,d) */
int G256_inv(int x) {
int a, b, c, d, e, p, q;
a = (x & 0xF0) >> 4;
b = (x & 0x0F);
c = G16_sq_scl(a ^ b);
d = G16_mul(a, b);
e = G16_inv(c ^ d);
p = G16_mul(e, b);
q = G16_mul(e, a);
return ((p << 4) | q);
}
/* convert to new basis in GF(2^8) */
/* i.e., bit matrix multiply */
int G256_newbasis(int x, int b[]) {
int i, y = 0;
for (i = 7; i >= 0; i--) {
if (x & 1)
y ^= b[i];
x >>= 1;
}
return (y);
}
/* find Sbox of n in GF(2^8) mod POLY */
int Sbox(int n) {
int t;
t = G256_newbasis(n, A2X);
t = G256_inv(t);
t = G256_newbasis(t, X2S);
return (t ^ 0x63);
}
/* find inverse Sbox of n in GF(2^8) mod POLY */
int iSbox(int n) {
int t;
t = G256_newbasis(n ^ 0x63, S2X);
t = G256_inv(t);
t = G256_newbasis(t, X2A);
return (t);
}
/* compute tables of Sbox & its inverse; print 'em out */
int main(void) {
int Sbox_tbl[256], iSbox_tbl[256], i, j;
for (i = 0; i < 256; i++) {
Sbox_tbl[i] = Sbox(i);
iSbox_tbl[i] = iSbox(i);
}
printf("char S[256] = {\n");
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
printf("%3d, ", Sbox_tbl[i * 16 + j]);
}
printf("\n");
}
printf("};\n\n");
printf("char Si[256] = {\n");
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
printf("%3d, ", iSbox_tbl[i * 16 + j]);
}
printf("\n");
}
printf("};\n\n");
return (0);
}