edits
[iotcloud.git] / PyORAM / examples / aesctr_performance.py
1 import time
2 import base64
3
4 from pyoram.crypto.aes import AES
5
6 def runtest(label, enc_func, dec_func):
7     print("")
8     print("$"*20)
9     print("{0:^20}".format(label))
10     print("$"*20)
11     for keysize in AES.key_sizes:
12         print("")
13         print("@@@@@@@@@@@@@@@@@@@@")
14         print(" Key Size: %s bytes" % (keysize))
15         print("@@@@@@@@@@@@@@@@@@@@")
16         print("\nTest Bulk")
17         #
18         # generate a key
19         #
20         key = AES.KeyGen(keysize)
21         print("Key: %s" % (base64.b64encode(key)))
22
23         #
24         # generate some plaintext
25         #
26         nblocks = 1000000
27         plaintext_numbytes = AES.block_size * nblocks
28         print("Plaintext Size: %s MB"
29               % (plaintext_numbytes * 1.0e-6))
30         # all zeros
31         plaintext = bytes(bytearray(plaintext_numbytes))
32
33         #
34         # time encryption
35         #
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)))
42
43         #
44         # time decryption
45         #
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)))
52
53         assert plaintext_decrypted == plaintext
54         assert ciphertext != plaintext
55         # IND-CPA
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
63         else:
64             assert enc_func is AES.GCMEnc
65             assert len(plaintext) == \
66                 len(ciphertext) - 2*AES.block_size
67
68         del plaintext
69         del plaintext_decrypted
70         del ciphertext
71
72         print("\nTest Chunks")
73         #
74         # generate a key
75         #
76         key = AES.KeyGen(keysize)
77         print("Key: %s" % (base64.b64encode(key)))
78
79         #
80         # generate some plaintext
81         #
82         nblocks = 1000
83         blocksize = 16000
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)]
90
91         #
92         # time encryption
93         #
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)))
101
102         #
103         # time decryption
104         #
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)))
112
113 def main():
114     runtest("AES - CTR Mode", AES.CTREnc, AES.CTRDec)
115     runtest("AES - GCM Mode", AES.GCMEnc, AES.GCMDec)
116
117 if __name__ == "__main__":
118     main()                                             # pragma: no cover