test_hs_ntor.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_ntor.c
  5. * \brief Test hidden service ntor functionality.
  6. */
  7. #include "test.h"
  8. #include "test_helpers.h"
  9. #include "log_test_helpers.h"
  10. #include "hs_ntor.h"
  11. /* Test the HS ntor handshake. Simulate the sending of an encrypted INTRODUCE1
  12. * cell, and verify the proper derivation of decryption keys on the other end.
  13. * Then simulate the sending of an authenticated RENDEZVOUS1 cell and verify
  14. * the proper verification on the other end. */
  15. static void
  16. test_hs_ntor(void *arg)
  17. {
  18. int retval;
  19. uint8_t subcredential[DIGEST256_LEN];
  20. ed25519_keypair_t service_intro_auth_keypair;
  21. curve25519_keypair_t service_intro_enc_keypair;
  22. curve25519_keypair_t service_ephemeral_rend_keypair;
  23. curve25519_keypair_t client_ephemeral_enc_keypair;
  24. hs_ntor_intro_cell_keys_t client_hs_ntor_intro_cell_keys;
  25. hs_ntor_intro_cell_keys_t service_hs_ntor_intro_cell_keys;
  26. hs_ntor_rend_cell_keys_t service_hs_ntor_rend_cell_keys;
  27. hs_ntor_rend_cell_keys_t client_hs_ntor_rend_cell_keys;
  28. (void) arg;
  29. /* Generate fake data for this unittest */
  30. {
  31. /* Generate fake subcredential */
  32. memset(subcredential, 'Z', DIGEST256_LEN);
  33. /* service */
  34. curve25519_keypair_generate(&service_intro_enc_keypair, 0);
  35. ed25519_keypair_generate(&service_intro_auth_keypair, 0);
  36. curve25519_keypair_generate(&service_ephemeral_rend_keypair, 0);
  37. /* client */
  38. curve25519_keypair_generate(&client_ephemeral_enc_keypair, 0);
  39. }
  40. /* Client: Simulate the sending of an encrypted INTRODUCE1 cell */
  41. retval =
  42. hs_ntor_client_get_introduce1_keys(&service_intro_auth_keypair.pubkey,
  43. &service_intro_enc_keypair.pubkey,
  44. &client_ephemeral_enc_keypair,
  45. subcredential,
  46. &client_hs_ntor_intro_cell_keys);
  47. tt_int_op(retval, ==, 0);
  48. /* Service: Simulate the decryption of the received INTRODUCE1 */
  49. retval =
  50. hs_ntor_service_get_introduce1_keys(&service_intro_auth_keypair.pubkey,
  51. &service_intro_enc_keypair,
  52. &client_ephemeral_enc_keypair.pubkey,
  53. subcredential,
  54. &service_hs_ntor_intro_cell_keys);
  55. tt_int_op(retval, ==, 0);
  56. /* Test that the INTRODUCE1 encryption/mac keys match! */
  57. tt_mem_op(client_hs_ntor_intro_cell_keys.enc_key, OP_EQ,
  58. service_hs_ntor_intro_cell_keys.enc_key,
  59. CIPHER256_KEY_LEN);
  60. tt_mem_op(client_hs_ntor_intro_cell_keys.mac_key, OP_EQ,
  61. service_hs_ntor_intro_cell_keys.mac_key,
  62. DIGEST256_LEN);
  63. /* Service: Simulate creation of RENDEZVOUS1 key material. */
  64. retval =
  65. hs_ntor_service_get_rendezvous1_keys(&service_intro_auth_keypair.pubkey,
  66. &service_intro_enc_keypair,
  67. &service_ephemeral_rend_keypair,
  68. &client_ephemeral_enc_keypair.pubkey,
  69. &service_hs_ntor_rend_cell_keys);
  70. tt_int_op(retval, ==, 0);
  71. /* Client: Simulate the verification of a received RENDEZVOUS1 cell */
  72. retval =
  73. hs_ntor_client_get_rendezvous1_keys(&service_intro_auth_keypair.pubkey,
  74. &client_ephemeral_enc_keypair,
  75. &service_intro_enc_keypair.pubkey,
  76. &service_ephemeral_rend_keypair.pubkey,
  77. &client_hs_ntor_rend_cell_keys);
  78. tt_int_op(retval, ==, 0);
  79. /* Test that the RENDEZVOUS1 key material match! */
  80. tt_mem_op(client_hs_ntor_rend_cell_keys.rend_cell_auth_mac, OP_EQ,
  81. service_hs_ntor_rend_cell_keys.rend_cell_auth_mac,
  82. DIGEST256_LEN);
  83. tt_mem_op(client_hs_ntor_rend_cell_keys.ntor_key_seed, OP_EQ,
  84. service_hs_ntor_rend_cell_keys.ntor_key_seed,
  85. DIGEST256_LEN);
  86. done:
  87. ;
  88. }
  89. struct testcase_t hs_ntor_tests[] = {
  90. { "hs_ntor", test_hs_ntor, TT_FORK,
  91. NULL, NULL },
  92. END_OF_TESTCASES
  93. };