2019-05-27 06:55:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2006-12-16 01:09:02 +00:00
|
|
|
/*
|
|
|
|
* PCBC: Propagating Cipher Block Chaining mode
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*
|
|
|
|
* Derived from cbc.c
|
|
|
|
* - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
|
|
|
|
*/
|
|
|
|
|
2018-04-09 13:54:47 +00:00
|
|
|
#include <crypto/algapi.h>
|
2020-12-11 12:27:15 +00:00
|
|
|
#include <crypto/internal/cipher.h>
|
2016-11-22 12:08:27 +00:00
|
|
|
#include <crypto/internal/skcipher.h>
|
2006-12-16 01:09:02 +00:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
|
|
|
|
struct skcipher_walk *walk,
|
2007-11-20 09:49:49 +00:00
|
|
|
struct crypto_cipher *tfm)
|
2006-12-16 01:09:02 +00:00
|
|
|
{
|
|
|
|
int bsize = crypto_cipher_blocksize(tfm);
|
|
|
|
unsigned int nbytes = walk->nbytes;
|
|
|
|
u8 *src = walk->src.virt.addr;
|
|
|
|
u8 *dst = walk->dst.virt.addr;
|
2019-01-04 04:16:13 +00:00
|
|
|
u8 * const iv = walk->iv;
|
2006-12-16 01:09:02 +00:00
|
|
|
|
|
|
|
do {
|
2007-11-20 09:49:49 +00:00
|
|
|
crypto_xor(iv, src, bsize);
|
2016-11-22 12:08:27 +00:00
|
|
|
crypto_cipher_encrypt_one(tfm, dst, iv);
|
crypto: algapi - make crypto_xor() take separate dst and src arguments
There are quite a number of occurrences in the kernel of the pattern
if (dst != src)
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
or
crypto_xor(keystream, src, nbytes);
memcpy(dst, keystream, nbytes);
where crypto_xor() is preceded or followed by a memcpy() invocation
that is only there because crypto_xor() uses its output parameter as
one of the inputs. To avoid having to add new instances of this pattern
in the arm64 code, which will be refactored to implement non-SIMD
fallbacks, add an alternative implementation called crypto_xor_cpy(),
taking separate input and output arguments. This removes the need for
the separate memcpy().
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2017-07-24 10:28:04 +00:00
|
|
|
crypto_xor_cpy(iv, dst, src, bsize);
|
2006-12-16 01:09:02 +00:00
|
|
|
|
|
|
|
src += bsize;
|
|
|
|
dst += bsize;
|
|
|
|
} while ((nbytes -= bsize) >= bsize);
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
|
|
|
|
struct skcipher_walk *walk,
|
2007-11-20 09:49:49 +00:00
|
|
|
struct crypto_cipher *tfm)
|
2006-12-16 01:09:02 +00:00
|
|
|
{
|
|
|
|
int bsize = crypto_cipher_blocksize(tfm);
|
|
|
|
unsigned int nbytes = walk->nbytes;
|
|
|
|
u8 *src = walk->src.virt.addr;
|
2019-01-04 04:16:13 +00:00
|
|
|
u8 * const iv = walk->iv;
|
2018-04-09 13:54:47 +00:00
|
|
|
u8 tmpbuf[MAX_CIPHER_BLOCKSIZE];
|
2006-12-16 01:09:02 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
memcpy(tmpbuf, src, bsize);
|
2007-11-20 09:49:49 +00:00
|
|
|
crypto_xor(iv, src, bsize);
|
2016-11-22 12:08:27 +00:00
|
|
|
crypto_cipher_encrypt_one(tfm, src, iv);
|
crypto: algapi - make crypto_xor() take separate dst and src arguments
There are quite a number of occurrences in the kernel of the pattern
if (dst != src)
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
or
crypto_xor(keystream, src, nbytes);
memcpy(dst, keystream, nbytes);
where crypto_xor() is preceded or followed by a memcpy() invocation
that is only there because crypto_xor() uses its output parameter as
one of the inputs. To avoid having to add new instances of this pattern
in the arm64 code, which will be refactored to implement non-SIMD
fallbacks, add an alternative implementation called crypto_xor_cpy(),
taking separate input and output arguments. This removes the need for
the separate memcpy().
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2017-07-24 10:28:04 +00:00
|
|
|
crypto_xor_cpy(iv, tmpbuf, src, bsize);
|
2006-12-16 01:09:02 +00:00
|
|
|
|
|
|
|
src += bsize;
|
|
|
|
} while ((nbytes -= bsize) >= bsize);
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
static int crypto_pcbc_encrypt(struct skcipher_request *req)
|
2006-12-16 01:09:02 +00:00
|
|
|
{
|
2016-11-22 12:08:27 +00:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
2019-01-04 04:16:22 +00:00
|
|
|
struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
|
2016-11-22 12:08:27 +00:00
|
|
|
struct skcipher_walk walk;
|
|
|
|
unsigned int nbytes;
|
2006-12-16 01:09:02 +00:00
|
|
|
int err;
|
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
2006-12-16 01:09:02 +00:00
|
|
|
|
2024-01-16 10:43:02 +00:00
|
|
|
while (walk.nbytes) {
|
2006-12-16 01:09:02 +00:00
|
|
|
if (walk.src.virt.addr == walk.dst.virt.addr)
|
2016-11-22 12:08:27 +00:00
|
|
|
nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
|
2019-01-04 04:16:22 +00:00
|
|
|
cipher);
|
2006-12-16 01:09:02 +00:00
|
|
|
else
|
2016-11-22 12:08:27 +00:00
|
|
|
nbytes = crypto_pcbc_encrypt_segment(req, &walk,
|
2019-01-04 04:16:22 +00:00
|
|
|
cipher);
|
2016-11-22 12:08:27 +00:00
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
2006-12-16 01:09:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
|
|
|
|
struct skcipher_walk *walk,
|
2007-11-20 09:49:49 +00:00
|
|
|
struct crypto_cipher *tfm)
|
2006-12-16 01:09:02 +00:00
|
|
|
{
|
|
|
|
int bsize = crypto_cipher_blocksize(tfm);
|
|
|
|
unsigned int nbytes = walk->nbytes;
|
|
|
|
u8 *src = walk->src.virt.addr;
|
|
|
|
u8 *dst = walk->dst.virt.addr;
|
2019-01-04 04:16:13 +00:00
|
|
|
u8 * const iv = walk->iv;
|
2006-12-16 01:09:02 +00:00
|
|
|
|
|
|
|
do {
|
2016-11-22 12:08:27 +00:00
|
|
|
crypto_cipher_decrypt_one(tfm, dst, src);
|
2007-11-20 09:49:49 +00:00
|
|
|
crypto_xor(dst, iv, bsize);
|
crypto: algapi - make crypto_xor() take separate dst and src arguments
There are quite a number of occurrences in the kernel of the pattern
if (dst != src)
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
or
crypto_xor(keystream, src, nbytes);
memcpy(dst, keystream, nbytes);
where crypto_xor() is preceded or followed by a memcpy() invocation
that is only there because crypto_xor() uses its output parameter as
one of the inputs. To avoid having to add new instances of this pattern
in the arm64 code, which will be refactored to implement non-SIMD
fallbacks, add an alternative implementation called crypto_xor_cpy(),
taking separate input and output arguments. This removes the need for
the separate memcpy().
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2017-07-24 10:28:04 +00:00
|
|
|
crypto_xor_cpy(iv, dst, src, bsize);
|
2006-12-16 01:09:02 +00:00
|
|
|
|
|
|
|
src += bsize;
|
|
|
|
dst += bsize;
|
|
|
|
} while ((nbytes -= bsize) >= bsize);
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
|
|
|
|
struct skcipher_walk *walk,
|
2007-11-20 09:49:49 +00:00
|
|
|
struct crypto_cipher *tfm)
|
2006-12-16 01:09:02 +00:00
|
|
|
{
|
|
|
|
int bsize = crypto_cipher_blocksize(tfm);
|
|
|
|
unsigned int nbytes = walk->nbytes;
|
|
|
|
u8 *src = walk->src.virt.addr;
|
2019-01-04 04:16:13 +00:00
|
|
|
u8 * const iv = walk->iv;
|
2018-04-09 13:54:47 +00:00
|
|
|
u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32));
|
2006-12-16 01:09:02 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
memcpy(tmpbuf, src, bsize);
|
2016-11-22 12:08:27 +00:00
|
|
|
crypto_cipher_decrypt_one(tfm, src, src);
|
2007-11-20 09:49:49 +00:00
|
|
|
crypto_xor(src, iv, bsize);
|
crypto: algapi - make crypto_xor() take separate dst and src arguments
There are quite a number of occurrences in the kernel of the pattern
if (dst != src)
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
or
crypto_xor(keystream, src, nbytes);
memcpy(dst, keystream, nbytes);
where crypto_xor() is preceded or followed by a memcpy() invocation
that is only there because crypto_xor() uses its output parameter as
one of the inputs. To avoid having to add new instances of this pattern
in the arm64 code, which will be refactored to implement non-SIMD
fallbacks, add an alternative implementation called crypto_xor_cpy(),
taking separate input and output arguments. This removes the need for
the separate memcpy().
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2017-07-24 10:28:04 +00:00
|
|
|
crypto_xor_cpy(iv, src, tmpbuf, bsize);
|
2006-12-16 01:09:02 +00:00
|
|
|
|
|
|
|
src += bsize;
|
|
|
|
} while ((nbytes -= bsize) >= bsize);
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
static int crypto_pcbc_decrypt(struct skcipher_request *req)
|
2006-12-16 01:09:02 +00:00
|
|
|
{
|
2016-11-22 12:08:27 +00:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
2019-01-04 04:16:22 +00:00
|
|
|
struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
|
2016-11-22 12:08:27 +00:00
|
|
|
struct skcipher_walk walk;
|
|
|
|
unsigned int nbytes;
|
2006-12-16 01:09:02 +00:00
|
|
|
int err;
|
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
2006-12-16 01:09:02 +00:00
|
|
|
|
2024-01-16 10:43:02 +00:00
|
|
|
while (walk.nbytes) {
|
2006-12-16 01:09:02 +00:00
|
|
|
if (walk.src.virt.addr == walk.dst.virt.addr)
|
2016-11-22 12:08:27 +00:00
|
|
|
nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
|
2019-01-04 04:16:22 +00:00
|
|
|
cipher);
|
2006-12-16 01:09:02 +00:00
|
|
|
else
|
2016-11-22 12:08:27 +00:00
|
|
|
nbytes = crypto_pcbc_decrypt_segment(req, &walk,
|
2019-01-04 04:16:22 +00:00
|
|
|
cipher);
|
2016-11-22 12:08:27 +00:00
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
2006-12-16 01:09:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
|
2006-12-16 01:09:02 +00:00
|
|
|
{
|
2016-11-22 12:08:27 +00:00
|
|
|
struct skcipher_instance *inst;
|
2007-01-01 07:37:02 +00:00
|
|
|
int err;
|
|
|
|
|
2019-12-20 05:29:40 +00:00
|
|
|
inst = skcipher_alloc_instance_simple(tmpl, tb);
|
2019-01-04 04:16:22 +00:00
|
|
|
if (IS_ERR(inst))
|
|
|
|
return PTR_ERR(inst);
|
2006-12-16 01:09:02 +00:00
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
inst->alg.encrypt = crypto_pcbc_encrypt;
|
|
|
|
inst->alg.decrypt = crypto_pcbc_decrypt;
|
2006-12-16 01:09:02 +00:00
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
err = skcipher_register_instance(tmpl, inst);
|
|
|
|
if (err)
|
2019-01-04 04:16:22 +00:00
|
|
|
inst->free(inst);
|
2019-12-20 05:29:40 +00:00
|
|
|
|
2016-11-22 12:08:27 +00:00
|
|
|
return err;
|
2006-12-16 01:09:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct crypto_template crypto_pcbc_tmpl = {
|
|
|
|
.name = "pcbc",
|
2016-11-22 12:08:27 +00:00
|
|
|
.create = crypto_pcbc_create,
|
2006-12-16 01:09:02 +00:00
|
|
|
.module = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init crypto_pcbc_module_init(void)
|
|
|
|
{
|
|
|
|
return crypto_register_template(&crypto_pcbc_tmpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit crypto_pcbc_module_exit(void)
|
|
|
|
{
|
|
|
|
crypto_unregister_template(&crypto_pcbc_tmpl);
|
|
|
|
}
|
|
|
|
|
2019-04-12 04:57:42 +00:00
|
|
|
subsys_initcall(crypto_pcbc_module_init);
|
2006-12-16 01:09:02 +00:00
|
|
|
module_exit(crypto_pcbc_module_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
2019-01-04 04:16:22 +00:00
|
|
|
MODULE_DESCRIPTION("PCBC block cipher mode of operation");
|
2014-11-25 00:32:38 +00:00
|
|
|
MODULE_ALIAS_CRYPTO("pcbc");
|
module: Convert symbol namespace to string literal
Clean up the existing export namespace code along the same lines of
commit 33def8498fdd ("treewide: Convert macro and uses of __section(foo)
to __section("foo")") and for the same reason, it is not desired for the
namespace argument to be a macro expansion itself.
Scripted using
git grep -l -e MODULE_IMPORT_NS -e EXPORT_SYMBOL_NS | while read file;
do
awk -i inplace '
/^#define EXPORT_SYMBOL_NS/ {
gsub(/__stringify\(ns\)/, "ns");
print;
next;
}
/^#define MODULE_IMPORT_NS/ {
gsub(/__stringify\(ns\)/, "ns");
print;
next;
}
/MODULE_IMPORT_NS/ {
$0 = gensub(/MODULE_IMPORT_NS\(([^)]*)\)/, "MODULE_IMPORT_NS(\"\\1\")", "g");
}
/EXPORT_SYMBOL_NS/ {
if ($0 ~ /(EXPORT_SYMBOL_NS[^(]*)\(([^,]+),/) {
if ($0 !~ /(EXPORT_SYMBOL_NS[^(]*)\(([^,]+), ([^)]+)\)/ &&
$0 !~ /(EXPORT_SYMBOL_NS[^(]*)\(\)/ &&
$0 !~ /^my/) {
getline line;
gsub(/[[:space:]]*\\$/, "");
gsub(/[[:space:]]/, "", line);
$0 = $0 " " line;
}
$0 = gensub(/(EXPORT_SYMBOL_NS[^(]*)\(([^,]+), ([^)]+)\)/,
"\\1(\\2, \"\\3\")", "g");
}
}
{ print }' $file;
done
Requested-by: Masahiro Yamada <masahiroy@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://mail.google.com/mail/u/2/#inbox/FMfcgzQXKWgMmjdFwwdsfgxzKpVHWPlc
Acked-by: Greg KH <gregkh@linuxfoundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2024-12-02 14:59:47 +00:00
|
|
|
MODULE_IMPORT_NS("CRYPTO_INTERNAL");
|