4 * ARC4 Cipher Algorithm
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/arc4.h>
17 #include <crypto/internal/skcipher.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
26 static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
29 struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
35 for (i = 0; i < 256; i++)
38 for (i = 0; i < 256; i++) {
40 j = (j + in_key[k] + a) & 0xff;
41 ctx->S[i] = ctx->S[j];
50 static int arc4_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *in_key,
53 return arc4_set_key(&tfm->base, in_key, key_len);
56 static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in,
59 u32 *const S = ctx->S;
81 *out++ = *in++ ^ S[a];
93 static void arc4_crypt_one(struct crypto_tfm *tfm, u8 *out, const u8 *in)
95 arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1);
98 static int ecb_arc4_crypt(struct skcipher_request *req)
100 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
101 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
102 struct skcipher_walk walk;
105 err = skcipher_walk_virt(&walk, req, false);
107 while (walk.nbytes > 0) {
108 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
110 err = skcipher_walk_done(&walk, 0);
116 static struct crypto_alg arc4_cipher = {
118 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
119 .cra_blocksize = ARC4_BLOCK_SIZE,
120 .cra_ctxsize = sizeof(struct arc4_ctx),
121 .cra_module = THIS_MODULE,
124 .cia_min_keysize = ARC4_MIN_KEY_SIZE,
125 .cia_max_keysize = ARC4_MAX_KEY_SIZE,
126 .cia_setkey = arc4_set_key,
127 .cia_encrypt = arc4_crypt_one,
128 .cia_decrypt = arc4_crypt_one,
133 static struct skcipher_alg arc4_skcipher = {
134 .base.cra_name = "ecb(arc4)",
135 .base.cra_priority = 100,
136 .base.cra_blocksize = ARC4_BLOCK_SIZE,
137 .base.cra_ctxsize = sizeof(struct arc4_ctx),
138 .base.cra_module = THIS_MODULE,
139 .min_keysize = ARC4_MIN_KEY_SIZE,
140 .max_keysize = ARC4_MAX_KEY_SIZE,
141 .setkey = arc4_set_key_skcipher,
142 .encrypt = ecb_arc4_crypt,
143 .decrypt = ecb_arc4_crypt,
146 static int __init arc4_init(void)
150 err = crypto_register_alg(&arc4_cipher);
154 err = crypto_register_skcipher(&arc4_skcipher);
156 crypto_unregister_alg(&arc4_cipher);
160 static void __exit arc4_exit(void)
162 crypto_unregister_alg(&arc4_cipher);
163 crypto_unregister_skcipher(&arc4_skcipher);
166 subsys_initcall(arc4_init);
167 module_exit(arc4_exit);
169 MODULE_LICENSE("GPL");
170 MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
172 MODULE_ALIAS_CRYPTO("arc4");