More cleanup

This commit is contained in:
2023-10-28 18:42:46 +00:00
parent b4fd8c29ff
commit 8acebc571e
6 changed files with 48 additions and 38 deletions
+6 -1
View File
@@ -8,4 +8,9 @@ executable(
) )
dependency('threads') 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'],
)
+7 -5
View File
@@ -1,3 +1,6 @@
#pragma once
#include "chacha20.h"
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -13,13 +16,12 @@
QUARTERSTEP(a, b, d, 8); \ QUARTERSTEP(a, b, d, 8); \
QUARTERSTEP(c, d, b, 7); QUARTERSTEP(c, d, b, 7);
static inline uint64_t static inline uint64_t _cha_block(cha_ctx* ctx, uint8_t* begin, uint8_t* end) {
_cha_block(uint32_t* state, uint8_t* begin, uint8_t* end) { uint64_t* counter = (uint64_t*)&ctx->state[12];
uint64_t* counter = (uint64_t*)&state[12];
uint8_t* c = begin; uint8_t* c = begin;
while (c < end) { while (c < end) {
uint32_t x[16]; uint32_t x[16];
memcpy(x, state, sizeof x); memcpy(x, ctx->state, sizeof x);
for (int i = 20; i > 0; i -= 2) { for (int i = 20; i > 0; i -= 2) {
QUARTERROUND(x[0], x[4], x[8], x[12]) QUARTERROUND(x[0], x[4], x[8], x[12])
QUARTERROUND(x[1], x[5], x[9], x[13]) 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]) QUARTERROUND(x[3], x[4], x[9], x[14])
} }
for (int i = 0; i < 16; i++) for (int i = 0; i < 16; i++)
x[i] += state[i]; x[i] += ctx->state[i];
++*counter; ++*counter;
+18 -17
View File
@@ -1,3 +1,5 @@
#pragma once
#include "chacha20.h"
#define VEC4_ROT(A, IMM) \ #define VEC4_ROT(A, IMM) \
_mm_or_si128(_mm_slli_epi32(A, IMM), _mm_srli_epi32(A, (32 - 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); \ _mm_storeu_si128((__m128i*)(CT + 192), x_##D); \
} }
static inline uint64_t static inline uint64_t _cha_4block(cha_ctx* ctx, uint8_t* begin, uint8_t* end) {
_cha_4block(uint32_t* state, uint8_t* begin, uint8_t* end) {
if (end - begin < 256) if (end - begin < 256)
return 0; return 0;
uint8_t* c = begin; uint8_t* c = begin;
uint32_t* x = state; uint32_t* state = ctx->state;
/* constant for shuffling bytes (replacing multiple-of-8 rotates) */ /* constant for shuffling bytes (replacing multiple-of-8 rotates) */
const __m128i rot16 = 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); _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 // Load state to vectors, duplicate four times
__m128i x_0 = _mm_set1_epi32(x[0]); __m128i x_0 = _mm_set1_epi32(state[0]);
__m128i x_1 = _mm_set1_epi32(x[1]); __m128i x_1 = _mm_set1_epi32(state[1]);
__m128i x_2 = _mm_set1_epi32(x[2]); __m128i x_2 = _mm_set1_epi32(state[2]);
__m128i x_3 = _mm_set1_epi32(x[3]); __m128i x_3 = _mm_set1_epi32(state[3]);
__m128i x_4 = _mm_set1_epi32(x[4]); __m128i x_4 = _mm_set1_epi32(state[4]);
__m128i x_5 = _mm_set1_epi32(x[5]); __m128i x_5 = _mm_set1_epi32(state[5]);
__m128i x_6 = _mm_set1_epi32(x[6]); __m128i x_6 = _mm_set1_epi32(state[6]);
__m128i x_7 = _mm_set1_epi32(x[7]); __m128i x_7 = _mm_set1_epi32(state[7]);
__m128i x_8 = _mm_set1_epi32(x[8]); __m128i x_8 = _mm_set1_epi32(state[8]);
__m128i x_9 = _mm_set1_epi32(x[9]); __m128i x_9 = _mm_set1_epi32(state[9]);
__m128i x_10 = _mm_set1_epi32(x[10]); __m128i x_10 = _mm_set1_epi32(state[10]);
__m128i x_11 = _mm_set1_epi32(x[11]); __m128i x_11 = _mm_set1_epi32(state[11]);
__m128i x_12; __m128i x_12;
__m128i x_13; __m128i x_13;
__m128i x_14 = _mm_set1_epi32(x[14]); __m128i x_14 = _mm_set1_epi32(state[14]);
__m128i x_15 = _mm_set1_epi32(x[15]); __m128i x_15 = _mm_set1_epi32(state[15]);
__m128i orig0 = x_0; __m128i orig0 = x_0;
__m128i orig1 = x_1; __m128i orig1 = x_1;
__m128i orig2 = x_2; __m128i orig2 = x_2;
+4 -2
View File
@@ -1,3 +1,5 @@
#pragma once
#include "chacha20.h"
#define VEC8_ROT(A, IMM) \ #define VEC8_ROT(A, IMM) \
_mm256_or_si256(_mm256_slli_epi32(A, IMM), _mm256_srli_epi32(A, (32 - 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]); \ _mm256_storeu_si256((__m256i*)(c + 448), t[D2]); \
} }
static inline uint64_t static inline uint64_t _cha_8block(cha_ctx* ctx, uint8_t* begin, uint8_t* end) {
_cha_8block(uint32_t* state, uint8_t* begin, uint8_t* end) {
if (end - begin < 512) if (end - begin < 512)
return 0; return 0;
uint8_t* c = begin; uint8_t* c = begin;
uint32_t* state = ctx->state;
uint64_t* counter = (uint64_t*)(state + 12); uint64_t* counter = (uint64_t*)(state + 12);
/* constant for shuffling bytes (replacing multiple-of-8 rotates) */ /* constant for shuffling bytes (replacing multiple-of-8 rotates) */
__m256i rot16 = _mm256_set_epi8( __m256i rot16 = _mm256_set_epi8(
+10 -10
View File
@@ -24,15 +24,15 @@
#include "cha8block.h" #include "cha8block.h"
void cha_init(cha_ctx* ctx, const uint8_t* key, const uint8_t* iv) { void cha_init(cha_ctx* ctx, const uint8_t* key, const uint8_t* iv) {
ctx->input[0] = 0x61707865; ctx->state[0] = 0x61707865;
ctx->input[1] = 0x3320646e; ctx->state[1] = 0x3320646e;
ctx->input[2] = 0x79622d32; ctx->state[2] = 0x79622d32;
ctx->input[3] = 0x6b206574; ctx->state[3] = 0x6b206574;
memcpy(ctx->input + 4, key, 32); memcpy(ctx->state + 4, key, 32);
memcpy(ctx->input + 12, iv, 16); 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) { int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen) {
// The included header will mess with these variables // 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 // TODO: Handle resume if we are not at block boundary
if (__builtin_cpu_supports("ssse3")) { if (__builtin_cpu_supports("ssse3")) {
if (__builtin_cpu_supports("avx2")) { if (__builtin_cpu_supports("avx2")) {
c += _cha_8block(ctx->input, c, end); c += _cha_8block(ctx, c, end);
assert(end - c < 512); assert(end - c < 512);
} }
c += _cha_4block(ctx->input, c, end); c += _cha_4block(ctx, c, end);
assert(end - c < 256); assert(end - c < 256);
} }
c += _cha_block(ctx->input, c, end); c += _cha_block(ctx, c, end);
assert(c == end); assert(c == end);
return 0; return 0;
} }
+3 -3
View File
@@ -1,10 +1,11 @@
#pragma once
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
static const uint64_t CHA_BLOCK_SIZE = 64; static const uint64_t CHA_BLOCK_SIZE = 64;
typedef struct cha_ctx { typedef struct cha_ctx {
uint32_t input[16]; uint32_t state[16];
} cha_ctx; } cha_ctx;
/// @brief Initialize cha_ctx /// @brief Initialize cha_ctx
@@ -31,6 +32,5 @@ int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen);
/// @param iv /// @param iv
/// @return /// @return
int cha_generate( int cha_generate(
unsigned char* out, uint64_t outlen, const unsigned char key[32], uint8_t* out, uint64_t outlen, const uint8_t key[32], const uint8_t iv[16]
const unsigned char iv[16]
); );