xref: /openbmc/linux/lib/crypto/chacha.c (revision b2a4411a)
15fb8ef25SArd Biesheuvel // SPDX-License-Identifier: GPL-2.0-or-later
25fb8ef25SArd Biesheuvel /*
35fb8ef25SArd Biesheuvel  * The "hash function" used as the core of the ChaCha stream cipher (RFC7539)
45fb8ef25SArd Biesheuvel  *
55fb8ef25SArd Biesheuvel  * Copyright (C) 2015 Martin Willi
65fb8ef25SArd Biesheuvel  */
75fb8ef25SArd Biesheuvel 
85fb8ef25SArd Biesheuvel #include <linux/bug.h>
95fb8ef25SArd Biesheuvel #include <linux/kernel.h>
105fb8ef25SArd Biesheuvel #include <linux/export.h>
115fb8ef25SArd Biesheuvel #include <linux/bitops.h>
125fb8ef25SArd Biesheuvel #include <linux/string.h>
135fb8ef25SArd Biesheuvel #include <asm/unaligned.h>
145fb8ef25SArd Biesheuvel #include <crypto/chacha.h>
155fb8ef25SArd Biesheuvel 
chacha_permute(u32 * x,int nrounds)165fb8ef25SArd Biesheuvel static void chacha_permute(u32 *x, int nrounds)
175fb8ef25SArd Biesheuvel {
185fb8ef25SArd Biesheuvel 	int i;
195fb8ef25SArd Biesheuvel 
205fb8ef25SArd Biesheuvel 	/* whitelist the allowed round counts */
215fb8ef25SArd Biesheuvel 	WARN_ON_ONCE(nrounds != 20 && nrounds != 12);
225fb8ef25SArd Biesheuvel 
235fb8ef25SArd Biesheuvel 	for (i = 0; i < nrounds; i += 2) {
245fb8ef25SArd Biesheuvel 		x[0]  += x[4];    x[12] = rol32(x[12] ^ x[0],  16);
255fb8ef25SArd Biesheuvel 		x[1]  += x[5];    x[13] = rol32(x[13] ^ x[1],  16);
265fb8ef25SArd Biesheuvel 		x[2]  += x[6];    x[14] = rol32(x[14] ^ x[2],  16);
275fb8ef25SArd Biesheuvel 		x[3]  += x[7];    x[15] = rol32(x[15] ^ x[3],  16);
285fb8ef25SArd Biesheuvel 
295fb8ef25SArd Biesheuvel 		x[8]  += x[12];   x[4]  = rol32(x[4]  ^ x[8],  12);
305fb8ef25SArd Biesheuvel 		x[9]  += x[13];   x[5]  = rol32(x[5]  ^ x[9],  12);
315fb8ef25SArd Biesheuvel 		x[10] += x[14];   x[6]  = rol32(x[6]  ^ x[10], 12);
325fb8ef25SArd Biesheuvel 		x[11] += x[15];   x[7]  = rol32(x[7]  ^ x[11], 12);
335fb8ef25SArd Biesheuvel 
345fb8ef25SArd Biesheuvel 		x[0]  += x[4];    x[12] = rol32(x[12] ^ x[0],   8);
355fb8ef25SArd Biesheuvel 		x[1]  += x[5];    x[13] = rol32(x[13] ^ x[1],   8);
365fb8ef25SArd Biesheuvel 		x[2]  += x[6];    x[14] = rol32(x[14] ^ x[2],   8);
375fb8ef25SArd Biesheuvel 		x[3]  += x[7];    x[15] = rol32(x[15] ^ x[3],   8);
385fb8ef25SArd Biesheuvel 
395fb8ef25SArd Biesheuvel 		x[8]  += x[12];   x[4]  = rol32(x[4]  ^ x[8],   7);
405fb8ef25SArd Biesheuvel 		x[9]  += x[13];   x[5]  = rol32(x[5]  ^ x[9],   7);
415fb8ef25SArd Biesheuvel 		x[10] += x[14];   x[6]  = rol32(x[6]  ^ x[10],  7);
425fb8ef25SArd Biesheuvel 		x[11] += x[15];   x[7]  = rol32(x[7]  ^ x[11],  7);
435fb8ef25SArd Biesheuvel 
445fb8ef25SArd Biesheuvel 		x[0]  += x[5];    x[15] = rol32(x[15] ^ x[0],  16);
455fb8ef25SArd Biesheuvel 		x[1]  += x[6];    x[12] = rol32(x[12] ^ x[1],  16);
465fb8ef25SArd Biesheuvel 		x[2]  += x[7];    x[13] = rol32(x[13] ^ x[2],  16);
475fb8ef25SArd Biesheuvel 		x[3]  += x[4];    x[14] = rol32(x[14] ^ x[3],  16);
485fb8ef25SArd Biesheuvel 
495fb8ef25SArd Biesheuvel 		x[10] += x[15];   x[5]  = rol32(x[5]  ^ x[10], 12);
505fb8ef25SArd Biesheuvel 		x[11] += x[12];   x[6]  = rol32(x[6]  ^ x[11], 12);
515fb8ef25SArd Biesheuvel 		x[8]  += x[13];   x[7]  = rol32(x[7]  ^ x[8],  12);
525fb8ef25SArd Biesheuvel 		x[9]  += x[14];   x[4]  = rol32(x[4]  ^ x[9],  12);
535fb8ef25SArd Biesheuvel 
545fb8ef25SArd Biesheuvel 		x[0]  += x[5];    x[15] = rol32(x[15] ^ x[0],   8);
555fb8ef25SArd Biesheuvel 		x[1]  += x[6];    x[12] = rol32(x[12] ^ x[1],   8);
565fb8ef25SArd Biesheuvel 		x[2]  += x[7];    x[13] = rol32(x[13] ^ x[2],   8);
575fb8ef25SArd Biesheuvel 		x[3]  += x[4];    x[14] = rol32(x[14] ^ x[3],   8);
585fb8ef25SArd Biesheuvel 
595fb8ef25SArd Biesheuvel 		x[10] += x[15];   x[5]  = rol32(x[5]  ^ x[10],  7);
605fb8ef25SArd Biesheuvel 		x[11] += x[12];   x[6]  = rol32(x[6]  ^ x[11],  7);
615fb8ef25SArd Biesheuvel 		x[8]  += x[13];   x[7]  = rol32(x[7]  ^ x[8],   7);
625fb8ef25SArd Biesheuvel 		x[9]  += x[14];   x[4]  = rol32(x[4]  ^ x[9],   7);
635fb8ef25SArd Biesheuvel 	}
645fb8ef25SArd Biesheuvel }
655fb8ef25SArd Biesheuvel 
665fb8ef25SArd Biesheuvel /**
67*b2a4411aSRandy Dunlap  * chacha_block_generic - generate one keystream block and increment block counter
685fb8ef25SArd Biesheuvel  * @state: input state matrix (16 32-bit words)
695fb8ef25SArd Biesheuvel  * @stream: output keystream block (64 bytes)
705fb8ef25SArd Biesheuvel  * @nrounds: number of rounds (20 or 12; 20 is recommended)
715fb8ef25SArd Biesheuvel  *
725fb8ef25SArd Biesheuvel  * This is the ChaCha core, a function from 64-byte strings to 64-byte strings.
735fb8ef25SArd Biesheuvel  * The caller has already converted the endianness of the input.  This function
745fb8ef25SArd Biesheuvel  * also handles incrementing the block counter in the input matrix.
755fb8ef25SArd Biesheuvel  */
chacha_block_generic(u32 * state,u8 * stream,int nrounds)765fb8ef25SArd Biesheuvel void chacha_block_generic(u32 *state, u8 *stream, int nrounds)
775fb8ef25SArd Biesheuvel {
785fb8ef25SArd Biesheuvel 	u32 x[16];
795fb8ef25SArd Biesheuvel 	int i;
805fb8ef25SArd Biesheuvel 
815fb8ef25SArd Biesheuvel 	memcpy(x, state, 64);
825fb8ef25SArd Biesheuvel 
835fb8ef25SArd Biesheuvel 	chacha_permute(x, nrounds);
845fb8ef25SArd Biesheuvel 
855fb8ef25SArd Biesheuvel 	for (i = 0; i < ARRAY_SIZE(x); i++)
865fb8ef25SArd Biesheuvel 		put_unaligned_le32(x[i] + state[i], &stream[i * sizeof(u32)]);
875fb8ef25SArd Biesheuvel 
885fb8ef25SArd Biesheuvel 	state[12]++;
895fb8ef25SArd Biesheuvel }
905fb8ef25SArd Biesheuvel EXPORT_SYMBOL(chacha_block_generic);
915fb8ef25SArd Biesheuvel 
925fb8ef25SArd Biesheuvel /**
935fb8ef25SArd Biesheuvel  * hchacha_block_generic - abbreviated ChaCha core, for XChaCha
945fb8ef25SArd Biesheuvel  * @state: input state matrix (16 32-bit words)
95*b2a4411aSRandy Dunlap  * @stream: output (8 32-bit words)
965fb8ef25SArd Biesheuvel  * @nrounds: number of rounds (20 or 12; 20 is recommended)
975fb8ef25SArd Biesheuvel  *
985fb8ef25SArd Biesheuvel  * HChaCha is the ChaCha equivalent of HSalsa and is an intermediate step
995fb8ef25SArd Biesheuvel  * towards XChaCha (see https://cr.yp.to/snuffle/xsalsa-20081128.pdf).  HChaCha
1005fb8ef25SArd Biesheuvel  * skips the final addition of the initial state, and outputs only certain words
1015fb8ef25SArd Biesheuvel  * of the state.  It should not be used for streaming directly.
1025fb8ef25SArd Biesheuvel  */
hchacha_block_generic(const u32 * state,u32 * stream,int nrounds)1035fb8ef25SArd Biesheuvel void hchacha_block_generic(const u32 *state, u32 *stream, int nrounds)
1045fb8ef25SArd Biesheuvel {
1055fb8ef25SArd Biesheuvel 	u32 x[16];
1065fb8ef25SArd Biesheuvel 
1075fb8ef25SArd Biesheuvel 	memcpy(x, state, 64);
1085fb8ef25SArd Biesheuvel 
1095fb8ef25SArd Biesheuvel 	chacha_permute(x, nrounds);
1105fb8ef25SArd Biesheuvel 
1115fb8ef25SArd Biesheuvel 	memcpy(&stream[0], &x[0], 16);
1125fb8ef25SArd Biesheuvel 	memcpy(&stream[4], &x[12], 16);
1135fb8ef25SArd Biesheuvel }
1145fb8ef25SArd Biesheuvel EXPORT_SYMBOL(hchacha_block_generic);
115