catcrypt.c, catsign.c: Shorten chunk sizes.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 22 Jan 2012 13:12:14 +0000 (13:12 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 22 Jan 2012 13:12:14 +0000 (13:12 +0000)
The chunks are written with a 16-bit length, so the maximum chunk size
is 2^16 - 1 = 65535.  Unfortunately, catsign tried to write 65536-byte
chunks, and catcrypt tried to cram a MAC tag in there too.  The result
is that chunk_write fails an assertion because the chunks are too big.

No idea why this ever worked before.

catcrypt.c
catsign.c

index a1e3495..6e4f245 100644 (file)
@@ -133,7 +133,7 @@ static int encrypt(int argc, char *argv[])
   const char *err;
   int i;
   int en;
-  size_t n;
+  size_t n, chsz;
   dstr d = DSTR_INIT;
   octet *tag, *ct;
   buf b;
@@ -261,6 +261,7 @@ static int encrypt(int argc, char *argv[])
   assert(GC_CLASS(c)->blksz <= sizeof(bb));
   dstr_ensure(&d, sizeof(bb) + GM_CLASS(m)->hashsz);
   seq = 0;
+  chsz = MASK16 - GM_CLASS(m)->hashsz;
   for (;;) {
     h = GM_INIT(m);
     GH_HASHU32(h, seq);
@@ -269,7 +270,7 @@ static int encrypt(int argc, char *argv[])
       GC_ENCRYPT(cx, 0, bb, GC_CLASS(c)->blksz);
       GC_SETIV(c, bb);
     }
-    n = fread(bb, 1, sizeof(bb), fp);
+    n = fread(bb, 1, chsz, fp);
     if (!n) break;
     buf_init(&b, d.buf, d.sz);
     tag = buf_get(&b, GM_CLASS(m)->hashsz);
index 5c6a6f8..8171404 100644 (file)
--- a/catsign.c
+++ b/catsign.c
@@ -199,7 +199,7 @@ static void textwrite(msgcanon *m, const void *bp, size_t n)
 static size_t binreadembed(msgcanon *m, void *bp)
   { return (chunk_read(m->e, bp)); }
 static size_t binreaddetach(msgcanon *m, void *bp)
-  { return (fread(bp, 1, MSGBUFSZ, m->fp)); }
+  { return (fread(bp, 1, MSGBUFSZ - 1, m->fp)); }
 
 static void binwriteembed(msgcanon *m, const void *bp, size_t n)
   { chunk_write(m->e, bp, n); }