^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Common values and helper functions for the ChaCha and XChaCha stream ciphers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * XChaCha extends ChaCha's nonce to 192 bits, while provably retaining ChaCha's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * security. Here they share the same key size, tfm context, and setkey
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * function; only their IV size and encrypt/decrypt function differ.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * The ChaCha paper specifies 20, 12, and 8-round variants. In general, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * recommended to use the 20-round variant ChaCha20. However, the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * variants can be needed in some performance-sensitive scenarios. The generic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * ChaCha code currently allows only the 20 and 12-round variants.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #ifndef _CRYPTO_CHACHA_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define _CRYPTO_CHACHA_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* 32-bit stream position, then 96-bit nonce (RFC7539 convention) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define CHACHA_IV_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define CHACHA_KEY_SIZE 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define CHACHA_BLOCK_SIZE 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define CHACHAPOLY_IV_SIZE 12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define CHACHA_STATE_WORDS (CHACHA_BLOCK_SIZE / sizeof(u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* 192-bit nonce, then 64-bit stream position */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define XCHACHA_IV_SIZE 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) void chacha_block_generic(u32 *state, u8 *stream, int nrounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static inline void chacha20_block(u32 *state, u8 *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) chacha_block_generic(state, stream, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void hchacha_block_arch(const u32 *state, u32 *out, int nrounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void hchacha_block_generic(const u32 *state, u32 *out, int nrounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static inline void hchacha_block(const u32 *state, u32 *out, int nrounds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) hchacha_block_arch(state, out, nrounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) hchacha_block_generic(state, out, nrounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static inline void chacha_init_consts(u32 *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) state[0] = 0x61707865; /* "expa" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) state[1] = 0x3320646e; /* "nd 3" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) state[2] = 0x79622d32; /* "2-by" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) state[3] = 0x6b206574; /* "te k" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static inline void chacha_init_generic(u32 *state, const u32 *key, const u8 *iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) chacha_init_consts(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) state[4] = key[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) state[5] = key[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) state[6] = key[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) state[7] = key[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) state[8] = key[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) state[9] = key[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) state[10] = key[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) state[11] = key[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) state[12] = get_unaligned_le32(iv + 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) state[13] = get_unaligned_le32(iv + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) state[14] = get_unaligned_le32(iv + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) state[15] = get_unaligned_le32(iv + 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static inline void chacha_init(u32 *state, const u32 *key, const u8 *iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) chacha_init_arch(state, key, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) chacha_init_generic(state, key, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) unsigned int bytes, int nrounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) void chacha_crypt_generic(u32 *state, u8 *dst, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) unsigned int bytes, int nrounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static inline void chacha_crypt(u32 *state, u8 *dst, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) unsigned int bytes, int nrounds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) chacha_crypt_arch(state, dst, src, bytes, nrounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) chacha_crypt_generic(state, dst, src, bytes, nrounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static inline void chacha20_crypt(u32 *state, u8 *dst, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unsigned int bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) chacha_crypt(state, dst, src, bytes, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #endif /* _CRYPTO_CHACHA_H */