diff mbox

crypto: algif_skcipher - initialize upon init request

Message ID 2770903.h32OWUVXRy@tachyon.chronox.de (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show

Commit Message

Stephan Mueller Nov. 30, 2014, 9:55 a.m. UTC
When using the algif_skcipher, the following call sequence causess a
re-initialization:

1. sendmsg with ALG_SET_OP and iov == NULL, iovlen == 0 (i.e
initializing the cipher, but not sending data)

2. sendmsg with msg->msg-controllen == 0 and iov != NULL (using the initalized
cipher handle by sending data)

In step 2, the cipher operation type (encryption or decryption) is reset
to always decryption, because the local variable of enc is put into
ctx->enc as ctx->user is still zero.

The same applies when all send data is processed and ctx->used falls to
zero followed by user space to send new data.

This patch changes the behavior to only reset the cipher operation type
(and the IV) if such configuration request is received.

Signed-off-by: Stephan Mueller <smueller@chronox.de>
---
 crypto/algif_skcipher.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Herbert Xu Dec. 2, 2014, 2:57 p.m. UTC | #1
On Sun, Nov 30, 2014 at 10:55:26AM +0100, Stephan Mueller wrote:
> When using the algif_skcipher, the following call sequence causess a
> re-initialization:
> 
> 1. sendmsg with ALG_SET_OP and iov == NULL, iovlen == 0 (i.e
> initializing the cipher, but not sending data)
> 
> 2. sendmsg with msg->msg-controllen == 0 and iov != NULL (using the initalized
> cipher handle by sending data)
> 
> In step 2, the cipher operation type (encryption or decryption) is reset
> to always decryption, because the local variable of enc is put into
> ctx->enc as ctx->user is still zero.
> 
> The same applies when all send data is processed and ctx->used falls to
> zero followed by user space to send new data.
> 
> This patch changes the behavior to only reset the cipher operation type
> (and the IV) if such configuration request is received.
> 
> Signed-off-by: Stephan Mueller <smueller@chronox.de>

Patch applied.
diff mbox

Patch

diff --git a/crypto/algif_skcipher.c b/crypto/algif_skcipher.c
index f80e652..1f157c7 100644
--- a/crypto/algif_skcipher.c
+++ b/crypto/algif_skcipher.c
@@ -251,6 +251,7 @@  static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
 	struct af_alg_control con = {};
 	long copied = 0;
 	bool enc = 0;
+	bool init = 0;
 	int err;
 	int i;
 
@@ -259,6 +260,7 @@  static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
 		if (err)
 			return err;
 
+		init = 1;
 		switch (con.op) {
 		case ALG_OP_ENCRYPT:
 			enc = 1;
@@ -280,7 +282,7 @@  static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
 	if (!ctx->more && ctx->used)
 		goto unlock;
 
-	if (!ctx->used) {
+	if (init) {
 		ctx->enc = enc;
 		if (con.iv)
 			memcpy(ctx->iv, con.iv->iv, ivsize);