Key.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #ifndef Key_H
  16. #define Key_H
  17. #include "crypto/random/Random.h"
  18. #include "memory/Allocator.h"
  19. #include "util/Linker.h"
  20. Linker_require("crypto/Key.c")
  21. #include <stdint.h>
  22. /**
  23. * Generates a new key such that its derived IP address is in fc00::/8.
  24. */
  25. int Key_gen(uint8_t addressOut[16],
  26. uint8_t publicKeyOut[32],
  27. uint8_t privateKeyOut[32],
  28. struct Random* rand);
  29. char* Key_parse_strerror(int error);
  30. #define Key_parse_TOO_SHORT -1
  31. #define Key_parse_MALFORMED -2
  32. #define Key_parse_DECODE_FAILED -3
  33. /** Invalid cjdns key (doesn't hash to an address beginning with FC) */
  34. #define Key_parse_INVALID -4
  35. /**
  36. * Parse a key.
  37. *
  38. * @param key a public key similar to h9xgk0418x538kg2h5yw4n32rkl4wk4wplkzsmltpg20q78bu7q0.k
  39. * @param keyBytesOut a 32 byte array which will be set to the key.
  40. * @param ip6Out a 16 byte array which will be set to the IPv6 address, if NULL it will be skipped
  41. * and the function will not check if the first byte is FC.
  42. * @return an error code or 0 if no error.
  43. */
  44. int Key_parse(String* key, uint8_t keyBytesOut[32], uint8_t ip6Out[16]);
  45. String* Key_stringify(uint8_t key[32], struct Allocator* alloc);
  46. #endif