| 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 | * 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. |
| 13 | */ |
| 14 | |
| 15 | #ifndef _CRYPTO_CHACHA_H |
| 16 | #define _CRYPTO_CHACHA_H |
| 17 | |
| 18 | #include <linux/unaligned.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/types.h> |
| 21 | |
| 22 | /* 32-bit stream position, then 96-bit nonce (RFC7539 convention) */ |
| 23 | #define CHACHA_IV_SIZE 16 |
| 24 | |
| 25 | #define CHACHA_KEY_SIZE 32 |
| 26 | #define CHACHA_BLOCK_SIZE 64 |
| 27 | #define CHACHAPOLY_IV_SIZE 12 |
| 28 | |
| 29 | #define CHACHA_KEY_WORDS 8 |
| 30 | #define CHACHA_STATE_WORDS 16 |
| 31 | #define HCHACHA_OUT_WORDS 8 |
| 32 | |
| 33 | /* 192-bit nonce, then 64-bit stream position */ |
| 34 | #define XCHACHA_IV_SIZE 32 |
| 35 | |
| 36 | struct chacha_state { |
| 37 | u32 x[CHACHA_STATE_WORDS]; |
| 38 | }; |
| 39 | |
| 40 | void chacha_block_generic(struct chacha_state *state, |
| 41 | u8 out[at_least CHACHA_BLOCK_SIZE], int nrounds); |
| 42 | static inline void chacha20_block(struct chacha_state *state, |
| 43 | u8 out[at_least CHACHA_BLOCK_SIZE]) |
| 44 | { |
| 45 | chacha_block_generic(state, out, nrounds: 20); |
| 46 | } |
| 47 | |
| 48 | void hchacha_block_generic(const struct chacha_state *state, |
| 49 | u32 out[at_least HCHACHA_OUT_WORDS], int nrounds); |
| 50 | |
| 51 | void hchacha_block(const struct chacha_state *state, |
| 52 | u32 out[at_least HCHACHA_OUT_WORDS], int nrounds); |
| 53 | |
| 54 | enum chacha_constants { /* expand 32-byte k */ |
| 55 | CHACHA_CONSTANT_EXPA = 0x61707865U, |
| 56 | CHACHA_CONSTANT_ND_3 = 0x3320646eU, |
| 57 | CHACHA_CONSTANT_2_BY = 0x79622d32U, |
| 58 | CHACHA_CONSTANT_TE_K = 0x6b206574U |
| 59 | }; |
| 60 | |
| 61 | static inline void chacha_init_consts(struct chacha_state *state) |
| 62 | { |
| 63 | state->x[0] = CHACHA_CONSTANT_EXPA; |
| 64 | state->x[1] = CHACHA_CONSTANT_ND_3; |
| 65 | state->x[2] = CHACHA_CONSTANT_2_BY; |
| 66 | state->x[3] = CHACHA_CONSTANT_TE_K; |
| 67 | } |
| 68 | |
| 69 | static inline void chacha_init(struct chacha_state *state, |
| 70 | const u32 key[at_least CHACHA_KEY_WORDS], |
| 71 | const u8 iv[at_least CHACHA_IV_SIZE]) |
| 72 | { |
| 73 | chacha_init_consts(state); |
| 74 | state->x[4] = key[0]; |
| 75 | state->x[5] = key[1]; |
| 76 | state->x[6] = key[2]; |
| 77 | state->x[7] = key[3]; |
| 78 | state->x[8] = key[4]; |
| 79 | state->x[9] = key[5]; |
| 80 | state->x[10] = key[6]; |
| 81 | state->x[11] = key[7]; |
| 82 | state->x[12] = get_unaligned_le32(p: iv + 0); |
| 83 | state->x[13] = get_unaligned_le32(p: iv + 4); |
| 84 | state->x[14] = get_unaligned_le32(p: iv + 8); |
| 85 | state->x[15] = get_unaligned_le32(p: iv + 12); |
| 86 | } |
| 87 | |
| 88 | void chacha_crypt(struct chacha_state *state, u8 *dst, const u8 *src, |
| 89 | unsigned int bytes, int nrounds); |
| 90 | |
| 91 | static inline void chacha20_crypt(struct chacha_state *state, |
| 92 | u8 *dst, const u8 *src, unsigned int bytes) |
| 93 | { |
| 94 | chacha_crypt(state, dst, src, bytes, nrounds: 20); |
| 95 | } |
| 96 | |
| 97 | static inline void chacha_zeroize_state(struct chacha_state *state) |
| 98 | { |
| 99 | memzero_explicit(s: state, count: sizeof(*state)); |
| 100 | } |
| 101 | |
| 102 | #endif /* _CRYPTO_CHACHA_H */ |
| 103 | |