From e6912e4438388021222b7b449d1e84709d943572 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Thu, 26 May 2016 09:26:09 +0100 Subject: [PATCH] symm/{chacha,salsa20}.c: Change how the test code sets up the cipher. Introduce a macro which does the key, nonce and position setup in one go. --- symm/chacha.c | 25 ++++++++++++++++--------- symm/salsa20.c | 24 ++++++++++++++++-------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/symm/chacha.c b/symm/chacha.c index 45f7cf82..5fc1c12c 100644 --- a/symm/chacha.c +++ b/symm/chacha.c @@ -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); \ diff --git a/symm/salsa20.c b/symm/salsa20.c index 9dc95e8b..b8c630dd 100644 --- a/symm/salsa20.c +++ b/symm/salsa20.c @@ -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); \ -- 2.11.0