Skip to content
0.2.0-dev Download ZIP
62 lines ·2.5 KB Raw
1/**
2 * \file chacha20_neon.h
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#ifndef TF_PSA_CRYPTO_CHACHA20_NEON_H
10#define TF_PSA_CRYPTO_CHACHA20_NEON_H
11
12#include "tf_psa_crypto_common.h"
13
14/*
15 * The Neon implementation can be configured to process multiple blocks in parallel; increasing the
16 * number of blocks gains a lot of performance, but adds on average around 250 bytes of code size
17 * for each additional block.
18 *
19 * This is controlled by setting MBEDTLS_CHACHA20_NEON_MULTIBLOCK in the range [0..6] (0 selects
20 * the scalar implementation; 1 selects single-block Neon; 2..6 select multi-block Neon).
21 *
22 * The default (i.e., if MBEDTLS_CHACHA20_NEON_MULTIBLOCK is not set) selects the fastest variant
23 * which has better code size than the scalar implementation (based on testing for Aarch64 on clang
24 * and gcc).
25 *
26 * Size & performance notes for Neon implementation from informal tests on Aarch64
27 * (applies to both gcc and clang except as noted):
28 * - When single-block is selected, this saves around 400-550 bytes of code-size c.f. the scalar
29 * implementation
30 * - Multi-block Neon is smaller and faster than scalar (up to 2 blocks for gcc, 3 for clang)
31 * - Code size increases consistently with number of blocks
32 * - Performance increases with number of blocks (except at 5 which is slightly slower than 4)
33 * - Performance is within a few % for gcc vs clang at all settings
34 * - Performance at 4 blocks roughly matches our hardware accelerated AES-GCM impl with
35 * better code size
36 * - Performance is worse at 7 or more blocks, due to running out of Neon registers
37 */
38
39#if !defined(MBEDTLS_HAVE_NEON_INTRINSICS)
40// Select scalar implementation if Neon not available
41 #define MBEDTLS_CHACHA20_NEON_MULTIBLOCK 0
42#elif !defined(MBEDTLS_CHACHA20_NEON_MULTIBLOCK)
43// By default, select the best performing option that is not a code-size regression (based on
44// measurements from recent gcc and clang).
45#if defined(MBEDTLS_ARCH_IS_THUMB)
46 #if defined(MBEDTLS_COMPILER_IS_GCC)
47 #define MBEDTLS_CHACHA20_NEON_MULTIBLOCK 1
48 #else
49 #define MBEDTLS_CHACHA20_NEON_MULTIBLOCK 2
50 #endif
51#elif defined(MBEDTLS_ARCH_IS_ARM64)
52 #define MBEDTLS_CHACHA20_NEON_MULTIBLOCK 3
53#else
54 #if defined(MBEDTLS_COMPILER_IS_GCC)
55 #define MBEDTLS_CHACHA20_NEON_MULTIBLOCK 2
56 #else
57 #define MBEDTLS_CHACHA20_NEON_MULTIBLOCK 3
58 #endif
59#endif
60#endif
61
62#endif /* TF_PSA_CRYPTO_CHACHA20_NEON_H */