Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * The "hash function" used as the core of the ChaCha stream cipher (RFC7539)
0004  *
0005  * Copyright (C) 2015 Martin Willi
0006  */
0007 
0008 #include <linux/bug.h>
0009 #include <linux/kernel.h>
0010 #include <linux/export.h>
0011 #include <linux/bitops.h>
0012 #include <linux/string.h>
0013 #include <asm/unaligned.h>
0014 #include <crypto/chacha.h>
0015 
0016 static void chacha_permute(u32 *x, int nrounds)
0017 {
0018     int i;
0019 
0020     /* whitelist the allowed round counts */
0021     WARN_ON_ONCE(nrounds != 20 && nrounds != 12);
0022 
0023     for (i = 0; i < nrounds; i += 2) {
0024         x[0]  += x[4];    x[12] = rol32(x[12] ^ x[0],  16);
0025         x[1]  += x[5];    x[13] = rol32(x[13] ^ x[1],  16);
0026         x[2]  += x[6];    x[14] = rol32(x[14] ^ x[2],  16);
0027         x[3]  += x[7];    x[15] = rol32(x[15] ^ x[3],  16);
0028 
0029         x[8]  += x[12];   x[4]  = rol32(x[4]  ^ x[8],  12);
0030         x[9]  += x[13];   x[5]  = rol32(x[5]  ^ x[9],  12);
0031         x[10] += x[14];   x[6]  = rol32(x[6]  ^ x[10], 12);
0032         x[11] += x[15];   x[7]  = rol32(x[7]  ^ x[11], 12);
0033 
0034         x[0]  += x[4];    x[12] = rol32(x[12] ^ x[0],   8);
0035         x[1]  += x[5];    x[13] = rol32(x[13] ^ x[1],   8);
0036         x[2]  += x[6];    x[14] = rol32(x[14] ^ x[2],   8);
0037         x[3]  += x[7];    x[15] = rol32(x[15] ^ x[3],   8);
0038 
0039         x[8]  += x[12];   x[4]  = rol32(x[4]  ^ x[8],   7);
0040         x[9]  += x[13];   x[5]  = rol32(x[5]  ^ x[9],   7);
0041         x[10] += x[14];   x[6]  = rol32(x[6]  ^ x[10],  7);
0042         x[11] += x[15];   x[7]  = rol32(x[7]  ^ x[11],  7);
0043 
0044         x[0]  += x[5];    x[15] = rol32(x[15] ^ x[0],  16);
0045         x[1]  += x[6];    x[12] = rol32(x[12] ^ x[1],  16);
0046         x[2]  += x[7];    x[13] = rol32(x[13] ^ x[2],  16);
0047         x[3]  += x[4];    x[14] = rol32(x[14] ^ x[3],  16);
0048 
0049         x[10] += x[15];   x[5]  = rol32(x[5]  ^ x[10], 12);
0050         x[11] += x[12];   x[6]  = rol32(x[6]  ^ x[11], 12);
0051         x[8]  += x[13];   x[7]  = rol32(x[7]  ^ x[8],  12);
0052         x[9]  += x[14];   x[4]  = rol32(x[4]  ^ x[9],  12);
0053 
0054         x[0]  += x[5];    x[15] = rol32(x[15] ^ x[0],   8);
0055         x[1]  += x[6];    x[12] = rol32(x[12] ^ x[1],   8);
0056         x[2]  += x[7];    x[13] = rol32(x[13] ^ x[2],   8);
0057         x[3]  += x[4];    x[14] = rol32(x[14] ^ x[3],   8);
0058 
0059         x[10] += x[15];   x[5]  = rol32(x[5]  ^ x[10],  7);
0060         x[11] += x[12];   x[6]  = rol32(x[6]  ^ x[11],  7);
0061         x[8]  += x[13];   x[7]  = rol32(x[7]  ^ x[8],   7);
0062         x[9]  += x[14];   x[4]  = rol32(x[4]  ^ x[9],   7);
0063     }
0064 }
0065 
0066 /**
0067  * chacha_block_generic - generate one keystream block and increment block counter
0068  * @state: input state matrix (16 32-bit words)
0069  * @stream: output keystream block (64 bytes)
0070  * @nrounds: number of rounds (20 or 12; 20 is recommended)
0071  *
0072  * This is the ChaCha core, a function from 64-byte strings to 64-byte strings.
0073  * The caller has already converted the endianness of the input.  This function
0074  * also handles incrementing the block counter in the input matrix.
0075  */
0076 void chacha_block_generic(u32 *state, u8 *stream, int nrounds)
0077 {
0078     u32 x[16];
0079     int i;
0080 
0081     memcpy(x, state, 64);
0082 
0083     chacha_permute(x, nrounds);
0084 
0085     for (i = 0; i < ARRAY_SIZE(x); i++)
0086         put_unaligned_le32(x[i] + state[i], &stream[i * sizeof(u32)]);
0087 
0088     state[12]++;
0089 }
0090 EXPORT_SYMBOL(chacha_block_generic);
0091 
0092 /**
0093  * hchacha_block_generic - abbreviated ChaCha core, for XChaCha
0094  * @state: input state matrix (16 32-bit words)
0095  * @stream: output (8 32-bit words)
0096  * @nrounds: number of rounds (20 or 12; 20 is recommended)
0097  *
0098  * HChaCha is the ChaCha equivalent of HSalsa and is an intermediate step
0099  * towards XChaCha (see https://cr.yp.to/snuffle/xsalsa-20081128.pdf).  HChaCha
0100  * skips the final addition of the initial state, and outputs only certain words
0101  * of the state.  It should not be used for streaming directly.
0102  */
0103 void hchacha_block_generic(const u32 *state, u32 *stream, int nrounds)
0104 {
0105     u32 x[16];
0106 
0107     memcpy(x, state, 64);
0108 
0109     chacha_permute(x, nrounds);
0110 
0111     memcpy(&stream[0], &x[0], 16);
0112     memcpy(&stream[4], &x[12], 16);
0113 }
0114 EXPORT_SYMBOL(hchacha_block_generic);