aes.lua

by hugeblank
658 days ago
COPY
1
-- AES implementation
2
-- By KillaVanilla
3
-- Made require-able by hugeblank
4

5
local out = {}
6

7
local sbox = {
8
[0]=0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
9
0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
10
0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
11
0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
12
0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
13
0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
14
0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
15
0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
16
0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
17
0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
18
0xE0, 0x32, 0x3A, 0x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0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 
56
0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 
57
0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 
58
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 
59
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d}
60

61
-- Finite-field multiplication lookup tables:
62

63
local mul_2 = {
64
[0]=0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e,0x10,0x12,0x14,0x16,0x18,0x1a,0x1c,0x1e,
65
0x20,0x22,0x24,0x26,0x28,0x2a,0x2c,0x2e,0x30,0x32,0x34,0x36,0x38,0x3a,0x3c,0x3e,
66
0x40,0x42,0x44,0x46,0x48,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,0x5e,
67
0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6e,0x70,0x72,0x74,0x76,0x78,0x7a,0x7c,0x7e,
68
0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e,0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e,
69
0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xae,0xb0,0xb2,0xb4,0xb6,0xb8,0xba,0xbc,0xbe,
70
0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc,0xce,0xd0,0xd2,0xd4,0xd6,0xd8,0xda,0xdc,0xde,
71
0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xee,0xf0,0xf2,0xf4,0xf6,0xf8,0xfa,0xfc,0xfe,
72
0x1b,0x19,0x1f,0x1d,0x13,0x11,0x17,0x15,0x0b,0x09,0x0f,0x0d,0x03,0x01,0x07,0x05,
73
0x3b,0x39,0x3f,0x3d,0x33,0x31,0x37,0x35,0x2b,0x29,0x2f,0x2d,0x23,0x21,0x27,0x25,
74
0x5b,0x59,0x5f,0x5d,0x53,0x51,0x57,0x55,0x4b,0x49,0x4f,0x4d,0x43,0x41,0x47,0x45,
75
0x7b,0x79,0x7f,0x7d,0x73,0x71,0x77,0x75,0x6b,0x69,0x6f,0x6d,0x63,0x61,0x67,0x65,
76
0x9b,0x99,0x9f,0x9d,0x93,0x91,0x97,0x95,0x8b,0x89,0x8f,0x8d,0x83,0x81,0x87,0x85,
77
0xbb,0xb9,0xbf,0xbd,0xb3,0xb1,0xb7,0xb5,0xab,0xa9,0xaf,0xad,0xa3,0xa1,0xa7,0xa5,
78
0xdb,0xd9,0xdf,0xdd,0xd3,0xd1,0xd7,0xd5,0xcb,0xc9,0xcf,0xcd,0xc3,0xc1,0xc7,0xc5,
79
0xfb,0xf9,0xff,0xfd,0xf3,0xf1,0xf7,0xf5,0xeb,0xe9,0xef,0xed,0xe3,0xe1,0xe7,0xe5,
80
}
81

82
local mul_3 = {
83
[0]=0x00,0x03,0x06,0x05,0x0c,0x0f,0x0a,0x09,0x18,0x1b,0x1e,0x1d,0x14,0x17,0x12,0x11,
84
0x30,0x33,0x36,0x35,0x3c,0x3f,0x3a,0x39,0x28,0x2b,0x2e,0x2d,0x24,0x27,0x22,0x21,
85
0x60,0x63,0x66,0x65,0x6c,0x6f,0x6a,0x69,0x78,0x7b,0x7e,0x7d,0x74,0x77,0x72,0x71,
86
0x50,0x53,0x56,0x55,0x5c,0x5f,0x5a,0x59,0x48,0x4b,0x4e,0x4d,0x44,0x47,0x42,0x41,
87
0xc0,0xc3,0xc6,0xc5,0xcc,0xcf,0xca,0xc9,0xd8,0xdb,0xde,0xdd,0xd4,0xd7,0xd2,0xd1,
88
0xf0,0xf3,0xf6,0xf5,0xfc,0xff,0xfa,0xf9,0xe8,0xeb,0xee,0xed,0xe4,0xe7,0xe2,0xe1,
89
0xa0,0xa3,0xa6,0xa5,0xac,0xaf,0xaa,0xa9,0xb8,0xbb,0xbe,0xbd,0xb4,0xb7,0xb2,0xb1,
90
0x90,0x93,0x96,0x95,0x9c,0x9f,0x9a,0x99,0x88,0x8b,0x8e,0x8d,0x84,0x87,0x82,0x81,
91
0x9b,0x98,0x9d,0x9e,0x97,0x94,0x91,0x92,0x83,0x80,0x85,0x86,0x8f,0x8c,0x89,0x8a,
92
0xab,0xa8,0xad,0xae,0xa7,0xa4,0xa1,0xa2,0xb3,0xb0,0xb5,0xb6,0xbf,0xbc,0xb9,0xba,
93
0xfb,0xf8,0xfd,0xfe,0xf7,0xf4,0xf1,0xf2,0xe3,0xe0,0xe5,0xe6,0xef,0xec,0xe9,0xea,
94
0xcb,0xc8,0xcd,0xce,0xc7,0xc4,0xc1,0xc2,0xd3,0xd0,0xd5,0xd6,0xdf,0xdc,0xd9,0xda,
95
0x5b,0x58,0x5d,0x5e,0x57,0x54,0x51,0x52,0x43,0x40,0x45,0x46,0x4f,0x4c,0x49,0x4a,
96
0x6b,0x68,0x6d,0x6e,0x67,0x64,0x61,0x62,0x73,0x70,0x75,0x76,0x7f,0x7c,0x79,0x7a,
97
0x3b,0x38,0x3d,0x3e,0x37,0x34,0x31,0x32,0x23,0x20,0x25,0x26,0x2f,0x2c,0x29,0x2a,
98
0x0b,0x08,0x0d,0x0e,0x07,0x04,0x01,0x02,0x13,0x10,0x15,0x16,0x1f,0x1c,0x19,0x1a,
99
}
100

101
local mul_9 = {
102
[0]=0x00,0x09,0x12,0x1b,0x24,0x2d,0x36,0x3f,0x48,0x41,0x5a,0x53,0x6c,0x65,0x7e,0x77,
103
0x90,0x99,0x82,0x8b,0xb4,0xbd,0xa6,0xaf,0xd8,0xd1,0xca,0xc3,0xfc,0xf5,0xee,0xe7,
104
0x3b,0x32,0x29,0x20,0x1f,0x16,0x0d,0x04,0x73,0x7a,0x61,0x68,0x57,0x5e,0x45,0x4c,
105
0xab,0xa2,0xb9,0xb0,0x8f,0x86,0x9d,0x94,0xe3,0xea,0xf1,0xf8,0xc7,0xce,0xd5,0xdc,
106
0x76,0x7f,0x64,0x6d,0x52,0x5b,0x40,0x49,0x3e,0x37,0x2c,0x25,0x1a,0x13,0x08,0x01,
107
0xe6,0xef,0xf4,0xfd,0xc2,0xcb,0xd0,0xd9,0xae,0xa7,0xbc,0xb5,0x8a,0x83,0x98,0x91,
108
0x4d,0x44,0x5f,0x56,0x69,0x60,0x7b,0x72,0x05,0x0c,0x17,0x1e,0x21,0x28,0x33,0x3a,
109
0xdd,0xd4,0xcf,0xc6,0xf9,0xf0,0xeb,0xe2,0x95,0x9c,0x87,0x8e,0xb1,0xb8,0xa3,0xaa,
110
0xec,0xe5,0xfe,0xf7,0xc8,0xc1,0xda,0xd3,0xa4,0xad,0xb6,0xbf,0x80,0x89,0x92,0x9b,
111
0x7c,0x75,0x6e,0x67,0x58,0x51,0x4a,0x43,0x34,0x3d,0x26,0x2f,0x10,0x19,0x02,0x0b,
112
0xd7,0xde,0xc5,0xcc,0xf3,0xfa,0xe1,0xe8,0x9f,0x96,0x8d,0x84,0xbb,0xb2,0xa9,0xa0,
113
0x47,0x4e,0x55,0x5c,0x63,0x6a,0x71,0x78,0x0f,0x06,0x1d,0x14,0x2b,0x22,0x39,0x30,
114
0x9a,0x93,0x88,0x81,0xbe,0xb7,0xac,0xa5,0xd2,0xdb,0xc0,0xc9,0xf6,0xff,0xe4,0xed,
115
0x0a,0x03,0x18,0x11,0x2e,0x27,0x3c,0x35,0x42,0x4b,0x50,0x59,0x66,0x6f,0x74,0x7d,
116
0xa1,0xa8,0xb3,0xba,0x85,0x8c,0x97,0x9e,0xe9,0xe0,0xfb,0xf2,0xcd,0xc4,0xdf,0xd6,
117
0x31,0x38,0x23,0x2a,0x15,0x1c,0x07,0x0e,0x79,0x70,0x6b,0x62,0x5d,0x54,0x4f,0x46,
118
}
119

120
local mul_11 = {
121
[0]=0x00,0x0b,0x16,0x1d,0x2c,0x27,0x3a,0x31,0x58,0x53,0x4e,0x45,0x74,0x7f,0x62,0x69,
122
0xb0,0xbb,0xa6,0xad,0x9c,0x97,0x8a,0x81,0xe8,0xe3,0xfe,0xf5,0xc4,0xcf,0xd2,0xd9,
123
0x7b,0x70,0x6d,0x66,0x57,0x5c,0x41,0x4a,0x23,0x28,0x35,0x3e,0x0f,0x04,0x19,0x12,
124
0xcb,0xc0,0xdd,0xd6,0xe7,0xec,0xf1,0xfa,0x93,0x98,0x85,0x8e,0xbf,0xb4,0xa9,0xa2,
125
0xf6,0xfd,0xe0,0xeb,0xda,0xd1,0xcc,0xc7,0xae,0xa5,0xb8,0xb3,0x82,0x89,0x94,0x9f,
126
0x46,0x4d,0x50,0x5b,0x6a,0x61,0x7c,0x77,0x1e,0x15,0x08,0x03,0x32,0x39,0x24,0x2f,
127
0x8d,0x86,0x9b,0x90,0xa1,0xaa,0xb7,0xbc,0xd5,0xde,0xc3,0xc8,0xf9,0xf2,0xef,0xe4,
128
0x3d,0x36,0x2b,0x20,0x11,0x1a,0x07,0x0c,0x65,0x6e,0x73,0x78,0x49,0x42,0x5f,0x54,
129
0xf7,0xfc,0xe1,0xea,0xdb,0xd0,0xcd,0xc6,0xaf,0xa4,0xb9,0xb2,0x83,0x88,0x95,0x9e,
130
0x47,0x4c,0x51,0x5a,0x6b,0x60,0x7d,0x76,0x1f,0x14,0x09,0x02,0x33,0x38,0x25,0x2e,
131
0x8c,0x87,0x9a,0x91,0xa0,0xab,0xb6,0xbd,0xd4,0xdf,0xc2,0xc9,0xf8,0xf3,0xee,0xe5,
132
0x3c,0x37,0x2a,0x21,0x10,0x1b,0x06,0x0d,0x64,0x6f,0x72,0x79,0x48,0x43,0x5e,0x55,
133
0x01,0x0a,0x17,0x1c,0x2d,0x26,0x3b,0x30,0x59,0x52,0x4f,0x44,0x75,0x7e,0x63,0x68,
134
0xb1,0xba,0xa7,0xac,0x9d,0x96,0x8b,0x80,0xe9,0xe2,0xff,0xf4,0xc5,0xce,0xd3,0xd8,
135
0x7a,0x71,0x6c,0x67,0x56,0x5d,0x40,0x4b,0x22,0x29,0x34,0x3f,0x0e,0x05,0x18,0x13,
136
0xca,0xc1,0xdc,0xd7,0xe6,0xed,0xf0,0xfb,0x92,0x99,0x84,0x8f,0xbe,0xb5,0xa8,0xa3,
137
}
138

139
local mul_13 = {
140
[0]=0x00,0x0d,0x1a,0x17,0x34,0x39,0x2e,0x23,0x68,0x65,0x72,0x7f,0x5c,0x51,0x46,0x4b,
141
0xd0,0xdd,0xca,0xc7,0xe4,0xe9,0xfe,0xf3,0xb8,0xb5,0xa2,0xaf,0x8c,0x81,0x96,0x9b,
142
0xbb,0xb6,0xa1,0xac,0x8f,0x82,0x95,0x98,0xd3,0xde,0xc9,0xc4,0xe7,0xea,0xfd,0xf0,
143
0x6b,0x66,0x71,0x7c,0x5f,0x52,0x45,0x48,0x03,0x0e,0x19,0x14,0x37,0x3a,0x2d,0x20,
144
0x6d,0x60,0x77,0x7a,0x59,0x54,0x43,0x4e,0x05,0x08,0x1f,0x12,0x31,0x3c,0x2b,0x26,
145
0xbd,0xb0,0xa7,0xaa,0x89,0x84,0x93,0x9e,0xd5,0xd8,0xcf,0xc2,0xe1,0xec,0xfb,0xf6,
146
0xd6,0xdb,0xcc,0xc1,0xe2,0xef,0xf8,0xf5,0xbe,0xb3,0xa4,0xa9,0x8a,0x87,0x90,0x9d,
147
0x06,0x0b,0x1c,0x11,0x32,0x3f,0x28,0x25,0x6e,0x63,0x74,0x79,0x5a,0x57,0x40,0x4d,
148
0xda,0xd7,0xc0,0xcd,0xee,0xe3,0xf4,0xf9,0xb2,0xbf,0xa8,0xa5,0x86,0x8b,0x9c,0x91,
149
0x0a,0x07,0x10,0x1d,0x3e,0x33,0x24,0x29,0x62,0x6f,0x78,0x75,0x56,0x5b,0x4c,0x41,
150
0x61,0x6c,0x7b,0x76,0x55,0x58,0x4f,0x42,0x09,0x04,0x13,0x1e,0x3d,0x30,0x27,0x2a,
151
0xb1,0xbc,0xab,0xa6,0x85,0x88,0x9f,0x92,0xd9,0xd4,0xc3,0xce,0xed,0xe0,0xf7,0xfa,
152
0xb7,0xba,0xad,0xa0,0x83,0x8e,0x99,0x94,0xdf,0xd2,0xc5,0xc8,0xeb,0xe6,0xf1,0xfc,
153
0x67,0x6a,0x7d,0x70,0x53,0x5e,0x49,0x44,0x0f,0x02,0x15,0x18,0x3b,0x36,0x21,0x2c,
154
0x0c,0x01,0x16,0x1b,0x38,0x35,0x22,0x2f,0x64,0x69,0x7e,0x73,0x50,0x5d,0x4a,0x47,
155
0xdc,0xd1,0xc6,0xcb,0xe8,0xe5,0xf2,0xff,0xb4,0xb9,0xae,0xa3,0x80,0x8d,0x9a,0x97,
156
}
157

158
local mul_14 = {
159
[0]=0x00,0x0e,0x1c,0x12,0x38,0x36,0x24,0x2a,0x70,0x7e,0x6c,0x62,0x48,0x46,0x54,0x5a,
160
0xe0,0xee,0xfc,0xf2,0xd8,0xd6,0xc4,0xca,0x90,0x9e,0x8c,0x82,0xa8,0xa6,0xb4,0xba,
161
0xdb,0xd5,0xc7,0xc9,0xe3,0xed,0xff,0xf1,0xab,0xa5,0xb7,0xb9,0x93,0x9d,0x8f,0x81,
162
0x3b,0x35,0x27,0x29,0x03,0x0d,0x1f,0x11,0x4b,0x45,0x57,0x59,0x73,0x7d,0x6f,0x61,
163
0xad,0xa3,0xb1,0xbf,0x95,0x9b,0x89,0x87,0xdd,0xd3,0xc1,0xcf,0xe5,0xeb,0xf9,0xf7,
164
0x4d,0x43,0x51,0x5f,0x75,0x7b,0x69,0x67,0x3d,0x33,0x21,0x2f,0x05,0x0b,0x19,0x17,
165
0x76,0x78,0x6a,0x64,0x4e,0x40,0x52,0x5c,0x06,0x08,0x1a,0x14,0x3e,0x30,0x22,0x2c,
166
0x96,0x98,0x8a,0x84,0xae,0xa0,0xb2,0xbc,0xe6,0xe8,0xfa,0xf4,0xde,0xd0,0xc2,0xcc,
167
0x41,0x4f,0x5d,0x53,0x79,0x77,0x65,0x6b,0x31,0x3f,0x2d,0x23,0x09,0x07,0x15,0x1b,
168
0xa1,0xaf,0xbd,0xb3,0x99,0x97,0x85,0x8b,0xd1,0xdf,0xcd,0xc3,0xe9,0xe7,0xf5,0xfb,
169
0x9a,0x94,0x86,0x88,0xa2,0xac,0xbe,0xb0,0xea,0xe4,0xf6,0xf8,0xd2,0xdc,0xce,0xc0,
170
0x7a,0x74,0x66,0x68,0x42,0x4c,0x5e,0x50,0x0a,0x04,0x16,0x18,0x32,0x3c,0x2e,0x20,
171
0xec,0xe2,0xf0,0xfe,0xd4,0xda,0xc8,0xc6,0x9c,0x92,0x80,0x8e,0xa4,0xaa,0xb8,0xb6,
172
0x0c,0x02,0x10,0x1e,0x34,0x3a,0x28,0x26,0x7c,0x72,0x60,0x6e,0x44,0x4a,0x58,0x56,
173
0x37,0x39,0x2b,0x25,0x0f,0x01,0x13,0x1d,0x47,0x49,0x5b,0x55,0x7f,0x71,0x63,0x6d,
174
0xd7,0xd9,0xcb,0xc5,0xef,0xe1,0xf3,0xfd,0xa7,0xa9,0xbb,0xb5,0x9f,0x91,0x83,0x8d,
175
}
176

177
local bxor = bit.bxor
178
local insert = table.insert
179

180
local function copy(input)
181
	local c = {}
182
	for i, v in pairs(input) do
183
		c[i] = v
184
	end
185
	return c
186
end
187

188
local function subBytes(input, invert)
189
	for i=1, #input do
190
		if not (sbox[input[i]] and inv_sbox[input[i]]) then
191
			error("subBytes: input["..i.."] > 0xFF")
192
		end
193
		if invert then
194
			input[i] = inv_sbox[input[i]]
195
		else
196
			input[i] = sbox[input[i]]
197
		end
198
	end
199
	return input
200
end
201

202
local function shiftRows(input)
203
	local copy = {}
204
	-- Row 1: No change
205
	copy[1] = input[1]
206
	copy[2] = input[2]
207
	copy[3] = input[3]
208
	copy[4] = input[4]
209
	-- Row 2: Offset 1
210
	copy[5] = input[6]
211
	copy[6] = input[7]
212
	copy[7] = input[8]
213
	copy[8] = input[5]
214
	-- Row 3: Offset 2
215
	copy[9] = input[11]
216
	copy[10] = input[12]
217
	copy[11] = input[9]
218
	copy[12] = input[10]
219
	-- Row 4: Offset 3
220
	copy[13] = input[16]
221
	copy[14] = input[13]
222
	copy[15] = input[14]
223
	copy[16] = input[15]
224
	return copy
225
end
226

227
local function invShiftRows(input)
228
	local copy = {}
229
	-- Row 1: No change
230
	copy[1] = input[1]
231
	copy[2] = input[2]
232
	copy[3] = input[3]
233
	copy[4] = input[4]
234
	-- Row 2: Offset 1
235
	copy[5] = input[8]
236
	copy[6] = input[5]
237
	copy[7] = input[6]
238
	copy[8] = input[7]
239
	-- Row 3: Offset 2
240
	copy[9] = input[11]
241
	copy[10] = input[12]
242
	copy[11] = input[9]
243
	copy[12] = input[10]
244
	-- Row 4: Offset 3
245
	copy[13] = input[14]
246
	copy[14] = input[15]
247
	copy[15] = input[16]
248
	copy[16] = input[13]
249
	return copy
250
end
251

252
local function finite_field_mul(a,b) -- Multiply two numbers in GF(256), assuming that polynomials are 8 bits wide
253
	local product = 0
254
	local mulA, mulB = a,b
255
	for i=1, 8 do
256
		--print("FFMul: MulA: "..mulA.." MulB: "..mulB)
257
		if mulA == 0 or mulB == 0 then
258
			break
259
		end
260
		if bit.band(1, mulB) > 0 then
261
			product = bxor(product, mulA)
262
		end
263
		mulB = bit.brshift(mulB, 1)
264
		local carry = bit.band(0x80, mulA)
265
		mulA = bit.band(0xFF, bit.blshift(mulA, 1))
266
		if carry > 0 then
267
			mulA = bxor( mulA, 0x1B )
268
		end
269
	end
270
	return product
271
end
272

273
local function mixColumn(column)
274
	local output = {}
275
	--print("MixColumn: #column: "..#column)
276
	output[1] = bxor( mul_2[column[1]], bxor( mul_3[column[2]], bxor( column[3], column[4] ) ) )
277
	output[2] = bxor( column[1], bxor( mul_2[column[2]], bxor( mul_3[column[3]], column[4] ) ) )
278
	output[3] = bxor( column[1], bxor( column[2], bxor( mul_2[column[3]], mul_3[column[4]] ) ) )
279
	output[4] = bxor( mul_3[column[1]], bxor( column[2], bxor( column[3], mul_2[column[4]] ) ) )
280
	return output
281
end
282

283
local function invMixColumn(column)
284
	local output = {}
285
	--print("InvMixColumn: #column: "..#column)
286
	output[1] = bxor( mul_14[column[1]], bxor( mul_11[column[2]], bxor( mul_13[column[3]], mul_9[column[4]] ) ) )
287
	output[2] = bxor( mul_9[column[1]], bxor( mul_14[column[2]], bxor( mul_11[column[3]], mul_13[column[4]] ) ) )
288
	output[3] = bxor( mul_13[column[1]], bxor( mul_9[column[2]], bxor( mul_14[column[3]], mul_11[column[4]] ) ) )
289
	output[4] = bxor( mul_11[column[1]], bxor( mul_13[column[2]], bxor( mul_9[column[3]], mul_14[column[4]] ) ) )
290
	return output
291
end
292

293
local function mixColumns(input, invert)
294
	--print("MixColumns: #input: "..#input)
295
	-- Ooops. I mixed the ROWS instead of the COLUMNS on accident.
296
	local output = {}
297
	--[[
298
	local c1 = { input[1], input[2], input[3], input[4] }
299
	local c2 = { input[5], input[6], input[7], input[8] }
300
	local c3 = { input[9], input[10], input[11], input[12] }
301
	local c4 = { input[13], input[14], input[15], input[16] }
302
	]]
303
	local c1 = { input[1], input[5], input[9], input[13] }
304
	local c2 = { input[2], input[6], input[10], input[14] }
305
	local c3 = { input[3], input[7], input[11], input[15] }
306
	local c4 = { input[4], input[8], input[12], input[16] }
307
	if invert then
308
		c1 = invMixColumn(c1)
309
		c2 = invMixColumn(c2)
310
		c3 = invMixColumn(c3)
311
		c4 = invMixColumn(c4)
312
	else
313
		c1 = mixColumn(c1)
314
		c2 = mixColumn(c2)
315
		c3 = mixColumn(c3)
316
		c4 = mixColumn(c4)
317
	end
318
	--[[
319
	output[1] = c1[1]
320
	output[2] = c1[2]
321
	output[3] = c1[3]
322
	output[4] = c1[4]
323
	
324
	output[5] = c2[1]
325
	output[6] = c2[2]
326
	output[7] = c2[3]
327
	output[8] = c2[4]
328
	
329
	output[9] = c3[1]
330
	output[10] = c3[2]
331
	output[11] = c3[3]
332
	output[12] = c3[4]
333
	
334
	output[13] = c4[1]
335
	output[14] = c4[2]
336
	output[15] = c4[3]
337
	output[16] = c4[4]
338
	]]
339
	
340
	output[1] = c1[1]
341
	output[5] = c1[2]
342
	output[9] = c1[3]
343
	output[13] = c1[4]
344
	
345
	output[2] = c2[1]
346
	output[6] = c2[2]
347
	output[10] = c2[3]
348
	output[14] = c2[4]
349
	
350
	output[3] = c3[1]
351
	output[7] = c3[2]
352
	output[11] = c3[3]
353
	output[15] = c3[4]
354
	
355
	output[4] = c4[1]
356
	output[8] = c4[2]
357
	output[12] = c4[3]
358
	output[16] = c4[4]
359
	
360
	return output
361
end
362

363
local function addRoundKey(input, exp_key, round)
364
	local output = {}
365
	for i=1, 16 do
366
		assert(input[i], "input["..i.."]=nil!")
367
		assert(exp_key[ ((round-1)*16)+i ], "round_key["..(((round-1)*16)+i).."]=nil!")
368
		output[i] = bxor( input[i], exp_key[ ((round-1)*16)+i ] )
369
	end
370
	return output
371
end
372

373
out.key_schedule = function(enc_key)
374
	local function core(in1, in2, in3, in4, i)
375
		local s1 = in2
376
		local s2 = in3
377
		local s3 = in4
378
		local s4 = in1
379
		s1 = bxor(sbox[s1], Rcon[i])
380
		s2 = sbox[s2]
381
		s3 = sbox[s3]
382
		s4 = sbox[s4]
383
		return s1, s2, s3, s4
384
	end
385
	
386
	local n, b, key_type = 0, 0, 0
387
	
388
	-- Len | n | b |
389
	-- 128 |16 |176|
390
	-- 192 |24 |208|
391
	-- 256 |32 |240|
392
	
393
	-- Determine keysize:
394
	
395
	if #enc_key < 16 then
396
		error("Encryption key is too small; key size must be more than 16 bytes.")
397
	elseif #enc_key >= 16 and #enc_key < 24 then
398
		n = 16
399
		b = 176
400
		--key_type = 1
401
	elseif #enc_key >= 24 and #enc_key < 32 then
402
		n = 24
403
		b = 208
404
		--key_type = 2
405
	else
406
		n = 32
407
		b = 240
408
		--key_type = 3
409
	end
410
	
411
	local exp_key = {}
412
	local rcon_iter = 1
413
	for i=1, n do
414
		exp_key[i] = enc_key[i]
415
	end
416
	while #exp_key < b do
417
		local t1 = exp_key[#exp_key]
418
		local t2 = exp_key[#exp_key-1]
419
		local t3 = exp_key[#exp_key-2]
420
		local t4 = exp_key[#exp_key-3]
421
		t1, t2, t3, t4 = core(t1, t2, t3, t4, rcon_iter)
422
		rcon_iter = rcon_iter+1
423
		t1 = bxor(t1, exp_key[#exp_key-(n-1)])
424
		t2 = bxor(t2, exp_key[#exp_key-(n-2)])
425
		t3 = bxor(t3, exp_key[#exp_key-(n-3)])
426
		t4 = bxor(t4, exp_key[#exp_key-(n-4)])
427
		insert(exp_key, t1)
428
		insert(exp_key, t2)
429
		insert(exp_key, t3)
430
		insert(exp_key, t4)
431
		for i=1, 3 do
432
			t1 = bxor(exp_key[#exp_key], exp_key[#exp_key-(n-1)])
433
			t2 = bxor(exp_key[#exp_key-1], exp_key[#exp_key-(n-2)])
434
			t3 = bxor(exp_key[#exp_key-2], exp_key[#exp_key-(n-3)])
435
			t4 = bxor(exp_key[#exp_key-3], exp_key[#exp_key-(n-4)])
436
			insert(exp_key, t1)
437
			insert(exp_key, t2)
438
			insert(exp_key, t3)
439
			insert(exp_key, t4)
440
		end
441
		if key_type == 3 then -- If we're processing a 256 bit key...
442
			-- Take the previous 4 bytes of the expanded key, run them through the sbox,
443
			-- then XOR them with the previous n bytes of the expanded key, then output them
444
			-- as the next 4 bytes of expanded key.
445
			t1 = bxor(sbox[exp_key[#exp_key]], exp_key[#exp_key-(n-1)])
446
			t2 = bxor(sbox[exp_key[#exp_key-1]], exp_key[#exp_key-(n-2)])
447
			t3 = bxor(sbox[exp_key[#exp_key-2]], exp_key[#exp_key-(n-3)])
448
			t4 = bxor(sbox[exp_key[#exp_key-3]], exp_key[#exp_key-(n-4)])
449
			insert(exp_key, t1)
450
			insert(exp_key, t2)
451
			insert(exp_key, t3)
452
			insert(exp_key, t4)
453
		end
454
		if key_type == 2 or key_type == 3 then -- If we're processing a 192-bit or 256-bit key..
455
			local i = 2
456
			if key_type == 3 then
457
				i = 3
458
			end
459
			for j=1, i do
460
				t1 = bxor(exp_key[#exp_key], exp_key[#exp_key-(n-1)])
461
				t2 = bxor(exp_key[#exp_key-1], exp_key[#exp_key-(n-2)])
462
				t3 = bxor(exp_key[#exp_key-2], exp_key[#exp_key-(n-3)])
463
				t4 = bxor(exp_key[#exp_key-3], exp_key[#exp_key-(n-4)])
464
				insert(exp_key, t1)
465
				insert(exp_key, t2)
466
				insert(exp_key, t3)
467
				insert(exp_key, t4)
468
			end
469
		end
470
	end
471
	return exp_key
472
end
473

474
-- Transform a string of bytes into 16 byte blocks, adding padding to ensure that each block contains 16 bytes.
475
-- For example:
476
-- "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" (contains 28 0xFF bytes)
477
-- Is transformed into this:
478
-- {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}, {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0,0,0,0} (16 0xFF bytes, followed by 12 0xFF bytes and 4 0x00 bytes for padding)
479

480
out.breakIntoBlocks  = function(data)
481
	if type(data) ~= "string" then
482
		error("breakIntoBlocks: data is not a string", 2)
483
	end
484
	while (#data % 16) ~= 0 do
485
		data = data.."\0"
486
	end
487
	local blocks = {}
488
	local blockNum = 1
489
	local output = {}
490
	for i=1, #data, 16 do
491
		blocks[blockNum] = {}
492
		for j=1, 16 do
493
			blocks[blockNum][j] = string.byte(data, ((blockNum-1)*16)+j, ((blockNum-1)*16)+j)
494
		end
495
		blockNum = blockNum+1
496
	end
497
	return blocks
498
end
499

500
-- Transform a string into a series of blocks.
501

502
-- For example, to get a key from a string:
503
-- local key = strToBlocks(keyStr)
504
-- key = key[1]
505

506
out.strToBlocks = function(str)
507
	local rawBytestream = {}
508
	local blocks = {}
509
	for i=1, #str do
510
		rawBytestream[i] = string.byte(str, i, i)
511
	end
512
	for i=1, math.ceil(#rawBytestream / 16) do
513
		blocks[i] = {}
514
		for j=1, 16 do
515
			blocks[i][j] = rawBytestream[ ((i-1)*16)+j ] or 0
516
		end
517
	end
518
	return blocks
519
end
520

521
-- Encrypt / Decrypt individual blocks:
522

523
out.encrypt_block = function(data, key)
524
	local exp_key = out.key_schedule(key)
525
	local state = data
526
	local nr = 0
527
	
528
	if #exp_key == 176 then -- Key type 1 (128-bits)
529
		nr = 10
530
	elseif #exp_key == 208 then -- Key type 2 (192-bits)
531
		nr = 12
532
	elseif #exp_key == 240 then -- Key type 3 (256-bits)
533
		nr = 14
534
	else
535
		error("encrypt_block: Unknown key size?", 2)
536
	end
537
	
538
	-- Inital round:
539
	state = addRoundKey(state, exp_key, 1)
540
	
541
	-- Repeat (Nr-1) times:
542
	for round_num = 2, nr-1 do	
543
		state = subBytes(state)
544
		state = shiftRows(state)
545
		state = mixColumns(state)
546
		state = addRoundKey(state, exp_key, round_num)
547
	end
548
	
549
	-- Final round (No mixColumns()):
550
	state = subBytes(state)
551
	state = shiftRows(state)
552
	state = addRoundKey(state, exp_key, nr)
553
	return state
554
end
555

556
out.decrypt_block = function(data, key)
557
	local exp_key = out.key_schedule(key)
558
	local state = data
559
	local nr = 0
560
	
561
	if #exp_key == 176 then -- Key type 1 (128-bits)
562
		nr = 10
563
	elseif #exp_key == 208 then -- Key type 2 (192-bits)
564
		nr = 12
565
	elseif #exp_key == 240 then -- Key type 3 (256-bits)
566
		nr = 14
567
	else
568
		error("decrypt_block: Unknown key size?", 2)
569
	end
570
	
571
	-- Inital round:
572
	state = addRoundKey(state, exp_key, nr)
573
	
574
	-- Repeat (Nr-1) times:
575
	for round_num = nr-1, 2, -1 do
576
		state = invShiftRows(state)
577
		state = subBytes(state, true)
578
		state = addRoundKey(state, exp_key, round_num)
579
		state = mixColumns(state, true)
580
	end
581
	
582
	-- Final round (No mixColumns()):
583
	state = invShiftRows(state)
584
	state = subBytes(state, true)
585
	state = addRoundKey(state, exp_key, 1)
586
	return state
587
end
588

589
out.encrypt_block_customExpKey = function(data, exp_key--[[, key_type]]) -- Encrypt blocks, but using a precalculated expanded key instead of performing the key expansion on every step like with the normal encrypt_block(2) call
590
	local state = data
591
	local nr = 0
592
	if #exp_key == 176 then -- Key type 1 (128-bits)
593
		nr = 10
594
	elseif #exp_key == 208 then -- Key type 2 (192-bits)
595
		nr = 12
596
	elseif #exp_key == 240 then -- Key type 3 (256-bits)
597
		nr = 14
598
	else
599
		error("encrypt_block: Unknown key size?", 2)
600
	end
601
	
602
	-- Inital round:
603
	state = addRoundKey(state, exp_key, 1)
604
	
605
	-- Repeat (Nr-1) times:
606
	for round_num = 2, nr-1 do	
607
		state = subBytes(state)
608
		state = shiftRows(state)
609
		state = mixColumns(state)
610
		state = addRoundKey(state, exp_key, round_num)
611
	end
612
	
613
	-- Final round (No mixColumns()):
614
	state = subBytes(state)
615
	state = shiftRows(state)
616
	state = addRoundKey(state, exp_key, nr)
617
	return state
618
end
619

620
out.decrypt_block_customExpKey = function(data, exp_key--[[, key_type]])
621
	local state = data
622
	local nr = 0
623
	if #exp_key == 176 then -- Key type 1 (128-bits)
624
		nr = 10
625
	elseif #exp_key == 208 then -- Key type 2 (192-bits)
626
		nr = 12
627
	elseif #exp_key == 240 then -- Key type 3 (256-bits)
628
		nr = 14
629
	else
630
		error("decrypt_block: Unknown key size?", 2)
631
	end
632
	
633
	-- Inital round:
634
	state = addRoundKey(state, exp_key, nr)
635
	
636
	-- Repeat (Nr-1) times:
637
	for round_num = nr-1, 2, -1 do
638
		state = invShiftRows(state)
639
		state = subBytes(state, true)
640
		state = addRoundKey(state, exp_key, round_num)
641
		state = mixColumns(state, true)
642
	end
643
	
644
	-- Final round (No mixColumns()):
645
	state = invShiftRows(state)
646
	state = subBytes(state, true)
647
	state = addRoundKey(state, exp_key, 1)
648
	return state
649
end
650

651
-- Encrypt / Decrypt bytestreams (tables of bytes):
652

653
-- ECB (electronic codebook) Mode (not secure, do not use):
654

655
out.encrypt_bytestream_ecb = function(data, key)
656
	local blocks = {}
657
	local outputBytestream = {}
658
	local exp_key = out.key_schedule(key)
659
	for i=1, #data, 16 do
660
		local block = {}
661
		for j=1, 16 do
662
			block[j] = data[i+(j-1)] or 0
663
		end
664
		block = out.encrypt_block_customExpKey(block, exp_key)
665
		for j=1, 16 do
666
			table.insert(outputBytestream, block[j])
667
		end
668
		os.queueEvent("")
669
		os.pullEvent("")
670
	end
671
	return outputBytestream
672
end
673

674
out.decrypt_bytestream_ecb = function(data, key)
675
	local outputBytestream = {}
676
	local exp_key = out.key_schedule(key)
677
	for i=1, #data, 16 do
678
		local block = {}
679
		for j=1, 16 do
680
			block[j] = data[i+(j-1)] or 0
681
		end
682
		block = out.decrypt_block_customExpKey(block, exp_key)
683
		for j=1, 16 do
684
			table.insert(outputBytestream, block[j])
685
		end
686
		os.queueEvent("")
687
		os.pullEvent("")
688
	end
689
	for i=#outputBytestream, 1, -1 do
690
		if outputBytestream[i] ~= 0 then
691
			break
692
		else
693
			outputBytestream[i] = nil
694
		end
695
	end
696
	return outputBytestream
697
end
698

699
-- CBC (cipher-block chaining) mode:
700

701
out.encrypt_bytestream = function(data, key, init_vector)
702
	local blocks = { init_vector }
703
	local outputBytestream = {}
704
	local exp_key = out.key_schedule(key)
705
	if not init_vector then
706
		error("encrypt_bytestream: No initalization vector was passed.", 2)
707
	end
708
	for i=1, #data do
709
		if data[i] == nil or data[i] >= 256 then
710
			if type(data[i]) == "number" then
711
				error("encrypt_bytestream: Invalid data at i="..i.." data[i]="..data[i], 2)
712
			else
713
				error("encrypt_bytestream: Invalid data at i="..i.." data[i]="..type(data[i]), 2)
714
			end
715
		end
716
	end
717
	local s = os.clock()
718
	for i=1, math.ceil(#data/16) do
719
		local block = {}
720
		if not blocks[i] then
721
			error("encrypt_bytestream: blocks["..i.."] is nil! Input size: "..#data, 2)
722
		end
723
		for j=1, 16 do
724
			block[j] = data[((i-1)*16)+j] or 0
725
			block[j] = bxor(block[j], blocks[i][j]) -- XOR this block with the previous one
726
		end
727
		--print("#bytes: "..#block)
728
		block = out.encrypt_block_customExpKey(block, exp_key)
729
		table.insert(blocks, block)
730
		for j=1, 16 do
731
			insert(outputBytestream, block[j])
732
		end
733
        if os.clock() - s >= 2.5 then
734
            os.queueEvent("")
735
            os.pullEvent("")
736
            s = os.clock()
737
        end
738
	end
739
	return outputBytestream
740
end
741

742
out.decrypt_bytestream = function(data, key, init_vector)
743
	local blocks = { init_vector }
744
	local outputBytestream = {}
745
	local exp_key = out.key_schedule(key)
746
	if not init_vector then
747
		error("decrypt_bytestream: No initalization vector was passed.", 2)
748
	end
749
	local s = os.clock()
750
	for i=1, math.ceil(#data/16) do
751
		local block = {}
752
		if not blocks[i] then
753
			error("decrypt_bytestream: blocks["..i.."] is nil! Input size: "..#data, 2)
754
		end
755
		for j=1, 16 do
756
			block[j] = data[((i-1)*16)+j] or 0
757
		end
758
		table.insert(blocks, block)
759
		local dec_block = out.decrypt_block_customExpKey(block, exp_key)
760
		for j=1, 16 do
761
			dec_block[j] = bxor(dec_block[j], blocks[i][j]) -- We use XOR on the plaintext, not the ciphertext
762
			table.insert(outputBytestream, dec_block[j])
763
		end
764
        if os.clock() - s >= 2.5 then
765
            os.queueEvent("")
766
            os.pullEvent("")
767
            s = os.clock()
768
        end
769
	end
770
	-- Remove padding:
771
	for i=#outputBytestream, #outputBytestream-15, -1 do
772
		if outputBytestream[i] ~= 0 then
773
			break
774
		else
775
			outputBytestream[i] = nil
776
		end
777
	end
778
	return outputBytestream
779
end
780

781
-- Encrypt / Decrypt strings:
782

783
out.encrypt_str = function(data, key, iv)
784
	local byteStream = {}
785
	for i=1, #data do
786
		table.insert(byteStream, string.byte(data, i, i))
787
	end
788
	local output_bytestream = {}
789
	if iv then
790
		output_bytestream = out.encrypt_bytestream(byteStream, key, iv)
791
	else
792
		output_bytestream = out.encrypt_bytestream_ecb(byteStream, key)
793
	end
794
	local output = ""
795
	for i=1, #output_bytestream do
796
		output = output..string.char(output_bytestream[i])
797
	end
798
	return output
799
end
800

801
out.decrypt_str = function(data, key, iv)
802
	local byteStream = {}
803
	for i=1, #data do
804
		table.insert(byteStream, string.byte(data, i, i))
805
	end
806
	local output_bytestream = {}
807
	if iv then
808
		output_bytestream = out.decrypt_bytestream(byteStream, key, iv)
809
	else
810
		output_bytestream = out.decrypt_bytestream_ecb(byteStream, key)
811
	end
812
	local output = ""
813
	for i=1, #output_bytestream do
814
		output = output..string.char(output_bytestream[i])
815
	end
816
	return output
817
end
818

819
out.davies_meyer = function(data, h0)
820
	local last_h = h0
821
    for dm_iter=1, 16 do
822
        for i=1, math.ceil(#data/16) do
823
            local block = {}
824
            for j=1, 16 do
825
                block[j] = data[((i-1)*16)+j] or 0
826
            end
827
            local block = encrypt_block(last_h, block)
828
            for j=1, 16 do
829
                block[j] = bxor(block[j], last_h[j]) -- XOR h[i-1] with h[i].
830
            end
831
            last_h = block
832
            os.queueEvent("")
833
            os.pullEvent("")
834
        end
835
    end
836
	return last_h
837
end
838

839
local function increment_ctr(blk)
840
	local cpy = {}
841
	for i=1, 16 do
842
		cpy[i] = blk[i] or 0
843
	end
844
	cpy[1] = cpy[1] + incAmt
845
	for i=2, 16 do
846
		if cpy[i-1] <= 255 then
847
			break
848
		end
849
		local carry = cpy[i-1] - 255
850
		cpy[i] = cpy[i]+carry
851
	end
852
	return cpy
853
end
854

855
local counter_mode_context = {
856
	key = {},
857
	ctr = {},
858
	stream_cache = {}, -- Use "leftover" bytes from generate() here.
859
	set_key = function(self, key)
860
		if type(key) == "string" then
861
			if #key < 16 then
862
				error("set_key: Key length ("..#key..") must be at least 16 characters!", 2)
863
			end
864
			for i=1, 16 do
865
				self.key[i] = string.byte(key, i, i)
866
			end
867
		elseif type(key) == "table" then
868
			if #key < 16 then
869
				error("set_key: Key length ("..#key..") must be at least 16 bytes!", 2)
870
			end
871
			for i=1, 16 do
872
				if type(key[i]) ~= "number" or key[i] > 255 or key[i] < 0 then
873
					if type(key[i]) == "nil" then
874
						error("set_key: Value key["..i.."] is invalid: nil", 2)
875
					else
876
						error("set_key: Value key["..i.."] is invalid: "..key[i], 2)
877
					end
878
				end
879
				self.key[i] = key[i]
880
			end
881
		else
882
			error("set_key: Key type is not supported: "..type(key), 2)
883
		end
884
	end,
885
	set_ctr = function(self, ctr)
886
		if type(ctr) == "string" then
887
			if #ctr < 16 then
888
				error("set_ctr: Counter length ("..#ctr..") must be at least 16 characters!", 2)
889
			end
890
			for i=1, 16 do
891
				self.ctr[i] = string.byte(ctr, i, i)
892
			end
893
		elseif type(ctr) == "table" then
894
			if #ctr < 16 then
895
				error("set_ctr: Counter length ("..#ctr..") must be at least 16 bytes!", 2)
896
			end
897
			for i=1, 16 do
898
				if type(ctr[i]) ~= "number" or ctr[i] > 255 or ctr[i] < 0 then
899
					if type(ctr[i]) == "nil" then
900
						error("set_ctr: Value ctr["..i.."] is invalid: nil", 2)
901
					else
902
						error("set_ctr: Value ctr["..i.."] is invalid: "..ctr[i], 2)
903
					end
904
				end
905
				self.ctr[i] = ctr[i]
906
			end
907
		elseif type(ctr) == "number" then
908
			local b1 = bit.band( ctr, 0xFF )
909
			local b2 = bit.band( bit.brshift(bit.band( ctr, 0xFF00 ), 8), 0xFF )
910
			local b3 = bit.band( bit.brshift(bit.band( ctr, 0xFF0000 ), 16), 0xFF )
911
			local b4 = bit.band( bit.brshift(bit.band( ctr, 0xFF000000 ), 24), 0xFF )
912
			self.ctr = {}
913
			for i=1, 16 do
914
				self.ctr[i] = 0
915
			end
916
			self.ctr[1] = b1
917
			self.ctr[2] = b2
918
			self.ctr[3] = b3
919
			self.ctr[4] = b4
920
		else
921
			error("set_ctr: Counter type is not supported: "..type(ctr), 2)
922
		end
923
	end,
924
	generate = function(self, bytes)
925
		local genBytes = {}
926
		if #self.stream_cache >= bytes then
927
			for i=1, bytes do
928
				table.insert(genBytes, table.remove(self.stream_cache))
929
			end
930
		else
931
			for i=1, #self.stream_cache do
932
				table.insert(genBytes, table.remove(self.stream_cache))
933
			end
934
			local blocksToGenerate = math.ceil((bytes - #genBytes) / 16)
935
			for i=1, blocksToGenerate-1 do
936
				self.ctr = increment_ctr(self.ctr)
937
				local block = out.encrypt_block(self.ctr, self.key)
938
				for i=1, 16 do
939
					table.insert(genBytes, block[i])
940
				end
941
			end
942
			self.ctr = increment_ctr(self.ctr)
943
			local block = out.encrypt_block(self.ctr, self.key)
944
			for i=1, (bytes - #genBytes) do
945
				table.insert(genBytes, table.remove(block))
946
			end
947
			for i=1, #block do
948
				table.insert(self.stream_cache, table.remove(block))
949
			end
950
		end
951
		return genBytes
952
	end,
953
}
954

955
out.new_ctrMode = function(key, iv)
956
	local context = {
957
		stream_cache = {},
958
		key = {},
959
		iv = {},
960
		__index = counter_mode_context,
961
	}
962
	setmetatable(context, context)
963
	context:set_key(key)
964
	context:set_ctr(iv)
965
	return context
966
end
967

968
return out