Browse Source

added cs465 aes lab with added CLI interface

Just added a simple CLI to allow users to leverage the aes library.
Obviously, this was just a project to learn more about AES, not
something to be used in production systems
Derek McQuay 3 years ago
parent
commit
f5ffe7eb31
3 changed files with 1009 additions and 0 deletions
  1. 555
    0
      cs465/aes/aes.go
  2. 356
    0
      cs465/aes/aes_test.go
  3. 98
    0
      cs465/main.go

+ 555
- 0
cs465/aes/aes.go View File

@@ -0,0 +1,555 @@
1
+package aes
2
+
3
+import (
4
+	"encoding/hex"
5
+	"fmt"
6
+	"log"
7
+	"strconv"
8
+)
9
+
10
+var sbox = [][]byte{
11
+	{0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76},
12
+	{0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0},
13
+	{0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15},
14
+	{0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75},
15
+	{0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84},
16
+	{0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf},
17
+	{0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8},
18
+	{0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2},
19
+	{0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73},
20
+	{0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb},
21
+	{0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79},
22
+	{0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08},
23
+	{0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a},
24
+	{0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e},
25
+	{0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf},
26
+	{0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16},
27
+}
28
+
29
+var invSbox = [][]byte{
30
+	{0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb},
31
+	{0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb},
32
+	{0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e},
33
+	{0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25},
34
+	{0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92},
35
+	{0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84},
36
+	{0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06},
37
+	{0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b},
38
+	{0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73},
39
+	{0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e},
40
+	{0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b},
41
+	{0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4},
42
+	{0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f},
43
+	{0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef},
44
+	{0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61},
45
+	{0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d},
46
+}
47
+
48
+var mm = []byte{
49
+	2, 3, 1, 1,
50
+	1, 2, 3, 1,
51
+	1, 1, 2, 3,
52
+	3, 1, 1, 2,
53
+}
54
+
55
+var iMM = []byte{
56
+	14, 11, 13, 9,
57
+	9, 14, 11, 13,
58
+	13, 9, 14, 11,
59
+	11, 13, 9, 14,
60
+}
61
+
62
+var rcon = []byte{0x00, // rcon[] is 1-based, so the first entry is just a place holder
63
+	0x01, 0x02, 0x04, 0x08,
64
+	0x10, 0x20, 0x40, 0x80,
65
+	0x1B, 0x36, 0x6C, 0xD8,
66
+	0xAB, 0x4D, 0x9A, 0x2F,
67
+	0x5E, 0xBC, 0x63, 0xC6,
68
+	0x97, 0x35, 0x6A, 0xD4,
69
+	0xB3, 0x7D, 0xFA, 0xEF,
70
+	0xC5, 0x91, 0x39, 0x72,
71
+	0xE4, 0xD3, 0xBD, 0x61,
72
+	0xC2, 0x9F, 0x25, 0x4A,
73
+	0x94, 0x33, 0x66, 0xCC,
74
+	0x83, 0x1D, 0x3A, 0x74,
75
+	0xE8, 0xCB, 0x8D,
76
+}
77
+
78
+//Block renamed []byte type for simplicity
79
+type Block []byte
80
+
81
+var keyexpanded []Block
82
+var key Block
83
+
84
+//ToString writes []Block to a string in ascii form
85
+func ToString(all []Block, keysize int, k Block, decrypt bool) string {
86
+	final := ""
87
+	for _, bl := range all {
88
+		result := Block{}
89
+		if decrypt {
90
+			result = InvCipher(bl, keysize, k)
91
+		} else {
92
+			result = Cipher(bl, keysize, k)
93
+		}
94
+		final += string(result)
95
+	}
96
+	return final
97
+}
98
+
99
+//ToHex writes []Block to a string in hex form
100
+func ToHex(all []Block, keysize int, k Block, decrypt bool) string {
101
+	final := ""
102
+	for _, bl := range all {
103
+		result := Block{}
104
+		if decrypt {
105
+			result = InvCipher(bl, keysize, k)
106
+		} else {
107
+			result = Cipher(bl, keysize, k)
108
+		}
109
+		for i := 0; i < 16; i++ {
110
+			final += fmt.Sprintf("0x%x ", result[i])
111
+		}
112
+	}
113
+	return final
114
+}
115
+
116
+//BlockGen helps create []Block from incoming string
117
+func BlockGen(arg string) []Block {
118
+	all := []Block{}
119
+	b := Block{}
120
+	for i, char := range arg {
121
+		value, err := strconv.ParseUint(
122
+			hex.EncodeToString(
123
+				[]byte(string(char))),
124
+			16,
125
+			32,
126
+		)
127
+		if err != nil {
128
+			log.Fatal(err)
129
+		}
130
+		if i%16 == 0 && i > 0 {
131
+			all = append(all, b)
132
+			b = b[:0]
133
+		}
134
+		b = append(b, byte(value))
135
+		if i == len(arg)-1 {
136
+			all = append(all, b)
137
+		}
138
+	}
139
+	return all
140
+}
141
+
142
+//Cipher performs AES cipher
143
+func Cipher(cur Block, bit int, incomingKey Block) Block {
144
+	if len(cur) != 16 {
145
+		missing := 16 - len(cur)
146
+		for i := 0; i < missing; i++ {
147
+			cur = append(cur, 0x00)
148
+		}
149
+	}
150
+	key = Block{}
151
+	keyexpanded = []Block{}
152
+	assignKey(incomingKey)
153
+	if bit == 128 {
154
+		keyExpansionBase(128)
155
+		cur = addRoundKey(cur, 0)
156
+		for i := 0; i < 9; i++ {
157
+			cur = subBytes(cur)
158
+			cur = shiftRows(cur)
159
+			cur = mixColumns(cur)
160
+			cur = addRoundKey(cur, i+1)
161
+		}
162
+		cur = subBytes(cur)
163
+		cur = shiftRows(cur)
164
+		cur = addRoundKey(cur, 10)
165
+		return cur
166
+	}
167
+	if bit == 192 {
168
+		keyExpansionBase(192)
169
+		cur = addRoundKey(cur, 0)
170
+		for i := 0; i < 11; i++ {
171
+			cur = subBytes(cur)
172
+			cur = shiftRows(cur)
173
+			cur = mixColumns(cur)
174
+			cur = addRoundKey(cur, i+1)
175
+			if i == 0 {
176
+			}
177
+		}
178
+		cur = subBytes(cur)
179
+		cur = shiftRows(cur)
180
+		cur = addRoundKey(cur, 12)
181
+		return cur
182
+	}
183
+	if bit == 256 {
184
+		keyExpansionBase(256)
185
+		cur = addRoundKey(cur, 0)
186
+		for i := 0; i < 13; i++ {
187
+			cur = subBytes(cur)
188
+			cur = shiftRows(cur)
189
+			cur = mixColumns(cur)
190
+			cur = addRoundKey(cur, i+1)
191
+			if i == 0 {
192
+			}
193
+		}
194
+		cur = subBytes(cur)
195
+		cur = shiftRows(cur)
196
+		cur = addRoundKey(cur, 14)
197
+		return cur
198
+	}
199
+	return cur
200
+}
201
+
202
+//InvCipher inverse AES cipher
203
+func InvCipher(cur Block, bit int, incomingKey Block) Block {
204
+	if len(cur) != 16 {
205
+		missing := 16 - len(cur)
206
+		for i := 0; i < missing; i++ {
207
+			cur = append(cur, 0x00)
208
+		}
209
+	}
210
+	key = Block{}
211
+	keyexpanded = []Block{}
212
+	assignKey(incomingKey)
213
+	if bit == 128 {
214
+		keyExpansionBase(128)
215
+		cur = addRoundKey(cur, 10)
216
+		for i := 9; i > 0; i-- {
217
+			cur = invShiftRows(cur)
218
+			cur = invSubBytes(cur)
219
+			cur = addRoundKey(cur, i)
220
+			cur = invMixColumns(cur)
221
+		}
222
+		cur = invSubBytes(cur)
223
+		cur = invShiftRows(cur)
224
+		cur = addRoundKey(cur, 0)
225
+		return cur
226
+	}
227
+	if bit == 192 {
228
+		keyExpansionBase(192)
229
+		cur = addRoundKey(cur, 12)
230
+		for i := 11; i > 0; i-- {
231
+			cur = invShiftRows(cur)
232
+			cur = invSubBytes(cur)
233
+			cur = addRoundKey(cur, i)
234
+			cur = invMixColumns(cur)
235
+		}
236
+		cur = invSubBytes(cur)
237
+		cur = invShiftRows(cur)
238
+		cur = addRoundKey(cur, 0)
239
+		return cur
240
+	}
241
+	if bit == 256 {
242
+		keyExpansionBase(256)
243
+		cur = addRoundKey(cur, 14)
244
+		for i := 13; i > 0; i-- {
245
+			cur = invShiftRows(cur)
246
+			cur = invSubBytes(cur)
247
+			cur = addRoundKey(cur, i)
248
+			cur = invMixColumns(cur)
249
+		}
250
+		cur = invSubBytes(cur)
251
+		cur = invShiftRows(cur)
252
+		cur = addRoundKey(cur, 0)
253
+		return cur
254
+	}
255
+	return cur
256
+}
257
+
258
+func addRoundKey(cur Block, iteration int) Block {
259
+	for i := 0; i < 16; i++ {
260
+		cur[i] = cur[i] ^ keyexpanded[iteration][i]
261
+	}
262
+	return cur
263
+}
264
+
265
+func assignKey(cur Block) {
266
+	key = cur
267
+}
268
+
269
+func keyExpansionBase(keysize int) {
270
+	if keysize == 128 {
271
+		keyexpanded = append(keyexpanded, key)
272
+		for i := 0; i < 10; i++ {
273
+			keyExpansion(keyexpanded[i], i+1)
274
+		}
275
+	} else if keysize == 192 {
276
+		keyexpanded = append(keyexpanded, key)
277
+		for i := 0; i < 8; i++ {
278
+			keyExpansion192(keyexpanded[i], i+1)
279
+		}
280
+		temp := keyexpanded
281
+		keyexpanded = []Block{}
282
+		for i := 0; i < 9; i++ {
283
+			if i == 8 {
284
+				a := Block{
285
+					temp[i][0], temp[i][1], temp[i][2], temp[i][3],
286
+					temp[i][6], temp[i][7], temp[i][8], temp[i][9],
287
+					temp[i][12], temp[i][13], temp[i][14], temp[i][15],
288
+					temp[i][18], temp[i][19], temp[i][20], temp[i][21],
289
+				}
290
+				b := Block{
291
+					temp[i][4], temp[i][5], 0, 0,
292
+					temp[i][10], temp[i][11], 0, 0,
293
+					temp[i][16], temp[i][17], 0, 0,
294
+					temp[i][22], temp[i][23], 0, 0,
295
+				}
296
+				keyexpanded = append(keyexpanded, a)
297
+				keyexpanded = append(keyexpanded, b)
298
+			} else {
299
+				a := Block{
300
+					temp[i][0], temp[i][1], temp[i][2], temp[i][3],
301
+					temp[i][6], temp[i][7], temp[i][8], temp[i][9],
302
+					temp[i][12], temp[i][13], temp[i][14], temp[i][15],
303
+					temp[i][18], temp[i][19], temp[i][20], temp[i][21],
304
+				}
305
+				b := Block{
306
+					temp[i][4], temp[i][5], temp[i+1][0], temp[i+1][1],
307
+					temp[i][10], temp[i][11], temp[i+1][6], temp[i+1][7],
308
+					temp[i][16], temp[i][17], temp[i+1][12], temp[i+1][13],
309
+					temp[i][22], temp[i][23], temp[i+1][18], temp[i+1][19],
310
+				}
311
+				c := Block{
312
+					temp[i+1][2], temp[i+1][3], temp[i+1][4], temp[i+1][5],
313
+					temp[i+1][8], temp[i+1][9], temp[i+1][10], temp[i+1][11],
314
+					temp[i+1][14], temp[i+1][15], temp[i+1][16], temp[i+1][17],
315
+					temp[i+1][20], temp[i+1][21], temp[i+1][22], temp[i+1][23],
316
+				}
317
+				keyexpanded = append(keyexpanded, a)
318
+				keyexpanded = append(keyexpanded, b)
319
+				keyexpanded = append(keyexpanded, c)
320
+			}
321
+			i++
322
+		}
323
+	} else if keysize == 256 {
324
+		keyexpanded = append(keyexpanded, key)
325
+		for i := 0; i < 8; i++ {
326
+			keyExpansion256(keyexpanded[i], i+1)
327
+		}
328
+		temp := keyexpanded
329
+		keyexpanded = []Block{}
330
+		for i := 0; i < 9; i++ {
331
+			a := Block{
332
+				temp[i][0], temp[i][1], temp[i][2], temp[i][3],
333
+				temp[i][8], temp[i][9], temp[i][10], temp[i][11],
334
+				temp[i][16], temp[i][17], temp[i][18], temp[i][19],
335
+				temp[i][24], temp[i][25], temp[i][26], temp[i][27],
336
+			}
337
+			b := Block{
338
+				temp[i][4], temp[i][5], temp[i][6], temp[i][7],
339
+				temp[i][12], temp[i][13], temp[i][14], temp[i][15],
340
+				temp[i][20], temp[i][21], temp[i][22], temp[i][23],
341
+				temp[i][28], temp[i][29], temp[i][30], temp[i][31],
342
+			}
343
+			keyexpanded = append(keyexpanded, a)
344
+			keyexpanded = append(keyexpanded, b)
345
+		}
346
+	}
347
+}
348
+
349
+func keyExpansion(cur Block, iteration int) Block {
350
+	var nb = Block{ //nb = nextBlock
351
+		0x00, 0x00, 0x00, 0x00,
352
+		0x00, 0x00, 0x00, 0x00,
353
+		0x00, 0x00, 0x00, 0x00,
354
+		0x00, 0x00, 0x00, 0x00,
355
+	}
356
+	rotword := []byte{cur[7], cur[11], cur[15], cur[3]}
357
+	for i := 0; i < 4; i++ {
358
+		upper, lower := splitBytes(rotword[i])
359
+		rotword[i] = sbox[upper][lower]
360
+	}
361
+	rotword[0] = rotword[0] ^ cur[0] ^ rcon[iteration]
362
+	rotword[1] = rotword[1] ^ cur[4]
363
+	rotword[2] = rotword[2] ^ cur[8]
364
+	rotword[3] = rotword[3] ^ cur[12]
365
+
366
+	nb[0], nb[4], nb[8], nb[12] = rotword[0], rotword[1], rotword[2], rotword[3]
367
+	nb[1], nb[5], nb[9], nb[13] = nb[0]^cur[1], nb[4]^cur[5], nb[8]^cur[9], nb[12]^cur[13]
368
+	nb[2], nb[6], nb[10], nb[14] = nb[1]^cur[2], nb[5]^cur[6], nb[9]^cur[10], nb[13]^cur[14]
369
+	nb[3], nb[7], nb[11], nb[15] = nb[2]^cur[3], nb[6]^cur[7], nb[10]^cur[11], nb[14]^cur[15]
370
+	keyexpanded = append(keyexpanded, nb)
371
+	return nb
372
+}
373
+
374
+func keyExpansion192(cur Block, iteration int) Block {
375
+	var nb = Block{ //nb = nextBlock
376
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
377
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
378
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
379
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
380
+	}
381
+	rotword := []byte{cur[11], cur[17], cur[23], cur[5]}
382
+	for i := 0; i < 4; i++ {
383
+		upper, lower := splitBytes(rotword[i])
384
+		rotword[i] = sbox[upper][lower]
385
+	}
386
+	rotword[0] = rotword[0] ^ cur[0] ^ rcon[iteration]
387
+	rotword[1] = rotword[1] ^ cur[6]
388
+	rotword[2] = rotword[2] ^ cur[12]
389
+	rotword[3] = rotword[3] ^ cur[18]
390
+
391
+	nb[0], nb[6], nb[12], nb[18] = rotword[0], rotword[1], rotword[2], rotword[3]
392
+	nb[1], nb[7], nb[13], nb[19] = nb[0]^cur[1], nb[6]^cur[7], nb[12]^cur[13], nb[18]^cur[19]
393
+	nb[2], nb[8], nb[14], nb[20] = nb[1]^cur[2], nb[7]^cur[8], nb[13]^cur[14], nb[19]^cur[20]
394
+	nb[3], nb[9], nb[15], nb[21] = nb[2]^cur[3], nb[8]^cur[9], nb[14]^cur[15], nb[20]^cur[21]
395
+	nb[4], nb[10], nb[16], nb[22] = nb[3]^cur[4], nb[9]^cur[10], nb[15]^cur[16], nb[21]^cur[22]
396
+	nb[5], nb[11], nb[17], nb[23] = nb[4]^cur[5], nb[10]^cur[11], nb[16]^cur[17], nb[22]^cur[23]
397
+	keyexpanded = append(keyexpanded, nb)
398
+
399
+	return nb
400
+}
401
+
402
+func keyExpansion256(cur Block, iteration int) Block {
403
+	var nb = Block{ //nb = nextBlock
404
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
405
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
406
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
407
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
408
+	}
409
+	rotword := []byte{cur[15], cur[23], cur[31], cur[7]}
410
+	for i := 0; i < 4; i++ {
411
+		upper, lower := splitBytes(rotword[i])
412
+		rotword[i] = sbox[upper][lower]
413
+	}
414
+	rotword[0] = rotword[0] ^ cur[0] ^ rcon[iteration]
415
+	rotword[1] = rotword[1] ^ cur[8]
416
+	rotword[2] = rotword[2] ^ cur[16]
417
+	rotword[3] = rotword[3] ^ cur[24]
418
+
419
+	nb[0], nb[8], nb[16], nb[24] = rotword[0], rotword[1], rotword[2], rotword[3]
420
+	nb[1], nb[9], nb[17], nb[25] = nb[0]^cur[1], nb[8]^cur[9], nb[16]^cur[17], nb[24]^cur[25]
421
+	nb[2], nb[10], nb[18], nb[26] = nb[1]^cur[2], nb[9]^cur[10], nb[17]^cur[18], nb[25]^cur[26]
422
+	nb[3], nb[11], nb[19], nb[27] = nb[2]^cur[3], nb[10]^cur[11], nb[18]^cur[19], nb[26]^cur[27]
423
+	sw := []byte{nb[3], nb[11], nb[19], nb[27]} //sw = subword
424
+	for i := 0; i < 4; i++ {
425
+		upper, lower := splitBytes(sw[i])
426
+		sw[i] = sbox[upper][lower]
427
+	}
428
+	nb[4], nb[12], nb[20], nb[28] = sw[0]^cur[4], sw[1]^cur[12], sw[2]^cur[20], sw[3]^cur[28]
429
+	nb[5], nb[13], nb[21], nb[29] = nb[4]^cur[5], nb[12]^cur[13], nb[20]^cur[21], nb[28]^cur[29]
430
+	nb[6], nb[14], nb[22], nb[30] = nb[5]^cur[6], nb[13]^cur[14], nb[21]^cur[22], nb[29]^cur[30]
431
+	nb[7], nb[15], nb[23], nb[31] = nb[6]^cur[7], nb[14]^cur[15], nb[22]^cur[23], nb[30]^cur[31]
432
+	keyexpanded = append(keyexpanded, nb)
433
+
434
+	return nb
435
+}
436
+
437
+func splitBytes(b byte) (byte, byte) {
438
+	return b >> 4, b & 0x0f
439
+}
440
+
441
+func subBytes(cur Block) Block {
442
+	for i := 0; i < 16; i++ {
443
+		upper, lower := splitBytes(cur[i])
444
+		cur[i] = sbox[upper][lower]
445
+	}
446
+	return cur
447
+}
448
+
449
+func invSubBytes(cur Block) Block {
450
+	for i := 0; i < 16; i++ {
451
+		upper, lower := splitBytes(cur[i])
452
+		cur[i] = invSbox[upper][lower]
453
+	}
454
+	return cur
455
+}
456
+
457
+func xtime(cur byte) []byte {
458
+	var bytes []byte
459
+	bytes = append(bytes, cur)
460
+	for i := 1; i < 8; i++ { // first iteration done outside of for-loop
461
+		if (cur >> 7) == 1 {
462
+			cur = cur << 1
463
+			cur = cur ^ 0x1b
464
+		} else {
465
+			cur = cur << 1
466
+		}
467
+		bytes = append(bytes, cur)
468
+	}
469
+	return bytes
470
+}
471
+
472
+func ffmult(cur []byte, multiplier byte) byte {
473
+	if multiplier == 1 {
474
+		return cur[0]
475
+	} else if multiplier == 2 {
476
+		return cur[1]
477
+	} else if multiplier == 3 {
478
+		return cur[0] ^ cur[1]
479
+	} else if multiplier == 9 {
480
+		return cur[0] ^ cur[3]
481
+	} else if multiplier == 11 {
482
+		return cur[0] ^ cur[1] ^ cur[3]
483
+	} else if multiplier == 13 {
484
+		return cur[0] ^ cur[2] ^ cur[3]
485
+	} else if multiplier == 14 {
486
+		return cur[1] ^ cur[2] ^ cur[3]
487
+	}
488
+	return 0
489
+}
490
+
491
+func mixColumnsAssist(cur []byte) []byte {
492
+	a1 := ffmult(xtime(cur[0]), mm[0]) ^ ffmult(xtime(cur[1]), mm[1]) ^ ffmult(xtime(cur[2]), mm[2]) ^ ffmult(xtime(cur[3]), mm[3])
493
+	a2 := ffmult(xtime(cur[0]), mm[4]) ^ ffmult(xtime(cur[1]), mm[5]) ^ ffmult(xtime(cur[2]), mm[6]) ^ ffmult(xtime(cur[3]), mm[7])
494
+	a3 := ffmult(xtime(cur[0]), mm[8]) ^ ffmult(xtime(cur[1]), mm[9]) ^ ffmult(xtime(cur[2]), mm[10]) ^ ffmult(xtime(cur[3]), mm[11])
495
+	a4 := ffmult(xtime(cur[0]), mm[12]) ^ ffmult(xtime(cur[1]), mm[13]) ^ ffmult(xtime(cur[2]), mm[14]) ^ ffmult(xtime(cur[3]), mm[15])
496
+	return []byte{a1, a2, a3, a4}
497
+}
498
+
499
+func mixColumns(cur Block) Block {
500
+	col1 := []byte{cur[0], cur[4], cur[8], cur[12]}
501
+	col2 := []byte{cur[1], cur[5], cur[9], cur[13]}
502
+	col3 := []byte{cur[2], cur[6], cur[10], cur[14]}
503
+	col4 := []byte{cur[3], cur[7], cur[11], cur[15]}
504
+	col1 = mixColumnsAssist(col1)
505
+	col2 = mixColumnsAssist(col2)
506
+	col3 = mixColumnsAssist(col3)
507
+	col4 = mixColumnsAssist(col4)
508
+	cur = Block{
509
+		col1[0], col2[0], col3[0], col4[0],
510
+		col1[1], col2[1], col3[1], col4[1],
511
+		col1[2], col2[2], col3[2], col4[2],
512
+		col1[3], col2[3], col3[3], col4[3],
513
+	}
514
+	return cur
515
+}
516
+
517
+func invMixColumns(cur Block) Block {
518
+	col1 := []byte{cur[0], cur[4], cur[8], cur[12]}
519
+	col2 := []byte{cur[1], cur[5], cur[9], cur[13]}
520
+	col3 := []byte{cur[2], cur[6], cur[10], cur[14]}
521
+	col4 := []byte{cur[3], cur[7], cur[11], cur[15]}
522
+	col1 = invMixColumnsAssist(col1)
523
+	col2 = invMixColumnsAssist(col2)
524
+	col3 = invMixColumnsAssist(col3)
525
+	col4 = invMixColumnsAssist(col4)
526
+	cur = Block{
527
+		col1[0], col2[0], col3[0], col4[0],
528
+		col1[1], col2[1], col3[1], col4[1],
529
+		col1[2], col2[2], col3[2], col4[2],
530
+		col1[3], col2[3], col3[3], col4[3],
531
+	}
532
+	return cur
533
+}
534
+
535
+func invMixColumnsAssist(cur []byte) []byte {
536
+	a1 := ffmult(xtime(cur[0]), iMM[0]) ^ ffmult(xtime(cur[1]), iMM[1]) ^ ffmult(xtime(cur[2]), iMM[2]) ^ ffmult(xtime(cur[3]), iMM[3])
537
+	a2 := ffmult(xtime(cur[0]), iMM[4]) ^ ffmult(xtime(cur[1]), iMM[5]) ^ ffmult(xtime(cur[2]), iMM[6]) ^ ffmult(xtime(cur[3]), iMM[7])
538
+	a3 := ffmult(xtime(cur[0]), iMM[8]) ^ ffmult(xtime(cur[1]), iMM[9]) ^ ffmult(xtime(cur[2]), iMM[10]) ^ ffmult(xtime(cur[3]), iMM[11])
539
+	a4 := ffmult(xtime(cur[0]), iMM[12]) ^ ffmult(xtime(cur[1]), iMM[13]) ^ ffmult(xtime(cur[2]), iMM[14]) ^ ffmult(xtime(cur[3]), iMM[15])
540
+	return []byte{a1, a2, a3, a4}
541
+}
542
+
543
+func shiftRows(cur Block) Block {
544
+	cur[4], cur[5], cur[6], cur[7] = cur[5], cur[6], cur[7], cur[4]
545
+	cur[8], cur[9], cur[10], cur[11] = cur[10], cur[11], cur[8], cur[9]
546
+	cur[12], cur[13], cur[14], cur[15] = cur[15], cur[12], cur[13], cur[14]
547
+	return cur
548
+}
549
+
550
+func invShiftRows(cur Block) Block {
551
+	cur[4], cur[5], cur[6], cur[7] = cur[7], cur[4], cur[5], cur[6]
552
+	cur[8], cur[9], cur[10], cur[11] = cur[10], cur[11], cur[8], cur[9]
553
+	cur[12], cur[13], cur[14], cur[15] = cur[13], cur[14], cur[15], cur[12]
554
+	return cur
555
+}

+ 356
- 0
cs465/aes/aes_test.go View File

@@ -0,0 +1,356 @@
1
+package aes
2
+
3
+import (
4
+	"bytes"
5
+	"testing"
6
+)
7
+
8
+func TestShiftRows(t *testing.T) {
9
+	input := Block{
10
+		0, 1, 2, 3,
11
+		4, 5, 6, 7,
12
+		8, 9, 10, 11,
13
+		12, 13, 14, 15,
14
+	}
15
+	expected := Block{
16
+		0, 1, 2, 3,
17
+		5, 6, 7, 4,
18
+		10, 11, 8, 9,
19
+		15, 12, 13, 14,
20
+	}
21
+	actual := shiftRows(input)
22
+	if !bytes.Equal(expected, actual) {
23
+		t.Errorf(
24
+			"failed to get right ShiftRows:\n\texpected: % x\n\t  actual: % x",
25
+			expected,
26
+			actual,
27
+		)
28
+
29
+	}
30
+}
31
+
32
+func TestSubBytes(t *testing.T) {
33
+	input := Block{
34
+		0x19, 0xa0, 0x9a, 0xe9,
35
+		0x3d, 0xf4, 0xc6, 0xf8,
36
+		0xe3, 0xe2, 0x8d, 0x48,
37
+		0xbe, 0x2b, 0x2a, 0x08,
38
+	}
39
+	expected := Block{
40
+		0xd4, 0xe0, 0xb8, 0x1e,
41
+		0x27, 0xbf, 0xb4, 0x41,
42
+		0x11, 0x98, 0x5d, 0x52,
43
+		0xae, 0xf1, 0xe5, 0x30,
44
+	}
45
+	actual := subBytes(input)
46
+	if !bytes.Equal(expected, actual) {
47
+		t.Errorf(
48
+			"failed to get right ShiftRows:\n\texpected: % x\n\t  actual: % x",
49
+			expected,
50
+			actual,
51
+		)
52
+	}
53
+}
54
+
55
+func TestSplitBytes(t *testing.T) {
56
+	input := byte(0xab)
57
+	expected1 := byte(0xa)
58
+	expected2 := byte(0xb)
59
+	actual1, actual2 := splitBytes(input)
60
+	if expected1 != actual1 || expected2 != actual2 {
61
+		t.Errorf(
62
+			"failed to get SplitBytes:\n\texpected: 0x%x 0x%x\n\t  actual: 0x%x 0x%x",
63
+			expected1,
64
+			expected2,
65
+			actual1,
66
+			actual2,
67
+		)
68
+	}
69
+}
70
+
71
+func TestXtime(t *testing.T) {
72
+	input := byte(0x14)
73
+	expected := []byte{
74
+		0x14, 0x28, 0x50, 0xa0,
75
+		0x5b, 0xb6, 0x77, 0xee,
76
+	}
77
+	actual := xtime(input)
78
+	if !bytes.Equal(expected, actual) {
79
+		t.Errorf(
80
+			"failed to get Xtime:\n\texpected: % x\n\tactual: % x",
81
+			expected,
82
+			actual,
83
+		)
84
+	}
85
+}
86
+
87
+func TestFFmult(t *testing.T) {
88
+	input := xtime(0x14)
89
+	expected1 := byte(0x14)
90
+	expected2 := byte(0x28)
91
+	expected3 := byte(0x3c)
92
+	actual1 := ffmult(input, 1)
93
+	actual2 := ffmult(input, 2)
94
+	actual3 := ffmult(input, 3)
95
+	if expected1 != actual1 || expected2 != actual2 || expected3 != actual3 {
96
+		t.Errorf(
97
+			"failed to get FFmult:\n\texpected: 0x% x 0x% x 0x% x\n\t actual:0x% x 0x% x 0x% x",
98
+			expected1,
99
+			expected2,
100
+			expected3,
101
+			actual1,
102
+			actual2,
103
+			actual3,
104
+		)
105
+	}
106
+}
107
+
108
+func TestMixColumns(t *testing.T) {
109
+	input := Block{
110
+		0xdb, 0xf2, 0x2d, 0x01,
111
+		0x13, 0x0a, 0x26, 0x01,
112
+		0x53, 0x22, 0x31, 0x01,
113
+		0x45, 0x5c, 0x4c, 0x01,
114
+	}
115
+	expected := Block{
116
+		0x8e, 0x9f, 0x4d, 0x01,
117
+		0x4d, 0xdc, 0x7e, 0x01,
118
+		0xa1, 0x58, 0xbd, 0x01,
119
+		0xbc, 0x9d, 0xf8, 0x01,
120
+	}
121
+	actual := mixColumns(input)
122
+	for i := 0; i < 16; i++ {
123
+		if actual[i] != expected[i] {
124
+			t.Errorf(
125
+				"failed to get MixColumn:\n\texpected: %v \n\tactual: %v", expected, actual,
126
+			)
127
+		}
128
+	}
129
+}
130
+
131
+func TestCipher(t *testing.T) {
132
+	key := Block{
133
+		0x2b, 0x28, 0xab, 0x09,
134
+		0x7e, 0xae, 0xf7, 0xcf,
135
+		0x15, 0xd2, 0x15, 0x4f,
136
+		0x16, 0xa6, 0x88, 0x3c,
137
+	}
138
+	input := Block{
139
+		0x32, 0x88, 0x31, 0xe0,
140
+		0x43, 0x5a, 0x31, 0x37,
141
+		0xf6, 0x30, 0x98, 0x07,
142
+		0xa8, 0x8d, 0xa2, 0x34,
143
+	}
144
+	expected := Block{
145
+		0x39, 0x02, 0xdc, 0x19,
146
+		0x25, 0xdc, 0x11, 0x6a,
147
+		0x84, 0x09, 0x85, 0x0b,
148
+		0x1d, 0xfb, 0x97, 0x32,
149
+	}
150
+	actual := Cipher(input, 128, key)
151
+	for i := 0; i < 16; i++ {
152
+		if actual[i] != expected[i] {
153
+			t.Errorf(
154
+				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
155
+			)
156
+		}
157
+	}
158
+
159
+	//Test case for 128 from FIPS
160
+
161
+	key = Block{
162
+		0x00, 0x04, 0x08, 0x0c,
163
+		0x01, 0x05, 0x09, 0x0d,
164
+		0x02, 0x06, 0x0a, 0x0e,
165
+		0x03, 0x07, 0x0b, 0x0f,
166
+	}
167
+	input = Block{
168
+		0x00, 0x44, 0x88, 0xcc,
169
+		0x11, 0x55, 0x99, 0xdd,
170
+		0x22, 0x66, 0xaa, 0xee,
171
+		0x33, 0x77, 0xbb, 0xff,
172
+	}
173
+	expected = Block{
174
+		0x69, 0x6a, 0xd8, 0x70,
175
+		0xc4, 0x7b, 0xcd, 0xb4,
176
+		0xe0, 0x04, 0xb7, 0xc5,
177
+		0xd8, 0x30, 0x80, 0x5a,
178
+	}
179
+	actual = Cipher(input, 128, key)
180
+	for i := 0; i < 16; i++ {
181
+		if actual[i] != expected[i] {
182
+			t.Errorf(
183
+				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
184
+			)
185
+		}
186
+	}
187
+
188
+	//Test case for 192 from FIPS
189
+	key = Block{
190
+		0x00, 0x04, 0x08, 0x0c, 0x10, 0x14,
191
+		0x01, 0x05, 0x09, 0x0d, 0x11, 0x15,
192
+		0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16,
193
+		0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17,
194
+	}
195
+	input = Block{
196
+		0x00, 0x44, 0x88, 0xcc,
197
+		0x11, 0x55, 0x99, 0xdd,
198
+		0x22, 0x66, 0xaa, 0xee,
199
+		0x33, 0x77, 0xbb, 0xff,
200
+	}
201
+	expected = Block{
202
+		0xdd, 0x86, 0x6e, 0xec,
203
+		0xa9, 0x4c, 0xaf, 0x0d,
204
+		0x7c, 0xdf, 0x70, 0x71,
205
+		0xa4, 0xe0, 0xa0, 0x91,
206
+	}
207
+	actual = Cipher(input, 192, key)
208
+	for i := 0; i < 16; i++ {
209
+		if actual[i] != expected[i] {
210
+			t.Errorf(
211
+				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
212
+			)
213
+		}
214
+	}
215
+
216
+	//Test case for 256 from FIPS
217
+	key = Block{
218
+		0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
219
+		0x01, 0x05, 0x09, 0x0d, 0x11, 0x15, 0x19, 0x1d,
220
+		0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16, 0x1a, 0x1e,
221
+		0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17, 0x1b, 0x1f,
222
+	}
223
+	input = Block{
224
+		0x00, 0x44, 0x88, 0xcc,
225
+		0x11, 0x55, 0x99, 0xdd,
226
+		0x22, 0x66, 0xaa, 0xee,
227
+		0x33, 0x77, 0xbb, 0xff,
228
+	}
229
+	expected = Block{
230
+		0x8e, 0x51, 0xea, 0x4b,
231
+		0xa2, 0x67, 0xfc, 0x49,
232
+		0xb7, 0x45, 0x49, 0x60,
233
+		0xca, 0xbf, 0x90, 0x89,
234
+	}
235
+	actual = Cipher(input, 256, key)
236
+	for i := 0; i < 16; i++ {
237
+		if actual[i] != expected[i] {
238
+			t.Errorf(
239
+				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
240
+			)
241
+		}
242
+	}
243
+}
244
+
245
+func TestInvCipher(t *testing.T) {
246
+	key := Block{
247
+		0x2b, 0x28, 0xab, 0x09,
248
+		0x7e, 0xae, 0xf7, 0xcf,
249
+		0x15, 0xd2, 0x15, 0x4f,
250
+		0x16, 0xa6, 0x88, 0x3c,
251
+	}
252
+	input := Block{
253
+		0x39, 0x02, 0xdc, 0x19,
254
+		0x25, 0xdc, 0x11, 0x6a,
255
+		0x84, 0x09, 0x85, 0x0b,
256
+		0x1d, 0xfb, 0x97, 0x32,
257
+	}
258
+	expected := Block{
259
+		0x32, 0x88, 0x31, 0xe0,
260
+		0x43, 0x5a, 0x31, 0x37,
261
+		0xf6, 0x30, 0x98, 0x07,
262
+		0xa8, 0x8d, 0xa2, 0x34,
263
+	}
264
+	actual := InvCipher(input, 128, key)
265
+	for i := 0; i < 16; i++ {
266
+		if actual[i] != expected[i] {
267
+			t.Errorf(
268
+				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
269
+			)
270
+		}
271
+	}
272
+
273
+	//Test case for 128 from FIPS
274
+	key = Block{
275
+		0x00, 0x04, 0x08, 0x0c,
276
+		0x01, 0x05, 0x09, 0x0d,
277
+		0x02, 0x06, 0x0a, 0x0e,
278
+		0x03, 0x07, 0x0b, 0x0f,
279
+	}
280
+	input = Block{
281
+		0x69, 0x6a, 0xd8, 0x70,
282
+		0xc4, 0x7b, 0xcd, 0xb4,
283
+		0xe0, 0x04, 0xb7, 0xc5,
284
+		0xd8, 0x30, 0x80, 0x5a,
285
+	}
286
+	expected = Block{
287
+		0x00, 0x44, 0x88, 0xcc,
288
+		0x11, 0x55, 0x99, 0xdd,
289
+		0x22, 0x66, 0xaa, 0xee,
290
+		0x33, 0x77, 0xbb, 0xff,
291
+	}
292
+	actual = InvCipher(input, 128, key)
293
+	for i := 0; i < 16; i++ {
294
+		if actual[i] != expected[i] {
295
+			t.Errorf(
296
+				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
297
+			)
298
+		}
299
+	}
300
+
301
+	//Test case for 192 from FIPS
302
+	key = Block{
303
+		0x00, 0x04, 0x08, 0x0c, 0x10, 0x14,
304
+		0x01, 0x05, 0x09, 0x0d, 0x11, 0x15,
305
+		0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16,
306
+		0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17,
307
+	}
308
+	input = Block{
309
+		0xdd, 0x86, 0x6e, 0xec,
310
+		0xa9, 0x4c, 0xaf, 0x0d,
311
+		0x7c, 0xdf, 0x70, 0x71,
312
+		0xa4, 0xe0, 0xa0, 0x91,
313
+	}
314
+	expected = Block{
315
+		0x00, 0x44, 0x88, 0xcc,
316
+		0x11, 0x55, 0x99, 0xdd,
317
+		0x22, 0x66, 0xaa, 0xee,
318
+		0x33, 0x77, 0xbb, 0xff,
319
+	}
320
+	actual = InvCipher(input, 192, key)
321
+	for i := 0; i < 16; i++ {
322
+		if actual[i] != expected[i] {
323
+			t.Errorf(
324
+				"failed to get Cipher:\n\texpected: %v \n\tactual:%v", expected, actual,
325
+			)
326
+		}
327
+	}
328
+
329
+	//Test case for 256 from FIPS
330
+	key = Block{
331
+		0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
332
+		0x01, 0x05, 0x09, 0x0d, 0x11, 0x15, 0x19, 0x1d,
333
+		0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16, 0x1a, 0x1e,
334
+		0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17, 0x1b, 0x1f,
335
+	}
336
+	input = Block{
337
+		0x8e, 0x51, 0xea, 0x4b,
338
+		0xa2, 0x67, 0xfc, 0x49,
339
+		0xb7, 0x45, 0x49, 0x60,
340
+		0xca, 0xbf, 0x90, 0x89,
341
+	}
342
+	expected = Block{
343
+		0x00, 0x44, 0x88, 0xcc,
344
+		0x11, 0x55, 0x99, 0xdd,
345
+		0x22, 0x66, 0xaa, 0xee,
346
+		0x33, 0x77, 0xbb, 0xff,
347
+	}
348
+	actual = InvCipher(input, 256, key)
349
+	for i := 0; i < 16; i++ {
350
+		if actual[i] != expected[i] {
351
+			t.Errorf(
352
+				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
353
+			)
354
+		}
355
+	}
356
+}

+ 98
- 0
cs465/main.go View File

@@ -0,0 +1,98 @@
1
+package main
2
+
3
+import (
4
+	"fmt"
5
+	"log"
6
+	"os"
7
+
8
+	"s.mcquay.me/dm/school/cs465/aes"
9
+
10
+	"github.com/spf13/cobra"
11
+)
12
+
13
+var key128 = aes.Block{
14
+	0x2b, 0x28, 0xab, 0x09,
15
+	0x7e, 0xae, 0xf7, 0xcf,
16
+	0x15, 0xd2, 0x15, 0x4f,
17
+	0x16, 0xa6, 0x88, 0x3c,
18
+}
19
+var key192 = aes.Block{
20
+	0x00, 0x04, 0x08, 0x0c, 0x10, 0x14,
21
+	0x01, 0x05, 0x09, 0x0d, 0x11, 0x15,
22
+	0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16,
23
+	0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17,
24
+}
25
+var key256 = aes.Block{
26
+	0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
27
+	0x01, 0x05, 0x09, 0x0d, 0x11, 0x15, 0x19, 0x1d,
28
+	0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16, 0x1a, 0x1e,
29
+	0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17, 0x1b, 0x1f,
30
+}
31
+
32
+func main() {
33
+	var keysize int
34
+	var decrypt bool
35
+	var ascii bool
36
+
37
+	var aes = &cobra.Command{
38
+		Use:   "aes",
39
+		Short: "aes cipher",
40
+		Long:  `perform aes cipher`,
41
+		Run: func(cmd *cobra.Command, args []string) {
42
+			if len(args) < 1 {
43
+				fmt.Println("missing input, see -h (--help) for more info")
44
+				os.Exit(1)
45
+			}
46
+			for _, arg := range args {
47
+				all := aes.BlockGen(arg)
48
+				if ascii {
49
+					switch keysize {
50
+					case 128:
51
+						fmt.Println(aes.ToString(all, 128, key128, decrypt))
52
+					case 192:
53
+						fmt.Println(aes.ToString(all, 192, key192, decrypt))
54
+					case 256:
55
+						fmt.Println(aes.ToString(all, 256, key256, decrypt))
56
+					default:
57
+						log.Fatal("keysize not valid")
58
+					}
59
+				} else {
60
+					switch keysize {
61
+					case 128:
62
+						fmt.Println(aes.ToHex(all, 128, key128, decrypt))
63
+					case 192:
64
+						fmt.Println(aes.ToHex(all, 192, key192, decrypt))
65
+					case 256:
66
+						fmt.Println(aes.ToHex(all, 256, key256, decrypt))
67
+					default:
68
+						log.Fatal("keysize not valid")
69
+					}
70
+				}
71
+			}
72
+		},
73
+	}
74
+	aes.Flags().IntVarP(
75
+		&keysize,
76
+		"keysize",
77
+		"k",
78
+		0,
79
+		"keysize",
80
+	)
81
+	aes.Flags().BoolVarP(
82
+		&ascii,
83
+		"ascii",
84
+		"a",
85
+		false,
86
+		"display in ascii (may mess up terminal :) )",
87
+	)
88
+	aes.Flags().BoolVarP(
89
+		&decrypt,
90
+		"decrypt",
91
+		"d",
92
+		false,
93
+		"decrypt",
94
+	)
95
+	var rootCmd = &cobra.Command{Use: "app"}
96
+	rootCmd.AddCommand(aes)
97
+	rootCmd.Execute()
98
+}

Loading…
Cancel
Save