blob: d000aa2120b0307fec2530a1a8d763a88cb7df9c [file] [log] [blame]
Eric Biggersb8181f32018-11-16 17:26:21 -08001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Common values and helper functions for the ChaCha and XChaCha stream ciphers.
4 *
5 * XChaCha extends ChaCha's nonce to 192 bits, while provably retaining ChaCha's
6 * security. Here they share the same key size, tfm context, and setkey
7 * function; only their IV size and encrypt/decrypt function differ.
Eric Biggersbb31ed52018-11-16 17:26:22 -08008 *
9 * The ChaCha paper specifies 20, 12, and 8-round variants. In general, it is
10 * recommended to use the 20-round variant ChaCha20. However, the other
11 * variants can be needed in some performance-sensitive scenarios. The generic
12 * ChaCha code currently allows only the 20 and 12-round variants.
Eric Biggersb8181f32018-11-16 17:26:21 -080013 */
14
15#ifndef _CRYPTO_CHACHA_H
16#define _CRYPTO_CHACHA_H
17
18#include <linux/types.h>
19#include <linux/crypto.h>
20
21/* 32-bit stream position, then 96-bit nonce (RFC7539 convention) */
22#define CHACHA_IV_SIZE 16
23
24#define CHACHA_KEY_SIZE 32
25#define CHACHA_BLOCK_SIZE 64
26
27/* 192-bit nonce, then 64-bit stream position */
28#define XCHACHA_IV_SIZE 32
29
30struct chacha_ctx {
31 u32 key[8];
32 int nrounds;
33};
34
35void chacha_block(u32 *state, u8 *stream, int nrounds);
36static inline void chacha20_block(u32 *state, u8 *stream)
37{
38 chacha_block(state, stream, 20);
39}
40void hchacha_block(const u32 *in, u32 *out, int nrounds);
41
42void crypto_chacha_init(u32 *state, struct chacha_ctx *ctx, u8 *iv);
43
44int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
45 unsigned int keysize);
Eric Biggersbb31ed52018-11-16 17:26:22 -080046int crypto_chacha12_setkey(struct crypto_tfm *tfm, const u8 *key,
47 unsigned int keysize);
Eric Biggersb8181f32018-11-16 17:26:21 -080048
49int crypto_chacha_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
50 struct scatterlist *src, unsigned int nbytes);
51int crypto_xchacha_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
52 struct scatterlist *src, unsigned int nbytes);
53
54#endif /* _CRYPTO_CHACHA_H */