Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mati Vait | cfa2b54 | 2011-06-08 21:26:00 +0800 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Cryptographic API |
| 4 | * |
| 5 | * ARC4 Cipher Algorithm |
| 6 | * |
| 7 | * Jon Oberheide <jon@oberheide.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 9 | |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 10 | #include <crypto/algapi.h> |
Iuliana Prodan | bd30cf5 | 2019-02-08 15:50:08 +0200 | [diff] [blame] | 11 | #include <crypto/arc4.h> |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 12 | #include <crypto/internal/skcipher.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Ard Biesheuvel | 611a23c | 2019-06-12 18:19:57 +0200 | [diff] [blame] | 16 | static int crypto_arc4_setkey(struct crypto_skcipher *tfm, const u8 *in_key, |
| 17 | unsigned int key_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | { |
Ard Biesheuvel | 611a23c | 2019-06-12 18:19:57 +0200 | [diff] [blame] | 19 | struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Ard Biesheuvel | dc51f25 | 2019-06-12 18:19:53 +0200 | [diff] [blame] | 21 | return arc4_setkey(ctx, in_key, key_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Ard Biesheuvel | 611a23c | 2019-06-12 18:19:57 +0200 | [diff] [blame] | 24 | static int crypto_arc4_crypt(struct skcipher_request *req) |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 25 | { |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 26 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 27 | struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 28 | struct skcipher_walk walk; |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 29 | int err; |
| 30 | |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 31 | err = skcipher_walk_virt(&walk, req, false); |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 32 | |
| 33 | while (walk.nbytes > 0) { |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 34 | arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr, |
| 35 | walk.nbytes); |
| 36 | err = skcipher_walk_done(&walk, 0); |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | return err; |
| 40 | } |
| 41 | |
Ard Biesheuvel | 611a23c | 2019-06-12 18:19:57 +0200 | [diff] [blame] | 42 | static struct skcipher_alg arc4_alg = { |
| 43 | /* |
| 44 | * For legacy reasons, this is named "ecb(arc4)", not "arc4". |
| 45 | * Nevertheless it's actually a stream cipher, not a block cipher. |
| 46 | */ |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 47 | .base.cra_name = "ecb(arc4)", |
Eric Biggers | d6ebf52 | 2019-06-02 22:40:57 -0700 | [diff] [blame] | 48 | .base.cra_driver_name = "ecb(arc4)-generic", |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 49 | .base.cra_priority = 100, |
| 50 | .base.cra_blocksize = ARC4_BLOCK_SIZE, |
| 51 | .base.cra_ctxsize = sizeof(struct arc4_ctx), |
| 52 | .base.cra_module = THIS_MODULE, |
| 53 | .min_keysize = ARC4_MIN_KEY_SIZE, |
| 54 | .max_keysize = ARC4_MAX_KEY_SIZE, |
Ard Biesheuvel | 611a23c | 2019-06-12 18:19:57 +0200 | [diff] [blame] | 55 | .setkey = crypto_arc4_setkey, |
| 56 | .encrypt = crypto_arc4_crypt, |
| 57 | .decrypt = crypto_arc4_crypt, |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 58 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | static int __init arc4_init(void) |
| 61 | { |
Ard Biesheuvel | 611a23c | 2019-06-12 18:19:57 +0200 | [diff] [blame] | 62 | return crypto_register_skcipher(&arc4_alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static void __exit arc4_exit(void) |
| 66 | { |
Ard Biesheuvel | 611a23c | 2019-06-12 18:19:57 +0200 | [diff] [blame] | 67 | crypto_unregister_skcipher(&arc4_alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 70 | subsys_initcall(arc4_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | module_exit(arc4_exit); |
| 72 | |
| 73 | MODULE_LICENSE("GPL"); |
| 74 | MODULE_DESCRIPTION("ARC4 Cipher Algorithm"); |
| 75 | MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>"); |
Ard Biesheuvel | 611a23c | 2019-06-12 18:19:57 +0200 | [diff] [blame] | 76 | MODULE_ALIAS_CRYPTO("ecb(arc4)"); |