blob: a2120e06bf849dd13a66db3762653aa3ae7ec066 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070016struct arc4_ctx {
Jussi Kivilinnad366db62012-06-09 18:25:46 +030017 u32 S[256];
18 u32 x, y;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019};
20
Herbert Xu6c2bb982006-05-16 22:09:29 +100021static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +100022 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Herbert Xu6c2bb982006-05-16 22:09:29 +100024 struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 int i, j = 0, k = 0;
26
27 ctx->x = 1;
28 ctx->y = 0;
29
Mati Vaitcfa2b542011-06-08 21:26:00 +080030 for (i = 0; i < 256; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 ctx->S[i] = i;
32
Mati Vaitcfa2b542011-06-08 21:26:00 +080033 for (i = 0; i < 256; i++) {
Jussi Kivilinnad366db62012-06-09 18:25:46 +030034 u32 a = ctx->S[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 j = (j + in_key[k] + a) & 0xff;
36 ctx->S[i] = ctx->S[j];
37 ctx->S[j] = a;
Mati Vaitcfa2b542011-06-08 21:26:00 +080038 if (++k >= key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 k = 0;
40 }
41
42 return 0;
43}
44
Eric Biggers426bcb52019-01-03 20:16:23 -080045static int arc4_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *in_key,
46 unsigned int key_len)
47{
48 return arc4_set_key(&tfm->base, in_key, key_len);
49}
50
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030051static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in,
52 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Jussi Kivilinnad366db62012-06-09 18:25:46 +030054 u32 *const S = ctx->S;
55 u32 x, y, a, b;
56 u32 ty, ta, tb;
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030057
58 if (len == 0)
59 return;
60
61 x = ctx->x;
62 y = ctx->y;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 a = S[x];
65 y = (y + a) & 0xff;
66 b = S[y];
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030067
68 do {
69 S[y] = a;
70 a = (a + b) & 0xff;
71 S[x] = b;
72 x = (x + 1) & 0xff;
73 ta = S[x];
74 ty = (y + ta) & 0xff;
75 tb = S[ty];
76 *out++ = *in++ ^ S[a];
77 if (--len == 0)
78 break;
79 y = ty;
80 a = ta;
81 b = tb;
82 } while (true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 ctx->x = x;
85 ctx->y = y;
86}
87
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030088static void arc4_crypt_one(struct crypto_tfm *tfm, u8 *out, const u8 *in)
89{
90 arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1);
91}
92
Eric Biggers426bcb52019-01-03 20:16:23 -080093static int ecb_arc4_crypt(struct skcipher_request *req)
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030094{
Eric Biggers426bcb52019-01-03 20:16:23 -080095 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
96 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
97 struct skcipher_walk walk;
Jussi Kivilinnace6dd362012-06-09 18:25:40 +030098 int err;
99
Eric Biggers426bcb52019-01-03 20:16:23 -0800100 err = skcipher_walk_virt(&walk, req, false);
Jussi Kivilinnace6dd362012-06-09 18:25:40 +0300101
102 while (walk.nbytes > 0) {
Eric Biggers426bcb52019-01-03 20:16:23 -0800103 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
104 walk.nbytes);
105 err = skcipher_walk_done(&walk, 0);
Jussi Kivilinnace6dd362012-06-09 18:25:40 +0300106 }
107
108 return err;
109}
110
Eric Biggers426bcb52019-01-03 20:16:23 -0800111static struct crypto_alg arc4_cipher = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .cra_name = "arc4",
113 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
114 .cra_blocksize = ARC4_BLOCK_SIZE,
115 .cra_ctxsize = sizeof(struct arc4_ctx),
116 .cra_module = THIS_MODULE,
Jussi Kivilinnace6dd362012-06-09 18:25:40 +0300117 .cra_u = {
118 .cipher = {
119 .cia_min_keysize = ARC4_MIN_KEY_SIZE,
120 .cia_max_keysize = ARC4_MAX_KEY_SIZE,
121 .cia_setkey = arc4_set_key,
122 .cia_encrypt = arc4_crypt_one,
123 .cia_decrypt = arc4_crypt_one,
124 },
125 },
Eric Biggers426bcb52019-01-03 20:16:23 -0800126};
127
128static struct skcipher_alg arc4_skcipher = {
129 .base.cra_name = "ecb(arc4)",
130 .base.cra_priority = 100,
131 .base.cra_blocksize = ARC4_BLOCK_SIZE,
132 .base.cra_ctxsize = sizeof(struct arc4_ctx),
133 .base.cra_module = THIS_MODULE,
134 .min_keysize = ARC4_MIN_KEY_SIZE,
135 .max_keysize = ARC4_MAX_KEY_SIZE,
136 .setkey = arc4_set_key_skcipher,
137 .encrypt = ecb_arc4_crypt,
138 .decrypt = ecb_arc4_crypt,
139};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141static int __init arc4_init(void)
142{
Eric Biggers426bcb52019-01-03 20:16:23 -0800143 int err;
144
145 err = crypto_register_alg(&arc4_cipher);
146 if (err)
147 return err;
148
149 err = crypto_register_skcipher(&arc4_skcipher);
150 if (err)
151 crypto_unregister_alg(&arc4_cipher);
152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static void __exit arc4_exit(void)
156{
Eric Biggers426bcb52019-01-03 20:16:23 -0800157 crypto_unregister_alg(&arc4_cipher);
158 crypto_unregister_skcipher(&arc4_skcipher);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Eric Biggersc4741b22019-04-11 21:57:42 -0700161subsys_initcall(arc4_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162module_exit(arc4_exit);
163
164MODULE_LICENSE("GPL");
165MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
166MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
Kees Cook5d26a102014-11-20 17:05:53 -0800167MODULE_ALIAS_CRYPTO("arc4");