hs_indexes.py 2.7 KB

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