Commit 61b0dd66 authored by Tudor Ambarus's avatar Tudor Ambarus Committed by Herbert Xu

crypto: atmel-tdes - Set the IV after {en,de}crypt

The req->iv of the skcipher_request is expected to contain the
last ciphertext block when the {en,de}crypt operation is done.
In case of in-place decryption, copy the ciphertext in an
intermediate buffer before decryption.

This fixes the following tcrypt tests:
alg: skcipher: atmel-cbc-des encryption test failed (wrong output IV) on test vector 0, cfg="in-place"
00000000: fe dc ba 98 76 54 32 10
alg: skcipher: atmel-cbc-tdes encryption test failed (wrong output IV) on test vector 0, cfg="in-place"
00000000: 7d 33 88 93 0f 93 b2 42
Signed-off-by: default avatarTudor Ambarus <tudor.ambarus@microchip.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent d1d787bc
...@@ -81,6 +81,7 @@ struct atmel_tdes_ctx { ...@@ -81,6 +81,7 @@ struct atmel_tdes_ctx {
struct atmel_tdes_reqctx { struct atmel_tdes_reqctx {
unsigned long mode; unsigned long mode;
u8 lastc[DES_BLOCK_SIZE];
}; };
struct atmel_tdes_dma { struct atmel_tdes_dma {
...@@ -572,6 +573,30 @@ static int atmel_tdes_crypt_start(struct atmel_tdes_dev *dd) ...@@ -572,6 +573,30 @@ static int atmel_tdes_crypt_start(struct atmel_tdes_dev *dd)
return err; return err;
} }
static void
atmel_tdes_set_iv_as_last_ciphertext_block(struct atmel_tdes_dev *dd)
{
struct skcipher_request *req = dd->req;
struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req);
struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
if (req->cryptlen < ivsize)
return;
if (rctx->mode & TDES_FLAGS_ENCRYPT) {
scatterwalk_map_and_copy(req->iv, req->dst,
req->cryptlen - ivsize, ivsize, 0);
} else {
if (req->src == req->dst)
memcpy(req->iv, rctx->lastc, ivsize);
else
scatterwalk_map_and_copy(req->iv, req->src,
req->cryptlen - ivsize,
ivsize, 0);
}
}
static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err) static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
{ {
struct skcipher_request *req = dd->req; struct skcipher_request *req = dd->req;
...@@ -580,6 +605,8 @@ static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err) ...@@ -580,6 +605,8 @@ static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
dd->flags &= ~TDES_FLAGS_BUSY; dd->flags &= ~TDES_FLAGS_BUSY;
atmel_tdes_set_iv_as_last_ciphertext_block(dd);
req->base.complete(&req->base, err); req->base.complete(&req->base, err);
} }
...@@ -668,8 +695,8 @@ static int atmel_tdes_crypt_dma_stop(struct atmel_tdes_dev *dd) ...@@ -668,8 +695,8 @@ static int atmel_tdes_crypt_dma_stop(struct atmel_tdes_dev *dd)
static int atmel_tdes_crypt(struct skcipher_request *req, unsigned long mode) static int atmel_tdes_crypt(struct skcipher_request *req, unsigned long mode)
{ {
struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx( struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
crypto_skcipher_reqtfm(req)); struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx(skcipher);
struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req); struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req);
if (mode & TDES_FLAGS_CFB8) { if (mode & TDES_FLAGS_CFB8) {
...@@ -700,6 +727,15 @@ static int atmel_tdes_crypt(struct skcipher_request *req, unsigned long mode) ...@@ -700,6 +727,15 @@ static int atmel_tdes_crypt(struct skcipher_request *req, unsigned long mode)
rctx->mode = mode; rctx->mode = mode;
if (!(mode & TDES_FLAGS_ENCRYPT) && req->src == req->dst) {
unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
if (req->cryptlen >= ivsize)
scatterwalk_map_and_copy(rctx->lastc, req->src,
req->cryptlen - ivsize,
ivsize, 0);
}
return atmel_tdes_handle_queue(ctx->dd, req); return atmel_tdes_handle_queue(ctx->dd, req);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment