[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Fix base32 implementation; make base32 implementation follo...



Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv20865/src/common

Modified Files:
	crypto.c crypto.h 
Log Message:
Fix base32 implementation; make base32 implementation follow standard; add more tests for base32

Index: crypto.c
===================================================================
RCS file: /home/or/cvsroot/src/common/crypto.c,v
retrieving revision 1.74
retrieving revision 1.75
diff -u -d -r1.74 -r1.75
--- crypto.c	6 Apr 2004 22:05:48 -0000	1.74
+++ crypto.c	8 Apr 2004 20:56:33 -0000	1.75
@@ -1188,6 +1188,9 @@
   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 @@
   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];

Index: crypto.h
===================================================================
RCS file: /home/or/cvsroot/src/common/crypto.h,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -d -r1.39 -r1.40
--- crypto.h	6 Apr 2004 20:55:46 -0000	1.39
+++ crypto.h	8 Apr 2004 20:56:33 -0000	1.40
@@ -78,7 +78,7 @@
 
 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 */