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