diff options
author | Dwayne Litzenberger <dlitz@dlitz.net> | 2013-02-17 10:18:57 -0800 |
---|---|---|
committer | Dwayne Litzenberger <dlitz@dlitz.net> | 2013-02-17 10:18:57 -0800 |
commit | e85ae486ab9b7ec1bee51431888283ac7a7fcaa2 (patch) | |
tree | 5632e210941f102de05d928a9a8b3583310b9b2a /pct-speedtest.py | |
parent | 3e0afc7ae36080ace1486e8a0ee8aa0ed25b9a3b (diff) | |
download | pycrypto-e85ae486ab9b7ec1bee51431888283ac7a7fcaa2.tar.gz |
pct-speedtest.py: Add PKCS#1 signing and verification
Diffstat (limited to 'pct-speedtest.py')
-rw-r--r-- | pct-speedtest.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/pct-speedtest.py b/pct-speedtest.py index 2757d7f..bd099ce 100644 --- a/pct-speedtest.py +++ b/pct-speedtest.py @@ -28,6 +28,8 @@ 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, SHA224, SHA256, SHA384, SHA512 from Crypto.Random import get_random_bytes @@ -199,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)), @@ -282,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() |