xref: /aosp_15_r20/external/boringssl/src/crypto/chacha/internal.h (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (c) 2018, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #ifndef OPENSSL_HEADER_CHACHA_INTERNAL
16 #define OPENSSL_HEADER_CHACHA_INTERNAL
17 
18 #include <openssl/base.h>
19 
20 #include "../internal.h"
21 
22 #if defined(__cplusplus)
23 extern "C" {
24 #endif
25 
26 
27 // CRYPTO_hchacha20 computes the HChaCha20 function, which should only be used
28 // as part of XChaCha20.
29 void CRYPTO_hchacha20(uint8_t out[32], const uint8_t key[32],
30                       const uint8_t nonce[16]);
31 
32 #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86)
33 
34 #define CHACHA20_ASM_NOHW
35 
36 #define CHACHA20_ASM_SSSE3
ChaCha20_ctr32_ssse3_capable(size_t len)37 OPENSSL_INLINE int ChaCha20_ctr32_ssse3_capable(size_t len) {
38   // Unlike the x86_64 version, the x86 SSSE3 routine runs for all non-zero
39   // lengths.
40   return len > 0 && CRYPTO_is_SSSE3_capable() && CRYPTO_is_FXSR_capable();
41 }
42 void ChaCha20_ctr32_ssse3(uint8_t *out, const uint8_t *in, size_t in_len,
43                           const uint32_t key[8], const uint32_t counter[4]);
44 
45 #elif !defined(OPENSSL_NO_ASM) && \
46     (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
47 
48 #define CHACHA20_ASM_NOHW
49 
50 #define CHACHA20_ASM_NEON
ChaCha20_ctr32_neon_capable(size_t len)51 OPENSSL_INLINE int ChaCha20_ctr32_neon_capable(size_t len) {
52   return len >= 192 && CRYPTO_is_NEON_capable();
53 }
54 void ChaCha20_ctr32_neon(uint8_t *out, const uint8_t *in, size_t in_len,
55                          const uint32_t key[8], const uint32_t counter[4]);
56 #elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64)
57 #define CHACHA20_ASM_NOHW
58 
59 #define CHACHA20_ASM_AVX2
ChaCha20_ctr32_avx2_capable(size_t len)60 OPENSSL_INLINE int ChaCha20_ctr32_avx2_capable(size_t len) {
61   return len > 128 && CRYPTO_is_AVX2_capable();
62 }
63 void ChaCha20_ctr32_avx2(uint8_t *out, const uint8_t *in, size_t in_len,
64                          const uint32_t key[8], const uint32_t counter[4]);
65 
66 #define CHACHA20_ASM_SSSE3_4X
ChaCha20_ctr32_ssse3_4x_capable(size_t len)67 OPENSSL_INLINE int ChaCha20_ctr32_ssse3_4x_capable(size_t len) {
68   int capable = len > 128 && CRYPTO_is_SSSE3_capable();
69   int faster = len > 192 || !CRYPTO_cpu_perf_is_like_silvermont();
70   return capable && faster;
71 }
72 void ChaCha20_ctr32_ssse3_4x(uint8_t *out, const uint8_t *in, size_t in_len,
73                              const uint32_t key[8], const uint32_t counter[4]);
74 
75 #define CHACHA20_ASM_SSSE3
ChaCha20_ctr32_ssse3_capable(size_t len)76 OPENSSL_INLINE int ChaCha20_ctr32_ssse3_capable(size_t len) {
77   return len > 128 && CRYPTO_is_SSSE3_capable();
78 }
79 void ChaCha20_ctr32_ssse3(uint8_t *out, const uint8_t *in, size_t in_len,
80                           const uint32_t key[8], const uint32_t counter[4]);
81 #endif
82 
83 #if defined(CHACHA20_ASM_NOHW)
84 // ChaCha20_ctr32_nohw encrypts |in_len| bytes from |in| and writes the result
85 // to |out|. If |in| and |out| alias, they must be equal. |in_len| may not be
86 // zero.
87 //
88 // |counter[0]| is the initial 32-bit block counter, and the remainder is the
89 // 96-bit nonce. If the counter overflows, the output is undefined. The function
90 // will produce output, but the output may vary by machine and may not be
91 // self-consistent. (On some architectures, the assembly implements a mix of
92 // 64-bit and 32-bit counters.)
93 void ChaCha20_ctr32_nohw(uint8_t *out, const uint8_t *in, size_t in_len,
94                          const uint32_t key[8], const uint32_t counter[4]);
95 #endif
96 
97 
98 #if defined(__cplusplus)
99 }  // extern C
100 #endif
101 
102 #endif  // OPENSSL_HEADER_CHACHA_INTERNAL
103