blob: 174fd5f44545d661d83c709978c6441a945a0e4f [file] [log] [blame]
Paul Kehrerbde6fb52013-10-18 18:08:49 -05001# 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
14from __future__ import absolute_import, division, print_function
15
Paul Kehrer7a2a1912013-10-20 13:45:23 -050016import pretend
17
Paul Kehrerd4cb34d2013-10-19 23:05:12 -050018import pytest
19
20import six
21
Paul Kehrerf4c59762013-10-22 20:26:24 -050022from cryptography.bindings import _default_backend
Paul Kehrerdaac6d02013-10-20 13:15:28 -050023
Paul Kehrerbde6fb52013-10-18 18:08:49 -050024from cryptography.primitives import hashes
25
26from .utils import generate_base_hash_test
27
28
Paul Kehrerd4cb34d2013-10-19 23:05:12 -050029class TestBaseHash(object):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050030 def test_base_hash_reject_unicode(self, backend):
31 m = hashes.SHA1(backend=backend)
Paul Kehrerd4cb34d2013-10-19 23:05:12 -050032 with pytest.raises(TypeError):
33 m.update(six.u("\u00FC"))
34
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050035 def test_base_hash_hexdigest_string_type(self, backend):
36 m = hashes.SHA1(backend=backend, data=b"")
Paul Kehrer0b2042b2013-10-22 14:28:24 -050037 assert isinstance(m.hexdigest(), str)
38
Paul Kehrerd4cb34d2013-10-19 23:05:12 -050039
Paul Kehrer7a2a1912013-10-20 13:45:23 -050040class TestCopyHash(object):
Paul Kehrerf4c59762013-10-22 20:26:24 -050041 def test_copy_backend_object(self):
Paul Kehrer14c3a352013-10-22 21:45:16 -050042 pretend_hashes = pretend.stub(copy_ctx=lambda a: "copiedctx")
43 pretend_backend = pretend.stub(hashes=pretend_hashes)
Paul Kehrer7a2a1912013-10-20 13:45:23 -050044 pretend_ctx = pretend.stub()
Paul Kehrerf4c59762013-10-22 20:26:24 -050045 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 Kehrer7a2a1912013-10-20 13:45:23 -050048
49
Paul Kehrer14c3a352013-10-22 21:45:16 -050050class TestDefaultBackendSHA1(object):
Paul Kehrerf4c59762013-10-22 20:26:24 -050051 def test_default_backend_creation(self):
Paul Kehrerdaac6d02013-10-20 13:15:28 -050052 """
Paul Kehrer14c3a352013-10-22 21:45:16 -050053 This test assumes the presence of SHA1 in the default backend.
Paul Kehrerdaac6d02013-10-20 13:15:28 -050054 """
55 h = hashes.SHA1()
Paul Kehrerf4c59762013-10-22 20:26:24 -050056 assert h._backend is _default_backend
Paul Kehrerdaac6d02013-10-20 13:15:28 -050057
58
Paul Kehrerbde6fb52013-10-18 18:08:49 -050059class TestSHA1(object):
60 test_SHA1 = generate_base_hash_test(
Paul Kehrerbb069c22013-10-18 19:51:01 -050061 hashes.SHA1,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050062 digest_size=20,
63 block_size=64,
Paul Kehrer14c3a352013-10-22 21:45:16 -050064 only_if=lambda backend: backend.hashes.supported(hashes.SHA1),
Paul Kehrerbde6fb52013-10-18 18:08:49 -050065 skip_message="Does not support SHA1",
66 )
Paul Kehrer7e5697c2013-10-18 21:07:36 -050067
68
69class TestSHA224(object):
70 test_SHA224 = generate_base_hash_test(
71 hashes.SHA224,
72 digest_size=28,
73 block_size=64,
Paul Kehrer14c3a352013-10-22 21:45:16 -050074 only_if=lambda backend: backend.hashes.supported(hashes.SHA224),
Paul Kehrer7e5697c2013-10-18 21:07:36 -050075 skip_message="Does not support SHA224",
76 )
77
78
79class TestSHA256(object):
80 test_SHA256 = generate_base_hash_test(
81 hashes.SHA256,
82 digest_size=32,
83 block_size=64,
Paul Kehrer14c3a352013-10-22 21:45:16 -050084 only_if=lambda backend: backend.hashes.supported(hashes.SHA256),
Paul Kehrer7e5697c2013-10-18 21:07:36 -050085 skip_message="Does not support SHA256",
86 )
87
88
89class TestSHA384(object):
90 test_SHA384 = generate_base_hash_test(
91 hashes.SHA384,
92 digest_size=48,
93 block_size=128,
Paul Kehrer14c3a352013-10-22 21:45:16 -050094 only_if=lambda backend: backend.hashes.supported(hashes.SHA384),
Paul Kehrer7e5697c2013-10-18 21:07:36 -050095 skip_message="Does not support SHA384",
96 )
97
98
99class TestSHA512(object):
100 test_SHA512 = generate_base_hash_test(
101 hashes.SHA512,
102 digest_size=64,
103 block_size=128,
Paul Kehrer14c3a352013-10-22 21:45:16 -0500104 only_if=lambda backend: backend.hashes.supported(hashes.SHA512),
Paul Kehrer7e5697c2013-10-18 21:07:36 -0500105 skip_message="Does not support SHA512",
106 )
Paul Kehrerc1794072013-10-18 21:42:57 -0500107
108
109class TestRIPEMD160(object):
110 test_RIPEMD160 = generate_base_hash_test(
111 hashes.RIPEMD160,
112 digest_size=20,
113 block_size=64,
Paul Kehrer14c3a352013-10-22 21:45:16 -0500114 only_if=lambda backend: backend.hashes.supported(hashes.RIPEMD160),
Paul Kehrerc1794072013-10-18 21:42:57 -0500115 skip_message="Does not support RIPEMD160",
116 )
Paul Kehrer79ff8bf2013-10-18 22:07:29 -0500117
118
119class TestWhirlpool(object):
120 test_Whirlpool = generate_base_hash_test(
121 hashes.Whirlpool,
122 digest_size=64,
123 block_size=64,
Paul Kehrer14c3a352013-10-22 21:45:16 -0500124 only_if=lambda backend: backend.hashes.supported(hashes.Whirlpool),
Paul Kehrer79ff8bf2013-10-18 22:07:29 -0500125 skip_message="Does not support Whirlpool",
126 )
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500127
128
129class TestMD5(object):
130 test_MD5 = generate_base_hash_test(
131 hashes.MD5,
132 digest_size=16,
133 block_size=64,
Paul Kehrer14c3a352013-10-22 21:45:16 -0500134 only_if=lambda backend: backend.hashes.supported(hashes.MD5),
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500135 skip_message="Does not support MD5",
136 )