diff options
author | Niels Möller <nisse@lysator.liu.se> | 2020-11-30 19:54:10 +0100 |
---|---|---|
committer | Niels Möller <nisse@lysator.liu.se> | 2020-11-30 20:02:57 +0100 |
commit | 58a0301437e9beb23130423ff1063a67b6f2b43b (patch) | |
tree | 1640513150ae83d26591656ffe12d0fa0e6230ae /chacha-crypt.c | |
parent | 58c55046beda976b10ac3ce930696d172e5e5038 (diff) | |
download | nettle-58a0301437e9beb23130423ff1063a67b6f2b43b.tar.gz |
ppc: New assembly for chacha_core4, doing four blocks in parallel.
* chacha-crypt.c: (_nettle_chacha_crypt_4core)
(_nettle_chacha_crypt32_4core): New functions.
* chacha-internal.h: Add prototypes for _nettle_chacha_4core and
related functions.
* configure.ac (asm_nettle_optional_list): Add chacha-4core.asm.
* powerpc64/fat/chacha-4core.asm: New file.
* powerpc64/p7/chacha-4core.asm: New file.
* fat-ppc.c (fat_init): When altivec is available, use
_nettle_chacha_crypt_4core and _nettle_chacha_crypt32_4core
instead of _2core variants.
Diffstat (limited to 'chacha-crypt.c')
-rw-r--r-- | chacha-crypt.c | 102 |
1 files changed, 93 insertions, 9 deletions
diff --git a/chacha-crypt.c b/chacha-crypt.c index a13898f1..d3af5f58 100644 --- a/chacha-crypt.c +++ b/chacha-crypt.c @@ -54,17 +54,60 @@ #define CHACHA_ROUNDS 20 -#if HAVE_NATIVE_chacha_3core +#if HAVE_NATIVE_chacha_4core +#define _nettle_chacha_crypt_4core chacha_crypt +#define _nettle_chacha_crypt32_4core chacha_crypt32 +#elif HAVE_NATIVE_chacha_3core #define _nettle_chacha_crypt_3core chacha_crypt #define _nettle_chacha_crypt32_3core chacha_crypt32 -#elif HAVE_NATIVE_chacha_2core -#define _nettle_chacha_crypt_2core chacha_crypt -#define _nettle_chacha_crypt32_2core chacha_crypt32 -#elif !(HAVE_NATIVE_fat_chacha_3core || HAVE_NATIVE_fat_chacha_2core) +#elif !(HAVE_NATIVE_fat_chacha_4core || HAVE_NATIVE_fat_chacha_3core) #define _nettle_chacha_crypt_1core chacha_crypt #define _nettle_chacha_crypt32_1core chacha_crypt32 #endif +#if HAVE_NATIVE_chacha_4core || HAVE_NATIVE_fat_chacha_4core +void +_nettle_chacha_crypt_4core(struct chacha_ctx *ctx, + size_t length, + uint8_t *dst, + const uint8_t *src) +{ + uint32_t x[4*_CHACHA_STATE_LENGTH]; + + if (!length) + return; + + while (length > 2*CHACHA_BLOCK_SIZE) + { + _nettle_chacha_4core (x, ctx->state, CHACHA_ROUNDS); + ctx->state[12] += 4; + ctx->state[13] += (ctx->state[12] < 4); + if (length <= 4*CHACHA_BLOCK_SIZE) + { + memxor3 (dst, src, x, length); + return; + } + memxor3 (dst, src, x, 4*CHACHA_BLOCK_SIZE); + + length -= 4*CHACHA_BLOCK_SIZE; + dst += 4*CHACHA_BLOCK_SIZE; + src += 4*CHACHA_BLOCK_SIZE; + } + if (length > CHACHA_BLOCK_SIZE) + { + _nettle_chacha_2core (x, ctx->state, CHACHA_ROUNDS); + ctx->state[12] += 2; + ctx->state[13] += (ctx->state[12] < 2); + } + else + { + _nettle_chacha_core (x, ctx->state, CHACHA_ROUNDS); + ctx->state[13] += (++ctx->state[12] == 0); + } + memxor3 (dst, src, x, length); +} +#endif + #if HAVE_NATIVE_chacha_3core || HAVE_NATIVE_fat_chacha_3core void _nettle_chacha_crypt_3core(struct chacha_ctx *ctx, @@ -108,7 +151,7 @@ _nettle_chacha_crypt_3core(struct chacha_ctx *ctx, } #endif -#if HAVE_NATIVE_chacha_2core || HAVE_NATIVE_fat_chacha_2core +#if 0 void _nettle_chacha_crypt_2core(struct chacha_ctx *ctx, size_t length, @@ -143,7 +186,7 @@ _nettle_chacha_crypt_2core(struct chacha_ctx *ctx, } #endif -#if !(HAVE_NATIVE_chacha_3core || HAVE_NATIVE_chacha_2core) +#if !(HAVE_NATIVE_chacha_4core || HAVE_NATIVE_chacha_3core) void _nettle_chacha_crypt_1core(struct chacha_ctx *ctx, size_t length, @@ -177,6 +220,47 @@ _nettle_chacha_crypt_1core(struct chacha_ctx *ctx, } #endif +#if HAVE_NATIVE_chacha_4core || HAVE_NATIVE_fat_chacha_4core +void +_nettle_chacha_crypt32_4core(struct chacha_ctx *ctx, + size_t length, + uint8_t *dst, + const uint8_t *src) +{ + uint32_t x[4*_CHACHA_STATE_LENGTH]; + + if (!length) + return; + + while (length > 2*CHACHA_BLOCK_SIZE) + { + _nettle_chacha_4core32 (x, ctx->state, CHACHA_ROUNDS); + ctx->state[12] += 4; + if (length <= 4*CHACHA_BLOCK_SIZE) + { + memxor3 (dst, src, x, length); + return; + } + memxor3 (dst, src, x, 4*CHACHA_BLOCK_SIZE); + + length -= 4*CHACHA_BLOCK_SIZE; + dst += 4*CHACHA_BLOCK_SIZE; + src += 4*CHACHA_BLOCK_SIZE; + } + if (length > CHACHA_BLOCK_SIZE) + { + _nettle_chacha_2core32 (x, ctx->state, CHACHA_ROUNDS); + ctx->state[12] += 2; + } + else + { + _nettle_chacha_core (x, ctx->state, CHACHA_ROUNDS); + ++ctx->state[12]; + } + memxor3 (dst, src, x, length); +} +#endif + #if HAVE_NATIVE_chacha_3core || HAVE_NATIVE_fat_chacha_3core void _nettle_chacha_crypt32_3core(struct chacha_ctx *ctx, @@ -218,7 +302,7 @@ _nettle_chacha_crypt32_3core(struct chacha_ctx *ctx, } #endif -#if HAVE_NATIVE_chacha_2core || HAVE_NATIVE_fat_chacha_2core +#if 0 void _nettle_chacha_crypt32_2core(struct chacha_ctx *ctx, size_t length, @@ -252,7 +336,7 @@ _nettle_chacha_crypt32_2core(struct chacha_ctx *ctx, } #endif -#if !(HAVE_NATIVE_chacha_3core || HAVE_NATIVE_chacha_2core) +#if !(HAVE_NATIVE_chacha_4core || HAVE_NATIVE_chacha_3core) void _nettle_chacha_crypt32_1core(struct chacha_ctx *ctx, size_t length, |