Skip to content

Commit 043a440

Browse files
committed
crypto: pcbc - Convert to skcipher
This patch converts lrw over to the skcipher interface. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 266d051 commit 043a440

1 file changed

Lines changed: 109 additions & 92 deletions

File tree

‎crypto/pcbc.c‎

Lines changed: 109 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,37 @@
1414
*
1515
*/
1616

17-
#include <crypto/algapi.h>
17+
#include <crypto/internal/skcipher.h>
1818
#include <linux/err.h>
1919
#include <linux/init.h>
2020
#include <linux/kernel.h>
2121
#include <linux/module.h>
22-
#include <linux/scatterlist.h>
2322
#include <linux/slab.h>
2423

2524
struct crypto_pcbc_ctx {
2625
struct crypto_cipher *child;
2726
};
2827

29-
static int crypto_pcbc_setkey(struct crypto_tfm *parent, const u8 *key,
28+
static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key,
3029
unsigned int keylen)
3130
{
32-
struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(parent);
31+
struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent);
3332
struct crypto_cipher *child = ctx->child;
3433
int err;
3534

3635
crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
37-
crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
38-
CRYPTO_TFM_REQ_MASK);
36+
crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
37+
CRYPTO_TFM_REQ_MASK);
3938
err = crypto_cipher_setkey(child, key, keylen);
40-
crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
41-
CRYPTO_TFM_RES_MASK);
39+
crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
40+
CRYPTO_TFM_RES_MASK);
4241
return err;
4342
}
4443

45-
static int crypto_pcbc_encrypt_segment(struct blkcipher_desc *desc,
46-
struct blkcipher_walk *walk,
44+
static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
45+
struct skcipher_walk *walk,
4746
struct crypto_cipher *tfm)
4847
{
49-
void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
50-
crypto_cipher_alg(tfm)->cia_encrypt;
5148
int bsize = crypto_cipher_blocksize(tfm);
5249
unsigned int nbytes = walk->nbytes;
5350
u8 *src = walk->src.virt.addr;
@@ -56,7 +53,7 @@ static int crypto_pcbc_encrypt_segment(struct blkcipher_desc *desc,
5653

5754
do {
5855
crypto_xor(iv, src, bsize);
59-
fn(crypto_cipher_tfm(tfm), dst, iv);
56+
crypto_cipher_encrypt_one(tfm, dst, iv);
6057
memcpy(iv, dst, bsize);
6158
crypto_xor(iv, src, bsize);
6259

@@ -67,12 +64,10 @@ static int crypto_pcbc_encrypt_segment(struct blkcipher_desc *desc,
6764
return nbytes;
6865
}
6966

70-
static int crypto_pcbc_encrypt_inplace(struct blkcipher_desc *desc,
71-
struct blkcipher_walk *walk,
67+
static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
68+
struct skcipher_walk *walk,
7269
struct crypto_cipher *tfm)
7370
{
74-
void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
75-
crypto_cipher_alg(tfm)->cia_encrypt;
7671
int bsize = crypto_cipher_blocksize(tfm);
7772
unsigned int nbytes = walk->nbytes;
7873
u8 *src = walk->src.virt.addr;
@@ -82,7 +77,7 @@ static int crypto_pcbc_encrypt_inplace(struct blkcipher_desc *desc,
8277
do {
8378
memcpy(tmpbuf, src, bsize);
8479
crypto_xor(iv, src, bsize);
85-
fn(crypto_cipher_tfm(tfm), src, iv);
80+
crypto_cipher_encrypt_one(tfm, src, iv);
8681
memcpy(iv, tmpbuf, bsize);
8782
crypto_xor(iv, src, bsize);
8883

@@ -94,46 +89,42 @@ static int crypto_pcbc_encrypt_inplace(struct blkcipher_desc *desc,
9489
return nbytes;
9590
}
9691

97-
static int crypto_pcbc_encrypt(struct blkcipher_desc *desc,
98-
struct scatterlist *dst, struct scatterlist *src,
99-
unsigned int nbytes)
92+
static int crypto_pcbc_encrypt(struct skcipher_request *req)
10093
{
101-
struct blkcipher_walk walk;
102-
struct crypto_blkcipher *tfm = desc->tfm;
103-
struct crypto_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
94+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
95+
struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
10496
struct crypto_cipher *child = ctx->child;
97+
struct skcipher_walk walk;
98+
unsigned int nbytes;
10599
int err;
106100

107-
blkcipher_walk_init(&walk, dst, src, nbytes);
108-
err = blkcipher_walk_virt(desc, &walk);
101+
err = skcipher_walk_virt(&walk, req, false);
109102

110103
while ((nbytes = walk.nbytes)) {
111104
if (walk.src.virt.addr == walk.dst.virt.addr)
112-
nbytes = crypto_pcbc_encrypt_inplace(desc, &walk,
105+
nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
113106
child);
114107
else
115-
nbytes = crypto_pcbc_encrypt_segment(desc, &walk,
108+
nbytes = crypto_pcbc_encrypt_segment(req, &walk,
116109
child);
117-
err = blkcipher_walk_done(desc, &walk, nbytes);
110+
err = skcipher_walk_done(&walk, nbytes);
118111
}
119112

120113
return err;
121114
}
122115

123-
static int crypto_pcbc_decrypt_segment(struct blkcipher_desc *desc,
124-
struct blkcipher_walk *walk,
116+
static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
117+
struct skcipher_walk *walk,
125118
struct crypto_cipher *tfm)
126119
{
127-
void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
128-
crypto_cipher_alg(tfm)->cia_decrypt;
129120
int bsize = crypto_cipher_blocksize(tfm);
130121
unsigned int nbytes = walk->nbytes;
131122
u8 *src = walk->src.virt.addr;
132123
u8 *dst = walk->dst.virt.addr;
133124
u8 *iv = walk->iv;
134125

135126
do {
136-
fn(crypto_cipher_tfm(tfm), dst, src);
127+
crypto_cipher_decrypt_one(tfm, dst, src);
137128
crypto_xor(dst, iv, bsize);
138129
memcpy(iv, src, bsize);
139130
crypto_xor(iv, dst, bsize);
@@ -147,21 +138,19 @@ static int crypto_pcbc_decrypt_segment(struct blkcipher_desc *desc,
147138
return nbytes;
148139
}
149140

150-
static int crypto_pcbc_decrypt_inplace(struct blkcipher_desc *desc,
151-
struct blkcipher_walk *walk,
141+
static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
142+
struct skcipher_walk *walk,
152143
struct crypto_cipher *tfm)
153144
{
154-
void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
155-
crypto_cipher_alg(tfm)->cia_decrypt;
156145
int bsize = crypto_cipher_blocksize(tfm);
157146
unsigned int nbytes = walk->nbytes;
158147
u8 *src = walk->src.virt.addr;
159148
u8 *iv = walk->iv;
160-
u8 tmpbuf[bsize];
149+
u8 tmpbuf[bsize] __attribute__ ((aligned(__alignof__(u32))));
161150

162151
do {
163152
memcpy(tmpbuf, src, bsize);
164-
fn(crypto_cipher_tfm(tfm), src, src);
153+
crypto_cipher_decrypt_one(tfm, src, src);
165154
crypto_xor(src, iv, bsize);
166155
memcpy(iv, tmpbuf, bsize);
167156
crypto_xor(iv, src, bsize);
@@ -174,37 +163,35 @@ static int crypto_pcbc_decrypt_inplace(struct blkcipher_desc *desc,
174163
return nbytes;
175164
}
176165

177-
static int crypto_pcbc_decrypt(struct blkcipher_desc *desc,
178-
struct scatterlist *dst, struct scatterlist *src,
179-
unsigned int nbytes)
166+
static int crypto_pcbc_decrypt(struct skcipher_request *req)
180167
{
181-
struct blkcipher_walk walk;
182-
struct crypto_blkcipher *tfm = desc->tfm;
183-
struct crypto_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
168+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
169+
struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
184170
struct crypto_cipher *child = ctx->child;
171+
struct skcipher_walk walk;
172+
unsigned int nbytes;
185173
int err;
186174

187-
blkcipher_walk_init(&walk, dst, src, nbytes);
188-
err = blkcipher_walk_virt(desc, &walk);
175+
err = skcipher_walk_virt(&walk, req, false);
189176

190177
while ((nbytes = walk.nbytes)) {
191178
if (walk.src.virt.addr == walk.dst.virt.addr)
192-
nbytes = crypto_pcbc_decrypt_inplace(desc, &walk,
179+
nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
193180
child);
194181
else
195-
nbytes = crypto_pcbc_decrypt_segment(desc, &walk,
182+
nbytes = crypto_pcbc_decrypt_segment(req, &walk,
196183
child);
197-
err = blkcipher_walk_done(desc, &walk, nbytes);
184+
err = skcipher_walk_done(&walk, nbytes);
198185
}
199186

200187
return err;
201188
}
202189

203-
static int crypto_pcbc_init_tfm(struct crypto_tfm *tfm)
190+
static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
204191
{
205-
struct crypto_instance *inst = (void *)tfm->__crt_alg;
206-
struct crypto_spawn *spawn = crypto_instance_ctx(inst);
207-
struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm);
192+
struct skcipher_instance *inst = skcipher_alg_instance(tfm);
193+
struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
194+
struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
208195
struct crypto_cipher *cipher;
209196

210197
cipher = crypto_spawn_cipher(spawn);
@@ -215,68 +202,98 @@ static int crypto_pcbc_init_tfm(struct crypto_tfm *tfm)
215202
return 0;
216203
}
217204

218-
static void crypto_pcbc_exit_tfm(struct crypto_tfm *tfm)
205+
static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
219206
{
220-
struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm);
207+
struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
208+
221209
crypto_free_cipher(ctx->child);
222210
}
223211

224-
static struct crypto_instance *crypto_pcbc_alloc(struct rtattr **tb)
212+
static void crypto_pcbc_free(struct skcipher_instance *inst)
213+
{
214+
crypto_drop_skcipher(skcipher_instance_ctx(inst));
215+
kfree(inst);
216+
}
217+
218+
static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
225219
{
226-
struct crypto_instance *inst;
220+
struct skcipher_instance *inst;
221+
struct crypto_attr_type *algt;
222+
struct crypto_spawn *spawn;
227223
struct crypto_alg *alg;
228224
int err;
229225

230-
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
231-
if (err)
232-
return ERR_PTR(err);
226+
algt = crypto_get_attr_type(tb);
227+
if (IS_ERR(algt))
228+
return PTR_ERR(algt);
229+
230+
if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) &
231+
~CRYPTO_ALG_INTERNAL)
232+
return -EINVAL;
233233

234-
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
235-
CRYPTO_ALG_TYPE_MASK);
234+
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
235+
if (!inst)
236+
return -ENOMEM;
237+
238+
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER |
239+
(algt->type & CRYPTO_ALG_INTERNAL),
240+
CRYPTO_ALG_TYPE_MASK |
241+
(algt->mask & CRYPTO_ALG_INTERNAL));
242+
err = PTR_ERR(alg);
236243
if (IS_ERR(alg))
237-
return ERR_CAST(alg);
244+
goto err_free_inst;
245+
246+
spawn = skcipher_instance_ctx(inst);
247+
err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
248+
CRYPTO_ALG_TYPE_MASK);
249+
crypto_mod_put(alg);
250+
if (err)
251+
goto err_free_inst;
238252

239-
inst = crypto_alloc_instance("pcbc", alg);
240-
if (IS_ERR(inst))
241-
goto out_put_alg;
253+
err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg);
254+
if (err)
255+
goto err_drop_spawn;
242256

243-
inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
244-
inst->alg.cra_priority = alg->cra_priority;
245-
inst->alg.cra_blocksize = alg->cra_blocksize;
246-
inst->alg.cra_alignmask = alg->cra_alignmask;
247-
inst->alg.cra_type = &crypto_blkcipher_type;
257+
inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL;
258+
inst->alg.base.cra_priority = alg->cra_priority;
259+
inst->alg.base.cra_blocksize = alg->cra_blocksize;
260+
inst->alg.base.cra_alignmask = alg->cra_alignmask;
248261

249262
/* We access the data as u32s when xoring. */
250-
inst->alg.cra_alignmask |= __alignof__(u32) - 1;
263+
inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
251264

252-
inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
253-
inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
254-
inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
265+
inst->alg.ivsize = alg->cra_blocksize;
266+
inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
267+
inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
255268

256-
inst->alg.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
269+
inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
257270

258-
inst->alg.cra_init = crypto_pcbc_init_tfm;
259-
inst->alg.cra_exit = crypto_pcbc_exit_tfm;
271+
inst->alg.init = crypto_pcbc_init_tfm;
272+
inst->alg.exit = crypto_pcbc_exit_tfm;
260273

261-
inst->alg.cra_blkcipher.setkey = crypto_pcbc_setkey;
262-
inst->alg.cra_blkcipher.encrypt = crypto_pcbc_encrypt;
263-
inst->alg.cra_blkcipher.decrypt = crypto_pcbc_decrypt;
274+
inst->alg.setkey = crypto_pcbc_setkey;
275+
inst->alg.encrypt = crypto_pcbc_encrypt;
276+
inst->alg.decrypt = crypto_pcbc_decrypt;
264277

265-
out_put_alg:
266-
crypto_mod_put(alg);
267-
return inst;
268-
}
278+
inst->free = crypto_pcbc_free;
269279

270-
static void crypto_pcbc_free(struct crypto_instance *inst)
271-
{
272-
crypto_drop_spawn(crypto_instance_ctx(inst));
280+
err = skcipher_register_instance(tmpl, inst);
281+
if (err)
282+
goto err_drop_spawn;
283+
284+
out:
285+
return err;
286+
287+
err_drop_spawn:
288+
crypto_drop_spawn(spawn);
289+
err_free_inst:
273290
kfree(inst);
291+
goto out;
274292
}
275293

276294
static struct crypto_template crypto_pcbc_tmpl = {
277295
.name = "pcbc",
278-
.alloc = crypto_pcbc_alloc,
279-
.free = crypto_pcbc_free,
296+
.create = crypto_pcbc_create,
280297
.module = THIS_MODULE,
281298
};
282299

0 commit comments

Comments
 (0)