2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-06-18 19:07:19 +08:00
|
|
|
/*
|
|
|
|
* Shared glue code for 128bit block ciphers
|
|
|
|
*
|
2013-04-09 02:50:55 +08:00
|
|
|
* Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
|
2012-06-18 19:07:19 +08:00
|
|
|
*
|
|
|
|
* CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
|
|
|
|
* Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
|
|
|
|
* CTR part based on code (crypto/ctr.c) by:
|
|
|
|
* (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <crypto/b128ops.h>
|
2017-04-03 03:19:15 +08:00
|
|
|
#include <crypto/gf128mul.h>
|
2016-11-22 20:08:29 +08:00
|
|
|
#include <crypto/internal/skcipher.h>
|
2019-08-16 20:21:50 +08:00
|
|
|
#include <crypto/scatterwalk.h>
|
2012-06-18 19:07:19 +08:00
|
|
|
#include <crypto/xts.h>
|
|
|
|
#include <asm/crypto/glue_helper.h>
|
|
|
|
|
2018-02-20 15:48:00 +08:00
|
|
|
int glue_ecb_req_128bit(const struct common_glue_ctx *gctx,
|
|
|
|
struct skcipher_request *req)
|
|
|
|
{
|
|
|
|
void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
|
|
|
|
const unsigned int bsize = 128 / 8;
|
|
|
|
struct skcipher_walk walk;
|
|
|
|
bool fpu_enabled = false;
|
|
|
|
unsigned int nbytes;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
|
|
|
|
|
|
|
while ((nbytes = walk.nbytes)) {
|
|
|
|
const u8 *src = walk.src.virt.addr;
|
|
|
|
u8 *dst = walk.dst.virt.addr;
|
|
|
|
unsigned int func_bytes;
|
|
|
|
unsigned int i;
|
|
|
|
|
2018-02-20 15:48:27 +08:00
|
|
|
fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
|
|
|
|
&walk, fpu_enabled, nbytes);
|
2018-02-20 15:48:00 +08:00
|
|
|
for (i = 0; i < gctx->num_funcs; i++) {
|
|
|
|
func_bytes = bsize * gctx->funcs[i].num_blocks;
|
|
|
|
|
|
|
|
if (nbytes < func_bytes)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Process multi-block batch */
|
|
|
|
do {
|
|
|
|
gctx->funcs[i].fn_u.ecb(ctx, dst, src);
|
|
|
|
src += func_bytes;
|
|
|
|
dst += func_bytes;
|
|
|
|
nbytes -= func_bytes;
|
|
|
|
} while (nbytes >= func_bytes);
|
|
|
|
|
|
|
|
if (nbytes < bsize)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
glue_fpu_end(fpu_enabled);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(glue_ecb_req_128bit);
|
|
|
|
|
|
|
|
int glue_cbc_encrypt_req_128bit(const common_glue_func_t fn,
|
|
|
|
struct skcipher_request *req)
|
|
|
|
{
|
|
|
|
void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
|
|
|
|
const unsigned int bsize = 128 / 8;
|
|
|
|
struct skcipher_walk walk;
|
|
|
|
unsigned int nbytes;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
|
|
|
|
|
|
|
while ((nbytes = walk.nbytes)) {
|
|
|
|
const u128 *src = (u128 *)walk.src.virt.addr;
|
|
|
|
u128 *dst = (u128 *)walk.dst.virt.addr;
|
|
|
|
u128 *iv = (u128 *)walk.iv;
|
|
|
|
|
|
|
|
do {
|
|
|
|
u128_xor(dst, src, iv);
|
|
|
|
fn(ctx, (u8 *)dst, (u8 *)dst);
|
|
|
|
iv = dst;
|
|
|
|
src++;
|
|
|
|
dst++;
|
|
|
|
nbytes -= bsize;
|
|
|
|
} while (nbytes >= bsize);
|
|
|
|
|
|
|
|
*(u128 *)walk.iv = *iv;
|
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(glue_cbc_encrypt_req_128bit);
|
|
|
|
|
|
|
|
int glue_cbc_decrypt_req_128bit(const struct common_glue_ctx *gctx,
|
|
|
|
struct skcipher_request *req)
|
|
|
|
{
|
|
|
|
void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
|
|
|
|
const unsigned int bsize = 128 / 8;
|
|
|
|
struct skcipher_walk walk;
|
|
|
|
bool fpu_enabled = false;
|
|
|
|
unsigned int nbytes;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
|
|
|
|
|
|
|
while ((nbytes = walk.nbytes)) {
|
|
|
|
const u128 *src = walk.src.virt.addr;
|
|
|
|
u128 *dst = walk.dst.virt.addr;
|
|
|
|
unsigned int func_bytes, num_blocks;
|
|
|
|
unsigned int i;
|
|
|
|
u128 last_iv;
|
|
|
|
|
2018-02-20 15:48:27 +08:00
|
|
|
fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
|
|
|
|
&walk, fpu_enabled, nbytes);
|
2018-02-20 15:48:00 +08:00
|
|
|
/* Start of the last block. */
|
|
|
|
src += nbytes / bsize - 1;
|
|
|
|
dst += nbytes / bsize - 1;
|
|
|
|
|
|
|
|
last_iv = *src;
|
|
|
|
|
|
|
|
for (i = 0; i < gctx->num_funcs; i++) {
|
|
|
|
num_blocks = gctx->funcs[i].num_blocks;
|
|
|
|
func_bytes = bsize * num_blocks;
|
|
|
|
|
|
|
|
if (nbytes < func_bytes)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Process multi-block batch */
|
|
|
|
do {
|
|
|
|
src -= num_blocks - 1;
|
|
|
|
dst -= num_blocks - 1;
|
|
|
|
|
2019-11-27 14:08:02 +08:00
|
|
|
gctx->funcs[i].fn_u.cbc(ctx, (u8 *)dst,
|
|
|
|
(const u8 *)src);
|
2018-02-20 15:48:00 +08:00
|
|
|
|
|
|
|
nbytes -= func_bytes;
|
|
|
|
if (nbytes < bsize)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
u128_xor(dst, dst, --src);
|
|
|
|
dst--;
|
|
|
|
} while (nbytes >= func_bytes);
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
u128_xor(dst, dst, (u128 *)walk.iv);
|
|
|
|
*(u128 *)walk.iv = last_iv;
|
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
glue_fpu_end(fpu_enabled);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(glue_cbc_decrypt_req_128bit);
|
|
|
|
|
|
|
|
int glue_ctr_req_128bit(const struct common_glue_ctx *gctx,
|
|
|
|
struct skcipher_request *req)
|
|
|
|
{
|
|
|
|
void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
|
|
|
|
const unsigned int bsize = 128 / 8;
|
|
|
|
struct skcipher_walk walk;
|
|
|
|
bool fpu_enabled = false;
|
|
|
|
unsigned int nbytes;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
|
|
|
|
|
|
|
while ((nbytes = walk.nbytes) >= bsize) {
|
|
|
|
const u128 *src = walk.src.virt.addr;
|
|
|
|
u128 *dst = walk.dst.virt.addr;
|
|
|
|
unsigned int func_bytes, num_blocks;
|
|
|
|
unsigned int i;
|
|
|
|
le128 ctrblk;
|
|
|
|
|
2018-02-20 15:48:27 +08:00
|
|
|
fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
|
|
|
|
&walk, fpu_enabled, nbytes);
|
2018-02-20 15:48:00 +08:00
|
|
|
|
|
|
|
be128_to_le128(&ctrblk, (be128 *)walk.iv);
|
|
|
|
|
|
|
|
for (i = 0; i < gctx->num_funcs; i++) {
|
|
|
|
num_blocks = gctx->funcs[i].num_blocks;
|
|
|
|
func_bytes = bsize * num_blocks;
|
|
|
|
|
|
|
|
if (nbytes < func_bytes)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Process multi-block batch */
|
|
|
|
do {
|
2019-11-27 14:08:02 +08:00
|
|
|
gctx->funcs[i].fn_u.ctr(ctx, (u8 *)dst,
|
|
|
|
(const u8 *)src,
|
|
|
|
&ctrblk);
|
2018-02-20 15:48:00 +08:00
|
|
|
src += num_blocks;
|
|
|
|
dst += num_blocks;
|
|
|
|
nbytes -= func_bytes;
|
|
|
|
} while (nbytes >= func_bytes);
|
|
|
|
|
|
|
|
if (nbytes < bsize)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
le128_to_be128((be128 *)walk.iv, &ctrblk);
|
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
glue_fpu_end(fpu_enabled);
|
|
|
|
|
|
|
|
if (nbytes) {
|
|
|
|
le128 ctrblk;
|
|
|
|
u128 tmp;
|
|
|
|
|
|
|
|
be128_to_le128(&ctrblk, (be128 *)walk.iv);
|
|
|
|
memcpy(&tmp, walk.src.virt.addr, nbytes);
|
2019-11-27 14:08:02 +08:00
|
|
|
gctx->funcs[gctx->num_funcs - 1].fn_u.ctr(ctx, (u8 *)&tmp,
|
|
|
|
(const u8 *)&tmp,
|
2018-02-20 15:48:00 +08:00
|
|
|
&ctrblk);
|
|
|
|
memcpy(walk.dst.virt.addr, &tmp, nbytes);
|
|
|
|
le128_to_be128((be128 *)walk.iv, &ctrblk);
|
|
|
|
|
|
|
|
err = skcipher_walk_done(&walk, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(glue_ctr_req_128bit);
|
|
|
|
|
2016-11-22 20:08:29 +08:00
|
|
|
static unsigned int __glue_xts_req_128bit(const struct common_glue_ctx *gctx,
|
|
|
|
void *ctx,
|
|
|
|
struct skcipher_walk *walk)
|
|
|
|
{
|
|
|
|
const unsigned int bsize = 128 / 8;
|
|
|
|
unsigned int nbytes = walk->nbytes;
|
|
|
|
u128 *src = walk->src.virt.addr;
|
|
|
|
u128 *dst = walk->dst.virt.addr;
|
|
|
|
unsigned int num_blocks, func_bytes;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* Process multi-block batch */
|
|
|
|
for (i = 0; i < gctx->num_funcs; i++) {
|
|
|
|
num_blocks = gctx->funcs[i].num_blocks;
|
|
|
|
func_bytes = bsize * num_blocks;
|
|
|
|
|
|
|
|
if (nbytes >= func_bytes) {
|
|
|
|
do {
|
2019-11-27 14:08:02 +08:00
|
|
|
gctx->funcs[i].fn_u.xts(ctx, (u8 *)dst,
|
|
|
|
(const u8 *)src,
|
2016-11-22 20:08:29 +08:00
|
|
|
walk->iv);
|
|
|
|
|
|
|
|
src += num_blocks;
|
|
|
|
dst += num_blocks;
|
|
|
|
nbytes -= func_bytes;
|
|
|
|
} while (nbytes >= func_bytes);
|
|
|
|
|
|
|
|
if (nbytes < bsize)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
int glue_xts_req_128bit(const struct common_glue_ctx *gctx,
|
|
|
|
struct skcipher_request *req,
|
|
|
|
common_glue_func_t tweak_fn, void *tweak_ctx,
|
2019-08-16 20:21:50 +08:00
|
|
|
void *crypt_ctx, bool decrypt)
|
2016-11-22 20:08:29 +08:00
|
|
|
{
|
2019-08-16 20:21:50 +08:00
|
|
|
const bool cts = (req->cryptlen % XTS_BLOCK_SIZE);
|
2016-11-22 20:08:29 +08:00
|
|
|
const unsigned int bsize = 128 / 8;
|
2019-08-16 20:21:50 +08:00
|
|
|
struct skcipher_request subreq;
|
2016-11-22 20:08:29 +08:00
|
|
|
struct skcipher_walk walk;
|
|
|
|
bool fpu_enabled = false;
|
2019-08-16 20:21:50 +08:00
|
|
|
unsigned int nbytes, tail;
|
2016-11-22 20:08:29 +08:00
|
|
|
int err;
|
|
|
|
|
2019-08-16 20:21:50 +08:00
|
|
|
if (req->cryptlen < XTS_BLOCK_SIZE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (unlikely(cts)) {
|
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
|
|
|
|
tail = req->cryptlen % XTS_BLOCK_SIZE + XTS_BLOCK_SIZE;
|
|
|
|
|
|
|
|
skcipher_request_set_tfm(&subreq, tfm);
|
|
|
|
skcipher_request_set_callback(&subreq,
|
|
|
|
crypto_skcipher_get_flags(tfm),
|
|
|
|
NULL, NULL);
|
|
|
|
skcipher_request_set_crypt(&subreq, req->src, req->dst,
|
|
|
|
req->cryptlen - tail, req->iv);
|
|
|
|
req = &subreq;
|
|
|
|
}
|
|
|
|
|
2016-11-22 20:08:29 +08:00
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
|
|
|
nbytes = walk.nbytes;
|
2019-08-16 20:21:50 +08:00
|
|
|
if (err)
|
2016-11-22 20:08:29 +08:00
|
|
|
return err;
|
|
|
|
|
|
|
|
/* set minimum length to bsize, for tweak_fn */
|
2018-02-20 15:48:27 +08:00
|
|
|
fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
|
|
|
|
&walk, fpu_enabled,
|
|
|
|
nbytes < bsize ? bsize : nbytes);
|
2016-11-22 20:08:29 +08:00
|
|
|
|
|
|
|
/* calculate first value of T */
|
|
|
|
tweak_fn(tweak_ctx, walk.iv, walk.iv);
|
|
|
|
|
|
|
|
while (nbytes) {
|
|
|
|
nbytes = __glue_xts_req_128bit(gctx, crypt_ctx, &walk);
|
|
|
|
|
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
|
|
|
nbytes = walk.nbytes;
|
|
|
|
}
|
|
|
|
|
2019-08-16 20:21:50 +08:00
|
|
|
if (unlikely(cts)) {
|
|
|
|
u8 *next_tweak, *final_tweak = req->iv;
|
|
|
|
struct scatterlist *src, *dst;
|
|
|
|
struct scatterlist s[2], d[2];
|
|
|
|
le128 b[2];
|
|
|
|
|
|
|
|
dst = src = scatterwalk_ffwd(s, req->src, req->cryptlen);
|
|
|
|
if (req->dst != req->src)
|
|
|
|
dst = scatterwalk_ffwd(d, req->dst, req->cryptlen);
|
|
|
|
|
|
|
|
if (decrypt) {
|
|
|
|
next_tweak = memcpy(b, req->iv, XTS_BLOCK_SIZE);
|
|
|
|
gf128mul_x_ble(b, b);
|
|
|
|
} else {
|
|
|
|
next_tweak = req->iv;
|
|
|
|
}
|
|
|
|
|
|
|
|
skcipher_request_set_crypt(&subreq, src, dst, XTS_BLOCK_SIZE,
|
|
|
|
next_tweak);
|
|
|
|
|
|
|
|
err = skcipher_walk_virt(&walk, req, false) ?:
|
|
|
|
skcipher_walk_done(&walk,
|
|
|
|
__glue_xts_req_128bit(gctx, crypt_ctx, &walk));
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
scatterwalk_map_and_copy(b, dst, 0, XTS_BLOCK_SIZE, 0);
|
|
|
|
memcpy(b + 1, b, tail - XTS_BLOCK_SIZE);
|
|
|
|
scatterwalk_map_and_copy(b, src, XTS_BLOCK_SIZE,
|
|
|
|
tail - XTS_BLOCK_SIZE, 0);
|
|
|
|
scatterwalk_map_and_copy(b, dst, 0, tail, 1);
|
|
|
|
|
|
|
|
skcipher_request_set_crypt(&subreq, dst, dst, XTS_BLOCK_SIZE,
|
|
|
|
final_tweak);
|
|
|
|
|
|
|
|
err = skcipher_walk_virt(&walk, req, false) ?:
|
|
|
|
skcipher_walk_done(&walk,
|
|
|
|
__glue_xts_req_128bit(gctx, crypt_ctx, &walk));
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2016-11-22 20:08:29 +08:00
|
|
|
glue_fpu_end(fpu_enabled);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(glue_xts_req_128bit);
|
|
|
|
|
2019-11-27 14:08:02 +08:00
|
|
|
void glue_xts_crypt_128bit_one(const void *ctx, u8 *dst, const u8 *src,
|
|
|
|
le128 *iv, common_glue_func_t fn)
|
2013-04-09 02:50:55 +08:00
|
|
|
{
|
|
|
|
le128 ivblk = *iv;
|
|
|
|
|
|
|
|
/* generate next IV */
|
2017-04-03 03:19:15 +08:00
|
|
|
gf128mul_x_ble(iv, &ivblk);
|
2013-04-09 02:50:55 +08:00
|
|
|
|
|
|
|
/* CC <- T xor C */
|
2019-11-27 14:08:02 +08:00
|
|
|
u128_xor((u128 *)dst, (const u128 *)src, (u128 *)&ivblk);
|
2013-04-09 02:50:55 +08:00
|
|
|
|
|
|
|
/* PP <- D(Key2,CC) */
|
2019-11-27 14:08:02 +08:00
|
|
|
fn(ctx, dst, dst);
|
2013-04-09 02:50:55 +08:00
|
|
|
|
|
|
|
/* P <- T xor PP */
|
2019-11-27 14:08:02 +08:00
|
|
|
u128_xor((u128 *)dst, (u128 *)dst, (u128 *)&ivblk);
|
2013-04-09 02:50:55 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit_one);
|
|
|
|
|
2012-06-18 19:07:19 +08:00
|
|
|
MODULE_LICENSE("GPL");
|