aeslua/aes.lua

Wed, 16 Feb 2011 20:29:33 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 16 Feb 2011 20:29:33 +0000
changeset 0
598d09faf89c
permissions
-rw-r--r--

There are no secrets better kept than the secrets that everybody guesses.

0
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local bit = require("bit");
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local gf = require("aeslua.gf");
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local util = require("aeslua.util");
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 -- Implementation of AES with nearly pure lua (only bitlib is needed)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 -- AES with lua is slow, really slow :-)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local public = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local private = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local aeslua = require("aeslua");
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 aeslua.aes = public;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 -- some constants
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 public.ROUNDS = "rounds";
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 public.KEY_TYPE = "type";
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 public.ENCRYPTION_KEY=1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 public.DECRYPTION_KEY=2;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 -- aes SBOX
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 private.SBox = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 private.iSBox = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 -- aes tables
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 private.table0 = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 private.table1 = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 private.table2 = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 private.table3 = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 private.tableInv0 = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 private.tableInv1 = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 private.tableInv2 = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 private.tableInv3 = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 -- round constants
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 private.rCon = {0x01000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 0x02000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 0x04000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 0x08000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 0x10000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 0x20000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 0x40000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 0x80000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 0x1b000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 0x36000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 0x6c000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 0xd8000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 0xab000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 0x4d000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 0x9a000000,
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 0x2f000000};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 -- affine transformation for calculating the S-Box of AES
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 function private.affinMap(byte)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local mask = 0xf8;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local result = 0;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 for i = 1,8 do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 result = bit.lshift(result,1);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local parity = util.byteParity(bit.band(byte,mask));
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 result = result + parity;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 -- simulate roll
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local lastbit = bit.band(mask, 1);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 mask = bit.band(bit.rshift(mask, 1),0xff);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 if (lastbit ~= 0) then
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 mask = bit.bor(mask, 0x80);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 else
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 mask = bit.band(mask, 0x7f);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 return bit.bxor(result, 0x63);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 -- calculate S-Box and inverse S-Box of AES
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 -- apply affine transformation to inverse in finite field 2^8
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 function private.calcSBox()
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local inverse;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 for i = 0, 255 do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 if (i ~= 0) then
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 inverse = gf.invert(i);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 else
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 inverse = i;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 local mapped = private.affinMap(inverse);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 private.SBox[i] = mapped;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 private.iSBox[mapped] = i;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 -- Calculate round tables
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 -- round tables are used to calculate shiftRow, MixColumn and SubBytes
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 -- with 4 table lookups and 4 xor operations.
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 function private.calcRoundTables()
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 for x = 0,255 do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 local byte = private.SBox[x];
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 private.table0[x] = util.putByte(gf.mul(0x03, byte), 0)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 + util.putByte( byte , 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 + util.putByte( byte , 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 + util.putByte(gf.mul(0x02, byte), 3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 private.table1[x] = util.putByte( byte , 0)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 + util.putByte( byte , 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 + util.putByte(gf.mul(0x02, byte), 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 + util.putByte(gf.mul(0x03, byte), 3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 private.table2[x] = util.putByte( byte , 0)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 + util.putByte(gf.mul(0x02, byte), 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 + util.putByte(gf.mul(0x03, byte), 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 + util.putByte( byte , 3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 private.table3[x] = util.putByte(gf.mul(0x02, byte), 0)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 + util.putByte(gf.mul(0x03, byte), 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 + util.putByte( byte , 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 + util.putByte( byte , 3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 -- Calculate inverse round tables
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 -- does the inverse of the normal roundtables for the equivalent
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 -- decryption algorithm.
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 function private.calcInvRoundTables()
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 for x = 0,255 do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 local byte = private.iSBox[x];
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 private.tableInv0[x] = util.putByte(gf.mul(0x0b, byte), 0)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 + util.putByte(gf.mul(0x0d, byte), 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 + util.putByte(gf.mul(0x09, byte), 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 + util.putByte(gf.mul(0x0e, byte), 3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 private.tableInv1[x] = util.putByte(gf.mul(0x0d, byte), 0)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 + util.putByte(gf.mul(0x09, byte), 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 + util.putByte(gf.mul(0x0e, byte), 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 + util.putByte(gf.mul(0x0b, byte), 3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 private.tableInv2[x] = util.putByte(gf.mul(0x09, byte), 0)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 + util.putByte(gf.mul(0x0e, byte), 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 + util.putByte(gf.mul(0x0b, byte), 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 + util.putByte(gf.mul(0x0d, byte), 3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 private.tableInv3[x] = util.putByte(gf.mul(0x0e, byte), 0)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 + util.putByte(gf.mul(0x0b, byte), 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 + util.putByte(gf.mul(0x0d, byte), 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 + util.putByte(gf.mul(0x09, byte), 3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 -- rotate word: 0xaabbccdd gets 0xbbccddaa
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 -- used for key schedule
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 function private.rotWord(word)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 local tmp = bit.band(word,0xff000000);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 return (bit.lshift(word,8) + bit.rshift(tmp,24)) ;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 -- replace all bytes in a word with the SBox.
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 -- used for key schedule
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 function private.subWord(word)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 return util.putByte(private.SBox[util.getByte(word,0)],0)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 + util.putByte(private.SBox[util.getByte(word,1)],1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 + util.putByte(private.SBox[util.getByte(word,2)],2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 + util.putByte(private.SBox[util.getByte(word,3)],3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 -- generate key schedule for aes encryption
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 -- returns table with all round keys and
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 -- the necessary number of rounds saved in [public.ROUNDS]
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 function public.expandEncryptionKey(key)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 local keySchedule = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 local keyWords = math.floor(#key / 4);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 if ((keyWords ~= 4 and keyWords ~= 6 and keyWords ~= 8) or (keyWords * 4 ~= #key)) then
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 error("Invalid key size: "..keyWords);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 return nil;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 keySchedule[public.ROUNDS] = keyWords + 6;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 keySchedule[public.KEY_TYPE] = public.ENCRYPTION_KEY;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 for i = 0,keyWords - 1 do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 keySchedule[i] = util.putByte(key[i*4+1], 3)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 + util.putByte(key[i*4+2], 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 + util.putByte(key[i*4+3], 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 + util.putByte(key[i*4+4], 0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 for i = keyWords, (keySchedule[public.ROUNDS] + 1)*4 - 1 do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 local tmp = keySchedule[i-1];
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 if ( i % keyWords == 0) then
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 tmp = private.rotWord(tmp);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 tmp = private.subWord(tmp);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 local index = math.floor(i/keyWords);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 tmp = bit.bxor(tmp,private.rCon[index]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 elseif (keyWords > 6 and i % keyWords == 4) then
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 tmp = private.subWord(tmp);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 keySchedule[i] = bit.bxor(keySchedule[(i-keyWords)],tmp);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 return keySchedule;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 -- Inverse mix column
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 -- used for key schedule of decryption key
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 function private.invMixColumnOld(word)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 local b0 = util.getByte(word,3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 local b1 = util.getByte(word,2);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 local b2 = util.getByte(word,1);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 local b3 = util.getByte(word,0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 return util.putByte(gf.add(gf.add(gf.add(gf.mul(0x0b, b1),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 gf.mul(0x0d, b2)),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 gf.mul(0x09, b3)),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 gf.mul(0x0e, b0)),3)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 + util.putByte(gf.add(gf.add(gf.add(gf.mul(0x0b, b2),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 gf.mul(0x0d, b3)),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 gf.mul(0x09, b0)),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 gf.mul(0x0e, b1)),2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 + util.putByte(gf.add(gf.add(gf.add(gf.mul(0x0b, b3),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 gf.mul(0x0d, b0)),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 gf.mul(0x09, b1)),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 gf.mul(0x0e, b2)),1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 + util.putByte(gf.add(gf.add(gf.add(gf.mul(0x0b, b0),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 gf.mul(0x0d, b1)),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 gf.mul(0x09, b2)),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 gf.mul(0x0e, b3)),0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 -- Optimized inverse mix column
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 -- look at http://fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 -- TODO: make it work
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 function private.invMixColumn(word)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 local b0 = util.getByte(word,3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 local b1 = util.getByte(word,2);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 local b2 = util.getByte(word,1);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 local b3 = util.getByte(word,0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 local t = bit.bxor(b3,b2);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 local u = bit.bxor(b1,b0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 local v = bit.bxor(t,u);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 v = bit.bxor(v,gf.mul(0x08,v));
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 v = bit.bxor(v,gf.mul(0x04, bit.bxor(b3,b1)));
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 local w = bit.bxor(v,gf.mul(0x04, bit.bxor(b2,b0)));
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 return util.putByte( bit.bxor(bit.bxor(b3,v), gf.mul(0x02, bit.bxor(b0,b3))), 0)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 + util.putByte( bit.bxor(bit.bxor(b2,w), gf.mul(0x02, t )), 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 + util.putByte( bit.bxor(bit.bxor(b1,v), gf.mul(0x02, bit.bxor(b0,b3))), 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 + util.putByte( bit.bxor(bit.bxor(b0,w), gf.mul(0x02, u )), 3);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 -- generate key schedule for aes decryption
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 -- uses key schedule for aes encryption and transforms each
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 -- key by inverse mix column.
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 function public.expandDecryptionKey(key)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 local keySchedule = public.expandEncryptionKey(key);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 if (keySchedule == nil) then
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 return nil;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 keySchedule[public.KEY_TYPE] = public.DECRYPTION_KEY;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 for i = 4, (keySchedule[public.ROUNDS] + 1)*4 - 5 do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 keySchedule[i] = private.invMixColumnOld(keySchedule[i]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 return keySchedule;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 -- xor round key to state
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 function private.addRoundKey(state, key, round)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 for i = 0, 3 do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 state[i] = bit.bxor(state[i], key[round*4+i]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 -- do encryption round (ShiftRow, SubBytes, MixColumn together)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 function private.doRound(origState, dstState)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 dstState[0] = bit.bxor(bit.bxor(bit.bxor(
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 private.table0[util.getByte(origState[0],3)],
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 private.table1[util.getByte(origState[1],2)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 private.table2[util.getByte(origState[2],1)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 private.table3[util.getByte(origState[3],0)]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 dstState[1] = bit.bxor(bit.bxor(bit.bxor(
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 private.table0[util.getByte(origState[1],3)],
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 private.table1[util.getByte(origState[2],2)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 private.table2[util.getByte(origState[3],1)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 private.table3[util.getByte(origState[0],0)]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 dstState[2] = bit.bxor(bit.bxor(bit.bxor(
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 private.table0[util.getByte(origState[2],3)],
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 private.table1[util.getByte(origState[3],2)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 private.table2[util.getByte(origState[0],1)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 private.table3[util.getByte(origState[1],0)]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 dstState[3] = bit.bxor(bit.bxor(bit.bxor(
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 private.table0[util.getByte(origState[3],3)],
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 private.table1[util.getByte(origState[0],2)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 private.table2[util.getByte(origState[1],1)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 private.table3[util.getByte(origState[2],0)]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 -- do last encryption round (ShiftRow and SubBytes)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 function private.doLastRound(origState, dstState)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 dstState[0] = util.putByte(private.SBox[util.getByte(origState[0],3)], 3)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 + util.putByte(private.SBox[util.getByte(origState[1],2)], 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 + util.putByte(private.SBox[util.getByte(origState[2],1)], 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 + util.putByte(private.SBox[util.getByte(origState[3],0)], 0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 dstState[1] = util.putByte(private.SBox[util.getByte(origState[1],3)], 3)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 + util.putByte(private.SBox[util.getByte(origState[2],2)], 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 + util.putByte(private.SBox[util.getByte(origState[3],1)], 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 + util.putByte(private.SBox[util.getByte(origState[0],0)], 0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 dstState[2] = util.putByte(private.SBox[util.getByte(origState[2],3)], 3)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 + util.putByte(private.SBox[util.getByte(origState[3],2)], 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 + util.putByte(private.SBox[util.getByte(origState[0],1)], 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 + util.putByte(private.SBox[util.getByte(origState[1],0)], 0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 dstState[3] = util.putByte(private.SBox[util.getByte(origState[3],3)], 3)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 + util.putByte(private.SBox[util.getByte(origState[0],2)], 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 + util.putByte(private.SBox[util.getByte(origState[1],1)], 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 + util.putByte(private.SBox[util.getByte(origState[2],0)], 0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 -- do decryption round
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359 function private.doInvRound(origState, dstState)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 dstState[0] = bit.bxor(bit.bxor(bit.bxor(
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 private.tableInv0[util.getByte(origState[0],3)],
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 private.tableInv1[util.getByte(origState[3],2)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 private.tableInv2[util.getByte(origState[2],1)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 private.tableInv3[util.getByte(origState[1],0)]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 dstState[1] = bit.bxor(bit.bxor(bit.bxor(
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 private.tableInv0[util.getByte(origState[1],3)],
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 private.tableInv1[util.getByte(origState[0],2)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 private.tableInv2[util.getByte(origState[3],1)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 private.tableInv3[util.getByte(origState[2],0)]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372 dstState[2] = bit.bxor(bit.bxor(bit.bxor(
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 private.tableInv0[util.getByte(origState[2],3)],
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374 private.tableInv1[util.getByte(origState[1],2)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
375 private.tableInv2[util.getByte(origState[0],1)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 private.tableInv3[util.getByte(origState[3],0)]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
378 dstState[3] = bit.bxor(bit.bxor(bit.bxor(
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379 private.tableInv0[util.getByte(origState[3],3)],
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 private.tableInv1[util.getByte(origState[2],2)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 private.tableInv2[util.getByte(origState[1],1)]),
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 private.tableInv3[util.getByte(origState[0],0)]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 -- do last decryption round
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 function private.doInvLastRound(origState, dstState)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389 dstState[0] = util.putByte(private.iSBox[util.getByte(origState[0],3)], 3)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
390 + util.putByte(private.iSBox[util.getByte(origState[3],2)], 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391 + util.putByte(private.iSBox[util.getByte(origState[2],1)], 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
392 + util.putByte(private.iSBox[util.getByte(origState[1],0)], 0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
393
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 dstState[1] = util.putByte(private.iSBox[util.getByte(origState[1],3)], 3)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
395 + util.putByte(private.iSBox[util.getByte(origState[0],2)], 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 + util.putByte(private.iSBox[util.getByte(origState[3],1)], 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397 + util.putByte(private.iSBox[util.getByte(origState[2],0)], 0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
398
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 dstState[2] = util.putByte(private.iSBox[util.getByte(origState[2],3)], 3)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 + util.putByte(private.iSBox[util.getByte(origState[1],2)], 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
401 + util.putByte(private.iSBox[util.getByte(origState[0],1)], 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 + util.putByte(private.iSBox[util.getByte(origState[3],0)], 0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 dstState[3] = util.putByte(private.iSBox[util.getByte(origState[3],3)], 3)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405 + util.putByte(private.iSBox[util.getByte(origState[2],2)], 2)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
406 + util.putByte(private.iSBox[util.getByte(origState[1],1)], 1)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
407 + util.putByte(private.iSBox[util.getByte(origState[0],0)], 0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
408 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
409
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
410 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
411 -- encrypts 16 Bytes
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
412 -- key encryption key schedule
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
413 -- input array with input data
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
414 -- inputOffset start index for input
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
415 -- output array for encrypted data
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
416 -- outputOffset start index for output
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
417 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
418 function public.encrypt(key, input, inputOffset, output, outputOffset)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
419 --default parameters
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
420 inputOffset = inputOffset or 1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
421 output = output or {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
422 outputOffset = outputOffset or 1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
423
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
424 local state = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
425 local tmpState = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
426
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
427 if (key[public.KEY_TYPE] ~= public.ENCRYPTION_KEY) then
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
428 error("No encryption key: "..key[public.KEY_TYPE]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
429 return;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
430 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
431
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
432 state = util.bytesToInts(input, inputOffset, 4);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
433 private.addRoundKey(state, key, 0);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
434
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
435 local round = 1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
436 while (round < key[public.ROUNDS] - 1) do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
437 -- do a double round to save temporary assignments
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
438 private.doRound(state, tmpState);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
439 private.addRoundKey(tmpState, key, round);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
440 round = round + 1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
441
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
442 private.doRound(tmpState, state);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
443 private.addRoundKey(state, key, round);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
444 round = round + 1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
445 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
446
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
447 private.doRound(state, tmpState);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
448 private.addRoundKey(tmpState, key, round);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
449 round = round +1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
450
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
451 private.doLastRound(tmpState, state);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
452 private.addRoundKey(state, key, round);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
453
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
454 return util.intsToBytes(state, output, outputOffset);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
455 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
456
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
457 --
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
458 -- decrypt 16 bytes
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
459 -- key decryption key schedule
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
460 -- input array with input data
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
461 -- inputOffset start index for input
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
462 -- output array for decrypted data
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
463 -- outputOffset start index for output
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
464 ---
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
465 function public.decrypt(key, input, inputOffset, output, outputOffset)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
466 -- default arguments
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
467 inputOffset = inputOffset or 1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
468 output = output or {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
469 outputOffset = outputOffset or 1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
470
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
471 local state = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
472 local tmpState = {};
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
473
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
474 if (key[public.KEY_TYPE] ~= public.DECRYPTION_KEY) then
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
475 error("No decryption key: "..key[public.KEY_TYPE]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
476 return;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
477 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
478
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
479 state = util.bytesToInts(input, inputOffset, 4);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
480 private.addRoundKey(state, key, key[public.ROUNDS]);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
481
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
482 local round = key[public.ROUNDS] - 1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
483 while (round > 2) do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
484 -- do a double round to save temporary assignments
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
485 private.doInvRound(state, tmpState);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
486 private.addRoundKey(tmpState, key, round);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
487 round = round - 1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
488
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
489 private.doInvRound(tmpState, state);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
490 private.addRoundKey(state, key, round);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
491 round = round - 1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
492 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
493
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
494 private.doInvRound(state, tmpState);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
495 private.addRoundKey(tmpState, key, round);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
496 round = round - 1;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
497
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
498 private.doInvLastRound(tmpState, state);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
499 private.addRoundKey(state, key, round);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
500
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
501 return util.intsToBytes(state, output, outputOffset);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
502 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
503
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
504 -- calculate all tables when loading this file
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
505 private.calcSBox();
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
506 private.calcRoundTables();
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
507 private.calcInvRoundTables();
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
508
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
509 return public;

mercurial