| 1 |
// Copyright (c) 2018, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_CHACHA_INTERNAL |
| 5 |
#define OPENSSL_HEADER_CHACHA_INTERNAL |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#include "../fipsmodule/cpucap/internal.h" |
| 10 |
#include "../internal.h" |
| 11 |
|
| 12 |
#if defined(__cplusplus) |
| 13 |
extern "C" { |
| 14 |
#endif |
| 15 |
|
| 16 |
|
| 17 |
// CRYPTO_hchacha20 computes the HChaCha20 function, which should only be used |
| 18 |
// as part of XChaCha20. |
| 19 |
void CRYPTO_hchacha20(uint8_t out[32], const uint8_t key[32], |
| 20 |
const uint8_t nonce[16]); |
| 21 |
|
| 22 |
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) |
| 23 |
|
| 24 |
#define CHACHA20_ASM_NOHW |
| 25 |
|
| 26 |
#define CHACHA20_ASM_SSSE3 |
| 27 |
OPENSSL_INLINE int ChaCha20_ctr32_ssse3_capable(size_t len) { |
| 28 |
// Unlike the x86_64 version, the x86 SSSE3 routine runs for all non-zero |
| 29 |
// lengths. |
| 30 |
return len > 0 && CRYPTO_is_SSSE3_capable() && CRYPTO_is_FXSR_capable(); |
| 31 |
} |
| 32 |
void ChaCha20_ctr32_ssse3(uint8_t *out, const uint8_t *in, size_t in_len, |
| 33 |
const uint32_t key[8], const uint32_t counter[4]); |
| 34 |
|
| 35 |
#elif !defined(OPENSSL_NO_ASM) && \ |
| 36 |
(defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) |
| 37 |
|
| 38 |
#define CHACHA20_ASM_NOHW |
| 39 |
|
| 40 |
#define CHACHA20_ASM_NEON |
| 41 |
OPENSSL_INLINE int ChaCha20_ctr32_neon_capable(size_t len) { |
| 42 |
return len >= 192 && CRYPTO_is_NEON_capable(); |
| 43 |
} |
| 44 |
void ChaCha20_ctr32_neon(uint8_t *out, const uint8_t *in, size_t in_len, |
| 45 |
const uint32_t key[8], const uint32_t counter[4]); |
| 46 |
#elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) |
| 47 |
#define CHACHA20_ASM_NOHW |
| 48 |
|
| 49 |
#define CHACHA20_ASM_AVX2 |
| 50 |
OPENSSL_INLINE int ChaCha20_ctr32_avx2_capable(size_t len) { |
| 51 |
return len > 128 && CRYPTO_is_AVX2_capable(); |
| 52 |
} |
| 53 |
void ChaCha20_ctr32_avx2(uint8_t *out, const uint8_t *in, size_t in_len, |
| 54 |
const uint32_t key[8], const uint32_t counter[4]); |
| 55 |
|
| 56 |
#define CHACHA20_ASM_SSSE3_4X |
| 57 |
OPENSSL_INLINE int ChaCha20_ctr32_ssse3_4x_capable(size_t len) { |
| 58 |
int capable = len > 128 && CRYPTO_is_SSSE3_capable(); |
| 59 |
int faster = len > 192 || !CRYPTO_cpu_perf_is_like_silvermont(); |
| 60 |
return capable && faster; |
| 61 |
} |
| 62 |
void ChaCha20_ctr32_ssse3_4x(uint8_t *out, const uint8_t *in, size_t in_len, |
| 63 |
const uint32_t key[8], const uint32_t counter[4]); |
| 64 |
|
| 65 |
#define CHACHA20_ASM_SSSE3 |
| 66 |
OPENSSL_INLINE int ChaCha20_ctr32_ssse3_capable(size_t len) { |
| 67 |
return len > 128 && CRYPTO_is_SSSE3_capable(); |
| 68 |
} |
| 69 |
void ChaCha20_ctr32_ssse3(uint8_t *out, const uint8_t *in, size_t in_len, |
| 70 |
const uint32_t key[8], const uint32_t counter[4]); |
| 71 |
#endif |
| 72 |
|
| 73 |
#if defined(CHACHA20_ASM_NOHW) |
| 74 |
// ChaCha20_ctr32_nohw encrypts |in_len| bytes from |in| and writes the result |
| 75 |
// to |out|. If |in| and |out| alias, they must be equal. |in_len| may not be |
| 76 |
// zero. |
| 77 |
// |
| 78 |
// |counter[0]| is the initial 32-bit block counter, and the remainder is the |
| 79 |
// 96-bit nonce. If the counter overflows, the output is undefined. The function |
| 80 |
// will produce output, but the output may vary by machine and may not be |
| 81 |
// self-consistent. (On some architectures, the assembly implements a mix of |
| 82 |
// 64-bit and 32-bit counters.) |
| 83 |
void ChaCha20_ctr32_nohw(uint8_t *out, const uint8_t *in, size_t in_len, |
| 84 |
const uint32_t key[8], const uint32_t counter[4]); |
| 85 |
#endif |
| 86 |
|
| 87 |
|
| 88 |
#if defined(__cplusplus) |
| 89 |
} // extern C |
| 90 |
#endif |
| 91 |
|
| 92 |
#endif // OPENSSL_HEADER_CHACHA_INTERNAL |