2018-09-20 16:18:39 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OFB: Output FeedBack mode
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 ARM Limited or its affiliates.
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <crypto/algapi.h>
|
2020-12-11 15:27:15 +03:00
|
|
|
#include <crypto/internal/cipher.h>
|
2018-09-20 16:18:39 +03:00
|
|
|
#include <crypto/internal/skcipher.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
2019-01-04 07:16:12 +03:00
|
|
|
static int crypto_ofb_crypt(struct skcipher_request *req)
|
2018-09-20 16:18:39 +03:00
|
|
|
{
|
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
2019-01-04 07:16:20 +03:00
|
|
|
struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
|
2019-01-04 07:16:12 +03:00
|
|
|
const unsigned int bsize = crypto_cipher_blocksize(cipher);
|
|
|
|
struct skcipher_walk walk;
|
|
|
|
int err;
|
2018-09-20 16:18:39 +03:00
|
|
|
|
2019-01-04 07:16:12 +03:00
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
2018-09-20 16:18:39 +03:00
|
|
|
|
2019-01-04 07:16:12 +03:00
|
|
|
while (walk.nbytes >= bsize) {
|
|
|
|
const u8 *src = walk.src.virt.addr;
|
|
|
|
u8 *dst = walk.dst.virt.addr;
|
|
|
|
u8 * const iv = walk.iv;
|
|
|
|
unsigned int nbytes = walk.nbytes;
|
2018-09-20 16:18:39 +03:00
|
|
|
|
2019-01-04 07:16:12 +03:00
|
|
|
do {
|
|
|
|
crypto_cipher_encrypt_one(cipher, iv, iv);
|
|
|
|
crypto_xor_cpy(dst, src, iv, bsize);
|
|
|
|
dst += bsize;
|
|
|
|
src += bsize;
|
|
|
|
} while ((nbytes -= bsize) >= bsize);
|
2018-09-20 16:18:39 +03:00
|
|
|
|
2019-01-04 07:16:12 +03:00
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
|
|
|
}
|
2018-09-20 16:18:39 +03:00
|
|
|
|
2019-01-04 07:16:12 +03:00
|
|
|
if (walk.nbytes) {
|
|
|
|
crypto_cipher_encrypt_one(cipher, walk.iv, walk.iv);
|
|
|
|
crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, walk.iv,
|
|
|
|
walk.nbytes);
|
|
|
|
err = skcipher_walk_done(&walk, 0);
|
|
|
|
}
|
|
|
|
return err;
|
2018-09-20 16:18:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb)
|
|
|
|
{
|
|
|
|
struct skcipher_instance *inst;
|
|
|
|
struct crypto_alg *alg;
|
|
|
|
int err;
|
|
|
|
|
2019-12-20 08:29:40 +03:00
|
|
|
inst = skcipher_alloc_instance_simple(tmpl, tb);
|
2019-01-04 07:16:20 +03:00
|
|
|
if (IS_ERR(inst))
|
|
|
|
return PTR_ERR(inst);
|
2018-09-20 16:18:39 +03:00
|
|
|
|
2019-12-20 08:29:40 +03:00
|
|
|
alg = skcipher_ialg_simple(inst);
|
|
|
|
|
2019-01-04 07:16:12 +03:00
|
|
|
/* OFB mode is a stream cipher. */
|
|
|
|
inst->alg.base.cra_blocksize = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To simplify the implementation, configure the skcipher walk to only
|
|
|
|
* give a partial block at the very end, never earlier.
|
|
|
|
*/
|
|
|
|
inst->alg.chunksize = alg->cra_blocksize;
|
|
|
|
|
|
|
|
inst->alg.encrypt = crypto_ofb_crypt;
|
|
|
|
inst->alg.decrypt = crypto_ofb_crypt;
|
2018-09-20 16:18:39 +03:00
|
|
|
|
|
|
|
err = skcipher_register_instance(tmpl, inst);
|
|
|
|
if (err)
|
2019-01-04 07:16:20 +03:00
|
|
|
inst->free(inst);
|
2018-09-20 16:18:39 +03:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct crypto_template crypto_ofb_tmpl = {
|
|
|
|
.name = "ofb",
|
|
|
|
.create = crypto_ofb_create,
|
|
|
|
.module = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init crypto_ofb_module_init(void)
|
|
|
|
{
|
|
|
|
return crypto_register_template(&crypto_ofb_tmpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit crypto_ofb_module_exit(void)
|
|
|
|
{
|
|
|
|
crypto_unregister_template(&crypto_ofb_tmpl);
|
|
|
|
}
|
|
|
|
|
2019-04-12 07:57:42 +03:00
|
|
|
subsys_initcall(crypto_ofb_module_init);
|
2018-09-20 16:18:39 +03:00
|
|
|
module_exit(crypto_ofb_module_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
2019-01-04 07:16:20 +03:00
|
|
|
MODULE_DESCRIPTION("OFB block cipher mode of operation");
|
2018-09-20 16:18:39 +03:00
|
|
|
MODULE_ALIAS_CRYPTO("ofb");
|
2020-12-11 15:27:15 +03:00
|
|
|
MODULE_IMPORT_NS(CRYPTO_INTERNAL);
|