Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / lib / crypto / arc4.c
blobc2020f19c652ceaffdd46ff7711ca32f688f45f7
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Cryptographic API
5 * ARC4 Cipher Algorithm
7 * Jon Oberheide <jon@oberheide.org>
8 */
10 #include <crypto/arc4.h>
11 #include <linux/module.h>
13 int arc4_setkey(struct arc4_ctx *ctx, const u8 *in_key, unsigned int key_len)
15 int i, j = 0, k = 0;
17 ctx->x = 1;
18 ctx->y = 0;
20 for (i = 0; i < 256; i++)
21 ctx->S[i] = i;
23 for (i = 0; i < 256; i++) {
24 u32 a = ctx->S[i];
26 j = (j + in_key[k] + a) & 0xff;
27 ctx->S[i] = ctx->S[j];
28 ctx->S[j] = a;
29 if (++k >= key_len)
30 k = 0;
33 return 0;
35 EXPORT_SYMBOL(arc4_setkey);
37 void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in, unsigned int len)
39 u32 *const S = ctx->S;
40 u32 x, y, a, b;
41 u32 ty, ta, tb;
43 if (len == 0)
44 return;
46 x = ctx->x;
47 y = ctx->y;
49 a = S[x];
50 y = (y + a) & 0xff;
51 b = S[y];
53 do {
54 S[y] = a;
55 a = (a + b) & 0xff;
56 S[x] = b;
57 x = (x + 1) & 0xff;
58 ta = S[x];
59 ty = (y + ta) & 0xff;
60 tb = S[ty];
61 *out++ = *in++ ^ S[a];
62 if (--len == 0)
63 break;
64 y = ty;
65 a = ta;
66 b = tb;
67 } while (true);
69 ctx->x = x;
70 ctx->y = y;
72 EXPORT_SYMBOL(arc4_crypt);
74 MODULE_LICENSE("GPL");