Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
arc4.c
Go to the documentation of this file.
1 /*
2  * Cryptographic API
3  *
4  * ARC4 Cipher Algorithm
5  *
6  * Jon Oberheide <[email protected]>
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 #include <linux/module.h>
15 #include <linux/init.h>
16 #include "rtl_crypto.h"
17 
18 #define ARC4_MIN_KEY_SIZE 1
19 #define ARC4_MAX_KEY_SIZE 256
20 #define ARC4_BLOCK_SIZE 1
21 
22 struct arc4_ctx {
23  u8 S[256];
24  u8 x, y;
25 };
26 
27 static int arc4_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
28 {
29  struct arc4_ctx *ctx = ctx_arg;
30  int i, j = 0, k = 0;
31 
32  ctx->x = 1;
33  ctx->y = 0;
34 
35  for(i = 0; i < 256; i++)
36  ctx->S[i] = i;
37 
38  for(i = 0; i < 256; i++)
39  {
40  u8 a = ctx->S[i];
41  j = (j + in_key[k] + a) & 0xff;
42  ctx->S[i] = ctx->S[j];
43  ctx->S[j] = a;
44  if((unsigned int)++k >= key_len)
45  k = 0;
46  }
47 
48  return 0;
49 }
50 
51 static void arc4_crypt(void *ctx_arg, u8 *out, const u8 *in)
52 {
53  struct arc4_ctx *ctx = ctx_arg;
54 
55  u8 *const S = ctx->S;
56  u8 x = ctx->x;
57  u8 y = ctx->y;
58  u8 a, b;
59 
60  a = S[x];
61  y = (y + a) & 0xff;
62  b = S[y];
63  S[x] = b;
64  S[y] = a;
65  x = (x + 1) & 0xff;
66  *out++ = *in ^ S[(a + b) & 0xff];
67 
68  ctx->x = x;
69  ctx->y = y;
70 }
71 
72 static struct crypto_alg arc4_alg = {
73  .cra_name = "arc4",
74  .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
75  .cra_blocksize = ARC4_BLOCK_SIZE,
76  .cra_ctxsize = sizeof(struct arc4_ctx),
77  .cra_module = THIS_MODULE,
78  .cra_list = LIST_HEAD_INIT(arc4_alg.cra_list),
79  .cra_u = { .cipher = {
80  .cia_min_keysize = ARC4_MIN_KEY_SIZE,
81  .cia_max_keysize = ARC4_MAX_KEY_SIZE,
82  .cia_setkey = arc4_set_key,
83  .cia_encrypt = arc4_crypt,
84  .cia_decrypt = arc4_crypt } }
85 };
86 
87 static int __init arc4_init(void)
88 {
89  return crypto_register_alg(&arc4_alg);
90 }
91 
92 
93 static void __exit arc4_exit(void)
94 {
95  crypto_unregister_alg(&arc4_alg);
96 }
97 
98 module_init(arc4_init);
99 module_exit(arc4_exit);
100 
101 MODULE_LICENSE("GPL");
102 MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
103 MODULE_AUTHOR("Jon Oberheide <[email protected]>");