blob: aa79571dbd4943a052f66729683a760e698c40eb [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>
14#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020016static int crypto_arc4_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
17 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020019 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Ard Biesheuveldc51f252019-06-12 18:19:53 +020021 return arc4_setkey(ctx, in_key, key_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022}
23
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020024static int crypto_arc4_crypt(struct skcipher_request *req)
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030025{
Eric Biggers426bcb52019-01-03 20:16:23 -080026 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
27 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
28 struct skcipher_walk walk;
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030029 int err;
30
Eric Biggers426bcb52019-01-03 20:16:23 -080031 err = skcipher_walk_virt(&walk, req, false);
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030032
33 while (walk.nbytes > 0) {
Eric Biggers426bcb52019-01-03 20:16:23 -080034 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
35 walk.nbytes);
36 err = skcipher_walk_done(&walk, 0);
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030037 }
38
39 return err;
40}
41
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020042static 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 Biggers426bcb52019-01-03 20:16:23 -080047 .base.cra_name = "ecb(arc4)",
Eric Biggersd6ebf522019-06-02 22:40:57 -070048 .base.cra_driver_name = "ecb(arc4)-generic",
Eric Biggers426bcb52019-01-03 20:16:23 -080049 .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 Biesheuvel611a23c2019-06-12 18:19:57 +020055 .setkey = crypto_arc4_setkey,
56 .encrypt = crypto_arc4_crypt,
57 .decrypt = crypto_arc4_crypt,
Eric Biggers426bcb52019-01-03 20:16:23 -080058};
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60static int __init arc4_init(void)
61{
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020062 return crypto_register_skcipher(&arc4_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void __exit arc4_exit(void)
66{
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020067 crypto_unregister_skcipher(&arc4_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Eric Biggersc4741b22019-04-11 21:57:42 -070070subsys_initcall(arc4_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071module_exit(arc4_exit);
72
73MODULE_LICENSE("GPL");
74MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
75MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020076MODULE_ALIAS_CRYPTO("ecb(arc4)");