test_ntor_cl.c 4.7 KB

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