Skip to content

Commit dc51f25

Browse files
Ard Biesheuvelherbertx
authored andcommitted
crypto: arc4 - refactor arc4 core code into separate library
Refactor the core rc4 handling so we can move most users to a library interface, permitting us to drop the cipher interface entirely in a future patch. This is part of an effort to simplify the crypto API and improve its robustness against incorrect use. Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 192125e commit dc51f25

File tree

7 files changed

+95
-60
lines changed

7 files changed

+95
-60
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4229,6 +4229,7 @@ F: crypto/
42294229
F: drivers/crypto/
42304230
F: include/crypto/
42314231
F: include/linux/crypto*
4232+
F: lib/crypto/
42324233

42334234
CRYPTOGRAPHIC RANDOM NUMBER GENERATOR
42344235
M: Neil Horman <nhorman@tuxdriver.com>

crypto/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,9 +1237,13 @@ config CRYPTO_ANUBIS
12371237
<https://www.cosic.esat.kuleuven.be/nessie/reports/>
12381238
<http://www.larc.usp.br/~pbarreto/AnubisPage.html>
12391239

1240+
config CRYPTO_LIB_ARC4
1241+
tristate
1242+
12401243
config CRYPTO_ARC4
12411244
tristate "ARC4 cipher algorithm"
12421245
select CRYPTO_BLKCIPHER
1246+
select CRYPTO_LIB_ARC4
12431247
help
12441248
ARC4 cipher algorithm.
12451249

crypto/arc4.c

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,12 @@
1818
#include <linux/init.h>
1919
#include <linux/module.h>
2020

21-
struct arc4_ctx {
22-
u32 S[256];
23-
u32 x, y;
24-
};
25-
2621
static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
2722
unsigned int key_len)
2823
{
2924
struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
30-
int i, j = 0, k = 0;
31-
32-
ctx->x = 1;
33-
ctx->y = 0;
3425

35-
for (i = 0; i < 256; i++)
36-
ctx->S[i] = i;
37-
38-
for (i = 0; i < 256; i++) {
39-
u32 a = ctx->S[i];
40-
j = (j + in_key[k] + a) & 0xff;
41-
ctx->S[i] = ctx->S[j];
42-
ctx->S[j] = a;
43-
if (++k >= key_len)
44-
k = 0;
45-
}
46-
47-
return 0;
26+
return arc4_setkey(ctx, in_key, key_len);
4827
}
4928

5029
static int arc4_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *in_key,
@@ -53,43 +32,6 @@ static int arc4_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *in_key,
5332
return arc4_set_key(&tfm->base, in_key, key_len);
5433
}
5534

56-
static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in,
57-
unsigned int len)
58-
{
59-
u32 *const S = ctx->S;
60-
u32 x, y, a, b;
61-
u32 ty, ta, tb;
62-
63-
if (len == 0)
64-
return;
65-
66-
x = ctx->x;
67-
y = ctx->y;
68-
69-
a = S[x];
70-
y = (y + a) & 0xff;
71-
b = S[y];
72-
73-
do {
74-
S[y] = a;
75-
a = (a + b) & 0xff;
76-
S[x] = b;
77-
x = (x + 1) & 0xff;
78-
ta = S[x];
79-
ty = (y + ta) & 0xff;
80-
tb = S[ty];
81-
*out++ = *in++ ^ S[a];
82-
if (--len == 0)
83-
break;
84-
y = ty;
85-
a = ta;
86-
b = tb;
87-
} while (true);
88-
89-
ctx->x = x;
90-
ctx->y = y;
91-
}
92-
9335
static void arc4_crypt_one(struct crypto_tfm *tfm, u8 *out, const u8 *in)
9436
{
9537
arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1);

include/crypto/arc4.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@
66
#ifndef _CRYPTO_ARC4_H
77
#define _CRYPTO_ARC4_H
88

9+
#include <linux/types.h>
10+
911
#define ARC4_MIN_KEY_SIZE 1
1012
#define ARC4_MAX_KEY_SIZE 256
1113
#define ARC4_BLOCK_SIZE 1
1214

15+
struct arc4_ctx {
16+
u32 S[256];
17+
u32 x, y;
18+
};
19+
20+
int arc4_setkey(struct arc4_ctx *ctx, const u8 *in_key, unsigned int key_len);
21+
void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in, unsigned int len);
22+
1323
#endif /* _CRYPTO_ARC4_H */

lib/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ endif
102102
obj-$(CONFIG_DEBUG_INFO_REDUCED) += debug_info.o
103103
CFLAGS_debug_info.o += $(call cc-option, -femit-struct-debug-detailed=any)
104104

105-
obj-y += math/
105+
obj-y += math/ crypto/
106106

107107
obj-$(CONFIG_GENERIC_IOMAP) += iomap.o
108108
obj-$(CONFIG_GENERIC_PCI_IOMAP) += pci_iomap.o

lib/crypto/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
obj-$(CONFIG_CRYPTO_LIB_ARC4) += libarc4.o
4+
libarc4-y := arc4.o

lib/crypto/arc4.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Cryptographic API
4+
*
5+
* ARC4 Cipher Algorithm
6+
*
7+
* Jon Oberheide <jon@oberheide.org>
8+
*/
9+
10+
#include <crypto/arc4.h>
11+
#include <linux/module.h>
12+
13+
int arc4_setkey(struct arc4_ctx *ctx, const u8 *in_key, unsigned int key_len)
14+
{
15+
int i, j = 0, k = 0;
16+
17+
ctx->x = 1;
18+
ctx->y = 0;
19+
20+
for (i = 0; i < 256; i++)
21+
ctx->S[i] = i;
22+
23+
for (i = 0; i < 256; i++) {
24+
u32 a = ctx->S[i];
25+
26+
j = (j + in_key[k] + a) & 0xff;
27+
ctx->S[i] = ctx->S[j];
28+
ctx->S[j] = a;
29+
if (++k >= key_len)
30+
k = 0;
31+
}
32+
33+
return 0;
34+
}
35+
EXPORT_SYMBOL(arc4_setkey);
36+
37+
void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in, unsigned int len)
38+
{
39+
u32 *const S = ctx->S;
40+
u32 x, y, a, b;
41+
u32 ty, ta, tb;
42+
43+
if (len == 0)
44+
return;
45+
46+
x = ctx->x;
47+
y = ctx->y;
48+
49+
a = S[x];
50+
y = (y + a) & 0xff;
51+
b = S[y];
52+
53+
do {
54+
S[y] = a;
55+
a = (a + b) & 0xff;
56+
S[x] = b;
57+
x = (x + 1) & 0xff;
58+
ta = S[x];
59+
ty = (y + ta) & 0xff;
60+
tb = S[ty];
61+
*out++ = *in++ ^ S[a];
62+
if (--len == 0)
63+
break;
64+
y = ty;
65+
a = ta;
66+
b = tb;
67+
} while (true);
68+
69+
ctx->x = x;
70+
ctx->y = y;
71+
}
72+
EXPORT_SYMBOL(arc4_crypt);
73+
74+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)