symm/cbc-def.h: Fix discarding output for short inputs.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 30 Oct 2018 10:29:39 +0000 (10:29 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 25 Nov 2018 11:38:04 +0000 (11:38 +0000)
You got a segfault if the input was smaller than the block size and the
destination pointer was null.  We need a temporary place for shuffling
the buffer around anyway, so it seems like the best approach is just to
make a (necessarily small) dummy destination.

symm/cbc-def.h

index 59c76f8..480f579 100644 (file)
@@ -155,7 +155,7 @@ void pre##_cbcencrypt(pre##_cbcctx *ctx,                            \
 {                                                                      \
   const octet *s = src;                                                        \
   octet *d = dest;                                                     \
-  octet b[PRE##_BLKSZ];                                                        \
+  octet b[PRE##_BLKSZ], bb[PRE##_BLKSZ];                               \
   octet y;                                                             \
   unsigned i;                                                          \
                                                                        \
@@ -173,7 +173,8 @@ void pre##_cbcencrypt(pre##_cbcctx *ctx,                            \
   if (sz < PRE##_BLKSZ) {                                              \
     pre##_eblk(&ctx->ctx, ctx->a, ctx->a);                             \
     BLKC_STORE(PRE, b, ctx->a);                                                \
-    if (d) { for (i = 0; i < sz; i++) d[i] = b[i] ^ (s ? s[i] : 0); }  \
+    if (!d) d = bb;                                                    \
+    for (i = 0; i < sz; i++) d[i] = b[i] ^ (s ? s[i] : 0);             \
     memmove(b, b + sz, PRE##_BLKSZ - sz);                              \
     memcpy(b + PRE##_BLKSZ - sz, d, sz);                               \
     BLKC_LOAD(PRE, ctx->a, b);                                         \