Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Salsa20: Salsa20 stream cipher algorithm |
| 3 | * |
| 4 | * Copyright (c) 2007 Tan Swee Heng <thesweeheng@gmail.com> |
| 5 | * |
| 6 | * Derived from: |
| 7 | * - salsa20.c: Public domain C code by Daniel J. Bernstein <djb@cr.yp.to> |
| 8 | * |
| 9 | * Salsa20 is a stream cipher candidate in eSTREAM, the ECRYPT Stream |
| 10 | * Cipher Project. It is designed by Daniel J. Bernstein <djb@cr.yp.to>. |
| 11 | * More information about eSTREAM and Salsa20 can be found here: |
| 12 | * http://www.ecrypt.eu.org/stream/ |
| 13 | * http://cr.yp.to/snuffle.html |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify it |
| 16 | * under the terms of the GNU General Public License as published by the Free |
| 17 | * Software Foundation; either version 2 of the License, or (at your option) |
| 18 | * any later version. |
| 19 | * |
| 20 | */ |
| 21 | |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 22 | #include <asm/unaligned.h> |
| 23 | #include <crypto/internal/skcipher.h> |
Eric Biggers | eb772f3 | 2018-01-05 11:09:58 -0800 | [diff] [blame] | 24 | #include <crypto/salsa20.h> |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 26 | |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 27 | static void salsa20_block(u32 *state, __le32 *stream) |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 28 | { |
| 29 | u32 x[16]; |
| 30 | int i; |
| 31 | |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 32 | memcpy(x, state, sizeof(x)); |
| 33 | |
| 34 | for (i = 0; i < 20; i += 2) { |
Harvey Harrison | f0d1ec3 | 2008-12-17 16:53:49 +1100 | [diff] [blame] | 35 | x[ 4] ^= rol32((x[ 0] + x[12]), 7); |
| 36 | x[ 8] ^= rol32((x[ 4] + x[ 0]), 9); |
| 37 | x[12] ^= rol32((x[ 8] + x[ 4]), 13); |
| 38 | x[ 0] ^= rol32((x[12] + x[ 8]), 18); |
| 39 | x[ 9] ^= rol32((x[ 5] + x[ 1]), 7); |
| 40 | x[13] ^= rol32((x[ 9] + x[ 5]), 9); |
| 41 | x[ 1] ^= rol32((x[13] + x[ 9]), 13); |
| 42 | x[ 5] ^= rol32((x[ 1] + x[13]), 18); |
| 43 | x[14] ^= rol32((x[10] + x[ 6]), 7); |
| 44 | x[ 2] ^= rol32((x[14] + x[10]), 9); |
| 45 | x[ 6] ^= rol32((x[ 2] + x[14]), 13); |
| 46 | x[10] ^= rol32((x[ 6] + x[ 2]), 18); |
| 47 | x[ 3] ^= rol32((x[15] + x[11]), 7); |
| 48 | x[ 7] ^= rol32((x[ 3] + x[15]), 9); |
| 49 | x[11] ^= rol32((x[ 7] + x[ 3]), 13); |
| 50 | x[15] ^= rol32((x[11] + x[ 7]), 18); |
| 51 | x[ 1] ^= rol32((x[ 0] + x[ 3]), 7); |
| 52 | x[ 2] ^= rol32((x[ 1] + x[ 0]), 9); |
| 53 | x[ 3] ^= rol32((x[ 2] + x[ 1]), 13); |
| 54 | x[ 0] ^= rol32((x[ 3] + x[ 2]), 18); |
| 55 | x[ 6] ^= rol32((x[ 5] + x[ 4]), 7); |
| 56 | x[ 7] ^= rol32((x[ 6] + x[ 5]), 9); |
| 57 | x[ 4] ^= rol32((x[ 7] + x[ 6]), 13); |
| 58 | x[ 5] ^= rol32((x[ 4] + x[ 7]), 18); |
| 59 | x[11] ^= rol32((x[10] + x[ 9]), 7); |
| 60 | x[ 8] ^= rol32((x[11] + x[10]), 9); |
| 61 | x[ 9] ^= rol32((x[ 8] + x[11]), 13); |
| 62 | x[10] ^= rol32((x[ 9] + x[ 8]), 18); |
| 63 | x[12] ^= rol32((x[15] + x[14]), 7); |
| 64 | x[13] ^= rol32((x[12] + x[15]), 9); |
| 65 | x[14] ^= rol32((x[13] + x[12]), 13); |
| 66 | x[15] ^= rol32((x[14] + x[13]), 18); |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 67 | } |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 68 | |
| 69 | for (i = 0; i < 16; i++) |
| 70 | stream[i] = cpu_to_le32(x[i] + state[i]); |
| 71 | |
| 72 | if (++state[8] == 0) |
| 73 | state[9]++; |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 76 | static void salsa20_docrypt(u32 *state, u8 *dst, const u8 *src, |
| 77 | unsigned int bytes) |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 78 | { |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 79 | __le32 stream[SALSA20_BLOCK_SIZE / sizeof(__le32)]; |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 80 | |
| 81 | if (dst != src) |
| 82 | memcpy(dst, src, bytes); |
| 83 | |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 84 | while (bytes >= SALSA20_BLOCK_SIZE) { |
| 85 | salsa20_block(state, stream); |
| 86 | crypto_xor(dst, (const u8 *)stream, SALSA20_BLOCK_SIZE); |
| 87 | bytes -= SALSA20_BLOCK_SIZE; |
| 88 | dst += SALSA20_BLOCK_SIZE; |
| 89 | } |
| 90 | if (bytes) { |
| 91 | salsa20_block(state, stream); |
| 92 | crypto_xor(dst, (const u8 *)stream, bytes); |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Eric Biggers | eb772f3 | 2018-01-05 11:09:58 -0800 | [diff] [blame] | 96 | void crypto_salsa20_init(u32 *state, const struct salsa20_ctx *ctx, |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 97 | const u8 *iv) |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 98 | { |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 99 | memcpy(state, ctx->initial_state, sizeof(ctx->initial_state)); |
| 100 | state[6] = get_unaligned_le32(iv + 0); |
| 101 | state[7] = get_unaligned_le32(iv + 4); |
| 102 | } |
Eric Biggers | eb772f3 | 2018-01-05 11:09:58 -0800 | [diff] [blame] | 103 | EXPORT_SYMBOL_GPL(crypto_salsa20_init); |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 104 | |
Eric Biggers | eb772f3 | 2018-01-05 11:09:58 -0800 | [diff] [blame] | 105 | int crypto_salsa20_setkey(struct crypto_skcipher *tfm, const u8 *key, |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 106 | unsigned int keysize) |
| 107 | { |
| 108 | static const char sigma[16] = "expand 32-byte k"; |
| 109 | static const char tau[16] = "expand 16-byte k"; |
| 110 | struct salsa20_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 111 | const char *constants; |
| 112 | |
| 113 | if (keysize != SALSA20_MIN_KEY_SIZE && |
| 114 | keysize != SALSA20_MAX_KEY_SIZE) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | ctx->initial_state[1] = get_unaligned_le32(key + 0); |
| 118 | ctx->initial_state[2] = get_unaligned_le32(key + 4); |
| 119 | ctx->initial_state[3] = get_unaligned_le32(key + 8); |
| 120 | ctx->initial_state[4] = get_unaligned_le32(key + 12); |
| 121 | if (keysize == 32) { /* recommended */ |
| 122 | key += 16; |
| 123 | constants = sigma; |
| 124 | } else { /* keysize == 16 */ |
| 125 | constants = tau; |
| 126 | } |
| 127 | ctx->initial_state[11] = get_unaligned_le32(key + 0); |
| 128 | ctx->initial_state[12] = get_unaligned_le32(key + 4); |
| 129 | ctx->initial_state[13] = get_unaligned_le32(key + 8); |
| 130 | ctx->initial_state[14] = get_unaligned_le32(key + 12); |
| 131 | ctx->initial_state[0] = get_unaligned_le32(constants + 0); |
| 132 | ctx->initial_state[5] = get_unaligned_le32(constants + 4); |
| 133 | ctx->initial_state[10] = get_unaligned_le32(constants + 8); |
| 134 | ctx->initial_state[15] = get_unaligned_le32(constants + 12); |
| 135 | |
| 136 | /* space for the nonce; it will be overridden for each request */ |
| 137 | ctx->initial_state[6] = 0; |
| 138 | ctx->initial_state[7] = 0; |
| 139 | |
| 140 | /* initial block number */ |
| 141 | ctx->initial_state[8] = 0; |
| 142 | ctx->initial_state[9] = 0; |
| 143 | |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 144 | return 0; |
| 145 | } |
Eric Biggers | eb772f3 | 2018-01-05 11:09:58 -0800 | [diff] [blame] | 146 | EXPORT_SYMBOL_GPL(crypto_salsa20_setkey); |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 147 | |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 148 | static int salsa20_crypt(struct skcipher_request *req) |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 149 | { |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 150 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 151 | const struct salsa20_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 152 | struct skcipher_walk walk; |
| 153 | u32 state[16]; |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 154 | int err; |
| 155 | |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 156 | err = skcipher_walk_virt(&walk, req, true); |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 157 | |
Eric Biggers | eb772f3 | 2018-01-05 11:09:58 -0800 | [diff] [blame] | 158 | crypto_salsa20_init(state, ctx, walk.iv); |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 159 | |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 160 | while (walk.nbytes > 0) { |
| 161 | unsigned int nbytes = walk.nbytes; |
Tan Swee Heng | eb6f13e | 2007-12-07 16:38:45 +0800 | [diff] [blame] | 162 | |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 163 | if (nbytes < walk.total) |
| 164 | nbytes = round_down(nbytes, walk.stride); |
| 165 | |
| 166 | salsa20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr, |
| 167 | nbytes); |
| 168 | err = skcipher_walk_done(&walk, walk.nbytes - nbytes); |
Tan Swee Heng | eb6f13e | 2007-12-07 16:38:45 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 171 | return err; |
| 172 | } |
| 173 | |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 174 | static struct skcipher_alg alg = { |
| 175 | .base.cra_name = "salsa20", |
| 176 | .base.cra_driver_name = "salsa20-generic", |
| 177 | .base.cra_priority = 100, |
| 178 | .base.cra_blocksize = 1, |
| 179 | .base.cra_ctxsize = sizeof(struct salsa20_ctx), |
| 180 | .base.cra_module = THIS_MODULE, |
| 181 | |
| 182 | .min_keysize = SALSA20_MIN_KEY_SIZE, |
| 183 | .max_keysize = SALSA20_MAX_KEY_SIZE, |
| 184 | .ivsize = SALSA20_IV_SIZE, |
| 185 | .chunksize = SALSA20_BLOCK_SIZE, |
Eric Biggers | eb772f3 | 2018-01-05 11:09:58 -0800 | [diff] [blame] | 186 | .setkey = crypto_salsa20_setkey, |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 187 | .encrypt = salsa20_crypt, |
| 188 | .decrypt = salsa20_crypt, |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 189 | }; |
| 190 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 191 | static int __init salsa20_generic_mod_init(void) |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 192 | { |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 193 | return crypto_register_skcipher(&alg); |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 194 | } |
| 195 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 196 | static void __exit salsa20_generic_mod_fini(void) |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 197 | { |
Eric Biggers | b62b3db | 2018-01-05 11:09:57 -0800 | [diff] [blame] | 198 | crypto_unregister_skcipher(&alg); |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 199 | } |
| 200 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 201 | module_init(salsa20_generic_mod_init); |
| 202 | module_exit(salsa20_generic_mod_fini); |
Tan Swee Heng | 2407d60 | 2007-11-23 19:45:00 +0800 | [diff] [blame] | 203 | |
| 204 | MODULE_LICENSE("GPL"); |
| 205 | MODULE_DESCRIPTION ("Salsa20 stream cipher algorithm"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 206 | MODULE_ALIAS_CRYPTO("salsa20"); |
Mathias Krause | 3e14dcf | 2015-01-11 18:17:42 +0100 | [diff] [blame] | 207 | MODULE_ALIAS_CRYPTO("salsa20-generic"); |