crypto_format.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* Copyright (c) 2012-2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /* Formatting and parsing code for crypto-related data structures. */
  4. #include "orconfig.h"
  5. #ifdef HAVE_SYS_STAT_H
  6. #include <sys/stat.h>
  7. #endif
  8. #include "crypto.h"
  9. #include "crypto_curve25519.h"
  10. #include "util.h"
  11. #include "torlog.h"
  12. int
  13. curve25519_public_to_base64(char *output,
  14. const curve25519_public_key_t *pkey)
  15. {
  16. char buf[128];
  17. base64_encode(buf, sizeof(buf),
  18. (const char*)pkey->public_key, CURVE25519_PUBKEY_LEN);
  19. buf[CURVE25519_BASE64_PADDED_LEN] = '\0';
  20. memcpy(output, buf, CURVE25519_BASE64_PADDED_LEN+1);
  21. return 0;
  22. }
  23. int
  24. curve25519_public_from_base64(curve25519_public_key_t *pkey,
  25. const char *input)
  26. {
  27. size_t len = strlen(input);
  28. if (len == CURVE25519_BASE64_PADDED_LEN - 1) {
  29. /* not padded */
  30. return digest256_from_base64((char*)pkey->public_key, input);
  31. } else if (len == CURVE25519_BASE64_PADDED_LEN) {
  32. char buf[128];
  33. if (base64_decode(buf, sizeof(buf), input, len) != CURVE25519_PUBKEY_LEN)
  34. return -1;
  35. memcpy(pkey->public_key, buf, CURVE25519_PUBKEY_LEN);
  36. return 0;
  37. } else {
  38. return -1;
  39. }
  40. }