blob: c7c9826564d31d58dcd8977714bb09b8a020d73e [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Theodore Ts'oe192be92016-06-12 18:13:36 -04002/*
Eric Biggers1ca1b912018-11-16 17:26:21 -08003 * The "hash function" used as the core of the ChaCha stream cipher (RFC7539)
Theodore Ts'oe192be92016-06-12 18:13:36 -04004 *
5 * Copyright (C) 2015 Martin Willi
Theodore Ts'oe192be92016-06-12 18:13:36 -04006 */
7
8#include <linux/kernel.h>
9#include <linux/export.h>
10#include <linux/bitops.h>
11#include <linux/cryptohash.h>
12#include <asm/unaligned.h>
Eric Biggers1ca1b912018-11-16 17:26:21 -080013#include <crypto/chacha.h>
Theodore Ts'oe192be92016-06-12 18:13:36 -040014
Eric Biggers1ca1b912018-11-16 17:26:21 -080015static void chacha_permute(u32 *x, int nrounds)
Theodore Ts'oe192be92016-06-12 18:13:36 -040016{
Theodore Ts'oe192be92016-06-12 18:13:36 -040017 int i;
18
Eric Biggers1ca1b912018-11-16 17:26:21 -080019 /* whitelist the allowed round counts */
Eric Biggersaa762402018-11-16 17:26:22 -080020 WARN_ON_ONCE(nrounds != 20 && nrounds != 12);
Eric Biggers1ca1b912018-11-16 17:26:21 -080021
22 for (i = 0; i < nrounds; i += 2) {
Eric Biggers7660b1f2017-12-31 18:02:45 -060023 x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 16);
24 x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 16);
25 x[2] += x[6]; x[14] = rol32(x[14] ^ x[2], 16);
26 x[3] += x[7]; x[15] = rol32(x[15] ^ x[3], 16);
Theodore Ts'oe192be92016-06-12 18:13:36 -040027
Eric Biggers7660b1f2017-12-31 18:02:45 -060028 x[8] += x[12]; x[4] = rol32(x[4] ^ x[8], 12);
29 x[9] += x[13]; x[5] = rol32(x[5] ^ x[9], 12);
30 x[10] += x[14]; x[6] = rol32(x[6] ^ x[10], 12);
31 x[11] += x[15]; x[7] = rol32(x[7] ^ x[11], 12);
Theodore Ts'oe192be92016-06-12 18:13:36 -040032
Eric Biggers7660b1f2017-12-31 18:02:45 -060033 x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 8);
34 x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 8);
35 x[2] += x[6]; x[14] = rol32(x[14] ^ x[2], 8);
36 x[3] += x[7]; x[15] = rol32(x[15] ^ x[3], 8);
Theodore Ts'oe192be92016-06-12 18:13:36 -040037
Eric Biggers7660b1f2017-12-31 18:02:45 -060038 x[8] += x[12]; x[4] = rol32(x[4] ^ x[8], 7);
39 x[9] += x[13]; x[5] = rol32(x[5] ^ x[9], 7);
40 x[10] += x[14]; x[6] = rol32(x[6] ^ x[10], 7);
41 x[11] += x[15]; x[7] = rol32(x[7] ^ x[11], 7);
Theodore Ts'oe192be92016-06-12 18:13:36 -040042
Eric Biggers7660b1f2017-12-31 18:02:45 -060043 x[0] += x[5]; x[15] = rol32(x[15] ^ x[0], 16);
44 x[1] += x[6]; x[12] = rol32(x[12] ^ x[1], 16);
45 x[2] += x[7]; x[13] = rol32(x[13] ^ x[2], 16);
46 x[3] += x[4]; x[14] = rol32(x[14] ^ x[3], 16);
Theodore Ts'oe192be92016-06-12 18:13:36 -040047
Eric Biggers7660b1f2017-12-31 18:02:45 -060048 x[10] += x[15]; x[5] = rol32(x[5] ^ x[10], 12);
49 x[11] += x[12]; x[6] = rol32(x[6] ^ x[11], 12);
50 x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 12);
51 x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 12);
Theodore Ts'oe192be92016-06-12 18:13:36 -040052
Eric Biggers7660b1f2017-12-31 18:02:45 -060053 x[0] += x[5]; x[15] = rol32(x[15] ^ x[0], 8);
54 x[1] += x[6]; x[12] = rol32(x[12] ^ x[1], 8);
55 x[2] += x[7]; x[13] = rol32(x[13] ^ x[2], 8);
56 x[3] += x[4]; x[14] = rol32(x[14] ^ x[3], 8);
Theodore Ts'oe192be92016-06-12 18:13:36 -040057
Eric Biggers7660b1f2017-12-31 18:02:45 -060058 x[10] += x[15]; x[5] = rol32(x[5] ^ x[10], 7);
59 x[11] += x[12]; x[6] = rol32(x[6] ^ x[11], 7);
60 x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 7);
61 x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 7);
Theodore Ts'oe192be92016-06-12 18:13:36 -040062 }
Eric Biggersdd333442018-11-16 17:26:18 -080063}
64
65/**
Eric Biggers1ca1b912018-11-16 17:26:21 -080066 * chacha_block - generate one keystream block and increment block counter
Eric Biggersdd333442018-11-16 17:26:18 -080067 * @state: input state matrix (16 32-bit words)
68 * @stream: output keystream block (64 bytes)
Eric Biggersaa762402018-11-16 17:26:22 -080069 * @nrounds: number of rounds (20 or 12; 20 is recommended)
Eric Biggersdd333442018-11-16 17:26:18 -080070 *
Eric Biggers1ca1b912018-11-16 17:26:21 -080071 * This is the ChaCha core, a function from 64-byte strings to 64-byte strings.
72 * The caller has already converted the endianness of the input. This function
73 * also handles incrementing the block counter in the input matrix.
Eric Biggersdd333442018-11-16 17:26:18 -080074 */
Eric Biggers1ca1b912018-11-16 17:26:21 -080075void chacha_block(u32 *state, u8 *stream, int nrounds)
Eric Biggersdd333442018-11-16 17:26:18 -080076{
77 u32 x[16];
78 int i;
79
80 memcpy(x, state, 64);
81
Eric Biggers1ca1b912018-11-16 17:26:21 -080082 chacha_permute(x, nrounds);
Theodore Ts'oe192be92016-06-12 18:13:36 -040083
84 for (i = 0; i < ARRAY_SIZE(x); i++)
Eric Biggersa5e9f552018-09-11 20:05:10 -070085 put_unaligned_le32(x[i] + state[i], &stream[i * sizeof(u32)]);
Theodore Ts'oe192be92016-06-12 18:13:36 -040086
87 state[12]++;
88}
Eric Biggers1ca1b912018-11-16 17:26:21 -080089EXPORT_SYMBOL(chacha_block);
Eric Biggersdd333442018-11-16 17:26:18 -080090
91/**
Eric Biggers1ca1b912018-11-16 17:26:21 -080092 * hchacha_block - abbreviated ChaCha core, for XChaCha
Eric Biggersdd333442018-11-16 17:26:18 -080093 * @in: input state matrix (16 32-bit words)
94 * @out: output (8 32-bit words)
Eric Biggersaa762402018-11-16 17:26:22 -080095 * @nrounds: number of rounds (20 or 12; 20 is recommended)
Eric Biggersdd333442018-11-16 17:26:18 -080096 *
Eric Biggers1ca1b912018-11-16 17:26:21 -080097 * HChaCha is the ChaCha equivalent of HSalsa and is an intermediate step
98 * towards XChaCha (see https://cr.yp.to/snuffle/xsalsa-20081128.pdf). HChaCha
99 * skips the final addition of the initial state, and outputs only certain words
100 * of the state. It should not be used for streaming directly.
Eric Biggersdd333442018-11-16 17:26:18 -0800101 */
Eric Biggers1ca1b912018-11-16 17:26:21 -0800102void hchacha_block(const u32 *in, u32 *out, int nrounds)
Eric Biggersdd333442018-11-16 17:26:18 -0800103{
104 u32 x[16];
105
106 memcpy(x, in, 64);
107
Eric Biggers1ca1b912018-11-16 17:26:21 -0800108 chacha_permute(x, nrounds);
Eric Biggersdd333442018-11-16 17:26:18 -0800109
110 memcpy(&out[0], &x[0], 16);
111 memcpy(&out[4], &x[12], 16);
112}
Eric Biggers1ca1b912018-11-16 17:26:21 -0800113EXPORT_SYMBOL(hchacha_block);