include/linux/relay.h: fix percpu annotation in struct rchan
[linux] / crypto / arc4.c
1 /*
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  */
14
15 #include <crypto/algapi.h>
16 #include <crypto/internal/skcipher.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19
20 #define ARC4_MIN_KEY_SIZE       1
21 #define ARC4_MAX_KEY_SIZE       256
22 #define ARC4_BLOCK_SIZE         1
23
24 struct arc4_ctx {
25         u32 S[256];
26         u32 x, y;
27 };
28
29 static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
30                         unsigned int key_len)
31 {
32         struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
33         int i, j = 0, k = 0;
34
35         ctx->x = 1;
36         ctx->y = 0;
37
38         for (i = 0; i < 256; i++)
39                 ctx->S[i] = i;
40
41         for (i = 0; i < 256; i++) {
42                 u32 a = ctx->S[i];
43                 j = (j + in_key[k] + a) & 0xff;
44                 ctx->S[i] = ctx->S[j];
45                 ctx->S[j] = a;
46                 if (++k >= key_len)
47                         k = 0;
48         }
49
50         return 0;
51 }
52
53 static 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
59 static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in,
60                        unsigned int len)
61 {
62         u32 *const S = ctx->S;
63         u32 x, y, a, b;
64         u32 ty, ta, tb;
65
66         if (len == 0)
67                 return;
68
69         x = ctx->x;
70         y = ctx->y;
71
72         a = S[x];
73         y = (y + a) & 0xff;
74         b = S[y];
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);
91
92         ctx->x = x;
93         ctx->y = y;
94 }
95
96 static 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
101 static int ecb_arc4_crypt(struct skcipher_request *req)
102 {
103         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
104         struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
105         struct skcipher_walk walk;
106         int err;
107
108         err = skcipher_walk_virt(&walk, req, false);
109
110         while (walk.nbytes > 0) {
111                 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
112                            walk.nbytes);
113                 err = skcipher_walk_done(&walk, 0);
114         }
115
116         return err;
117 }
118
119 static struct crypto_alg arc4_cipher = {
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,
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         },
134 };
135
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,
147 };
148
149 static int __init arc4_init(void)
150 {
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;
161 }
162
163 static void __exit arc4_exit(void)
164 {
165         crypto_unregister_alg(&arc4_cipher);
166         crypto_unregister_skcipher(&arc4_skcipher);
167 }
168
169 module_init(arc4_init);
170 module_exit(arc4_exit);
171
172 MODULE_LICENSE("GPL");
173 MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
174 MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
175 MODULE_ALIAS_CRYPTO("arc4");