test_ntor_cl.c 4.6 KB

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