Blame view
crypto/arc4.c
3.35 KB
cfa2b54ec crypto: arc4 - Fi... |
1 |
/* |
1da177e4c Linux-2.6.12-rc2 |
2 3 4 5 6 7 8 9 10 11 12 13 |
* Cryptographic API * * ARC4 Cipher Algorithm * * Jon Oberheide <jon@oberheide.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * */ |
ce6dd3689 crypto: arc4 - im... |
14 |
|
1da177e4c Linux-2.6.12-rc2 |
15 16 17 |
#include <linux/module.h> #include <linux/init.h> #include <linux/crypto.h> |
ce6dd3689 crypto: arc4 - im... |
18 |
#include <crypto/algapi.h> |
1da177e4c Linux-2.6.12-rc2 |
19 20 21 22 23 24 |
#define ARC4_MIN_KEY_SIZE 1 #define ARC4_MAX_KEY_SIZE 256 #define ARC4_BLOCK_SIZE 1 struct arc4_ctx { |
d366db605 crypto: arc4 - im... |
25 26 |
u32 S[256]; u32 x, y; |
1da177e4c Linux-2.6.12-rc2 |
27 |
}; |
6c2bb98bc [CRYPTO] all: Pas... |
28 |
static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
560c06ae1 [CRYPTO] api: Get... |
29 |
unsigned int key_len) |
1da177e4c Linux-2.6.12-rc2 |
30 |
{ |
6c2bb98bc [CRYPTO] all: Pas... |
31 |
struct arc4_ctx *ctx = crypto_tfm_ctx(tfm); |
1da177e4c Linux-2.6.12-rc2 |
32 33 34 35 |
int i, j = 0, k = 0; ctx->x = 1; ctx->y = 0; |
cfa2b54ec crypto: arc4 - Fi... |
36 |
for (i = 0; i < 256; i++) |
1da177e4c Linux-2.6.12-rc2 |
37 |
ctx->S[i] = i; |
cfa2b54ec crypto: arc4 - Fi... |
38 |
for (i = 0; i < 256; i++) { |
d366db605 crypto: arc4 - im... |
39 |
u32 a = ctx->S[i]; |
1da177e4c Linux-2.6.12-rc2 |
40 41 42 |
j = (j + in_key[k] + a) & 0xff; ctx->S[i] = ctx->S[j]; ctx->S[j] = a; |
cfa2b54ec crypto: arc4 - Fi... |
43 |
if (++k >= key_len) |
1da177e4c Linux-2.6.12-rc2 |
44 45 46 47 48 |
k = 0; } return 0; } |
ce6dd3689 crypto: arc4 - im... |
49 50 |
static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in, unsigned int len) |
1da177e4c Linux-2.6.12-rc2 |
51 |
{ |
d366db605 crypto: arc4 - im... |
52 53 54 |
u32 *const S = ctx->S; u32 x, y, a, b; u32 ty, ta, tb; |
ce6dd3689 crypto: arc4 - im... |
55 56 57 58 59 60 |
if (len == 0) return; x = ctx->x; y = ctx->y; |
1da177e4c Linux-2.6.12-rc2 |
61 62 63 64 |
a = S[x]; y = (y + a) & 0xff; b = S[y]; |
ce6dd3689 crypto: arc4 - im... |
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
do { S[y] = a; a = (a + b) & 0xff; S[x] = b; x = (x + 1) & 0xff; ta = S[x]; ty = (y + ta) & 0xff; tb = S[ty]; *out++ = *in++ ^ S[a]; if (--len == 0) break; y = ty; a = ta; b = tb; } while (true); |
1da177e4c Linux-2.6.12-rc2 |
81 82 83 84 |
ctx->x = x; ctx->y = y; } |
ce6dd3689 crypto: arc4 - im... |
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
static void arc4_crypt_one(struct crypto_tfm *tfm, u8 *out, const u8 *in) { arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1); } static int ecb_arc4_crypt(struct blkcipher_desc *desc, struct scatterlist *dst, struct scatterlist *src, unsigned int nbytes) { struct arc4_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); struct blkcipher_walk walk; int err; blkcipher_walk_init(&walk, dst, src, nbytes); err = blkcipher_walk_virt(desc, &walk); while (walk.nbytes > 0) { u8 *wsrc = walk.src.virt.addr; u8 *wdst = walk.dst.virt.addr; arc4_crypt(ctx, wdst, wsrc, walk.nbytes); err = blkcipher_walk_done(desc, &walk, 0); } return err; } static struct crypto_alg arc4_algs[2] = { { |
1da177e4c Linux-2.6.12-rc2 |
114 115 116 117 118 |
.cra_name = "arc4", .cra_flags = CRYPTO_ALG_TYPE_CIPHER, .cra_blocksize = ARC4_BLOCK_SIZE, .cra_ctxsize = sizeof(struct arc4_ctx), .cra_module = THIS_MODULE, |
ce6dd3689 crypto: arc4 - im... |
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
.cra_u = { .cipher = { .cia_min_keysize = ARC4_MIN_KEY_SIZE, .cia_max_keysize = ARC4_MAX_KEY_SIZE, .cia_setkey = arc4_set_key, .cia_encrypt = arc4_crypt_one, .cia_decrypt = arc4_crypt_one, }, }, }, { .cra_name = "ecb(arc4)", .cra_priority = 100, .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, .cra_blocksize = ARC4_BLOCK_SIZE, .cra_ctxsize = sizeof(struct arc4_ctx), .cra_alignmask = 0, .cra_type = &crypto_blkcipher_type, .cra_module = THIS_MODULE, .cra_u = { .blkcipher = { .min_keysize = ARC4_MIN_KEY_SIZE, .max_keysize = ARC4_MAX_KEY_SIZE, .setkey = arc4_set_key, .encrypt = ecb_arc4_crypt, .decrypt = ecb_arc4_crypt, }, }, } }; |
1da177e4c Linux-2.6.12-rc2 |
147 148 149 |
static int __init arc4_init(void) { |
ce6dd3689 crypto: arc4 - im... |
150 |
return crypto_register_algs(arc4_algs, ARRAY_SIZE(arc4_algs)); |
1da177e4c Linux-2.6.12-rc2 |
151 |
} |
1da177e4c Linux-2.6.12-rc2 |
152 153 |
static void __exit arc4_exit(void) { |
ce6dd3689 crypto: arc4 - im... |
154 |
crypto_unregister_algs(arc4_algs, ARRAY_SIZE(arc4_algs)); |
1da177e4c Linux-2.6.12-rc2 |
155 156 157 158 159 160 161 162 |
} module_init(arc4_init); module_exit(arc4_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("ARC4 Cipher Algorithm"); MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>"); |