blob: 3254dcc3436889d0b34427add7558bbd90491dbe [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Cryptographic API
4 *
5 * ARC4 Cipher Algorithm
6 *
7 * Jon Oberheide <jon@oberheide.org>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
9
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010#include <crypto/algapi.h>
David Brazdil0f672f62019-12-10 10:32:29 +000011#include <crypto/arc4.h>
12#include <crypto/internal/skcipher.h>
13#include <linux/init.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020014#include <linux/kernel.h>
David Brazdil0f672f62019-12-10 10:32:29 +000015#include <linux/module.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020016#include <linux/sched.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017
David Brazdil0f672f62019-12-10 10:32:29 +000018static int crypto_arc4_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
19 unsigned int key_len)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020{
David Brazdil0f672f62019-12-10 10:32:29 +000021 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022
David Brazdil0f672f62019-12-10 10:32:29 +000023 return arc4_setkey(ctx, in_key, key_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024}
25
David Brazdil0f672f62019-12-10 10:32:29 +000026static int crypto_arc4_crypt(struct skcipher_request *req)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027{
David Brazdil0f672f62019-12-10 10:32:29 +000028 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
29 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
30 struct skcipher_walk walk;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031 int err;
32
David Brazdil0f672f62019-12-10 10:32:29 +000033 err = skcipher_walk_virt(&walk, req, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034
35 while (walk.nbytes > 0) {
David Brazdil0f672f62019-12-10 10:32:29 +000036 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
37 walk.nbytes);
38 err = skcipher_walk_done(&walk, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039 }
40
41 return err;
42}
43
Olivier Deprez157378f2022-04-04 15:47:50 +020044static 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
David Brazdil0f672f62019-12-10 10:32:29 +000052static 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 */
57 .base.cra_name = "ecb(arc4)",
58 .base.cra_driver_name = "ecb(arc4)-generic",
59 .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,
65 .setkey = crypto_arc4_setkey,
66 .encrypt = crypto_arc4_crypt,
67 .decrypt = crypto_arc4_crypt,
Olivier Deprez157378f2022-04-04 15:47:50 +020068 .init = crypto_arc4_init,
David Brazdil0f672f62019-12-10 10:32:29 +000069};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070
71static int __init arc4_init(void)
72{
David Brazdil0f672f62019-12-10 10:32:29 +000073 return crypto_register_skcipher(&arc4_alg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074}
75
76static void __exit arc4_exit(void)
77{
David Brazdil0f672f62019-12-10 10:32:29 +000078 crypto_unregister_skcipher(&arc4_alg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079}
80
David Brazdil0f672f62019-12-10 10:32:29 +000081subsys_initcall(arc4_init);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082module_exit(arc4_exit);
83
84MODULE_LICENSE("GPL");
85MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
86MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
David Brazdil0f672f62019-12-10 10:32:29 +000087MODULE_ALIAS_CRYPTO("ecb(arc4)");