Debian Codegraph @

lib/Crypto/SelfTest/Hash/test_SHA3_384.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # SelfTest/Hash/test_SHA3_384.py: Self-test for the SHA-3/384 hash function
4 #
5 # ===================================================================
6 # The contents of this file are dedicated to the public domain. To
7 # the extent that dedication to the public domain is not available,
8 # everyone is granted a worldwide, perpetual, royalty-free,
9 # non-exclusive license to exercise all rights associated with the
10 # contents of this file for any purpose whatsoever.
11 # No rights are reserved.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
17 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 # SOFTWARE.
21 # ===================================================================
22
23 """Self-test suite for Crypto.Hash.SHA3_384"""
24
25 import unittest
26 from binascii import hexlify
27
28 from Crypto.SelfTest.loader import load_test_vectors
29 from Crypto.SelfTest.st_common import list_test_cases
30 from Crypto.Hash import SHA3_384 as SHA3
31 from Crypto.Util.py3compat import b
32
33
34 class APITest(unittest.TestCase):
35
36 def test_update_after_digest(self):
37 msg=b("rrrrttt")
38
39 # Normally, update() cannot be done after digest()
40 h = SHA3.new(data=msg[:4])
41 dig1 = h.digest()
42 self.assertRaises(TypeError, h.update, msg[4:])
43 dig2 = SHA3.new(data=msg).digest()
44
45 # With the proper flag, it is allowed
46 h = SHA3.new(data=msg[:4], update_after_digest=True)
47 self.assertEqual(h.digest(), dig1)
48 # ... and the subsequent digest applies to the entire message
49 # up to that point
50 h.update(msg[4:])
51 self.assertEqual(h.digest(), dig2)
52
53
54 def get_tests(config={}):
55 from .common import make_hash_tests
56
57 tests = []
58
59 test_vectors = load_test_vectors(("Hash", "SHA3"),
60 "ShortMsgKAT_SHA3-384.txt",
61 "KAT SHA-3 384",
62 { "len" : lambda x: int(x) } ) or []
63
64 test_data = []
65 for tv in test_vectors:
66 if tv.len == 0:
67 tv.msg = b("")
68 test_data.append((hexlify(tv.md), tv.msg, tv.desc))
69
70 tests += make_hash_tests(SHA3, "SHA3_384", test_data,
71 digest_size=SHA3.digest_size,
72 oid="2.16.840.1.101.3.4.2.9")
73 tests += list_test_cases(APITest)
74 return tests
75
76 if __name__ == '__main__':
77 import unittest
78 suite = lambda: unittest.TestSuite(get_tests())
79 unittest.main(defaultTest='suite')