crypto_format.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright (c) 2012-2015, 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 "crypto_ed25519.h"
  11. #include "util.h"
  12. #include "torlog.h"
  13. int
  14. curve25519_public_to_base64(char *output,
  15. const curve25519_public_key_t *pkey)
  16. {
  17. char buf[128];
  18. base64_encode(buf, sizeof(buf),
  19. (const char*)pkey->public_key, CURVE25519_PUBKEY_LEN);
  20. buf[CURVE25519_BASE64_PADDED_LEN] = '\0';
  21. memcpy(output, buf, CURVE25519_BASE64_PADDED_LEN+1);
  22. return 0;
  23. }
  24. int
  25. curve25519_public_from_base64(curve25519_public_key_t *pkey,
  26. const char *input)
  27. {
  28. size_t len = strlen(input);
  29. if (len == CURVE25519_BASE64_PADDED_LEN - 1) {
  30. /* not padded */
  31. return digest256_from_base64((char*)pkey->public_key, input);
  32. } else if (len == CURVE25519_BASE64_PADDED_LEN) {
  33. char buf[128];
  34. if (base64_decode(buf, sizeof(buf), input, len) != CURVE25519_PUBKEY_LEN)
  35. return -1;
  36. memcpy(pkey->public_key, buf, CURVE25519_PUBKEY_LEN);
  37. return 0;
  38. } else {
  39. return -1;
  40. }
  41. }
  42. /** Try to decode the string <b>input</b> into an ed25519 public key. On
  43. * success, store the value in <b>pkey</b> and return 0. Otherwise return
  44. * -1. */
  45. int
  46. ed25519_public_from_base64(ed25519_public_key_t *pkey,
  47. const char *input)
  48. {
  49. return digest256_from_base64((char*)pkey->pubkey, input);
  50. }
  51. /** Encode the public key <b>pkey</b> into the buffer at <b>output</b>,
  52. * which must have space for ED25519_BASE64_LEN bytes of encoded key,
  53. * plus one byte for a terminating NUL. Return 0 on success, -1 on failure.
  54. */
  55. int
  56. ed25519_public_to_base64(char *output,
  57. const ed25519_public_key_t *pkey)
  58. {
  59. return digest256_to_base64(output, (const char *)pkey->pubkey);
  60. }
  61. /** Encode the signature <b>sig</b> into the buffer at <b>output</b>,
  62. * which must have space for ED25519_SIG_BASE64_LEN bytes of encoded signature,
  63. * plus one byte for a terminating NUL. Return 0 on success, -1 on failure.
  64. */
  65. int
  66. ed25519_signature_to_base64(char *output,
  67. const ed25519_signature_t *sig)
  68. {
  69. char buf[256];
  70. int n = base64_encode_nopad(buf, sizeof(buf), sig->sig, ED25519_SIG_LEN);
  71. tor_assert(n == ED25519_SIG_BASE64_LEN);
  72. memcpy(output, buf, ED25519_SIG_BASE64_LEN+1);
  73. return 0;
  74. }
  75. /** Try to decode the string <b>input</b> into an ed25519 signature. On
  76. * success, store the value in <b>sig</b> and return 0. Otherwise return
  77. * -1. */
  78. int
  79. ed25519_signature_from_base64(ed25519_signature_t *sig,
  80. const char *input)
  81. {
  82. if (strlen(input) != ED25519_SIG_BASE64_LEN)
  83. return -1;
  84. char buf[ED25519_SIG_BASE64_LEN+3];
  85. memcpy(buf, input, ED25519_SIG_BASE64_LEN);
  86. buf[ED25519_SIG_BASE64_LEN+0] = '=';
  87. buf[ED25519_SIG_BASE64_LEN+1] = '=';
  88. buf[ED25519_SIG_BASE64_LEN+2] = 0;
  89. char decoded[128];
  90. int n = base64_decode(decoded, sizeof(decoded), buf, strlen(buf));
  91. if (n < 0 || n != ED25519_SIG_LEN)
  92. return -1;
  93. memcpy(sig->sig, decoded, ED25519_SIG_LEN);
  94. return 0;
  95. }