forked from savvysiddharth/aes-128
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·408 lines (340 loc) · 10.2 KB
/
main.js
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
Array.prototype.rotate = (function() {
// save references to array functions to make lookup faster
var push = Array.prototype.push,
splice = Array.prototype.splice;
return function(count) {
var len = this.length >>> 0, // convert to uint
count = count >> 0; // convert to int
// convert count to value in range [0, len)
count = ((count % len) + len) % len;
// use splice.call() instead of this.splice() to make function generic
push.apply(this, splice.call(this, 0, count));
return this;
};
})();
//gives padded binary string matrix from decimal matrix
function dec2binMatrix(arr) {
let newarr = [];
for(let i=0; i<arr.length; i++) {
newarr[i] = [];
for(let j=0; j<arr.length; j++) {
const bin = dec2bin(arr[i][j]);
let padding = '';
for(let k=0; k<(8-bin.length); k++) {
padding += '0';
}
newarr[i][j] = padding + bin;
}
}
return newarr;
}
//b matrix should be binary string
function matrixMultiply(m1, m2) {
var result = [];
for (var i = 0; i < m1.length; i++) {
result[i] = [];
for (var j = 0; j < m2[0].length; j++) {
var sum = 0;
for (var k = 0; k < m1[0].length; k++) {
const m2_val = parseInt(m2[k][j], 2);
const multiplied = GFmultiply(m1[i][k], m2_val, 8);
sum ^= parseInt(multiplied,2);
}
result[i][j] = sum;
}
}
result = dec2binMatrix(result);
return result;
}
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
function sbox(row, col) {
const sbox = [[99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,118],[202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192],[183,253,147,38,54,63,247,204,52,165,229,241,113,216,49,21],[4,199,35,195,24,150,5,154,7,18,128,226,235,39,178,117],[9,131,44,26,27,110,90,160,82,59,214,179,41,227,47,132],[83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207],[208,239,170,251,67,77,51,133,69,249,2,127,80,60,159,168],[81,163,64,143,146,157,56,245,188,182,218,33,16,255,243,210],[205,12,19,236,95,151,68,23,196,167,126,61,100,93,25,115],[96,129,79,220,34,42,144,136,70,238,184,20,222,94,11,219],[224,50,58,10,73,6,36,92,194,211,172,98,145,149,228,121],[231,200,55,109,141,213,78,169,108,86,244,234,101,122,174,8],[186,120,37,46,28,166,180,198,232,221,116,31,75,189,139,138],[112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,158],[225,248,152,17,105,217,142,148,155,30,135,233,206,85,40,223],[140,161,137,13,191,230,66,104,65,153,45,15,176,84,187,22]];
let bin = dec2bin(sbox[row][col]);
let padding = '';
for(let k=0; k<(8-bin.length); k++) {
padding += '0';
}
return (padding+bin);
}
//takes string and gives 4x4 matrix with binary in strings, if no string then plain matrix
function get4x4matrix(str) {
//creating matrix
let arr = [];
for(let i=0; i<4; i++) {
arr[i] = [];
for(let j=0; j<4; j++) {
arr[i][j] = '00000000';
}
}
if(str) {
//adding string into input matrix
for(let i=0, ctr=0; i<4; i++) {
for(let j=0; j<4; j++) {
if(ctr < str.length) {
const bin = dec2bin(str.charCodeAt(ctr));
let padding = "";
for(let k=0; k<(8-bin.length); k++) {
padding += '0';
}
arr[j][i] = padding + bin;
}
ctr++;
}
}
}
return arr;
}
// takes a key and gives next key
function getNextKey(key, round) {
let lastCol = [];
const roundConstant = [];
const rc = [1,2,4,8,16,32,64,128,27,54];
//making roundConstant
roundConstant[0] = rc[round-1];
for(let i=1; i<4; i++) {
roundConstant[i] = 0;
}
for(let i=0; i<4; i++) {
lastCol.push(key[i][3]);
}
lastCol.rotate(1);
//substituting that col, and xor'ing with round constant
for(let i=0; i<4; i++) {
const first4 = lastCol[i].substring(0,4);
const last4 = lastCol[i].substring(4);
const row = parseInt(first4, 2);
const col = parseInt(last4, 2);
let valFromSbox = parseInt(sbox(row,col), 2);
lastCol[i] = valFromSbox ^ roundConstant[i];
}
//for name sake...
const theCol = lastCol;
let nextKey = get4x4matrix();
//xoring theCol with key cols, and updating theCol after each col
for(let i=0; i<4; i++) {
for(let j=0; j<4; j++) {
const keyVal_ji = parseInt(key[j][i], 2);
nextKey[j][i] = keyVal_ji ^ theCol[j];
theCol[j] = nextKey[j][i];
}
}
//converting nextKey matrix from decimal to padded binary
nextKey = dec2binMatrix(nextKey);
return nextKey;
}
function addRoundKey(arr, key) {
let newarr = get4x4matrix();
for(let i=0; i<4; i++) {
for(let j=0; j<4; j++) {
const val1 = parseInt(arr[j][i],2);
const val2 = parseInt(key[j][i], 2);
const xored = val1 ^ val2;
const bin = dec2bin(xored);
let padding = "";
for(let k=0; k<(8-bin.length); k++) {
padding += '0';
}
newarr[j][i] = padding + bin;
}
}
return newarr;
}
function substituteBytes(arr) {
let newarr = get4x4matrix();
for(let i=0; i<4; i++) {
for(let j=0; j<4; j++) {
const bin = arr[j][i];
const first4 = bin.substring(0,4);
const last4 = bin.substring(4);
const row = parseInt(first4, 2);
const col = parseInt(last4, 2);
newarr[j][i] = sbox(row,col);
}
}
return newarr;
}
function shiftRows(arr) {
for(let i=0; i<4; i++) {
arr[i].rotate(i);
}
return arr;
}
function mixColumns(arr) {
const mix_col_matrix = [[2,3,1,1], [1,2,3,1], [1,1,2,3], [3,1,1,2]];
return matrixMultiply(mix_col_matrix, arr);
}
function printHexTable(arr) {
let newarr = getHexTable(arr);
console.table(newarr);
}
function getHexTable(arr) {
let newarr = [];
for(let i=0; i<arr.length; i++) {
newarr[i] = [];
for(let j=0; j<arr[i].length; j++) {
let dec = parseInt(arr[i][j],2);
let hex = dec.toString(16);
if(hex.length == 1) hex = '0' + hex;
hex = hex.toUpperCase();
newarr[i][j] = hex;
}
}
return newarr;
}
function aes_init() {
const plain_text = document.querySelector("#plain_text").value;
const ikey = document.querySelector("#key").value;
if(plain_text.length == 0 || ikey.length == 0) {
alert('enter something');
return;
}
// const plain_text = "Two One Nine Two";
// const ikey = "Thats my Kung Fu";
let input = get4x4matrix(plain_text); //input matrix
let key = get4x4matrix(ikey); //key matrix
//ADD ROUND KEY - 0
let state_arr = addRoundKey(input, key);
const stepsBox = document.querySelector("#results-container");
stepsBox.innerHTML = '';
stepsBox.innerHTML += "<h2> ROUND : "+ 0 +"</h2> <br>";
stepsBox.innerHTML += "Input:";
stepsBox.appendChild(createTable(getHexTable(input)));
stepsBox.innerHTML += "<br>";
stepsBox.innerHTML += "Key:";
stepsBox.appendChild(createTable(getHexTable(key)));
stepsBox.innerHTML += "<br>";
stepsBox.innerHTML += "Add round key:";
stepsBox.appendChild(createTable(getHexTable(state_arr)));
stepsBox.innerHTML += "<br>";
const TOTAL_ROUNDS = 10;
round = 1;
while(round <= TOTAL_ROUNDS) {
stepsBox.innerHTML += "<hr><br>"
stepsBox.innerHTML += "<h2> ROUND : "+round+"</h2> <br>";
//SUBSTITUTION BYTES
state_arr = substituteBytes(state_arr);
stepsBox.innerHTML += "Substitution bytes:";
stepsBox.appendChild(createTable(getHexTable(state_arr)));
stepsBox.innerHTML += "<br>";
//SHIFT ROWS
state_arr = shiftRows(state_arr);
stepsBox.innerHTML += "Shift rows:";
stepsBox.appendChild(createTable(getHexTable(state_arr)));
stepsBox.innerHTML += "<br>";
//MIX COLUMNS
if(round != 10) {
state_arr = mixColumns(state_arr);
stepsBox.innerHTML += "Mix columns:";
stepsBox.appendChild(createTable(getHexTable(state_arr)));
stepsBox.innerHTML += "<br>";
}
//GENERATING NEW KEY
key = getNextKey(key , round);
stepsBox.innerHTML += "This round key:";
stepsBox.appendChild(createTable(getHexTable(key)));
stepsBox.innerHTML += "<br>";
//ADD ROUND KEY
state_arr = addRoundKey(state_arr, key);
stepsBox.innerHTML += "Add round key:";
stepsBox.appendChild(createTable(getHexTable(state_arr)));
stepsBox.innerHTML += "<br>";
round++;
}
const resultBox = document.querySelector("#resultbox");
resultBox.innerHTML = 'Final result: <br><br>';
resultBox.appendChild(createTable(getHexTable(state_arr)));
}
//rotates left with 1 unit
function rotateLeftOneUnit(l){
let temp = []
temp = l.slice(1)
temp.push(l[0])
return temp
}
function leftShift(l, shiftBy){
for(let i=0;i<shiftBy;i++){
l = rotateLeftOneUnit(l)
}
return l
}
function xorList(l1,l2){
let res = []
for(let i=0;i<l1.length;i++){
res.push(l1[i]^l2[i])
}
return res
}
function padTo15(str) {
let padding = '';
for(let i=0; i<15-str.length; i++) {
padding += '0';
}
return padding + str;
}
function count(l,num){
let count = 0
for(let i=0;i<l.length;i++)
if(l[i]==num){
count++
}
return count
}
function GFmultiply(A,B,N){ //145
A = dec2bin(A);
B = dec2bin(B);
A = padTo15(A);
B = padTo15(B);
let tempA = []
let tempB = []
for(let i=0;i<A.length;i++){
tempA.push(parseInt(A.charAt(i)))
tempB.push(parseInt(B.charAt(i)))
}
A = tempA
B = tempB
if(count(A,0)==2*N-1 || count(B,0)==2*N-1){
return A
}
let res = []
for(let i=0;i<2*N-1;i++){
res[i] = 0
}
let shift_count = 2*(N-1)
for(let i = 0;i<B.length;i++){
if(B[i]==1){
res = xorList(res, leftShift(A, shift_count))
}
shift_count -= 1
}
if(N == 8){
let nonRed = [0,0,0,0,0,0,1,0,0,0,1,1,0,1,1]
while(res.indexOf(1)<=6){
let temp = res.indexOf(1)
let count = 6 - temp
let L = leftShift(nonRed, count)
res = xorList(res, L)
}
}
res_string = '';
for(let ch of res) {
res_string += ch;
}
return res_string.substring(7);
}
function createTable(tableData) {
var table = document.createElement('table');
var tableBody = document.createElement('tbody');
tableData.forEach(function(rowData) {
var row = document.createElement('tr');
rowData.forEach(function(cellData) {
var cell = document.createElement('td');
cell.appendChild(document.createTextNode(cellData));
row.appendChild(cell);
});
tableBody.appendChild(row);
});
table.appendChild(tableBody);
return table;
}