Debian Codegraph @

lib/Crypto/SelfTest/Hash/test_cSHAKE.py

raw symbols SCIP
1 # ===================================================================
2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions
4 # are met:
5 #
6 # 1. Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer.
8 # 2. Redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in
10 # the documentation and/or other materials provided with the
11 # distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
16 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
17 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 # POSSIBILITY OF SUCH DAMAGE.
25 # ===================================================================
26
27 """Self-test suite for Crypto.Hash.cSHAKE128 and cSHAKE256"""
28
29 import unittest
30
31 from Crypto.SelfTest.loader import load_test_vectors
32 from Crypto.SelfTest.st_common import list_test_cases
33
34 from Crypto.Hash import cSHAKE128, cSHAKE256, SHAKE128, SHAKE256
35 from Crypto.Util.py3compat import b, bchr, tobytes
36
37
38 class cSHAKETest(unittest.TestCase):
39
40 def test_left_encode(self):
41 from Crypto.Hash.cSHAKE128 import _left_encode
42 self.assertEqual(_left_encode(0), b'\x01\x00')
43 self.assertEqual(_left_encode(1), b'\x01\x01')
44 self.assertEqual(_left_encode(256), b'\x02\x01\x00')
45
46 def test_bytepad(self):
47 from Crypto.Hash.cSHAKE128 import _bytepad
48 self.assertEqual(_bytepad(b'', 4), b'\x01\x04\x00\x00')
49 self.assertEqual(_bytepad(b'A', 4), b'\x01\x04A\x00')
50 self.assertEqual(_bytepad(b'AA', 4), b'\x01\x04AA')
51 self.assertEqual(_bytepad(b'AAA', 4), b'\x01\x04AAA\x00\x00\x00')
52 self.assertEqual(_bytepad(b'AAAA', 4), b'\x01\x04AAAA\x00\x00')
53 self.assertEqual(_bytepad(b'AAAAA', 4), b'\x01\x04AAAAA\x00')
54 self.assertEqual(_bytepad(b'AAAAAA', 4), b'\x01\x04AAAAAA')
55 self.assertEqual(_bytepad(b'AAAAAAA', 4), b'\x01\x04AAAAAAA\x00\x00\x00')
56
57 def test_new_positive(self):
58
59 xof1 = self.cshake.new()
60 xof2 = self.cshake.new(data=b("90"))
61 xof3 = self.cshake.new().update(b("90"))
62
63 self.assertNotEqual(xof1.read(10), xof2.read(10))
64 xof3.read(10)
65 self.assertEqual(xof2.read(10), xof3.read(10))
66
67 xof1 = self.cshake.new()
68 ref = xof1.read(10)
69 xof2 = self.cshake.new(custom=b(""))
70 xof3 = self.cshake.new(custom=b("foo"))
71
72 self.assertEqual(ref, xof2.read(10))
73 self.assertNotEqual(ref, xof3.read(10))
74
75 xof1 = self.cshake.new(custom=b("foo"))
76 xof2 = self.cshake.new(custom=b("foo"), data=b("90"))
77 xof3 = self.cshake.new(custom=b("foo")).update(b("90"))
78
79 self.assertNotEqual(xof1.read(10), xof2.read(10))
80 xof3.read(10)
81 self.assertEqual(xof2.read(10), xof3.read(10))
82
83 def test_update(self):
84 pieces = [bchr(10) * 200, bchr(20) * 300]
85 h = self.cshake.new()
86 h.update(pieces[0]).update(pieces[1])
87 digest = h.read(10)
88 h = self.cshake.new()
89 h.update(pieces[0] + pieces[1])
90 self.assertEqual(h.read(10), digest)
91
92 def test_update_negative(self):
93 h = self.cshake.new()
94 self.assertRaises(TypeError, h.update, u"string")
95
96 def test_digest(self):
97 h = self.cshake.new()
98 digest = h.read(90)
99
100 # read returns a byte string of the right length
101 self.assertTrue(isinstance(digest, type(b("digest"))))
102 self.assertEqual(len(digest), 90)
103
104 def test_update_after_read(self):
105 mac = self.cshake.new()
106 mac.update(b("rrrr"))
107 mac.read(90)
108 self.assertRaises(TypeError, mac.update, b("ttt"))
109
110 def test_shake(self):
111 # When no customization string is passed, results must match SHAKE
112 for digest_len in range(64):
113 xof1 = self.cshake.new(b'TEST')
114 xof2 = self.shake.new(b'TEST')
115 self.assertEqual(xof1.read(digest_len), xof2.read(digest_len))
116
117
118 class cSHAKE128Test(cSHAKETest):
119 cshake = cSHAKE128
120 shake = SHAKE128
121
122
123 class cSHAKE256Test(cSHAKETest):
124 cshake = cSHAKE256
125 shake = SHAKE256
126
127
128 class cSHAKEVectors(unittest.TestCase):
129 pass
130
131
132 vector_files = [("ShortMsgSamples_cSHAKE128.txt", "Short Message Samples cSHAKE128", "128_cshake", cSHAKE128),
133 ("ShortMsgSamples_cSHAKE256.txt", "Short Message Samples cSHAKE256", "256_cshake", cSHAKE256),
134 ("CustomMsgSamples_cSHAKE128.txt", "Custom Message Samples cSHAKE128", "custom_128_cshake", cSHAKE128),
135 ("CustomMsgSamples_cSHAKE256.txt", "Custom Message Samples cSHAKE256", "custom_256_cshake", cSHAKE256),
136 ]
137
138 for file, descr, tag, test_class in vector_files:
139
140 test_vectors = load_test_vectors(("Hash", "SHA3"), file, descr,
141 {"len": lambda x: int(x),
142 "nlen": lambda x: int(x),
143 "slen": lambda x: int(x)}) or []
144
145 for idx, tv in enumerate(test_vectors):
146 if getattr(tv, "len", 0) == 0:
147 data = b("")
148 else:
149 data = tobytes(tv.msg)
150 assert(tv.len == len(tv.msg)*8)
151 if getattr(tv, "nlen", 0) != 0:
152 raise ValueError("Unsupported cSHAKE test vector")
153 if getattr(tv, "slen", 0) == 0:
154 custom = b("")
155 else:
156 custom = tobytes(tv.s)
157 assert(tv.slen == len(tv.s)*8)
158
159 def new_test(self, data=data, result=tv.md, custom=custom, test_class=test_class):
160 hobj = test_class.new(data=data, custom=custom)
161 digest = hobj.read(len(result))
162 self.assertEqual(digest, result)
163
164 setattr(cSHAKEVectors, "test_%s_%d" % (tag, idx), new_test)
165
166
167 def get_tests(config={}):
168 tests = []
169 tests += list_test_cases(cSHAKE128Test)
170 tests += list_test_cases(cSHAKE256Test)
171 tests += list_test_cases(cSHAKEVectors)
172 return tests
173
174
175 if __name__ == '__main__':
176 import unittest
177 suite = lambda: unittest.TestSuite(get_tests())
178 unittest.main(defaultTest='suite')