1

crypto: cbc - Convert from skcipher to lskcipher

Replace the existing skcipher CBC template with an lskcipher version.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
Herbert Xu 2023-09-14 16:28:28 +08:00
parent 32a8dc4afc
commit 705b52fef3

View File

@ -5,8 +5,6 @@
* Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au> * Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au>
*/ */
#include <crypto/algapi.h>
#include <crypto/internal/cipher.h>
#include <crypto/internal/skcipher.h> #include <crypto/internal/skcipher.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/init.h> #include <linux/init.h>
@ -14,99 +12,71 @@
#include <linux/log2.h> #include <linux/log2.h>
#include <linux/module.h> #include <linux/module.h>
static int crypto_cbc_encrypt_segment(struct skcipher_walk *walk, static int crypto_cbc_encrypt_segment(struct crypto_lskcipher *tfm,
struct crypto_skcipher *skcipher) const u8 *src, u8 *dst, unsigned nbytes,
u8 *iv)
{ {
unsigned int bsize = crypto_skcipher_blocksize(skcipher); unsigned int bsize = crypto_lskcipher_blocksize(tfm);
void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
unsigned int nbytes = walk->nbytes;
u8 *src = walk->src.virt.addr;
u8 *dst = walk->dst.virt.addr;
struct crypto_cipher *cipher;
struct crypto_tfm *tfm;
u8 *iv = walk->iv;
cipher = skcipher_cipher_simple(skcipher); for (; nbytes >= bsize; src += bsize, dst += bsize, nbytes -= bsize) {
tfm = crypto_cipher_tfm(cipher);
fn = crypto_cipher_alg(cipher)->cia_encrypt;
do {
crypto_xor(iv, src, bsize); crypto_xor(iv, src, bsize);
fn(tfm, dst, iv); crypto_lskcipher_encrypt(tfm, iv, dst, bsize, NULL);
memcpy(iv, dst, bsize); memcpy(iv, dst, bsize);
}
src += bsize;
dst += bsize;
} while ((nbytes -= bsize) >= bsize);
return nbytes; return nbytes;
} }
static int crypto_cbc_encrypt_inplace(struct skcipher_walk *walk, static int crypto_cbc_encrypt_inplace(struct crypto_lskcipher *tfm,
struct crypto_skcipher *skcipher) u8 *src, unsigned nbytes, u8 *oiv)
{ {
unsigned int bsize = crypto_skcipher_blocksize(skcipher); unsigned int bsize = crypto_lskcipher_blocksize(tfm);
void (*fn)(struct crypto_tfm *, u8 *, const u8 *); u8 *iv = oiv;
unsigned int nbytes = walk->nbytes;
u8 *src = walk->src.virt.addr;
struct crypto_cipher *cipher;
struct crypto_tfm *tfm;
u8 *iv = walk->iv;
cipher = skcipher_cipher_simple(skcipher); if (nbytes < bsize)
tfm = crypto_cipher_tfm(cipher); goto out;
fn = crypto_cipher_alg(cipher)->cia_encrypt;
do { do {
crypto_xor(src, iv, bsize); crypto_xor(src, iv, bsize);
fn(tfm, src, src); crypto_lskcipher_encrypt(tfm, src, src, bsize, NULL);
iv = src; iv = src;
src += bsize; src += bsize;
} while ((nbytes -= bsize) >= bsize); } while ((nbytes -= bsize) >= bsize);
memcpy(walk->iv, iv, bsize); memcpy(oiv, iv, bsize);
out:
return nbytes; return nbytes;
} }
static int crypto_cbc_encrypt(struct skcipher_request *req) static int crypto_cbc_encrypt(struct crypto_lskcipher *tfm, const u8 *src,
u8 *dst, unsigned len, u8 *iv, bool final)
{ {
struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
struct skcipher_walk walk; struct crypto_lskcipher *cipher = *ctx;
int err; int rem;
err = skcipher_walk_virt(&walk, req, false); if (src == dst)
rem = crypto_cbc_encrypt_inplace(cipher, dst, len, iv);
else
rem = crypto_cbc_encrypt_segment(cipher, src, dst, len, iv);
while (walk.nbytes) { return rem && final ? -EINVAL : rem;
if (walk.src.virt.addr == walk.dst.virt.addr)
err = crypto_cbc_encrypt_inplace(&walk, skcipher);
else
err = crypto_cbc_encrypt_segment(&walk, skcipher);
err = skcipher_walk_done(&walk, err);
}
return err;
} }
static int crypto_cbc_decrypt_segment(struct skcipher_walk *walk, static int crypto_cbc_decrypt_segment(struct crypto_lskcipher *tfm,
struct crypto_skcipher *skcipher) const u8 *src, u8 *dst, unsigned nbytes,
u8 *oiv)
{ {
unsigned int bsize = crypto_skcipher_blocksize(skcipher); unsigned int bsize = crypto_lskcipher_blocksize(tfm);
void (*fn)(struct crypto_tfm *, u8 *, const u8 *); const u8 *iv = oiv;
unsigned int nbytes = walk->nbytes;
u8 *src = walk->src.virt.addr;
u8 *dst = walk->dst.virt.addr;
struct crypto_cipher *cipher;
struct crypto_tfm *tfm;
u8 *iv = walk->iv;
cipher = skcipher_cipher_simple(skcipher); if (nbytes < bsize)
tfm = crypto_cipher_tfm(cipher); goto out;
fn = crypto_cipher_alg(cipher)->cia_decrypt;
do { do {
fn(tfm, dst, src); crypto_lskcipher_decrypt(tfm, src, dst, bsize, NULL);
crypto_xor(dst, iv, bsize); crypto_xor(dst, iv, bsize);
iv = src; iv = src;
@ -114,83 +84,72 @@ static int crypto_cbc_decrypt_segment(struct skcipher_walk *walk,
dst += bsize; dst += bsize;
} while ((nbytes -= bsize) >= bsize); } while ((nbytes -= bsize) >= bsize);
memcpy(walk->iv, iv, bsize); memcpy(oiv, iv, bsize);
out:
return nbytes; return nbytes;
} }
static int crypto_cbc_decrypt_inplace(struct skcipher_walk *walk, static int crypto_cbc_decrypt_inplace(struct crypto_lskcipher *tfm,
struct crypto_skcipher *skcipher) u8 *src, unsigned nbytes, u8 *iv)
{ {
unsigned int bsize = crypto_skcipher_blocksize(skcipher); unsigned int bsize = crypto_lskcipher_blocksize(tfm);
void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
unsigned int nbytes = walk->nbytes;
u8 *src = walk->src.virt.addr;
u8 last_iv[MAX_CIPHER_BLOCKSIZE]; u8 last_iv[MAX_CIPHER_BLOCKSIZE];
struct crypto_cipher *cipher;
struct crypto_tfm *tfm;
cipher = skcipher_cipher_simple(skcipher); if (nbytes < bsize)
tfm = crypto_cipher_tfm(cipher); goto out;
fn = crypto_cipher_alg(cipher)->cia_decrypt;
/* Start of the last block. */ /* Start of the last block. */
src += nbytes - (nbytes & (bsize - 1)) - bsize; src += nbytes - (nbytes & (bsize - 1)) - bsize;
memcpy(last_iv, src, bsize); memcpy(last_iv, src, bsize);
for (;;) { for (;;) {
fn(tfm, src, src); crypto_lskcipher_decrypt(tfm, src, src, bsize, NULL);
if ((nbytes -= bsize) < bsize) if ((nbytes -= bsize) < bsize)
break; break;
crypto_xor(src, src - bsize, bsize); crypto_xor(src, src - bsize, bsize);
src -= bsize; src -= bsize;
} }
crypto_xor(src, walk->iv, bsize); crypto_xor(src, iv, bsize);
memcpy(walk->iv, last_iv, bsize); memcpy(iv, last_iv, bsize);
out:
return nbytes; return nbytes;
} }
static int crypto_cbc_decrypt(struct skcipher_request *req) static int crypto_cbc_decrypt(struct crypto_lskcipher *tfm, const u8 *src,
u8 *dst, unsigned len, u8 *iv, bool final)
{ {
struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
struct skcipher_walk walk; struct crypto_lskcipher *cipher = *ctx;
int err; int rem;
err = skcipher_walk_virt(&walk, req, false); if (src == dst)
rem = crypto_cbc_decrypt_inplace(cipher, dst, len, iv);
else
rem = crypto_cbc_decrypt_segment(cipher, src, dst, len, iv);
while (walk.nbytes) { return rem && final ? -EINVAL : rem;
if (walk.src.virt.addr == walk.dst.virt.addr)
err = crypto_cbc_decrypt_inplace(&walk, skcipher);
else
err = crypto_cbc_decrypt_segment(&walk, skcipher);
err = skcipher_walk_done(&walk, err);
}
return err;
} }
static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb) static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
{ {
struct skcipher_instance *inst; struct lskcipher_instance *inst;
struct crypto_alg *alg;
int err; int err;
inst = skcipher_alloc_instance_simple(tmpl, tb); inst = lskcipher_alloc_instance_simple(tmpl, tb);
if (IS_ERR(inst)) if (IS_ERR(inst))
return PTR_ERR(inst); return PTR_ERR(inst);
alg = skcipher_ialg_simple(inst);
err = -EINVAL; err = -EINVAL;
if (!is_power_of_2(alg->cra_blocksize)) if (!is_power_of_2(inst->alg.co.base.cra_blocksize))
goto out_free_inst; goto out_free_inst;
inst->alg.encrypt = crypto_cbc_encrypt; inst->alg.encrypt = crypto_cbc_encrypt;
inst->alg.decrypt = crypto_cbc_decrypt; inst->alg.decrypt = crypto_cbc_decrypt;
err = skcipher_register_instance(tmpl, inst); err = lskcipher_register_instance(tmpl, inst);
if (err) { if (err) {
out_free_inst: out_free_inst:
inst->free(inst); inst->free(inst);