test_ntor_cl.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Copyright (c) 2012-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define ONION_NTOR_PRIVATE
  7. #include "or.h"
  8. #include "util.h"
  9. #include "compat.h"
  10. #include "crypto.h"
  11. #include "crypto_curve25519.h"
  12. #include "onion_ntor.h"
  13. #define N_ARGS(n) STMT_BEGIN { \
  14. if (argc < (n)) { \
  15. fprintf(stderr, "%s needs %d arguments.\n",argv[1],n); \
  16. return 1; \
  17. } \
  18. } STMT_END
  19. #define BASE16(idx, var, n) STMT_BEGIN { \
  20. const char *s = argv[(idx)]; \
  21. if (base16_decode((char*)var, n, s, strlen(s)) < (int)n ) { \
  22. fprintf(stderr, "couldn't decode argument %d (%s)\n",idx,s); \
  23. return 1; \
  24. } \
  25. } STMT_END
  26. #define INT(idx, var) STMT_BEGIN { \
  27. var = atoi(argv[(idx)]); \
  28. if (var <= 0) { \
  29. fprintf(stderr, "bad integer argument %d (%s)\n",idx,argv[(idx)]); \
  30. } \
  31. } STMT_END
  32. static int
  33. client1(int argc, char **argv)
  34. {
  35. /* client1 nodeID B -> msg state */
  36. curve25519_public_key_t B;
  37. uint8_t node_id[DIGEST_LEN];
  38. ntor_handshake_state_t *state = NULL;
  39. uint8_t msg[NTOR_ONIONSKIN_LEN];
  40. char buf[1024];
  41. N_ARGS(4);
  42. BASE16(2, node_id, DIGEST_LEN);
  43. BASE16(3, B.public_key, CURVE25519_PUBKEY_LEN);
  44. if (onion_skin_ntor_create(node_id, &B, &state, msg)<0) {
  45. fprintf(stderr, "handshake failed");
  46. return 2;
  47. }
  48. base16_encode(buf, sizeof(buf), (const char*)msg, sizeof(msg));
  49. printf("%s\n", buf);
  50. base16_encode(buf, sizeof(buf), (void*)state, sizeof(*state));
  51. printf("%s\n", buf);
  52. ntor_handshake_state_free(state);
  53. return 0;
  54. }
  55. static int
  56. server1(int argc, char **argv)
  57. {
  58. uint8_t msg_in[NTOR_ONIONSKIN_LEN];
  59. curve25519_keypair_t kp;
  60. di_digest256_map_t *keymap=NULL;
  61. uint8_t node_id[DIGEST_LEN];
  62. int keybytes;
  63. uint8_t msg_out[NTOR_REPLY_LEN];
  64. uint8_t *keys = NULL;
  65. char *hexkeys = NULL;
  66. int result = 0;
  67. char buf[256];
  68. /* server1: b nodeID msg N -> msg keys */
  69. N_ARGS(6);
  70. BASE16(2, kp.seckey.secret_key, CURVE25519_SECKEY_LEN);
  71. BASE16(3, node_id, DIGEST_LEN);
  72. BASE16(4, msg_in, NTOR_ONIONSKIN_LEN);
  73. INT(5, keybytes);
  74. curve25519_public_key_generate(&kp.pubkey, &kp.seckey);
  75. dimap_add_entry(&keymap, kp.pubkey.public_key, &kp);
  76. keys = tor_malloc(keybytes);
  77. hexkeys = tor_malloc(keybytes*2+1);
  78. if (onion_skin_ntor_server_handshake(
  79. msg_in, keymap, NULL, node_id, msg_out, keys,
  80. (size_t)keybytes)<0) {
  81. fprintf(stderr, "handshake failed");
  82. result = 2;
  83. goto done;
  84. }
  85. base16_encode(buf, sizeof(buf), (const char*)msg_out, sizeof(msg_out));
  86. printf("%s\n", buf);
  87. base16_encode(hexkeys, keybytes*2+1, (const char*)keys, keybytes);
  88. printf("%s\n", hexkeys);
  89. done:
  90. tor_free(keys);
  91. tor_free(hexkeys);
  92. dimap_free(keymap, NULL);
  93. return result;
  94. }
  95. static int
  96. client2(int argc, char **argv)
  97. {
  98. struct ntor_handshake_state_t state;
  99. uint8_t msg[NTOR_REPLY_LEN];
  100. int keybytes;
  101. uint8_t *keys;
  102. char *hexkeys;
  103. int result = 0;
  104. N_ARGS(5);
  105. BASE16(2, (&state), sizeof(state));
  106. BASE16(3, msg, sizeof(msg));
  107. INT(4, keybytes);
  108. keys = tor_malloc(keybytes);
  109. hexkeys = tor_malloc(keybytes*2+1);
  110. if (onion_skin_ntor_client_handshake(&state, msg, keys, keybytes, NULL)<0) {
  111. fprintf(stderr, "handshake failed");
  112. result = 2;
  113. goto done;
  114. }
  115. base16_encode(hexkeys, keybytes*2+1, (const char*)keys, keybytes);
  116. printf("%s\n", hexkeys);
  117. done:
  118. tor_free(keys);
  119. tor_free(hexkeys);
  120. return result;
  121. }
  122. int
  123. main(int argc, char **argv)
  124. {
  125. /*
  126. client1: nodeID B -> msg state
  127. server1: b nodeID msg N -> msg keys
  128. client2: state msg N -> keys
  129. */
  130. if (argc < 2) {
  131. fprintf(stderr, "I need arguments. Read source for more info.\n");
  132. return 1;
  133. }
  134. curve25519_init();
  135. if (!strcmp(argv[1], "client1")) {
  136. return client1(argc, argv);
  137. } else if (!strcmp(argv[1], "server1")) {
  138. return server1(argc, argv);
  139. } else if (!strcmp(argv[1], "client2")) {
  140. return client2(argc, argv);
  141. } else {
  142. fprintf(stderr, "What's a %s?\n", argv[1]);
  143. return 1;
  144. }
  145. }