test_hs_ntor.c 4.3 KB

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