test_ntor_cl.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Copyright (c) 2012, 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;
  42. uint8_t msg[NTOR_ONIONSKIN_LEN];
  43. char buf[1024];
  44. memset(&state, 0, sizeof(state));
  45. N_ARGS(4);
  46. BASE16(2, node_id, DIGEST_LEN);
  47. BASE16(3, B.public_key, CURVE25519_PUBKEY_LEN);
  48. if (onion_skin_ntor_create(node_id, &B, &state, msg)<0) {
  49. fprintf(stderr, "handshake failed");
  50. return 2;
  51. }
  52. base16_encode(buf, sizeof(buf), (const char*)msg, sizeof(msg));
  53. printf("%s\n", buf);
  54. base16_encode(buf, sizeof(buf), (void*)state, sizeof(*state));
  55. printf("%s\n", buf);
  56. ntor_handshake_state_free(state);
  57. return 0;
  58. }
  59. static int
  60. server1(int argc, char **argv)
  61. {
  62. uint8_t msg_in[NTOR_ONIONSKIN_LEN];
  63. curve25519_keypair_t kp;
  64. di_digest256_map_t *keymap=NULL;
  65. uint8_t node_id[DIGEST_LEN];
  66. int keybytes;
  67. uint8_t msg_out[NTOR_REPLY_LEN];
  68. uint8_t *keys;
  69. char *hexkeys;
  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. return 2;
  86. }
  87. base16_encode(buf, sizeof(buf), (const char*)msg_out, sizeof(msg_out));
  88. printf("%s\n", buf);
  89. base16_encode(hexkeys, keybytes*2+1, (const char*)keys, keybytes);
  90. printf("%s\n", hexkeys);
  91. tor_free(keys);
  92. tor_free(hexkeys);
  93. return 0;
  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. N_ARGS(5);
  104. BASE16(2, (&state), sizeof(state));
  105. BASE16(3, msg, sizeof(msg));
  106. INT(4, keybytes);
  107. keys = tor_malloc(keybytes);
  108. hexkeys = tor_malloc(keybytes*2+1);
  109. if (onion_skin_ntor_client_handshake(&state, msg, keys, keybytes)<0) {
  110. fprintf(stderr, "handshake failed");
  111. return 2;
  112. }
  113. base16_encode(hexkeys, keybytes*2+1, (const char*)keys, keybytes);
  114. printf("%s\n", hexkeys);
  115. tor_free(keys);
  116. tor_free(hexkeys);
  117. return 0;
  118. }
  119. int
  120. main(int argc, char **argv)
  121. {
  122. /*
  123. client1: nodeID B -> msg state
  124. server1: b nodeID msg N -> msg keys
  125. client2: state msg N -> keys
  126. */
  127. if (argc < 2) {
  128. fprintf(stderr, "I need arguments. Read source for more info.\n");
  129. return 1;
  130. } else if (!strcmp(argv[1], "client1")) {
  131. return client1(argc, argv);
  132. } else if (!strcmp(argv[1], "server1")) {
  133. return server1(argc, argv);
  134. } else if (!strcmp(argv[1], "client2")) {
  135. return client2(argc, argv);
  136. } else {
  137. fprintf(stderr, "What's a %s?\n", argv[1]);
  138. return 1;
  139. }
  140. }