test_ntor_cl.c 4.6 KB

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