mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-10 21:23:58 +01:00
test: Unit test for the hs indexes
Using a test vector in python, test both hs_build_hsdir_index() and hs_build_hs_index(). This commit also adds the hs_build_address.py to EXTRA_DIST which was missing. Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
parent
715a8ea81c
commit
75ec72f24d
70
src/test/hs_indexes.py
Normal file
70
src/test/hs_indexes.py
Normal file
@ -0,0 +1,70 @@
|
||||
#
|
||||
# The hidden service subsystem has two type of index. The first type is a
|
||||
# value that each node in the network gets assigned to using their identity
|
||||
# key which is their position in the hashring. (hs_build_hsdir_index()).
|
||||
#
|
||||
# The second type is a value that both the client and service computes to
|
||||
# store/fetch the descriptor on the hashring. (hs_build_hs_index()).
|
||||
#
|
||||
|
||||
import sys
|
||||
import hashlib
|
||||
import struct
|
||||
import base64
|
||||
|
||||
# Python 3.6+, the SHA3 is available in hashlib natively. Else this requires
|
||||
# the pysha3 package (pip install pysha3).
|
||||
if sys.version_info < (3, 6):
|
||||
import sha3
|
||||
# Test vector to make sure the right sha3 version will be used. pysha3 < 1.0
|
||||
# used the old Keccak implementation. During the finalization of SHA3, NIST
|
||||
# changed the delimiter suffix from 0x01 to 0x06. The Keccak sponge function
|
||||
# stayed the same. pysha3 1.0 provides the previous Keccak hash, too.
|
||||
TEST_VALUE = "e167f68d6563d75bb25f3aa49c29ef612d41352dc00606de7cbd630bb2665f51"
|
||||
if TEST_VALUE != sha3.sha3_256(b"Hello World").hexdigest():
|
||||
print("pysha3 version is < 1.0. Please install from:")
|
||||
print("https://github.com/tiran/pysha3https://github.com/tiran/pysha3")
|
||||
sys.exit(1)
|
||||
|
||||
# The first index we'll build is the position index in the hashring that is
|
||||
# constructed by the hs_build_hsdir_index() function. Construction is:
|
||||
# SHA3-256("node-idx" | node_identity |
|
||||
# shared_random_value | INT_8(period_length) | INT_8(period_num) )
|
||||
|
||||
PREFIX = "node-idx".encode()
|
||||
# 32 bytes ed25519 pubkey.
|
||||
IDENTITY = ("\x42" * 32).encode()
|
||||
# SRV is 32 bytes.
|
||||
SRV = ("\x43" * 32).encode()
|
||||
# Time period length is a 8 bytes value.
|
||||
PERIOD_LEN = 1440
|
||||
# Period number is a 8 bytes value.
|
||||
PERIOD_NUM = 42
|
||||
|
||||
data = struct.pack('!8s32s32sQQ', PREFIX, IDENTITY, SRV, PERIOD_NUM,
|
||||
PERIOD_LEN)
|
||||
hsdir_index = hashlib.sha3_256(data).hexdigest()
|
||||
|
||||
print("[hs_build_hsdir_index] %s" % (hsdir_index))
|
||||
|
||||
# The second index we'll build is where the HS stores and the client fetches
|
||||
# the descriptor on the hashring. It is constructed by the hs_build_hs_index()
|
||||
# function and the construction is:
|
||||
# SHA3-256("store-at-idx" | blinded_public_key |
|
||||
# INT_8(replicanum) | INT_8(period_num) | INT_8(period_length) )
|
||||
|
||||
PREFIX = "store-at-idx".encode()
|
||||
# 32 bytes ed25519 pubkey.
|
||||
PUBKEY = ("\x42" * 32).encode()
|
||||
# Replica number is a 8 bytes value.
|
||||
REPLICA_NUM = 1
|
||||
# Time period length is a 8 bytes value.
|
||||
PERIOD_LEN = 1440
|
||||
# Period number is a 8 bytes value.
|
||||
PERIOD_NUM = 42
|
||||
|
||||
data = struct.pack('!12s32sQQQ', PREFIX, PUBKEY, REPLICA_NUM, PERIOD_LEN,
|
||||
PERIOD_NUM)
|
||||
hs_index = hashlib.sha3_256(data).hexdigest()
|
||||
|
||||
print("[hs_build_hs_index] %s" % (hs_index))
|
@ -332,6 +332,8 @@ EXTRA_DIST += \
|
||||
src/test/bt_test.py \
|
||||
src/test/ntor_ref.py \
|
||||
src/test/hs_ntor_ref.py \
|
||||
src/test/hs_build_address.py \
|
||||
src/test/hs_indexes.py \
|
||||
src/test/fuzz_static_testcases.sh \
|
||||
src/test/slownacl_curve25519.py \
|
||||
src/test/zero_length_keys.sh \
|
||||
|
@ -1453,6 +1453,51 @@ helper_client_pick_hsdir(const ed25519_public_key_t *onion_identity_pk,
|
||||
;
|
||||
}
|
||||
|
||||
static void
|
||||
test_hs_indexes(void *arg)
|
||||
{
|
||||
int ret;
|
||||
uint64_t period_num = 42;
|
||||
ed25519_public_key_t pubkey;
|
||||
|
||||
(void) arg;
|
||||
|
||||
/* Build the hs_index */
|
||||
{
|
||||
uint8_t hs_index[DIGEST256_LEN];
|
||||
const char *b32_test_vector =
|
||||
"37e5cbbd56a22823714f18f1623ece5983a0d64c78495a8cfab854245e5f9a8a";
|
||||
char test_vector[DIGEST256_LEN];
|
||||
ret = base16_decode(test_vector, sizeof(test_vector), b32_test_vector,
|
||||
strlen(b32_test_vector));
|
||||
tt_int_op(ret, OP_EQ, sizeof(test_vector));
|
||||
/* Our test vector uses a public key set to 32 bytes of \x42. */
|
||||
memset(&pubkey, '\x42', sizeof(pubkey));
|
||||
hs_build_hs_index(1, &pubkey, period_num, hs_index);
|
||||
tt_mem_op(hs_index, OP_EQ, test_vector, sizeof(hs_index));
|
||||
}
|
||||
|
||||
/* Build the hsdir_index */
|
||||
{
|
||||
uint8_t srv[DIGEST256_LEN];
|
||||
uint8_t hsdir_index[DIGEST256_LEN];
|
||||
const char *b32_test_vector =
|
||||
"db475361014a09965e7e5e4d4a25b8f8d4b8f16cb1d8a7e95eed50249cc1a2d5";
|
||||
char test_vector[DIGEST256_LEN];
|
||||
ret = base16_decode(test_vector, sizeof(test_vector), b32_test_vector,
|
||||
strlen(b32_test_vector));
|
||||
tt_int_op(ret, OP_EQ, sizeof(test_vector));
|
||||
/* Our test vector uses a public key set to 32 bytes of \x42. */
|
||||
memset(&pubkey, '\x42', sizeof(pubkey));
|
||||
memset(srv, '\x43', sizeof(srv));
|
||||
hs_build_hsdir_index(&pubkey, srv, period_num, hsdir_index);
|
||||
tt_mem_op(hsdir_index, OP_EQ, test_vector, sizeof(hsdir_index));
|
||||
}
|
||||
|
||||
done:
|
||||
;
|
||||
}
|
||||
|
||||
#define EARLY_IN_SRV_TO_TP 0
|
||||
#define LATE_IN_SRV_TO_TP 1
|
||||
#define EARLY_IN_TP_TO_SRV 2
|
||||
@ -1751,6 +1796,9 @@ struct testcase_t hs_common_tests[] = {
|
||||
NULL, NULL },
|
||||
{ "client_service_hsdir_set_sync", test_client_service_hsdir_set_sync,
|
||||
TT_FORK, NULL, NULL },
|
||||
{ "hs_indexes", test_hs_indexes, TT_FORK,
|
||||
NULL, NULL },
|
||||
|
||||
END_OF_TESTCASES
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user