From: Mark Wooding Date: Thu, 26 May 2016 08:26:09 +0000 (+0100) Subject: symm/{chacha,salsa20}.c: Abstract out cipher and rand initialization. X-Git-Tag: 2.4.0~41 X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/commitdiff_plain/d9ce319f383299b7beac7be505d6dd5ee233ebbc symm/{chacha,salsa20}.c: Abstract out cipher and rand initialization. --- diff --git a/symm/chacha.c b/symm/chacha.c index d64b0d1c..45f7cf82 100644 --- a/symm/chacha.c +++ b/symm/chacha.c @@ -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) \ diff --git a/symm/salsa20.c b/symm/salsa20.c index fb58d36f..9dc95e8b 100644 --- a/symm/salsa20.c +++ b/symm/salsa20.c @@ -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) \