X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/blobdiff_plain/44ff6c114bdcd5cef753d2788d029ce46e35727a..8c5956c14f5834a072e1a9345ae1f356b14164ca:/symm/salsa20.c diff --git a/symm/salsa20.c b/symm/salsa20.c index 4b35cbd7..e7c35f4f 100644 --- a/symm/salsa20.c +++ b/symm/salsa20.c @@ -5,17 +5,41 @@ * (c) 2015 Straylight/Edgeware */ +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + /*----- Header files ------------------------------------------------------*/ +#include "config.h" + #include #include #include "arena.h" +#include "dispatch.h" #include "gcipher.h" #include "grand.h" #include "keysz.h" #include "paranoia.h" +#include "rsvr.h" #include "salsa20.h" #include "salsa20-core.h" @@ -39,9 +63,46 @@ const octet salsa20_keysz[] = { KSZ_SET, 32, 16, 10, 0 }; * the feedforward step. */ -static void core(unsigned r, const salsa20_matrix src, salsa20_matrix dest) +CPU_DISPATCH(static, (void), void, core, + (unsigned r, const salsa20_matrix src, salsa20_matrix dest), + (r, src, dest), pick_core, simple_core); + +static void simple_core(unsigned r, const salsa20_matrix src, + salsa20_matrix dest) { SALSA20_nR(dest, src, r); SALSA20_FFWD(dest, src); } +#if CPUFAM_X86 || CPUFAM_AMD64 +extern core__functype salsa20_core_x86ish_sse2; +extern core__functype salsa20_core_x86ish_avx; +#endif + +#if CPUFAM_ARMEL +extern core__functype salsa20_core_arm_neon; +#endif + +#if CPUFAM_ARM64 +extern core__functype salsa20_core_arm64; +#endif + +static core__functype *pick_core(void) +{ +#if CPUFAM_X86 || CPUFAM_AMD64 + DISPATCH_PICK_COND(salsa20_core, salsa20_core_x86ish_avx, + cpu_feature_p(CPUFEAT_X86_AVX)); + DISPATCH_PICK_COND(salsa20_core, salsa20_core_x86ish_sse2, + cpu_feature_p(CPUFEAT_X86_SSE2)); +#endif +#if CPUFAM_ARMEL + DISPATCH_PICK_COND(salsa20_core, salsa20_core_arm_neon, + cpu_feature_p(CPUFEAT_ARM_NEON)); +#endif +#if CPUFAM_ARM64 + DISPATCH_PICK_COND(salsa20_core, salsa20_core_arm64, + cpu_feature_p(CPUFEAT_ARM_NEON)); +#endif + DISPATCH_PICK_FALLBACK(salsa20_core, simple_core); +} + /* --- @populate@ --- * * * Arguments: @salsa20_matrix a@ = a matrix to fill in @@ -61,38 +122,49 @@ static void populate(salsa20_matrix a, const void *key, size_t ksz) KSZ_ASSERT(salsa20, ksz); - a[ 1] = LOAD32_L(k + 0); - a[ 2] = LOAD32_L(k + 4); + /* Here's the pattern of key, constant, nonce, and counter pieces in the + * matrix, before and after our permutation. + * + * [ C0 K0 K1 K2 ] [ C0 C1 C2 C3 ] + * [ K3 C1 N0 N1 ] --> [ K3 T1 K7 K2 ] + * [ T0 T1 C2 K4 ] [ T0 K6 K1 N1 ] + * [ K5 K6 K7 C3 ] [ K5 K0 N0 K4 ] + */ + + a[13] = LOAD32_L(k + 0); + a[10] = LOAD32_L(k + 4); if (ksz == 10) { - a[ 3] = LOAD16_L(k + 8); + a[ 7] = LOAD16_L(k + 8); a[ 4] = 0; } else { - a[ 3] = LOAD32_L(k + 8); + a[ 7] = LOAD32_L(k + 8); a[ 4] = LOAD32_L(k + 12); } if (ksz <= 16) { - a[11] = a[ 1]; - a[12] = a[ 2]; - a[13] = a[ 3]; - a[14] = a[ 4]; + a[15] = a[13]; + a[12] = a[10]; + a[ 9] = a[ 7]; + a[ 6] = a[ 4]; a[ 0] = SALSA20_A128; - a[ 5] = SALSA20_B128; - a[10] = ksz == 10 ? SALSA20_C80 : SALSA20_C128; - a[15] = SALSA20_D128; + a[ 1] = SALSA20_B128; + a[ 2] = ksz == 10 ? SALSA20_C80 : SALSA20_C128; + a[ 3] = SALSA20_D128; } else { - a[11] = LOAD32_L(k + 16); + a[15] = LOAD32_L(k + 16); a[12] = LOAD32_L(k + 20); - a[13] = LOAD32_L(k + 24); - a[14] = LOAD32_L(k + 28); + a[ 9] = LOAD32_L(k + 24); + a[ 6] = LOAD32_L(k + 28); a[ 0] = SALSA20_A256; - a[ 5] = SALSA20_B256; - a[10] = SALSA20_C256; - a[15] = SALSA20_D256; + a[ 1] = SALSA20_B256; + a[ 2] = SALSA20_C256; + a[ 3] = SALSA20_D256; } } /*----- Salsa20 implementation --------------------------------------------*/ +static const octet zerononce[XSALSA20_NONCESZ]; + /* --- @salsa20_init@ --- * * * Arguments: @salsa20_ctx *ctx@ = context to fill in @@ -108,16 +180,15 @@ static void populate(salsa20_matrix a, const void *key, size_t ksz) void salsa20_init(salsa20_ctx *ctx, const void *key, size_t ksz, const void *nonce) { - static const octet zerononce[SALSA20_NONCESZ]; - populate(ctx->a, key, ksz); salsa20_setnonce(ctx, nonce ? nonce : zerononce); } -/* --- @salsa20_setnonce@ --- * +/* --- @salsa20_setnonce{,_ietf}@ --- * * * Arguments: @salsa20_ctx *ctx@ = pointer to context - * @const void *nonce@ = the nonce (@SALSA20_NONCESZ@ bytes) + * @const void *nonce@ = the nonce (@SALSA20_NONCESZ@ or + * @SALSA20_IETF_NONCESZ@ bytes) * * Returns: --- * @@ -130,15 +201,25 @@ void salsa20_setnonce(salsa20_ctx *ctx, const void *nonce) { const octet *n = nonce; - ctx->a[6] = LOAD32_L(n + 0); - ctx->a[7] = LOAD32_L(n + 4); + ctx->a[14] = LOAD32_L(n + 0); + ctx->a[11] = LOAD32_L(n + 4); salsa20_seek(ctx, 0); } -/* --- @salsa20_seek@, @salsa20_seeku64@ --- * +void salsa20_setnonce_ietf(salsa20_ctx *ctx, const void *nonce) +{ + const octet *n = nonce; + + ctx->a[ 5] = LOAD32_L(n + 0); + ctx->a[14] = LOAD32_L(n + 4); + ctx->a[11] = LOAD32_L(n + 8); + salsa20_seek_ietf(ctx, 0); +} + +/* --- @salsa20_seek{,u64,_ietf}@ --- * * * Arguments: @salsa20_ctx *ctx@ = pointer to context - * @unsigned long i@, @kludge64 i@ = new position to set + * @unsigned long i@, @kludge64 i@, @uint32@ = new position * * Returns: --- * @@ -153,11 +234,14 @@ void salsa20_seek(salsa20_ctx *ctx, unsigned long i) void salsa20_seeku64(salsa20_ctx *ctx, kludge64 i) { - ctx->a[8] = LO64(i); ctx->a[9] = HI64(i); - ctx->bufi = SALSA20_OUTSZ; + ctx->a[8] = LO64(i); ctx->a[5] = HI64(i); + ctx->off = 0; } -/* --- @salsa20_tell@, @salsa20_tellu64@ --- * +void salsa20_seek_ietf(salsa20_ctx *ctx, uint32 i) + { ctx->a[8] = i; } + +/* --- @salsa20_tell{,u64,_ietf}@ --- * * * Arguments: @salsa20_ctx *ctx@ = pointer to context * @@ -169,7 +253,10 @@ unsigned long salsa20_tell(salsa20_ctx *ctx) { kludge64 i = salsa20_tellu64(ctx); return (GET64(unsigned long, i)); } kludge64 salsa20_tellu64(salsa20_ctx *ctx) - { kludge64 i; SET64(i, ctx->a[9], ctx->a[8]); return (i); } + { kludge64 i; SET64(i, ctx->a[5], ctx->a[8]); return (i); } + +uint32 salsa20_tell_ietf(salsa20_ctx *ctx) + { return (ctx->a[5]); } /* --- @salsa20{,12,8}_encrypt@ --- * * @@ -188,6 +275,8 @@ kludge64 salsa20_tellu64(salsa20_ctx *ctx) * to @dest@. */ +static const rsvr_policy policy = { 0, SALSA20_OUTSZ, SALSA20_OUTSZ }; + #define SALSA20_ENCRYPT(r, ctx, src, dest, sz) \ SALSA20_DECOR(salsa20, r, _encrypt)(ctx, src, dest, sz) #define DEFENCRYPT(r) \ @@ -197,41 +286,40 @@ kludge64 salsa20_tellu64(salsa20_ctx *ctx) salsa20_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/SALSA20_OUTSZ; \ - pos = salsa20_tellu64(ctx); \ - ASSIGN64(delta, n); \ - ADD64(pos, pos, delta); \ - salsa20_seeku64(ctx, pos); \ - sz = sz%SALSA20_OUTSZ; \ - } else if (!src) { \ - while (sz >= SALSA20_OUTSZ) { \ - core(r, ctx->a, b); \ - SALSA20_STEP(ctx->a); \ - SALSA20_GENFULL(b, d); \ - sz -= SALSA20_OUTSZ; \ + rsvr_mkplan(&plan, &policy, ctx->off, sz); \ + \ + if (plan.head) { \ + if (!ctx->off) { \ + core(r, ctx->a, b); SALSA20_STEP(ctx->a); \ + SALSA20_PREPBUF(ctx, b); \ } \ - } else { \ - while (sz >= SALSA20_OUTSZ) { \ - core(r, ctx->a, b); \ - SALSA20_STEP(ctx->a); \ - SALSA20_MIXFULL(b, d, s); \ - sz -= SALSA20_OUTSZ; \ + SALSA20_OUTBUF(ctx, d, s, plan.head); \ + } \ + \ + ctx->off -= plan.from_rsvr; \ + \ + if (!d) { \ + if (plan.from_input) { \ + pos = salsa20_tellu64(ctx); \ + ASSIGN64(delta, plan.from_input/SALSA20_OUTSZ); \ + ADD64(pos, pos, delta); \ + salsa20_seeku64(ctx, pos); \ } \ + } else if (!s) while (plan.from_input) { \ + core(r, ctx->a, b); SALSA20_STEP(ctx->a); \ + SALSA20_GENFULL(b, d); plan.from_input -= SALSA20_OUTSZ; \ + } else while (plan.from_input) { \ + core(r, ctx->a, b); SALSA20_STEP(ctx->a); \ + SALSA20_MIXFULL(b, d, s); plan.from_input -= SALSA20_OUTSZ; \ } \ \ - if (sz) { \ - core(r, ctx->a, b); \ - SALSA20_STEP(ctx->a); \ + if (plan.tail) { \ + core(r, ctx->a, b); SALSA20_STEP(ctx->a); \ SALSA20_PREPBUF(ctx, b); \ - SALSA20_OUTBUF(ctx, d, s, sz); \ - assert(!sz); \ + SALSA20_OUTBUF(ctx, d, s, plan.tail); \ } \ } SALSA20_VARS(DEFENCRYPT) @@ -272,10 +360,10 @@ SALSA20_VARS(DEFENCRYPT) * speed critical, so we do it the harder way. \ */ \ \ - for (i = 0; i < 4; i++) k[i + 6] = src[i]; \ + for (i = 0; i < 4; i++) k[14 - 3*i] = src[i]; \ core(r, k, a); \ - for (i = 0; i < 4; i++) dest[i] = a[5*i] - k[5*i]; \ - for (i = 4; i < 8; i++) dest[i] = a[i + 2] - k[i + 2]; \ + for (i = 0; i < 4; i++) dest[i] = a[5*i] - k[i]; \ + for (i = 4; i < 8; i++) dest[i] = a[i + 2] - k[26 - 3*i]; \ } \ \ void HSALSA20_PRF(r, salsa20_ctx *ctx, const void *src, void *dest) \ @@ -336,13 +424,11 @@ SALSA20_VARS(DEFHSALSA20) void XSALSA20_INIT(r, XSALSA20_CTX(r) *ctx, \ const void *key, size_t ksz, const void *nonce) \ { \ - static const octet zerononce[XSALSA20_NONCESZ]; \ - \ populate(ctx->k, key, ksz); \ ctx->s.a[ 0] = SALSA20_A256; \ - ctx->s.a[ 5] = SALSA20_B256; \ - ctx->s.a[10] = SALSA20_C256; \ - ctx->s.a[15] = SALSA20_D256; \ + ctx->s.a[ 1] = SALSA20_B256; \ + ctx->s.a[ 2] = SALSA20_C256; \ + ctx->s.a[ 3] = SALSA20_D256; \ XSALSA20_SETNONCE(r, ctx, nonce ? nonce : zerononce); \ } SALSA20_VARS(DEFXINIT) @@ -371,13 +457,13 @@ SALSA20_VARS(DEFXINIT) \ for (i = 0; i < 4; i++) in[i] = LOAD32_L(n + 4*i); \ HSALSA20_RAW(r, ctx->k, in, out); \ - for (i = 0; i < 4; i++) ctx->s.a[i + 1] = out[i]; \ - for (i = 4; i < 8; i++) ctx->s.a[i + 7] = out[i]; \ + for (i = 0; i < 4; i++) ctx->s.a[13 - 3*i] = out[i]; \ + for (i = 4; i < 8; i++) ctx->s.a[27 - 3*i] = out[i]; \ salsa20_setnonce(&ctx->s, n + 16); \ } SALSA20_VARS(DEFXNONCE) -/* --- @xsalsa20{,12,8}_seek@, @xsalsa20{,12,8}_seeku64@ --- * +/* --- @xsalsa20{,12,8}_seek{,u64}@ --- * * * Arguments: @xsalsa20R_ctx *ctx@ = pointer to context * @unsigned long i@, @kludge64 i@ = new position to set @@ -394,7 +480,7 @@ SALSA20_VARS(DEFXNONCE) * different. */ -/* --- @xsalsa20{,12,8}_tell@, @xsalsa20{,12,8}_tellu64@ --- * +/* --- @xsalsa20{,12,8}_tell{,u64}@ --- * * * Arguments: @salsa20_ctx *ctx@ = pointer to context * @@ -444,20 +530,29 @@ typedef struct gctx { gcipher c; salsa20_ctx ctx; } gctx; static void gsetiv(gcipher *c, const void *iv) { gctx *g = (gctx *)c; salsa20_setnonce(&g->ctx, iv); } +static void gsetiv_ietf(gcipher *c, const void *iv) + { gctx *g = (gctx *)c; salsa20_setnonce_ietf(&g->ctx, 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 const gcipher_ops gops_##r, gops_##r##_ietf; \ \ 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 gcipher *ginit_##r##_ietf(const void *k, size_t sz) \ + { return (ginit(k, sz, &gops_##r##_ietf)); } \ \ static void gencrypt_##r(gcipher *c, const void *s, \ void *t, size_t sz) \ @@ -468,9 +563,19 @@ static void gdestroy(gcipher *c) gencrypt_##r, gencrypt_##r, gdestroy, gsetiv, 0 \ }; \ \ + static const gcipher_ops gops_##r##_ietf = { \ + &SALSA20_DECOR(salsa20, r, _ietf), \ + gencrypt_##r, gencrypt_##r, gdestroy, gsetiv_ietf, 0 \ + }; \ + \ const gccipher SALSA20_DECOR(salsa20, r, ) = { \ SALSA20_NAME_##r, salsa20_keysz, \ SALSA20_NONCESZ, ginit_##r \ + }; \ + \ + const gccipher SALSA20_DECOR(salsa20, r, _ietf) = { \ + SALSA20_NAME_##r "-ietf", salsa20_keysz, \ + SALSA20_IETF_NONCESZ, ginit_##r##_ietf \ }; SALSA20_VARS(DEFGCIPHER) @@ -642,15 +747,41 @@ typedef struct grctx { static void gr_seek(void *r, kludge64 pos) { grctx *g = r; salsa20_seeku64(&g->ctx, pos); } +static void gr_seek_ietf(void *r, kludge64 pos) + { grctx *g = r; salsa20_seek_ietf(&g->ctx, LO64(pos)); } + static kludge64 gr_tell(void *r) { grctx *g = r; return (salsa20_tellu64(&g->ctx)); } +static kludge64 gr_tell_ietf(void *r) +{ + grctx *g = r; + kludge64 pos; + + SET64(pos, 0, salsa20_tell_ietf(&g->ctx)); + return (pos); +} + static void gr_setnonce(void *r, const void *n) { grctx *g = r; salsa20_setnonce(&g->ctx, n); } +static void gr_setnonce_ietf(void *r, const void *n) + { grctx *g = r; salsa20_setnonce(&g->ctx, 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); + if (n) myops->setnonce(g, n); + return (&g->r.r); +} + #define DEFGRAND(rr) \ \ static void gr_generate_##rr(void *r, void *b, size_t sz) \ @@ -660,21 +791,34 @@ static void grdestroy(grand *r) { SALSA20_NONCESZ, gr_seek, gr_tell, \ gr_setnonce, gr_generate_##rr }; \ \ + static const grops grops_##rr##_ietf = \ + { SALSA20_IETF_NONCESZ, gr_seek_ietf, gr_tell_ietf, \ + gr_setnonce_ietf, gr_generate_##rr }; \ + \ static const grand_ops grops_rand_##rr = { \ SALSA20_NAME_##rr, GRAND_CRYPTO, 0, \ grmisc, grdestroy, grword, \ grbyte, grword, grand_defaultrange, grfill \ }; \ \ + static const grand_ops grops_rand_##rr##_ietf = { \ + SALSA20_NAME_##rr "-ietf", GRAND_CRYPTO, 0, \ + grmisc, grdestroy, grword, \ + grbyte, grword, grand_defaultrange, grfill \ + }; \ + \ grand *SALSA20_DECOR(salsa20, rr, _rand) \ (const void *k, size_t ksz, const void *n) \ + { return (grinit(k, ksz, n, &grops_rand_##rr, &grops_##rr)); } \ + \ + grand *SALSA20_DECOR(salsa20, rr, _ietf_rand) \ + (const void *k, size_t ksz, const void *n) \ { \ - grctx *g = S_CREATE(g); \ - 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##_ietf, \ + &grops_##rr##_ietf)); \ } + SALSA20_VARS(DEFGRAND) #define DEFXGRAND(rr) \ @@ -712,7 +856,7 @@ SALSA20_VARS(DEFGRAND) grand *SALSA20_DECOR(xsalsa20, rr, _rand) \ (const void *k, size_t ksz, const void *n) \ { \ - grxctx_##rr *g = S_CREATE(g); \ + grxctx_##rr *g = S_CREATE(grxctx_##rr); \ g->r.r.ops = &grxops_rand_##rr; \ g->r.ops = &grxops_##rr; \ XSALSA20_INIT(rr, &g->ctx, k, ksz, n); \ @@ -730,23 +874,31 @@ SALSA20_VARS(DEFXGRAND) #include #include +static const int perm[] = { + 0, 13, 10, 7, + 4, 1, 14, 11, + 8, 5, 2, 15, + 12, 9, 6, 3 +}; + #define DEFVCORE(r) \ static int v_core_##r(dstr *v) \ { \ salsa20_matrix a, b; \ dstr d = DSTR_INIT; \ - int i, n; \ + int i, j, n; \ int ok = 1; \ \ DENSURE(&d, SALSA20_OUTSZ); d.len = SALSA20_OUTSZ; \ n = *(int *)v[0].buf; \ for (i = 0; i < SALSA20_OUTSZ/4; i++) \ - a[i] = LOAD32_L(v[1].buf + 4*i); \ + b[i] = LOAD32_L(v[1].buf + 4*i); \ for (i = 0; i < n; i++) { \ + for (j = 0; j < 16; j++) a[perm[j]] = b[j]; \ core(r, a, b); \ memcpy(a, b, sizeof(a)); \ } \ - for (i = 0; i < SALSA20_OUTSZ/4; i++) STORE32_L(d.buf + 4*i, a[i]); \ + for (i = 0; i < SALSA20_OUTSZ/4; i++) STORE32_L(d.buf + 4*i, b[i]); \ \ if (d.len != v[2].len || memcmp(d.buf, v[2].buf, v[2].len) != 0) { \ ok = 0; \ @@ -767,15 +919,28 @@ SALSA20_VARS(DEFXGRAND) 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); \ + else if (nsz == 12) salsa20_setnonce_ietf(ctx, n); \ + if (psz == 8) { LOAD64_(pos64, p); salsa20_seeku64(ctx, pos64); } \ + else if (psz == 4) salsa20_seek_ietf(ctx, LOAD32(p)); \ +} 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; \ @@ -791,11 +956,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); \