344 """ |
344 """ |
345 state = state[:] |
345 state = state[:] |
346 for i in range(nbr): |
346 for i in range(nbr): |
347 if isInv: |
347 if isInv: |
348 state[statePointer:statePointer + 4] = \ |
348 state[statePointer:statePointer + 4] = \ |
349 state[statePointer + 3:statePointer + 4] + \ |
349 state[statePointer + 3:statePointer + 4] + \ |
350 state[statePointer:statePointer + 3] |
350 state[statePointer:statePointer + 3] |
351 else: |
351 else: |
352 state[statePointer:statePointer + 4] = \ |
352 state[statePointer:statePointer + 4] = \ |
353 state[statePointer + 1:statePointer + 4] + \ |
353 state[statePointer + 1:statePointer + 4] + \ |
354 state[statePointer:statePointer + 1] |
354 state[statePointer:statePointer + 1] |
355 return state |
355 return state |
356 |
356 |
357 def __mixColumns(self, state, isInv): |
357 def __mixColumns(self, state, isInv): |
358 """ |
358 """ |
359 Private method to perform a galois multiplication of the 4x4 matrix. |
359 Private method to perform a galois multiplication of the 4x4 matrix. |
391 mult = [2, 1, 1, 3] |
391 mult = [2, 1, 1, 3] |
392 cpy = column[:] |
392 cpy = column[:] |
393 g = self.__galois_multiplication |
393 g = self.__galois_multiplication |
394 |
394 |
395 column[0] = g(cpy[0], mult[0]) ^ g(cpy[3], mult[1]) ^ \ |
395 column[0] = g(cpy[0], mult[0]) ^ g(cpy[3], mult[1]) ^ \ |
396 g(cpy[2], mult[2]) ^ g(cpy[1], mult[3]) |
396 g(cpy[2], mult[2]) ^ g(cpy[1], mult[3]) |
397 column[1] = g(cpy[1], mult[0]) ^ g(cpy[0], mult[1]) ^ \ |
397 column[1] = g(cpy[1], mult[0]) ^ g(cpy[0], mult[1]) ^ \ |
398 g(cpy[3], mult[2]) ^ g(cpy[2], mult[3]) |
398 g(cpy[3], mult[2]) ^ g(cpy[2], mult[3]) |
399 column[2] = g(cpy[2], mult[0]) ^ g(cpy[1], mult[1]) ^ \ |
399 column[2] = g(cpy[2], mult[0]) ^ g(cpy[1], mult[1]) ^ \ |
400 g(cpy[0], mult[2]) ^ g(cpy[3], mult[3]) |
400 g(cpy[0], mult[2]) ^ g(cpy[3], mult[3]) |
401 column[3] = g(cpy[3], mult[0]) ^ g(cpy[2], mult[1]) ^ \ |
401 column[3] = g(cpy[3], mult[0]) ^ g(cpy[2], mult[1]) ^ \ |
402 g(cpy[1], mult[2]) ^ g(cpy[0], mult[3]) |
402 g(cpy[1], mult[2]) ^ g(cpy[0], mult[3]) |
403 return column |
403 return column |
404 |
404 |
405 def __aes_round(self, state, roundKey): |
405 def __aes_round(self, state, roundKey): |
406 """ |
406 """ |
407 Private method to apply the 4 operations of the forward round in |
407 Private method to apply the 4 operations of the forward round in |
662 firstRound = True |
662 firstRound = True |
663 if input: |
663 if input: |
664 for j in range(int(math.ceil(float(len(input)) / 16))): |
664 for j in range(int(math.ceil(float(len(input)) / 16))): |
665 start = j * 16 |
665 start = j * 16 |
666 end = j * 16 + 16 |
666 end = j * 16 + 16 |
667 if end > len(input): |
667 if end > len(input): |
668 end = len(input) |
668 end = len(input) |
669 plaintext = self.__extractBytes(input, start, end, mode) |
669 plaintext = self.__extractBytes(input, start, end, mode) |
670 # print 'PT@%s:%s' % (j, plaintext) |
670 # print 'PT@%s:%s' % (j, plaintext) |
671 if mode == self.ModeOfOperation["CFB"]: |
671 if mode == self.ModeOfOperation["CFB"]: |
672 if firstRound: |
672 if firstRound: |