def cipher(self, pt, key): # Note: we assume fixed key here for huge speedup # TODO: make this an option #if self.ks is None: self.ks = [keyScheduleRounds(key, 0, r) for r in range(11)] ret = {} Nr = 10 state = pt ret['Plaintext'] = self.flatten(state[:]) ret['Key'] = self.flatten(self.ks[0]) state = [state[i] ^ self.ks[0][i] for i in range(16)] ret['Round 0: AddRoundKey Output'] = self.flatten(state[:]) for r in range(1, Nr): state = subbytes(state) ret['Round ' + str(r) + ': SubBytes Output'] = self.flatten(state[:]) state = shiftrows(state) ret['Round ' + str(r) + ': ShiftRows Output'] = self.flatten(state[:]) state = mixcolumns(state) ret['Round ' + str(r) + ': MixColumns Output'] = self.flatten(state[:]) ret['Round ' + str(r) + ': RoundKey'] = self.flatten(self.ks[r]) state = [state[i] ^ self.ks[r][i] for i in range(16)] ret['Round ' + str(r) + ': AddRoundKey Output'] = self.flatten(state[:]) state = subbytes(state) ret['Round 10: SubBytes Output'] = self.flatten(state[:]) state = shiftrows(state) ret['Round 10: ShiftRows Output'] = self.flatten(state[:]) ret['Round 10: RoundKey'] = self.flatten(self.ks[Nr]) state = [state[i] ^ self.ks[Nr][i] for i in range(16)] ret['Ciphertext'] = self.flatten(state[:]) return ret
def cipher(self, pt, key): Nr = 14 if self.ks is None: self.ks = [keyScheduleRounds(key, 0, r) for r in range(Nr+1)] ret = {} state = pt ret['Plaintext'] = self.flatten(state[:]) ret['Key (bytes 0-15)'] = self.flatten(self.ks[0]) ret['Key (bytes 16-31)'] = self.flatten(self.ks[1]) state = [state[i] ^ self.ks[0][i] for i in range(16)] ret['Round 0: AddRoundKey Output'] = self.flatten(state[:]) for r in range(1, Nr): state = subbytes(state) ret['Round ' + str(r) + ': SubBytes Output'] = self.flatten(state[:]) state = shiftrows(state) ret['Round ' + str(r) + ': ShiftRows Output'] = self.flatten(state[:]) state = mixcolumns(state) ret['Round ' + str(r) + ': MixColumns Output'] = self.flatten(state[:]) state = [state[i] ^ self.ks[r][i] for i in range(16)] ret['Round ' + str(r) + ': AddRoundKey Output'] = self.flatten(state[:]) state = subbytes(state) ret['Round 14: SubBytes Output'] = self.flatten(state[:]) state = shiftrows(state) ret['Round 14: ShiftRows Output'] = self.flatten(state[:]) state = [state[i] ^ self.ks[Nr][i] for i in range(16)] ret['Ciphertext'] = self.flatten(state[:]) return ret
def shiftrows(self, state): """Helper function: performs AES shiftrows on all bytes of state""" return shiftrows(state)