blob: aa79571dbd4943a052f66729683a760e698c40eb [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>
14#include <linux/module.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015
David Brazdil0f672f62019-12-10 10:32:29 +000016static int crypto_arc4_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
17 unsigned int key_len)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018{
David Brazdil0f672f62019-12-10 10:32:29 +000019 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020
David Brazdil0f672f62019-12-10 10:32:29 +000021 return arc4_setkey(ctx, in_key, key_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022}
23
David Brazdil0f672f62019-12-10 10:32:29 +000024static int crypto_arc4_crypt(struct skcipher_request *req)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025{
David Brazdil0f672f62019-12-10 10:32:29 +000026 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
27 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
28 struct skcipher_walk walk;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029 int err;
30
David Brazdil0f672f62019-12-10 10:32:29 +000031 err = skcipher_walk_virt(&walk, req, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032
33 while (walk.nbytes > 0) {
David Brazdil0f672f62019-12-10 10:32:29 +000034 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
35 walk.nbytes);
36 err = skcipher_walk_done(&walk, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037 }
38
39 return err;
40}
41
David Brazdil0f672f62019-12-10 10:32:29 +000042static 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 */
47 .base.cra_name = "ecb(arc4)",
48 .base.cra_driver_name = "ecb(arc4)-generic",
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,
55 .setkey = crypto_arc4_setkey,
56 .encrypt = crypto_arc4_crypt,
57 .decrypt = crypto_arc4_crypt,
58};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059
60static int __init arc4_init(void)
61{
David Brazdil0f672f62019-12-10 10:32:29 +000062 return crypto_register_skcipher(&arc4_alg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063}
64
65static void __exit arc4_exit(void)
66{
David Brazdil0f672f62019-12-10 10:32:29 +000067 crypto_unregister_skcipher(&arc4_alg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068}
69
David Brazdil0f672f62019-12-10 10:32:29 +000070subsys_initcall(arc4_init);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071module_exit(arc4_exit);
72
73MODULE_LICENSE("GPL");
74MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
75MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
David Brazdil0f672f62019-12-10 10:32:29 +000076MODULE_ALIAS_CRYPTO("ecb(arc4)");