4 * ARC4 Cipher Algorithm
6 * Jon Oberheide <jon@oberheide.org>
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.
15 #include <crypto/algapi.h>
16 #include <crypto/internal/skcipher.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
20 #define ARC4_MIN_KEY_SIZE 1
21 #define ARC4_MAX_KEY_SIZE 256
22 #define ARC4_BLOCK_SIZE 1
29 static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
32 struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
38 for (i = 0; i < 256; i++)
41 for (i = 0; i < 256; i++) {
43 j = (j + in_key[k] + a) & 0xff;
44 ctx->S[i] = ctx->S[j];
53 static int arc4_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *in_key,
56 return arc4_set_key(&tfm->base, in_key, key_len);
59 static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in,
62 u32 *const S = ctx->S;
84 *out++ = *in++ ^ S[a];
96 static void arc4_crypt_one(struct crypto_tfm *tfm, u8 *out, const u8 *in)
98 arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1);
101 static int ecb_arc4_crypt(struct skcipher_request *req)
103 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
104 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
105 struct skcipher_walk walk;
108 err = skcipher_walk_virt(&walk, req, false);
110 while (walk.nbytes > 0) {
111 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
113 err = skcipher_walk_done(&walk, 0);
119 static struct crypto_alg arc4_cipher = {
121 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
122 .cra_blocksize = ARC4_BLOCK_SIZE,
123 .cra_ctxsize = sizeof(struct arc4_ctx),
124 .cra_module = THIS_MODULE,
127 .cia_min_keysize = ARC4_MIN_KEY_SIZE,
128 .cia_max_keysize = ARC4_MAX_KEY_SIZE,
129 .cia_setkey = arc4_set_key,
130 .cia_encrypt = arc4_crypt_one,
131 .cia_decrypt = arc4_crypt_one,
136 static struct skcipher_alg arc4_skcipher = {
137 .base.cra_name = "ecb(arc4)",
138 .base.cra_priority = 100,
139 .base.cra_blocksize = ARC4_BLOCK_SIZE,
140 .base.cra_ctxsize = sizeof(struct arc4_ctx),
141 .base.cra_module = THIS_MODULE,
142 .min_keysize = ARC4_MIN_KEY_SIZE,
143 .max_keysize = ARC4_MAX_KEY_SIZE,
144 .setkey = arc4_set_key_skcipher,
145 .encrypt = ecb_arc4_crypt,
146 .decrypt = ecb_arc4_crypt,
149 static int __init arc4_init(void)
153 err = crypto_register_alg(&arc4_cipher);
157 err = crypto_register_skcipher(&arc4_skcipher);
159 crypto_unregister_alg(&arc4_cipher);
163 static void __exit arc4_exit(void)
165 crypto_unregister_alg(&arc4_cipher);
166 crypto_unregister_skcipher(&arc4_skcipher);
169 module_init(arc4_init);
170 module_exit(arc4_exit);
172 MODULE_LICENSE("GPL");
173 MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
174 MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
175 MODULE_ALIAS_CRYPTO("arc4");