1
0

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 <http://www.gnu.org/licenses/>.
  14. */
  15. #define string_strerror
  16. #include "crypto/AddressCalc.h"
  17. #include "util/Base32.h"
  18. #include "util/Hex.h"
  19. #include "net/Ducttape.h"
  20. #include "util/platform/libc/string.h"
  21. #include "crypto_scalarmult_curve25519.h"
  22. #include <unistd.h>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. static int usage(char* appName)
  26. {
  27. printf("Usage: %s\n"
  28. "\n"
  29. "As private keys are very sensitive, %s reads them from stdin.\n"
  30. "If your shell, terminal, or other program keeps history,\n"
  31. "please avoid echo 'key' | %s, or similar constructs.\n"
  32. "A heredoc is suitable for use in scripts, and will avoid\n"
  33. "unwanted revealing of the key in process lists, like so:\n"
  34. "%s <<EOF\n"
  35. "key\n"
  36. "EOF\n"
  37. "\n"
  38. "Key should be in hex form, maximum 64 characters.\n"
  39. "Extra characters will be silently ignored.\n",
  40. appName, appName, appName, appName);
  41. return 0;
  42. }
  43. int main(int argc, char** argv)
  44. {
  45. struct Address address;
  46. uint8_t addressOut[40];
  47. uint8_t privateKey[32];
  48. uint8_t publicKeyBase32Out[53];
  49. uint8_t privateKeyHexIn[65] = {0};
  50. if (argc > 1)
  51. {
  52. return usage(argv[0]);
  53. }
  54. if (read(0,privateKeyHexIn,64) < 0)
  55. {
  56. fprintf(stderr, "Reading private key failed: %s\n", strerror(errno));
  57. return 1;
  58. }
  59. for (uint8_t* n = privateKeyHexIn;n < privateKeyHexIn + 65;n++)
  60. {
  61. if ('\n' == *n)
  62. {
  63. *n = '\0';
  64. break;
  65. }
  66. }
  67. Hex_decode(privateKey, 32, privateKeyHexIn, 65);
  68. crypto_scalarmult_curve25519_base(address.key, privateKey);
  69. AddressCalc_addressForPublicKey(address.ip6.bytes, address.key);
  70. if (address.ip6.bytes[0] == 0xFC) {
  71. Base32_encode(publicKeyBase32Out, 53, address.key, 32);
  72. Address_printIp(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. }