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 <linux/module.h>
0011 #include "arc4.h"
0012 
0013 MODULE_LICENSE("GPL");
0014 
0015 int cifs_arc4_setkey(struct arc4_ctx *ctx, const u8 *in_key, unsigned int key_len)
0016 {
0017     int i, j = 0, k = 0;
0018 
0019     ctx->x = 1;
0020     ctx->y = 0;
0021 
0022     for (i = 0; i < 256; i++)
0023         ctx->S[i] = i;
0024 
0025     for (i = 0; i < 256; i++) {
0026         u32 a = ctx->S[i];
0027 
0028         j = (j + in_key[k] + a) & 0xff;
0029         ctx->S[i] = ctx->S[j];
0030         ctx->S[j] = a;
0031         if (++k >= key_len)
0032             k = 0;
0033     }
0034 
0035     return 0;
0036 }
0037 EXPORT_SYMBOL_GPL(cifs_arc4_setkey);
0038 
0039 void cifs_arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in, unsigned int len)
0040 {
0041     u32 *const S = ctx->S;
0042     u32 x, y, a, b;
0043     u32 ty, ta, tb;
0044 
0045     if (len == 0)
0046         return;
0047 
0048     x = ctx->x;
0049     y = ctx->y;
0050 
0051     a = S[x];
0052     y = (y + a) & 0xff;
0053     b = S[y];
0054 
0055     do {
0056         S[y] = a;
0057         a = (a + b) & 0xff;
0058         S[x] = b;
0059         x = (x + 1) & 0xff;
0060         ta = S[x];
0061         ty = (y + ta) & 0xff;
0062         tb = S[ty];
0063         *out++ = *in++ ^ S[a];
0064         if (--len == 0)
0065             break;
0066         y = ty;
0067         a = ta;
0068         b = tb;
0069     } while (true);
0070 
0071     ctx->x = x;
0072     ctx->y = y;
0073 }
0074 EXPORT_SYMBOL_GPL(cifs_arc4_crypt);