diff options
Diffstat (limited to 'pct-speedtest.py')
-rw-r--r-- | pct-speedtest.py | 80 |
1 files changed, 78 insertions, 2 deletions
diff --git a/pct-speedtest.py b/pct-speedtest.py index eb2778f..bd099ce 100644 --- a/pct-speedtest.py +++ b/pct-speedtest.py @@ -28,10 +28,17 @@ import os import sys from Crypto.PublicKey import RSA +from Crypto.Cipher import PKCS1_OAEP, PKCS1_v1_5 as RSAES_PKCS1_v1_5 +from Crypto.Signature import PKCS1_PSS, PKCS1_v1_5 as RSASSA_PKCS1_v1_5 from Crypto.Cipher import AES, ARC2, ARC4, Blowfish, CAST, DES3, DES, XOR -from Crypto.Hash import HMAC, MD2, MD4, MD5, SHA, SHA224, SHA256, SHA384, SHA512 +from Crypto.Hash import HMAC, MD2, MD4, MD5, SHA224, SHA256, SHA384, SHA512 from Crypto.Random import get_random_bytes try: + from Crypto.Hash import SHA1 +except ImportError: + # Maybe it's called SHA + from Crypto.Hash import SHA as SHA1 +try: from Crypto.Hash import RIPEMD160 except ImportError: # Maybe it's called RIPEMD @@ -194,6 +201,59 @@ class Benchmark: mac_constructor = lambda data=None: hmac_constructor(key, data, digestmod) self.test_hash_large(mac_name, mac_constructor, digest_size) + def test_pkcs1_sign(self, scheme_name, scheme_constructor, hash_name, hash_constructor, digest_size): + self.announce_start("%s signing %s (%d-byte inputs)" % (scheme_name, hash_name, digest_size)) + + # Make a key + k = RSA.generate(2048) + sigscheme = scheme_constructor(k) + + # Make some hashes + blocks = self.random_blocks(digest_size, 50) + hashes = [] + for b in blocks: + hashes.append(hash_constructor(b)) + + # Perform signing + t0 = time.time() + for h in hashes: + sigscheme.sign(h) + t = time.time() + + speed = len(hashes) / (t - t0) + self.announce_result(speed, "sigs/sec") + + def test_pkcs1_verify(self, scheme_name, scheme_constructor, hash_name, hash_constructor, digest_size): + self.announce_start("%s verification %s (%d-byte inputs)" % (scheme_name, hash_name, digest_size)) + + # Make a key + k = RSA.generate(2048) + sigscheme = scheme_constructor(k) + + # Make some hashes + blocks = self.random_blocks(digest_size, 50) + hashes = [] + for b in blocks: + hashes.append(hash_constructor(b)) + + # Make some signatures + signatures = [] + for h in hashes: + signatures.append(sigscheme.sign(h)) + + # Double the list, to make timing better + hashes = hashes + hashes + signatures = signatures + signatures + + # Perform verification + t0 = time.time() + for h, s in zip(hashes, signatures): + sigscheme.verify(h, s) + t = time.time() + + speed = len(hashes) / (t - t0) + self.announce_result(speed, "sigs/sec") + def run(self): pubkey_specs = [ ("RSA(1024)", RSA, int(1024/8)), @@ -221,7 +281,7 @@ class Benchmark: ("MD2", MD2), ("MD4", MD4), ("MD5", MD5), - ("SHA", SHA), + ("SHA1", SHA1), ("SHA224", SHA224), ("SHA256", SHA256), ("SHA384", SHA384), @@ -277,6 +337,22 @@ class Benchmark: self.test_hmac_small("hmac+"+hash_name, hmac.HMAC, func, func().digest_size) self.test_hmac_large("hmac+"+hash_name, hmac.HMAC, func, func().digest_size) + # PKCS1_v1_5 (sign) + Crypto.Hash + for hash_name, module in hash_specs: + self.test_pkcs1_sign("PKCS#1-v1.5", RSASSA_PKCS1_v1_5.new, hash_name, module.new, module.digest_size) + + # PKCS1_PSS (sign) + Crypto.Hash + for hash_name, module in hash_specs: + self.test_pkcs1_sign("PKCS#1-PSS", PKCS1_PSS.new, hash_name, module.new, module.digest_size) + + # PKCS1_v1_5 (verify) + Crypto.Hash + for hash_name, module in hash_specs: + self.test_pkcs1_verify("PKCS#1-v1.5", RSASSA_PKCS1_v1_5.new, hash_name, module.new, module.digest_size) + + # PKCS1_PSS (verify) + Crypto.Hash + for hash_name, module in hash_specs: + self.test_pkcs1_verify("PKCS#1-PSS", PKCS1_PSS.new, hash_name, module.new, module.digest_size) + if __name__ == '__main__': Benchmark().run() |