symm/{chacha,salsa20}.c: Change how the test code sets up the cipher.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 26 May 2016 08:26:09 +0000 (09:26 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 22 Apr 2017 19:44:05 +0000 (20:44 +0100)
Introduce a macro which does the key, nonce and position setup in one
go.

symm/chacha.c
symm/salsa20.c

index 45f7cf8..5fc1c12 100644 (file)
@@ -811,16 +811,26 @@ CHACHA_VARS(DEFXGRAND)
 CHACHA_VARS(DEFVCORE)
 
 #define CHACHA_CTX(r) chacha_ctx
-#define CHACHA_INIT(r, ctx, k, ksz, n) chacha_init(ctx, k, ksz, n)
-#define CHACHA_SEEKU64(r, ctx, i) chacha_seeku64(ctx, i)
-#define XCHACHA_SEEKU64(r, ctx, i) xchacha##r##_seeku64(ctx, i)
+
+#define CHACHA_TESTSETUP(r, ctx, k, ksz, n, nsz, p, psz) do {          \
+  kludge64 pos64;                                                      \
+  chacha_init(ctx, k, ksz, 0);                                         \
+  if (nsz == 8) chacha_setnonce(ctx, n);                               \
+  if (psz == 8) { LOAD64_(pos64, p); chacha_seeku64(ctx, pos64); }     \
+} while (0)
+
+#define XCHACHA_TESTSETUP(r, ctx, k, ksz, n, nsz, p, psz) do {         \
+  kludge64 pos64;                                                      \
+  XCHACHA_INIT(r, ctx, k, ksz, 0);                                     \
+  if (nsz == 24) XCHACHA_SETNONCE(r, ctx, n);                          \
+  if (psz == 8) { LOAD64_(pos64, p); xchacha##r##_seeku64(ctx, pos64); } \
+} while (0)
 
 #define DEFxVENC(base, BASE, r)                                                \
   static int v_encrypt_##base##_##r(dstr *v)                           \
   {                                                                    \
     BASE##_CTX(r) ctx;                                                 \
     dstr d = DSTR_INIT;                                                        \
-    kludge64 pos;                                                      \
     const octet *p, *p0;                                               \
     octet *q;                                                          \
     size_t sz, sz0, step;                                              \
@@ -836,11 +846,8 @@ CHACHA_VARS(DEFVCORE)
     while (step < sz0 + skip) {                                                \
       step = step ? 3*step + 4 : 1;                                    \
       if (step > sz0 + skip) step = sz0 + skip;                                \
-      BASE##_INIT(r, &ctx, v[0].buf, v[0].len, v[1].buf);              \
-      if (v[2].len) {                                                  \
-       LOAD64_(pos, v[2].buf);                                         \
-       BASE##_SEEKU64(r, &ctx, pos);                                   \
-      }                                                                        \
+      BASE##_TESTSETUP(r, &ctx, v[0].buf, v[0].len,                    \
+                      v[1].buf, v[1].len, v[2].buf, v[2].len);         \
                                                                        \
       for (sz = skip; sz >= step; sz -= step)                          \
        BASE##_ENCRYPT(r, &ctx, 0, 0, step);                            \
index 9dc95e8..b8c630d 100644 (file)
@@ -841,15 +841,26 @@ static const int perm[] = {
 SALSA20_VARS(DEFVCORE)
 
 #define SALSA20_CTX(r) salsa20_ctx
-#define SALSA20_INIT(r, ctx, k, ksz, n) salsa20_init(ctx, k, ksz, n)
-#define SALSA20_SEEKU64(r, ctx, i) salsa20_seeku64(ctx, i)
+
+#define SALSA20_TESTSETUP(r, ctx, k, ksz, n, nsz, p, psz) do {         \
+  kludge64 pos64;                                                      \
+  salsa20_init(ctx, k, ksz, 0);                                                \
+  if (nsz == 8) salsa20_setnonce(ctx, n);                              \
+  if (psz == 8) { LOAD64_(pos64, p); salsa20_seeku64(ctx, pos64); }    \
+} while (0)
+
+#define XSALSA20_TESTSETUP(r, ctx, k, ksz, n, nsz, p, psz) do {                \
+  kludge64 pos64;                                                      \
+  XSALSA20_INIT(r, ctx, k, ksz, 0);                                    \
+  if (nsz == 24) XSALSA20_SETNONCE(r, ctx, n);                         \
+  if (psz == 8) { LOAD64_(pos64, p); XSALSA20_SEEKU64(r, ctx, pos64); }        \
+} while (0)
 
 #define DEFxVENC(base, BASE, r)                                                \
   static int v_encrypt_##base##_##r(dstr *v)                           \
   {                                                                    \
     BASE##_CTX(r) ctx;                                                 \
     dstr d = DSTR_INIT;                                                        \
-    kludge64 pos;                                                      \
     const octet *p, *p0;                                               \
     octet *q;                                                          \
     size_t sz, sz0, step;                                              \
@@ -865,11 +876,8 @@ SALSA20_VARS(DEFVCORE)
     while (step < sz0 + skip) {                                                \
       step = step ? 3*step + 4 : 1;                                    \
       if (step > sz0 + skip) step = sz0 + skip;                                \
-      BASE##_INIT(r, &ctx, v[0].buf, v[0].len, v[1].buf);              \
-      if (v[2].len) {                                                  \
-       LOAD64_(pos, v[2].buf);                                         \
-       BASE##_SEEKU64(r, &ctx, pos);                                   \
-      }                                                                        \
+      BASE##_TESTSETUP(r, &ctx, v[0].buf, v[0].len,                    \
+                      v[1].buf, v[1].len, v[2].buf, v[2].len);         \
                                                                        \
       for (sz = skip; sz >= step; sz -= step)                          \
        BASE##_ENCRYPT(r, &ctx, 0, 0, step);                            \