PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/pycrypto/pct-speedtest.py

http://github.com/tav/pylibs
Python | 200 lines | 132 code | 32 blank | 36 comment | 21 complexity | ab730abc64556f4d91a32bcdf13959e2 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # pct-speedtest.py: Speed test for the Python Cryptography Toolkit
  5. #
  6. # Written in 2009 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  7. #
  8. # ===================================================================
  9. # The contents of this file are dedicated to the public domain. To
  10. # the extent that dedication to the public domain is not available,
  11. # everyone is granted a worldwide, perpetual, royalty-free,
  12. # non-exclusive license to exercise all rights associated with the
  13. # contents of this file for any purpose whatsoever.
  14. # No rights are reserved.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24. # ===================================================================
  25. import time
  26. import os
  27. import sys
  28. from Crypto.Cipher import AES, ARC2, ARC4, Blowfish, CAST, DES3, DES, XOR
  29. from Crypto.Hash import MD2, MD4, MD5, SHA256, SHA
  30. try:
  31. from Crypto.Hash import RIPEMD
  32. except ImportError: # Some builds of PyCrypto don't have the RIPEMD module
  33. RIPEMD = None
  34. class Benchmark:
  35. def __init__(self):
  36. self.__random_data = None
  37. def random_keys(self, bytes):
  38. """Return random keys of the specified number of bytes.
  39. If this function has been called before with the same number of bytes,
  40. cached keys are used instead of randomly generating new ones.
  41. """
  42. return self.random_blocks(bytes, 10**5) # 100k
  43. def random_blocks(self, bytes_per_block, blocks):
  44. bytes = bytes_per_block * blocks
  45. data = self.random_data(bytes)
  46. retval = []
  47. for i in xrange(blocks):
  48. p = i * bytes_per_block
  49. retval.append(data[p:p+bytes_per_block])
  50. return retval
  51. def random_data(self, bytes):
  52. if self.__random_data is None:
  53. self.__random_data = self._random_bytes(bytes)
  54. return self.__random_data
  55. elif bytes == len(self.__random_data):
  56. return self.__random_data
  57. elif bytes < len(self.__random_data):
  58. return self.__random_data[:bytes]
  59. else:
  60. self.__random_data += self._random_bytes(bytes - len(self.__random_data))
  61. return self.__random_data
  62. def _random_bytes(self, b):
  63. return os.urandom(b)
  64. def announce_start(self, test_name):
  65. sys.stdout.write("%s: " % (test_name,))
  66. sys.stdout.flush()
  67. def announce_result(self, value, units):
  68. sys.stdout.write("%.2f %s\n" % (value, units))
  69. sys.stdout.flush()
  70. def test_key_setup(self, cipher_name, module, key_bytes, mode):
  71. self.announce_start("%s key setup" % (cipher_name,))
  72. # Generate random keys for use with the tests
  73. keys = self.random_keys(key_bytes)
  74. # Perform key setups
  75. if mode is None:
  76. t0 = time.time()
  77. for k in keys:
  78. module.new(k)
  79. t = time.time()
  80. else:
  81. t0 = time.time()
  82. for k in keys:
  83. module.new(k, module.MODE_ECB)
  84. t = time.time()
  85. key_setups_per_second = len(keys) / (t - t0)
  86. self.announce_result(key_setups_per_second/1000, "kKeys/sec")
  87. def test_encryption(self, cipher_name, module, key_bytes, mode):
  88. self.announce_start("%s encryption" % (cipher_name,))
  89. # Generate random keys for use with the tests
  90. rand = self.random_data(key_bytes + module.block_size)
  91. key, iv = rand[:key_bytes], rand[key_bytes:]
  92. blocks = self.random_blocks(16384, 1000)
  93. if mode is None:
  94. cipher = module.new(key)
  95. else:
  96. cipher = module.new(key, mode, iv)
  97. # Perform encryption
  98. t0 = time.time()
  99. for b in blocks:
  100. cipher.encrypt(b)
  101. t = time.time()
  102. encryption_speed = (len(blocks) * len(blocks[0])) / (t - t0)
  103. self.announce_result(encryption_speed / 10**6, "MBps")
  104. def test_hash_small(self, hash_name, module):
  105. self.announce_start("%s (%d-byte inputs)" % (hash_name, module.digest_size))
  106. blocks = self.random_blocks(module.digest_size, 10000)
  107. # Initialize hashes
  108. t0 = time.time()
  109. for b in blocks:
  110. module.new(b).digest()
  111. t = time.time()
  112. hashes_per_second = len(blocks) / (t - t0)
  113. self.announce_result(hashes_per_second / 1000, "kHashes/sec")
  114. def test_hash_large(self, hash_name, module):
  115. self.announce_start("%s (single large input)" % (hash_name,))
  116. blocks = self.random_blocks(16384, 10000)
  117. # Perform hashing
  118. t0 = time.time()
  119. h = module.new()
  120. for b in blocks:
  121. h.update(b)
  122. h.digest()
  123. t = time.time()
  124. hash_speed = len(blocks) * len(blocks[0]) / (t - t0)
  125. self.announce_result(hash_speed / 10**6, "MBps")
  126. def run(self):
  127. block_specs = [
  128. ("DES", DES, 8),
  129. ("DES3", DES3, 24),
  130. ("AES128", AES, 16),
  131. ("AES192", AES, 24),
  132. ("AES256", AES, 32),
  133. ("Blowfish(256)", Blowfish, 32),
  134. ("CAST(40)", CAST, 5),
  135. ("CAST(80)", CAST, 10),
  136. ("CAST(128)", CAST, 16),
  137. ]
  138. stream_specs = [
  139. ("ARC2(128)", ARC2, 16),
  140. ("ARC4(128)", ARC4, 16),
  141. ("XOR(24)", XOR, 3),
  142. ("XOR(256)", XOR, 32),
  143. ]
  144. hash_specs = [
  145. ("MD2", MD2),
  146. ("MD4", MD4),
  147. ("MD5", MD5),
  148. ("SHA", SHA),
  149. ("SHA256", SHA256),
  150. ]
  151. if RIPEMD is not None:
  152. hash_specs += [("RIPEMD", RIPEMD)]
  153. for cipher_name, module, key_bytes in block_specs:
  154. self.test_key_setup(cipher_name, module, key_bytes, module.MODE_CBC)
  155. self.test_encryption("%s-CBC" % (cipher_name,), module, key_bytes, module.MODE_CBC)
  156. self.test_encryption("%s-CFB" % (cipher_name,), module, key_bytes, module.MODE_CFB)
  157. self.test_encryption("%s-PGP" % (cipher_name,), module, key_bytes, module.MODE_PGP)
  158. self.test_encryption("%s-OFB" % (cipher_name,), module, key_bytes, module.MODE_OFB)
  159. for cipher_name, module, key_bytes in stream_specs:
  160. self.test_key_setup(cipher_name, module, key_bytes, None)
  161. self.test_encryption(cipher_name, module, key_bytes, None)
  162. for hash_name, module in hash_specs:
  163. self.test_hash_small(hash_name, module)
  164. self.test_hash_large(hash_name, module)
  165. if __name__ == '__main__':
  166. Benchmark().run()
  167. # vim:set ts=4 sw=4 sts=4 expandtab: