Browse Source

now base16_encode() and base32_encode() can't ever fail

svn:r2103
Roger Dingledine 21 years ago
parent
commit
7459d067a5
7 changed files with 23 additions and 47 deletions
  1. 5 11
      src/common/crypto.c
  2. 2 2
      src/common/crypto.h
  3. 1 0
      src/common/util.c
  4. 1 2
      src/or/rendcommon.c
  5. 2 7
      src/or/rendmid.c
  6. 9 20
      src/or/rendservice.c
  7. 3 5
      src/or/test.c

+ 5 - 11
src/common/crypto.c

@@ -1406,20 +1406,16 @@ base64_decode(char *dest, int destlen, const char *src, int srclen)
 }
 
 /** Implements base32 encoding as in rfc3548.  Limitation: Requires
- * that srclen is a multiple of 5.
+ * that srclen*8 is a multiple of 5.
  */
-int
+void
 base32_encode(char *dest, int destlen, const char *src, int srclen)
 {
   int nbits, i, bit, v, u;
   nbits = srclen * 8;
 
-  if ((nbits%5) != 0)
-    /* We need an even multiple of 5 bits. */
-    return -1;
-  if ((nbits/5)+1 > destlen)
-    /* Not enough space. */
-    return -1;
+  tor_assert((nbits%5) == 0); /* We need an even multiple of 5 bits. */
+  tor_assert((nbits/5)+1 <= destlen); /* We need enough space. */
 
   for (i=0,bit=0; bit < nbits; ++i, bit+=5) {
     /* set v to the 16-bit value starting at src[bits/8], 0-padded. */
@@ -1430,10 +1426,9 @@ base32_encode(char *dest, int destlen, const char *src, int srclen)
     dest[i] = BASE32_CHARS[u];
   }
   dest[i] = '\0';
-  return 0;
 }
 
-int base16_encode(char *dest, int destlen, const char *src, int srclen)
+void base16_encode(char *dest, int destlen, const char *src, int srclen)
 {
   const char *end;
   char *cp;
@@ -1448,7 +1443,6 @@ int base16_encode(char *dest, int destlen, const char *src, int srclen)
     cp += 2;
   }
   *cp = '\0';
-  return 0;
 }
 
 static const char HEX_DIGITS[] = "0123456789ABCDEFabcdef";

+ 2 - 2
src/common/crypto.h

@@ -92,8 +92,8 @@ 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 "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);
+void base32_encode(char *dest, int destlen, const char *src, int srclen);
+void 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 */

+ 1 - 0
src/common/util.c

@@ -1254,6 +1254,7 @@ int check_private_dir(const char *dirname, int create)
 {
   int r;
   struct stat st;
+  tor_assert(dirname);
   if (stat(dirname, &st)) {
     if (errno != ENOENT) {
       log(LOG_WARN, "Directory %s cannot be read: %s", dirname,

+ 1 - 2
src/or/rendcommon.c

@@ -146,8 +146,7 @@ int rend_get_service_id(crypto_pk_env_t *pk, char *out)
   tor_assert(pk);
   if (crypto_pk_get_digest(pk, buf) < 0)
     return -1;
-  if (base32_encode(out, REND_SERVICE_ID_LEN+1, buf, 10) < 0)
-    return -1;
+  base32_encode(out, REND_SERVICE_ID_LEN+1, buf, 10);
   return 0;
 }
 

+ 2 - 7
src/or/rendmid.c

@@ -69,10 +69,7 @@ rend_mid_establish_intro(circuit_t *circ, const char *request, int request_len)
     goto err;
   }
 
-  if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
-                    pk_digest,10)) {
-    goto err;
-  }
+  base32_encode(serviceid, REND_SERVICE_ID_LEN+1, pk_digest,10);
 
   /* Close any other intro circuits with the same pk. */
   c = NULL;
@@ -133,9 +130,7 @@ rend_mid_introduce(circuit_t *circ, const char *request, int request_len)
     goto err;
   }
 
-  if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request,10)) {
-    goto err;
-  }
+  base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request,10);
 
   /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
   intro_circ = circuit_get_next_by_pk_and_purpose(

+ 9 - 20
src/or/rendservice.c

@@ -353,10 +353,8 @@ rend_service_introduce(circuit_t *circuit, const char *request, int request_len)
   char serviceid[REND_SERVICE_ID_LEN+1];
   char hexcookie[9];
 
-  if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
-                    circuit->rend_pk_digest,10)) {
-    return -1;
-  }
+  base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
+                circuit->rend_pk_digest,10);
   log_fn(LOG_INFO, "Received INTRODUCE2 cell for service %s on circ %d",
          serviceid, circuit->n_circ_id);
 
@@ -382,9 +380,7 @@ rend_service_introduce(circuit_t *circuit, const char *request, int request_len)
     return -1;
   }
   if (memcmp(circuit->rend_pk_digest, request, DIGEST_LEN)) {
-    if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request, 10)) {
-      return -1;
-    }
+    base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request, 10);
     log_fn(LOG_WARN, "Got an INTRODUCE2 cell for the wrong service (%s)",
            serviceid);
     return -1;
@@ -552,10 +548,8 @@ rend_service_intro_has_opened(circuit_t *circuit)
   tor_assert(circuit->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
   tor_assert(CIRCUIT_IS_ORIGIN(circuit) && circuit->cpath);
 
-  if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
-                    circuit->rend_pk_digest,10)) {
-    tor_assert(0);
-  }
+  base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
+                circuit->rend_pk_digest,10);
 
   service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
   if (!service) {
@@ -644,10 +638,8 @@ rend_service_rendezvous_has_opened(circuit_t *circuit)
   tor_assert(hop);
 
   hex_encode(circuit->rend_cookie, 4, hexcookie);
-  if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
-                    circuit->rend_pk_digest,10)) {
-    tor_assert(0);
-  }
+  base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
+                circuit->rend_pk_digest,10);
 
   log_fn(LOG_INFO,
        "Done building circuit %d to rendezvous with cookie %s for service %s",
@@ -933,11 +925,8 @@ rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ)
 
   tor_assert(circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
   log_fn(LOG_DEBUG,"beginning to hunt for addr/port");
-  if (base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
-                    circ->rend_pk_digest,10)) {
-    log_fn(LOG_WARN,"bug: base32 failed");
-    return -1;
-  }
+  base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
+                circ->rend_pk_digest,10);
   service = rend_service_get_by_pk_digest(circ->rend_pk_digest);
   if (!service) {
     log_fn(LOG_WARN, "Couldn't find any service associated with pk %s on rendezvous circuit %d; closing",

+ 3 - 5
src/or/test.c

@@ -412,18 +412,16 @@ test_crypto()
    *        [00110101 01100011 01101000 01110010 01110011]
    * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
    */
-  i = base32_encode(data2, 9, data1, 5);
+  base32_encode(data2, 9, data1, 5);
   test_streq(data2, "gvrwq4tt");
 
   strcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4");
-  i = base32_encode(data2, 30, data1, 10);
-  test_eq(i,0);
+  base32_encode(data2, 30, data1, 10);
   test_streq(data2, "772w2rfobvomsywe");
 
   /* Base16 tests */
   strcpy(data1, "6chrs\xff");
-  i = base16_encode(data2, 13, data1, 6);
-  test_eq(i,0);
+  base16_encode(data2, 13, data1, 6);
   test_streq(data2, "3663687273FF");
 
   strcpy(data1, "f0d678affc000100");