test_hs_ntor_cl.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Copyright (c) 2017-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /** This is a wrapper over the little-t-tor HS ntor functions. The wrapper is
  4. * used by src/test/hs_ntor_ref.py to conduct the HS ntor integration
  5. * tests.
  6. *
  7. * The logic of this wrapper is basically copied from src/test/test_ntor_cl.c
  8. */
  9. #include "orconfig.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #define ONION_NTOR_PRIVATE
  13. #include "or/or.h"
  14. #include "common/util.h"
  15. #include "lib/crypt_ops/crypto.h"
  16. #include "lib/crypt_ops/crypto_curve25519.h"
  17. #include "or/hs_ntor.h"
  18. #include "or/onion_ntor.h"
  19. #define N_ARGS(n) STMT_BEGIN { \
  20. if (argc < (n)) { \
  21. fprintf(stderr, "%s needs %d arguments.\n",argv[1],n); \
  22. return 1; \
  23. } \
  24. } STMT_END
  25. #define BASE16(idx, var, n) STMT_BEGIN { \
  26. const char *s = argv[(idx)]; \
  27. if (base16_decode((char*)var, n, s, strlen(s)) < (int)n ) { \
  28. fprintf(stderr, "couldn't decode argument %d (%s)\n",idx,s); \
  29. return 1; \
  30. } \
  31. } STMT_END
  32. #define INT(idx, var) STMT_BEGIN { \
  33. var = atoi(argv[(idx)]); \
  34. if (var <= 0) { \
  35. fprintf(stderr, "bad integer argument %d (%s)\n",idx,argv[(idx)]); \
  36. } \
  37. } STMT_END
  38. /** The first part of the HS ntor protocol. The client-side computes all
  39. necessary key material and sends the appropriate message to the service. */
  40. static int
  41. client1(int argc, char **argv)
  42. {
  43. int retval;
  44. /* Inputs */
  45. curve25519_public_key_t intro_enc_pubkey;
  46. ed25519_public_key_t intro_auth_pubkey;
  47. curve25519_keypair_t client_ephemeral_enc_keypair;
  48. uint8_t subcredential[DIGEST256_LEN];
  49. /* Output */
  50. hs_ntor_intro_cell_keys_t hs_ntor_intro_cell_keys;
  51. char buf[256];
  52. N_ARGS(6);
  53. BASE16(2, intro_auth_pubkey.pubkey, ED25519_PUBKEY_LEN);
  54. BASE16(3, intro_enc_pubkey.public_key, CURVE25519_PUBKEY_LEN);
  55. BASE16(4, client_ephemeral_enc_keypair.seckey.secret_key,
  56. CURVE25519_SECKEY_LEN);
  57. BASE16(5, subcredential, DIGEST256_LEN);
  58. /* Generate keypair */
  59. curve25519_public_key_generate(&client_ephemeral_enc_keypair.pubkey,
  60. &client_ephemeral_enc_keypair.seckey);
  61. retval = hs_ntor_client_get_introduce1_keys(&intro_auth_pubkey,
  62. &intro_enc_pubkey,
  63. &client_ephemeral_enc_keypair,
  64. subcredential,
  65. &hs_ntor_intro_cell_keys);
  66. if (retval < 0) {
  67. goto done;
  68. }
  69. /* Send ENC_KEY */
  70. base16_encode(buf, sizeof(buf),
  71. (const char*)hs_ntor_intro_cell_keys.enc_key,
  72. sizeof(hs_ntor_intro_cell_keys.enc_key));
  73. printf("%s\n", buf);
  74. /* Send MAC_KEY */
  75. base16_encode(buf, sizeof(buf),
  76. (const char*)hs_ntor_intro_cell_keys.mac_key,
  77. sizeof(hs_ntor_intro_cell_keys.mac_key));
  78. printf("%s\n", buf);
  79. done:
  80. return retval;
  81. }
  82. /** The second part of the HS ntor protocol. The service-side computes all
  83. necessary key material and sends the appropriate message to the client */
  84. static int
  85. server1(int argc, char **argv)
  86. {
  87. int retval;
  88. /* Inputs */
  89. curve25519_keypair_t intro_enc_keypair;
  90. ed25519_public_key_t intro_auth_pubkey;
  91. curve25519_public_key_t client_ephemeral_enc_pubkey;
  92. uint8_t subcredential[DIGEST256_LEN];
  93. /* Output */
  94. hs_ntor_intro_cell_keys_t hs_ntor_intro_cell_keys;
  95. hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys;
  96. curve25519_keypair_t service_ephemeral_rend_keypair;
  97. char buf[256];
  98. N_ARGS(6);
  99. BASE16(2, intro_auth_pubkey.pubkey, ED25519_PUBKEY_LEN);
  100. BASE16(3, intro_enc_keypair.seckey.secret_key, CURVE25519_SECKEY_LEN);
  101. BASE16(4, client_ephemeral_enc_pubkey.public_key, CURVE25519_PUBKEY_LEN);
  102. BASE16(5, subcredential, DIGEST256_LEN);
  103. /* Generate keypair */
  104. curve25519_public_key_generate(&intro_enc_keypair.pubkey,
  105. &intro_enc_keypair.seckey);
  106. curve25519_keypair_generate(&service_ephemeral_rend_keypair, 0);
  107. /* Get INTRODUCE1 keys */
  108. retval = hs_ntor_service_get_introduce1_keys(&intro_auth_pubkey,
  109. &intro_enc_keypair,
  110. &client_ephemeral_enc_pubkey,
  111. subcredential,
  112. &hs_ntor_intro_cell_keys);
  113. if (retval < 0) {
  114. goto done;
  115. }
  116. /* Get RENDEZVOUS1 keys */
  117. retval = hs_ntor_service_get_rendezvous1_keys(&intro_auth_pubkey,
  118. &intro_enc_keypair,
  119. &service_ephemeral_rend_keypair,
  120. &client_ephemeral_enc_pubkey,
  121. &hs_ntor_rend_cell_keys);
  122. if (retval < 0) {
  123. goto done;
  124. }
  125. /* Send ENC_KEY */
  126. base16_encode(buf, sizeof(buf),
  127. (const char*)hs_ntor_intro_cell_keys.enc_key,
  128. sizeof(hs_ntor_intro_cell_keys.enc_key));
  129. printf("%s\n", buf);
  130. /* Send MAC_KEY */
  131. base16_encode(buf, sizeof(buf),
  132. (const char*)hs_ntor_intro_cell_keys.mac_key,
  133. sizeof(hs_ntor_intro_cell_keys.mac_key));
  134. printf("%s\n", buf);
  135. /* Send AUTH_MAC */
  136. base16_encode(buf, sizeof(buf),
  137. (const char*)hs_ntor_rend_cell_keys.rend_cell_auth_mac,
  138. sizeof(hs_ntor_rend_cell_keys.rend_cell_auth_mac));
  139. printf("%s\n", buf);
  140. /* Send NTOR_KEY_SEED */
  141. base16_encode(buf, sizeof(buf),
  142. (const char*)hs_ntor_rend_cell_keys.ntor_key_seed,
  143. sizeof(hs_ntor_rend_cell_keys.ntor_key_seed));
  144. printf("%s\n", buf);
  145. /* Send service ephemeral pubkey (Y) */
  146. base16_encode(buf, sizeof(buf),
  147. (const char*)service_ephemeral_rend_keypair.pubkey.public_key,
  148. sizeof(service_ephemeral_rend_keypair.pubkey.public_key));
  149. printf("%s\n", buf);
  150. done:
  151. return retval;
  152. }
  153. /** The final step of the ntor protocol, the client computes and returns the
  154. * rendezvous key material. */
  155. static int
  156. client2(int argc, char **argv)
  157. {
  158. int retval;
  159. /* Inputs */
  160. curve25519_public_key_t intro_enc_pubkey;
  161. ed25519_public_key_t intro_auth_pubkey;
  162. curve25519_keypair_t client_ephemeral_enc_keypair;
  163. curve25519_public_key_t service_ephemeral_rend_pubkey;
  164. uint8_t subcredential[DIGEST256_LEN];
  165. /* Output */
  166. hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys;
  167. char buf[256];
  168. N_ARGS(7);
  169. BASE16(2, intro_auth_pubkey.pubkey, ED25519_PUBKEY_LEN);
  170. BASE16(3, client_ephemeral_enc_keypair.seckey.secret_key,
  171. CURVE25519_SECKEY_LEN);
  172. BASE16(4, intro_enc_pubkey.public_key, CURVE25519_PUBKEY_LEN);
  173. BASE16(5, service_ephemeral_rend_pubkey.public_key, CURVE25519_PUBKEY_LEN);
  174. BASE16(6, subcredential, DIGEST256_LEN);
  175. /* Generate keypair */
  176. curve25519_public_key_generate(&client_ephemeral_enc_keypair.pubkey,
  177. &client_ephemeral_enc_keypair.seckey);
  178. /* Get RENDEZVOUS1 keys */
  179. retval = hs_ntor_client_get_rendezvous1_keys(&intro_auth_pubkey,
  180. &client_ephemeral_enc_keypair,
  181. &intro_enc_pubkey,
  182. &service_ephemeral_rend_pubkey,
  183. &hs_ntor_rend_cell_keys);
  184. if (retval < 0) {
  185. goto done;
  186. }
  187. /* Send AUTH_MAC */
  188. base16_encode(buf, sizeof(buf),
  189. (const char*)hs_ntor_rend_cell_keys.rend_cell_auth_mac,
  190. sizeof(hs_ntor_rend_cell_keys.rend_cell_auth_mac));
  191. printf("%s\n", buf);
  192. /* Send NTOR_KEY_SEED */
  193. base16_encode(buf, sizeof(buf),
  194. (const char*)hs_ntor_rend_cell_keys.ntor_key_seed,
  195. sizeof(hs_ntor_rend_cell_keys.ntor_key_seed));
  196. printf("%s\n", buf);
  197. done:
  198. return 1;
  199. }
  200. /** Perform a different part of the protocol depdning on the argv used. */
  201. int
  202. main(int argc, char **argv)
  203. {
  204. if (argc < 2) {
  205. fprintf(stderr, "I need arguments. Read source for more info.\n");
  206. return 1;
  207. }
  208. curve25519_init();
  209. if (!strcmp(argv[1], "client1")) {
  210. return client1(argc, argv);
  211. } else if (!strcmp(argv[1], "server1")) {
  212. return server1(argc, argv);
  213. } else if (!strcmp(argv[1], "client2")) {
  214. return client2(argc, argv);
  215. } else {
  216. fprintf(stderr, "What's a %s?\n", argv[1]);
  217. return 1;
  218. }
  219. }