Skip to content

Commit 2e0e1f9

Browse files
ElonZhang0328gregkh
authored andcommitted
crypto: rockchip - update new iv to device in multiple operations
commit c1c214a upstream. For chain mode in cipher(eg. AES-CBC/DES-CBC), the iv is continuously updated in the operation. The new iv value should be written to device register by software. Reported-by: Eric Biggers <ebiggers@google.com> Fixes: 433cd2c ("crypto: rockchip - add crypto driver for rk3288") Cc: <stable@vger.kernel.org> # v4.5+ Signed-off-by: Zhang Zhijie <zhangzj@rock-chips.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5aabf06 commit 2e0e1f9

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎drivers/crypto/rockchip/rk3288_crypto.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ struct rk_cipher_ctx {
245245
struct rk_crypto_info *dev;
246246
unsigned int keylen;
247247
u32 mode;
248+
u8 iv[AES_BLOCK_SIZE];
248249
};
249250

250251
enum alg_type {

‎drivers/crypto/rockchip/rk3288_crypto_ablkcipher.c‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,17 @@ static void crypto_dma_start(struct rk_crypto_info *dev)
242242
static int rk_set_data_start(struct rk_crypto_info *dev)
243243
{
244244
int err;
245+
struct ablkcipher_request *req =
246+
ablkcipher_request_cast(dev->async_req);
247+
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
248+
struct rk_cipher_ctx *ctx = crypto_ablkcipher_ctx(tfm);
249+
u32 ivsize = crypto_ablkcipher_ivsize(tfm);
250+
u8 *src_last_blk = page_address(sg_page(dev->sg_src)) +
251+
dev->sg_src->offset + dev->sg_src->length - ivsize;
252+
253+
/* store the iv that need to be updated in chain mode */
254+
if (ctx->mode & RK_CRYPTO_DEC)
255+
memcpy(ctx->iv, src_last_blk, ivsize);
245256

246257
err = dev->load_data(dev, dev->sg_src, dev->sg_dst);
247258
if (!err)
@@ -286,6 +297,28 @@ static void rk_iv_copyback(struct rk_crypto_info *dev)
286297
memcpy_fromio(req->info, dev->reg + RK_CRYPTO_AES_IV_0, ivsize);
287298
}
288299

300+
static void rk_update_iv(struct rk_crypto_info *dev)
301+
{
302+
struct ablkcipher_request *req =
303+
ablkcipher_request_cast(dev->async_req);
304+
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
305+
struct rk_cipher_ctx *ctx = crypto_ablkcipher_ctx(tfm);
306+
u32 ivsize = crypto_ablkcipher_ivsize(tfm);
307+
u8 *new_iv = NULL;
308+
309+
if (ctx->mode & RK_CRYPTO_DEC) {
310+
new_iv = ctx->iv;
311+
} else {
312+
new_iv = page_address(sg_page(dev->sg_dst)) +
313+
dev->sg_dst->offset + dev->sg_dst->length - ivsize;
314+
}
315+
316+
if (ivsize == DES_BLOCK_SIZE)
317+
memcpy_toio(dev->reg + RK_CRYPTO_TDES_IV_0, new_iv, ivsize);
318+
else if (ivsize == AES_BLOCK_SIZE)
319+
memcpy_toio(dev->reg + RK_CRYPTO_AES_IV_0, new_iv, ivsize);
320+
}
321+
289322
/* return:
290323
* true some err was occurred
291324
* fault no err, continue
@@ -307,6 +340,7 @@ static int rk_ablk_rx(struct rk_crypto_info *dev)
307340
}
308341
}
309342
if (dev->left_bytes) {
343+
rk_update_iv(dev);
310344
if (dev->aligned) {
311345
if (sg_is_last(dev->sg_src)) {
312346
dev_err(dev->dev, "[%s:%d] Lack of data\n",

0 commit comments

Comments
 (0)