aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2013-01-14 12:32:00 -0500
committerNick Mathewson <nickm@torproject.org>2013-01-14 12:32:00 -0500
commitc9242f4fd4c0ae783946db77b9ecc6214d41bd72 (patch)
tree54aa6dc09c8ee4f64b468265e3856cc77523ecf6 /src/common
parentd357b97b6d7dcd98229218549245fcc3fcbee8f7 (diff)
parent31d888c834b135d8f36ceec181f3d1ea7af62267 (diff)
downloadtor-c9242f4fd4c0ae783946db77b9ecc6214d41bd72.tar.gz
tor-c9242f4fd4c0ae783946db77b9ecc6214d41bd72.zip
Merge branch 'bug7869'
Diffstat (limited to 'src/common')
-rw-r--r--src/common/crypto_curve25519.c30
-rw-r--r--src/common/crypto_curve25519.h7
2 files changed, 37 insertions, 0 deletions
diff --git a/src/common/crypto_curve25519.c b/src/common/crypto_curve25519.c
index a4ab65cf4f..aead4f8baa 100644
--- a/src/common/crypto_curve25519.c
+++ b/src/common/crypto_curve25519.c
@@ -178,3 +178,33 @@ curve25519_handshake(uint8_t *output,
curve25519_impl(output, skey->secret_key, pkey->public_key);
}
+int
+curve25519_public_to_base64(char *output,
+ const curve25519_public_key_t *pkey)
+{
+ char buf[128];
+ base64_encode(buf, sizeof(buf),
+ (const char*)pkey->public_key, CURVE25519_PUBKEY_LEN);
+ buf[CURVE25519_BASE64_PADDED_LEN] = '\0';
+ memcpy(output, buf, CURVE25519_BASE64_PADDED_LEN+1);
+ return 0;
+}
+
+int
+curve25519_public_from_base64(curve25519_public_key_t *pkey,
+ const char *input)
+{
+ size_t len = strlen(input);
+ if (len == CURVE25519_BASE64_PADDED_LEN - 1) {
+ /* not padded */
+ return digest256_from_base64((char*)pkey->public_key, input);
+ } else if (len == CURVE25519_BASE64_PADDED_LEN) {
+ char buf[128];
+ if (base64_decode(buf, sizeof(buf), input, len) != CURVE25519_PUBKEY_LEN)
+ return -1;
+ memcpy(pkey->public_key, buf, CURVE25519_PUBKEY_LEN);
+ return 0;
+ } else {
+ return -1;
+ }
+}
diff --git a/src/common/crypto_curve25519.h b/src/common/crypto_curve25519.h
index e768b8c427..9c732b9c46 100644
--- a/src/common/crypto_curve25519.h
+++ b/src/common/crypto_curve25519.h
@@ -51,6 +51,13 @@ int curve25519_keypair_read_from_file(curve25519_keypair_t *keypair_out,
char **tag_out,
const char *fname);
+#define CURVE25519_BASE64_PADDED_LEN 44
+
+int curve25519_public_from_base64(curve25519_public_key_t *pkey,
+ const char *input);
+int curve25519_public_to_base64(char *output,
+ const curve25519_public_key_t *pkey);
+
#ifdef CRYPTO_CURVE25519_PRIVATE
int curve25519_impl(uint8_t *output, const uint8_t *secret,
const uint8_t *basepoint);