diff options
Diffstat (limited to 'src/libcryptobox/chacha20/chacha.c')
-rw-r--r-- | src/libcryptobox/chacha20/chacha.c | 143 |
1 files changed, 71 insertions, 72 deletions
diff --git a/src/libcryptobox/chacha20/chacha.c b/src/libcryptobox/chacha20/chacha.c index 653c614d9..0b471c80a 100644 --- a/src/libcryptobox/chacha20/chacha.c +++ b/src/libcryptobox/chacha20/chacha.c @@ -32,37 +32,39 @@ extern unsigned cpu_config; typedef struct chacha_impl_t { unsigned long cpu_flags; const char *desc; - void (*chacha) (const chacha_key *key, const chacha_iv *iv, - const unsigned char *in, unsigned char *out, size_t inlen, - size_t rounds); - void (*xchacha) (const chacha_key *key, const chacha_iv24 *iv, - const unsigned char *in, unsigned char *out, size_t inlen, - size_t rounds); - void (*chacha_blocks) (chacha_state_internal *state, - const unsigned char *in, unsigned char *out, size_t bytes); - void (*hchacha) (const unsigned char key[32], const unsigned char iv[16], - unsigned char out[32], size_t rounds); + void (*chacha)(const chacha_key *key, const chacha_iv *iv, + const unsigned char *in, unsigned char *out, size_t inlen, + size_t rounds); + void (*xchacha)(const chacha_key *key, const chacha_iv24 *iv, + const unsigned char *in, unsigned char *out, size_t inlen, + size_t rounds); + void (*chacha_blocks)(chacha_state_internal *state, + const unsigned char *in, unsigned char *out, size_t bytes); + void (*hchacha)(const unsigned char key[32], const unsigned char iv[16], + unsigned char out[32], size_t rounds); } chacha_impl_t; -#define CHACHA_DECLARE(ext) \ - void chacha_##ext(const chacha_key *key, const chacha_iv *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); \ - void xchacha_##ext(const chacha_key *key, const chacha_iv24 *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); \ - void chacha_blocks_##ext(chacha_state_internal *state, const unsigned char *in, unsigned char *out, size_t bytes); \ - void hchacha_##ext(const unsigned char key[32], const unsigned char iv[16], unsigned char out[32], size_t rounds); -#define CHACHA_IMPL(cpuflags, desc, ext) \ - {(cpuflags), desc, chacha_##ext, xchacha_##ext, chacha_blocks_##ext, hchacha_##ext} +#define CHACHA_DECLARE(ext) \ + void chacha_##ext(const chacha_key *key, const chacha_iv *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); \ + void xchacha_##ext(const chacha_key *key, const chacha_iv24 *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); \ + void chacha_blocks_##ext(chacha_state_internal *state, const unsigned char *in, unsigned char *out, size_t bytes); \ + void hchacha_##ext(const unsigned char key[32], const unsigned char iv[16], unsigned char out[32], size_t rounds); +#define CHACHA_IMPL(cpuflags, desc, ext) \ + { \ + (cpuflags), desc, chacha_##ext, xchacha_##ext, chacha_blocks_##ext, hchacha_##ext \ + } #if defined(HAVE_AVX2) && defined(__x86_64__) - CHACHA_DECLARE(avx2) - #define CHACHA_AVX2 CHACHA_IMPL(CPUID_AVX2, "avx2", avx2) +CHACHA_DECLARE(avx2) +#define CHACHA_AVX2 CHACHA_IMPL(CPUID_AVX2, "avx2", avx2) #endif #if defined(HAVE_AVX) && defined(__x86_64__) - CHACHA_DECLARE(avx) - #define CHACHA_AVX CHACHA_IMPL(CPUID_AVX, "avx", avx) +CHACHA_DECLARE(avx) +#define CHACHA_AVX CHACHA_IMPL(CPUID_AVX, "avx", avx) #endif #if defined(HAVE_SSE2) && defined(__x86_64__) - CHACHA_DECLARE(sse2) - #define CHACHA_SSE2 CHACHA_IMPL(CPUID_SSE2, "sse2", sse2) +CHACHA_DECLARE(sse2) +#define CHACHA_SSE2 CHACHA_IMPL(CPUID_SSE2, "sse2", sse2) #endif CHACHA_DECLARE(ref) @@ -84,18 +86,18 @@ static const chacha_impl_t chacha_list[] = { static const chacha_impl_t *chacha_impl = &chacha_list[0]; static int -chacha_is_aligned (const void *p) +chacha_is_aligned(const void *p) { return ((size_t) p & (sizeof(size_t) - 1)) == 0; } const char * -chacha_load (void) +chacha_load(void) { guint i; if (cpu_config != 0) { - for (i = 0; i < G_N_ELEMENTS (chacha_list); i ++) { + for (i = 0; i < G_N_ELEMENTS(chacha_list); i++) { if (chacha_list[i].cpu_flags & cpu_config) { chacha_impl = &chacha_list[i]; break; @@ -106,21 +108,21 @@ chacha_load (void) return chacha_impl->desc; } -void chacha_init (chacha_state *S, const chacha_key *key, - const chacha_iv *iv, size_t rounds) +void chacha_init(chacha_state *S, const chacha_key *key, + const chacha_iv *iv, size_t rounds) { chacha_state_internal *state = (chacha_state_internal *) S; - memcpy (state->s + 0, key, 32); - memset (state->s + 32, 0, 8); - memcpy (state->s + 40, iv, 8); + memcpy(state->s + 0, key, 32); + memset(state->s + 32, 0, 8); + memcpy(state->s + 40, iv, 8); state->rounds = rounds; state->leftover = 0; } /* processes inlen bytes (can do partial blocks), handling input/output alignment */ static void -chacha_consume (chacha_state_internal *state, - const unsigned char *in, unsigned char *out, size_t inlen) +chacha_consume(chacha_state_internal *state, + const unsigned char *in, unsigned char *out, size_t inlen) { unsigned char buffer[16 * CHACHA_BLOCKBYTES]; int in_aligned, out_aligned; @@ -130,10 +132,10 @@ chacha_consume (chacha_state_internal *state, return; /* if everything is aligned, handle directly */ - in_aligned = chacha_is_aligned (in); - out_aligned = chacha_is_aligned (out); + in_aligned = chacha_is_aligned(in); + out_aligned = chacha_is_aligned(out); if (in_aligned && out_aligned) { - chacha_impl->chacha_blocks (state, in, out, inlen); + chacha_impl->chacha_blocks(state, in, out, inlen); return; } @@ -143,12 +145,12 @@ chacha_consume (chacha_state_internal *state, const unsigned char *src = in; unsigned char *dst = (out_aligned) ? out : buffer; if (!in_aligned) { - memcpy (buffer, in, bytes); + memcpy(buffer, in, bytes); src = buffer; } - chacha_impl->chacha_blocks (state, src, dst, bytes); + chacha_impl->chacha_blocks(state, src, dst, bytes); if (!out_aligned) - memcpy (out, buffer, bytes); + memcpy(out, buffer, bytes); if (in) in += bytes; out += bytes; @@ -157,16 +159,16 @@ chacha_consume (chacha_state_internal *state, } /* hchacha */ -void hchacha (const unsigned char key[32], - const unsigned char iv[16], unsigned char out[32], size_t rounds) +void hchacha(const unsigned char key[32], + const unsigned char iv[16], unsigned char out[32], size_t rounds) { - chacha_impl->hchacha (key, iv, out, rounds); + chacha_impl->hchacha(key, iv, out, rounds); } /* update, returns number of bytes written to out */ size_t -chacha_update (chacha_state *S, const unsigned char *in, unsigned char *out, - size_t inlen) +chacha_update(chacha_state *S, const unsigned char *in, unsigned char *out, + size_t inlen) { chacha_state_internal *state = (chacha_state_internal *) S; unsigned char *out_start = out; @@ -178,11 +180,11 @@ chacha_update (chacha_state *S, const unsigned char *in, unsigned char *out, if (state->leftover) { bytes = (CHACHA_BLOCKBYTES - state->leftover); if (in) { - memcpy (state->buffer + state->leftover, in, bytes); + memcpy(state->buffer + state->leftover, in, bytes); in += bytes; } - chacha_consume (state, (in) ? state->buffer : NULL, out, - CHACHA_BLOCKBYTES); + chacha_consume(state, (in) ? state->buffer : NULL, out, + CHACHA_BLOCKBYTES); inlen -= bytes; out += CHACHA_BLOCKBYTES; state->leftover = 0; @@ -191,7 +193,7 @@ chacha_update (chacha_state *S, const unsigned char *in, unsigned char *out, /* handle the direct data */ bytes = (inlen & ~(CHACHA_BLOCKBYTES - 1)); if (bytes) { - chacha_consume (state, in, out, bytes); + chacha_consume(state, in, out, bytes); inlen -= bytes; if (in) in += bytes; @@ -202,9 +204,9 @@ chacha_update (chacha_state *S, const unsigned char *in, unsigned char *out, /* handle leftover data */ if (inlen) { if (in) - memcpy (state->buffer + state->leftover, in, inlen); + memcpy(state->buffer + state->leftover, in, inlen); else - memset (state->buffer + state->leftover, 0, inlen); + memset(state->buffer + state->leftover, 0, inlen); state->leftover += inlen; } @@ -213,51 +215,48 @@ chacha_update (chacha_state *S, const unsigned char *in, unsigned char *out, /* finalize, write out any leftover data */ size_t -chacha_final (chacha_state *S, unsigned char *out) +chacha_final(chacha_state *S, unsigned char *out) { chacha_state_internal *state = (chacha_state_internal *) S; size_t leftover = state->leftover; if (leftover) { - if (chacha_is_aligned (out)) { - chacha_impl->chacha_blocks (state, state->buffer, out, leftover); + if (chacha_is_aligned(out)) { + chacha_impl->chacha_blocks(state, state->buffer, out, leftover); } else { - chacha_impl->chacha_blocks (state, state->buffer, state->buffer, - leftover); - memcpy (out, state->buffer, leftover); + chacha_impl->chacha_blocks(state, state->buffer, state->buffer, + leftover); + memcpy(out, state->buffer, leftover); } } - rspamd_explicit_memzero (S, sizeof(chacha_state)); + rspamd_explicit_memzero(S, sizeof(chacha_state)); return leftover; } /* one-shot, input/output assumed to be word aligned */ -void -chacha (const chacha_key *key, const chacha_iv *iv, - const unsigned char *in, unsigned char *out, size_t inlen, - size_t rounds) +void chacha(const chacha_key *key, const chacha_iv *iv, + const unsigned char *in, unsigned char *out, size_t inlen, + size_t rounds) { - chacha_impl->chacha (key, iv, in, out, inlen, rounds); + chacha_impl->chacha(key, iv, in, out, inlen, rounds); } /* xchacha, chacha with a 192 bit nonce */ -void -xchacha_init (chacha_state *S, const chacha_key *key, - const chacha_iv24 *iv, size_t rounds) +void xchacha_init(chacha_state *S, const chacha_key *key, + const chacha_iv24 *iv, size_t rounds) { chacha_key subkey; - hchacha (key->b, iv->b, subkey.b, rounds); - chacha_init (S, &subkey, (chacha_iv *) (iv->b + 16), rounds); + hchacha(key->b, iv->b, subkey.b, rounds); + chacha_init(S, &subkey, (chacha_iv *) (iv->b + 16), rounds); } /* one-shot, input/output assumed to be word aligned */ -void -xchacha (const chacha_key *key, const chacha_iv24 *iv, - const unsigned char *in, unsigned char *out, size_t inlen, - size_t rounds) +void xchacha(const chacha_key *key, const chacha_iv24 *iv, + const unsigned char *in, unsigned char *out, size_t inlen, + size_t rounds) { - chacha_impl->xchacha (key, iv, in, out, inlen, rounds); + chacha_impl->xchacha(key, iv, in, out, inlen, rounds); } |