def doKeyExpansion(nRounds, nRows, nColumns, wordSize, nKeyWords): keyExp = KeyExpansion(0, nRounds, nRows, nColumns, wordSize, nKeyWords) keyExp.getKey() print( "KeyExpansion(%2d, %2d, %2d, %2d, %2d)-> %6d xors" % (nRounds, nRows, nColumns, wordSize, nKeyWords, keyExp.xors) )
def expandKey(key, rounds, nRows, nColumns, wordSize, nKeyColumns, loglevel): print loglevel keyExpansion = KeyExpansion(key, rounds, nRows, nColumns, wordSize, nKeyColumns, loglevel=loglevel) print keyExpansion for round in range(rounds): subkey = keyExpansion.getSubKey(round*nRows, (round+1)*nRows) subkeyLst = hexlist(subkey, nColumns, wordSize) try: # FIXME! subkeyHex = hex(subkey2int(subkey, nRows, nColumns, wordSize)) except: if len(subkeyLst) == 0: subkeyHex = "" else: subkeyHex = "subkey2int exception" print("Round %d:\t%s\t%s" % (round, subkeyLst, subkeyHex)) print("block size: %d, key size: %d bits (%d blocks of %d bits), " "expanded to %d bits (%d rounds)" % (nRows*nColumns*wordSize, nRows*nKeyColumns*wordSize, nRows, nColumns*wordSize, rounds*nRows*nColumns*wordSize, rounds))
def test_AES(loglevel, rounds, aes, aes_round, Nk): key = aes['key'] keyExpansion = KeyExpansion(key, rounds, 4, 4, 8, nKeyWords=Nk, loglevel=loglevel) for round in aes_round.keys(): subkey = keyExpansion.getSubKey(round*4, (round+1)*4) if len(subkey) > 0: if subkey2int(subkey) != aes_round[round]['k_sch']: print("For round %d\n\t%s\n\t%s" % (round, hex(subkey2int(subkey)), hex(aes_round[round]['k_sch']))) print("For round %d\n\t%s\n\t%s" % (round, hexlist(subkey), hexlist(int2subkey(aes_round[round]['k_sch'])))) print("error!") return False else: print("Round %d ok:\t%s\t%35s" % (round, hexlist(subkey), hex(subkey2int(subkey)))) else: return False return True