1
0

privatetopublic.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "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. int main(int argc, char** argv)
  43. {
  44. struct Address address;
  45. uint8_t addressOut[40];
  46. uint8_t privateKey[32];
  47. uint8_t publicKeyBase32Out[53];
  48. uint8_t privateKeyHexIn[65] = {0};
  49. if (argc > 1)
  50. {
  51. return usage(argv[0]);
  52. }
  53. if (read(0,privateKeyHexIn,64) < 0)
  54. {
  55. fprintf(stderr, "Reading private key failed: %s\n", strerror(errno));
  56. return 1;
  57. }
  58. for (uint8_t* n = privateKeyHexIn;n < privateKeyHexIn + 65;n++)
  59. {
  60. if ('\n' == *n)
  61. {
  62. *n = '\0';
  63. break;
  64. }
  65. }
  66. Hex_decode(privateKey, 32, privateKeyHexIn, 64);
  67. crypto_scalarmult_curve25519_base(address.key, privateKey);
  68. if (AddressCalc_addressForPublicKey(address.ip6.bytes, address.key)) {
  69. Base32_encode(publicKeyBase32Out, 53, address.key, 32);
  70. Address_printShortIp(addressOut, &address);
  71. printf( "Input privkey: %s\n"
  72. "Matching pubkey: %s.k\n"
  73. "Resulting address: %s\n"
  74. ,privateKeyHexIn,publicKeyBase32Out,addressOut);
  75. return 0;
  76. } else {
  77. fprintf(stderr, "Not a valid cjdns address\n");
  78. return 1;
  79. }
  80. }