symm/{chacha,salsa20}.c: Abstract out cipher and rand initialization.
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)
symm/chacha.c
symm/salsa20.c

index d64b0d1..45f7cf8 100644 (file)
@@ -485,17 +485,20 @@ static void gsetiv(gcipher *c, const void *iv)
 static void gdestroy(gcipher *c)
   { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); }
 
+static gcipher *ginit(const void *k, size_t sz, const gcipher_ops *ops)
+{
+  gctx *g = S_CREATE(gctx);
+  g->c.ops = ops;
+  chacha_init(&g->ctx, k, sz, 0);
+  return (&g->c);
+}
+
 #define DEFGCIPHER(r)                                                  \
                                                                        \
   static const gcipher_ops gops_##r;                                   \
                                                                        \
   static gcipher *ginit_##r(const void *k, size_t sz)                  \
-  {                                                                    \
-    gctx *g = S_CREATE(gctx);                                          \
-    g->c.ops = &gops_##r;                                              \
-    chacha_init(&g->ctx, k, sz, 0);                                    \
-    return (&g->c);                                                    \
-  }                                                                    \
+    { return (ginit(k, sz, &gops_##r)); }                              \
                                                                        \
   static void gencrypt_##r(gcipher *c, const void *s,                  \
                           void *t, size_t sz)                          \
@@ -689,6 +692,17 @@ static void gr_setnonce(void *r, const void *n)
 static void grdestroy(grand *r)
   { grctx *g = (grctx *)r; BURN(*g); S_DESTROY(g); }
 
+static grand *grinit(const void *k, size_t ksz, const void *n,
+                    const grand_ops *ops, const grops *myops)
+{
+    grctx *g = S_CREATE(grctx);
+    g->r.r.ops = ops;
+    g->r.ops = myops;
+    chacha_init(&g->ctx, k, ksz, 0);
+    myops->setnonce(g, n);
+    return (&g->r.r);
+}
+
 #define DEFGRAND(rr)                                                   \
                                                                        \
   static void gr_generate_##rr(void *r, void *b, size_t sz)            \
@@ -705,13 +719,7 @@ static void grdestroy(grand *r)
   };                                                                   \
                                                                        \
   grand *chacha##rr##_rand(const void *k, size_t ksz, const void *n)   \
-  {                                                                    \
-    grctx *g = S_CREATE(grctx);                                                \
-    g->r.r.ops = &grops_rand_##rr;                                     \
-    g->r.ops = &grops_##rr;                                            \
-    chacha_init(&g->ctx, k, ksz, n);                                   \
-    return (&g->r.r);                                                  \
-  }
+    { return (grinit(k, ksz, n, &grops_rand_##rr, &grops_##rr)); }
 CHACHA_VARS(DEFGRAND)
 
 #define DEFXGRAND(rr)                                                  \
index fb58d36..9dc95e8 100644 (file)
@@ -505,17 +505,20 @@ static void gsetiv(gcipher *c, const void *iv)
 static void gdestroy(gcipher *c)
   { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); }
 
+static gcipher *ginit(const void *k, size_t sz, const gcipher_ops *ops)
+{
+  gctx *g = S_CREATE(gctx);
+  g->c.ops = ops;
+  salsa20_init(&g->ctx, k, sz, 0);
+  return (&g->c);
+}
+
 #define DEFGCIPHER(r)                                                  \
                                                                        \
   static const gcipher_ops gops_##r;                                   \
                                                                        \
   static gcipher *ginit_##r(const void *k, size_t sz)                  \
-  {                                                                    \
-    gctx *g = S_CREATE(gctx);                                          \
-    g->c.ops = &gops_##r;                                              \
-    salsa20_init(&g->ctx, k, sz, 0);                                   \
-    return (&g->c);                                                    \
-  }                                                                    \
+    { return (ginit(k, sz, &gops_##r)); }                              \
                                                                        \
   static void gencrypt_##r(gcipher *c, const void *s,                  \
                           void *t, size_t sz)                          \
@@ -709,6 +712,17 @@ static void gr_setnonce(void *r, const void *n)
 static void grdestroy(grand *r)
   { grctx *g = (grctx *)r; BURN(*g); S_DESTROY(g); }
 
+static grand *grinit(const void *k, size_t ksz, const void *n,
+                    const grand_ops *ops, const grops *myops)
+{
+    grctx *g = S_CREATE(grctx);
+    g->r.r.ops = ops;
+    g->r.ops = myops;
+    salsa20_init(&g->ctx, k, ksz, 0);
+    myops->setnonce(g, n);
+    return (&g->r.r);
+}
+
 #define DEFGRAND(rr)                                                   \
                                                                        \
   static void gr_generate_##rr(void *r, void *b, size_t sz)            \
@@ -726,13 +740,7 @@ static void grdestroy(grand *r)
                                                                        \
   grand *SALSA20_DECOR(salsa20, rr, _rand)                             \
     (const void *k, size_t ksz, const void *n)                         \
-  {                                                                    \
-    grctx *g = S_CREATE(grctx);                                                \
-    g->r.r.ops = &grops_rand_##rr;                                     \
-    g->r.ops = &grops_##rr;                                            \
-    salsa20_init(&g->ctx, k, ksz, n);                                  \
-    return (&g->r.r);                                                  \
-  }
+    { return (grinit(k, ksz, n, &grops_rand_##rr, &grops_##rr)); }
 SALSA20_VARS(DEFGRAND)
 
 #define DEFXGRAND(rr)                                                  \