Skip to content

Commit ce6dd36

Browse files
jkivilinherbertx
authored andcommitted
crypto: arc4 - improve performance by adding ecb(arc4)
Currently arc4.c provides simple one-byte blocksize cipher which is wrapped by ecb() module, giving function call overhead on every encrypted byte. This patch adds ecb(arc4) directly into arc4.c for higher performance. tcrypt results (speed ratios: new/old): AMD Phenom II, x86-64 : x2.7 Intel Core 2, x86-64 : x1.9 Intel Atom N260, i386 : x1.4 Cc: Jon Oberheide <jon@oberheide.org> Signed-off-by: Jussi Kivilinna <jussi.kivilinna@mbnet.fi> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 31b4cd2 commit ce6dd36

1 file changed

Lines changed: 87 additions & 22 deletions

File tree

‎crypto/arc4.c‎

Lines changed: 87 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
* (at your option) any later version.
1212
*
1313
*/
14+
1415
#include <linux/module.h>
1516
#include <linux/init.h>
1617
#include <linux/crypto.h>
18+
#include <crypto/algapi.h>
1719

1820
#define ARC4_MIN_KEY_SIZE 1
1921
#define ARC4_MAX_KEY_SIZE 256
@@ -48,51 +50,114 @@ static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
4850
return 0;
4951
}
5052

51-
static void arc4_crypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
53+
static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in,
54+
unsigned int len)
5255
{
53-
struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
54-
5556
u8 *const S = ctx->S;
56-
u8 x = ctx->x;
57-
u8 y = ctx->y;
58-
u8 a, b;
57+
u8 x, y, a, b;
58+
u8 ty, ta, tb;
59+
60+
if (len == 0)
61+
return;
62+
63+
x = ctx->x;
64+
y = ctx->y;
5965

6066
a = S[x];
6167
y = (y + a) & 0xff;
6268
b = S[y];
63-
S[x] = b;
64-
S[y] = a;
65-
x = (x + 1) & 0xff;
66-
*out++ = *in ^ S[(a + b) & 0xff];
69+
70+
do {
71+
S[y] = a;
72+
a = (a + b) & 0xff;
73+
S[x] = b;
74+
x = (x + 1) & 0xff;
75+
ta = S[x];
76+
ty = (y + ta) & 0xff;
77+
tb = S[ty];
78+
*out++ = *in++ ^ S[a];
79+
if (--len == 0)
80+
break;
81+
y = ty;
82+
a = ta;
83+
b = tb;
84+
} while (true);
6785

6886
ctx->x = x;
6987
ctx->y = y;
7088
}
7189

72-
static struct crypto_alg arc4_alg = {
90+
static void arc4_crypt_one(struct crypto_tfm *tfm, u8 *out, const u8 *in)
91+
{
92+
arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1);
93+
}
94+
95+
static int ecb_arc4_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
96+
struct scatterlist *src, unsigned int nbytes)
97+
{
98+
struct arc4_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
99+
struct blkcipher_walk walk;
100+
int err;
101+
102+
blkcipher_walk_init(&walk, dst, src, nbytes);
103+
104+
err = blkcipher_walk_virt(desc, &walk);
105+
106+
while (walk.nbytes > 0) {
107+
u8 *wsrc = walk.src.virt.addr;
108+
u8 *wdst = walk.dst.virt.addr;
109+
110+
arc4_crypt(ctx, wdst, wsrc, walk.nbytes);
111+
112+
err = blkcipher_walk_done(desc, &walk, 0);
113+
}
114+
115+
return err;
116+
}
117+
118+
static struct crypto_alg arc4_algs[2] = { {
73119
.cra_name = "arc4",
74120
.cra_flags = CRYPTO_ALG_TYPE_CIPHER,
75121
.cra_blocksize = ARC4_BLOCK_SIZE,
76122
.cra_ctxsize = sizeof(struct arc4_ctx),
77123
.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-
};
124+
.cra_u = {
125+
.cipher = {
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,
131+
},
132+
},
133+
}, {
134+
.cra_name = "ecb(arc4)",
135+
.cra_priority = 100,
136+
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
137+
.cra_blocksize = ARC4_BLOCK_SIZE,
138+
.cra_ctxsize = sizeof(struct arc4_ctx),
139+
.cra_alignmask = 0,
140+
.cra_type = &crypto_blkcipher_type,
141+
.cra_module = THIS_MODULE,
142+
.cra_u = {
143+
.blkcipher = {
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,
149+
},
150+
},
151+
} };
86152

87153
static int __init arc4_init(void)
88154
{
89-
return crypto_register_alg(&arc4_alg);
155+
return crypto_register_algs(arc4_algs, ARRAY_SIZE(arc4_algs));
90156
}
91157

92-
93158
static void __exit arc4_exit(void)
94159
{
95-
crypto_unregister_alg(&arc4_alg);
160+
crypto_unregister_algs(arc4_algs, ARRAY_SIZE(arc4_algs));
96161
}
97162

98163
module_init(arc4_init);

0 commit comments

Comments
 (0)