diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-09-30 03:07:51 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-10-02 03:41:49 +0200 |
commit | 6c470a7f5f5e3c115dce116b358df97bb3ebb37a (patch) | |
tree | e989ffe895f5a8d5b5d3d4576a489f89bb9cbba1 /src/crypto/include/zinc | |
parent | 8451e85a596dbd8f9f961f49a3e7ee09ac82aa10 (diff) |
chacha20: split chacha20_ctx into union
This avoids casts and makes counter increments obvious.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'src/crypto/include/zinc')
-rw-r--r-- | src/crypto/include/zinc/chacha20.h | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/src/crypto/include/zinc/chacha20.h b/src/crypto/include/zinc/chacha20.h index 14bbadd..37ec3b4 100644 --- a/src/crypto/include/zinc/chacha20.h +++ b/src/crypto/include/zinc/chacha20.h @@ -29,33 +29,38 @@ enum { /* expand 32-byte k */ }; struct chacha20_ctx { - u32 constant[4]; - u32 key[8]; - u32 counter[4]; + union { + u32 state[16]; + struct { + u32 constant[4]; + u32 key[8]; + u32 counter[4]; + }; + }; } __aligned(32); -static inline void chacha20_init(struct chacha20_ctx *state, +static inline void chacha20_init(struct chacha20_ctx *ctx, const u8 key[CHACHA20_KEY_SIZE], const u64 nonce) { - state->constant[0] = CHACHA20_CONSTANT_EXPA; - state->constant[1] = CHACHA20_CONSTANT_ND_3; - state->constant[2] = CHACHA20_CONSTANT_2_BY; - state->constant[3] = CHACHA20_CONSTANT_TE_K; - state->key[0] = get_unaligned_le32(key + 0); - state->key[1] = get_unaligned_le32(key + 4); - state->key[2] = get_unaligned_le32(key + 8); - state->key[3] = get_unaligned_le32(key + 12); - state->key[4] = get_unaligned_le32(key + 16); - state->key[5] = get_unaligned_le32(key + 20); - state->key[6] = get_unaligned_le32(key + 24); - state->key[7] = get_unaligned_le32(key + 28); - state->counter[0] = 0; - state->counter[1] = 0; - state->counter[2] = nonce & U32_MAX; - state->counter[3] = nonce >> 32; + ctx->constant[0] = CHACHA20_CONSTANT_EXPA; + ctx->constant[1] = CHACHA20_CONSTANT_ND_3; + ctx->constant[2] = CHACHA20_CONSTANT_2_BY; + ctx->constant[3] = CHACHA20_CONSTANT_TE_K; + ctx->key[0] = get_unaligned_le32(key + 0); + ctx->key[1] = get_unaligned_le32(key + 4); + ctx->key[2] = get_unaligned_le32(key + 8); + ctx->key[3] = get_unaligned_le32(key + 12); + ctx->key[4] = get_unaligned_le32(key + 16); + ctx->key[5] = get_unaligned_le32(key + 20); + ctx->key[6] = get_unaligned_le32(key + 24); + ctx->key[7] = get_unaligned_le32(key + 28); + ctx->counter[0] = 0; + ctx->counter[1] = 0; + ctx->counter[2] = nonce & U32_MAX; + ctx->counter[3] = nonce >> 32; } -void chacha20(struct chacha20_ctx *state, u8 *dst, const u8 *src, u32 len, +void chacha20(struct chacha20_ctx *ctx, u8 *dst, const u8 *src, u32 len, simd_context_t *simd_context); void hchacha20(u32 derived_key[CHACHA20_KEY_WORDS], |