summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2004-04-08 20:56:33 +0000
committerNick Mathewson <nickm@torproject.org>2004-04-08 20:56:33 +0000
commitaa7cfd93e53f5d2d01abbc81ce8ee6f6d27ec3c5 (patch)
tree6cee8a54f8800578928b3365d39288ee468808e5 /src/common
parentd23769239651df818c70f26ab88d57fd712c25bc (diff)
downloadtor-aa7cfd93e53f5d2d01abbc81ce8ee6f6d27ec3c5.tar.gz
tor-aa7cfd93e53f5d2d01abbc81ce8ee6f6d27ec3c5.zip
Fix base32 implementation; make base32 implementation follow standard; add more tests for base32
svn:r1574
Diffstat (limited to 'src/common')
-rw-r--r--src/common/crypto.c9
-rw-r--r--src/common/crypto.h2
2 files changed, 7 insertions, 4 deletions
diff --git a/src/common/crypto.c b/src/common/crypto.c
index a6e57129d9..278fe1dedd 100644
--- a/src/common/crypto.c
+++ b/src/common/crypto.c
@@ -1188,6 +1188,9 @@ base64_decode(char *dest, int destlen, const char *src, int srclen)
return ret;
}
+/* Implement base32 encoding as in rfc3548. Limitation: Requires that
+ * srclen is a multiple of 5.
+ */
int
base32_encode(char *dest, int destlen, const char *src, int srclen)
{
@@ -1197,14 +1200,14 @@ base32_encode(char *dest, int destlen, const char *src, int srclen)
if ((nbits%5) != 0)
/* We need an even multiple of 5 bits. */
return -1;
- if ((nbits/5)+1 < destlen)
+ if ((nbits/5)+1 > destlen)
/* Not enough space. */
return -1;
for (i=0,bit=0; bit < nbits; ++i, bit+=5) {
/* set v to the 16-bit value starting at src[bits/8], 0-padded. */
- v = ((unsigned char)src[bit/8]) << 8;
- if (bit+5<nbits) v += src[(bit/8)+1];
+ v = ((uint8_t)src[bit/8]) << 8;
+ if (bit+5<nbits) v += (uint8_t)src[(bit/8)+1];
/* set u to the 5-bit value at the bit'th bit of src. */
u = (v >> (11-(bit%8))) & 0x1F;
dest[i] = BASE32_CHARS[u];
diff --git a/src/common/crypto.h b/src/common/crypto.h
index d9da82a05f..f3bd0e6a8e 100644
--- a/src/common/crypto.h
+++ b/src/common/crypto.h
@@ -78,7 +78,7 @@ int crypto_pk_check_fingerprint_syntax(const char *s);
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 "abcdefghijklmnopqrstuvwxyz012345"
+#define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
int base32_encode(char *dest, int destlen, const char *src, int srclen);
/* Key negotiation */