Exemplo n.º 1
0
def basic_tests():
	print(colors.HEADER+"[Basic encryption tests]"+colors.ENDC)

	test_key = uuid.uuid4().hex+uuid.uuid4().hex # 256-bit (64 character) hex key
	print(colors.OKBLUE+"Generated 256-bit AES key: "+colors.WARNING+test_key+colors.ENDC)

	print(colors.OKBLUE+"Checking that two S-Boxes generated from the same key are equal..."+colors.ENDC)
	sbox_one = dAES.generateDynamicSbox(dAES.sboxOrig, dAES.hexToKey(test_key))
	sbox_two = dAES.generateDynamicSbox(dAES.sboxOrig, dAES.hexToKey(test_key))
	if not sbox_one==sbox_two:
		print(colors.OKBLUE+"\tS-Boxes aren't the same! "+colors.FAIL+"[FAILED]"+colors.ENDC)
		exit(1)
	print(colors.OKBLUE+"\tS-Boxes are the same "+colors.OKGREEN+"[PASSED]"+colors.ENDC)

	int_test_key = dAES.hexToKey(test_key)
	plain = uuid.uuid4().hex+uuid.uuid4().hex+uuid.uuid4().hex+uuid.uuid4().hex[0:random.randrange(0,32)]
	plaintwo = uuid.uuid4().hex+uuid.uuid4().hex+uuid.uuid4().hex+uuid.uuid4().hex[0:random.randrange(0,32)]
	print(colors.OKBLUE+"Generating "+str(len(plain))+" bytes of plaintext..."+colors.ENDC)
	print(colors.OKBLUE+"Encrypting plaintext..."+colors.ENDC)
	hard = dAES.encrypt(plain, int_test_key)
	hardtwo = dAES.encrypt(plain, int_test_key)
	if plain!=hard:
		print(colors.OKBLUE+"\tEncrypted ciphertext isn't the same as plaintext! "+colors.OKGREEN+"[PASSED]"+colors.ENDC)
	else:
		print(colors.OKBLUE+"\tEncrypted ciphertext is the same as plaintext! "+colors.FAIL+"[FAILED]"+colors.ENDC)
		exit(1)
	print(colors.OKBLUE+"Decrypting ciphertext..."+colors.ENDC)
	decd = dAES.decrypt(hard, int_test_key)
	if plain==decd:
		print(colors.OKBLUE+"\tDecrypted plaintext is the same as original plaintext! "+colors.OKGREEN+"[PASSED]"+colors.ENDC)
	else:
		print(colors.OKBLUE+"\tDecrypted plaintext isn't the same as original plaintext! "+colors.FAIL+"[FAILED]"+colors.ENDC)
		exit(1)
	print(colors.HEADER+"## All basic tests passed ##"+colors.ENDC)
Exemplo n.º 2
0
def sbox_tests(iters):
	# sbox tests
	print(colors.HEADER+"\n[Dynamic S-Box tests]"+colors.ENDC)
	test_key = uuid.uuid4().hex+uuid.uuid4().hex

	sbox_sim = []
	for i in range(iters):
		sbox_key = uuid.uuid4().hex+uuid.uuid4().hex
		sbox_keys = bitFlip(sbox_key)
		sbox_clean = dAES.generateDynamicSbox(dAES.sboxOrig, dAES.hexToKey(sbox_key))
		while len(sbox_keys) > 0:
			k = random.choice(sbox_keys)
			
			sbox_test = dAES.generateDynamicSbox(dAES.sboxOrig, dAES.hexToKey(k))

			diff = sboxDiff(sbox_clean, sbox_test)
			sbox_sim.append(diff)
			sbox_keys.pop(sbox_keys.index(k))
	print(colors.OKBLUE+"Tested "+str(len(sbox_sim))+" S-Boxes"+colors.ENDC)
	print(colors.OKBLUE+"\tMean difference: "+colors.OKGREEN+str(round(mean(sbox_sim)*100, 2))+"%"+colors.ENDC)
	print(colors.OKBLUE+"\tPercentage with no difference: "+colors.OKGREEN+str(round((sbox_sim.count(0)/len(sbox_sim))*100, 2))+"%"+colors.ENDC)
	return sbox_sim