4 from pyoram.crypto.aes import AES
6 def runtest(label, enc_func, dec_func):
9 print("{0:^20}".format(label))
11 for keysize in AES.key_sizes:
13 print("@@@@@@@@@@@@@@@@@@@@")
14 print(" Key Size: %s bytes" % (keysize))
15 print("@@@@@@@@@@@@@@@@@@@@")
20 key = AES.KeyGen(keysize)
21 print("Key: %s" % (base64.b64encode(key)))
24 # generate some plaintext
27 plaintext_numbytes = AES.block_size * nblocks
28 print("Plaintext Size: %s MB"
29 % (plaintext_numbytes * 1.0e-6))
31 plaintext = bytes(bytearray(plaintext_numbytes))
36 start_time = time.time()
37 ciphertext = enc_func(key, plaintext)
38 stop_time = time.time()
39 print("Encryption Time: %.3fs (%.3f MB/s)"
40 % (stop_time-start_time,
41 (plaintext_numbytes * 1.0e-6) / (stop_time-start_time)))
46 start_time = time.time()
47 plaintext_decrypted = dec_func(key, ciphertext)
48 stop_time = time.time()
49 print("Decryption Time: %.3fs (%.3f MB/s)"
50 % (stop_time-start_time,
51 (plaintext_numbytes * 1.0e-6) / (stop_time-start_time)))
53 assert plaintext_decrypted == plaintext
54 assert ciphertext != plaintext
56 assert enc_func(key, plaintext) != ciphertext
57 # make sure the only difference is not in the IV
58 assert enc_func(key, plaintext)[AES.block_size:] \
59 != ciphertext[AES.block_size:]
60 if enc_func is AES.CTREnc:
61 assert len(plaintext) == \
62 len(ciphertext) - AES.block_size
64 assert enc_func is AES.GCMEnc
65 assert len(plaintext) == \
66 len(ciphertext) - 2*AES.block_size
69 del plaintext_decrypted
72 print("\nTest Chunks")
76 key = AES.KeyGen(keysize)
77 print("Key: %s" % (base64.b64encode(key)))
80 # generate some plaintext
84 total_bytes = blocksize * nblocks
85 print("Block Size: %s KB" % (blocksize * 1.0e-3))
86 print("Block Count: %s" % (nblocks))
87 print("Total: %s MB" % (total_bytes * 1.0e-6))
88 plaintext_blocks = [bytes(bytearray(blocksize))
89 for i in range(nblocks)]
94 start_time = time.time()
95 ciphertext_blocks = [enc_func(key, b)
96 for b in plaintext_blocks]
97 stop_time = time.time()
98 print("Encryption Time: %.3fs (%.3f MB/s)"
99 % (stop_time-start_time,
100 (total_bytes * 1.0e-6) / (stop_time-start_time)))
105 start_time = time.time()
106 plaintext_decrypted_blocks = [dec_func(key, c)
107 for c in ciphertext_blocks]
108 stop_time = time.time()
109 print("Decryption Time: %.3fs (%.3f MB/s)"
110 % (stop_time-start_time,
111 (total_bytes * 1.0e-6) / (stop_time-start_time)))
114 runtest("AES - CTR Mode", AES.CTREnc, AES.CTRDec)
115 runtest("AES - GCM Mode", AES.GCMEnc, AES.GCMDec)
117 if __name__ == "__main__":
118 main() # pragma: no cover