| 1 | """ |
|---|
| 2 | Tests for allmydata.util.hashutil. |
|---|
| 3 | |
|---|
| 4 | Ported to Python 3. |
|---|
| 5 | """ |
|---|
| 6 | |
|---|
| 7 | from twisted.trial import unittest |
|---|
| 8 | |
|---|
| 9 | from allmydata.util import hashutil, base32 |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class HashUtilTests(unittest.TestCase): |
|---|
| 13 | |
|---|
| 14 | def test_random_key(self): |
|---|
| 15 | k = hashutil.random_key() |
|---|
| 16 | self.failUnlessEqual(len(k), hashutil.KEYLEN) |
|---|
| 17 | self.assertIsInstance(k, bytes) |
|---|
| 18 | |
|---|
| 19 | def test_sha256d(self): |
|---|
| 20 | h1 = hashutil.tagged_hash(b"tag1", b"value") |
|---|
| 21 | self.assertIsInstance(h1, bytes) |
|---|
| 22 | h2 = hashutil.tagged_hasher(b"tag1") |
|---|
| 23 | h2.update(b"value") |
|---|
| 24 | h2a = h2.digest() |
|---|
| 25 | h2b = h2.digest() |
|---|
| 26 | self.assertIsInstance(h2a, bytes) |
|---|
| 27 | self.failUnlessEqual(h1, h2a) |
|---|
| 28 | self.failUnlessEqual(h2a, h2b) |
|---|
| 29 | |
|---|
| 30 | def test_sha256d_truncated(self): |
|---|
| 31 | h1 = hashutil.tagged_hash(b"tag1", b"value", 16) |
|---|
| 32 | h2 = hashutil.tagged_hasher(b"tag1", 16) |
|---|
| 33 | h2.update(b"value") |
|---|
| 34 | h2 = h2.digest() |
|---|
| 35 | self.failUnlessEqual(len(h1), 16) |
|---|
| 36 | self.failUnlessEqual(len(h2), 16) |
|---|
| 37 | self.failUnlessEqual(h1, h2) |
|---|
| 38 | |
|---|
| 39 | def test_well_known_tagged_hash(self): |
|---|
| 40 | self.assertEqual( |
|---|
| 41 | b"yra322btzoqjp4ts2jon5dztgnilcdg6jgztgk7joi6qpjkitg2q", |
|---|
| 42 | base32.b2a(hashutil.tagged_hash(b"tag", b"hello world")), |
|---|
| 43 | ) |
|---|
| 44 | self.assertEqual( |
|---|
| 45 | b"kfbsfssrv2bvtp3regne6j7gpdjcdjwncewriyfdtt764o5oa7ta", |
|---|
| 46 | base32.b2a(hashutil.tagged_hash(b"different", b"hello world")), |
|---|
| 47 | ) |
|---|
| 48 | self.assertEqual( |
|---|
| 49 | b"z34pzkgo36chbjz2qykonlxthc4zdqqquapw4bcaoogzvmmcr3zq", |
|---|
| 50 | base32.b2a(hashutil.tagged_hash(b"different", b"goodbye world")), |
|---|
| 51 | ) |
|---|
| 52 | |
|---|
| 53 | def test_well_known_tagged_pair_hash(self): |
|---|
| 54 | self.assertEqual( |
|---|
| 55 | b"wmto44q3shtezwggku2fxztfkwibvznkfu6clatnvfog527sb6dq", |
|---|
| 56 | base32.b2a(hashutil.tagged_pair_hash(b"tag", b"hello", b"world")), |
|---|
| 57 | ) |
|---|
| 58 | self.assertEqual( |
|---|
| 59 | b"lzn27njx246jhijpendqrxlk4yb23nznbcrihommbymg5e7quh4a", |
|---|
| 60 | base32.b2a(hashutil.tagged_pair_hash(b"different", b"hello", b"world")), |
|---|
| 61 | ) |
|---|
| 62 | self.assertEqual( |
|---|
| 63 | b"qnehpoypxxdhjheqq7dayloghtu42yr55uylc776zt23ii73o3oq", |
|---|
| 64 | base32.b2a(hashutil.tagged_pair_hash(b"different", b"goodbye", b"world")), |
|---|
| 65 | ) |
|---|
| 66 | |
|---|
| 67 | def test_chk(self): |
|---|
| 68 | h1 = hashutil.convergence_hash(3, 10, 1000, b"data", b"secret") |
|---|
| 69 | h2 = hashutil.convergence_hasher(3, 10, 1000, b"secret") |
|---|
| 70 | h2.update(b"data") |
|---|
| 71 | h2 = h2.digest() |
|---|
| 72 | self.failUnlessEqual(h1, h2) |
|---|
| 73 | self.assertIsInstance(h1, bytes) |
|---|
| 74 | self.assertIsInstance(h2, bytes) |
|---|
| 75 | |
|---|
| 76 | def test_hashers(self): |
|---|
| 77 | h1 = hashutil.block_hash(b"foo") |
|---|
| 78 | h2 = hashutil.block_hasher() |
|---|
| 79 | h2.update(b"foo") |
|---|
| 80 | self.failUnlessEqual(h1, h2.digest()) |
|---|
| 81 | self.assertIsInstance(h1, bytes) |
|---|
| 82 | |
|---|
| 83 | h1 = hashutil.uri_extension_hash(b"foo") |
|---|
| 84 | h2 = hashutil.uri_extension_hasher() |
|---|
| 85 | h2.update(b"foo") |
|---|
| 86 | self.failUnlessEqual(h1, h2.digest()) |
|---|
| 87 | self.assertIsInstance(h1, bytes) |
|---|
| 88 | |
|---|
| 89 | h1 = hashutil.plaintext_hash(b"foo") |
|---|
| 90 | h2 = hashutil.plaintext_hasher() |
|---|
| 91 | h2.update(b"foo") |
|---|
| 92 | self.failUnlessEqual(h1, h2.digest()) |
|---|
| 93 | self.assertIsInstance(h1, bytes) |
|---|
| 94 | |
|---|
| 95 | h1 = hashutil.crypttext_hash(b"foo") |
|---|
| 96 | h2 = hashutil.crypttext_hasher() |
|---|
| 97 | h2.update(b"foo") |
|---|
| 98 | self.failUnlessEqual(h1, h2.digest()) |
|---|
| 99 | self.assertIsInstance(h1, bytes) |
|---|
| 100 | |
|---|
| 101 | h1 = hashutil.crypttext_segment_hash(b"foo") |
|---|
| 102 | h2 = hashutil.crypttext_segment_hasher() |
|---|
| 103 | h2.update(b"foo") |
|---|
| 104 | self.failUnlessEqual(h1, h2.digest()) |
|---|
| 105 | self.assertIsInstance(h1, bytes) |
|---|
| 106 | |
|---|
| 107 | h1 = hashutil.plaintext_segment_hash(b"foo") |
|---|
| 108 | h2 = hashutil.plaintext_segment_hasher() |
|---|
| 109 | h2.update(b"foo") |
|---|
| 110 | self.failUnlessEqual(h1, h2.digest()) |
|---|
| 111 | self.assertIsInstance(h1, bytes) |
|---|
| 112 | |
|---|
| 113 | def test_timing_safe_compare(self): |
|---|
| 114 | self.failUnless(hashutil.timing_safe_compare(b"a", b"a")) |
|---|
| 115 | self.failUnless(hashutil.timing_safe_compare(b"ab", b"ab")) |
|---|
| 116 | self.failIf(hashutil.timing_safe_compare(b"a", b"b")) |
|---|
| 117 | self.failIf(hashutil.timing_safe_compare(b"a", b"aa")) |
|---|
| 118 | |
|---|
| 119 | def _testknown(self, hashf, expected_a, *args): |
|---|
| 120 | got = hashf(*args) |
|---|
| 121 | self.assertIsInstance(got, bytes) |
|---|
| 122 | got_a = base32.b2a(got) |
|---|
| 123 | self.failUnlessEqual(got_a, expected_a) |
|---|
| 124 | |
|---|
| 125 | def test_storage_index_hash_known_answers(self): |
|---|
| 126 | """ |
|---|
| 127 | Verify backwards compatibility by comparing ``storage_index_hash`` outputs |
|---|
| 128 | for some well-known (to us) inputs. |
|---|
| 129 | """ |
|---|
| 130 | # This is a marginal case. b"" is not a valid aes 128 key. The |
|---|
| 131 | # implementation does nothing to avoid producing a result for it, |
|---|
| 132 | # though. |
|---|
| 133 | self._testknown(hashutil.storage_index_hash, b"qb5igbhcc5esa6lwqorsy7e6am", b"") |
|---|
| 134 | |
|---|
| 135 | # This is a little bit more realistic though clearly this is a poor key choice. |
|---|
| 136 | self._testknown(hashutil.storage_index_hash, b"wvggbrnrezdpa5yayrgiw5nzja", b"x" * 16) |
|---|
| 137 | |
|---|
| 138 | # Here's a much more realistic key that I generated by reading some |
|---|
| 139 | # bytes from /dev/urandom. I computed the expected hash value twice. |
|---|
| 140 | # First using hashlib.sha256 and then with sha256sum(1). The input |
|---|
| 141 | # string given to the hash function was "43:<storage index tag>,<key>" |
|---|
| 142 | # in each case. |
|---|
| 143 | self._testknown( |
|---|
| 144 | hashutil.storage_index_hash, |
|---|
| 145 | b"aarbseqqrpsfowduchcjbonscq", |
|---|
| 146 | base32.a2b(b"2ckv3dfzh6rgjis6ogfqhyxnzy"), |
|---|
| 147 | ) |
|---|
| 148 | |
|---|
| 149 | def test_convergence_hasher_tag(self): |
|---|
| 150 | """ |
|---|
| 151 | ``_convergence_hasher_tag`` constructs the convergence hasher tag from a |
|---|
| 152 | unique prefix, the required, total, and segment size parameters, and a |
|---|
| 153 | convergence secret. |
|---|
| 154 | """ |
|---|
| 155 | self.assertEqual( |
|---|
| 156 | b"allmydata_immutable_content_to_key_with_added_secret_v1+" |
|---|
| 157 | b"16:\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42," |
|---|
| 158 | b"9:3,10,1024,", |
|---|
| 159 | hashutil._convergence_hasher_tag( |
|---|
| 160 | k=3, |
|---|
| 161 | n=10, |
|---|
| 162 | segsize=1024, |
|---|
| 163 | convergence=b"\x42" * 16, |
|---|
| 164 | ), |
|---|
| 165 | ) |
|---|
| 166 | |
|---|
| 167 | def test_convergence_hasher_out_of_bounds(self): |
|---|
| 168 | """ |
|---|
| 169 | ``_convergence_hasher_tag`` raises ``ValueError`` if k or n is not between |
|---|
| 170 | 1 and 256 inclusive or if k is greater than n. |
|---|
| 171 | """ |
|---|
| 172 | segsize = 1024 |
|---|
| 173 | secret = b"\x42" * 16 |
|---|
| 174 | for bad_k in (0, 2, 257): |
|---|
| 175 | with self.assertRaises(ValueError): |
|---|
| 176 | hashutil._convergence_hasher_tag( |
|---|
| 177 | k=bad_k, n=1, segsize=segsize, convergence=secret, |
|---|
| 178 | ) |
|---|
| 179 | for bad_n in (0, 1, 257): |
|---|
| 180 | with self.assertRaises(ValueError): |
|---|
| 181 | hashutil._convergence_hasher_tag( |
|---|
| 182 | k=2, n=bad_n, segsize=segsize, convergence=secret, |
|---|
| 183 | ) |
|---|
| 184 | |
|---|
| 185 | def test_known_answers(self): |
|---|
| 186 | """ |
|---|
| 187 | Verify backwards compatibility by comparing hash outputs for some |
|---|
| 188 | well-known (to us) inputs. |
|---|
| 189 | """ |
|---|
| 190 | self._testknown(hashutil.block_hash, b"msjr5bh4evuh7fa3zw7uovixfbvlnstr5b65mrerwfnvjxig2jvq", b"") |
|---|
| 191 | self._testknown(hashutil.uri_extension_hash, b"wthsu45q7zewac2mnivoaa4ulh5xvbzdmsbuyztq2a5fzxdrnkka", b"") |
|---|
| 192 | self._testknown(hashutil.plaintext_hash, b"5lz5hwz3qj3af7n6e3arblw7xzutvnd3p3fjsngqjcb7utf3x3da", b"") |
|---|
| 193 | self._testknown(hashutil.crypttext_hash, b"itdj6e4njtkoiavlrmxkvpreosscssklunhwtvxn6ggho4rkqwga", b"") |
|---|
| 194 | self._testknown(hashutil.crypttext_segment_hash, b"aovy5aa7jej6ym5ikgwyoi4pxawnoj3wtaludjz7e2nb5xijb7aa", b"") |
|---|
| 195 | self._testknown(hashutil.plaintext_segment_hash, b"4fdgf6qruaisyukhqcmoth4t3li6bkolbxvjy4awwcpprdtva7za", b"") |
|---|
| 196 | self._testknown(hashutil.convergence_hash, b"3mo6ni7xweplycin6nowynw2we", 3, 10, 100, b"", b"converge") |
|---|
| 197 | self._testknown(hashutil.my_renewal_secret_hash, b"ujhr5k5f7ypkp67jkpx6jl4p47pyta7hu5m527cpcgvkafsefm6q", b"") |
|---|
| 198 | self._testknown(hashutil.my_cancel_secret_hash, b"rjwzmafe2duixvqy6h47f5wfrokdziry6zhx4smew4cj6iocsfaa", b"") |
|---|
| 199 | self._testknown(hashutil.file_renewal_secret_hash, b"hzshk2kf33gzbd5n3a6eszkf6q6o6kixmnag25pniusyaulqjnia", b"", b"si") |
|---|
| 200 | self._testknown(hashutil.file_cancel_secret_hash, b"bfciwvr6w7wcavsngxzxsxxaszj72dej54n4tu2idzp6b74g255q", b"", b"si") |
|---|
| 201 | self._testknown(hashutil.bucket_renewal_secret_hash, b"e7imrzgzaoashsncacvy3oysdd2m5yvtooo4gmj4mjlopsazmvuq", b"", b"\x00"*20) |
|---|
| 202 | self._testknown(hashutil.bucket_cancel_secret_hash, b"dvdujeyxeirj6uux6g7xcf4lvesk632aulwkzjar7srildvtqwma", b"", b"\x00"*20) |
|---|
| 203 | self._testknown(hashutil.hmac, b"c54ypfi6pevb3nvo6ba42jtglpkry2kbdopqsi7dgrm4r7tw5sra", b"tag", b"") |
|---|
| 204 | self._testknown(hashutil.mutable_rwcap_key_hash, b"6rvn2iqrghii5n4jbbwwqqsnqu", b"iv", b"wk") |
|---|
| 205 | self._testknown(hashutil.ssk_writekey_hash, b"ykpgmdbpgbb6yqz5oluw2q26ye", b"") |
|---|
| 206 | self._testknown(hashutil.ssk_write_enabler_master_hash, b"izbfbfkoait4dummruol3gy2bnixrrrslgye6ycmkuyujnenzpia", b"") |
|---|
| 207 | self._testknown(hashutil.ssk_write_enabler_hash, b"fuu2dvx7g6gqu5x22vfhtyed7p4pd47y5hgxbqzgrlyvxoev62tq", b"wk", b"\x00"*20) |
|---|
| 208 | self._testknown(hashutil.ssk_pubkey_fingerprint_hash, b"3opzw4hhm2sgncjx224qmt5ipqgagn7h5zivnfzqycvgqgmgz35q", b"") |
|---|
| 209 | self._testknown(hashutil.ssk_readkey_hash, b"vugid4as6qbqgeq2xczvvcedai", b"") |
|---|
| 210 | self._testknown(hashutil.ssk_readkey_data_hash, b"73wsaldnvdzqaf7v4pzbr2ae5a", b"iv", b"rk") |
|---|
| 211 | self._testknown(hashutil.ssk_storage_index_hash, b"j7icz6kigb6hxrej3tv4z7ayym", b"") |
|---|
| 212 | |
|---|
| 213 | self._testknown(hashutil.permute_server_hash, |
|---|
| 214 | b"kb4354zeeurpo3ze5e275wzbynm6hlap", # b32(expected) |
|---|
| 215 | b"SI", # peer selection index == storage_index |
|---|
| 216 | base32.a2b(b"u33m4y7klhz3bypswqkozwetvabelhxt"), # seed |
|---|
| 217 | ) |
|---|