def doMixColumns(nRows, nColumns, wordSize): mixColumns = MixColumns(nRows, nColumns, wordSize) state = [[randint(0, 2**wordSize) for i in range(nColumns)] for j in range(nRows)] mixColumns.do(state) print("MixColumns(%2d, %2d, %2d)-> %6d xors" % (nRows, nColumns, wordSize, mixColumns.xors))
def test_base(loglevel): stateMatrix = [[0x00, 0x01, 0x02, 0x03], [0x10, 0x11, 0x12, 0x13], [0x20, 0x21, 0x22, 0x23], [0x30, 0x31, 0x32, 0x33]] mixcolumns = MixColumns(4, 4, 8, loglevel=levelFromMeaning(loglevel)) print("Testing %s, details:\nEach value in a cell will be interpreted " "as a polynomial representation of a binary field extension " "modulo %s, and the columns as polynomial in a ring modulo %s." % (mixcolumns, mixcolumns.SubfieldModulo, mixcolumns.PolynomialRingModulo)) stateConverted = mixcolumns.do(stateMatrix) if mixcolumns.invert(stateConverted) == stateMatrix: return True print("ALERT:\n\t%s\n!=\n\t%s" % (stateMatrix, stateConverted)) return False
def test_aes128_round1(loglevel): stateMatrix = [[99, 9, 205, 186], [83, 96, 112, 202], [224, 225, 183, 208], [140, 4, 81, 231]] # 0x6353e08c0960e104cd70b751bacad0e7 stateMixed = [[95, 87, 247, 29], [114, 245, 190, 185], [100, 188, 59, 249], [21, 146, 41, 26]] # 0x5f72641557f5bc92f7be3b291db9f91a mixcolumns = MixColumns(4, 4, 8) mixcolumns.logLevel = levelFromMeaning(loglevel) print("Testing AES128 round1") stateConverted = mixcolumns.do(stateMatrix) if stateMixed == stateConverted: return True print("ALERT:\n\t%s\n!=\n\t%s" % (stateMixed, stateConverted)) return False