privatetopublic.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/AddressCalc.h"
  16. #include "dht/Address.h"
  17. #include "util/Base32.h"
  18. #include "util/Hex.h"
  19. #include <sodium/crypto_scalarmult_curve25519.h>
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. static int usage(char* appName)
  25. {
  26. printf("Usage: %s\n"
  27. "\n"
  28. "As private keys are very sensitive, %s reads them from stdin.\n"
  29. "If your shell, terminal, or other program keeps history,\n"
  30. "please avoid echo 'key' | %s, or similar constructs.\n"
  31. "A heredoc is suitable for use in scripts, and will avoid\n"
  32. "unwanted revealing of the key in process lists, like so:\n"
  33. "%s <<EOF\n"
  34. "key\n"
  35. "EOF\n"
  36. "\n"
  37. "Key should be in hex form, maximum 64 characters.\n"
  38. "Extra characters will be silently ignored.\n",
  39. appName, appName, appName, appName);
  40. return 0;
  41. }
  42. // This is invoked from privatetopublic.rs
  43. int privatetopublic_main(int argc, char** argv);
  44. int privatetopublic_main(int argc, char** argv)
  45. {
  46. struct Address address = {0};
  47. uint8_t addressOut[40];
  48. uint8_t privateKey[32];
  49. uint8_t publicKeyBase32Out[53];
  50. uint8_t privateKeyHexIn[65] = {0};
  51. if (argc > 1)
  52. {
  53. return usage(argv[0]);
  54. }
  55. if (read(0,privateKeyHexIn,64) < 0)
  56. {
  57. fprintf(stderr, "Reading private key failed: %s\n", strerror(errno));
  58. return 1;
  59. }
  60. for (uint8_t* n = privateKeyHexIn;n < privateKeyHexIn + 65;n++)
  61. {
  62. if ('\n' == *n)
  63. {
  64. *n = '\0';
  65. break;
  66. }
  67. }
  68. Hex_decode(privateKey, 32, privateKeyHexIn, 64);
  69. crypto_scalarmult_curve25519_base(address.key, privateKey);
  70. if (AddressCalc_addressForPublicKey(address.ip6.bytes, address.key)) {
  71. Base32_encode(publicKeyBase32Out, 53, address.key, 32);
  72. Address_printShortIp(addressOut, &address);
  73. printf( "Input privkey: %s\n"
  74. "Matching pubkey: %s.k\n"
  75. "Resulting address: %s\n"
  76. ,privateKeyHexIn,publicKeyBase32Out,addressOut);
  77. return 0;
  78. } else {
  79. fprintf(stderr, "Not a valid cjdns address\n");
  80. return 1;
  81. }
  82. }