X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/blobdiff_plain/e6912e4438388021222b7b449d1e84709d943572..1778ca95377e045bd56a0a99d4d27b476ed85345:/symm/chacha.c diff --git a/symm/chacha.c b/symm/chacha.c index 5fc1c12c..655ecd02 100644 --- a/symm/chacha.c +++ b/symm/chacha.c @@ -163,10 +163,11 @@ void chacha_init(chacha_ctx *ctx, const void *key, size_t 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: --- * @@ -184,10 +185,20 @@ void chacha_setnonce(chacha_ctx *ctx, const void *nonce) chacha_seek(ctx, 0); } -/* --- @chacha_seek{,u64}@ --- * +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: --- * @@ -206,7 +217,10 @@ void chacha_seeku64(chacha_ctx *ctx, kludge64 i) ctx->bufi = CHACHA_OUTSZ; } -/* --- @chacha_tell{,u64}@ --- * +void chacha_seek_ietf(chacha_ctx *ctx, uint32 i) + { ctx->a[12] = i; } + +/* --- @chacha_tell{,u64,_ietf}@ --- * * * Arguments: @chacha_ctx *ctx@ = pointer to context * @@ -220,6 +234,9 @@ unsigned long chacha_tell(chacha_ctx *ctx) kludge64 chacha_tellu64(chacha_ctx *ctx) { kludge64 i; SET64(i, ctx->a[13], ctx->a[12]); return (i); } +uint32 chacha_tell_ietf(chacha_ctx *ctx) + { return (ctx->a[12]); } + /* --- @chacha{20,12,8}_encrypt@ --- * * * Arguments: @chacha_ctx *ctx@ = pointer to context @@ -482,6 +499,9 @@ typedef struct gctx { gcipher c; chacha_ctx ctx; } gctx; 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); } @@ -495,11 +515,14 @@ static gcipher *ginit(const void *k, size_t sz, const gcipher_ops *ops) #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) \ { 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) \ { gctx *g = (gctx *)c; CHACHA_ENCRYPT(r, &g->ctx, s, t, sz); } \ @@ -509,9 +532,19 @@ static gcipher *ginit(const void *k, size_t sz, const gcipher_ops *ops) 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) @@ -683,12 +716,27 @@ typedef struct grctx { 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); } @@ -712,14 +760,33 @@ static grand *grinit(const void *k, size_t ksz, const void *n, { 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)); } + { return (grinit(k, ksz, n, &grops_rand_##rr, &grops_##rr)); } \ + \ + grand *chacha##rr##_ietf_rand(const void *k, size_t ksz, \ + const void *n) \ + { \ + return (grinit(k, ksz, n, \ + &grops_rand_##rr##_ietf, \ + &grops_##rr##_ietf)); \ + } + CHACHA_VARS(DEFGRAND) #define DEFXGRAND(rr) \ @@ -816,7 +883,9 @@ CHACHA_VARS(DEFVCORE) 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 { \