[tor-commits] [tor/master] Add encode/decode functions for ed25519 public keys
nickm at torproject.org
nickm at torproject.org
Thu Sep 25 19:12:41 UTC 2014
commit c75e275574b7a3496cd5050ef75f8d82d075642e
Author: Nick Mathewson <nickm at torproject.org>
Date: Mon Sep 30 13:38:12 2013 -0400
Add encode/decode functions for ed25519 public keys
---
src/common/crypto_ed25519.h | 9 ++++++++-
src/common/crypto_format.c | 22 ++++++++++++++++++++++
src/test/test_crypto.c | 30 ++++++++++++++++++++++++++----
3 files changed, 56 insertions(+), 5 deletions(-)
diff --git a/src/common/crypto_ed25519.h b/src/common/crypto_ed25519.h
index a68f2ec..35f0125 100644
--- a/src/common/crypto_ed25519.h
+++ b/src/common/crypto_ed25519.h
@@ -69,8 +69,15 @@ int ed25519_checksig_batch(int *okay_out,
int n_checkable);
#endif
+#define ED25519_BASE64_LEN 43
+
+int ed25519_public_from_base64(ed25519_public_key_t *pkey,
+ const char *input);
+int ed25519_public_to_base64(char *output,
+ const ed25519_public_key_t *pkey);
+
/* XXXX write secret keys to disk, load secret keys from disk, read encrypted,
- * write encrypted. serialize public. parse public. */
+ * write encrypted. */
#endif
diff --git a/src/common/crypto_format.c b/src/common/crypto_format.c
index be669c8..a9f104c 100644
--- a/src/common/crypto_format.c
+++ b/src/common/crypto_format.c
@@ -9,6 +9,7 @@
#endif
#include "crypto.h"
#include "crypto_curve25519.h"
+#include "crypto_ed25519.h"
#include "util.h"
#include "torlog.h"
@@ -43,3 +44,24 @@ curve25519_public_from_base64(curve25519_public_key_t *pkey,
}
}
+/** Try to decode the string <b>input</b> into an ed25519 public key. On
+ * success, store the value in <b>pkey</b> and return 0. Otherwise return
+ * -1. */
+int
+ed25519_public_from_base64(ed25519_public_key_t *pkey,
+ const char *input)
+{
+ return digest256_from_base64((char*)pkey->pubkey, input);
+}
+
+/** Encode the public key <b>pkey</b> into the buffer at <b>output</b>,
+ * which must have space for ED25519_BASE64_LEN bytes of encoded key,
+ * plus one byte for a terminating NUL. Return 0 on success, -1 on failure.
+ */
+int
+ed25519_public_to_base64(char *output,
+ const ed25519_public_key_t *pkey)
+{
+ return digest256_to_base64(output, (const char *)pkey->pubkey);
+}
+
diff --git a/src/test/test_crypto.c b/src/test/test_crypto.c
index a4ca609..5b2ce45 100644
--- a/src/test/test_crypto.c
+++ b/src/test/test_crypto.c
@@ -1341,12 +1341,33 @@ test_crypto_ed25519_test_vectors(void *arg)
tor_free(mem_op_hex_tmp);
}
-/* XXX
- Check known values for secret->public, for public,msg->signature.
- */
+#endif
+static void
+test_crypto_ed25519_encode(void *arg)
+{
+ char buf[ED25519_BASE64_LEN+1];
+ ed25519_keypair_t kp;
+ ed25519_public_key_t pk;
+ char *mem_op_hex_tmp = NULL;
+ (void) arg;
-#endif
+ /* Test roundtrip. */
+ tt_int_op(0, ==, ed25519_keypair_generate(&kp, 0));
+ tt_int_op(0, ==, ed25519_public_to_base64(buf, &kp.pubkey));
+ tt_int_op(ED25519_BASE64_LEN, ==, strlen(buf));
+ tt_int_op(0, ==, ed25519_public_from_base64(&pk, buf));
+ test_memeq(kp.pubkey.pubkey, pk.pubkey, ED25519_PUBKEY_LEN);
+
+ /* Test known value. */
+ tt_int_op(0, ==, ed25519_public_from_base64(&pk,
+ "lVIuIctLjbGZGU5wKMNXxXlSE3cW4kaqkqm04u6pxvM"));
+ test_memeq_hex(pk.pubkey,
+ "95522e21cb4b8db199194e7028c357c57952137716e246aa92a9b4e2eea9c6f3");
+
+ done:
+ tor_free(mem_op_hex_tmp);
+}
static void
test_crypto_siphash(void *arg)
@@ -1487,6 +1508,7 @@ struct testcase_t crypto_tests[] = {
{ "curve25519_persist", test_crypto_curve25519_persist, 0, NULL, NULL },
{ "ed25519_simple", test_crypto_ed25519_simple, 0, NULL, NULL },
{ "ed25519_test_vectors", test_crypto_ed25519_test_vectors, 0, NULL, NULL },
+ { "ed25519_encode", test_crypto_ed25519_encode, 0, NULL, NULL },
#endif
{ "siphash", test_crypto_siphash, 0, NULL, NULL },
END_OF_TESTCASES
More information about the tor-commits
mailing list