Eric Biggers | b8181f3 | 2018-11-16 17:26:21 -0800 | [diff] [blame^] | 1 | /* 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. |
| 8 | */ |
| 9 | |
| 10 | #ifndef _CRYPTO_CHACHA_H |
| 11 | #define _CRYPTO_CHACHA_H |
| 12 | |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/crypto.h> |
| 15 | |
| 16 | /* 32-bit stream position, then 96-bit nonce (RFC7539 convention) */ |
| 17 | #define CHACHA_IV_SIZE 16 |
| 18 | |
| 19 | #define CHACHA_KEY_SIZE 32 |
| 20 | #define CHACHA_BLOCK_SIZE 64 |
| 21 | |
| 22 | /* 192-bit nonce, then 64-bit stream position */ |
| 23 | #define XCHACHA_IV_SIZE 32 |
| 24 | |
| 25 | struct chacha_ctx { |
| 26 | u32 key[8]; |
| 27 | int nrounds; |
| 28 | }; |
| 29 | |
| 30 | void chacha_block(u32 *state, u8 *stream, int nrounds); |
| 31 | static inline void chacha20_block(u32 *state, u8 *stream) |
| 32 | { |
| 33 | chacha_block(state, stream, 20); |
| 34 | } |
| 35 | void hchacha_block(const u32 *in, u32 *out, int nrounds); |
| 36 | |
| 37 | void crypto_chacha_init(u32 *state, struct chacha_ctx *ctx, u8 *iv); |
| 38 | |
| 39 | int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key, |
| 40 | unsigned int keysize); |
| 41 | |
| 42 | int crypto_chacha_crypt(struct blkcipher_desc *desc, struct scatterlist *dst, |
| 43 | struct scatterlist *src, unsigned int nbytes); |
| 44 | int crypto_xchacha_crypt(struct blkcipher_desc *desc, struct scatterlist *dst, |
| 45 | struct scatterlist *src, unsigned int nbytes); |
| 46 | |
| 47 | #endif /* _CRYPTO_CHACHA_H */ |