aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@google.com>2019-01-03 23:16:23 -0500
committerHerbert Xu <herbert@gondor.apana.org.au>2019-01-11 01:16:58 -0500
commit426bcb50856f61f5883b0e2e7f885ca142e7729c (patch)
tree94fbaf6fdd782ebf17d6134a0ab3f3f444b2d5ab /crypto
parent0be487ba2e2f1593f7274b04615367d8830f6461 (diff)
crypto: arc4 - convert to skcipher API
Convert the "ecb(arc4)" algorithm from the deprecated "blkcipher" API to the "skcipher" API. (Note that this is really a stream cipher and not a block cipher in ECB mode as the name implies, but that's a problem for another day...) Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/arc4.c82
1 files changed, 44 insertions, 38 deletions
diff --git a/crypto/arc4.c b/crypto/arc4.c
index f1a81925558f..652d24399afa 100644
--- a/crypto/arc4.c
+++ b/crypto/arc4.c
@@ -12,10 +12,10 @@
12 * 12 *
13 */ 13 */
14 14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/crypto.h>
18#include <crypto/algapi.h> 15#include <crypto/algapi.h>
16#include <crypto/internal/skcipher.h>
17#include <linux/init.h>
18#include <linux/module.h>
19 19
20#define ARC4_MIN_KEY_SIZE 1 20#define ARC4_MIN_KEY_SIZE 1
21#define ARC4_MAX_KEY_SIZE 256 21#define ARC4_MAX_KEY_SIZE 256
@@ -50,6 +50,12 @@ static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
50 return 0; 50 return 0;
51} 51}
52 52
53static int arc4_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *in_key,
54 unsigned int key_len)
55{
56 return arc4_set_key(&tfm->base, in_key, key_len);
57}
58
53static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in, 59static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in,
54 unsigned int len) 60 unsigned int len)
55{ 61{
@@ -92,30 +98,25 @@ static void arc4_crypt_one(struct crypto_tfm *tfm, u8 *out, const u8 *in)
92 arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1); 98 arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1);
93} 99}
94 100
95static int ecb_arc4_crypt(struct blkcipher_desc *desc, struct scatterlist *dst, 101static int ecb_arc4_crypt(struct skcipher_request *req)
96 struct scatterlist *src, unsigned int nbytes)
97{ 102{
98 struct arc4_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); 103 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
99 struct blkcipher_walk walk; 104 struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
105 struct skcipher_walk walk;
100 int err; 106 int err;
101 107
102 blkcipher_walk_init(&walk, dst, src, nbytes); 108 err = skcipher_walk_virt(&walk, req, false);
103
104 err = blkcipher_walk_virt(desc, &walk);
105 109
106 while (walk.nbytes > 0) { 110 while (walk.nbytes > 0) {
107 u8 *wsrc = walk.src.virt.addr; 111 arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
108 u8 *wdst = walk.dst.virt.addr; 112 walk.nbytes);
109 113 err = skcipher_walk_done(&walk, 0);
110 arc4_crypt(ctx, wdst, wsrc, walk.nbytes);
111
112 err = blkcipher_walk_done(desc, &walk, 0);
113 } 114 }
114 115
115 return err; 116 return err;
116} 117}
117 118
118static struct crypto_alg arc4_algs[2] = { { 119static struct crypto_alg arc4_cipher = {
119 .cra_name = "arc4", 120 .cra_name = "arc4",
120 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 121 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
121 .cra_blocksize = ARC4_BLOCK_SIZE, 122 .cra_blocksize = ARC4_BLOCK_SIZE,
@@ -130,34 +131,39 @@ static struct crypto_alg arc4_algs[2] = { {
130 .cia_decrypt = arc4_crypt_one, 131 .cia_decrypt = arc4_crypt_one,
131 }, 132 },
132 }, 133 },
133}, { 134};
134 .cra_name = "ecb(arc4)", 135
135 .cra_priority = 100, 136static struct skcipher_alg arc4_skcipher = {
136 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, 137 .base.cra_name = "ecb(arc4)",
137 .cra_blocksize = ARC4_BLOCK_SIZE, 138 .base.cra_priority = 100,
138 .cra_ctxsize = sizeof(struct arc4_ctx), 139 .base.cra_blocksize = ARC4_BLOCK_SIZE,
139 .cra_alignmask = 0, 140 .base.cra_ctxsize = sizeof(struct arc4_ctx),
140 .cra_type = &crypto_blkcipher_type, 141 .base.cra_module = THIS_MODULE,
141 .cra_module = THIS_MODULE, 142 .min_keysize = ARC4_MIN_KEY_SIZE,
142 .cra_u = { 143 .max_keysize = ARC4_MAX_KEY_SIZE,
143 .blkcipher = { 144 .setkey = arc4_set_key_skcipher,
144 .min_keysize = ARC4_MIN_KEY_SIZE, 145 .encrypt = ecb_arc4_crypt,
145 .max_keysize = ARC4_MAX_KEY_SIZE, 146 .decrypt = ecb_arc4_crypt,
146 .setkey = arc4_set_key, 147};
147 .encrypt = ecb_arc4_crypt,
148 .decrypt = ecb_arc4_crypt,
149 },
150 },
151} };
152 148
153static int __init arc4_init(void) 149static int __init arc4_init(void)
154{ 150{
155 return crypto_register_algs(arc4_algs, ARRAY_SIZE(arc4_algs)); 151 int err;
152
153 err = crypto_register_alg(&arc4_cipher);
154 if (err)
155 return err;
156
157 err = crypto_register_skcipher(&arc4_skcipher);
158 if (err)
159 crypto_unregister_alg(&arc4_cipher);
160 return err;
156} 161}
157 162
158static void __exit arc4_exit(void) 163static void __exit arc4_exit(void)
159{ 164{
160 crypto_unregister_algs(arc4_algs, ARRAY_SIZE(arc4_algs)); 165 crypto_unregister_alg(&arc4_cipher);
166 crypto_unregister_skcipher(&arc4_skcipher);
161} 167}
162 168
163module_init(arc4_init); 169module_init(arc4_init);