src/eric7/Utilities/crypto/py3AES.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
30 30
31 31
32 def append_PKCS7_padding(b): 32 def append_PKCS7_padding(b):
33 """ 33 """
34 Function to pad the given data to a multiple of 16-bytes by PKCS7 padding. 34 Function to pad the given data to a multiple of 16-bytes by PKCS7 padding.
35 35
36 @param b data to be padded (bytes) 36 @param b data to be padded (bytes)
37 @return padded data (bytes) 37 @return padded data (bytes)
38 """ 38 """
39 numpads = 16 - (len(b) % 16) 39 numpads = 16 - (len(b) % 16)
40 return b + numpads * bytes(chr(numpads), encoding="ascii") 40 return b + numpads * bytes(chr(numpads), encoding="ascii")
41 41
42 42
43 def strip_PKCS7_padding(b): 43 def strip_PKCS7_padding(b):
44 """ 44 """
45 Function to strip off PKCS7 padding. 45 Function to strip off PKCS7 padding.
46 46
47 @param b data to be stripped (bytes) 47 @param b data to be stripped (bytes)
48 @return stripped data (bytes) 48 @return stripped data (bytes)
49 @exception ValueError data padding is invalid 49 @exception ValueError data padding is invalid
50 """ 50 """
51 if len(b) % 16 or not b: 51 if len(b) % 16 or not b:
52 raise ValueError( 52 raise ValueError("Data of len {0} can't be PCKS7-padded".format(len(b)))
53 "Data of len {0} can't be PCKS7-padded".format(len(b)))
54 numpads = b[-1] 53 numpads = b[-1]
55 if numpads > 16: 54 if numpads > 16:
56 raise ValueError( 55 raise ValueError("Data ending with {0} can't be PCKS7-padded".format(b[-1]))
57 "Data ending with {0} can't be PCKS7-padded".format(b[-1]))
58 return b[:-numpads] 56 return b[:-numpads]
59 57
60 58
61 class AES: 59 class AES:
62 """ 60 """
63 Class implementing the Advanced Encryption Standard algorithm. 61 Class implementing the Advanced Encryption Standard algorithm.
64 """ 62 """
63
65 # valid key sizes 64 # valid key sizes
66 KeySize = { 65 KeySize = {
67 "SIZE_128": 16, 66 "SIZE_128": 16,
68 "SIZE_192": 24, 67 "SIZE_192": 24,
69 "SIZE_256": 32, 68 "SIZE_256": 32,
70 } 69 }
71 70
72 # Rijndael S-box 71 # Rijndael S-box
73 sbox = [0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 72 sbox = [
74 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 73 0x63,
75 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 74 0x7C,
76 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 75 0x77,
77 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 76 0x7B,
78 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 77 0xF2,
79 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 78 0x6B,
80 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 79 0x6F,
81 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 80 0xC5,
82 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 81 0x30,
83 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 82 0x01,
84 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, 83 0x67,
85 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 84 0x2B,
86 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 85 0xFE,
87 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 86 0xD7,
88 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 87 0xAB,
89 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 88 0x76,
90 0xea, 0x65, 0x7a, 0xae, 0x08, 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 89 0xCA,
91 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 90 0x82,
92 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 91 0xC9,
93 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 92 0x7D,
94 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 0x8c, 0xa1, 93 0xFA,
95 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 94 0x59,
96 0x54, 0xbb, 0x16] 95 0x47,
96 0xF0,
97 0xAD,
98 0xD4,
99 0xA2,
100 0xAF,
101 0x9C,
102 0xA4,
103 0x72,
104 0xC0,
105 0xB7,
106 0xFD,
107 0x93,
108 0x26,
109 0x36,
110 0x3F,
111 0xF7,
112 0xCC,
113 0x34,
114 0xA5,
115 0xE5,
116 0xF1,
117 0x71,
118 0xD8,
119 0x31,
120 0x15,
121 0x04,
122 0xC7,
123 0x23,
124 0xC3,
125 0x18,
126 0x96,
127 0x05,
128 0x9A,
129 0x07,
130 0x12,
131 0x80,
132 0xE2,
133 0xEB,
134 0x27,
135 0xB2,
136 0x75,
137 0x09,
138 0x83,
139 0x2C,
140 0x1A,
141 0x1B,
142 0x6E,
143 0x5A,
144 0xA0,
145 0x52,
146 0x3B,
147 0xD6,
148 0xB3,
149 0x29,
150 0xE3,
151 0x2F,
152 0x84,
153 0x53,
154 0xD1,
155 0x00,
156 0xED,
157 0x20,
158 0xFC,
159 0xB1,
160 0x5B,
161 0x6A,
162 0xCB,
163 0xBE,
164 0x39,
165 0x4A,
166 0x4C,
167 0x58,
168 0xCF,
169 0xD0,
170 0xEF,
171 0xAA,
172 0xFB,
173 0x43,
174 0x4D,
175 0x33,
176 0x85,
177 0x45,
178 0xF9,
179 0x02,
180 0x7F,
181 0x50,
182 0x3C,
183 0x9F,
184 0xA8,
185 0x51,
186 0xA3,
187 0x40,
188 0x8F,
189 0x92,
190 0x9D,
191 0x38,
192 0xF5,
193 0xBC,
194 0xB6,
195 0xDA,
196 0x21,
197 0x10,
198 0xFF,
199 0xF3,
200 0xD2,
201 0xCD,
202 0x0C,
203 0x13,
204 0xEC,
205 0x5F,
206 0x97,
207 0x44,
208 0x17,
209 0xC4,
210 0xA7,
211 0x7E,
212 0x3D,
213 0x64,
214 0x5D,
215 0x19,
216 0x73,
217 0x60,
218 0x81,
219 0x4F,
220 0xDC,
221 0x22,
222 0x2A,
223 0x90,
224 0x88,
225 0x46,
226 0xEE,
227 0xB8,
228 0x14,
229 0xDE,
230 0x5E,
231 0x0B,
232 0xDB,
233 0xE0,
234 0x32,
235 0x3A,
236 0x0A,
237 0x49,
238 0x06,
239 0x24,
240 0x5C,
241 0xC2,
242 0xD3,
243 0xAC,
244 0x62,
245 0x91,
246 0x95,
247 0xE4,
248 0x79,
249 0xE7,
250 0xC8,
251 0x37,
252 0x6D,
253 0x8D,
254 0xD5,
255 0x4E,
256 0xA9,
257 0x6C,
258 0x56,
259 0xF4,
260 0xEA,
261 0x65,
262 0x7A,
263 0xAE,
264 0x08,
265 0xBA,
266 0x78,
267 0x25,
268 0x2E,
269 0x1C,
270 0xA6,
271 0xB4,
272 0xC6,
273 0xE8,
274 0xDD,
275 0x74,
276 0x1F,
277 0x4B,
278 0xBD,
279 0x8B,
280 0x8A,
281 0x70,
282 0x3E,
283 0xB5,
284 0x66,
285 0x48,
286 0x03,
287 0xF6,
288 0x0E,
289 0x61,
290 0x35,
291 0x57,
292 0xB9,
293 0x86,
294 0xC1,
295 0x1D,
296 0x9E,
297 0xE1,
298 0xF8,
299 0x98,
300 0x11,
301 0x69,
302 0xD9,
303 0x8E,
304 0x94,
305 0x9B,
306 0x1E,
307 0x87,
308 0xE9,
309 0xCE,
310 0x55,
311 0x28,
312 0xDF,
313 0x8C,
314 0xA1,
315 0x89,
316 0x0D,
317 0xBF,
318 0xE6,
319 0x42,
320 0x68,
321 0x41,
322 0x99,
323 0x2D,
324 0x0F,
325 0xB0,
326 0x54,
327 0xBB,
328 0x16,
329 ]
97 330
98 # Rijndael Inverted S-box 331 # Rijndael Inverted S-box
99 rsbox = [0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 332 rsbox = [
100 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 333 0x52,
101 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 334 0x09,
102 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 335 0x6A,
103 0x42, 0xfa, 0xc3, 0x4e, 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 336 0xD5,
104 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 337 0x30,
105 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 338 0x36,
106 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 339 0xA5,
107 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, 0x90, 0xd8, 0xab, 340 0x38,
108 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 341 0xBF,
109 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 342 0x40,
110 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41, 343 0xA3,
111 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 344 0x9E,
112 0x73, 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 345 0x81,
113 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 346 0xF3,
114 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 347 0xD7,
115 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 348 0xFB,
116 0xfe, 0x78, 0xcd, 0x5a, 0xf4, 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 349 0x7C,
117 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, 0x60, 350 0xE3,
118 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 351 0x39,
119 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 352 0x82,
120 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2b, 353 0x9B,
121 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 354 0x2F,
122 0x21, 0x0c, 0x7d] 355 0xFF,
356 0x87,
357 0x34,
358 0x8E,
359 0x43,
360 0x44,
361 0xC4,
362 0xDE,
363 0xE9,
364 0xCB,
365 0x54,
366 0x7B,
367 0x94,
368 0x32,
369 0xA6,
370 0xC2,
371 0x23,
372 0x3D,
373 0xEE,
374 0x4C,
375 0x95,
376 0x0B,
377 0x42,
378 0xFA,
379 0xC3,
380 0x4E,
381 0x08,
382 0x2E,
383 0xA1,
384 0x66,
385 0x28,
386 0xD9,
387 0x24,
388 0xB2,
389 0x76,
390 0x5B,
391 0xA2,
392 0x49,
393 0x6D,
394 0x8B,
395 0xD1,
396 0x25,
397 0x72,
398 0xF8,
399 0xF6,
400 0x64,
401 0x86,
402 0x68,
403 0x98,
404 0x16,
405 0xD4,
406 0xA4,
407 0x5C,
408 0xCC,
409 0x5D,
410 0x65,
411 0xB6,
412 0x92,
413 0x6C,
414 0x70,
415 0x48,
416 0x50,
417 0xFD,
418 0xED,
419 0xB9,
420 0xDA,
421 0x5E,
422 0x15,
423 0x46,
424 0x57,
425 0xA7,
426 0x8D,
427 0x9D,
428 0x84,
429 0x90,
430 0xD8,
431 0xAB,
432 0x00,
433 0x8C,
434 0xBC,
435 0xD3,
436 0x0A,
437 0xF7,
438 0xE4,
439 0x58,
440 0x05,
441 0xB8,
442 0xB3,
443 0x45,
444 0x06,
445 0xD0,
446 0x2C,
447 0x1E,
448 0x8F,
449 0xCA,
450 0x3F,
451 0x0F,
452 0x02,
453 0xC1,
454 0xAF,
455 0xBD,
456 0x03,
457 0x01,
458 0x13,
459 0x8A,
460 0x6B,
461 0x3A,
462 0x91,
463 0x11,
464 0x41,
465 0x4F,
466 0x67,
467 0xDC,
468 0xEA,
469 0x97,
470 0xF2,
471 0xCF,
472 0xCE,
473 0xF0,
474 0xB4,
475 0xE6,
476 0x73,
477 0x96,
478 0xAC,
479 0x74,
480 0x22,
481 0xE7,
482 0xAD,
483 0x35,
484 0x85,
485 0xE2,
486 0xF9,
487 0x37,
488 0xE8,
489 0x1C,
490 0x75,
491 0xDF,
492 0x6E,
493 0x47,
494 0xF1,
495 0x1A,
496 0x71,
497 0x1D,
498 0x29,
499 0xC5,
500 0x89,
501 0x6F,
502 0xB7,
503 0x62,
504 0x0E,
505 0xAA,
506 0x18,
507 0xBE,
508 0x1B,
509 0xFC,
510 0x56,
511 0x3E,
512 0x4B,
513 0xC6,
514 0xD2,
515 0x79,
516 0x20,
517 0x9A,
518 0xDB,
519 0xC0,
520 0xFE,
521 0x78,
522 0xCD,
523 0x5A,
524 0xF4,
525 0x1F,
526 0xDD,
527 0xA8,
528 0x33,
529 0x88,
530 0x07,
531 0xC7,
532 0x31,
533 0xB1,
534 0x12,
535 0x10,
536 0x59,
537 0x27,
538 0x80,
539 0xEC,
540 0x5F,
541 0x60,
542 0x51,
543 0x7F,
544 0xA9,
545 0x19,
546 0xB5,
547 0x4A,
548 0x0D,
549 0x2D,
550 0xE5,
551 0x7A,
552 0x9F,
553 0x93,
554 0xC9,
555 0x9C,
556 0xEF,
557 0xA0,
558 0xE0,
559 0x3B,
560 0x4D,
561 0xAE,
562 0x2A,
563 0xF5,
564 0xB0,
565 0xC8,
566 0xEB,
567 0xBB,
568 0x3C,
569 0x83,
570 0x53,
571 0x99,
572 0x61,
573 0x17,
574 0x2B,
575 0x04,
576 0x7E,
577 0xBA,
578 0x77,
579 0xD6,
580 0x26,
581 0xE1,
582 0x69,
583 0x14,
584 0x63,
585 0x55,
586 0x21,
587 0x0C,
588 0x7D,
589 ]
123 590
124 # Rijndael Rcon 591 # Rijndael Rcon
125 Rcon = [0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 592 Rcon = [
126 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 593 0x8D,
127 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 594 0x01,
128 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 595 0x02,
129 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 596 0x04,
130 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 597 0x08,
131 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 598 0x10,
132 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 599 0x20,
133 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 600 0x40,
134 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 601 0x80,
135 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 602 0x1B,
136 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 603 0x36,
137 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 604 0x6C,
138 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 605 0xD8,
139 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 606 0xAB,
140 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 607 0x4D,
141 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 608 0x9A,
142 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 609 0x2F,
143 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 610 0x5E,
144 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 611 0xBC,
145 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 612 0x63,
146 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 613 0xC6,
147 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 614 0x97,
148 0xe8, 0xcb] 615 0x35,
616 0x6A,
617 0xD4,
618 0xB3,
619 0x7D,
620 0xFA,
621 0xEF,
622 0xC5,
623 0x91,
624 0x39,
625 0x72,
626 0xE4,
627 0xD3,
628 0xBD,
629 0x61,
630 0xC2,
631 0x9F,
632 0x25,
633 0x4A,
634 0x94,
635 0x33,
636 0x66,
637 0xCC,
638 0x83,
639 0x1D,
640 0x3A,
641 0x74,
642 0xE8,
643 0xCB,
644 0x8D,
645 0x01,
646 0x02,
647 0x04,
648 0x08,
649 0x10,
650 0x20,
651 0x40,
652 0x80,
653 0x1B,
654 0x36,
655 0x6C,
656 0xD8,
657 0xAB,
658 0x4D,
659 0x9A,
660 0x2F,
661 0x5E,
662 0xBC,
663 0x63,
664 0xC6,
665 0x97,
666 0x35,
667 0x6A,
668 0xD4,
669 0xB3,
670 0x7D,
671 0xFA,
672 0xEF,
673 0xC5,
674 0x91,
675 0x39,
676 0x72,
677 0xE4,
678 0xD3,
679 0xBD,
680 0x61,
681 0xC2,
682 0x9F,
683 0x25,
684 0x4A,
685 0x94,
686 0x33,
687 0x66,
688 0xCC,
689 0x83,
690 0x1D,
691 0x3A,
692 0x74,
693 0xE8,
694 0xCB,
695 0x8D,
696 0x01,
697 0x02,
698 0x04,
699 0x08,
700 0x10,
701 0x20,
702 0x40,
703 0x80,
704 0x1B,
705 0x36,
706 0x6C,
707 0xD8,
708 0xAB,
709 0x4D,
710 0x9A,
711 0x2F,
712 0x5E,
713 0xBC,
714 0x63,
715 0xC6,
716 0x97,
717 0x35,
718 0x6A,
719 0xD4,
720 0xB3,
721 0x7D,
722 0xFA,
723 0xEF,
724 0xC5,
725 0x91,
726 0x39,
727 0x72,
728 0xE4,
729 0xD3,
730 0xBD,
731 0x61,
732 0xC2,
733 0x9F,
734 0x25,
735 0x4A,
736 0x94,
737 0x33,
738 0x66,
739 0xCC,
740 0x83,
741 0x1D,
742 0x3A,
743 0x74,
744 0xE8,
745 0xCB,
746 0x8D,
747 0x01,
748 0x02,
749 0x04,
750 0x08,
751 0x10,
752 0x20,
753 0x40,
754 0x80,
755 0x1B,
756 0x36,
757 0x6C,
758 0xD8,
759 0xAB,
760 0x4D,
761 0x9A,
762 0x2F,
763 0x5E,
764 0xBC,
765 0x63,
766 0xC6,
767 0x97,
768 0x35,
769 0x6A,
770 0xD4,
771 0xB3,
772 0x7D,
773 0xFA,
774 0xEF,
775 0xC5,
776 0x91,
777 0x39,
778 0x72,
779 0xE4,
780 0xD3,
781 0xBD,
782 0x61,
783 0xC2,
784 0x9F,
785 0x25,
786 0x4A,
787 0x94,
788 0x33,
789 0x66,
790 0xCC,
791 0x83,
792 0x1D,
793 0x3A,
794 0x74,
795 0xE8,
796 0xCB,
797 0x8D,
798 0x01,
799 0x02,
800 0x04,
801 0x08,
802 0x10,
803 0x20,
804 0x40,
805 0x80,
806 0x1B,
807 0x36,
808 0x6C,
809 0xD8,
810 0xAB,
811 0x4D,
812 0x9A,
813 0x2F,
814 0x5E,
815 0xBC,
816 0x63,
817 0xC6,
818 0x97,
819 0x35,
820 0x6A,
821 0xD4,
822 0xB3,
823 0x7D,
824 0xFA,
825 0xEF,
826 0xC5,
827 0x91,
828 0x39,
829 0x72,
830 0xE4,
831 0xD3,
832 0xBD,
833 0x61,
834 0xC2,
835 0x9F,
836 0x25,
837 0x4A,
838 0x94,
839 0x33,
840 0x66,
841 0xCC,
842 0x83,
843 0x1D,
844 0x3A,
845 0x74,
846 0xE8,
847 0xCB,
848 ]
149 849
150 def __getSBoxValue(self, num): 850 def __getSBoxValue(self, num):
151 """ 851 """
152 Private method to retrieve a given S-Box value. 852 Private method to retrieve a given S-Box value.
153 853
154 @param num position of the value (integer) 854 @param num position of the value (integer)
155 @return value of the S-Box (integer) 855 @return value of the S-Box (integer)
156 """ 856 """
157 return self.sbox[num] 857 return self.sbox[num]
158 858
159 def __getSBoxInvert(self, num): 859 def __getSBoxInvert(self, num):
160 """ 860 """
161 Private method to retrieve a given Inverted S-Box value. 861 Private method to retrieve a given Inverted S-Box value.
162 862
163 @param num position of the value (integer) 863 @param num position of the value (integer)
164 @return value of the Inverted S-Box (integer) 864 @return value of the Inverted S-Box (integer)
165 """ 865 """
166 return self.rsbox[num] 866 return self.rsbox[num]
167 867
169 """ 869 """
170 Private method performing Rijndael's key schedule rotate operation. 870 Private method performing Rijndael's key schedule rotate operation.
171 871
172 Rotate the data word eight bits to the left: eg, 872 Rotate the data word eight bits to the left: eg,
173 rotate(1d2c3a4f) == 2c3a4f1d. 873 rotate(1d2c3a4f) == 2c3a4f1d.
174 874
175 @param data data of size 4 (bytearray) 875 @param data data of size 4 (bytearray)
176 @return rotated data (bytearray) 876 @return rotated data (bytearray)
177 """ 877 """
178 return data[1:] + data[:1] 878 return data[1:] + data[:1]
179 879
180 def __getRconValue(self, num): 880 def __getRconValue(self, num):
181 """ 881 """
182 Private method to retrieve a given Rcon value. 882 Private method to retrieve a given Rcon value.
183 883
184 @param num position of the value (integer) 884 @param num position of the value (integer)
185 @return Rcon value (integer) 885 @return Rcon value (integer)
186 """ 886 """
187 return self.Rcon[num] 887 return self.Rcon[num]
188 888
189 def __core(self, data, iteration): 889 def __core(self, data, iteration):
190 """ 890 """
191 Private method performing the key schedule core operation. 891 Private method performing the key schedule core operation.
192 892
193 @param data data to operate on (bytearray) 893 @param data data to operate on (bytearray)
194 @param iteration iteration counter (integer) 894 @param iteration iteration counter (integer)
195 @return modified data (bytearray) 895 @return modified data (bytearray)
196 """ 896 """
197 # rotate the 32-bit word 8 bits to the left 897 # rotate the 32-bit word 8 bits to the left
207 def __expandKey(self, key, size, expandedKeySize): 907 def __expandKey(self, key, size, expandedKeySize):
208 """ 908 """
209 Private method performing Rijndael's key expansion. 909 Private method performing Rijndael's key expansion.
210 910
211 Expands a 128, 192 or 256 bit key into a 176, 208 or 240 bit key. 911 Expands a 128, 192 or 256 bit key into a 176, 208 or 240 bit key.
212 912
213 @param key key to be expanded (bytes or bytearray) 913 @param key key to be expanded (bytes or bytearray)
214 @param size size of the key in bytes (16, 24 or 32) 914 @param size size of the key in bytes (16, 24 or 32)
215 @param expandedKeySize size of the expanded key (integer) 915 @param expandedKeySize size of the expanded key (integer)
216 @return expanded key (bytearray) 916 @return expanded key (bytearray)
217 """ 917 """
225 expandedKey[j] = key[j] 925 expandedKey[j] = key[j]
226 currentSize += size 926 currentSize += size
227 927
228 while currentSize < expandedKeySize: 928 while currentSize < expandedKeySize:
229 # assign the previous 4 bytes to the temporary value t 929 # assign the previous 4 bytes to the temporary value t
230 t = expandedKey[currentSize - 4:currentSize] 930 t = expandedKey[currentSize - 4 : currentSize]
231 931
232 # every 16, 24, 32 bytes we apply the core schedule to t 932 # every 16, 24, 32 bytes we apply the core schedule to t
233 # and increment rconIteration afterwards 933 # and increment rconIteration afterwards
234 if currentSize % size == 0: 934 if currentSize % size == 0:
235 t = self.__core(t, rconIteration) 935 t = self.__core(t, rconIteration)
236 rconIteration += 1 936 rconIteration += 1
237 # For 256-bit keys, we add an extra sbox to the calculation 937 # For 256-bit keys, we add an extra sbox to the calculation
238 if ( 938 if size == self.KeySize["SIZE_256"] and ((currentSize % size) == 16):
239 size == self.KeySize["SIZE_256"] and
240 ((currentSize % size) == 16)
241 ):
242 for ll in range(4): 939 for ll in range(4):
243 t[ll] = self.__getSBoxValue(t[ll]) 940 t[ll] = self.__getSBoxValue(t[ll])
244 941
245 # We XOR t with the four-byte block 16, 24, 32 bytes before the new 942 # We XOR t with the four-byte block 16, 24, 32 bytes before the new
246 # expanded key. This becomes the next four bytes in the expanded 943 # expanded key. This becomes the next four bytes in the expanded
247 # key. 944 # key.
248 for m in range(4): 945 for m in range(4):
249 expandedKey[currentSize] = ( 946 expandedKey[currentSize] = expandedKey[currentSize - size] ^ t[m]
250 expandedKey[currentSize - size] ^ t[m] 947 currentSize += 1 # __IGNORE_WARNING_Y113__
251 )
252 currentSize += 1 # __IGNORE_WARNING_Y113__
253 948
254 return expandedKey 949 return expandedKey
255 950
256 def __addRoundKey(self, state, roundKey): 951 def __addRoundKey(self, state, roundKey):
257 """ 952 """
258 Private method to add (XORs) the round key to the state. 953 Private method to add (XORs) the round key to the state.
259 954
260 @param state state to be changed (bytearray) 955 @param state state to be changed (bytearray)
261 @param roundKey key to be used for the modification (bytearray) 956 @param roundKey key to be used for the modification (bytearray)
262 @return modified state (bytearray) 957 @return modified state (bytearray)
263 """ 958 """
264 buf = state[:] 959 buf = state[:]
267 return buf 962 return buf
268 963
269 def __createRoundKey(self, expandedKey, roundKeyPointer): 964 def __createRoundKey(self, expandedKey, roundKeyPointer):
270 """ 965 """
271 Private method to create a round key. 966 Private method to create a round key.
272 967
273 @param expandedKey expanded key to be used (bytearray) 968 @param expandedKey expanded key to be used (bytearray)
274 @param roundKeyPointer position within the expanded key (integer) 969 @param roundKeyPointer position within the expanded key (integer)
275 @return round key (bytearray) 970 @return round key (bytearray)
276 """ 971 """
277 roundKey = bytearray(16) 972 roundKey = bytearray(16)
282 977
283 def __galois_multiplication(self, a, b): 978 def __galois_multiplication(self, a, b):
284 """ 979 """
285 Private method to perform a Galois multiplication of 8 bit characters 980 Private method to perform a Galois multiplication of 8 bit characters
286 a and b. 981 a and b.
287 982
288 @param a first factor (byte) 983 @param a first factor (byte)
289 @param b second factor (byte) 984 @param b second factor (byte)
290 @return result (byte) 985 @return result (byte)
291 """ 986 """
292 p = 0 987 p = 0
296 hi_bit_set = a & 0x80 991 hi_bit_set = a & 0x80
297 a <<= 1 992 a <<= 1
298 # keep a 8 bit 993 # keep a 8 bit
299 a &= 0xFF 994 a &= 0xFF
300 if hi_bit_set: 995 if hi_bit_set:
301 a ^= 0x1b 996 a ^= 0x1B
302 b >>= 1 997 b >>= 1
303 return p 998 return p
304 999
305 def __subBytes(self, state, isInv): 1000 def __subBytes(self, state, isInv):
306 """ 1001 """
307 Private method to substitute all the values from the state with the 1002 Private method to substitute all the values from the state with the
308 value in the SBox using the state value as index for the SBox. 1003 value in the SBox using the state value as index for the SBox.
309 1004
310 @param state state to be worked on (bytearray) 1005 @param state state to be worked on (bytearray)
311 @param isInv flag indicating an inverse operation (boolean) 1006 @param isInv flag indicating an inverse operation (boolean)
312 @return modified state (bytearray) 1007 @return modified state (bytearray)
313 """ 1008 """
314 state = state[:] 1009 state = state[:]
319 1014
320 def __shiftRows(self, state, isInv): 1015 def __shiftRows(self, state, isInv):
321 """ 1016 """
322 Private method to iterate over the 4 rows and call __shiftRow() with 1017 Private method to iterate over the 4 rows and call __shiftRow() with
323 that row. 1018 that row.
324 1019
325 @param state state to be worked on (bytearray) 1020 @param state state to be worked on (bytearray)
326 @param isInv flag indicating an inverse operation (boolean) 1021 @param isInv flag indicating an inverse operation (boolean)
327 @return modified state (bytearray) 1022 @return modified state (bytearray)
328 """ 1023 """
329 state = state[:] 1024 state = state[:]
332 return state 1027 return state
333 1028
334 def __shiftRow(self, state, statePointer, nbr, isInv): 1029 def __shiftRow(self, state, statePointer, nbr, isInv):
335 """ 1030 """
336 Private method to shift the bytes of a row to the left. 1031 Private method to shift the bytes of a row to the left.
337 1032
338 @param state state to be worked on (bytearray) 1033 @param state state to be worked on (bytearray)
339 @param statePointer index into the state (integer) 1034 @param statePointer index into the state (integer)
340 @param nbr number of positions to shift (integer) 1035 @param nbr number of positions to shift (integer)
341 @param isInv flag indicating an inverse operation (boolean) 1036 @param isInv flag indicating an inverse operation (boolean)
342 @return modified state (bytearray) 1037 @return modified state (bytearray)
343 """ 1038 """
344 state = state[:] 1039 state = state[:]
345 for _ in range(nbr): 1040 for _ in range(nbr):
346 if isInv: 1041 if isInv:
347 state[statePointer:statePointer + 4] = ( 1042 state[statePointer : statePointer + 4] = (
348 state[statePointer + 3:statePointer + 4] + 1043 state[statePointer + 3 : statePointer + 4]
349 state[statePointer:statePointer + 3] 1044 + state[statePointer : statePointer + 3]
350 ) 1045 )
351 else: 1046 else:
352 state[statePointer:statePointer + 4] = ( 1047 state[statePointer : statePointer + 4] = (
353 state[statePointer + 1:statePointer + 4] + 1048 state[statePointer + 1 : statePointer + 4]
354 state[statePointer:statePointer + 1] 1049 + state[statePointer : statePointer + 1]
355 ) 1050 )
356 return state 1051 return state
357 1052
358 def __mixColumns(self, state, isInv): 1053 def __mixColumns(self, state, isInv):
359 """ 1054 """
360 Private method to perform a galois multiplication of the 4x4 matrix. 1055 Private method to perform a galois multiplication of the 4x4 matrix.
361 1056
362 @param state state to be worked on (bytearray) 1057 @param state state to be worked on (bytearray)
363 @param isInv flag indicating an inverse operation (boolean) 1058 @param isInv flag indicating an inverse operation (boolean)
364 @return modified state (bytearray) 1059 @return modified state (bytearray)
365 """ 1060 """
366 state = state[:] 1061 state = state[:]
367 # iterate over the 4 columns 1062 # iterate over the 4 columns
368 for i in range(4): 1063 for i in range(4):
369 # construct one column by slicing over the 4 rows 1064 # construct one column by slicing over the 4 rows
370 column = state[i:i + 16:4] 1065 column = state[i : i + 16 : 4]
371 # apply the __mixColumn on one column 1066 # apply the __mixColumn on one column
372 column = self.__mixColumn(column, isInv) 1067 column = self.__mixColumn(column, isInv)
373 # put the values back into the state 1068 # put the values back into the state
374 state[i:i + 16:4] = column 1069 state[i : i + 16 : 4] = column
375 1070
376 return state 1071 return state
377 1072
378 # galois multiplication of 1 column of the 4x4 matrix 1073 # galois multiplication of 1 column of the 4x4 matrix
379 def __mixColumn(self, column, isInv): 1074 def __mixColumn(self, column, isInv):
380 """ 1075 """
381 Private method to perform a galois multiplication of 1 column the 1076 Private method to perform a galois multiplication of 1 column the
382 4x4 matrix. 1077 4x4 matrix.
383 1078
384 @param column column to be worked on (bytearray) 1079 @param column column to be worked on (bytearray)
385 @param isInv flag indicating an inverse operation (boolean) 1080 @param isInv flag indicating an inverse operation (boolean)
386 @return modified column (bytearray) 1081 @return modified column (bytearray)
387 """ 1082 """
388 column = column[:] 1083 column = column[:]
389 mult = [14, 9, 13, 11] if isInv else [2, 1, 1, 3] 1084 mult = [14, 9, 13, 11] if isInv else [2, 1, 1, 3]
390 cpy = column[:] 1085 cpy = column[:]
391 g = self.__galois_multiplication 1086 g = self.__galois_multiplication
392 1087
393 column[0] = ( 1088 column[0] = (
394 g(cpy[0], mult[0]) ^ g(cpy[3], mult[1]) ^ 1089 g(cpy[0], mult[0])
395 g(cpy[2], mult[2]) ^ g(cpy[1], mult[3]) 1090 ^ g(cpy[3], mult[1])
1091 ^ g(cpy[2], mult[2])
1092 ^ g(cpy[1], mult[3])
396 ) 1093 )
397 column[1] = ( 1094 column[1] = (
398 g(cpy[1], mult[0]) ^ g(cpy[0], mult[1]) ^ 1095 g(cpy[1], mult[0])
399 g(cpy[3], mult[2]) ^ g(cpy[2], mult[3]) 1096 ^ g(cpy[0], mult[1])
1097 ^ g(cpy[3], mult[2])
1098 ^ g(cpy[2], mult[3])
400 ) 1099 )
401 column[2] = ( 1100 column[2] = (
402 g(cpy[2], mult[0]) ^ g(cpy[1], mult[1]) ^ 1101 g(cpy[2], mult[0])
403 g(cpy[0], mult[2]) ^ g(cpy[3], mult[3]) 1102 ^ g(cpy[1], mult[1])
1103 ^ g(cpy[0], mult[2])
1104 ^ g(cpy[3], mult[3])
404 ) 1105 )
405 column[3] = ( 1106 column[3] = (
406 g(cpy[3], mult[0]) ^ g(cpy[2], mult[1]) ^ 1107 g(cpy[3], mult[0])
407 g(cpy[1], mult[2]) ^ g(cpy[0], mult[3]) 1108 ^ g(cpy[2], mult[1])
1109 ^ g(cpy[1], mult[2])
1110 ^ g(cpy[0], mult[3])
408 ) 1111 )
409 return column 1112 return column
410 1113
411 def __aes_round(self, state, roundKey): 1114 def __aes_round(self, state, roundKey):
412 """ 1115 """
413 Private method to apply the 4 operations of the forward round in 1116 Private method to apply the 4 operations of the forward round in
414 sequence. 1117 sequence.
415 1118
416 @param state state to be worked on (bytearray) 1119 @param state state to be worked on (bytearray)
417 @param roundKey round key to be used (bytearray) 1120 @param roundKey round key to be used (bytearray)
418 @return modified state (bytearray) 1121 @return modified state (bytearray)
419 """ 1122 """
420 state = self.__subBytes(state, False) 1123 state = self.__subBytes(state, False)
425 1128
426 def __aes_invRound(self, state, roundKey): 1129 def __aes_invRound(self, state, roundKey):
427 """ 1130 """
428 Private method to apply the 4 operations of the inverse round in 1131 Private method to apply the 4 operations of the inverse round in
429 sequence. 1132 sequence.
430 1133
431 @param state state to be worked on (bytearray) 1134 @param state state to be worked on (bytearray)
432 @param roundKey round key to be used (bytearray) 1135 @param roundKey round key to be used (bytearray)
433 @return modified state (bytearray) 1136 @return modified state (bytearray)
434 """ 1137 """
435 state = self.__shiftRows(state, True) 1138 state = self.__shiftRows(state, True)
439 return state 1142 return state
440 1143
441 def __aes_main(self, state, expandedKey, nbrRounds): 1144 def __aes_main(self, state, expandedKey, nbrRounds):
442 """ 1145 """
443 Private method to do the AES encryption for one round. 1146 Private method to do the AES encryption for one round.
444 1147
445 Perform the initial operations, the standard round, and the 1148 Perform the initial operations, the standard round, and the
446 final operations of the forward AES, creating a round key for 1149 final operations of the forward AES, creating a round key for
447 each round. 1150 each round.
448 1151
449 @param state state to be worked on (bytearray) 1152 @param state state to be worked on (bytearray)
450 @param expandedKey expanded key to be used (bytearray) 1153 @param expandedKey expanded key to be used (bytearray)
451 @param nbrRounds number of rounds to be done (integer) 1154 @param nbrRounds number of rounds to be done (integer)
452 @return modified state (bytearray) 1155 @return modified state (bytearray)
453 """ 1156 """
454 state = self.__addRoundKey( 1157 state = self.__addRoundKey(state, self.__createRoundKey(expandedKey, 0))
455 state, self.__createRoundKey(expandedKey, 0))
456 i = 1 1158 i = 1
457 while i < nbrRounds: 1159 while i < nbrRounds:
458 state = self.__aes_round( 1160 state = self.__aes_round(state, self.__createRoundKey(expandedKey, 16 * i))
459 state, self.__createRoundKey(expandedKey, 16 * i))
460 i += 1 1161 i += 1
461 state = self.__subBytes(state, False) 1162 state = self.__subBytes(state, False)
462 state = self.__shiftRows(state, False) 1163 state = self.__shiftRows(state, False)
463 state = self.__addRoundKey( 1164 state = self.__addRoundKey(
464 state, self.__createRoundKey(expandedKey, 16 * nbrRounds)) 1165 state, self.__createRoundKey(expandedKey, 16 * nbrRounds)
1166 )
465 return state 1167 return state
466 1168
467 def __aes_invMain(self, state, expandedKey, nbrRounds): 1169 def __aes_invMain(self, state, expandedKey, nbrRounds):
468 """ 1170 """
469 Private method to do the inverse AES encryption for one round. 1171 Private method to do the inverse AES encryption for one round.
470 1172
471 Perform the initial operations, the standard round, and the 1173 Perform the initial operations, the standard round, and the
472 final operations of the inverse AES, creating a round key for 1174 final operations of the inverse AES, creating a round key for
473 each round. 1175 each round.
474 1176
475 @param state state to be worked on (bytearray) 1177 @param state state to be worked on (bytearray)
476 @param expandedKey expanded key to be used (bytearray) 1178 @param expandedKey expanded key to be used (bytearray)
477 @param nbrRounds number of rounds to be done (integer) 1179 @param nbrRounds number of rounds to be done (integer)
478 @return modified state (bytearray) 1180 @return modified state (bytearray)
479 """ 1181 """
480 state = self.__addRoundKey( 1182 state = self.__addRoundKey(
481 state, self.__createRoundKey(expandedKey, 16 * nbrRounds)) 1183 state, self.__createRoundKey(expandedKey, 16 * nbrRounds)
1184 )
482 i = nbrRounds - 1 1185 i = nbrRounds - 1
483 while i > 0: 1186 while i > 0:
484 state = self.__aes_invRound( 1187 state = self.__aes_invRound(
485 state, self.__createRoundKey(expandedKey, 16 * i)) 1188 state, self.__createRoundKey(expandedKey, 16 * i)
1189 )
486 i -= 1 1190 i -= 1
487 state = self.__shiftRows(state, True) 1191 state = self.__shiftRows(state, True)
488 state = self.__subBytes(state, True) 1192 state = self.__subBytes(state, True)
489 state = self.__addRoundKey( 1193 state = self.__addRoundKey(state, self.__createRoundKey(expandedKey, 0))
490 state, self.__createRoundKey(expandedKey, 0))
491 return state 1194 return state
492 1195
493 def encrypt(self, iput, key, size): 1196 def encrypt(self, iput, key, size):
494 """ 1197 """
495 Public method to encrypt a 128 bit input block against the given key 1198 Public method to encrypt a 128 bit input block against the given key
496 of size specified. 1199 of size specified.
497 1200
498 @param iput input data (bytearray) 1201 @param iput input data (bytearray)
499 @param key key to be used (bytes or bytearray) 1202 @param key key to be used (bytes or bytearray)
500 @param size key size (16, 24 or 32) 1203 @param size key size (16, 24 or 32)
501 @return encrypted data (bytes) 1204 @return encrypted data (bytes)
502 @exception ValueError key size is invalid 1205 @exception ValueError key size is invalid
503 """ 1206 """
504 if size not in self.KeySize.values(): 1207 if size not in self.KeySize.values():
505 raise ValueError("Wrong key size given ({0}).".format(size)) 1208 raise ValueError("Wrong key size given ({0}).".format(size))
506 1209
507 output = bytearray(16) 1210 output = bytearray(16)
508 # the number of rounds 1211 # the number of rounds
509 nbrRounds = 0 1212 nbrRounds = 0
510 # the 128 bit block to encode 1213 # the 128 bit block to encode
511 block = bytearray(16) 1214 block = bytearray(16)
550 # decrypts a 128 bit input block against the given key of size specified 1253 # decrypts a 128 bit input block against the given key of size specified
551 def decrypt(self, iput, key, size): 1254 def decrypt(self, iput, key, size):
552 """ 1255 """
553 Public method to decrypt a 128 bit input block against the given key 1256 Public method to decrypt a 128 bit input block against the given key
554 of size specified. 1257 of size specified.
555 1258
556 @param iput input data (bytearray) 1259 @param iput input data (bytearray)
557 @param key key to be used (bytes or bytearray) 1260 @param key key to be used (bytes or bytearray)
558 @param size key size (16, 24 or 32) 1261 @param size key size (16, 24 or 32)
559 @return decrypted data (bytes) 1262 @return decrypted data (bytes)
560 @exception ValueError key size is invalid 1263 @exception ValueError key size is invalid
561 """ 1264 """
562 if size not in self.KeySize.values(): 1265 if size not in self.KeySize.values():
563 raise ValueError("Wrong key size given ({0}).".format(size)) 1266 raise ValueError("Wrong key size given ({0}).".format(size))
564 1267
565 output = bytearray(16) 1268 output = bytearray(16)
566 # the number of rounds 1269 # the number of rounds
567 nbrRounds = 0 1270 nbrRounds = 0
568 # the 128 bit block to decode 1271 # the 128 bit block to decode
569 block = bytearray(16) 1272 block = bytearray(16)
570 # set the number of rounds 1273 # set the number of rounds
571 1274
572 if size == self.KeySize["SIZE_128"]: 1275 if size == self.KeySize["SIZE_128"]:
573 nbrRounds = 10 1276 nbrRounds = 10
574 elif size == self.KeySize["SIZE_192"]: 1277 elif size == self.KeySize["SIZE_192"]:
575 nbrRounds = 12 1278 nbrRounds = 12
576 else: 1279 else:
605 1308
606 class AESModeOfOperation: 1309 class AESModeOfOperation:
607 """ 1310 """
608 Class implementing the different AES mode of operations. 1311 Class implementing the different AES mode of operations.
609 """ 1312 """
1313
610 aes = AES() 1314 aes = AES()
611 1315
612 # structure of supported modes of operation 1316 # structure of supported modes of operation
613 ModeOfOperation = { 1317 ModeOfOperation = {
614 "OFB": 0, 1318 "OFB": 0,
617 } 1321 }
618 1322
619 def __extractBytes(self, inputData, start, end, mode): 1323 def __extractBytes(self, inputData, start, end, mode):
620 """ 1324 """
621 Private method to extract a range of bytes from the input. 1325 Private method to extract a range of bytes from the input.
622 1326
623 @param inputData input data (bytes) 1327 @param inputData input data (bytes)
624 @param start start index (integer) 1328 @param start start index (integer)
625 @param end end index (integer) 1329 @param end end index (integer)
626 @param mode mode of operation (0, 1, 2) 1330 @param mode mode of operation (0, 1, 2)
627 @return extracted bytes (bytearray) 1331 @return extracted bytes (bytearray)
628 """ 1332 """
629 if end - start > 16: 1333 if end - start > 16:
630 end = start + 16 1334 end = start + 16
631 ar = (bytearray(16) if mode == self.ModeOfOperation["CBC"] 1335 ar = bytearray(16) if mode == self.ModeOfOperation["CBC"] else bytearray()
632 else bytearray())
633 1336
634 i = start 1337 i = start
635 j = 0 1338 j = 0
636 while len(ar) < end - start: 1339 while len(ar) < end - start:
637 ar.append(0) 1340 ar.append(0)
642 return ar 1345 return ar
643 1346
644 def encrypt(self, inputData, mode, key, size, IV): 1347 def encrypt(self, inputData, mode, key, size, IV):
645 """ 1348 """
646 Public method to perform the encryption operation. 1349 Public method to perform the encryption operation.
647 1350
648 @param inputData data to be encrypted (bytes) 1351 @param inputData data to be encrypted (bytes)
649 @param mode mode of operation (0, 1 or 2) 1352 @param mode mode of operation (0, 1 or 2)
650 @param key key to be used (bytes) 1353 @param key key to be used (bytes)
651 @param size length of the key (16, 24 or 32) 1354 @param size length of the key (16, 24 or 32)
652 @param IV initialisation vector (bytearray) 1355 @param IV initialisation vector (bytearray)
653 @return tuple with mode of operation, length of the input data and 1356 @return tuple with mode of operation, length of the input data and
654 the encrypted data (integer, integer, bytes) 1357 the encrypted data (integer, integer, bytes)
655 @exception ValueError key size is invalid or decrypted data is invalid 1358 @exception ValueError key size is invalid or decrypted data is invalid
656 """ 1359 """
657 if len(key) % size: 1360 if len(key) % size:
658 raise ValueError("Illegal size ({0}) for key '{1}'.".format( 1361 raise ValueError("Illegal size ({0}) for key '{1}'.".format(size, key))
659 size, key))
660 if len(IV) % 16: 1362 if len(IV) % 16:
661 raise ValueError("IV is not a multiple of 16.") 1363 raise ValueError("IV is not a multiple of 16.")
662 # the AES input/output 1364 # the AES input/output
663 iput = bytearray(16) 1365 iput = bytearray(16)
664 output = bytearray() 1366 output = bytearray()
731 # size - the bit length of the key 1433 # size - the bit length of the key
732 # IV - the 128 bit number array Initilization Vector 1434 # IV - the 128 bit number array Initilization Vector
733 def decrypt(self, cipherIn, originalsize, mode, key, size, IV): 1435 def decrypt(self, cipherIn, originalsize, mode, key, size, IV):
734 """ 1436 """
735 Public method to perform the decryption operation. 1437 Public method to perform the decryption operation.
736 1438
737 @param cipherIn data to be decrypted (bytes) 1439 @param cipherIn data to be decrypted (bytes)
738 @param originalsize unencrypted string length (required for CBC) 1440 @param originalsize unencrypted string length (required for CBC)
739 (integer) 1441 (integer)
740 @param mode mode of operation (0, 1 or 2) 1442 @param mode mode of operation (0, 1 or 2)
741 @param key key to be used (bytes) 1443 @param key key to be used (bytes)
743 @param IV initialisation vector (bytearray) 1445 @param IV initialisation vector (bytearray)
744 @return decrypted data (bytes) 1446 @return decrypted data (bytes)
745 @exception ValueError key size is invalid or decrypted data is invalid 1447 @exception ValueError key size is invalid or decrypted data is invalid
746 """ 1448 """
747 if len(key) % size: 1449 if len(key) % size:
748 raise ValueError("Illegal size ({0}) for key '{1}'.".format( 1450 raise ValueError("Illegal size ({0}) for key '{1}'.".format(size, key))
749 size, key))
750 if len(IV) % 16: 1451 if len(IV) % 16:
751 raise ValueError("IV is not a multiple of 16.") 1452 raise ValueError("IV is not a multiple of 16.")
752 # the AES input/output 1453 # the AES input/output
753 ciphertext = bytearray() 1454 ciphertext = bytearray()
754 iput = bytearray() 1455 iput = bytearray()
820 1521
821 1522
822 def encryptData(key, data, mode=AESModeOfOperation.ModeOfOperation["CBC"]): 1523 def encryptData(key, data, mode=AESModeOfOperation.ModeOfOperation["CBC"]):
823 """ 1524 """
824 Module function to encrypt the given data with the given key. 1525 Module function to encrypt the given data with the given key.
825 1526
826 @param key key to be used for encryption (bytes) 1527 @param key key to be used for encryption (bytes)
827 @param data data to be encrypted (bytes) 1528 @param data data to be encrypted (bytes)
828 @param mode mode of operations (0, 1 or 2) 1529 @param mode mode of operations (0, 1 or 2)
829 @return encrypted data prepended with the initialization vector (bytes) 1530 @return encrypted data prepended with the initialization vector (bytes)
830 @exception ValueError raised to indicate an invalid key size 1531 @exception ValueError raised to indicate an invalid key size
832 key = bytearray(key) 1533 key = bytearray(key)
833 if mode == AESModeOfOperation.ModeOfOperation["CBC"]: 1534 if mode == AESModeOfOperation.ModeOfOperation["CBC"]:
834 data = append_PKCS7_padding(data) 1535 data = append_PKCS7_padding(data)
835 keysize = len(key) 1536 keysize = len(key)
836 if keysize not in AES.KeySize.values(): 1537 if keysize not in AES.KeySize.values():
837 raise ValueError('invalid key size: {0}'.format(keysize)) 1538 raise ValueError("invalid key size: {0}".format(keysize))
838 # create a new iv using random data 1539 # create a new iv using random data
839 iv = bytearray([i for i in os.urandom(16)]) 1540 iv = bytearray([i for i in os.urandom(16)])
840 moo = AESModeOfOperation() 1541 moo = AESModeOfOperation()
841 mode, length, ciph = moo.encrypt(data, mode, key, keysize, iv) 1542 mode, length, ciph = moo.encrypt(data, mode, key, keysize, iv)
842 # With padding, the original length does not need to be known. It's a bad 1543 # With padding, the original length does not need to be known. It's a bad
846 1547
847 1548
848 def decryptData(key, data, mode=AESModeOfOperation.ModeOfOperation["CBC"]): 1549 def decryptData(key, data, mode=AESModeOfOperation.ModeOfOperation["CBC"]):
849 """ 1550 """
850 Module function to decrypt the given data with the given key. 1551 Module function to decrypt the given data with the given key.
851 1552
852 @param key key to be used for decryption (bytes) 1553 @param key key to be used for decryption (bytes)
853 @param data data to be decrypted (with initialization vector prepended) 1554 @param data data to be decrypted (with initialization vector prepended)
854 (bytes) 1555 (bytes)
855 @param mode mode of operations (0, 1 or 2) 1556 @param mode mode of operations (0, 1 or 2)
856 @return decrypted data (bytes) 1557 @return decrypted data (bytes)
857 @exception ValueError raised to indicate an invalid key size 1558 @exception ValueError raised to indicate an invalid key size
858 """ 1559 """
859 key = bytearray(key) 1560 key = bytearray(key)
860 keysize = len(key) 1561 keysize = len(key)
861 if keysize not in AES.KeySize.values(): 1562 if keysize not in AES.KeySize.values():
862 raise ValueError('invalid key size: {0}'.format(keysize)) 1563 raise ValueError("invalid key size: {0}".format(keysize))
863 # iv is first 16 bytes 1564 # iv is first 16 bytes
864 iv = bytearray(data[:16]) 1565 iv = bytearray(data[:16])
865 data = bytearray(data[16:]) 1566 data = bytearray(data[16:])
866 moo = AESModeOfOperation() 1567 moo = AESModeOfOperation()
867 decr = moo.decrypt(data, None, mode, key, keysize, iv) 1568 decr = moo.decrypt(data, None, mode, key, keysize, iv)

eric ide

mercurial