crypto: arc4 - convert to skcipher API
[linux-2.6-block.git] / crypto / arc4.c
CommitLineData
cfa2b54e 1/*
1da177e4
LT
2 * 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 */
ce6dd368 14
ce6dd368 15#include <crypto/algapi.h>
426bcb50
EB
16#include <crypto/internal/skcipher.h>
17#include <linux/init.h>
18#include <linux/module.h>
1da177e4
LT
19
20#define ARC4_MIN_KEY_SIZE 1
21#define ARC4_MAX_KEY_SIZE 256
22#define ARC4_BLOCK_SIZE 1
23
24struct arc4_ctx {
d366db60
JK
25 u32 S[256];
26 u32 x, y;
1da177e4
LT
27};
28
6c2bb98b 29static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
560c06ae 30 unsigned int key_len)
1da177e4 31{
6c2bb98b 32 struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4
LT
33 int i, j = 0, k = 0;
34
35 ctx->x = 1;
36 ctx->y = 0;
37
cfa2b54e 38 for (i = 0; i < 256; i++)
1da177e4
LT
39 ctx->S[i] = i;
40
cfa2b54e 41 for (i = 0; i < 256; i++) {
d366db60 42 u32 a = ctx->S[i];
1da177e4
LT
43 j = (j + in_key[k] + a) & 0xff;
44 ctx->S[i] = ctx->S[j];
45 ctx->S[j] = a;
cfa2b54e 46 if (++k >= key_len)
1da177e4
LT
47 k = 0;
48 }
49
50 return 0;
51}
52
426bcb50
EB
53static int arc4_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *in_key,
54 unsigned int key_len)
55{
56 return arc4_set_key(&tfm->base, in_key, key_len);
57}
58
ce6dd368
JK
59static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in,
60 unsigned int len)
1da177e4 61{
d366db60
JK
62 u32 *const S = ctx->S;
63 u32 x, y, a, b;
64 u32 ty, ta, tb;
ce6dd368
JK
65
66 if (len == 0)
67 return;
68
69 x = ctx->x;
70 y = ctx->y;
1da177e4
LT
71
72 a = S[x];
73 y = (y + a) & 0xff;
74 b = S[y];
ce6dd368
JK
75
76 do {
77 S[y] = a;
78 a = (a + b) & 0xff;
79 S[x] = b;
80 x = (x + 1) & 0xff;
81 ta = S[x];
82 ty = (y + ta) & 0xff;
83 tb = S[ty];
84 *out++ = *in++ ^ S[a];
85 if (--len == 0)
86 break;
87 y = ty;
88 a = ta;
89 b = tb;
90 } while (true);
1da177e4
LT
91
92 ctx->x = x;
93 ctx->y = y;
94}
95
ce6dd368
JK
96static void arc4_crypt_one(struct crypto_tfm *tfm, u8 *out, const u8 *in)
97{
98 arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1);
99}
100
426bcb50 101static int ecb_arc4_crypt(struct skcipher_request *req)
ce6dd368 102{
426bcb50
EB
103 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
104 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
105 struct skcipher_walk walk;
ce6dd368
JK
106 int err;
107
426bcb50 108 err = skcipher_walk_virt(&walk, req, false);
ce6dd368
JK
109
110 while (walk.nbytes > 0) {
426bcb50
EB
111 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
112 walk.nbytes);
113 err = skcipher_walk_done(&walk, 0);
ce6dd368
JK
114 }
115
116 return err;
117}
118
426bcb50 119static struct crypto_alg arc4_cipher = {
1da177e4
LT
120 .cra_name = "arc4",
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,
ce6dd368
JK
125 .cra_u = {
126 .cipher = {
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,
132 },
133 },
426bcb50
EB
134};
135
136static 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,
147};
1da177e4
LT
148
149static int __init arc4_init(void)
150{
426bcb50
EB
151 int err;
152
153 err = crypto_register_alg(&arc4_cipher);
154 if (err)
155 return err;
156
157 err = crypto_register_skcipher(&arc4_skcipher);
158 if (err)
159 crypto_unregister_alg(&arc4_cipher);
160 return err;
1da177e4
LT
161}
162
1da177e4
LT
163static void __exit arc4_exit(void)
164{
426bcb50
EB
165 crypto_unregister_alg(&arc4_cipher);
166 crypto_unregister_skcipher(&arc4_skcipher);
1da177e4
LT
167}
168
169module_init(arc4_init);
170module_exit(arc4_exit);
171
172MODULE_LICENSE("GPL");
173MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
174MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
5d26a105 175MODULE_ALIAS_CRYPTO("arc4");