0001
0002
0003
0004
0005
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
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
0068
0069
0070
0071
0072
0073
0074
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
0094
0095
0096
0097
0098
0099
0100
0101
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);