| 1 |
import unittest |
| 2 |
from binascii import unhexlify, hexlify |
| 3 |
|
| 4 |
from Crypto.Util.py3compat import tobytes |
| 5 |
from Crypto.SelfTest.st_common import list_test_cases |
| 6 |
|
| 7 |
from Crypto.Hash import TupleHash128, TupleHash256 |
| 8 |
|
| 9 |
|
| 10 |
class TupleHashTest(unittest.TestCase): |
| 11 |
|
| 12 |
def new(self, *args, **kwargs): |
| 13 |
return self.TupleHash.new(*args, **kwargs) |
| 14 |
|
| 15 |
def test_new_positive(self): |
| 16 |
|
| 17 |
h = self.new() |
| 18 |
for new_func in self.TupleHash.new, h.new: |
| 19 |
|
| 20 |
for dbits in range(64, 1024 + 1, 8): |
| 21 |
hobj = new_func(digest_bits=dbits) |
| 22 |
self.assertEqual(hobj.digest_size * 8, dbits) |
| 23 |
|
| 24 |
for dbytes in range(8, 128 + 1): |
| 25 |
hobj = new_func(digest_bytes=dbytes) |
| 26 |
self.assertEqual(hobj.digest_size, dbytes) |
| 27 |
|
| 28 |
hobj = h.new() |
| 29 |
self.assertEqual(hobj.digest_size, self.default_bytes) |
| 30 |
|
| 31 |
def test_new_negative(self): |
| 32 |
|
| 33 |
h = self.new() |
| 34 |
for new_func in self.TupleHash.new, h.new: |
| 35 |
self.assertRaises(TypeError, new_func, |
| 36 |
digest_bytes=self.minimum_bytes, |
| 37 |
digest_bits=self.minimum_bits) |
| 38 |
self.assertRaises(ValueError, new_func, digest_bytes=0) |
| 39 |
self.assertRaises(ValueError, new_func, |
| 40 |
digest_bits=self.minimum_bits + 7) |
| 41 |
self.assertRaises(ValueError, new_func, |
| 42 |
digest_bits=self.minimum_bits - 8) |
| 43 |
self.assertRaises(ValueError, new_func, |
| 44 |
digest_bits=self.minimum_bytes - 1) |
| 45 |
|
| 46 |
def test_default_digest_size(self): |
| 47 |
digest = self.new().digest() |
| 48 |
self.assertEqual(len(digest), self.default_bytes) |
| 49 |
|
| 50 |
def test_update(self): |
| 51 |
h = self.new() |
| 52 |
h.update(b'') |
| 53 |
h.digest() |
| 54 |
|
| 55 |
h = self.new() |
| 56 |
h.update(b'') |
| 57 |
h.update(b'STRING1') |
| 58 |
h.update(b'STRING2') |
| 59 |
mac1 = h.digest() |
| 60 |
|
| 61 |
h = self.new() |
| 62 |
h.update(b'STRING1') |
| 63 |
h.update(b'STRING2') |
| 64 |
mac2 = h.digest() |
| 65 |
self.assertNotEqual(mac1, mac2) |
| 66 |
|
| 67 |
h = self.new() |
| 68 |
h.update(b'STRING1', b'STRING2') |
| 69 |
self.assertEqual(mac2, h.digest()) |
| 70 |
|
| 71 |
h = self.new() |
| 72 |
t = b'STRING1', b'STRING2' |
| 73 |
h.update(*t) |
| 74 |
self.assertEqual(mac2, h.digest()) |
| 75 |
|
| 76 |
def test_update_negative(self): |
| 77 |
h = self.new() |
| 78 |
self.assertRaises(TypeError, h.update, u"string") |
| 79 |
self.assertRaises(TypeError, h.update, None) |
| 80 |
self.assertRaises(TypeError, h.update, (b'STRING1', b'STRING2')) |
| 81 |
|
| 82 |
def test_digest(self): |
| 83 |
h = self.new() |
| 84 |
digest = h.digest() |
| 85 |
|
| 86 |
|
| 87 |
self.assertEqual(h.digest(), digest) |
| 88 |
|
| 89 |
self.assertTrue(isinstance(digest, type(b"digest"))) |
| 90 |
|
| 91 |
def test_update_after_digest(self): |
| 92 |
msg = b"rrrrttt" |
| 93 |
|
| 94 |
|
| 95 |
h = self.new() |
| 96 |
h.update(msg) |
| 97 |
dig1 = h.digest() |
| 98 |
self.assertRaises(TypeError, h.update, dig1) |
| 99 |
|
| 100 |
def test_hex_digest(self): |
| 101 |
mac = self.new() |
| 102 |
digest = mac.digest() |
| 103 |
hexdigest = mac.hexdigest() |
| 104 |
|
| 105 |
|
| 106 |
self.assertEqual(hexlify(digest), tobytes(hexdigest)) |
| 107 |
|
| 108 |
self.assertEqual(mac.hexdigest(), hexdigest) |
| 109 |
|
| 110 |
self.assertTrue(isinstance(hexdigest, type("digest"))) |
| 111 |
|
| 112 |
def test_bytearray(self): |
| 113 |
|
| 114 |
data = b"\x00\x01\x02" |
| 115 |
|
| 116 |
|
| 117 |
data_ba = bytearray(data) |
| 118 |
|
| 119 |
h1 = self.new() |
| 120 |
h2 = self.new() |
| 121 |
h1.update(data) |
| 122 |
h2.update(data_ba) |
| 123 |
data_ba[:1] = b'\xFF' |
| 124 |
|
| 125 |
self.assertEqual(h1.digest(), h2.digest()) |
| 126 |
|
| 127 |
def test_memoryview(self): |
| 128 |
|
| 129 |
data = b"\x00\x01\x02" |
| 130 |
|
| 131 |
def get_mv_ro(data): |
| 132 |
return memoryview(data) |
| 133 |
|
| 134 |
def get_mv_rw(data): |
| 135 |
return memoryview(bytearray(data)) |
| 136 |
|
| 137 |
for get_mv in (get_mv_ro, get_mv_rw): |
| 138 |
|
| 139 |
|
| 140 |
data_mv = get_mv(data) |
| 141 |
|
| 142 |
h1 = self.new() |
| 143 |
h2 = self.new() |
| 144 |
h1.update(data) |
| 145 |
h2.update(data_mv) |
| 146 |
if not data_mv.readonly: |
| 147 |
data_mv[:1] = b'\xFF' |
| 148 |
|
| 149 |
self.assertEqual(h1.digest(), h2.digest()) |
| 150 |
|
| 151 |
|
| 152 |
class TupleHash128Test(TupleHashTest): |
| 153 |
|
| 154 |
TupleHash = TupleHash128 |
| 155 |
|
| 156 |
minimum_bytes = 8 |
| 157 |
default_bytes = 64 |
| 158 |
|
| 159 |
minimum_bits = 64 |
| 160 |
default_bits = 512 |
| 161 |
|
| 162 |
|
| 163 |
class TupleHash256Test(TupleHashTest): |
| 164 |
|
| 165 |
TupleHash = TupleHash256 |
| 166 |
|
| 167 |
minimum_bytes = 8 |
| 168 |
default_bytes = 64 |
| 169 |
|
| 170 |
minimum_bits = 64 |
| 171 |
default_bits = 512 |
| 172 |
|
| 173 |
|
| 174 |
class NISTExampleTestVectors(unittest.TestCase): |
| 175 |
|
| 176 |
|
| 177 |
test_data = [ |
| 178 |
( |
| 179 |
( |
| 180 |
"00 01 02", |
| 181 |
"10 11 12 13 14 15", |
| 182 |
), |
| 183 |
"", |
| 184 |
"C5 D8 78 6C 1A FB 9B 82 11 1A B3 4B 65 B2 C0 04" |
| 185 |
"8F A6 4E 6D 48 E2 63 26 4C E1 70 7D 3F FC 8E D1", |
| 186 |
"KMAC128 Sample #1 NIST", |
| 187 |
TupleHash128 |
| 188 |
), |
| 189 |
( |
| 190 |
( |
| 191 |
"00 01 02", |
| 192 |
"10 11 12 13 14 15", |
| 193 |
), |
| 194 |
"My Tuple App", |
| 195 |
"75 CD B2 0F F4 DB 11 54 E8 41 D7 58 E2 41 60 C5" |
| 196 |
"4B AE 86 EB 8C 13 E7 F5 F4 0E B3 55 88 E9 6D FB", |
| 197 |
"KMAC128 Sample #2 NIST", |
| 198 |
TupleHash128 |
| 199 |
), |
| 200 |
( |
| 201 |
( |
| 202 |
"00 01 02", |
| 203 |
"10 11 12 13 14 15", |
| 204 |
"20 21 22 23 24 25 26 27 28", |
| 205 |
), |
| 206 |
"My Tuple App", |
| 207 |
"E6 0F 20 2C 89 A2 63 1E DA 8D 4C 58 8C A5 FD 07" |
| 208 |
"F3 9E 51 51 99 8D EC CF 97 3A DB 38 04 BB 6E 84", |
| 209 |
"KMAC128 Sample #3 NIST", |
| 210 |
TupleHash128 |
| 211 |
), |
| 212 |
( |
| 213 |
( |
| 214 |
"00 01 02", |
| 215 |
"10 11 12 13 14 15", |
| 216 |
), |
| 217 |
"", |
| 218 |
"CF B7 05 8C AC A5 E6 68 F8 1A 12 A2 0A 21 95 CE" |
| 219 |
"97 A9 25 F1 DB A3 E7 44 9A 56 F8 22 01 EC 60 73" |
| 220 |
"11 AC 26 96 B1 AB 5E A2 35 2D F1 42 3B DE 7B D4" |
| 221 |
"BB 78 C9 AE D1 A8 53 C7 86 72 F9 EB 23 BB E1 94", |
| 222 |
"KMAC256 Sample #4 NIST", |
| 223 |
TupleHash256 |
| 224 |
), |
| 225 |
( |
| 226 |
( |
| 227 |
"00 01 02", |
| 228 |
"10 11 12 13 14 15", |
| 229 |
), |
| 230 |
"My Tuple App", |
| 231 |
"14 7C 21 91 D5 ED 7E FD 98 DB D9 6D 7A B5 A1 16" |
| 232 |
"92 57 6F 5F E2 A5 06 5F 3E 33 DE 6B BA 9F 3A A1" |
| 233 |
"C4 E9 A0 68 A2 89 C6 1C 95 AA B3 0A EE 1E 41 0B" |
| 234 |
"0B 60 7D E3 62 0E 24 A4 E3 BF 98 52 A1 D4 36 7E", |
| 235 |
"KMAC256 Sample #5 NIST", |
| 236 |
TupleHash256 |
| 237 |
), |
| 238 |
( |
| 239 |
( |
| 240 |
"00 01 02", |
| 241 |
"10 11 12 13 14 15", |
| 242 |
"20 21 22 23 24 25 26 27 28", |
| 243 |
), |
| 244 |
"My Tuple App", |
| 245 |
"45 00 0B E6 3F 9B 6B FD 89 F5 47 17 67 0F 69 A9" |
| 246 |
"BC 76 35 91 A4 F0 5C 50 D6 88 91 A7 44 BC C6 E7" |
| 247 |
"D6 D5 B5 E8 2C 01 8D A9 99 ED 35 B0 BB 49 C9 67" |
| 248 |
"8E 52 6A BD 8E 85 C1 3E D2 54 02 1D B9 E7 90 CE", |
| 249 |
"KMAC256 Sample #6 NIST", |
| 250 |
TupleHash256 |
| 251 |
), |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
] |
| 256 |
|
| 257 |
def setUp(self): |
| 258 |
td = [] |
| 259 |
for tv_in in self.test_data: |
| 260 |
tv_out = [None] * len(tv_in) |
| 261 |
|
| 262 |
tv_out[0] = [] |
| 263 |
for string in tv_in[0]: |
| 264 |
tv_out[0].append(unhexlify(string.replace(" ", ""))) |
| 265 |
|
| 266 |
tv_out[1] = tobytes(tv_in[1]) |
| 267 |
tv_out[2] = unhexlify(tv_in[2].replace(" ", "")) |
| 268 |
tv_out[3] = tv_in[3] |
| 269 |
tv_out[4] = tv_in[4] |
| 270 |
td.append(tv_out) |
| 271 |
self.test_data = td |
| 272 |
|
| 273 |
def runTest(self): |
| 274 |
|
| 275 |
for data, custom, digest, text, module in self.test_data: |
| 276 |
hd1 = module.new(custom=custom, digest_bytes=len(digest)) |
| 277 |
hd2 = module.new(custom=custom, digest_bytes=len(digest)) |
| 278 |
|
| 279 |
|
| 280 |
for string in data: |
| 281 |
hd1.update(string) |
| 282 |
|
| 283 |
|
| 284 |
hd2.update(*data) |
| 285 |
|
| 286 |
self.assertEqual(hd1.digest(), digest, msg=text) |
| 287 |
self.assertEqual(hd2.digest(), digest, msg=text) |
| 288 |
|
| 289 |
def get_tests(config={}): |
| 290 |
tests = [] |
| 291 |
|
| 292 |
tests += list_test_cases(TupleHash128Test) |
| 293 |
tests += list_test_cases(TupleHash256Test) |
| 294 |
tests.append(NISTExampleTestVectors()) |
| 295 |
|
| 296 |
return tests |
| 297 |
|
| 298 |
|
| 299 |
if __name__ == '__main__': |
| 300 |
def suite(): |
| 301 |
return unittest.TestSuite(get_tests()) |
| 302 |
unittest.main(defaultTest='suite') |