From 8acebc571ed68f3fa7742c4944213d8431bef834 Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Sat, 28 Oct 2023 18:42:46 +0000 Subject: [PATCH] More cleanup --- meson.build | 7 ++++++- src/cha1block.h | 12 +++++++----- src/cha4block.h | 35 ++++++++++++++++++----------------- src/cha8block.h | 6 ++++-- src/chacha20.c | 20 ++++++++++---------- src/chacha20.h | 6 +++--- 6 files changed, 48 insertions(+), 38 deletions(-) diff --git a/meson.build b/meson.build index 4c03928..cad8096 100644 --- a/meson.build +++ b/meson.build @@ -8,4 +8,9 @@ executable( ) dependency('threads') -library('randquik-chacha20', 'src/chacha20.c', build_by_default: true) +library( + 'randquik-chacha20', + 'src/chacha20.c', + build_by_default: true, + c_args: ['-Wall', '-O3', '-march=native'], +) diff --git a/src/cha1block.h b/src/cha1block.h index b4459f8..1891c2a 100644 --- a/src/cha1block.h +++ b/src/cha1block.h @@ -1,3 +1,6 @@ +#pragma once +#include "chacha20.h" + #include #include #include @@ -13,13 +16,12 @@ QUARTERSTEP(a, b, d, 8); \ QUARTERSTEP(c, d, b, 7); -static inline uint64_t -_cha_block(uint32_t* state, uint8_t* begin, uint8_t* end) { - uint64_t* counter = (uint64_t*)&state[12]; +static inline uint64_t _cha_block(cha_ctx* ctx, uint8_t* begin, uint8_t* end) { + uint64_t* counter = (uint64_t*)&ctx->state[12]; uint8_t* c = begin; while (c < end) { uint32_t x[16]; - memcpy(x, state, sizeof x); + memcpy(x, ctx->state, sizeof x); for (int i = 20; i > 0; i -= 2) { QUARTERROUND(x[0], x[4], x[8], x[12]) QUARTERROUND(x[1], x[5], x[9], x[13]) @@ -31,7 +33,7 @@ _cha_block(uint32_t* state, uint8_t* begin, uint8_t* end) { QUARTERROUND(x[3], x[4], x[9], x[14]) } for (int i = 0; i < 16; i++) - x[i] += state[i]; + x[i] += ctx->state[i]; ++*counter; diff --git a/src/cha4block.h b/src/cha4block.h index 626bf10..84fa0d0 100644 --- a/src/cha4block.h +++ b/src/cha4block.h @@ -1,3 +1,5 @@ +#pragma once +#include "chacha20.h" #define VEC4_ROT(A, IMM) \ _mm_or_si128(_mm_slli_epi32(A, IMM), _mm_srli_epi32(A, (32 - IMM))) @@ -41,12 +43,11 @@ _mm_storeu_si128((__m128i*)(CT + 192), x_##D); \ } -static inline uint64_t -_cha_4block(uint32_t* state, uint8_t* begin, uint8_t* end) { +static inline uint64_t _cha_4block(cha_ctx* ctx, uint8_t* begin, uint8_t* end) { if (end - begin < 256) return 0; uint8_t* c = begin; - uint32_t* x = state; + uint32_t* state = ctx->state; /* constant for shuffling bytes (replacing multiple-of-8 rotates) */ const __m128i rot16 = @@ -55,22 +56,22 @@ _cha_4block(uint32_t* state, uint8_t* begin, uint8_t* end) { _mm_set_epi8(14, 13, 12, 15, 10, 9, 8, 11, 6, 5, 4, 7, 2, 1, 0, 3); // Load state to vectors, duplicate four times - __m128i x_0 = _mm_set1_epi32(x[0]); - __m128i x_1 = _mm_set1_epi32(x[1]); - __m128i x_2 = _mm_set1_epi32(x[2]); - __m128i x_3 = _mm_set1_epi32(x[3]); - __m128i x_4 = _mm_set1_epi32(x[4]); - __m128i x_5 = _mm_set1_epi32(x[5]); - __m128i x_6 = _mm_set1_epi32(x[6]); - __m128i x_7 = _mm_set1_epi32(x[7]); - __m128i x_8 = _mm_set1_epi32(x[8]); - __m128i x_9 = _mm_set1_epi32(x[9]); - __m128i x_10 = _mm_set1_epi32(x[10]); - __m128i x_11 = _mm_set1_epi32(x[11]); + __m128i x_0 = _mm_set1_epi32(state[0]); + __m128i x_1 = _mm_set1_epi32(state[1]); + __m128i x_2 = _mm_set1_epi32(state[2]); + __m128i x_3 = _mm_set1_epi32(state[3]); + __m128i x_4 = _mm_set1_epi32(state[4]); + __m128i x_5 = _mm_set1_epi32(state[5]); + __m128i x_6 = _mm_set1_epi32(state[6]); + __m128i x_7 = _mm_set1_epi32(state[7]); + __m128i x_8 = _mm_set1_epi32(state[8]); + __m128i x_9 = _mm_set1_epi32(state[9]); + __m128i x_10 = _mm_set1_epi32(state[10]); + __m128i x_11 = _mm_set1_epi32(state[11]); __m128i x_12; __m128i x_13; - __m128i x_14 = _mm_set1_epi32(x[14]); - __m128i x_15 = _mm_set1_epi32(x[15]); + __m128i x_14 = _mm_set1_epi32(state[14]); + __m128i x_15 = _mm_set1_epi32(state[15]); __m128i orig0 = x_0; __m128i orig1 = x_1; __m128i orig2 = x_2; diff --git a/src/cha8block.h b/src/cha8block.h index 12d54ee..3d0bc8d 100644 --- a/src/cha8block.h +++ b/src/cha8block.h @@ -1,3 +1,5 @@ +#pragma once +#include "chacha20.h" #define VEC8_ROT(A, IMM) \ _mm256_or_si256(_mm256_slli_epi32(A, IMM), _mm256_srli_epi32(A, (32 - IMM))) @@ -179,12 +181,12 @@ _mm256_storeu_si256((__m256i*)(c + 448), t[D2]); \ } -static inline uint64_t -_cha_8block(uint32_t* state, uint8_t* begin, uint8_t* end) { +static inline uint64_t _cha_8block(cha_ctx* ctx, uint8_t* begin, uint8_t* end) { if (end - begin < 512) return 0; uint8_t* c = begin; + uint32_t* state = ctx->state; uint64_t* counter = (uint64_t*)(state + 12); /* constant for shuffling bytes (replacing multiple-of-8 rotates) */ __m256i rot16 = _mm256_set_epi8( diff --git a/src/chacha20.c b/src/chacha20.c index f3f1d86..54713a7 100644 --- a/src/chacha20.c +++ b/src/chacha20.c @@ -24,15 +24,15 @@ #include "cha8block.h" void cha_init(cha_ctx* ctx, const uint8_t* key, const uint8_t* iv) { - ctx->input[0] = 0x61707865; - ctx->input[1] = 0x3320646e; - ctx->input[2] = 0x79622d32; - ctx->input[3] = 0x6b206574; - memcpy(ctx->input + 4, key, 32); - memcpy(ctx->input + 12, iv, 16); + ctx->state[0] = 0x61707865; + ctx->state[1] = 0x3320646e; + ctx->state[2] = 0x79622d32; + ctx->state[3] = 0x6b206574; + memcpy(ctx->state + 4, key, 32); + memcpy(ctx->state + 12, iv, 16); } -void cha_wipe(cha_ctx* ctx) { memset(&ctx, 0, sizeof(cha_ctx)); } +void cha_wipe(cha_ctx* ctx) { memset(ctx, 0, sizeof(cha_ctx)); } int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen) { // The included header will mess with these variables @@ -41,13 +41,13 @@ int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen) { // TODO: Handle resume if we are not at block boundary if (__builtin_cpu_supports("ssse3")) { if (__builtin_cpu_supports("avx2")) { - c += _cha_8block(ctx->input, c, end); + c += _cha_8block(ctx, c, end); assert(end - c < 512); } - c += _cha_4block(ctx->input, c, end); + c += _cha_4block(ctx, c, end); assert(end - c < 256); } - c += _cha_block(ctx->input, c, end); + c += _cha_block(ctx, c, end); assert(c == end); return 0; } diff --git a/src/chacha20.h b/src/chacha20.h index 1eea9a5..5c0cce6 100644 --- a/src/chacha20.h +++ b/src/chacha20.h @@ -1,10 +1,11 @@ +#pragma once #include #include static const uint64_t CHA_BLOCK_SIZE = 64; typedef struct cha_ctx { - uint32_t input[16]; + uint32_t state[16]; } cha_ctx; /// @brief Initialize cha_ctx @@ -31,6 +32,5 @@ int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen); /// @param iv /// @return int cha_generate( - unsigned char* out, uint64_t outlen, const unsigned char key[32], - const unsigned char iv[16] + uint8_t* out, uint64_t outlen, const uint8_t key[32], const uint8_t iv[16] );