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()).
15 # Python 3.6+, the SHA3 is available in hashlib natively. Else this requires
16 # the pysha3 package (pip install pysha3).
17 if sys
.version_info
< (3, 6):
19 # Test vector to make sure the right sha3 version will be used. pysha3 < 1.0
20 # used the old Keccak implementation. During the finalization of SHA3, NIST
21 # changed the delimiter suffix from 0x01 to 0x06. The Keccak sponge function
22 # stayed the same. pysha3 1.0 provides the previous Keccak hash, too.
23 TEST_VALUE
= "e167f68d6563d75bb25f3aa49c29ef612d41352dc00606de7cbd630bb2665f51"
24 if TEST_VALUE
!= sha3
.sha3_256(b
"Hello World").hexdigest():
25 print("pysha3 version is < 1.0. Please install from:")
26 print("https://github.com/tiran/pysha3https://github.com/tiran/pysha3")
29 # The first index we'll build is the position index in the hashring that is
30 # constructed by the hs_build_hsdir_index() function. Construction is:
31 # SHA3-256("node-idx" | node_identity |
32 # shared_random_value | INT_8(period_length) | INT_8(period_num) )
34 PREFIX
= "node-idx".encode()
35 # 32 bytes ed25519 pubkey.
36 IDENTITY
= ("\x42" * 32).encode()
38 SRV
= ("\x43" * 32).encode()
39 # Time period length is a 8 bytes value.
41 # Period number is a 8 bytes value.
44 data
= struct
.pack('!8s32s32sQQ', PREFIX
, IDENTITY
, SRV
, PERIOD_NUM
,
46 hsdir_index
= hashlib
.sha3_256(data
).hexdigest()
48 print("[hs_build_hsdir_index] %s" % (hsdir_index
))
50 # The second index we'll build is where the HS stores and the client fetches
51 # the descriptor on the hashring. It is constructed by the hs_build_hs_index()
52 # function and the construction is:
53 # SHA3-256("store-at-idx" | blinded_public_key |
54 # INT_8(replicanum) | INT_8(period_num) | INT_8(period_length) )
56 PREFIX
= "store-at-idx".encode()
57 # 32 bytes ed25519 pubkey.
58 PUBKEY
= ("\x42" * 32).encode()
59 # Replica number is a 8 bytes value.
61 # Time period length is a 8 bytes value.
63 # Period number is a 8 bytes value.
66 data
= struct
.pack('!12s32sQQQ', PREFIX
, PUBKEY
, REPLICA_NUM
, PERIOD_LEN
,
68 hs_index
= hashlib
.sha3_256(data
).hexdigest()
70 print("[hs_build_hs_index] %s" % (hs_index
))