Coverage Report

Created: 2026-08-08 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/chacha/internal.h
Line
Count
Source
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
23
BSSL_NAMESPACE_BEGIN
24
25
// CRYPTO_hchacha20 computes the HChaCha20 function, which should only be used
26
// as part of XChaCha20.
27
void CRYPTO_hchacha20(uint8_t out[32], const uint8_t key[32],
28
                      const uint8_t nonce[16]);
29
30
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86)
31
32
#define CHACHA20_ASM_NOHW
33
34
#define CHACHA20_ASM_SSSE3
35
inline int ChaCha20_ctr32_ssse3_capable(size_t len) {
36
  // Unlike the x86_64 version, the x86 SSSE3 routine runs for all non-zero
37
  // lengths.
38
  return len > 0 && CRYPTO_is_SSSE3_capable();
39
}
40
extern "C" void ChaCha20_ctr32_ssse3(uint8_t *out, const uint8_t *in,
41
                                     size_t in_len, const uint32_t key[8],
42
                                     const uint32_t counter[4]);
43
44
#elif !defined(OPENSSL_NO_ASM) && \
45
    (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
46
47
#define CHACHA20_ASM_NOHW
48
49
#define CHACHA20_ASM_NEON
50
inline int ChaCha20_ctr32_neon_capable(size_t len) {
51
  return len >= 192 && CRYPTO_is_NEON_capable();
52
}
53
extern "C" void ChaCha20_ctr32_neon(uint8_t *out, const uint8_t *in,
54
                                    size_t in_len, const uint32_t key[8],
55
                                    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
60
409
inline int ChaCha20_ctr32_avx2_capable(size_t len) {
61
409
  return len > 128 && CRYPTO_is_AVX2_capable();
62
409
}
63
extern "C" void ChaCha20_ctr32_avx2(uint8_t *out, const uint8_t *in,
64
                                    size_t in_len, const uint32_t key[8],
65
                                    const uint32_t counter[4]);
66
67
#define CHACHA20_ASM_SSSE3_4X
68
409
inline int ChaCha20_ctr32_ssse3_4x_capable(size_t len) {
69
409
  int capable = len > 128 && CRYPTO_is_SSSE3_capable();
70
409
  int faster = len > 192 || !CRYPTO_cpu_perf_is_like_silvermont();
71
409
  return capable && faster;
72
409
}
73
extern "C" void ChaCha20_ctr32_ssse3_4x(uint8_t *out, const uint8_t *in,
74
                                        size_t in_len, const uint32_t key[8],
75
                                        const uint32_t counter[4]);
76
77
#define CHACHA20_ASM_SSSE3
78
409
inline int ChaCha20_ctr32_ssse3_capable(size_t len) {
79
409
  return len > 128 && CRYPTO_is_SSSE3_capable();
80
409
}
81
extern "C" void ChaCha20_ctr32_ssse3(uint8_t *out, const uint8_t *in,
82
                                     size_t in_len, const uint32_t key[8],
83
                                     const uint32_t counter[4]);
84
#endif
85
86
#if defined(CHACHA20_ASM_NOHW)
87
// ChaCha20_ctr32_nohw encrypts `in_len` bytes from `in` and writes the result
88
// to `out`. If `in` and `out` alias, they must be equal. `in_len` may not be
89
// zero.
90
//
91
// `counter[0]` is the initial 32-bit block counter, and the remainder is the
92
// 96-bit nonce. If the counter overflows, the output is undefined. The function
93
// will produce output, but the output may vary by machine and may not be
94
// self-consistent. (On some architectures, the assembly implements a mix of
95
// 64-bit and 32-bit counters.)
96
extern "C" void ChaCha20_ctr32_nohw(uint8_t *out, const uint8_t *in,
97
                                    size_t in_len, const uint32_t key[8],
98
                                    const uint32_t counter[4]);
99
#endif
100
101
BSSL_NAMESPACE_END
102
103
#endif  // OPENSSL_HEADER_CRYPTO_CHACHA_INTERNAL_H