]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
e192be9d | 2 | /* |
1ca1b917 | 3 | * The "hash function" used as the core of the ChaCha stream cipher (RFC7539) |
e192be9d TT |
4 | * |
5 | * Copyright (C) 2015 Martin Willi | |
e192be9d TT |
6 | */ |
7 | ||
5fb8ef25 | 8 | #include <linux/bug.h> |
e192be9d TT |
9 | #include <linux/kernel.h> |
10 | #include <linux/export.h> | |
11 | #include <linux/bitops.h> | |
5fb8ef25 | 12 | #include <linux/string.h> |
e192be9d | 13 | #include <asm/unaligned.h> |
1ca1b917 | 14 | #include <crypto/chacha.h> |
e192be9d | 15 | |
1ca1b917 | 16 | static void chacha_permute(u32 *x, int nrounds) |
e192be9d | 17 | { |
e192be9d TT |
18 | int i; |
19 | ||
1ca1b917 | 20 | /* whitelist the allowed round counts */ |
aa762409 | 21 | WARN_ON_ONCE(nrounds != 20 && nrounds != 12); |
1ca1b917 EB |
22 | |
23 | for (i = 0; i < nrounds; i += 2) { | |
7660b1fb EB |
24 | x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 16); |
25 | x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 16); | |
26 | x[2] += x[6]; x[14] = rol32(x[14] ^ x[2], 16); | |
27 | x[3] += x[7]; x[15] = rol32(x[15] ^ x[3], 16); | |
e192be9d | 28 | |
7660b1fb EB |
29 | x[8] += x[12]; x[4] = rol32(x[4] ^ x[8], 12); |
30 | x[9] += x[13]; x[5] = rol32(x[5] ^ x[9], 12); | |
31 | x[10] += x[14]; x[6] = rol32(x[6] ^ x[10], 12); | |
32 | x[11] += x[15]; x[7] = rol32(x[7] ^ x[11], 12); | |
e192be9d | 33 | |
7660b1fb EB |
34 | x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 8); |
35 | x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 8); | |
36 | x[2] += x[6]; x[14] = rol32(x[14] ^ x[2], 8); | |
37 | x[3] += x[7]; x[15] = rol32(x[15] ^ x[3], 8); | |
e192be9d | 38 | |
7660b1fb EB |
39 | x[8] += x[12]; x[4] = rol32(x[4] ^ x[8], 7); |
40 | x[9] += x[13]; x[5] = rol32(x[5] ^ x[9], 7); | |
41 | x[10] += x[14]; x[6] = rol32(x[6] ^ x[10], 7); | |
42 | x[11] += x[15]; x[7] = rol32(x[7] ^ x[11], 7); | |
e192be9d | 43 | |
7660b1fb EB |
44 | x[0] += x[5]; x[15] = rol32(x[15] ^ x[0], 16); |
45 | x[1] += x[6]; x[12] = rol32(x[12] ^ x[1], 16); | |
46 | x[2] += x[7]; x[13] = rol32(x[13] ^ x[2], 16); | |
47 | x[3] += x[4]; x[14] = rol32(x[14] ^ x[3], 16); | |
e192be9d | 48 | |
7660b1fb EB |
49 | x[10] += x[15]; x[5] = rol32(x[5] ^ x[10], 12); |
50 | x[11] += x[12]; x[6] = rol32(x[6] ^ x[11], 12); | |
51 | x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 12); | |
52 | x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 12); | |
e192be9d | 53 | |
7660b1fb EB |
54 | x[0] += x[5]; x[15] = rol32(x[15] ^ x[0], 8); |
55 | x[1] += x[6]; x[12] = rol32(x[12] ^ x[1], 8); | |
56 | x[2] += x[7]; x[13] = rol32(x[13] ^ x[2], 8); | |
57 | x[3] += x[4]; x[14] = rol32(x[14] ^ x[3], 8); | |
e192be9d | 58 | |
7660b1fb EB |
59 | x[10] += x[15]; x[5] = rol32(x[5] ^ x[10], 7); |
60 | x[11] += x[12]; x[6] = rol32(x[6] ^ x[11], 7); | |
61 | x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 7); | |
62 | x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 7); | |
e192be9d | 63 | } |
dd333449 EB |
64 | } |
65 | ||
66 | /** | |
1ca1b917 | 67 | * chacha_block - generate one keystream block and increment block counter |
dd333449 EB |
68 | * @state: input state matrix (16 32-bit words) |
69 | * @stream: output keystream block (64 bytes) | |
aa762409 | 70 | * @nrounds: number of rounds (20 or 12; 20 is recommended) |
dd333449 | 71 | * |
1ca1b917 EB |
72 | * This is the ChaCha core, a function from 64-byte strings to 64-byte strings. |
73 | * The caller has already converted the endianness of the input. This function | |
74 | * also handles incrementing the block counter in the input matrix. | |
dd333449 | 75 | */ |
5fb8ef25 | 76 | void chacha_block_generic(u32 *state, u8 *stream, int nrounds) |
dd333449 EB |
77 | { |
78 | u32 x[16]; | |
79 | int i; | |
80 | ||
81 | memcpy(x, state, 64); | |
82 | ||
1ca1b917 | 83 | chacha_permute(x, nrounds); |
e192be9d TT |
84 | |
85 | for (i = 0; i < ARRAY_SIZE(x); i++) | |
a5e9f557 | 86 | put_unaligned_le32(x[i] + state[i], &stream[i * sizeof(u32)]); |
e192be9d TT |
87 | |
88 | state[12]++; | |
89 | } | |
5fb8ef25 | 90 | EXPORT_SYMBOL(chacha_block_generic); |
dd333449 EB |
91 | |
92 | /** | |
5fb8ef25 AB |
93 | * hchacha_block_generic - abbreviated ChaCha core, for XChaCha |
94 | * @state: input state matrix (16 32-bit words) | |
dd333449 | 95 | * @out: output (8 32-bit words) |
aa762409 | 96 | * @nrounds: number of rounds (20 or 12; 20 is recommended) |
dd333449 | 97 | * |
1ca1b917 EB |
98 | * HChaCha is the ChaCha equivalent of HSalsa and is an intermediate step |
99 | * towards XChaCha (see https://cr.yp.to/snuffle/xsalsa-20081128.pdf). HChaCha | |
100 | * skips the final addition of the initial state, and outputs only certain words | |
101 | * of the state. It should not be used for streaming directly. | |
dd333449 | 102 | */ |
5fb8ef25 | 103 | void hchacha_block_generic(const u32 *state, u32 *stream, int nrounds) |
dd333449 EB |
104 | { |
105 | u32 x[16]; | |
106 | ||
5fb8ef25 | 107 | memcpy(x, state, 64); |
dd333449 | 108 | |
1ca1b917 | 109 | chacha_permute(x, nrounds); |
dd333449 | 110 | |
5fb8ef25 AB |
111 | memcpy(&stream[0], &x[0], 16); |
112 | memcpy(&stream[4], &x[12], 16); | |
dd333449 | 113 | } |
5fb8ef25 | 114 | EXPORT_SYMBOL(hchacha_block_generic); |