blob: 3254dcc3436889d0b34427add7558bbd90491dbe [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mati Vaitcfa2b542011-06-08 21:26:00 +08002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Cryptographic API
4 *
5 * ARC4 Cipher Algorithm
6 *
7 * Jon Oberheide <jon@oberheide.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Jussi Kivilinnace6dd362012-06-09 18:25:40 +03009
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030010#include <crypto/algapi.h>
Iuliana Prodanbd30cf52019-02-08 15:50:08 +020011#include <crypto/arc4.h>
Eric Biggers426bcb52019-01-03 20:16:23 -080012#include <crypto/internal/skcipher.h>
13#include <linux/init.h>
Ard Biesheuvel9ace6772020-08-31 18:16:49 +030014#include <linux/kernel.h>
Eric Biggers426bcb52019-01-03 20:16:23 -080015#include <linux/module.h>
Ard Biesheuvel9ace6772020-08-31 18:16:49 +030016#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020018static int crypto_arc4_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
19 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020021 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Ard Biesheuveldc51f252019-06-12 18:19:53 +020023 return arc4_setkey(ctx, in_key, key_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024}
25
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020026static int crypto_arc4_crypt(struct skcipher_request *req)
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030027{
Eric Biggers426bcb52019-01-03 20:16:23 -080028 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
29 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
30 struct skcipher_walk walk;
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030031 int err;
32
Eric Biggers426bcb52019-01-03 20:16:23 -080033 err = skcipher_walk_virt(&walk, req, false);
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030034
35 while (walk.nbytes > 0) {
Eric Biggers426bcb52019-01-03 20:16:23 -080036 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
37 walk.nbytes);
38 err = skcipher_walk_done(&walk, 0);
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030039 }
40
41 return err;
42}
43
Ard Biesheuvel9ace6772020-08-31 18:16:49 +030044static int crypto_arc4_init(struct crypto_skcipher *tfm)
45{
46 pr_warn_ratelimited("\"%s\" (%ld) uses obsolete ecb(arc4) skcipher\n",
47 current->comm, (unsigned long)current->pid);
48
49 return 0;
50}
51
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020052static struct skcipher_alg arc4_alg = {
53 /*
54 * For legacy reasons, this is named "ecb(arc4)", not "arc4".
55 * Nevertheless it's actually a stream cipher, not a block cipher.
56 */
Eric Biggers426bcb52019-01-03 20:16:23 -080057 .base.cra_name = "ecb(arc4)",
Eric Biggersd6ebf522019-06-02 22:40:57 -070058 .base.cra_driver_name = "ecb(arc4)-generic",
Eric Biggers426bcb52019-01-03 20:16:23 -080059 .base.cra_priority = 100,
60 .base.cra_blocksize = ARC4_BLOCK_SIZE,
61 .base.cra_ctxsize = sizeof(struct arc4_ctx),
62 .base.cra_module = THIS_MODULE,
63 .min_keysize = ARC4_MIN_KEY_SIZE,
64 .max_keysize = ARC4_MAX_KEY_SIZE,
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020065 .setkey = crypto_arc4_setkey,
66 .encrypt = crypto_arc4_crypt,
67 .decrypt = crypto_arc4_crypt,
Ard Biesheuvel9ace6772020-08-31 18:16:49 +030068 .init = crypto_arc4_init,
Eric Biggers426bcb52019-01-03 20:16:23 -080069};
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71static int __init arc4_init(void)
72{
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020073 return crypto_register_skcipher(&arc4_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void __exit arc4_exit(void)
77{
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020078 crypto_unregister_skcipher(&arc4_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Eric Biggersc4741b22019-04-11 21:57:42 -070081subsys_initcall(arc4_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082module_exit(arc4_exit);
83
84MODULE_LICENSE("GPL");
85MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
86MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020087MODULE_ALIAS_CRYPTO("ecb(arc4)");