summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2004-07-01 01:16:59 +0000
committerNick Mathewson <nickm@torproject.org>2004-07-01 01:16:59 +0000
commit541add90a16a2b7b75e5aa3a54071f3a5db00502 (patch)
treee6fbde49f0a12931e102a5211651fd014d06f9b9 /src/common
parentf42f04c859a68dab45d021dd4197da816ec72b07 (diff)
downloadtor-541add90a16a2b7b75e5aa3a54071f3a5db00502.tar.gz
tor-541add90a16a2b7b75e5aa3a54071f3a5db00502.zip
Track routers by hash of identity key; use hex hash of identity key in place of nickname; accept (and use) hash of identity key in EXTEND cells.
svn:r1994
Diffstat (limited to 'src/common')
-rw-r--r--src/common/crypto.c60
-rw-r--r--src/common/crypto.h4
2 files changed, 62 insertions, 2 deletions
diff --git a/src/common/crypto.c b/src/common/crypto.c
index 17e71487a8..93a4738cff 100644
--- a/src/common/crypto.c
+++ b/src/common/crypto.c
@@ -837,8 +837,8 @@ int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out)
* space).
*
* Fingerprints are computed as the SHA1 digest of the ASN.1 encoding
- * of the public key, converted to hexadecimal, with a space after every
- * four digits.
+ * of the public key, converted to hexadecimal, in upper case, with a
+ * space after every four digits.
*/
int
crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out)
@@ -1433,6 +1433,62 @@ base32_encode(char *dest, int destlen, const char *src, int srclen)
return 0;
}
+int base16_encode(char *dest, int destlen, const char *src, int srclen)
+{
+ const char *end;
+ char *cp;
+
+ if (destlen < srclen*2+1)
+ return -1;
+
+ cp = dest;
+ end = src+srclen;
+ while (src<end) {
+ sprintf(cp,"%02X",*(const uint8_t*)src);
+ ++src;
+ cp += 2;
+ }
+ *dest = '\0';
+ return 0;
+}
+
+static const char HEX_DIGITS[] = "0123456789ABCDEFabcdef";
+
+static INLINE int hex_decode_digit(char c)
+{
+ const char *cp;
+ int n;
+ cp = strchr(HEX_DIGITS, c);
+ if (!cp)
+ return -1;
+ n = cp-HEX_DIGITS;
+ if (n<=15)
+ return n; /* digit or uppercase */
+ else
+ return n-6; /* lowercase */
+}
+
+int base16_decode(char *dest, int destlen, const char *src, int srclen)
+{
+ const char *end;
+ int v1,v2;
+ if ((srclen % 2) != 0)
+ return -1;
+ if (destlen < srclen/2)
+ return -1;
+ end = src+srclen;
+ while (src<end) {
+ v1 = hex_decode_digit(*src);
+ v2 = hex_decode_digit(*(src+1));
+ if(v1<0||v2<0)
+ return -1;
+ *(uint8_t*)dest = (v1<<4)|v2;
+ ++dest;
+ }
+ *dest = '\0';
+ return 0;
+}
+
/*
Local Variables:
mode:c
diff --git a/src/common/crypto.h b/src/common/crypto.h
index fa8380075e..09bb50f05a 100644
--- a/src/common/crypto.h
+++ b/src/common/crypto.h
@@ -37,6 +37,8 @@
/** Length of encoded public key fingerprints, including space; but not
* including terminating NUL. */
#define FINGERPRINT_LEN 49
+/** Length of hex encoding of SHA1 digest, not including final NUL. */
+#define HEX_DIGEST_LEN 40
typedef struct crypto_pk_env_t crypto_pk_env_t;
typedef struct crypto_cipher_env_t crypto_cipher_env_t;
@@ -91,6 +93,8 @@ int base64_encode(char *dest, int destlen, const char *src, int srclen);
int base64_decode(char *dest, int destlen, const char *src, int srclen);
#define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
int base32_encode(char *dest, int destlen, const char *src, int srclen);
+int base16_encode(char *dest, int destlen, const char *src, int srclen);
+int base16_decode(char *dest, int destlen, const char *src, int srclen);
/* Key negotiation */
crypto_dh_env_t *crypto_dh_new();