11ca1b917SEric Biggers /* SPDX-License-Identifier: GPL-2.0 */
21ca1b917SEric Biggers /*
31ca1b917SEric Biggers * Common values and helper functions for the ChaCha and XChaCha stream ciphers.
41ca1b917SEric Biggers *
51ca1b917SEric Biggers * XChaCha extends ChaCha's nonce to 192 bits, while provably retaining ChaCha's
61ca1b917SEric Biggers * security. Here they share the same key size, tfm context, and setkey
71ca1b917SEric Biggers * function; only their IV size and encrypt/decrypt function differ.
8aa762409SEric Biggers *
9aa762409SEric Biggers * The ChaCha paper specifies 20, 12, and 8-round variants. In general, it is
10aa762409SEric Biggers * recommended to use the 20-round variant ChaCha20. However, the other
11aa762409SEric Biggers * variants can be needed in some performance-sensitive scenarios. The generic
12aa762409SEric Biggers * ChaCha code currently allows only the 20 and 12-round variants.
131ca1b917SEric Biggers */
141ca1b917SEric Biggers
151ca1b917SEric Biggers #ifndef _CRYPTO_CHACHA_H
161ca1b917SEric Biggers #define _CRYPTO_CHACHA_H
171ca1b917SEric Biggers
18*5f60d5f6SAl Viro #include <linux/unaligned.h>
191ca1b917SEric Biggers #include <linux/types.h>
201ca1b917SEric Biggers
211ca1b917SEric Biggers /* 32-bit stream position, then 96-bit nonce (RFC7539 convention) */
221ca1b917SEric Biggers #define CHACHA_IV_SIZE 16
231ca1b917SEric Biggers
241ca1b917SEric Biggers #define CHACHA_KEY_SIZE 32
251ca1b917SEric Biggers #define CHACHA_BLOCK_SIZE 64
261ca1b917SEric Biggers #define CHACHAPOLY_IV_SIZE 12
271ca1b917SEric Biggers
2884e03fa3SArd Biesheuvel #define CHACHA_STATE_WORDS (CHACHA_BLOCK_SIZE / sizeof(u32))
2984e03fa3SArd Biesheuvel
301ca1b917SEric Biggers /* 192-bit nonce, then 64-bit stream position */
311ca1b917SEric Biggers #define XCHACHA_IV_SIZE 32
321ca1b917SEric Biggers
335fb8ef25SArd Biesheuvel void chacha_block_generic(u32 *state, u8 *stream, int nrounds);
chacha20_block(u32 * state,u8 * stream)341ca1b917SEric Biggers static inline void chacha20_block(u32 *state, u8 *stream)
351ca1b917SEric Biggers {
365fb8ef25SArd Biesheuvel chacha_block_generic(state, stream, 20);
371ca1b917SEric Biggers }
381ca1b917SEric Biggers
395fb8ef25SArd Biesheuvel void hchacha_block_arch(const u32 *state, u32 *out, int nrounds);
405fb8ef25SArd Biesheuvel void hchacha_block_generic(const u32 *state, u32 *out, int nrounds);
411ca1b917SEric Biggers
hchacha_block(const u32 * state,u32 * out,int nrounds)425fb8ef25SArd Biesheuvel static inline void hchacha_block(const u32 *state, u32 *out, int nrounds)
435fb8ef25SArd Biesheuvel {
445fb8ef25SArd Biesheuvel if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
455fb8ef25SArd Biesheuvel hchacha_block_arch(state, out, nrounds);
465fb8ef25SArd Biesheuvel else
475fb8ef25SArd Biesheuvel hchacha_block_generic(state, out, nrounds);
485fb8ef25SArd Biesheuvel }
491ca1b917SEric Biggers
5096562f28SDominik Brodowski enum chacha_constants { /* expand 32-byte k */
5196562f28SDominik Brodowski CHACHA_CONSTANT_EXPA = 0x61707865U,
5296562f28SDominik Brodowski CHACHA_CONSTANT_ND_3 = 0x3320646eU,
5396562f28SDominik Brodowski CHACHA_CONSTANT_2_BY = 0x79622d32U,
5496562f28SDominik Brodowski CHACHA_CONSTANT_TE_K = 0x6b206574U
5596562f28SDominik Brodowski };
5696562f28SDominik Brodowski
chacha_init_consts(u32 * state)57a181e0fdSEric Biggers static inline void chacha_init_consts(u32 *state)
585fb8ef25SArd Biesheuvel {
5996562f28SDominik Brodowski state[0] = CHACHA_CONSTANT_EXPA;
6096562f28SDominik Brodowski state[1] = CHACHA_CONSTANT_ND_3;
6196562f28SDominik Brodowski state[2] = CHACHA_CONSTANT_2_BY;
6296562f28SDominik Brodowski state[3] = CHACHA_CONSTANT_TE_K;
63a181e0fdSEric Biggers }
64a181e0fdSEric Biggers
65a181e0fdSEric Biggers void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv);
chacha_init_generic(u32 * state,const u32 * key,const u8 * iv)66a181e0fdSEric Biggers static inline void chacha_init_generic(u32 *state, const u32 *key, const u8 *iv)
67a181e0fdSEric Biggers {
68a181e0fdSEric Biggers chacha_init_consts(state);
695fb8ef25SArd Biesheuvel state[4] = key[0];
705fb8ef25SArd Biesheuvel state[5] = key[1];
715fb8ef25SArd Biesheuvel state[6] = key[2];
725fb8ef25SArd Biesheuvel state[7] = key[3];
735fb8ef25SArd Biesheuvel state[8] = key[4];
745fb8ef25SArd Biesheuvel state[9] = key[5];
755fb8ef25SArd Biesheuvel state[10] = key[6];
765fb8ef25SArd Biesheuvel state[11] = key[7];
775fb8ef25SArd Biesheuvel state[12] = get_unaligned_le32(iv + 0);
785fb8ef25SArd Biesheuvel state[13] = get_unaligned_le32(iv + 4);
795fb8ef25SArd Biesheuvel state[14] = get_unaligned_le32(iv + 8);
805fb8ef25SArd Biesheuvel state[15] = get_unaligned_le32(iv + 12);
815fb8ef25SArd Biesheuvel }
825fb8ef25SArd Biesheuvel
chacha_init(u32 * state,const u32 * key,const u8 * iv)835fb8ef25SArd Biesheuvel static inline void chacha_init(u32 *state, const u32 *key, const u8 *iv)
845fb8ef25SArd Biesheuvel {
855fb8ef25SArd Biesheuvel if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
865fb8ef25SArd Biesheuvel chacha_init_arch(state, key, iv);
875fb8ef25SArd Biesheuvel else
885fb8ef25SArd Biesheuvel chacha_init_generic(state, key, iv);
895fb8ef25SArd Biesheuvel }
905fb8ef25SArd Biesheuvel
915fb8ef25SArd Biesheuvel void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src,
925fb8ef25SArd Biesheuvel unsigned int bytes, int nrounds);
935fb8ef25SArd Biesheuvel void chacha_crypt_generic(u32 *state, u8 *dst, const u8 *src,
945fb8ef25SArd Biesheuvel unsigned int bytes, int nrounds);
955fb8ef25SArd Biesheuvel
chacha_crypt(u32 * state,u8 * dst,const u8 * src,unsigned int bytes,int nrounds)965fb8ef25SArd Biesheuvel static inline void chacha_crypt(u32 *state, u8 *dst, const u8 *src,
975fb8ef25SArd Biesheuvel unsigned int bytes, int nrounds)
985fb8ef25SArd Biesheuvel {
995fb8ef25SArd Biesheuvel if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
1005fb8ef25SArd Biesheuvel chacha_crypt_arch(state, dst, src, bytes, nrounds);
1015fb8ef25SArd Biesheuvel else
1025fb8ef25SArd Biesheuvel chacha_crypt_generic(state, dst, src, bytes, nrounds);
1035fb8ef25SArd Biesheuvel }
1045fb8ef25SArd Biesheuvel
chacha20_crypt(u32 * state,u8 * dst,const u8 * src,unsigned int bytes)1055fb8ef25SArd Biesheuvel static inline void chacha20_crypt(u32 *state, u8 *dst, const u8 *src,
1065fb8ef25SArd Biesheuvel unsigned int bytes)
1075fb8ef25SArd Biesheuvel {
1085fb8ef25SArd Biesheuvel chacha_crypt(state, dst, src, bytes, 20);
1095fb8ef25SArd Biesheuvel }
1101ca1b917SEric Biggers
1111ca1b917SEric Biggers #endif /* _CRYPTO_CHACHA_H */
112