Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | # you may not use this file except in compliance with the License. |
| 3 | # You may obtain a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 10 | # implied. |
| 11 | # See the License for the specific language governing permissions and |
| 12 | # limitations under the License. |
| 13 | |
| 14 | from __future__ import absolute_import, division, print_function |
| 15 | |
Paul Kehrer | 7a2a191 | 2013-10-20 13:45:23 -0500 | [diff] [blame] | 16 | import pretend |
| 17 | |
Paul Kehrer | d4cb34d | 2013-10-19 23:05:12 -0500 | [diff] [blame] | 18 | import pytest |
| 19 | |
| 20 | import six |
| 21 | |
Paul Kehrer | f4c5976 | 2013-10-22 20:26:24 -0500 | [diff] [blame] | 22 | from cryptography.bindings import _default_backend |
Paul Kehrer | daac6d0 | 2013-10-20 13:15:28 -0500 | [diff] [blame] | 23 | |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 24 | from cryptography.primitives import hashes |
| 25 | |
| 26 | from .utils import generate_base_hash_test |
| 27 | |
| 28 | |
Paul Kehrer | d4cb34d | 2013-10-19 23:05:12 -0500 | [diff] [blame] | 29 | class TestBaseHash(object): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 30 | def test_base_hash_reject_unicode(self, backend): |
| 31 | m = hashes.SHA1(backend=backend) |
Paul Kehrer | d4cb34d | 2013-10-19 23:05:12 -0500 | [diff] [blame] | 32 | with pytest.raises(TypeError): |
| 33 | m.update(six.u("\u00FC")) |
| 34 | |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame] | 35 | def test_base_hash_hexdigest_string_type(self, backend): |
| 36 | m = hashes.SHA1(backend=backend, data=b"") |
Paul Kehrer | 0b2042b | 2013-10-22 14:28:24 -0500 | [diff] [blame] | 37 | assert isinstance(m.hexdigest(), str) |
| 38 | |
Paul Kehrer | d4cb34d | 2013-10-19 23:05:12 -0500 | [diff] [blame] | 39 | |
Paul Kehrer | 7a2a191 | 2013-10-20 13:45:23 -0500 | [diff] [blame] | 40 | class TestCopyHash(object): |
Paul Kehrer | f4c5976 | 2013-10-22 20:26:24 -0500 | [diff] [blame] | 41 | def test_copy_backend_object(self): |
Paul Kehrer | 14c3a35 | 2013-10-22 21:45:16 -0500 | [diff] [blame] | 42 | pretend_hashes = pretend.stub(copy_ctx=lambda a: "copiedctx") |
| 43 | pretend_backend = pretend.stub(hashes=pretend_hashes) |
Paul Kehrer | 7a2a191 | 2013-10-20 13:45:23 -0500 | [diff] [blame] | 44 | pretend_ctx = pretend.stub() |
Paul Kehrer | f4c5976 | 2013-10-22 20:26:24 -0500 | [diff] [blame] | 45 | h = hashes.SHA1(backend=pretend_backend, ctx=pretend_ctx) |
| 46 | assert h._backend is pretend_backend |
| 47 | assert h.copy()._backend is h._backend |
Paul Kehrer | 7a2a191 | 2013-10-20 13:45:23 -0500 | [diff] [blame] | 48 | |
| 49 | |
Paul Kehrer | 14c3a35 | 2013-10-22 21:45:16 -0500 | [diff] [blame] | 50 | class TestDefaultBackendSHA1(object): |
Paul Kehrer | f4c5976 | 2013-10-22 20:26:24 -0500 | [diff] [blame] | 51 | def test_default_backend_creation(self): |
Paul Kehrer | daac6d0 | 2013-10-20 13:15:28 -0500 | [diff] [blame] | 52 | """ |
Paul Kehrer | 14c3a35 | 2013-10-22 21:45:16 -0500 | [diff] [blame] | 53 | This test assumes the presence of SHA1 in the default backend. |
Paul Kehrer | daac6d0 | 2013-10-20 13:15:28 -0500 | [diff] [blame] | 54 | """ |
| 55 | h = hashes.SHA1() |
Paul Kehrer | f4c5976 | 2013-10-22 20:26:24 -0500 | [diff] [blame] | 56 | assert h._backend is _default_backend |
Paul Kehrer | daac6d0 | 2013-10-20 13:15:28 -0500 | [diff] [blame] | 57 | |
| 58 | |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 59 | class TestSHA1(object): |
| 60 | test_SHA1 = generate_base_hash_test( |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 61 | hashes.SHA1, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 62 | digest_size=20, |
| 63 | block_size=64, |
Paul Kehrer | 14c3a35 | 2013-10-22 21:45:16 -0500 | [diff] [blame] | 64 | only_if=lambda backend: backend.hashes.supported(hashes.SHA1), |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 65 | skip_message="Does not support SHA1", |
| 66 | ) |
Paul Kehrer | 7e5697c | 2013-10-18 21:07:36 -0500 | [diff] [blame] | 67 | |
| 68 | |
| 69 | class TestSHA224(object): |
| 70 | test_SHA224 = generate_base_hash_test( |
| 71 | hashes.SHA224, |
| 72 | digest_size=28, |
| 73 | block_size=64, |
Paul Kehrer | 14c3a35 | 2013-10-22 21:45:16 -0500 | [diff] [blame] | 74 | only_if=lambda backend: backend.hashes.supported(hashes.SHA224), |
Paul Kehrer | 7e5697c | 2013-10-18 21:07:36 -0500 | [diff] [blame] | 75 | skip_message="Does not support SHA224", |
| 76 | ) |
| 77 | |
| 78 | |
| 79 | class TestSHA256(object): |
| 80 | test_SHA256 = generate_base_hash_test( |
| 81 | hashes.SHA256, |
| 82 | digest_size=32, |
| 83 | block_size=64, |
Paul Kehrer | 14c3a35 | 2013-10-22 21:45:16 -0500 | [diff] [blame] | 84 | only_if=lambda backend: backend.hashes.supported(hashes.SHA256), |
Paul Kehrer | 7e5697c | 2013-10-18 21:07:36 -0500 | [diff] [blame] | 85 | skip_message="Does not support SHA256", |
| 86 | ) |
| 87 | |
| 88 | |
| 89 | class TestSHA384(object): |
| 90 | test_SHA384 = generate_base_hash_test( |
| 91 | hashes.SHA384, |
| 92 | digest_size=48, |
| 93 | block_size=128, |
Paul Kehrer | 14c3a35 | 2013-10-22 21:45:16 -0500 | [diff] [blame] | 94 | only_if=lambda backend: backend.hashes.supported(hashes.SHA384), |
Paul Kehrer | 7e5697c | 2013-10-18 21:07:36 -0500 | [diff] [blame] | 95 | skip_message="Does not support SHA384", |
| 96 | ) |
| 97 | |
| 98 | |
| 99 | class TestSHA512(object): |
| 100 | test_SHA512 = generate_base_hash_test( |
| 101 | hashes.SHA512, |
| 102 | digest_size=64, |
| 103 | block_size=128, |
Paul Kehrer | 14c3a35 | 2013-10-22 21:45:16 -0500 | [diff] [blame] | 104 | only_if=lambda backend: backend.hashes.supported(hashes.SHA512), |
Paul Kehrer | 7e5697c | 2013-10-18 21:07:36 -0500 | [diff] [blame] | 105 | skip_message="Does not support SHA512", |
| 106 | ) |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 107 | |
| 108 | |
| 109 | class TestRIPEMD160(object): |
| 110 | test_RIPEMD160 = generate_base_hash_test( |
| 111 | hashes.RIPEMD160, |
| 112 | digest_size=20, |
| 113 | block_size=64, |
Paul Kehrer | 14c3a35 | 2013-10-22 21:45:16 -0500 | [diff] [blame] | 114 | only_if=lambda backend: backend.hashes.supported(hashes.RIPEMD160), |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 115 | skip_message="Does not support RIPEMD160", |
| 116 | ) |
Paul Kehrer | 79ff8bf | 2013-10-18 22:07:29 -0500 | [diff] [blame] | 117 | |
| 118 | |
| 119 | class TestWhirlpool(object): |
| 120 | test_Whirlpool = generate_base_hash_test( |
| 121 | hashes.Whirlpool, |
| 122 | digest_size=64, |
| 123 | block_size=64, |
Paul Kehrer | 14c3a35 | 2013-10-22 21:45:16 -0500 | [diff] [blame] | 124 | only_if=lambda backend: backend.hashes.supported(hashes.Whirlpool), |
Paul Kehrer | 79ff8bf | 2013-10-18 22:07:29 -0500 | [diff] [blame] | 125 | skip_message="Does not support Whirlpool", |
| 126 | ) |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 127 | |
| 128 | |
| 129 | class TestMD5(object): |
| 130 | test_MD5 = generate_base_hash_test( |
| 131 | hashes.MD5, |
| 132 | digest_size=16, |
| 133 | block_size=64, |
Paul Kehrer | 14c3a35 | 2013-10-22 21:45:16 -0500 | [diff] [blame] | 134 | only_if=lambda backend: backend.hashes.supported(hashes.MD5), |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 135 | skip_message="Does not support MD5", |
| 136 | ) |