Blame view

lib/chacha20.c 2.49 KB
e192be9d9   Theodore Ts'o   random: replace n...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
   * ChaCha20 256-bit cipher algorithm, RFC7539
   *
   * Copyright (C) 2015 Martin Willi
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   */
  
  #include <linux/kernel.h>
  #include <linux/export.h>
  #include <linux/bitops.h>
  #include <linux/cryptohash.h>
  #include <asm/unaligned.h>
  #include <crypto/chacha20.h>
9f480faec   Eric Biggers   crypto: chacha20 ...
18
  void chacha20_block(u32 *state, u32 *stream)
e192be9d9   Theodore Ts'o   random: replace n...
19
20
21
22
23
24
25
26
  {
  	u32 x[16], *out = stream;
  	int i;
  
  	for (i = 0; i < ARRAY_SIZE(x); i++)
  		x[i] = state[i];
  
  	for (i = 0; i < 20; i += 2) {
7660b1fb3   Eric Biggers   crypto: chacha20 ...
27
28
29
30
  		x[0]  += x[4];    x[12] = rol32(x[12] ^ x[0],  16);
  		x[1]  += x[5];    x[13] = rol32(x[13] ^ x[1],  16);
  		x[2]  += x[6];    x[14] = rol32(x[14] ^ x[2],  16);
  		x[3]  += x[7];    x[15] = rol32(x[15] ^ x[3],  16);
e192be9d9   Theodore Ts'o   random: replace n...
31

7660b1fb3   Eric Biggers   crypto: chacha20 ...
32
33
34
35
  		x[8]  += x[12];   x[4]  = rol32(x[4]  ^ x[8],  12);
  		x[9]  += x[13];   x[5]  = rol32(x[5]  ^ x[9],  12);
  		x[10] += x[14];   x[6]  = rol32(x[6]  ^ x[10], 12);
  		x[11] += x[15];   x[7]  = rol32(x[7]  ^ x[11], 12);
e192be9d9   Theodore Ts'o   random: replace n...
36

7660b1fb3   Eric Biggers   crypto: chacha20 ...
37
38
39
40
  		x[0]  += x[4];    x[12] = rol32(x[12] ^ x[0],   8);
  		x[1]  += x[5];    x[13] = rol32(x[13] ^ x[1],   8);
  		x[2]  += x[6];    x[14] = rol32(x[14] ^ x[2],   8);
  		x[3]  += x[7];    x[15] = rol32(x[15] ^ x[3],   8);
e192be9d9   Theodore Ts'o   random: replace n...
41

7660b1fb3   Eric Biggers   crypto: chacha20 ...
42
43
44
45
  		x[8]  += x[12];   x[4]  = rol32(x[4]  ^ x[8],   7);
  		x[9]  += x[13];   x[5]  = rol32(x[5]  ^ x[9],   7);
  		x[10] += x[14];   x[6]  = rol32(x[6]  ^ x[10],  7);
  		x[11] += x[15];   x[7]  = rol32(x[7]  ^ x[11],  7);
e192be9d9   Theodore Ts'o   random: replace n...
46

7660b1fb3   Eric Biggers   crypto: chacha20 ...
47
48
49
50
  		x[0]  += x[5];    x[15] = rol32(x[15] ^ x[0],  16);
  		x[1]  += x[6];    x[12] = rol32(x[12] ^ x[1],  16);
  		x[2]  += x[7];    x[13] = rol32(x[13] ^ x[2],  16);
  		x[3]  += x[4];    x[14] = rol32(x[14] ^ x[3],  16);
e192be9d9   Theodore Ts'o   random: replace n...
51

7660b1fb3   Eric Biggers   crypto: chacha20 ...
52
53
54
55
  		x[10] += x[15];   x[5]  = rol32(x[5]  ^ x[10], 12);
  		x[11] += x[12];   x[6]  = rol32(x[6]  ^ x[11], 12);
  		x[8]  += x[13];   x[7]  = rol32(x[7]  ^ x[8],  12);
  		x[9]  += x[14];   x[4]  = rol32(x[4]  ^ x[9],  12);
e192be9d9   Theodore Ts'o   random: replace n...
56

7660b1fb3   Eric Biggers   crypto: chacha20 ...
57
58
59
60
  		x[0]  += x[5];    x[15] = rol32(x[15] ^ x[0],   8);
  		x[1]  += x[6];    x[12] = rol32(x[12] ^ x[1],   8);
  		x[2]  += x[7];    x[13] = rol32(x[13] ^ x[2],   8);
  		x[3]  += x[4];    x[14] = rol32(x[14] ^ x[3],   8);
e192be9d9   Theodore Ts'o   random: replace n...
61

7660b1fb3   Eric Biggers   crypto: chacha20 ...
62
63
64
65
  		x[10] += x[15];   x[5]  = rol32(x[5]  ^ x[10],  7);
  		x[11] += x[12];   x[6]  = rol32(x[6]  ^ x[11],  7);
  		x[8]  += x[13];   x[7]  = rol32(x[7]  ^ x[8],   7);
  		x[9]  += x[14];   x[4]  = rol32(x[4]  ^ x[9],   7);
e192be9d9   Theodore Ts'o   random: replace n...
66
67
68
69
70
71
72
73
  	}
  
  	for (i = 0; i < ARRAY_SIZE(x); i++)
  		out[i] = cpu_to_le32(x[i] + state[i]);
  
  	state[12]++;
  }
  EXPORT_SYMBOL(chacha20_block);