| 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 |
import time |
| 27 |
import os |
| 28 |
import sys |
| 29 |
|
| 30 |
from Crypto.PublicKey import RSA |
| 31 |
from Crypto.Cipher import PKCS1_OAEP, PKCS1_v1_5 as RSAES_PKCS1_v1_5 |
| 32 |
from Crypto.Signature import PKCS1_PSS, PKCS1_v1_5 as RSASSA_PKCS1_v1_5 |
| 33 |
from Crypto.Cipher import (AES, ARC2, ARC4, Blowfish, CAST, DES3, DES, |
| 34 |
Salsa20, ChaCha20) |
| 35 |
from Crypto.Hash import (HMAC, MD2, MD4, MD5, SHA224, SHA256, SHA384, SHA512, |
| 36 |
CMAC, SHA3_224, SHA3_256, SHA3_384, SHA3_512, |
| 37 |
BLAKE2b, BLAKE2s) |
| 38 |
from Crypto.Random import get_random_bytes |
| 39 |
import Crypto.Util.Counter |
| 40 |
from Crypto.Util.number import bytes_to_long |
| 41 |
try: |
| 42 |
from Crypto.Hash import SHA1 |
| 43 |
except ImportError: |
| 44 |
|
| 45 |
from Crypto.Hash import SHA as SHA1 |
| 46 |
try: |
| 47 |
from Crypto.Hash import RIPEMD160 |
| 48 |
except ImportError: |
| 49 |
|
| 50 |
try: |
| 51 |
from Crypto.Hash import RIPEMD as RIPEMD160 |
| 52 |
except ImportError: |
| 53 |
|
| 54 |
RIPEMD160 = None |
| 55 |
|
| 56 |
try: |
| 57 |
import hashlib |
| 58 |
import hmac |
| 59 |
except ImportError: |
| 60 |
hashlib = hmac = None |
| 61 |
|
| 62 |
from Crypto.Random import random as pycrypto_random |
| 63 |
import random as stdlib_random |
| 64 |
|
| 65 |
class BLAKE2b_512(object): |
| 66 |
digest_size = 512 |
| 67 |
@staticmethod |
| 68 |
def new(data=None): |
| 69 |
return BLAKE2b.new(digest_bits=512, data=data) |
| 70 |
|
| 71 |
class BLAKE2s_256(object): |
| 72 |
digest_size = 256 |
| 73 |
@staticmethod |
| 74 |
def new(data=None): |
| 75 |
return BLAKE2s.new(digest_bits=256, data=data) |
| 76 |
|
| 77 |
class ChaCha20_old_style(object): |
| 78 |
@staticmethod |
| 79 |
def new(key, nonce): |
| 80 |
return ChaCha20.new(key=key, nonce=nonce) |
| 81 |
|
| 82 |
class ModeNotAvailable(ValueError): |
| 83 |
pass |
| 84 |
|
| 85 |
rng = get_random_bytes |
| 86 |
|
| 87 |
class Benchmark: |
| 88 |
|
| 89 |
def __init__(self): |
| 90 |
self.__random_data = None |
| 91 |
|
| 92 |
def random_keys(self, bytes, n=10**5): |
| 93 |
"""Return random keys of the specified number of bytes. |
| 94 |
If this function has been called before with the same number of bytes, |
| 95 |
cached keys are used instead of randomly generating new ones. |
| 96 |
""" |
| 97 |
return self.random_blocks(bytes, n) |
| 98 |
|
| 99 |
def random_blocks(self, bytes_per_block, blocks): |
| 100 |
bytes = bytes_per_block * blocks |
| 101 |
data = self.random_data(bytes) |
| 102 |
retval = [] |
| 103 |
for i in range(blocks): |
| 104 |
p = i * bytes_per_block |
| 105 |
retval.append(data[p:p+bytes_per_block]) |
| 106 |
return retval |
| 107 |
|
| 108 |
def random_data(self, bytes): |
| 109 |
if self.__random_data is None: |
| 110 |
self.__random_data = self._random_bytes(bytes) |
| 111 |
return self.__random_data |
| 112 |
elif bytes == len(self.__random_data): |
| 113 |
return self.__random_data |
| 114 |
elif bytes < len(self.__random_data): |
| 115 |
return self.__random_data[:bytes] |
| 116 |
else: |
| 117 |
self.__random_data += self._random_bytes(bytes - len(self.__random_data)) |
| 118 |
return self.__random_data |
| 119 |
|
| 120 |
def _random_bytes(self, b): |
| 121 |
return os.urandom(b) |
| 122 |
|
| 123 |
def announce_start(self, test_name): |
| 124 |
sys.stdout.write("%s: " % (test_name,)) |
| 125 |
sys.stdout.flush() |
| 126 |
|
| 127 |
def announce_result(self, value, units): |
| 128 |
sys.stdout.write("%.2f %s\n" % (value, units)) |
| 129 |
sys.stdout.flush() |
| 130 |
|
| 131 |
def test_random_module(self, module_name, module): |
| 132 |
self.announce_start("%s.choice" % (module_name,)) |
| 133 |
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
| 134 |
t0 = time.perf_counter() |
| 135 |
for i in range(5000): |
| 136 |
module.choice(alphabet) |
| 137 |
t = time.perf_counter() |
| 138 |
invocations_per_second = 5000 / (t - t0) |
| 139 |
self.announce_result(invocations_per_second, "invocations/sec") |
| 140 |
|
| 141 |
def test_pubkey_setup(self, pubkey_name, module, key_bytes): |
| 142 |
self.announce_start("%s pubkey setup" % (pubkey_name,)) |
| 143 |
keys = self.random_keys(key_bytes)[:5] |
| 144 |
|
| 145 |
t0 = time.perf_counter() |
| 146 |
for k in keys: |
| 147 |
module.generate(key_bytes*8) |
| 148 |
t = time.perf_counter() |
| 149 |
pubkey_setups_per_second = len(keys) / (t - t0) |
| 150 |
self.announce_result(pubkey_setups_per_second, "Keys/sec") |
| 151 |
|
| 152 |
def test_key_setup(self, cipher_name, module, key_bytes, params): |
| 153 |
self.generate_cipher(module, key_bytes, params) |
| 154 |
self.announce_start("%s key setup" % (cipher_name,)) |
| 155 |
|
| 156 |
for x in range(5000): |
| 157 |
t0 = time.perf_counter() |
| 158 |
self.generate_cipher(module, key_bytes, params) |
| 159 |
t = time.perf_counter() |
| 160 |
|
| 161 |
key_setups_per_second = 5000 / (t - t0) |
| 162 |
self.announce_result(key_setups_per_second/1000, "kKeys/sec") |
| 163 |
|
| 164 |
def test_encryption(self, cipher_name, module, key_bytes, params): |
| 165 |
self.announce_start("%s encryption" % (cipher_name,)) |
| 166 |
|
| 167 |
pt_size = 16384000 |
| 168 |
pt = rng(pt_size) |
| 169 |
cipher = self.generate_cipher(module, key_bytes, params) |
| 170 |
|
| 171 |
params_dict = dict([param.split('=') for param in params.split()]) |
| 172 |
|
| 173 |
|
| 174 |
if params_dict.get('mode') == 'MODE_SIV': |
| 175 |
t0 = time.perf_counter() |
| 176 |
cipher.encrypt_and_digest(pt) |
| 177 |
t = time.perf_counter() |
| 178 |
else: |
| 179 |
t0 = time.perf_counter() |
| 180 |
cipher.encrypt(pt) |
| 181 |
t = time.perf_counter() |
| 182 |
|
| 183 |
encryption_speed = pt_size / (t - t0) |
| 184 |
self.announce_result(encryption_speed / 10**6, "MBps") |
| 185 |
|
| 186 |
def test_hash_small(self, hash_name, hash_constructor, digest_size): |
| 187 |
self.announce_start("%s (%d-byte inputs)" % (hash_name, digest_size)) |
| 188 |
|
| 189 |
blocks = self.random_blocks(digest_size, 10000) |
| 190 |
|
| 191 |
|
| 192 |
t0 = time.perf_counter() |
| 193 |
for b in blocks: |
| 194 |
hash_constructor(b).digest() |
| 195 |
t = time.perf_counter() |
| 196 |
|
| 197 |
hashes_per_second = len(blocks) / (t - t0) |
| 198 |
self.announce_result(hashes_per_second / 1000, "kHashes/sec") |
| 199 |
|
| 200 |
def test_hash_large(self, hash_name, hash_constructor, digest_size): |
| 201 |
self.announce_start("%s (single large input)" % (hash_name,)) |
| 202 |
|
| 203 |
blocks = self.random_blocks(16384, 10000) |
| 204 |
|
| 205 |
|
| 206 |
t0 = time.perf_counter() |
| 207 |
h = hash_constructor() |
| 208 |
for b in blocks: |
| 209 |
h.update(b) |
| 210 |
h.digest() |
| 211 |
t = time.perf_counter() |
| 212 |
|
| 213 |
hash_speed = len(blocks) * len(blocks[0]) / (t - t0) |
| 214 |
self.announce_result(hash_speed / 10**6, "MBps") |
| 215 |
|
| 216 |
def test_hmac_small(self, mac_name, hmac_constructor, digestmod, digest_size): |
| 217 |
keys = iter(self.random_keys(digest_size)) |
| 218 |
if sys.version_info[0] == 2: |
| 219 |
mac_constructor = lambda data=None: hmac_constructor(keys.next(), data, digestmod) |
| 220 |
else: |
| 221 |
mac_constructor = lambda data=None: hmac_constructor(keys.__next__(), data, digestmod) |
| 222 |
self.test_hash_small(mac_name, mac_constructor, digest_size) |
| 223 |
|
| 224 |
def test_hmac_large(self, mac_name, hmac_constructor, digestmod, digest_size): |
| 225 |
key = self.random_keys(digest_size)[0] |
| 226 |
mac_constructor = lambda data=None: hmac_constructor(key, data, digestmod) |
| 227 |
self.test_hash_large(mac_name, mac_constructor, digest_size) |
| 228 |
|
| 229 |
def test_cmac_small(self, mac_name, cmac_constructor, ciphermod, key_size): |
| 230 |
keys = iter(self.random_keys(key_size)) |
| 231 |
if sys.version_info[0] == 2: |
| 232 |
mac_constructor = lambda data=None: cmac_constructor(keys.next(), data, ciphermod) |
| 233 |
else: |
| 234 |
mac_constructor = lambda data=None: cmac_constructor(keys.__next__(), data, ciphermod) |
| 235 |
self.test_hash_small(mac_name, mac_constructor, ciphermod.block_size) |
| 236 |
|
| 237 |
def test_cmac_large(self, mac_name, cmac_constructor, ciphermod, key_size): |
| 238 |
key = self.random_keys(key_size)[0] |
| 239 |
mac_constructor = lambda data=None: cmac_constructor(key, data, ciphermod) |
| 240 |
self.test_hash_large(mac_name, mac_constructor, ciphermod.block_size) |
| 241 |
|
| 242 |
def test_pkcs1_sign(self, scheme_name, scheme_constructor, hash_name, hash_constructor, digest_size): |
| 243 |
self.announce_start("%s signing %s (%d-byte inputs)" % (scheme_name, hash_name, digest_size)) |
| 244 |
|
| 245 |
|
| 246 |
k = RSA.generate(2048) |
| 247 |
sigscheme = scheme_constructor(k) |
| 248 |
|
| 249 |
|
| 250 |
blocks = self.random_blocks(digest_size, 50) |
| 251 |
hashes = [] |
| 252 |
for b in blocks: |
| 253 |
hashes.append(hash_constructor(b)) |
| 254 |
|
| 255 |
|
| 256 |
t0 = time.perf_counter() |
| 257 |
for h in hashes: |
| 258 |
sigscheme.sign(h) |
| 259 |
t = time.perf_counter() |
| 260 |
|
| 261 |
speed = len(hashes) / (t - t0) |
| 262 |
self.announce_result(speed, "sigs/sec") |
| 263 |
|
| 264 |
def test_pkcs1_verify(self, scheme_name, scheme_constructor, hash_name, hash_constructor, digest_size): |
| 265 |
self.announce_start("%s verification %s (%d-byte inputs)" % (scheme_name, hash_name, digest_size)) |
| 266 |
|
| 267 |
|
| 268 |
k = RSA.generate(2048) |
| 269 |
sigscheme = scheme_constructor(k) |
| 270 |
|
| 271 |
|
| 272 |
blocks = self.random_blocks(digest_size, 50) |
| 273 |
hashes = [] |
| 274 |
for b in blocks: |
| 275 |
hashes.append(hash_constructor(b)) |
| 276 |
|
| 277 |
|
| 278 |
signatures = [] |
| 279 |
for h in hashes: |
| 280 |
signatures.append(sigscheme.sign(h)) |
| 281 |
|
| 282 |
|
| 283 |
hashes = hashes + hashes |
| 284 |
signatures = signatures + signatures |
| 285 |
|
| 286 |
|
| 287 |
t0 = time.perf_counter() |
| 288 |
for h, s in zip(hashes, signatures): |
| 289 |
sigscheme.verify(h, s) |
| 290 |
t = time.perf_counter() |
| 291 |
|
| 292 |
speed = len(hashes) / (t - t0) |
| 293 |
self.announce_result(speed, "sigs/sec") |
| 294 |
|
| 295 |
|
| 296 |
def generate_cipher(self, module, key_size, params): |
| 297 |
params_dict = {} |
| 298 |
if params: |
| 299 |
params_dict = dict([x.split("=") for x in params.split(" ")]) |
| 300 |
|
| 301 |
gen_tuple = [] |
| 302 |
gen_dict = {} |
| 303 |
|
| 304 |
|
| 305 |
if params_dict.get('ks') == "x2": |
| 306 |
key = rng(2 * key_size) |
| 307 |
else: |
| 308 |
key = rng(key_size) |
| 309 |
gen_tuple.append(key) |
| 310 |
|
| 311 |
|
| 312 |
mode = params_dict.get("mode") |
| 313 |
if mode: |
| 314 |
mode_value = getattr(module, mode, None) |
| 315 |
if mode_value is None: |
| 316 |
|
| 317 |
raise ModeNotAvailable() |
| 318 |
gen_tuple.append(getattr(module, mode)) |
| 319 |
|
| 320 |
|
| 321 |
iv_length = params_dict.get("iv") |
| 322 |
if iv_length is None: |
| 323 |
iv_length = params_dict.get("nonce") |
| 324 |
if iv_length: |
| 325 |
if iv_length == "bs": |
| 326 |
iv_length = module.block_size |
| 327 |
iv = rng(int(iv_length)) |
| 328 |
gen_tuple.append(iv) |
| 329 |
|
| 330 |
|
| 331 |
le = params_dict.get("little_endian") |
| 332 |
if le: |
| 333 |
if le == "True": |
| 334 |
le = True |
| 335 |
else: |
| 336 |
le = False |
| 337 |
|
| 338 |
|
| 339 |
gen_tuple = gen_tuple[:-1] |
| 340 |
ctr = Crypto.Util.Counter.new(module.block_size*8, |
| 341 |
initial_value=bytes_to_long(iv), |
| 342 |
little_endian=le, |
| 343 |
allow_wraparound=True) |
| 344 |
gen_dict['counter'] = ctr |
| 345 |
|
| 346 |
|
| 347 |
return module.new(*gen_tuple, **gen_dict) |
| 348 |
|
| 349 |
def run(self): |
| 350 |
pubkey_specs = [ |
| 351 |
("RSA(1024)", RSA, int(1024/8)), |
| 352 |
("RSA(2048)", RSA, int(2048/8)), |
| 353 |
("RSA(4096)", RSA, int(4096/8)), |
| 354 |
] |
| 355 |
block_cipher_modes = [ |
| 356 |
|
| 357 |
("CBC", True, "mode=MODE_CBC iv=bs"), |
| 358 |
("CFB-8", False, "mode=MODE_CFB iv=bs"), |
| 359 |
("OFB", False, "mode=MODE_OFB iv=bs"), |
| 360 |
("ECB", False, "mode=MODE_ECB"), |
| 361 |
("CTR-LE", True, "mode=MODE_CTR iv=bs little_endian=True"), |
| 362 |
("CTR-BE", False, "mode=MODE_CTR iv=bs little_endian=False"), |
| 363 |
("OPENPGP", False, "mode=MODE_OPENPGP iv=bs"), |
| 364 |
("CCM", True, "mode=MODE_CCM nonce=12"), |
| 365 |
("GCM", True, "mode=MODE_GCM nonce=16"), |
| 366 |
("EAX", True, "mode=MODE_EAX nonce=16"), |
| 367 |
("SIV", True, "mode=MODE_SIV ks=x2 nonce=16"), |
| 368 |
("OCB", True, "mode=MODE_OCB nonce=15"), |
| 369 |
] |
| 370 |
block_specs = [ |
| 371 |
|
| 372 |
("DES", DES, 8), |
| 373 |
("DES3", DES3, 24), |
| 374 |
("AES128", AES, 16), |
| 375 |
("AES192", AES, 24), |
| 376 |
("AES256", AES, 32), |
| 377 |
("Blowfish(256)", Blowfish, 32), |
| 378 |
("CAST(128)", CAST, 16), |
| 379 |
("ARC2(128)", ARC2, 16), |
| 380 |
] |
| 381 |
stream_specs = [ |
| 382 |
|
| 383 |
("ARC4(128)", ARC4, 16, 0), |
| 384 |
("Salsa20(16)", Salsa20, 16, 8), |
| 385 |
("Salsa20(32)", Salsa20, 32, 8), |
| 386 |
("ChaCha20", ChaCha20_old_style, 32, 8), |
| 387 |
] |
| 388 |
hash_specs = [ |
| 389 |
("MD2", MD2), |
| 390 |
("MD4", MD4), |
| 391 |
("MD5", MD5), |
| 392 |
("SHA1", SHA1), |
| 393 |
("SHA224", SHA224), |
| 394 |
("SHA256", SHA256), |
| 395 |
("SHA384", SHA384), |
| 396 |
("SHA512", SHA512), |
| 397 |
("SHA3_224", SHA3_224), |
| 398 |
("SHA3_256", SHA3_256), |
| 399 |
("SHA3_384", SHA3_384), |
| 400 |
("SHA3_512", SHA3_512), |
| 401 |
("BLAKE2b", BLAKE2b_512), |
| 402 |
("BLAKE2s", BLAKE2s_256), |
| 403 |
] |
| 404 |
if RIPEMD160 is not None: |
| 405 |
hash_specs += [("RIPEMD160", RIPEMD160)] |
| 406 |
|
| 407 |
hashlib_specs = [] |
| 408 |
if hashlib is not None: |
| 409 |
if hasattr(hashlib, 'md5'): hashlib_specs.append(("hashlib.md5", hashlib.md5)) |
| 410 |
if hasattr(hashlib, 'sha1'): hashlib_specs.append(("hashlib.sha1", hashlib.sha1)) |
| 411 |
if hasattr(hashlib, 'sha224'): hashlib_specs.append(("hashlib.sha224", hashlib.sha224)) |
| 412 |
if hasattr(hashlib, 'sha256'): hashlib_specs.append(("hashlib.sha256", hashlib.sha256)) |
| 413 |
if hasattr(hashlib, 'sha384'): hashlib_specs.append(("hashlib.sha384", hashlib.sha384)) |
| 414 |
if hasattr(hashlib, 'sha512'): hashlib_specs.append(("hashlib.sha512", hashlib.sha512)) |
| 415 |
|
| 416 |
|
| 417 |
self.test_random_module("stdlib random", stdlib_random) |
| 418 |
|
| 419 |
|
| 420 |
self.test_random_module("Crypto.Random.random", pycrypto_random) |
| 421 |
|
| 422 |
|
| 423 |
for pubkey_name, module, key_bytes in pubkey_specs: |
| 424 |
self.test_pubkey_setup(pubkey_name, module, key_bytes) |
| 425 |
|
| 426 |
|
| 427 |
for cipher_name, module, key_bytes in block_specs: |
| 428 |
|
| 429 |
|
| 430 |
for mode_name, test_ks, params in block_cipher_modes: |
| 431 |
|
| 432 |
mode_text = "%s-%s" % (cipher_name, mode_name) |
| 433 |
try: |
| 434 |
if test_ks: |
| 435 |
self.test_key_setup(mode_text, module, key_bytes, params) |
| 436 |
self.test_encryption(mode_text, module, key_bytes, params) |
| 437 |
except ModeNotAvailable as e: |
| 438 |
pass |
| 439 |
|
| 440 |
|
| 441 |
for cipher_name, module, key_bytes, nonce_bytes in stream_specs: |
| 442 |
params = "" |
| 443 |
if nonce_bytes: |
| 444 |
params = "nonce=" + str(nonce_bytes) |
| 445 |
self.test_key_setup(cipher_name, module, key_bytes, params) |
| 446 |
self.test_encryption(cipher_name, module, key_bytes, params) |
| 447 |
|
| 448 |
|
| 449 |
for hash_name, module in hash_specs: |
| 450 |
self.test_hash_small(hash_name, module.new, module.digest_size) |
| 451 |
self.test_hash_large(hash_name, module.new, module.digest_size) |
| 452 |
|
| 453 |
|
| 454 |
for hash_name, func in hashlib_specs: |
| 455 |
self.test_hash_small(hash_name, func, func().digest_size) |
| 456 |
self.test_hash_large(hash_name, func, func().digest_size) |
| 457 |
|
| 458 |
|
| 459 |
for hash_name, module in hash_specs: |
| 460 |
if not hasattr(module, "block_size"): |
| 461 |
continue |
| 462 |
self.test_hmac_small("HMAC-"+hash_name, HMAC.new, module, module.digest_size) |
| 463 |
self.test_hmac_large("HMAC-"+hash_name, HMAC.new, module, module.digest_size) |
| 464 |
|
| 465 |
|
| 466 |
for hash_name, func in hashlib_specs: |
| 467 |
if not hasattr(module, "block_size"): |
| 468 |
continue |
| 469 |
self.test_hmac_small("hmac+"+hash_name, hmac.HMAC, func, func().digest_size) |
| 470 |
self.test_hmac_large("hmac+"+hash_name, hmac.HMAC, func, func().digest_size) |
| 471 |
|
| 472 |
|
| 473 |
for cipher_name, module, key_size in (("AES128", AES, 16),): |
| 474 |
self.test_cmac_small(cipher_name+"-CMAC", CMAC.new, module, key_size) |
| 475 |
self.test_cmac_large(cipher_name+"-CMAC", CMAC.new, module, key_size) |
| 476 |
|
| 477 |
|
| 478 |
for hash_name, module in hash_specs: |
| 479 |
self.test_pkcs1_sign("PKCS#1-v1.5", RSASSA_PKCS1_v1_5.new, hash_name, module.new, module.digest_size) |
| 480 |
|
| 481 |
|
| 482 |
for hash_name, module in hash_specs: |
| 483 |
self.test_pkcs1_sign("PKCS#1-PSS", PKCS1_PSS.new, hash_name, module.new, module.digest_size) |
| 484 |
|
| 485 |
|
| 486 |
for hash_name, module in hash_specs: |
| 487 |
self.test_pkcs1_verify("PKCS#1-v1.5", RSASSA_PKCS1_v1_5.new, hash_name, module.new, module.digest_size) |
| 488 |
|
| 489 |
|
| 490 |
for hash_name, module in hash_specs: |
| 491 |
self.test_pkcs1_verify("PKCS#1-PSS", PKCS1_PSS.new, hash_name, module.new, module.digest_size) |
| 492 |
|
| 493 |
if __name__ == '__main__': |
| 494 |
Benchmark().run() |
| 495 |
|
| 496 |
|