1 // Copyright 2018 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef OPENSSL_HEADER_CRYPTO_CHACHA_INTERNAL_H
16 #define OPENSSL_HEADER_CRYPTO_CHACHA_INTERNAL_H
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 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();
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 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 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 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 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_CRYPTO_CHACHA_INTERNAL_H
103