0.2.0-dev Download ZIP
294 lines ·10.2 KB Raw
| 1 | /** |
| 2 | * \file chacha20_neon.c |
| 3 | * |
| 4 | * \brief Neon implementation of ChaCha20 |
| 5 | * |
| 6 | * Copyright The Mbed TLS Contributors |
| 7 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 8 | */ |
| 9 | |
| 10 | #include "tf_psa_crypto_common.h" |
| 11 | #include "chacha20_neon.h" |
| 12 | |
| 13 | #if defined(MBEDTLS_CHACHA20_C) && (MBEDTLS_CHACHA20_NEON_MULTIBLOCK != 0) |
| 14 | |
| 15 | #include "mbedtls/private/chacha20.h" |
| 16 | #include "mbedtls/platform_util.h" |
| 17 | #include "mbedtls/private/error_common.h" |
| 18 | |
| 19 | #include <stddef.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include "mbedtls/platform.h" |
| 23 | |
| 24 | #define CHACHA20_CTR_INDEX (12U) |
| 25 | #define CHACHA20_MAX_BLOCKS UINT32_MAX |
| 26 | #define CHACHA20_COUNTER_EXHAUSTED (MBEDTLS_CHACHA20_BLOCK_SIZE_BYTES + 1U) |
| 27 | |
| 28 | // Tested on all combinations of Armv7 arm/thumb2; Armv8 arm/thumb2/aarch64; Armv8 aarch64_be on |
| 29 | // clang 14, gcc 11, and some more recent versions. |
| 30 | |
| 31 | typedef struct { |
| 32 | uint32x4_t a, b, c, d; |
| 33 | } chacha20_neon_regs_t; |
| 34 | |
| 35 | // Define rotate-left operations that rotate within each 32-bit element in a 128-bit vector. |
| 36 | static inline uint32x4_t chacha20_neon_vrotlq_16_u32(uint32x4_t v) |
| 37 | { |
| 38 | return vreinterpretq_u32_u16(vrev32q_u16(vreinterpretq_u16_u32(v))); |
| 39 | } |
| 40 | |
| 41 | static inline uint32x4_t chacha20_neon_vrotlq_12_u32(uint32x4_t v) |
| 42 | { |
| 43 | uint32x4_t x = vshlq_n_u32(v, 12); |
| 44 | return vsriq_n_u32(x, v, 20); |
| 45 | } |
| 46 | |
| 47 | static inline uint32x4_t chacha20_neon_vrotlq_8_u32(uint32x4_t v) |
| 48 | { |
| 49 | uint32x4_t result; |
| 50 | #if defined(MBEDTLS_ARCH_IS_ARM64) |
| 51 | // This implementation is slightly faster, but only supported on 64-bit Arm |
| 52 | // Table look-up which results in an 8-bit rotate-left within each 32-bit element |
| 53 | const uint8_t idx_rotl8[16] = { 3, 0, 1, 2, 7, 4, 5, 6, 11, 8, 9, 10, 15, 12, 13, 14 }; |
| 54 | const uint8x16_t vrotl8_tbl = vld1q_u8(idx_rotl8); |
| 55 | result = vreinterpretq_u32_u8(vqtbl1q_u8(vreinterpretq_u8_u32(v), vrotl8_tbl)); |
| 56 | #else |
| 57 | uint32x4_t a = vshlq_n_u32(v, 8); |
| 58 | result = vsriq_n_u32(a, v, 24); |
| 59 | #endif |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | static inline uint32x4_t chacha20_neon_vrotlq_7_u32(uint32x4_t v) |
| 64 | { |
| 65 | uint32x4_t x = vshlq_n_u32(v, 7); |
| 66 | return vsriq_n_u32(x, v, 25); |
| 67 | } |
| 68 | |
| 69 | // Increment the 32-bit element within v that corresponds to the ChaCha20 counter |
| 70 | static inline uint32x4_t chacha20_neon_inc_counter(uint32x4_t v) |
| 71 | { |
| 72 | /* { 1, 0, 0, 0 } */ |
| 73 | uint32x4_t counter_increment = vcombine_u32(vcreate_u32(1), vdup_n_u32(0)); |
| 74 | return vaddq_u32(v, counter_increment); |
| 75 | } |
| 76 | |
| 77 | static inline chacha20_neon_regs_t chacha20_neon_singlepass(chacha20_neon_regs_t r) |
| 78 | { |
| 79 | for (unsigned i = 0; i < 2; i++) { |
| 80 | r.a = vaddq_u32(r.a, r.b); // r.a += b |
| 81 | r.d = veorq_u32(r.d, r.a); // r.d ^= a |
| 82 | r.d = chacha20_neon_vrotlq_16_u32(r.d); // r.d <<<= 16 |
| 83 | |
| 84 | r.c = vaddq_u32(r.c, r.d); // r.c += d |
| 85 | r.b = veorq_u32(r.b, r.c); // r.b ^= c |
| 86 | r.b = chacha20_neon_vrotlq_12_u32(r.b); // r.b <<<= 12 |
| 87 | |
| 88 | r.a = vaddq_u32(r.a, r.b); // r.a += b |
| 89 | r.d = veorq_u32(r.d, r.a); // r.d ^= a |
| 90 | r.d = chacha20_neon_vrotlq_8_u32(r.d); // r.d <<<= 8 |
| 91 | |
| 92 | r.c = vaddq_u32(r.c, r.d); // r.c += d |
| 93 | r.b = veorq_u32(r.b, r.c); // r.b ^= c |
| 94 | r.b = chacha20_neon_vrotlq_7_u32(r.b); // r.b <<<= 7 |
| 95 | |
| 96 | // re-order b, c and d for the diagonal rounds |
| 97 | r.c = vextq_u32(r.c, r.c, 2); |
| 98 | if (i == 0) { |
| 99 | r.b = vextq_u32(r.b, r.b, 1); |
| 100 | r.d = vextq_u32(r.d, r.d, 3); |
| 101 | } else { |
| 102 | // restore element order in b, c, d |
| 103 | r.b = vextq_u32(r.b, r.b, 3); |
| 104 | r.d = vextq_u32(r.d, r.d, 1); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return r; |
| 109 | } |
| 110 | |
| 111 | static inline void chacha20_neon_finish_block(chacha20_neon_regs_t r, |
| 112 | chacha20_neon_regs_t r_original, |
| 113 | uint8_t **output, |
| 114 | const uint8_t **input) |
| 115 | { |
| 116 | const uint8_t *i = *input; |
| 117 | uint8_t *o = *output; |
| 118 | |
| 119 | r.a = vaddq_u32(r.a, r_original.a); |
| 120 | r.b = vaddq_u32(r.b, r_original.b); |
| 121 | r.c = vaddq_u32(r.c, r_original.c); |
| 122 | r.d = vaddq_u32(r.d, r_original.d); |
| 123 | |
| 124 | vst1q_u8(o + 0, veorq_u8(vld1q_u8(i + 0), vreinterpretq_u8_u32(r.a))); |
| 125 | vst1q_u8(o + 16, veorq_u8(vld1q_u8(i + 16), vreinterpretq_u8_u32(r.b))); |
| 126 | vst1q_u8(o + 32, veorq_u8(vld1q_u8(i + 32), vreinterpretq_u8_u32(r.c))); |
| 127 | vst1q_u8(o + 48, veorq_u8(vld1q_u8(i + 48), vreinterpretq_u8_u32(r.d))); |
| 128 | |
| 129 | *input = i + MBEDTLS_CHACHA20_BLOCK_SIZE_BYTES; |
| 130 | *output = o + MBEDTLS_CHACHA20_BLOCK_SIZE_BYTES; |
| 131 | } |
| 132 | |
| 133 | // Prevent gcc from rolling up the (manually unrolled) interleaved block loops |
| 134 | MBEDTLS_OPTIMIZE_FOR_PERFORMANCE |
| 135 | static inline uint32x4_t chacha20_neon_blocks(chacha20_neon_regs_t r_original, |
| 136 | uint8_t *output, |
| 137 | const uint8_t *input, |
| 138 | size_t blocks) |
| 139 | { |
| 140 | // Assuming 32 regs, with 4 for original values plus 4 for scratch, with 4 regs per block, |
| 141 | // we should be able to process up to 24/4 = 6 blocks simultaneously. |
| 142 | // Testing confirms that perf indeed increases with more blocks, and then falls off after 6. |
| 143 | |
| 144 | for (;;) { |
| 145 | chacha20_neon_regs_t r[6]; |
| 146 | |
| 147 | // It's essential to unroll these loops to benefit from interleaving multiple blocks. |
| 148 | // If MBEDTLS_CHACHA20_NEON_MULTIBLOCK < 6, gcc and clang will optimise away the unused bits |
| 149 | r[0] = r_original; |
| 150 | r[1] = r_original; |
| 151 | r[2] = r_original; |
| 152 | r[3] = r_original; |
| 153 | r[4] = r_original; |
| 154 | r[5] = r_original; |
| 155 | r[1].d = chacha20_neon_inc_counter(r[0].d); |
| 156 | r[2].d = chacha20_neon_inc_counter(r[1].d); |
| 157 | r[3].d = chacha20_neon_inc_counter(r[2].d); |
| 158 | r[4].d = chacha20_neon_inc_counter(r[3].d); |
| 159 | r[5].d = chacha20_neon_inc_counter(r[4].d); |
| 160 | |
| 161 | for (unsigned i = 0; i < 10; i++) { |
| 162 | r[0] = chacha20_neon_singlepass(r[0]); |
| 163 | r[1] = chacha20_neon_singlepass(r[1]); |
| 164 | r[2] = chacha20_neon_singlepass(r[2]); |
| 165 | r[3] = chacha20_neon_singlepass(r[3]); |
| 166 | r[4] = chacha20_neon_singlepass(r[4]); |
| 167 | r[5] = chacha20_neon_singlepass(r[5]); |
| 168 | } |
| 169 | |
| 170 | chacha20_neon_finish_block(r[0], r_original, &output, &input); |
| 171 | r_original.d = chacha20_neon_inc_counter(r_original.d); |
| 172 | if (--blocks == 0) { |
| 173 | return r_original.d; |
| 174 | } |
| 175 | #if MBEDTLS_CHACHA20_NEON_MULTIBLOCK >= 2 |
| 176 | chacha20_neon_finish_block(r[1], r_original, &output, &input); |
| 177 | r_original.d = chacha20_neon_inc_counter(r_original.d); |
| 178 | if (--blocks == 0) { |
| 179 | return r_original.d; |
| 180 | } |
| 181 | #endif |
| 182 | #if MBEDTLS_CHACHA20_NEON_MULTIBLOCK >= 3 |
| 183 | chacha20_neon_finish_block(r[2], r_original, &output, &input); |
| 184 | r_original.d = chacha20_neon_inc_counter(r_original.d); |
| 185 | if (--blocks == 0) { |
| 186 | return r_original.d; |
| 187 | } |
| 188 | #endif |
| 189 | #if MBEDTLS_CHACHA20_NEON_MULTIBLOCK >= 4 |
| 190 | chacha20_neon_finish_block(r[3], r_original, &output, &input); |
| 191 | r_original.d = chacha20_neon_inc_counter(r_original.d); |
| 192 | if (--blocks == 0) { |
| 193 | return r_original.d; |
| 194 | } |
| 195 | #endif |
| 196 | #if MBEDTLS_CHACHA20_NEON_MULTIBLOCK >= 5 |
| 197 | chacha20_neon_finish_block(r[4], r_original, &output, &input); |
| 198 | r_original.d = chacha20_neon_inc_counter(r_original.d); |
| 199 | if (--blocks == 0) { |
| 200 | return r_original.d; |
| 201 | } |
| 202 | #endif |
| 203 | #if MBEDTLS_CHACHA20_NEON_MULTIBLOCK >= 6 |
| 204 | chacha20_neon_finish_block(r[5], r_original, &output, &input); |
| 205 | r_original.d = chacha20_neon_inc_counter(r_original.d); |
| 206 | if (--blocks == 0) { |
| 207 | return r_original.d; |
| 208 | } |
| 209 | #endif |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | int mbedtls_chacha20_update(mbedtls_chacha20_context *ctx, |
| 214 | size_t size, |
| 215 | const unsigned char *input, |
| 216 | unsigned char *output) |
| 217 | { |
| 218 | size_t offset = 0U; |
| 219 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 220 | size_t blocks = 0U; |
| 221 | size_t partial_block_size = 0U; |
| 222 | int final_counter_block = 0; |
| 223 | |
| 224 | ret = mbedtls_chacha20_check_counter_wrap(ctx, size); |
| 225 | if (ret != 0) { |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | /* Use leftover keystream bytes, if available */ |
| 230 | while (ctx->keystream_bytes_used < MBEDTLS_CHACHA20_BLOCK_SIZE_BYTES && size > 0) { |
| 231 | output[offset] = input[offset] |
| 232 | ^ ctx->keystream8[ctx->keystream_bytes_used]; |
| 233 | |
| 234 | ctx->keystream_bytes_used++; |
| 235 | offset++; |
| 236 | size--; |
| 237 | |
| 238 | if (ctx->keystream_bytes_used == MBEDTLS_CHACHA20_BLOCK_SIZE_BYTES && |
| 239 | ctx->state[CHACHA20_CTR_INDEX] == 0U) { |
| 240 | ctx->keystream_bytes_used = CHACHA20_COUNTER_EXHAUSTED; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | blocks = size / MBEDTLS_CHACHA20_BLOCK_SIZE_BYTES; |
| 245 | partial_block_size = size % MBEDTLS_CHACHA20_BLOCK_SIZE_BYTES; |
| 246 | if (blocks != 0U || partial_block_size != 0U) { |
| 247 | uint64_t generated_blocks = (uint64_t) blocks; |
| 248 | |
| 249 | if (partial_block_size != 0U) { |
| 250 | generated_blocks++; |
| 251 | } |
| 252 | |
| 253 | final_counter_block = |
| 254 | generated_blocks - 1U == |
| 255 | (uint64_t) CHACHA20_MAX_BLOCKS - ctx->state[CHACHA20_CTR_INDEX]; |
| 256 | } |
| 257 | |
| 258 | /* Load state into NEON registers */ |
| 259 | chacha20_neon_regs_t state; |
| 260 | state.a = vld1q_u32(&ctx->state[0]); |
| 261 | state.b = vld1q_u32(&ctx->state[4]); |
| 262 | state.c = vld1q_u32(&ctx->state[8]); |
| 263 | state.d = vld1q_u32(&ctx->state[12]); |
| 264 | |
| 265 | /* Process full blocks */ |
| 266 | if (blocks != 0U) { |
| 267 | state.d = chacha20_neon_blocks(state, output + offset, input + offset, blocks); |
| 268 | |
| 269 | offset += MBEDTLS_CHACHA20_BLOCK_SIZE_BYTES * blocks; |
| 270 | size -= MBEDTLS_CHACHA20_BLOCK_SIZE_BYTES * blocks; |
| 271 | } |
| 272 | |
| 273 | /* Last (partial) block */ |
| 274 | if (size > 0U) { |
| 275 | /* Generate new keystream block and increment counter */ |
| 276 | memset(ctx->keystream8, 0, MBEDTLS_CHACHA20_BLOCK_SIZE_BYTES); |
| 277 | state.d = chacha20_neon_blocks(state, ctx->keystream8, ctx->keystream8, 1); |
| 278 | |
| 279 | mbedtls_xor_no_simd(output + offset, input + offset, ctx->keystream8, size); |
| 280 | |
| 281 | ctx->keystream_bytes_used = size; |
| 282 | } |
| 283 | |
| 284 | if (final_counter_block && partial_block_size == 0U) { |
| 285 | ctx->keystream_bytes_used = CHACHA20_COUNTER_EXHAUSTED; |
| 286 | } |
| 287 | |
| 288 | /* Capture state */ |
| 289 | vst1q_u32(&ctx->state[12], state.d); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | #endif /* defined(MBEDTLS_CHACHA20_C) && (MBEDTLS_CHACHA20_NEON_MULTIBLOCK != 0) */ |