blob: dbb1f8b65396e5afec33516c155017f1149b20f7 [file] [log] [blame]
Mati Vaitcfa2b542011-06-08 21:26:00 +08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Cryptographic API
3 *
4 * ARC4 Cipher Algorithm
5 *
6 * Jon Oberheide <jon@oberheide.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 */
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030014
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030015#include <crypto/algapi.h>
Iuliana Prodanbd30cf52019-02-08 15:50:08 +020016#include <crypto/arc4.h>
Eric Biggers426bcb52019-01-03 20:16:23 -080017#include <crypto/internal/skcipher.h>
18#include <linux/init.h>
19#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020021static int crypto_arc4_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
22 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020024 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Ard Biesheuveldc51f252019-06-12 18:19:53 +020026 return arc4_setkey(ctx, in_key, key_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020029static int crypto_arc4_crypt(struct skcipher_request *req)
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030030{
Eric Biggers426bcb52019-01-03 20:16:23 -080031 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
32 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
33 struct skcipher_walk walk;
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030034 int err;
35
Eric Biggers426bcb52019-01-03 20:16:23 -080036 err = skcipher_walk_virt(&walk, req, false);
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030037
38 while (walk.nbytes > 0) {
Eric Biggers426bcb52019-01-03 20:16:23 -080039 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
40 walk.nbytes);
41 err = skcipher_walk_done(&walk, 0);
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030042 }
43
44 return err;
45}
46
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020047static struct skcipher_alg arc4_alg = {
48 /*
49 * For legacy reasons, this is named "ecb(arc4)", not "arc4".
50 * Nevertheless it's actually a stream cipher, not a block cipher.
51 */
Eric Biggers426bcb52019-01-03 20:16:23 -080052 .base.cra_name = "ecb(arc4)",
Eric Biggersd6ebf522019-06-02 22:40:57 -070053 .base.cra_driver_name = "ecb(arc4)-generic",
Eric Biggers426bcb52019-01-03 20:16:23 -080054 .base.cra_priority = 100,
55 .base.cra_blocksize = ARC4_BLOCK_SIZE,
56 .base.cra_ctxsize = sizeof(struct arc4_ctx),
57 .base.cra_module = THIS_MODULE,
58 .min_keysize = ARC4_MIN_KEY_SIZE,
59 .max_keysize = ARC4_MAX_KEY_SIZE,
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020060 .setkey = crypto_arc4_setkey,
61 .encrypt = crypto_arc4_crypt,
62 .decrypt = crypto_arc4_crypt,
Eric Biggers426bcb52019-01-03 20:16:23 -080063};
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static int __init arc4_init(void)
66{
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020067 return crypto_register_skcipher(&arc4_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static void __exit arc4_exit(void)
71{
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020072 crypto_unregister_skcipher(&arc4_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Eric Biggersc4741b22019-04-11 21:57:42 -070075subsys_initcall(arc4_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076module_exit(arc4_exit);
77
78MODULE_LICENSE("GPL");
79MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
80MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
Ard Biesheuvel611a23c2019-06-12 18:19:57 +020081MODULE_ALIAS_CRYPTO("ecb(arc4)");