def main(): parser = OptionParser() parser.add_option('', "--log-level", default="info", help="Set log level: error, warning, info, debug, trace") parser.add_option('', "--rijndael", type='str', help="Comma separated set of Rijndael's generalised" "parameters. For example from the original Rijndael: " "10,4,4,8 for 128, or 12,4,4,8,6 for 192 or " "14,4,4,8,8 for 256 " "(nRounds, nRows, nColumns, wordSize[, nKeyColumns])") import sys (options, args) = parser.parse_args() loglevel = levelFromMeaning(options.log_level) if options.rijndael is not None: parameters = extractParams(options.rijndael) now = datetime.now().strftime("%Y%m%d_%H%M%S") fileName = "%s_gRijndaelXORxtr.csv" % (now) doRijndael(fileName, *parameters) else: # BinaryPolynomialsXORCtr() # PolynomialRingXORCtr() # SBoxXORctr() # keyExpansionXORctr() # addRoundKeyXORctr() # subBytesXORctr() # mixColumnsXORctr() gRijndaelXORxtr()
def main(): parser = OptionParser() parser.add_option('', "--log-level", default="info", help="Set log level: error, warning, info, debug, trace") parser.add_option('', "--rijndael", type='str', help="Comma separated set of Rijndael's generalised" "parameters. For example from the original Rijndael: " "10,4,4,8 for 128, or 12,4,4,8,6 for 192 or " "14,4,4,8,8 for 256 " "(nRounds, nRows, nColumns, wordSize[, nKeyColumns])") import sys (options, args) = parser.parse_args() loglevel = levelFromMeaning(options.log_level) if options.rijndael is not None: parameters = extractParams(options.rijndael) rindaelTest = RindaelTest(options.log_level, *parameters) print("Encryption process") encryptionDiffusion(rindaelTest.encrypt) print("subBytes process") encryptionDiffusion(rindaelTest.subBytesDiffusion) print("shiftRows process") encryptionDiffusion(rindaelTest.shiftRowsDiffusion) print("mixColumns process") encryptionDiffusion(rindaelTest.mixColumnsDiffusion) print("subBytes & mixColumns process") encryptionDiffusion(rindaelTest.subBytesAndmixColumnsDiffusion)
def main(): parser = OptionParser() parser.add_option('', "--log-level", default="info", help="Set log level: error, warning, info, debug, trace") parser.add_option('', "--test", type='str') parser.add_option('', "--rounds", type='int', default=0) parser.add_option('', "--key", type='int', default=0) import sys (options, args) = parser.parse_args() loglevel = levelFromMeaning(options.log_level) if options.test: if options.test.lower() in ["aes128", "aes192", "aes256"]: if options.test.lower() == "aes128": if options.rounds == 0: result = test_AES128(loglevel) else: result = test_AES128(loglevel, rounds=options.rounds) elif options.test.lower() == "aes192": if options.rounds == 0: result = test_AES192(loglevel) else: result = test_AES192(loglevel, rounds=options.rounds) elif options.test.lower() == "aes256": if options.rounds == 0: result = test_AES256(loglevel) else: result = test_AES256(loglevel, rounds=options.rounds) else: result = False if result: sys.exit(0) sys.exit(-1) else: rounds, nRows, nColumns, wordSize, nKeyColumns = \ extractParams(options.test) expandKey(options.key, rounds, nRows, nColumns, wordSize, nKeyColumns, options.log_level) else: for test in [test_AES128, test_AES192, test_AES256]: if not test(levelFromMeaning(options.log_level)): sys.exit(-1) sys.exit(0)
def test_standard(loglevel): stateMatrix = [[0x00, 0x01, 0x02, 0x03], [0x10, 0x11, 0x12, 0x13], [0x20, 0x21, 0x22, 0x23], [0x30, 0x31, 0x32, 0x33]] shiftrows = ShiftRows(4) shiftrows.logLevel = levelFromMeaning(loglevel) print("Testing %s." % (shiftrows)) stateConverted = shiftrows.do(stateMatrix) if shiftrows.invert(stateConverted) == stateMatrix: return True print("ALERT:\n\t%s\n!=\n\t%s" % (stateMatrix, stateConverted)) return False
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
def main(): parser = OptionParser() parser.add_option('', "--log-level", default="info", help="Set log level: error, warning, info, debug, trace") parser.add_option('', "--rijndael", type='str', help="Comma separated set of Rijndael's generalised" "parameters. For example from the original Rijndael: " "10,4,4,8 for 128, or 12,4,4,8,6 for 192 or " "14,4,4,8,8 for 256 " "(nRounds, nRows, nColumns, wordSize[, nKeyColumns])") import sys (options, args) = parser.parse_args() loglevel = levelFromMeaning(options.log_level) if options.rijndael is not None: parameters = extractParams(options.rijndael) rindaelTest = DiffusionTest(options.log_level, *parameters) print("Encryption process") halfdiffusion = encryptionDiffusion(rindaelTest.encrypt) print("\n\tHalf-diffusion: %d" % (halfdiffusion)) print("\tFull-diffusion: %d\n\n" % (halfdiffusion*2)) # print("subBytes process") # if encryptionDiffusion(rindaelTest.subBytesDiffusion): # print("\n\tFull diffusion: %d\n\n" % (halfdiffusion*2)) # print("shiftRows process") # if encryptionDiffusion(rindaelTest.shiftRowsDiffusion): # print("\n\tFull diffusion: %d\n\n" % (halfdiffusion*2)) # print("mixColumns process") # if encryptionDiffusion(rindaelTest.mixColumnsDiffusion): # print("\n\tFull diffusion: %d\n\n" % (halfdiffusion*2)) # print("subBytes & mixColumns process") # if encryptionDiffusion(rindaelTest.subBytesAndmixColumnsDiffusion): # print("\n\tFull diffusion: %d\n\n" % (halfdiffusion*2)) else: now = datetime.now().strftime("%Y%m%d_%H%M%S") fileName = "%s_fulldiffusion.csv" % (now) with open(fileName, 'a') as f: f.write("rounds\trow\tcolumns\twordsize\tkolumns\tblock\tkey" "\tfull-diffusion\n") for nRows in range(2, 9): for nColumns in range(2, 17): for wordSize in range(3, 17): for nKolumns in range(2, 17): if nKolumns >= nColumns: nRounds = max(nKolumns, nColumns) + 6 blockSize = nRows*nColumns*wordSize keySize = nRows*nKolumns*wordSize rindaelTest = DiffusionTest(options.log_level, nRounds, nRows, nColumns, wordSize, nKolumns) halfdiffusion = \ encryptionDiffusion(rindaelTest.encrypt) print("nRounds = %2d, nRows = %2d, " "nColumns = %2d, wordSize = %2d, " "nKeyColumns = %2d, blockSize = %4d, " "keySize = %4s -> full-diffusion = %2d" % (nRounds, nRows, nColumns, wordSize, nKolumns, blockSize, keySize, halfdiffusion*2)) with open(fileName, 'a') as f: f.write("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n" % (nRounds, nRows, nColumns, wordSize, nKolumns, blockSize, keySize, halfdiffusion*2))
def main(): parser = OptionParser() parser.add_option('', "--log-level", default="info", help="Set log level: error, warning, info, debug, trace") parser.add_option('', "--rijndael", type='str', help="Comma separated set of Rijndael's generalised" "parameters. For example from the original Rijndael: " "10,4,4,8 for 128, or 12,4,4,8,6 for 192 or " "14,4,4,8,8 for 256 " "(nRounds, nRows, nColumns, wordSize[, nKeyColumns])") parser.add_option('', "--processors", type="str", help="Tell the application how many processors will be " "used. A positive number will establish the number of " "parallel workers and each will use one of the cores. " "With the string'max' the application will use all the " "available cores. Telling a negative number with be " "understood as how many below the maximum will be used.") import sys (options, args) = parser.parse_args() loglevel = levelFromMeaning(options.log_level) if options.rijndael is not None: parameters = extractParams(options.rijndael) rindaelTest = DiffusionTest(options.log_level, *parameters) print("Encryption process") halfdiffusion = encryptionDiffusion(rindaelTest.encrypt) print("\n\tHalf-diffusion: %d" % (halfdiffusion)) print("\tFull-diffusion: %d\n\n" % (halfdiffusion * 2)) else: now = datetime.now().strftime("%Y%m%d_%H%M%S") fileName = "%s_fulldiffusion.csv" % (now) with open(fileName, 'a') as f: f.write("rounds\trow\tcolumns\twordsize\tkolumns\tblock\tkey" "\tfull-diffusion\n") lock = _Lock() arginLst = [] for nRows in range(2, 9): for nColumns in range(2, 17): for wordSize in range(3, 17): for nKolumns in range(2, 17): if nKolumns >= nColumns: nRounds = max(nKolumns, nColumns) + 6 arginLst.append([ options.log_level, nRounds, nRows, nColumns, wordSize, nKolumns ]) if options.processors is None or Pool is None: if Pool is None: print("\n\tyamp not available!" "\n\tCalculation will NOT be parallel") sleep(5) for argin in arginLst: argout = target(argin) write2cvs(argin, argout, **{ 'lock': lock, 'fileName': fileName }) else: pool = Pool(target, arginLst, options.processors, postHook=write2cvs, postExtraArgs={ 'lock': lock, 'fileName': fileName }, debug=True, logLevel=DEBUG, log2File=True, loggerName="FullDiffusion", loggingFolder='.') pool.log2file = True pool.start() while pool.isAlive(): sleep(10) computation, perWorker = pool.computation for i, each in enumerate(perWorker): perWorker[i] = "%s" % each contributions = pool.contributions states = pool.workersStarted for i, each in enumerate(states): s = "%s" % each states[i] = s[0] progress = pool.progress print("\n\tprogress: %.2f%%" % ((progress) * 100)) print("\tcontributions: %s" % (contributions)) print("\tstates: %s" % (states)) print("\tcomputation: %s %s\n" % (computation, perWorker))