Initialize the register dumping machinery while testing assembler code.
[catacomb] / symm / chacha.c
index a554c19..8cc16b8 100644 (file)
@@ -41,6 +41,7 @@
 #include "grand.h"
 #include "keysz.h"
 #include "paranoia.h"
+#include "rsvr.h"
 
 /*----- Global variables --------------------------------------------------*/
 
@@ -96,7 +97,8 @@ static core__functype *pick_core(void)
                     cpu_feature_p(CPUFEAT_ARM_NEON));
 #endif
 #if CPUFAM_ARM64
-  DISPATCH_PICK_COND(chacha_core, chacha_core_arm64, 1);
+  DISPATCH_PICK_COND(chacha_core, chacha_core_arm64,
+                    cpu_feature_p(CPUFEAT_ARM_NEON));
 #endif
   DISPATCH_PICK_FALLBACK(chacha_core, simple_core);
 }
@@ -152,6 +154,8 @@ static void populate(chacha_matrix a, const void *key, size_t ksz)
 
 /*----- ChaCha implementation ---------------------------------------------*/
 
+static const octet zerononce[XCHACHA_NONCESZ];
+
 /* --- @chacha_init@ --- *
  *
  * Arguments:  @chacha_ctx *ctx@ = context to fill in
@@ -167,8 +171,6 @@ static void populate(chacha_matrix a, const void *key, size_t ksz)
 void chacha_init(chacha_ctx *ctx, const void *key, size_t ksz,
                  const void *nonce)
 {
-  static const octet zerononce[CHACHA_NONCESZ];
-
   populate(ctx->a, key, ksz);
   chacha_setnonce(ctx, nonce ? nonce : zerononce);
 }
@@ -264,6 +266,8 @@ uint32 chacha_tell_ietf(chacha_ctx *ctx)
  *             to @dest@.
  */
 
+static const rsvr_policy policy = { 0, CHACHA_OUTSZ, CHACHA_OUTSZ };
+
 #define CHACHA_ENCRYPT(r, ctx, src, dest, sz)                          \
   chacha##r##_encrypt(ctx, src, dest, sz)
 #define DEFENCRYPT(r)                                                  \
@@ -273,41 +277,40 @@ uint32 chacha_tell_ietf(chacha_ctx *ctx)
     chacha_matrix b;                                                   \
     const octet *s = src;                                              \
     octet *d = dest;                                                   \
-    size_t n;                                                          \
+    rsvr_plan plan;                                                    \
     kludge64 pos, delta;                                               \
                                                                        \
-    SALSA20_OUTBUF(ctx, d, s, sz);                                     \
-    if (!sz) return;                                                   \
-                                                                       \
-    if (!dest) {                                                       \
-      n = sz/CHACHA_OUTSZ;                                             \
-      pos = chacha_tellu64(ctx);                                       \
-      ASSIGN64(delta, n);                                              \
-      ADD64(pos, pos, delta);                                          \
-      chacha_seeku64(ctx, pos);                                                \
-      sz = sz%CHACHA_OUTSZ;                                            \
-    } else if (!src) {                                                 \
-      while (sz >= CHACHA_OUTSZ) {                                     \
-       core(r, ctx->a, b);                                             \
-       CHACHA_STEP(ctx->a);                                            \
-       SALSA20_GENFULL(b, d);                                          \
-       sz -= CHACHA_OUTSZ;                                             \
+    rsvr_mkplan(&plan, &policy, ctx->off, sz);                         \
+                                                                       \
+    if (plan.head) {                                                   \
+      if (!ctx->off) {                                                 \
+       core(r, ctx->a, b); CHACHA_STEP(ctx->a);                        \
+       SALSA20_PREPBUF(ctx, b);                                        \
       }                                                                        \
-    } else {                                                           \
-      while (sz >= CHACHA_OUTSZ) {                                     \
-       core(r, ctx->a, b);                                             \
-       CHACHA_STEP(ctx->a);                                            \
-       SALSA20_MIXFULL(b, d, s);                                       \
-       sz -= CHACHA_OUTSZ;                                             \
+      SALSA20_OUTBUF(ctx, d, s, plan.head);                            \
+    }                                                                  \
+                                                                       \
+    ctx->off -= plan.from_rsvr;                                                \
+                                                                       \
+    if (!d) {                                                          \
+      if (plan.from_input) {                                           \
+       pos = chacha_tellu64(ctx);                                      \
+       ASSIGN64(delta, plan.from_input/SALSA20_OUTSZ);                 \
+       ADD64(pos, pos, delta);                                         \
+       chacha_seeku64(ctx, pos);                                       \
       }                                                                        \
+    } else if (!s) while (plan.from_input) {                           \
+      core(r, ctx->a, b); CHACHA_STEP(ctx->a);                         \
+      SALSA20_GENFULL(b, d); plan.from_input -= CHACHA_OUTSZ;          \
+    } else while (plan.from_input) {                                   \
+      core(r, ctx->a, b); CHACHA_STEP(ctx->a);                         \
+      SALSA20_MIXFULL(b, d, s); plan.from_input -= CHACHA_OUTSZ;       \
     }                                                                  \
                                                                        \
-    if (sz) {                                                          \
-      core(r, ctx->a, b);                                              \
-      CHACHA_STEP(ctx->a);                                             \
+    if (plan.tail) {                                                   \
+      core(r, ctx->a, b); CHACHA_STEP(ctx->a);                         \
       SALSA20_PREPBUF(ctx, b);                                         \
-      SALSA20_OUTBUF(ctx, d, s, sz);                                   \
-      assert(!sz);                                                     \
+      SALSA20_OUTBUF(ctx, d, s, plan.tail);                            \
     }                                                                  \
   }
 CHACHA_VARS(DEFENCRYPT)
@@ -403,8 +406,6 @@ CHACHA_VARS(DEFHCHACHA)
   void XCHACHA_INIT(r, XCHACHA_CTX(r) *ctx,                            \
                    const void *key, size_t ksz, const void *nonce)     \
   {                                                                    \
-    static const octet zerononce[XCHACHA_NONCESZ];                     \
-                                                                       \
     populate(ctx->k, key, ksz);                                                \
     ctx->s.a[ 0] = CHACHA_A256;                                                \
     ctx->s.a[ 1] = CHACHA_B256;                                                \
@@ -593,7 +594,7 @@ CHACHA_VARS(DEFGCIPHER)
                                                                        \
   const gccipher xchacha##r = {                                                \
     "xchacha" #r, chacha_keysz,                                                \
-    CHACHA_NONCESZ, gxinit_##r                                         \
+    XCHACHA_NONCESZ, gxinit_##r                                                \
   };
 
 CHACHA_VARS(DEFGXCIPHER)
@@ -848,9 +849,14 @@ CHACHA_VARS(DEFXGRAND)
 #include <stdio.h>
 #include <string.h>
 
+#include <mLib/macros.h>
 #include <mLib/quis.h>
 #include <mLib/testrig.h>
 
+#ifdef ENABLE_ASM_DEBUG
+#  include "regdump.h"
+#endif
+
 #define DEFVCORE(r)                                                    \
   static int v_core_##r(dstr *v)                                       \
   {                                                                    \
@@ -869,7 +875,7 @@ CHACHA_VARS(DEFXGRAND)
     }                                                                  \
     for (i = 0; i < CHACHA_OUTSZ/4; i++) STORE32_L(d.buf + 4*i, a[i]); \
                                                                        \
-    if (d.len != v[2].len || memcmp(d.buf, v[2].buf, v[2].len) != 0) { \
+    if (d.len != v[2].len || MEMCMP(d.buf, !=, v[2].buf, v[2].len)) {  \
       ok = 0;                                                          \
       printf("\nfail core:"                                            \
             "\n\titerations = %d"                                      \
@@ -939,7 +945,7 @@ CHACHA_VARS(DEFVCORE)
       }                                                                        \
       if (sz) BASE##_ENCRYPT(r, &ctx, p, q, sz);                       \
                                                                        \
-      if (d.len != v[5].len || memcmp(d.buf, v[5].buf, v[5].len) != 0) { \
+      if (d.len != v[5].len || MEMCMP(d.buf, !=, v[5].buf, v[5].len)) {        \
        ok = 0;                                                         \
        printf("\nfail encrypt:"                                        \
               "\n\tstep           = %lu"                               \
@@ -985,6 +991,9 @@ CHACHA_VARS(DEFXTAB)
 
 int main(int argc, char *argv[])
 {
+#ifdef ENABLE_ASM_DEBUG
+  regdump_init();
+#endif
   test_run(argc, argv, defs, SRCDIR"/t/chacha");
   return (0);
 }