Debian Codegraph @

src/main/org/h2/security/AES.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.security;
7
8 import org.h2.util.Bits;
9
10 /**
11 * An implementation of the AES block cipher algorithm,
12 * also known as Rijndael. Only AES-128 is supported by this class.
13 */
14 public class AES implements BlockCipher {
15
16 private static final int[] RCON = new int[10];
17 private static final int[] FS = new int[256];
18 private static final int[] FT0 = new int[256];
19 private static final int[] FT1 = new int[256];
20 private static final int[] FT2 = new int[256];
21 private static final int[] FT3 = new int[256];
22 private static final int[] RS = new int[256];
23 private static final int[] RT0 = new int[256];
24 private static final int[] RT1 = new int[256];
25 private static final int[] RT2 = new int[256];
26 private static final int[] RT3 = new int[256];
27 private final int[] encKey = new int[44];
28 private final int[] decKey = new int[44];
29
30 private static int rot8(int x) {
31 return (x >>> 8) | (x << 24);
32 }
33
34 private static int xtime(int x) {
35 return ((x << 1) ^ (((x & 0x80) != 0) ? 0x1b : 0)) & 255;
36 }
37
38 private static int mul(int[] pow, int[] log, int x, int y) {
39 return (x != 0 && y != 0) ? pow[(log[x] + log[y]) % 255] : 0;
40 }
41
42 static {
43 int[] pow = new int[256];
44 int[] log = new int[256];
45 for (int i = 0, x = 1; i < 256; i++, x ^= xtime(x)) {
46 pow[i] = x;
47 log[x] = i;
48 }
49 for (int i = 0, x = 1; i < 10; i++, x = xtime(x)) {
50 RCON[i] = x << 24;
51 }
52 FS[0x00] = 0x63;
53 RS[0x63] = 0x00;
54 for (int i = 1; i < 256; i++) {
55 int x = pow[255 - log[i]], y = x;
56 y = ((y << 1) | (y >> 7)) & 255;
57 x ^= y;
58 y = ((y << 1) | (y >> 7)) & 255;
59 x ^= y;
60 y = ((y << 1) | (y >> 7)) & 255;
61 x ^= y;
62 y = ((y << 1) | (y >> 7)) & 255;
63 x ^= y ^ 0x63;
64 FS[i] = x & 255;
65 RS[x] = i & 255;
66 }
67 for (int i = 0; i < 256; i++) {
68 int x = FS[i], y = xtime(x);
69 FT0[i] = (x ^ y) ^ (x << 8) ^ (x << 16) ^ (y << 24);
70 FT1[i] = rot8(FT0[i]);
71 FT2[i] = rot8(FT1[i]);
72 FT3[i] = rot8(FT2[i]);
73 y = RS[i];
74 RT0[i] = mul(pow, log, 0x0b, y) ^ (mul(pow, log, 0x0d, y) << 8)
75 ^ (mul(pow, log, 0x09, y) << 16) ^ (mul(pow, log, 0x0e, y) << 24);
76 RT1[i] = rot8(RT0[i]);
77 RT2[i] = rot8(RT1[i]);
78 RT3[i] = rot8(RT2[i]);
79 }
80 }
81
82 private static int getDec(int t) {
83 return RT0[FS[(t >> 24) & 255]] ^ RT1[FS[(t >> 16) & 255]]
84 ^ RT2[FS[(t >> 8) & 255]] ^ RT3[FS[t & 255]];
85 }
86
87 @Override
88 public void setKey(byte[] key) {
89 for (int i = 0, j = 0; i < 4; i++) {
90 encKey[i] = decKey[i] = ((key[j++] & 255) << 24)
91 | ((key[j++] & 255) << 16) | ((key[j++] & 255) << 8)
92 | (key[j++] & 255);
93 }
94 int e = 0;
95 for (int i = 0; i < 10; i++, e += 4) {
96 encKey[e + 4] = encKey[e] ^ RCON[i]
97 ^ (FS[(encKey[e + 3] >> 16) & 255] << 24)
98 ^ (FS[(encKey[e + 3] >> 8) & 255] << 16)
99 ^ (FS[encKey[e + 3] & 255] << 8)
100 ^ FS[(encKey[e + 3] >> 24) & 255];
101 encKey[e + 5] = encKey[e + 1] ^ encKey[e + 4];
102 encKey[e + 6] = encKey[e + 2] ^ encKey[e + 5];
103 encKey[e + 7] = encKey[e + 3] ^ encKey[e + 6];
104 }
105 int d = 0;
106 decKey[d++] = encKey[e++];
107 decKey[d++] = encKey[e++];
108 decKey[d++] = encKey[e++];
109 decKey[d++] = encKey[e++];
110 for (int i = 1; i < 10; i++) {
111 e -= 8;
112 decKey[d++] = getDec(encKey[e++]);
113 decKey[d++] = getDec(encKey[e++]);
114 decKey[d++] = getDec(encKey[e++]);
115 decKey[d++] = getDec(encKey[e++]);
116 }
117 e -= 8;
118 decKey[d++] = encKey[e++];
119 decKey[d++] = encKey[e++];
120 decKey[d++] = encKey[e++];
121 decKey[d] = encKey[e];
122 }
123
124 @Override
125 public void encrypt(byte[] bytes, int off, int len) {
126 for (int i = off; i < off + len; i += 16) {
127 encryptBlock(bytes, bytes, i);
128 }
129 }
130
131 @Override
132 public void decrypt(byte[] bytes, int off, int len) {
133 for (int i = off; i < off + len; i += 16) {
134 decryptBlock(bytes, bytes, i);
135 }
136 }
137
138 private void encryptBlock(byte[] in, byte[] out, int off) {
139 int[] k = encKey;
140 int x0 = Bits.readInt(in, off) ^ k[0];
141 int x1 = Bits.readInt(in, off + 4) ^ k[1];
142 int x2 = Bits.readInt(in, off + 8) ^ k[2];
143 int x3 = Bits.readInt(in, off + 12) ^ k[3];
144 int y0 = FT0[(x0 >> 24) & 255] ^ FT1[(x1 >> 16) & 255]
145 ^ FT2[(x2 >> 8) & 255] ^ FT3[x3 & 255] ^ k[4];
146 int y1 = FT0[(x1 >> 24) & 255] ^ FT1[(x2 >> 16) & 255]
147 ^ FT2[(x3 >> 8) & 255] ^ FT3[x0 & 255] ^ k[5];
148 int y2 = FT0[(x2 >> 24) & 255] ^ FT1[(x3 >> 16) & 255]
149 ^ FT2[(x0 >> 8) & 255] ^ FT3[x1 & 255] ^ k[6];
150 int y3 = FT0[(x3 >> 24) & 255] ^ FT1[(x0 >> 16) & 255]
151 ^ FT2[(x1 >> 8) & 255] ^ FT3[x2 & 255] ^ k[7];
152 x0 = FT0[(y0 >> 24) & 255] ^ FT1[(y1 >> 16) & 255]
153 ^ FT2[(y2 >> 8) & 255] ^ FT3[y3 & 255] ^ k[8];
154 x1 = FT0[(y1 >> 24) & 255] ^ FT1[(y2 >> 16) & 255]
155 ^ FT2[(y3 >> 8) & 255] ^ FT3[y0 & 255] ^ k[9];
156 x2 = FT0[(y2 >> 24) & 255] ^ FT1[(y3 >> 16) & 255]
157 ^ FT2[(y0 >> 8) & 255] ^ FT3[y1 & 255] ^ k[10];
158 x3 = FT0[(y3 >> 24) & 255] ^ FT1[(y0 >> 16) & 255]
159 ^ FT2[(y1 >> 8) & 255] ^ FT3[y2 & 255] ^ k[11];
160 y0 = FT0[(x0 >> 24) & 255] ^ FT1[(x1 >> 16) & 255]
161 ^ FT2[(x2 >> 8) & 255] ^ FT3[x3 & 255] ^ k[12];
162 y1 = FT0[(x1 >> 24) & 255] ^ FT1[(x2 >> 16) & 255]
163 ^ FT2[(x3 >> 8) & 255] ^ FT3[x0 & 255] ^ k[13];
164 y2 = FT0[(x2 >> 24) & 255] ^ FT1[(x3 >> 16) & 255]
165 ^ FT2[(x0 >> 8) & 255] ^ FT3[x1 & 255] ^ k[14];
166 y3 = FT0[(x3 >> 24) & 255] ^ FT1[(x0 >> 16) & 255]
167 ^ FT2[(x1 >> 8) & 255] ^ FT3[x2 & 255] ^ k[15];
168 x0 = FT0[(y0 >> 24) & 255] ^ FT1[(y1 >> 16) & 255]
169 ^ FT2[(y2 >> 8) & 255] ^ FT3[y3 & 255] ^ k[16];
170 x1 = FT0[(y1 >> 24) & 255] ^ FT1[(y2 >> 16) & 255]
171 ^ FT2[(y3 >> 8) & 255] ^ FT3[y0 & 255] ^ k[17];
172 x2 = FT0[(y2 >> 24) & 255] ^ FT1[(y3 >> 16) & 255]
173 ^ FT2[(y0 >> 8) & 255] ^ FT3[y1 & 255] ^ k[18];
174 x3 = FT0[(y3 >> 24) & 255] ^ FT1[(y0 >> 16) & 255]
175 ^ FT2[(y1 >> 8) & 255] ^ FT3[y2 & 255] ^ k[19];
176 y0 = FT0[(x0 >> 24) & 255] ^ FT1[(x1 >> 16) & 255]
177 ^ FT2[(x2 >> 8) & 255] ^ FT3[x3 & 255] ^ k[20];
178 y1 = FT0[(x1 >> 24) & 255] ^ FT1[(x2 >> 16) & 255]
179 ^ FT2[(x3 >> 8) & 255] ^ FT3[x0 & 255] ^ k[21];
180 y2 = FT0[(x2 >> 24) & 255] ^ FT1[(x3 >> 16) & 255]
181 ^ FT2[(x0 >> 8) & 255] ^ FT3[x1 & 255] ^ k[22];
182 y3 = FT0[(x3 >> 24) & 255] ^ FT1[(x0 >> 16) & 255]
183 ^ FT2[(x1 >> 8) & 255] ^ FT3[x2 & 255] ^ k[23];
184 x0 = FT0[(y0 >> 24) & 255] ^ FT1[(y1 >> 16) & 255]
185 ^ FT2[(y2 >> 8) & 255] ^ FT3[y3 & 255] ^ k[24];
186 x1 = FT0[(y1 >> 24) & 255] ^ FT1[(y2 >> 16) & 255]
187 ^ FT2[(y3 >> 8) & 255] ^ FT3[y0 & 255] ^ k[25];
188 x2 = FT0[(y2 >> 24) & 255] ^ FT1[(y3 >> 16) & 255]
189 ^ FT2[(y0 >> 8) & 255] ^ FT3[y1 & 255] ^ k[26];
190 x3 = FT0[(y3 >> 24) & 255] ^ FT1[(y0 >> 16) & 255]
191 ^ FT2[(y1 >> 8) & 255] ^ FT3[y2 & 255] ^ k[27];
192 y0 = FT0[(x0 >> 24) & 255] ^ FT1[(x1 >> 16) & 255]
193 ^ FT2[(x2 >> 8) & 255] ^ FT3[x3 & 255] ^ k[28];
194 y1 = FT0[(x1 >> 24) & 255] ^ FT1[(x2 >> 16) & 255]
195 ^ FT2[(x3 >> 8) & 255] ^ FT3[x0 & 255] ^ k[29];
196 y2 = FT0[(x2 >> 24) & 255] ^ FT1[(x3 >> 16) & 255]
197 ^ FT2[(x0 >> 8) & 255] ^ FT3[x1 & 255] ^ k[30];
198 y3 = FT0[(x3 >> 24) & 255] ^ FT1[(x0 >> 16) & 255]
199 ^ FT2[(x1 >> 8) & 255] ^ FT3[x2 & 255] ^ k[31];
200 x0 = FT0[(y0 >> 24) & 255] ^ FT1[(y1 >> 16) & 255]
201 ^ FT2[(y2 >> 8) & 255] ^ FT3[y3 & 255] ^ k[32];
202 x1 = FT0[(y1 >> 24) & 255] ^ FT1[(y2 >> 16) & 255]
203 ^ FT2[(y3 >> 8) & 255] ^ FT3[y0 & 255] ^ k[33];
204 x2 = FT0[(y2 >> 24) & 255] ^ FT1[(y3 >> 16) & 255]
205 ^ FT2[(y0 >> 8) & 255] ^ FT3[y1 & 255] ^ k[34];
206 x3 = FT0[(y3 >> 24) & 255] ^ FT1[(y0 >> 16) & 255]
207 ^ FT2[(y1 >> 8) & 255] ^ FT3[y2 & 255] ^ k[35];
208 y0 = FT0[(x0 >> 24) & 255] ^ FT1[(x1 >> 16) & 255]
209 ^ FT2[(x2 >> 8) & 255] ^ FT3[x3 & 255] ^ k[36];
210 y1 = FT0[(x1 >> 24) & 255] ^ FT1[(x2 >> 16) & 255]
211 ^ FT2[(x3 >> 8) & 255] ^ FT3[x0 & 255] ^ k[37];
212 y2 = FT0[(x2 >> 24) & 255] ^ FT1[(x3 >> 16) & 255]
213 ^ FT2[(x0 >> 8) & 255] ^ FT3[x1 & 255] ^ k[38];
214 y3 = FT0[(x3 >> 24) & 255] ^ FT1[(x0 >> 16) & 255]
215 ^ FT2[(x1 >> 8) & 255] ^ FT3[x2 & 255] ^ k[39];
216 x0 = ((FS[(y0 >> 24) & 255] << 24) | (FS[(y1 >> 16) & 255] << 16)
217 | (FS[(y2 >> 8) & 255] << 8) | FS[y3 & 255]) ^ k[40];
218 x1 = ((FS[(y1 >> 24) & 255] << 24) | (FS[(y2 >> 16) & 255] << 16)
219 | (FS[(y3 >> 8) & 255] << 8) | FS[y0 & 255]) ^ k[41];
220 x2 = ((FS[(y2 >> 24) & 255] << 24) | (FS[(y3 >> 16) & 255] << 16)
221 | (FS[(y0 >> 8) & 255] << 8) | FS[y1 & 255]) ^ k[42];
222 x3 = ((FS[(y3 >> 24) & 255] << 24) | (FS[(y0 >> 16) & 255] << 16)
223 | (FS[(y1 >> 8) & 255] << 8) | FS[y2 & 255]) ^ k[43];
224 Bits.writeInt(out, off, x0);
225 Bits.writeInt(out, off + 4, x1);
226 Bits.writeInt(out, off + 8, x2);
227 Bits.writeInt(out, off + 12, x3);
228 }
229
230 private void decryptBlock(byte[] in, byte[] out, int off) {
231 int[] k = decKey;
232 int x0 = Bits.readInt(in, off) ^ k[0];
233 int x1 = Bits.readInt(in, off + 4) ^ k[1];
234 int x2 = Bits.readInt(in, off + 8) ^ k[2];
235 int x3 = Bits.readInt(in, off + 12) ^ k[3];
236 int y0 = RT0[(x0 >> 24) & 255] ^ RT1[(x3 >> 16) & 255]
237 ^ RT2[(x2 >> 8) & 255] ^ RT3[x1 & 255] ^ k[4];
238 int y1 = RT0[(x1 >> 24) & 255] ^ RT1[(x0 >> 16) & 255]
239 ^ RT2[(x3 >> 8) & 255] ^ RT3[x2 & 255] ^ k[5];
240 int y2 = RT0[(x2 >> 24) & 255] ^ RT1[(x1 >> 16) & 255]
241 ^ RT2[(x0 >> 8) & 255] ^ RT3[x3 & 255] ^ k[6];
242 int y3 = RT0[(x3 >> 24) & 255] ^ RT1[(x2 >> 16) & 255]
243 ^ RT2[(x1 >> 8) & 255] ^ RT3[x0 & 255] ^ k[7];
244 x0 = RT0[(y0 >> 24) & 255] ^ RT1[(y3 >> 16) & 255]
245 ^ RT2[(y2 >> 8) & 255] ^ RT3[y1 & 255] ^ k[8];
246 x1 = RT0[(y1 >> 24) & 255] ^ RT1[(y0 >> 16) & 255]
247 ^ RT2[(y3 >> 8) & 255] ^ RT3[y2 & 255] ^ k[9];
248 x2 = RT0[(y2 >> 24) & 255] ^ RT1[(y1 >> 16) & 255]
249 ^ RT2[(y0 >> 8) & 255] ^ RT3[y3 & 255] ^ k[10];
250 x3 = RT0[(y3 >> 24) & 255] ^ RT1[(y2 >> 16) & 255]
251 ^ RT2[(y1 >> 8) & 255] ^ RT3[y0 & 255] ^ k[11];
252 y0 = RT0[(x0 >> 24) & 255] ^ RT1[(x3 >> 16) & 255]
253 ^ RT2[(x2 >> 8) & 255] ^ RT3[x1 & 255] ^ k[12];
254 y1 = RT0[(x1 >> 24) & 255] ^ RT1[(x0 >> 16) & 255]
255 ^ RT2[(x3 >> 8) & 255] ^ RT3[x2 & 255] ^ k[13];
256 y2 = RT0[(x2 >> 24) & 255] ^ RT1[(x1 >> 16) & 255]
257 ^ RT2[(x0 >> 8) & 255] ^ RT3[x3 & 255] ^ k[14];
258 y3 = RT0[(x3 >> 24) & 255] ^ RT1[(x2 >> 16) & 255]
259 ^ RT2[(x1 >> 8) & 255] ^ RT3[x0 & 255] ^ k[15];
260 x0 = RT0[(y0 >> 24) & 255] ^ RT1[(y3 >> 16) & 255]
261 ^ RT2[(y2 >> 8) & 255] ^ RT3[y1 & 255] ^ k[16];
262 x1 = RT0[(y1 >> 24) & 255] ^ RT1[(y0 >> 16) & 255]
263 ^ RT2[(y3 >> 8) & 255] ^ RT3[y2 & 255] ^ k[17];
264 x2 = RT0[(y2 >> 24) & 255] ^ RT1[(y1 >> 16) & 255]
265 ^ RT2[(y0 >> 8) & 255] ^ RT3[y3 & 255] ^ k[18];
266 x3 = RT0[(y3 >> 24) & 255] ^ RT1[(y2 >> 16) & 255]
267 ^ RT2[(y1 >> 8) & 255] ^ RT3[y0 & 255] ^ k[19];
268 y0 = RT0[(x0 >> 24) & 255] ^ RT1[(x3 >> 16) & 255]
269 ^ RT2[(x2 >> 8) & 255] ^ RT3[x1 & 255] ^ k[20];
270 y1 = RT0[(x1 >> 24) & 255] ^ RT1[(x0 >> 16) & 255]
271 ^ RT2[(x3 >> 8) & 255] ^ RT3[x2 & 255] ^ k[21];
272 y2 = RT0[(x2 >> 24) & 255] ^ RT1[(x1 >> 16) & 255]
273 ^ RT2[(x0 >> 8) & 255] ^ RT3[x3 & 255] ^ k[22];
274 y3 = RT0[(x3 >> 24) & 255] ^ RT1[(x2 >> 16) & 255]
275 ^ RT2[(x1 >> 8) & 255] ^ RT3[x0 & 255] ^ k[23];
276 x0 = RT0[(y0 >> 24) & 255] ^ RT1[(y3 >> 16) & 255]
277 ^ RT2[(y2 >> 8) & 255] ^ RT3[y1 & 255] ^ k[24];
278 x1 = RT0[(y1 >> 24) & 255] ^ RT1[(y0 >> 16) & 255]
279 ^ RT2[(y3 >> 8) & 255] ^ RT3[y2 & 255] ^ k[25];
280 x2 = RT0[(y2 >> 24) & 255] ^ RT1[(y1 >> 16) & 255]
281 ^ RT2[(y0 >> 8) & 255] ^ RT3[y3 & 255] ^ k[26];
282 x3 = RT0[(y3 >> 24) & 255] ^ RT1[(y2 >> 16) & 255]
283 ^ RT2[(y1 >> 8) & 255] ^ RT3[y0 & 255] ^ k[27];
284 y0 = RT0[(x0 >> 24) & 255] ^ RT1[(x3 >> 16) & 255]
285 ^ RT2[(x2 >> 8) & 255] ^ RT3[x1 & 255] ^ k[28];
286 y1 = RT0[(x1 >> 24) & 255] ^ RT1[(x0 >> 16) & 255]
287 ^ RT2[(x3 >> 8) & 255] ^ RT3[x2 & 255] ^ k[29];
288 y2 = RT0[(x2 >> 24) & 255] ^ RT1[(x1 >> 16) & 255]
289 ^ RT2[(x0 >> 8) & 255] ^ RT3[x3 & 255] ^ k[30];
290 y3 = RT0[(x3 >> 24) & 255] ^ RT1[(x2 >> 16) & 255]
291 ^ RT2[(x1 >> 8) & 255] ^ RT3[x0 & 255] ^ k[31];
292 x0 = RT0[(y0 >> 24) & 255] ^ RT1[(y3 >> 16) & 255]
293 ^ RT2[(y2 >> 8) & 255] ^ RT3[y1 & 255] ^ k[32];
294 x1 = RT0[(y1 >> 24) & 255] ^ RT1[(y0 >> 16) & 255]
295 ^ RT2[(y3 >> 8) & 255] ^ RT3[y2 & 255] ^ k[33];
296 x2 = RT0[(y2 >> 24) & 255] ^ RT1[(y1 >> 16) & 255]
297 ^ RT2[(y0 >> 8) & 255] ^ RT3[y3 & 255] ^ k[34];
298 x3 = RT0[(y3 >> 24) & 255] ^ RT1[(y2 >> 16) & 255]
299 ^ RT2[(y1 >> 8) & 255] ^ RT3[y0 & 255] ^ k[35];
300 y0 = RT0[(x0 >> 24) & 255] ^ RT1[(x3 >> 16) & 255]
301 ^ RT2[(x2 >> 8) & 255] ^ RT3[x1 & 255] ^ k[36];
302 y1 = RT0[(x1 >> 24) & 255] ^ RT1[(x0 >> 16) & 255]
303 ^ RT2[(x3 >> 8) & 255] ^ RT3[x2 & 255] ^ k[37];
304 y2 = RT0[(x2 >> 24) & 255] ^ RT1[(x1 >> 16) & 255]
305 ^ RT2[(x0 >> 8) & 255] ^ RT3[x3 & 255] ^ k[38];
306 y3 = RT0[(x3 >> 24) & 255] ^ RT1[(x2 >> 16) & 255]
307 ^ RT2[(x1 >> 8) & 255] ^ RT3[x0 & 255] ^ k[39];
308 x0 = ((RS[(y0 >> 24) & 255] << 24) | (RS[(y3 >> 16) & 255] << 16)
309 | (RS[(y2 >> 8) & 255] << 8) | RS[y1 & 255]) ^ k[40];
310 x1 = ((RS[(y1 >> 24) & 255] << 24) | (RS[(y0 >> 16) & 255] << 16)
311 | (RS[(y3 >> 8) & 255] << 8) | RS[y2 & 255]) ^ k[41];
312 x2 = ((RS[(y2 >> 24) & 255] << 24) | (RS[(y1 >> 16) & 255] << 16)
313 | (RS[(y0 >> 8) & 255] << 8) | RS[y3 & 255]) ^ k[42];
314 x3 = ((RS[(y3 >> 24) & 255] << 24) | (RS[(y2 >> 16) & 255] << 16)
315 | (RS[(y1 >> 8) & 255] << 8) | RS[y0 & 255]) ^ k[43];
316 Bits.writeInt(out, off, x0);
317 Bits.writeInt(out, off + 4, x1);
318 Bits.writeInt(out, off + 8, x2);
319 Bits.writeInt(out, off + 12, x3);
320 }
321
322 @Override
323 public int getKeyLength() {
324 return 16;
325 }
326
327 }