4 * ARC4 Cipher Algorithm
6 * Jon Oberheide <jon@oberheide.org>
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.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/crypto.h>
18 #include <crypto/algapi.h>
20 #define ARC4_MIN_KEY_SIZE 1
21 #define ARC4_MAX_KEY_SIZE 256
22 #define ARC4_BLOCK_SIZE 1
29 static int arc4_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
32 struct arc4_ctx
*ctx
= crypto_tfm_ctx(tfm
);
38 for (i
= 0; i
< 256; i
++)
41 for (i
= 0; i
< 256; i
++) {
43 j
= (j
+ in_key
[k
] + a
) & 0xff;
44 ctx
->S
[i
] = ctx
->S
[j
];
53 static void arc4_crypt(struct arc4_ctx
*ctx
, u8
*out
, const u8
*in
,
56 u32
*const S
= ctx
->S
;
78 *out
++ = *in
++ ^ S
[a
];
90 static void arc4_crypt_one(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
92 arc4_crypt(crypto_tfm_ctx(tfm
), out
, in
, 1);
95 static int ecb_arc4_crypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
96 struct scatterlist
*src
, unsigned int nbytes
)
98 struct arc4_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
99 struct blkcipher_walk walk
;
102 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
104 err
= blkcipher_walk_virt(desc
, &walk
);
106 while (walk
.nbytes
> 0) {
107 u8
*wsrc
= walk
.src
.virt
.addr
;
108 u8
*wdst
= walk
.dst
.virt
.addr
;
110 arc4_crypt(ctx
, wdst
, wsrc
, walk
.nbytes
);
112 err
= blkcipher_walk_done(desc
, &walk
, 0);
118 static struct crypto_alg arc4_algs
[2] = { {
120 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
121 .cra_blocksize
= ARC4_BLOCK_SIZE
,
122 .cra_ctxsize
= sizeof(struct arc4_ctx
),
123 .cra_module
= THIS_MODULE
,
126 .cia_min_keysize
= ARC4_MIN_KEY_SIZE
,
127 .cia_max_keysize
= ARC4_MAX_KEY_SIZE
,
128 .cia_setkey
= arc4_set_key
,
129 .cia_encrypt
= arc4_crypt_one
,
130 .cia_decrypt
= arc4_crypt_one
,
134 .cra_name
= "ecb(arc4)",
136 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
137 .cra_blocksize
= ARC4_BLOCK_SIZE
,
138 .cra_ctxsize
= sizeof(struct arc4_ctx
),
140 .cra_type
= &crypto_blkcipher_type
,
141 .cra_module
= THIS_MODULE
,
144 .min_keysize
= ARC4_MIN_KEY_SIZE
,
145 .max_keysize
= ARC4_MAX_KEY_SIZE
,
146 .setkey
= arc4_set_key
,
147 .encrypt
= ecb_arc4_crypt
,
148 .decrypt
= ecb_arc4_crypt
,
153 static int __init
arc4_init(void)
155 return crypto_register_algs(arc4_algs
, ARRAY_SIZE(arc4_algs
));
158 static void __exit
arc4_exit(void)
160 crypto_unregister_algs(arc4_algs
, ARRAY_SIZE(arc4_algs
));
163 module_init(arc4_init
);
164 module_exit(arc4_exit
);
166 MODULE_LICENSE("GPL");
167 MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
168 MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
169 MODULE_ALIAS_CRYPTO("arc4");