TOR_VEGAS: Implement Prop#324 TOR_VEGAS.
[tor.git] / src / test / hs_indexes.py
blob5c6d893a6624af640bee53807e77e21bf7bcc97f
2 # The hidden service subsystem has two type of index. The first type is a
3 # value that each node in the network gets assigned to using their identity
4 # key which is their position in the hashring. (hs_build_hsdir_index()).
6 # The second type is a value that both the client and service computes to
7 # store/fetch the descriptor on the hashring. (hs_build_hs_index()).
10 # Future imports for Python 2.7, mandatory in 3.0
11 from __future__ import division
12 from __future__ import print_function
13 from __future__ import unicode_literals
15 import sys
16 import hashlib
17 import struct
18 import base64
20 # Python 3.6+, the SHA3 is available in hashlib natively. Else this requires
21 # the pysha3 package (pip install pysha3).
22 if sys.version_info < (3, 6):
23 import sha3
24 # Test vector to make sure the right sha3 version will be used. pysha3 < 1.0
25 # used the old Keccak implementation. During the finalization of SHA3, NIST
26 # changed the delimiter suffix from 0x01 to 0x06. The Keccak sponge function
27 # stayed the same. pysha3 1.0 provides the previous Keccak hash, too.
28 TEST_VALUE = "e167f68d6563d75bb25f3aa49c29ef612d41352dc00606de7cbd630bb2665f51"
29 if TEST_VALUE != sha3.sha3_256(b"Hello World").hexdigest():
30 print("pysha3 version is < 1.0. Please install from:")
31 print("https://github.com/tiran/pysha3https://github.com/tiran/pysha3")
32 sys.exit(1)
34 # The first index we'll build is the position index in the hashring that is
35 # constructed by the hs_build_hsdir_index() function. Construction is:
36 # SHA3-256("node-idx" | node_identity |
37 # shared_random_value | INT_8(period_length) | INT_8(period_num) )
39 PREFIX = "node-idx".encode()
40 # 32 bytes ed25519 pubkey.
41 IDENTITY = ("\x42" * 32).encode()
42 # SRV is 32 bytes.
43 SRV = ("\x43" * 32).encode()
44 # Time period length is a 8 bytes value.
45 PERIOD_LEN = 1440
46 # Period number is a 8 bytes value.
47 PERIOD_NUM = 42
49 data = struct.pack('!8s32s32sQQ', PREFIX, IDENTITY, SRV, PERIOD_NUM,
50 PERIOD_LEN)
51 hsdir_index = hashlib.sha3_256(data).hexdigest()
53 print("[hs_build_hsdir_index] %s" % (hsdir_index))
55 # The second index we'll build is where the HS stores and the client fetches
56 # the descriptor on the hashring. It is constructed by the hs_build_hs_index()
57 # function and the construction is:
58 # SHA3-256("store-at-idx" | blinded_public_key |
59 # INT_8(replicanum) | INT_8(period_num) | INT_8(period_length) )
61 PREFIX = "store-at-idx".encode()
62 # 32 bytes ed25519 pubkey.
63 PUBKEY = ("\x42" * 32).encode()
64 # Replica number is a 8 bytes value.
65 REPLICA_NUM = 1
66 # Time period length is a 8 bytes value.
67 PERIOD_LEN = 1440
68 # Period number is a 8 bytes value.
69 PERIOD_NUM = 42
71 data = struct.pack('!12s32sQQQ', PREFIX, PUBKEY, REPLICA_NUM, PERIOD_LEN,
72 PERIOD_NUM)
73 hs_index = hashlib.sha3_256(data).hexdigest()
75 print("[hs_build_hs_index] %s" % (hs_index))