Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 1 | import binascii |
| 2 | import os |
| 3 | |
| 4 | import pytest |
| 5 | |
Alex Gaynor | a20631d | 2013-10-16 14:17:36 -0700 | [diff] [blame] | 6 | from cryptography.bindings import _ALL_APIS |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 7 | from cryptography.primitives.block import BlockCipher |
| 8 | |
| 9 | |
Alex Gaynor | 016eed1 | 2013-10-16 14:16:04 -0700 | [diff] [blame] | 10 | def generate_encrypt_test(param_loader, path, file_names, cipher_factory, |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 11 | mode_factory, only_if=lambda api: True, |
| 12 | skip_message=None): |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 13 | def test_encryption(self): |
Alex Gaynor | a20631d | 2013-10-16 14:17:36 -0700 | [diff] [blame] | 14 | for api in _ALL_APIS: |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 15 | for file_name in file_names: |
| 16 | for params in param_loader(os.path.join(path, file_name)): |
| 17 | yield ( |
| 18 | encrypt_test, |
| 19 | api, |
| 20 | cipher_factory, |
| 21 | mode_factory, |
| 22 | params, |
| 23 | only_if, |
| 24 | skip_message |
| 25 | ) |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 26 | return test_encryption |
| 27 | |
| 28 | |
Alex Gaynor | 512dd69 | 2013-10-16 14:27:52 -0700 | [diff] [blame] | 29 | def encrypt_test(api, cipher_factory, mode_factory, params, only_if, |
| 30 | skip_message): |
| 31 | if not only_if(api): |
| 32 | pytest.skip(skip_message) |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 33 | plaintext = params.pop("plaintext") |
| 34 | ciphertext = params.pop("ciphertext") |
| 35 | cipher = BlockCipher( |
| 36 | cipher_factory(**params), |
| 37 | mode_factory(**params), |
| 38 | api |
| 39 | ) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame^] | 40 | encryptor = cipher.encryptor() |
| 41 | actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext)) |
| 42 | actual_ciphertext += encryptor.finalize() |
Alex Gaynor | fb39b3f | 2013-10-16 14:30:59 -0700 | [diff] [blame] | 43 | assert actual_ciphertext == binascii.unhexlify(ciphertext) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame^] | 44 | decryptor = cipher.decryptor() |
| 45 | actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext)) |
| 46 | actual_plaintext += decryptor.finalize() |
| 47 | assert actual_plaintext == binascii.unhexlify(plaintext) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 48 | |
| 49 | |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 50 | def generate_hash_test(param_loader, path, file_names, hash_cls, |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 51 | only_if=None, skip_message=None): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 52 | def test_hash(self): |
| 53 | for api in _ALL_APIS: |
| 54 | for file_name in file_names: |
| 55 | for params in param_loader(os.path.join(path, file_name)): |
| 56 | yield ( |
| 57 | hash_test, |
| 58 | api, |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 59 | hash_cls, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 60 | params, |
| 61 | only_if, |
| 62 | skip_message |
| 63 | ) |
| 64 | return test_hash |
| 65 | |
| 66 | |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 67 | def hash_test(api, hash_cls, params, only_if, skip_message): |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 68 | if only_if is not None and not only_if(api): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 69 | pytest.skip(skip_message) |
| 70 | msg = params[0] |
| 71 | md = params[1] |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 72 | m = hash_cls(api=api) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 73 | m.update(binascii.unhexlify(msg)) |
| 74 | assert m.hexdigest() == md.replace(" ", "").lower() |
| 75 | |
| 76 | |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 77 | def generate_base_hash_test(hash_cls, digest_size, block_size, |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 78 | only_if=None, skip_message=None): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 79 | def test_base_hash(self): |
| 80 | for api in _ALL_APIS: |
| 81 | yield ( |
| 82 | base_hash_test, |
| 83 | api, |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 84 | hash_cls, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 85 | digest_size, |
| 86 | block_size, |
| 87 | only_if, |
| 88 | skip_message, |
| 89 | ) |
| 90 | return test_base_hash |
| 91 | |
| 92 | |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 93 | def base_hash_test(api, hash_cls, digest_size, block_size, only_if, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 94 | skip_message): |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 95 | if only_if is not None and not only_if(api): |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 96 | pytest.skip(skip_message) |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 97 | m = hash_cls(api=api) |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 98 | assert m.digest_size == digest_size |
| 99 | assert m.block_size == block_size |
| 100 | m_copy = m.copy() |
| 101 | assert m != m_copy |
| 102 | assert m._ctx != m_copy._ctx |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 103 | |
| 104 | |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 105 | def generate_long_string_hash_test(hash_factory, md, only_if=None, |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 106 | skip_message=None): |
| 107 | def test_long_string_hash(self): |
| 108 | for api in _ALL_APIS: |
| 109 | yield( |
| 110 | long_string_hash_test, |
| 111 | api, |
| 112 | hash_factory, |
| 113 | md, |
| 114 | only_if, |
| 115 | skip_message |
| 116 | ) |
| 117 | return test_long_string_hash |
| 118 | |
| 119 | |
| 120 | def long_string_hash_test(api, hash_factory, md, only_if, skip_message): |
Donald Stufft | 2acd77a | 2013-10-19 19:49:30 -0400 | [diff] [blame] | 121 | if only_if is not None and not only_if(api): |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 122 | pytest.skip(skip_message) |
| 123 | m = hash_factory(api) |
| 124 | m.update(b"a" * 1000000) |
| 125 | assert m.hexdigest() == md.lower() |