CryptoAddress_test.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/CString.h"
  18. #include "util/Bits.h"
  19. #include "util/Base32.h"
  20. #include "util/Hex.h"
  21. #include "util/Assert.h"
  22. #include "crypto_scalarmult_curve25519.h"
  23. #include <stdio.h>
  24. // hex: 751d3db85b848deaf221e0ed2b6cc17f587b29057d74cdd4dc0bd18b7157288e
  25. static const uint8_t privateKey[32] = {
  26. 0x75, 0x1d, 0x3d, 0xb8, 0x5b, 0x84, 0x8d, 0xea,
  27. 0xf2, 0x21, 0xe0, 0xed, 0x2b, 0x6c, 0xc1, 0x7f,
  28. 0x58, 0x7b, 0x29, 0x05, 0x7d, 0x74, 0xcd, 0xd4,
  29. 0xdc, 0x0b, 0xd1, 0x8b, 0x71, 0x57, 0x28, 0x8e
  30. };
  31. // hex: d7c0df45001a5be5e81c95e519be5199055237cb9116882cadcefe48ab735173
  32. // base32: r6jzx210usqbgnm3pdtm1z6btd14pvdtkn5j8qnpgqzknpggkuw0
  33. static const uint8_t publicKey[32] = {
  34. 0xd7, 0xc0, 0xdf, 0x45, 0x00, 0x1a, 0x5b, 0xe5,
  35. 0xe8, 0x1c, 0x95, 0xe5, 0x19, 0xbe, 0x51, 0x99,
  36. 0x05, 0x52, 0x37, 0xcb, 0x91, 0x16, 0x88, 0x2c,
  37. 0xad, 0xce, 0xfe, 0x48, 0xab, 0x73, 0x51, 0x73
  38. };
  39. static const char publicKeyBase32[] = "r6jzx210usqbgnm3pdtm1z6btd14pvdtkn5j8qnpgqzknpggkuw0";
  40. static const char ipv6[] = "fc68:cb2c:60db:cb96:19ac:34a8:fd34:03fc";
  41. int main()
  42. {
  43. /* verify public key */
  44. struct Address address;
  45. crypto_scalarmult_curve25519_base(address.key, privateKey);
  46. AddressCalc_addressForPublicKey(address.ip6.bytes, address.key);
  47. uint8_t privateKeyHexOut[65];
  48. uint8_t publicKeyHexOut[65];
  49. uint8_t publicKeyBase32Out[53];
  50. Hex_encode(privateKeyHexOut, 65, privateKey, 32);
  51. Hex_encode(publicKeyHexOut, 65, publicKey, 32);
  52. printf("Private key %s (hex)\n\nExpect:\nPublic Key: %s (hex)\n"
  53. "Public Key: %s (base32)\nAddress: %s\n",
  54. privateKeyHexOut,
  55. publicKeyHexOut,
  56. publicKeyBase32,
  57. ipv6);
  58. uint8_t addressOut[40];
  59. Hex_encode(publicKeyHexOut, 65, address.key, 32);
  60. Base32_encode(publicKeyBase32Out, 53, address.key, 32);
  61. Address_printIp(addressOut, &address);
  62. printf("\nGot:\nPublic Key: %s (hex)\n"
  63. "Public Key: %s (base32)\nAddress: %s\n",
  64. publicKeyHexOut,
  65. publicKeyBase32Out,
  66. addressOut);
  67. Assert_true(0 == Bits_memcmp(address.key, publicKey, 32));
  68. Assert_true(0 == CString_strcmp(publicKeyBase32, (char*) publicKeyBase32Out));
  69. Assert_true(0 == CString_strcmp(ipv6, (char*) addressOut));
  70. return 0;
  71. }