234 # and increment rconIteration afterwards |
234 # and increment rconIteration afterwards |
235 if currentSize % size == 0: |
235 if currentSize % size == 0: |
236 t = self.__core(t, rconIteration) |
236 t = self.__core(t, rconIteration) |
237 rconIteration += 1 |
237 rconIteration += 1 |
238 # For 256-bit keys, we add an extra sbox to the calculation |
238 # For 256-bit keys, we add an extra sbox to the calculation |
239 if size == self.KeySize["SIZE_256"] and \ |
239 if ( |
240 ((currentSize % size) == 16): |
240 size == self.KeySize["SIZE_256"] and |
|
241 ((currentSize % size) == 16) |
|
242 ): |
241 for l in range(4): |
243 for l in range(4): |
242 t[l] = self.__getSBoxValue(t[l]) |
244 t[l] = self.__getSBoxValue(t[l]) |
243 |
245 |
244 # We XOR t with the four-byte block 16, 24, 32 bytes before the new |
246 # We XOR t with the four-byte block 16, 24, 32 bytes before the new |
245 # expanded key. This becomes the next four bytes in the expanded |
247 # expanded key. This becomes the next four bytes in the expanded |
246 # key. |
248 # key. |
247 for m in range(4): |
249 for m in range(4): |
248 expandedKey[currentSize] = \ |
250 expandedKey[currentSize] = ( |
249 expandedKey[currentSize - size] ^ t[m] |
251 expandedKey[currentSize - size] ^ t[m] |
|
252 ) |
250 currentSize += 1 |
253 currentSize += 1 |
251 |
254 |
252 return expandedKey |
255 return expandedKey |
253 |
256 |
254 def __addRoundKey(self, state, roundKey): |
257 def __addRoundKey(self, state, roundKey): |
343 @return modified state (bytearray) |
346 @return modified state (bytearray) |
344 """ |
347 """ |
345 state = state[:] |
348 state = state[:] |
346 for _ in range(nbr): |
349 for _ in range(nbr): |
347 if isInv: |
350 if isInv: |
348 state[statePointer:statePointer + 4] = \ |
351 state[statePointer:statePointer + 4] = ( |
349 state[statePointer + 3:statePointer + 4] + \ |
352 state[statePointer + 3:statePointer + 4] + |
350 state[statePointer:statePointer + 3] |
353 state[statePointer:statePointer + 3] |
|
354 ) |
351 else: |
355 else: |
352 state[statePointer:statePointer + 4] = \ |
356 state[statePointer:statePointer + 4] = ( |
353 state[statePointer + 1:statePointer + 4] + \ |
357 state[statePointer + 1:statePointer + 4] + |
354 state[statePointer:statePointer + 1] |
358 state[statePointer:statePointer + 1] |
|
359 ) |
355 return state |
360 return state |
356 |
361 |
357 def __mixColumns(self, state, isInv): |
362 def __mixColumns(self, state, isInv): |
358 """ |
363 """ |
359 Private method to perform a galois multiplication of the 4x4 matrix. |
364 Private method to perform a galois multiplication of the 4x4 matrix. |