Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 2 | /* |
Eric Biggers | aa76240 | 2018-11-16 17:26:22 -0800 | [diff] [blame] | 3 | * ChaCha and XChaCha stream ciphers, including ChaCha20 (RFC7539) |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2015 Martin Willi |
| 6 | * Copyright (C) 2018 Google LLC |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <asm/unaligned.h> |
| 10 | #include <crypto/algapi.h> |
Ard Biesheuvel | 5fb8ef2 | 2019-11-08 13:22:08 +0100 | [diff] [blame] | 11 | #include <crypto/internal/chacha.h> |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 12 | #include <crypto/internal/skcipher.h> |
| 13 | #include <linux/module.h> |
| 14 | |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 15 | static int chacha_stream_xor(struct skcipher_request *req, |
Eric Biggers | 860ab2e | 2019-06-02 22:47:14 -0700 | [diff] [blame] | 16 | const struct chacha_ctx *ctx, const u8 *iv) |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 17 | { |
| 18 | struct skcipher_walk walk; |
| 19 | u32 state[16]; |
| 20 | int err; |
| 21 | |
| 22 | err = skcipher_walk_virt(&walk, req, false); |
| 23 | |
Ard Biesheuvel | 22cf705 | 2019-11-08 13:22:18 +0100 | [diff] [blame] | 24 | chacha_init_generic(state, ctx->key, iv); |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 25 | |
| 26 | while (walk.nbytes > 0) { |
| 27 | unsigned int nbytes = walk.nbytes; |
| 28 | |
| 29 | if (nbytes < walk.total) |
Eric Biggers | 7aceaaef | 2019-03-12 22:12:45 -0700 | [diff] [blame] | 30 | nbytes = round_down(nbytes, CHACHA_BLOCK_SIZE); |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 31 | |
Ard Biesheuvel | 5fb8ef2 | 2019-11-08 13:22:08 +0100 | [diff] [blame] | 32 | chacha_crypt_generic(state, walk.dst.virt.addr, |
| 33 | walk.src.virt.addr, nbytes, ctx->nrounds); |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 34 | err = skcipher_walk_done(&walk, walk.nbytes - nbytes); |
| 35 | } |
| 36 | |
| 37 | return err; |
| 38 | } |
| 39 | |
Ard Biesheuvel | 22cf705 | 2019-11-08 13:22:18 +0100 | [diff] [blame] | 40 | static int crypto_chacha_crypt(struct skcipher_request *req) |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 41 | { |
| 42 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 43 | struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 44 | |
| 45 | return chacha_stream_xor(req, ctx, req->iv); |
| 46 | } |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 47 | |
Ard Biesheuvel | 22cf705 | 2019-11-08 13:22:18 +0100 | [diff] [blame] | 48 | static int crypto_xchacha_crypt(struct skcipher_request *req) |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 49 | { |
| 50 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 51 | struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 52 | struct chacha_ctx subctx; |
| 53 | u32 state[16]; |
| 54 | u8 real_iv[16]; |
| 55 | |
| 56 | /* Compute the subkey given the original key and first 128 nonce bits */ |
Ard Biesheuvel | 22cf705 | 2019-11-08 13:22:18 +0100 | [diff] [blame] | 57 | chacha_init_generic(state, ctx->key, req->iv); |
Ard Biesheuvel | 5fb8ef2 | 2019-11-08 13:22:08 +0100 | [diff] [blame] | 58 | hchacha_block_generic(state, subctx.key, ctx->nrounds); |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 59 | subctx.nrounds = ctx->nrounds; |
| 60 | |
| 61 | /* Build the real IV */ |
| 62 | memcpy(&real_iv[0], req->iv + 24, 8); /* stream position */ |
| 63 | memcpy(&real_iv[8], req->iv + 16, 8); /* remaining 64 nonce bits */ |
| 64 | |
| 65 | /* Generate the stream and XOR it with the data */ |
| 66 | return chacha_stream_xor(req, &subctx, real_iv); |
| 67 | } |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 68 | |
| 69 | static struct skcipher_alg algs[] = { |
| 70 | { |
| 71 | .base.cra_name = "chacha20", |
| 72 | .base.cra_driver_name = "chacha20-generic", |
| 73 | .base.cra_priority = 100, |
| 74 | .base.cra_blocksize = 1, |
| 75 | .base.cra_ctxsize = sizeof(struct chacha_ctx), |
| 76 | .base.cra_module = THIS_MODULE, |
| 77 | |
| 78 | .min_keysize = CHACHA_KEY_SIZE, |
| 79 | .max_keysize = CHACHA_KEY_SIZE, |
| 80 | .ivsize = CHACHA_IV_SIZE, |
| 81 | .chunksize = CHACHA_BLOCK_SIZE, |
Eric Biggers | 2043323 | 2019-11-17 23:21:29 -0800 | [diff] [blame] | 82 | .setkey = chacha20_setkey, |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 83 | .encrypt = crypto_chacha_crypt, |
| 84 | .decrypt = crypto_chacha_crypt, |
| 85 | }, { |
| 86 | .base.cra_name = "xchacha20", |
| 87 | .base.cra_driver_name = "xchacha20-generic", |
| 88 | .base.cra_priority = 100, |
| 89 | .base.cra_blocksize = 1, |
| 90 | .base.cra_ctxsize = sizeof(struct chacha_ctx), |
| 91 | .base.cra_module = THIS_MODULE, |
| 92 | |
| 93 | .min_keysize = CHACHA_KEY_SIZE, |
| 94 | .max_keysize = CHACHA_KEY_SIZE, |
| 95 | .ivsize = XCHACHA_IV_SIZE, |
| 96 | .chunksize = CHACHA_BLOCK_SIZE, |
Eric Biggers | 2043323 | 2019-11-17 23:21:29 -0800 | [diff] [blame] | 97 | .setkey = chacha20_setkey, |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 98 | .encrypt = crypto_xchacha_crypt, |
| 99 | .decrypt = crypto_xchacha_crypt, |
Eric Biggers | aa76240 | 2018-11-16 17:26:22 -0800 | [diff] [blame] | 100 | }, { |
| 101 | .base.cra_name = "xchacha12", |
| 102 | .base.cra_driver_name = "xchacha12-generic", |
| 103 | .base.cra_priority = 100, |
| 104 | .base.cra_blocksize = 1, |
| 105 | .base.cra_ctxsize = sizeof(struct chacha_ctx), |
| 106 | .base.cra_module = THIS_MODULE, |
| 107 | |
| 108 | .min_keysize = CHACHA_KEY_SIZE, |
| 109 | .max_keysize = CHACHA_KEY_SIZE, |
| 110 | .ivsize = XCHACHA_IV_SIZE, |
| 111 | .chunksize = CHACHA_BLOCK_SIZE, |
Eric Biggers | 2043323 | 2019-11-17 23:21:29 -0800 | [diff] [blame] | 112 | .setkey = chacha12_setkey, |
Eric Biggers | aa76240 | 2018-11-16 17:26:22 -0800 | [diff] [blame] | 113 | .encrypt = crypto_xchacha_crypt, |
| 114 | .decrypt = crypto_xchacha_crypt, |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 115 | } |
| 116 | }; |
| 117 | |
| 118 | static int __init chacha_generic_mod_init(void) |
| 119 | { |
| 120 | return crypto_register_skciphers(algs, ARRAY_SIZE(algs)); |
| 121 | } |
| 122 | |
| 123 | static void __exit chacha_generic_mod_fini(void) |
| 124 | { |
| 125 | crypto_unregister_skciphers(algs, ARRAY_SIZE(algs)); |
| 126 | } |
| 127 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 128 | subsys_initcall(chacha_generic_mod_init); |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 129 | module_exit(chacha_generic_mod_fini); |
| 130 | |
| 131 | MODULE_LICENSE("GPL"); |
| 132 | MODULE_AUTHOR("Martin Willi <martin@strongswan.org>"); |
| 133 | MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (generic)"); |
| 134 | MODULE_ALIAS_CRYPTO("chacha20"); |
| 135 | MODULE_ALIAS_CRYPTO("chacha20-generic"); |
| 136 | MODULE_ALIAS_CRYPTO("xchacha20"); |
| 137 | MODULE_ALIAS_CRYPTO("xchacha20-generic"); |
Eric Biggers | aa76240 | 2018-11-16 17:26:22 -0800 | [diff] [blame] | 138 | MODULE_ALIAS_CRYPTO("xchacha12"); |
| 139 | MODULE_ALIAS_CRYPTO("xchacha12-generic"); |