1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * ARC4 Cipher Algorithm
10 #include <crypto/algapi.h>
11 #include <crypto/arc4.h>
12 #include <crypto/internal/skcipher.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
21 static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
24 struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
30 for (i = 0; i < 256; i++)
33 for (i = 0; i < 256; i++) {
35 j = (j + in_key[k] + a) & 0xff;
36 ctx->S[i] = ctx->S[j];
45 static int arc4_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *in_key,
48 return arc4_set_key(&tfm->base, in_key, key_len);
51 static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in,
54 u32 *const S = ctx->S;
76 *out++ = *in++ ^ S[a];
88 static void arc4_crypt_one(struct crypto_tfm *tfm, u8 *out, const u8 *in)
90 arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1);
93 static int ecb_arc4_crypt(struct skcipher_request *req)
95 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
96 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
97 struct skcipher_walk walk;
100 err = skcipher_walk_virt(&walk, req, false);
102 while (walk.nbytes > 0) {
103 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
105 err = skcipher_walk_done(&walk, 0);
111 static struct crypto_alg arc4_cipher = {
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,
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,
128 static 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,
141 static int __init arc4_init(void)
145 err = crypto_register_alg(&arc4_cipher);
149 err = crypto_register_skcipher(&arc4_skcipher);
151 crypto_unregister_alg(&arc4_cipher);
155 static void __exit arc4_exit(void)
157 crypto_unregister_alg(&arc4_cipher);
158 crypto_unregister_skcipher(&arc4_skcipher);
161 subsys_initcall(arc4_init);
162 module_exit(arc4_exit);
164 MODULE_LICENSE("GPL");
165 MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
167 MODULE_ALIAS_CRYPTO("arc4");