Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * ChaCha20 256-bit cipher algorithm, RFC7539 |
| 3 | * |
| 4 | * Copyright (C) 2015 Martin Willi |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/export.h> |
| 14 | #include <linux/bitops.h> |
| 15 | #include <linux/cryptohash.h> |
| 16 | #include <asm/unaligned.h> |
| 17 | #include <crypto/chacha20.h> |
| 18 | |
Eric Biggers | 8e2d31a | 2018-09-11 20:05:10 -0700 | [diff] [blame^] | 19 | void chacha20_block(u32 *state, u8 *stream) |
Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 20 | { |
Eric Biggers | 8e2d31a | 2018-09-11 20:05:10 -0700 | [diff] [blame^] | 21 | u32 x[16]; |
Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 22 | int i; |
| 23 | |
| 24 | for (i = 0; i < ARRAY_SIZE(x); i++) |
| 25 | x[i] = state[i]; |
| 26 | |
| 27 | for (i = 0; i < 20; i += 2) { |
Eric Biggers | 7660b1f | 2017-12-31 18:02:45 -0600 | [diff] [blame] | 28 | x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 16); |
| 29 | x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 16); |
| 30 | x[2] += x[6]; x[14] = rol32(x[14] ^ x[2], 16); |
| 31 | x[3] += x[7]; x[15] = rol32(x[15] ^ x[3], 16); |
Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 32 | |
Eric Biggers | 7660b1f | 2017-12-31 18:02:45 -0600 | [diff] [blame] | 33 | x[8] += x[12]; x[4] = rol32(x[4] ^ x[8], 12); |
| 34 | x[9] += x[13]; x[5] = rol32(x[5] ^ x[9], 12); |
| 35 | x[10] += x[14]; x[6] = rol32(x[6] ^ x[10], 12); |
| 36 | x[11] += x[15]; x[7] = rol32(x[7] ^ x[11], 12); |
Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 37 | |
Eric Biggers | 7660b1f | 2017-12-31 18:02:45 -0600 | [diff] [blame] | 38 | x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 8); |
| 39 | x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 8); |
| 40 | x[2] += x[6]; x[14] = rol32(x[14] ^ x[2], 8); |
| 41 | x[3] += x[7]; x[15] = rol32(x[15] ^ x[3], 8); |
Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 42 | |
Eric Biggers | 7660b1f | 2017-12-31 18:02:45 -0600 | [diff] [blame] | 43 | x[8] += x[12]; x[4] = rol32(x[4] ^ x[8], 7); |
| 44 | x[9] += x[13]; x[5] = rol32(x[5] ^ x[9], 7); |
| 45 | x[10] += x[14]; x[6] = rol32(x[6] ^ x[10], 7); |
| 46 | x[11] += x[15]; x[7] = rol32(x[7] ^ x[11], 7); |
Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 47 | |
Eric Biggers | 7660b1f | 2017-12-31 18:02:45 -0600 | [diff] [blame] | 48 | x[0] += x[5]; x[15] = rol32(x[15] ^ x[0], 16); |
| 49 | x[1] += x[6]; x[12] = rol32(x[12] ^ x[1], 16); |
| 50 | x[2] += x[7]; x[13] = rol32(x[13] ^ x[2], 16); |
| 51 | x[3] += x[4]; x[14] = rol32(x[14] ^ x[3], 16); |
Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 52 | |
Eric Biggers | 7660b1f | 2017-12-31 18:02:45 -0600 | [diff] [blame] | 53 | x[10] += x[15]; x[5] = rol32(x[5] ^ x[10], 12); |
| 54 | x[11] += x[12]; x[6] = rol32(x[6] ^ x[11], 12); |
| 55 | x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 12); |
| 56 | x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 12); |
Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 57 | |
Eric Biggers | 7660b1f | 2017-12-31 18:02:45 -0600 | [diff] [blame] | 58 | x[0] += x[5]; x[15] = rol32(x[15] ^ x[0], 8); |
| 59 | x[1] += x[6]; x[12] = rol32(x[12] ^ x[1], 8); |
| 60 | x[2] += x[7]; x[13] = rol32(x[13] ^ x[2], 8); |
| 61 | x[3] += x[4]; x[14] = rol32(x[14] ^ x[3], 8); |
Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 62 | |
Eric Biggers | 7660b1f | 2017-12-31 18:02:45 -0600 | [diff] [blame] | 63 | x[10] += x[15]; x[5] = rol32(x[5] ^ x[10], 7); |
| 64 | x[11] += x[12]; x[6] = rol32(x[6] ^ x[11], 7); |
| 65 | x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 7); |
| 66 | x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 7); |
Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | for (i = 0; i < ARRAY_SIZE(x); i++) |
Eric Biggers | 8e2d31a | 2018-09-11 20:05:10 -0700 | [diff] [blame^] | 70 | put_unaligned_le32(x[i] + state[i], &stream[i * sizeof(u32)]); |
Theodore Ts'o | e192be9 | 2016-06-12 18:13:36 -0400 | [diff] [blame] | 71 | |
| 72 | state[12]++; |
| 73 | } |
| 74 | EXPORT_SYMBOL(chacha20_block); |