#include "grand.h"
#include "keysz.h"
#include "paranoia.h"
+#include "rsvr.h"
/*----- Global variables --------------------------------------------------*/
* the feedforward step.
*/
-CPU_DISPATCH(static, (void),
- void, core, (unsigned r, const chacha_matrix src,
- chacha_matrix dest),
- (r, src, dest),
- pick_core, simple_core);
+CPU_DISPATCH(static, (void), void, core,
+ (unsigned r, const chacha_matrix src, chacha_matrix dest),
+ (r, src, dest), pick_core, simple_core);
static void simple_core(unsigned r, const chacha_matrix src,
chacha_matrix dest)
#if CPUFAM_X86 || CPUFAM_AMD64
extern core__functype chacha_core_x86ish_sse2;
+extern core__functype chacha_core_x86ish_avx;
#endif
#if CPUFAM_ARMEL
extern core__functype chacha_core_arm_neon;
#endif
+#if CPUFAM_ARM64
+extern core__functype chacha_core_arm64;
+#endif
+
static core__functype *pick_core(void)
{
#if CPUFAM_X86 || CPUFAM_AMD64
+ DISPATCH_PICK_COND(chacha_core, chacha_core_x86ish_avx,
+ cpu_feature_p(CPUFEAT_X86_AVX));
DISPATCH_PICK_COND(chacha_core, chacha_core_x86ish_sse2,
cpu_feature_p(CPUFEAT_X86_SSE2));
#endif
DISPATCH_PICK_COND(chacha_core, chacha_core_arm_neon,
cpu_feature_p(CPUFEAT_ARM_NEON));
#endif
+#if CPUFAM_ARM64
+ DISPATCH_PICK_COND(chacha_core, chacha_core_arm64,
+ cpu_feature_p(CPUFEAT_ARM_NEON));
+#endif
DISPATCH_PICK_FALLBACK(chacha_core, simple_core);
}
/*----- ChaCha implementation ---------------------------------------------*/
+static const octet zerononce[XCHACHA_NONCESZ];
+
/* --- @chacha_init@ --- *
*
* Arguments: @chacha_ctx *ctx@ = context to fill in
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);
}
-/* --- @chacha_setnonce@ --- *
+/* --- @chacha_setnonce{,_ietf}@ --- *
*
* Arguments: @chacha_ctx *ctx@ = pointer to context
- * @const void *nonce@ = the nonce (@CHACHA_NONCESZ@ bytes)
+ * @const void *nonce@ = the nonce (@CHACHA_NONCESZ@ or
+ * @CHACHA_IETF_NONCESZ@ bytes)
*
* Returns: ---
*
chacha_seek(ctx, 0);
}
-/* --- @chacha_seek@, @chacha_seeku64@ --- *
+void chacha_setnonce_ietf(chacha_ctx *ctx, const void *nonce)
+{
+ const octet *n = nonce;
+
+ ctx->a[13] = LOAD32_L(n + 0);
+ ctx->a[14] = LOAD32_L(n + 4);
+ ctx->a[15] = LOAD32_L(n + 8);
+ chacha_seek_ietf(ctx, 0);
+}
+
+/* --- @chacha_seek{,u64,_ietf}@ --- *
*
* Arguments: @chacha_ctx *ctx@ = pointer to context
- * @unsigned long i@, @kludge64 i@ = new position to set
+ * @unsigned long i@, @kludge64 i@, @uint32 i@ = new position
*
* Returns: ---
*
void chacha_seeku64(chacha_ctx *ctx, kludge64 i)
{
ctx->a[12] = LO64(i); ctx->a[13] = HI64(i);
- ctx->bufi = CHACHA_OUTSZ;
+ ctx->off = CHACHA_OUTSZ;
}
-/* --- @chacha_tell@, @chacha_tellu64@ --- *
+void chacha_seek_ietf(chacha_ctx *ctx, uint32 i)
+ { ctx->a[12] = i; }
+
+/* --- @chacha_tell{,u64,_ietf}@ --- *
*
* Arguments: @chacha_ctx *ctx@ = pointer to context
*
{ kludge64 i = chacha_tellu64(ctx); return (GET64(unsigned long, i)); }
kludge64 chacha_tellu64(chacha_ctx *ctx)
- { kludge64 i; SET64(i, ctx->a[9], ctx->a[8]); return (i); }
+ { kludge64 i; SET64(i, ctx->a[13], ctx->a[12]); return (i); }
+
+uint32 chacha_tell_ietf(chacha_ctx *ctx)
+ { return (ctx->a[12]); }
-/* --- @chacha{,12,8}_encrypt@ --- *
+/* --- @chacha{20,12,8}_encrypt@ --- *
*
* Arguments: @chacha_ctx *ctx@ = pointer to context
* @const void *src@ = source buffer (or null)
* 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) \
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)
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; \
}
CHACHA_VARS(DEFXNONCE)
-/* --- @xchacha{20,12,8}_seek@, @xchacha{20,12,8}_seeku64@ --- *
+/* --- @xchacha{20,12,8}_seek{,u64}@ --- *
*
* Arguments: @xchachaR_ctx *ctx@ = pointer to context
* @unsigned long i@, @kludge64 i@ = new position to set
* different.
*/
-/* --- @xchacha{20,12,8}_tell@, @xchacha{20,12,8}_tellu64@ --- *
+/* --- @xchacha{20,12,8}_tell{,u64}@ --- *
*
* Arguments: @chacha_ctx *ctx@ = pointer to context
*
* different.
*/
-/* --- @xchacha{,12,8}_encrypt@ --- *
+/* --- @xchacha{20,12,8}_encrypt@ --- *
*
* Arguments: @xchachaR_ctx *ctx@ = pointer to context
* @const void *src@ = source buffer (or null)
static void gsetiv(gcipher *c, const void *iv)
{ gctx *g = (gctx *)c; chacha_setnonce(&g->ctx, iv); }
+static void gsetiv_ietf(gcipher *c, const void *iv)
+ { gctx *g = (gctx *)c; chacha_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;
+ chacha_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; \
- chacha_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) \
gencrypt_##r, gencrypt_##r, gdestroy, gsetiv, 0 \
}; \
\
+ static const gcipher_ops gops_##r##_ietf = { \
+ &chacha##r##_ietf, \
+ gencrypt_##r, gencrypt_##r, gdestroy, gsetiv_ietf, 0 \
+ }; \
+ \
const gccipher chacha##r = { \
"chacha" #r, chacha_keysz, \
CHACHA_NONCESZ, ginit_##r \
+ }; \
+ \
+ const gccipher chacha##r##_ietf = { \
+ "chacha" #r "-ietf", chacha_keysz, \
+ CHACHA_IETF_NONCESZ, ginit_##r##_ietf \
};
CHACHA_VARS(DEFGCIPHER)
\
const gccipher xchacha##r = { \
"xchacha" #r, chacha_keysz, \
- CHACHA_NONCESZ, gxinit_##r \
+ XCHACHA_NONCESZ, gxinit_##r \
};
CHACHA_VARS(DEFGXCIPHER)
static void gr_seek(void *r, kludge64 pos)
{ grctx *g = r; chacha_seeku64(&g->ctx, pos); }
+static void gr_seek_ietf(void *r, kludge64 pos)
+ { grctx *g = r; chacha_seek_ietf(&g->ctx, LO64(pos)); }
+
static kludge64 gr_tell(void *r)
{ grctx *g = r; return (chacha_tellu64(&g->ctx)); }
+static kludge64 gr_tell_ietf(void *r)
+{
+ grctx *g = r;
+ kludge64 pos;
+
+ SET64(pos, 0, chacha_tell_ietf(&g->ctx));
+ return (pos);
+}
+
static void gr_setnonce(void *r, const void *n)
{ grctx *g = r; chacha_setnonce(&g->ctx, n); }
+static void gr_setnonce_ietf(void *r, const void *n)
+ { grctx *g = r; chacha_setnonce_ietf(&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;
+ chacha_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) \
{ CHACHA_NONCESZ, gr_seek, gr_tell, \
gr_setnonce, gr_generate_##rr }; \
\
+ static const grops grops_##rr##_ietf = \
+ { CHACHA_IETF_NONCESZ, gr_seek_ietf, gr_tell_ietf, \
+ gr_setnonce_ietf, gr_generate_##rr }; \
+ \
static const grand_ops grops_rand_##rr = { \
"chacha" #rr, GRAND_CRYPTO, 0, \
grmisc, grdestroy, grword, \
grbyte, grword, grand_defaultrange, grfill \
}; \
\
+ static const grand_ops grops_rand_##rr##_ietf = { \
+ "chacha" #rr "-ietf", GRAND_CRYPTO, 0, \
+ grmisc, grdestroy, grword, \
+ grbyte, grword, grand_defaultrange, grfill \
+ }; \
+ \
grand *chacha##rr##_rand(const void *k, size_t ksz, const void *n) \
+ { return (grinit(k, ksz, n, &grops_rand_##rr, &grops_##rr)); } \
+ \
+ grand *chacha##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; \
- chacha_init(&g->ctx, k, ksz, n); \
- return (&g->r.r); \
+ return (grinit(k, ksz, n, \
+ &grops_rand_##rr##_ietf, \
+ &grops_##rr##_ietf)); \
}
+
CHACHA_VARS(DEFGRAND)
#define DEFXGRAND(rr) \
\
grand *xchacha##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; \
XCHACHA_INIT(rr, &g->ctx, k, ksz, n); \
#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) \
{ \
} \
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" \
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); \
+ else if (nsz == 12) chacha_setnonce_ietf(ctx, n); \
+ if (psz == 8) { LOAD64_(pos64, p); chacha_seeku64(ctx, pos64); } \
+ else if (psz == 4) chacha_seek_ietf(ctx, LOAD32(p)); \
+} 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; \
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); \
} \
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" \
int main(int argc, char *argv[])
{
+#ifdef ENABLE_ASM_DEBUG
+ regdump_init();
+#endif
test_run(argc, argv, defs, SRCDIR"/t/chacha");
return (0);
}