Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Cryptographic API
0004  *
0005  * ARC4 Cipher Algorithm
0006  *
0007  * Jon Oberheide <jon@oberheide.org>
0008  */
0009 
0010 #include <crypto/arc4.h>
0011 #include <linux/module.h>
0012 
0013 int arc4_setkey(struct arc4_ctx *ctx, const u8 *in_key, unsigned int key_len)
0014 {
0015     int i, j = 0, k = 0;
0016 
0017     ctx->x = 1;
0018     ctx->y = 0;
0019 
0020     for (i = 0; i < 256; i++)
0021         ctx->S[i] = i;
0022 
0023     for (i = 0; i < 256; i++) {
0024         u32 a = ctx->S[i];
0025 
0026         j = (j + in_key[k] + a) & 0xff;
0027         ctx->S[i] = ctx->S[j];
0028         ctx->S[j] = a;
0029         if (++k >= key_len)
0030             k = 0;
0031     }
0032 
0033     return 0;
0034 }
0035 EXPORT_SYMBOL(arc4_setkey);
0036 
0037 void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in, unsigned int len)
0038 {
0039     u32 *const S = ctx->S;
0040     u32 x, y, a, b;
0041     u32 ty, ta, tb;
0042 
0043     if (len == 0)
0044         return;
0045 
0046     x = ctx->x;
0047     y = ctx->y;
0048 
0049     a = S[x];
0050     y = (y + a) & 0xff;
0051     b = S[y];
0052 
0053     do {
0054         S[y] = a;
0055         a = (a + b) & 0xff;
0056         S[x] = b;
0057         x = (x + 1) & 0xff;
0058         ta = S[x];
0059         ty = (y + ta) & 0xff;
0060         tb = S[ty];
0061         *out++ = *in++ ^ S[a];
0062         if (--len == 0)
0063             break;
0064         y = ty;
0065         a = ta;
0066         b = tb;
0067     } while (true);
0068 
0069     ctx->x = x;
0070     ctx->y = y;
0071 }
0072 EXPORT_SYMBOL(arc4_crypt);
0073 
0074 MODULE_LICENSE("GPL");