privatetopublic.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <http://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, 65);
  67. crypto_scalarmult_curve25519_base(address.key, privateKey);
  68. AddressCalc_addressForPublicKey(address.ip6.bytes, address.key);
  69. if (address.ip6.bytes[0] == 0xFC) {
  70. Base32_encode(publicKeyBase32Out, 53, address.key, 32);
  71. Address_printShortIp(addressOut, &address);
  72. printf( "Input privkey: %s\n"
  73. "Matching pubkey: %s.k\n"
  74. "Resulting address: %s\n"
  75. ,privateKeyHexIn,publicKeyBase32Out,addressOut);
  76. return 0;
  77. } else {
  78. fprintf(stderr, "Not a valid cjdns address\n");
  79. return 1;
  80. }
  81. }