-
Notifications
You must be signed in to change notification settings - Fork 2
/
bench-pycrypt.py
472 lines (372 loc) · 17.3 KB
/
bench-pycrypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
"""
Benchmark the PyCrypto and cryptography libraries.
(C) 2016 Willem Hengeveld <itsme@xs4all.nl>
"""
from __future__ import division, print_function, absolute_import, unicode_literals
import inspect
import time
import types
import Crypto.Cipher.AES
import Crypto.Cipher.Blowfish
import Crypto.Cipher.DES
import Crypto.Cipher.XOR
import Crypto.Cipher.ARC2
import Crypto.Cipher.ARC4
import Crypto.Cipher.CAST
import Crypto.Cipher.DES3
import Crypto.Hash.SHA224
import Crypto.Hash.SHA256
import Crypto.Hash.SHA384
import Crypto.Hash.SHA512
import Crypto.Hash.MD5
import Crypto.Hash.MD4
import Crypto.Hash.MD2
import Crypto.Hash.SHA
import Crypto.Hash.RIPEMD
import Crypto.Random
import Crypto.Util.number
import Crypto.PublicKey.RSA
from cryptography.hazmat.backends import default_backend
import cryptography.hazmat.primitives.asymmetric.rsa
import cryptography.hazmat.primitives.asymmetric.ec
import cryptography.hazmat.primitives.asymmetric.padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import ciphers
from cryptography.hazmat.primitives.ciphers import algorithms, modes
import random
import os
backend = default_backend()
def getCachedValue(cache, bits, generator):
""" Return and cache large (prime) numbers of specified number of bits """
if bits not in cache:
# populate with 2 numbers
cache[bits] = [generator(bits) for _ in range(2)]
# rotate, so each call will return a different number
a = cache[bits]
cache[bits] = a[1:] + a[:1]
return cache[bits][0]
bignumbers = {}
def generateNBitNumber(bits):
# Crypto.Util.number.getRandomNBitInteger
return getCachedValue(bignumbers, bits, random.getrandbits)
bigprimes = {} # cache big primes
def generateNBitPrime(bits):
return getCachedValue(bigprimes, bits, Crypto.Util.number.getPrime)
def generateNBitString(bits):
#return Crypto.Util.number.long_to_bytes(generateNBitNumber(bits))
return os.urandom(bits//8)
class Perftest(object):
"""
Benchmark the test() of childclasses.
First a few dummy rounds are executed to make sure
all dynamic initializations are done.
Then a baseline is established by counting how
many iterations are executed in 1/20 second.
Then a 'full' test is run by measuring and executing
for 1 full second.
"""
def __init__(self, description):
self.description = description
def countrounds(self, to):
""" count how many iterations of self.test() can be run in the given time span <to> """
ts = time.clock()
te = ts+to
count = 0
while time.clock() < te:
self.test()
count += 1
return count
def run(self, counttime, fulltime):
""" run the test """
try:
for _ in range(4):
self.test()
nrounds = int(self.countrounds(counttime)*(fulltime/counttime))
ts = time.clock()
for _ in range(nrounds):
self.test()
te = time.clock()
except Exception as e:
print(e)
te, ts = 1, 0
nrounds = 0
print('%8d iter in %8.4f sec : %10.1f iter/sec: %s' % (nrounds, te-ts, nrounds/(te-ts), self.description))
class TestPythonSysRandom(Perftest):
""" test PyCrypto SysRandom algorithms """
def __init__(self, msgbits):
super(TestPythonSysRandom, self).__init__("sysrand:%d" % (msgbits))
self.msgbits = msgbits
self.rng = random.SystemRandom()
def test(self):
return self.rng.getrandbits(self.msgbits)
class TestPythonRandom(Perftest):
""" test PyCrypto PRNG algorithms """
def __init__(self, msgbits):
super(TestPythonRandom, self).__init__("random:%d" % (msgbits))
self.msgbits = msgbits
def test(self):
return random.getrandbits(self.msgbits)
class TestOsURandom(Perftest):
""" test os.urandom """
def __init__(self, msgbits):
super(TestOsURandom, self).__init__("os.urandom:%d" % (msgbits))
self.msgbits = msgbits
def test(self):
return os.urandom(self.msgbits//8)
class TestPyCryptPRNG(Perftest):
""" test PyCrypto PRNG algorithms """
def __init__(self, msgbits):
super(TestPyCryptPRNG, self).__init__("PRNG:%d" % (msgbits))
self.msgbits = msgbits
def test(self):
return Crypto.Util.number.getRandomNBitInteger(self.msgbits)
class TestGenPrime(Perftest):
def __init__(self, bits):
super(TestGenPrime, self).__init__("Crypto.GenPrime:%d" % bits)
self.bits = bits
def test(self):
return Crypto.Util.number.getPrime(self.bits)
class TestPyCryptHash(Perftest):
""" test PyCrypto hash algorithms """
def __init__(self, algo, msgbits):
super(TestPyCryptHash, self).__init__("%s:%d" % (algo.__name__, msgbits))
self.algo = algo
self.msg = generateNBitString(msgbits)
def test(self):
return self.algo.new(self.msg).digest()
class TestPyCryptCipher(Perftest):
""" test PyCrypto symmetric ciphers """
def __init__(self, algo, keybits, msgbits):
super(TestPyCryptCipher, self).__init__("%s:%d/%d" % (algo.__name__, keybits, msgbits))
key = generateNBitString(keybits)
self.cipher = algo.new(key)
self.msg = generateNBitString(msgbits)
def test(self):
return self.cipher.encrypt(self.msg)
class TestPyCryptRsa(Perftest):
""" test PyCrypto RSA """
keycache = {}
def __init__(self, modbits, msgbits):
super(TestPyCryptRsa, self).__init__("Crypto.RSA:%d/%d" % (modbits, msgbits))
if modbits not in self.keycache:
self.keycache[modbits] = Crypto.PublicKey.RSA.generate(modbits, Crypto.Random.new().read)
self.privkey = self.keycache[modbits]
self.msg = generateNBitString(msgbits)
def test(self):
return self.privkey.encrypt(self.msg, None)
class TestCryptHash(Perftest):
""" test cryptography hash algorithms """
def __init__(self, algo, msgbits):
super(TestCryptHash, self).__init__("cryptography.%s:%d" % (algo.name, msgbits))
self.algo = algo
self.msg = generateNBitString(msgbits)
def test(self):
h = hashes.Hash(self.algo, backend=backend)
h.update(self.msg)
return h.finalize()
class TestCryptCipher(Perftest):
""" test cryptography symmetric ciphers """
def __init__(self, algo, keybits, msgbits):
super(TestCryptCipher, self).__init__("cryptography.%s:%d/%d" % (algo.name, keybits, msgbits))
try:
key = generateNBitString(keybits)
self.cipher = ciphers.Cipher(algo(key), mode=None if algo.name=="RC4" else modes.ECB(), backend=backend)
self.encryptor = self.cipher.encryptor()
self.msg = generateNBitString(msgbits)
except Exception as e:
print(e)
self.encryptor = None
def test(self):
if self.encryptor:
return self.encryptor.update(self.msg)
class TestCryptRsa(Perftest):
""" test cryptography RSA """
keycache = {}
def __init__(self, modbits, msgbits):
super(TestCryptRsa, self).__init__("cryptography.rsa:%d/%d" % (modbits, msgbits))
if modbits not in self.keycache:
self.keycache[modbits] = backend.generate_rsa_private_key(65537, modbits)
self.privkey = self.keycache[modbits]
self.pubkey = self.privkey.public_key()
self.msg = generateNBitString(msgbits)
def test(self):
self.pubkey.encrypt(self.msg, cryptography.hazmat.primitives.asymmetric.padding.PKCS1v15())
class TestCryptEc(Perftest):
""" test cryptography Elliptic Curve DH """
def __init__(self, curve):
super(TestCryptEc, self).__init__("cryptography.ec:%s" % curve.name)
self.alice_priv = backend.generate_elliptic_curve_private_key(curve)
self.bob_priv = backend.generate_elliptic_curve_private_key(curve)
self.bob_pub = self.bob_priv.public_key()
def test(self):
self.alice_priv.exchange(cryptography.hazmat.primitives.asymmetric.ec.ECDH(), self.bob_pub)
class TestModexp(Perftest):
""" test modular exponentiation for RSA """
def __init__(self, modbits, msgbits, expbits):
super(TestModexp, self).__init__("modexp:%d/%d/%d" % (modbits, msgbits, expbits))
self.msg = generateNBitNumber(msgbits)
self.exponent = generateNBitNumber(expbits)
self.modulus = generateNBitPrime(modbits)
def test(self):
return pow(self.msg, self.exponent, self.modulus)
import gmpy2
class TestGmpPowmod(Perftest):
""" test gmpy modular exponentiation for RSA """
def __init__(self, modbits, msgbits, expbits):
super(TestGmpPowmod, self).__init__("powmod:%d/%d/%d" % (modbits, msgbits, expbits))
self.msg = generateNBitNumber(msgbits)
self.exponent = generateNBitNumber(expbits)
self.modulus = generateNBitPrime(modbits)
def test(self):
return gmpy2.powmod(self.msg, self.exponent, self.modulus)
class TestLambda(Perftest):
""" test generic lambda expression """
def __init__(self, fn):
super(TestLambda, self).__init__(inspect.getsource(fn).strip())
self.fn = fn
def test(self):
return self.fn()
class TestEval(Perftest):
""" test string containing a python expression """
def __init__(self, fn):
super(TestEval, self).__init__("eval:"+fn)
name = "tst_%x" % id(self)
exec("def %s(): %s" % (name, fn))
self.test = locals().get(name)
# an empty function
def nop(): pass
def TestArgRange(testname, *args, **kwargs):
""" generate tests, enumerating through a set of arguments """
# cur contains the current index set
cur = [0 for _ in range(len(kwargs))]
# end contains the max index for the specific args.
end = [len(_) if type(_)==tuple else 1 for _ in kwargs.values()]
while True:
# build arg list
kw = {}
for (k, v), i in zip(kwargs.items(), cur):
if type(v)==tuple:
kw[k] = v[i]
else:
kw[k] = v
# create test
yield testname(*args, **kw)
# increment ix list
i = 0
while i<len(cur) and cur[i]==end[i]-1:
cur[i] = 0
i += 1
while i<len(cur) and end[i]==1:
i += 1
if i<len(cur):
cur[i] += 1
else:
break
def create_test_list():
hash_msg_bitsizes = tuple(pow(2, _) for _ in range(8, 24))
symm_msg_bitsizes = tuple(pow(2, _) for _ in range(8, 24))
prng_msg_bitsizes = tuple(pow(2, _) for _ in range(8, 18))
return [
TestArgRange(TestPythonRandom, msgbits=prng_msg_bitsizes),
TestArgRange(TestOsURandom, msgbits=prng_msg_bitsizes),
TestArgRange(TestPythonSysRandom, msgbits=prng_msg_bitsizes),
TestArgRange(TestPyCryptPRNG, msgbits=prng_msg_bitsizes),
TestArgRange(TestGenPrime, bits=(32, 64, 128, 256, 512)),
# cryptography symmetric ciphers
TestArgRange(TestCryptCipher, algorithms.AES, keybits=256, msgbits=symm_msg_bitsizes),
TestArgRange(TestCryptCipher, algorithms.TripleDES, keybits=64, msgbits=symm_msg_bitsizes),
TestArgRange(TestCryptCipher, algorithms.TripleDES, keybits=128, msgbits=symm_msg_bitsizes),
TestArgRange(TestCryptCipher, algorithms.TripleDES, keybits=192, msgbits=symm_msg_bitsizes),
TestArgRange(TestCryptCipher, algorithms.Blowfish, keybits=256, msgbits=symm_msg_bitsizes),
TestArgRange(TestCryptCipher, algorithms.CAST5, keybits=128, msgbits=symm_msg_bitsizes),
TestArgRange(TestCryptCipher, algorithms.ARC4, keybits=256, msgbits=symm_msg_bitsizes),
TestArgRange(TestCryptCipher, algorithms.IDEA, keybits=128, msgbits=symm_msg_bitsizes),
TestArgRange(TestCryptCipher, algorithms.SEED, keybits=128, msgbits=symm_msg_bitsizes),
TestArgRange(TestCryptCipher, algorithms.Camellia, keybits=256, msgbits=symm_msg_bitsizes),
# pycrypt symmetric ciphers
TestArgRange(TestPyCryptCipher, Crypto.Cipher.AES, keybits=256, msgbits=symm_msg_bitsizes),
TestArgRange(TestPyCryptCipher, Crypto.Cipher.DES, keybits=64, msgbits=symm_msg_bitsizes),
TestArgRange(TestPyCryptCipher, Crypto.Cipher.DES3, keybits=128, msgbits=symm_msg_bitsizes),
TestArgRange(TestPyCryptCipher, Crypto.Cipher.DES3, keybits=192, msgbits=symm_msg_bitsizes),
TestArgRange(TestPyCryptCipher, Crypto.Cipher.Blowfish, keybits=256, msgbits=symm_msg_bitsizes),
TestArgRange(TestPyCryptCipher, Crypto.Cipher.CAST, keybits=128, msgbits=symm_msg_bitsizes),
TestArgRange(TestPyCryptCipher, Crypto.Cipher.ARC4, keybits=256, msgbits=symm_msg_bitsizes),
TestArgRange(TestPyCryptCipher, Crypto.Cipher.XOR, keybits=256, msgbits=symm_msg_bitsizes),
TestArgRange(TestPyCryptCipher, Crypto.Cipher.ARC2, keybits=256, msgbits=symm_msg_bitsizes),
# cryptography hashes
TestArgRange(TestCryptHash, hashes.SHA224(), msgbits=hash_msg_bitsizes),
TestArgRange(TestCryptHash, hashes.SHA256(), msgbits=hash_msg_bitsizes),
TestArgRange(TestCryptHash, hashes.SHA384(), msgbits=hash_msg_bitsizes),
TestArgRange(TestCryptHash, hashes.SHA512(), msgbits=hash_msg_bitsizes),
TestArgRange(TestCryptHash, hashes.SHA1(), msgbits=hash_msg_bitsizes),
TestArgRange(TestCryptHash, hashes.MD5(), msgbits=hash_msg_bitsizes),
TestArgRange(TestCryptHash, hashes.RIPEMD160(), msgbits=hash_msg_bitsizes),
TestArgRange(TestCryptHash, hashes.Whirlpool(), msgbits=hash_msg_bitsizes),
# pycrypt hashes
TestArgRange(TestPyCryptHash, Crypto.Hash.SHA224, msgbits=hash_msg_bitsizes),
TestArgRange(TestPyCryptHash, Crypto.Hash.SHA256, msgbits=hash_msg_bitsizes),
TestArgRange(TestPyCryptHash, Crypto.Hash.SHA384, msgbits=hash_msg_bitsizes),
TestArgRange(TestPyCryptHash, Crypto.Hash.SHA512, msgbits=hash_msg_bitsizes),
TestArgRange(TestPyCryptHash, Crypto.Hash.SHA, msgbits=hash_msg_bitsizes),
TestArgRange(TestPyCryptHash, Crypto.Hash.MD5, msgbits=hash_msg_bitsizes),
TestArgRange(TestPyCryptHash, Crypto.Hash.RIPEMD, msgbits=hash_msg_bitsizes),
TestArgRange(TestPyCryptHash, Crypto.Hash.MD2, msgbits=hash_msg_bitsizes),
TestArgRange(TestPyCryptHash, Crypto.Hash.MD4, msgbits=hash_msg_bitsizes),
# cryptography rsa
TestArgRange(TestCryptRsa, modbits=(1024,2048,4096), msgbits=(2,256,512,1024)),
# PyCrypto rsa
TestArgRange(TestPyCryptRsa, modbits=(1024,2048,4096), msgbits=(2,256,512,1024)),
# now some tests to see how very simple operations perform
TestEval("pass"),
TestEval("1+2"),
TestEval("8*9"),
TestLambda(nop),
TestLambda(lambda: 1),
TestLambda(lambda: 1+2),
TestLambda(lambda: 1+2+3+4),
TestLambda(lambda: 8*9),
TestLambda(lambda: pow(1, 1, 2)),
# some elliptic curve crypto
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECT571R1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECT409R1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECT283R1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECT233R1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECT163R2),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECT571K1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECT409K1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECT283K1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECT233K1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECT163K1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECP521R1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECP384R1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECP256R1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECP256K1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECP224R1),
TestCryptEc(cryptography.hazmat.primitives.asymmetric.ec.SECP192R1),
]
def create_modexp_list():
return [
TestArgRange(TestModexp, modbits=(128,256,512,1024,2048,4096), msgbits=(2,256,512,1024,2048,4096), expbits=(2,16,128,256,512,1024,2048,4096)),
TestArgRange(TestGmpPowmod, modbits=(128,256,512,1024,2048,4096), msgbits=(2,256,512,1024,2048,4096), expbits=(2,16,128,256,512,1024,2048,4096)),
]
def main():
import argparse
parser = argparse.ArgumentParser(description='Crypto benchmark')
parser.add_argument('--modexp', action='store_true')
parser.add_argument('--baselinetest', type=float, default=0.005, help='How long to make the initial algorithm measurement')
parser.add_argument('--fulltest', type=float, default=0.1, help='How long to actually measure the algorithm')
args = parser.parse_args()
if args.modexp:
tests = create_modexp_list()
else:
tests = create_test_list()
for test in tests:
if type(test) == types.GeneratorType:
for item in test:
item.run(args.baselinetest, args.fulltest)
elif isinstance(test, Perftest):
test.run(args.baselinetest, args.fulltest)
else:
print("?", test)
if __name__ == '__main__':
main()