From 34794954a4982f1a778f03b12d19baf95ef42bd2 Mon Sep 17 00:00:00 2001
From: Nikos Mavrogiannopoulos <nmav@gnutls.org>
Date: Wed, 23 Nov 2011 23:47:11 +0100
Subject: [PATCH 148/291] cryptodev: corrected

---
 drivers/staging/crypto/cryptodev/cryptodev_auth.c  |  8 +++----
 .../crypto/cryptodev/examples/cipher-aead.c        | 27 ++++++++++++++++++----
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/drivers/staging/crypto/cryptodev/cryptodev_auth.c b/drivers/staging/crypto/cryptodev/cryptodev_auth.c
index fdbfff675efc..6e7088a2e094 100644
--- a/drivers/staging/crypto/cryptodev/cryptodev_auth.c
+++ b/drivers/staging/crypto/cryptodev/cryptodev_auth.c
@@ -70,7 +70,7 @@ static int get_userbuf_aead(struct csession *ses, struct kernel_crypt_auth_op *k
 		return -EINVAL;
 	}
 
-	if (caop->auth_len != 0)
+	if (caop->auth_len > 0)
 		auth_pagecount = PAGECOUNT(caop->auth_src, caop->auth_len);
 
 	dst_pagecount = PAGECOUNT(caop->dst, kcaop->dst_len);
@@ -90,20 +90,20 @@ static int get_userbuf_aead(struct csession *ses, struct kernel_crypt_auth_op *k
 			return -EINVAL;
 		}
 		(*auth_sg) = ses->sg;
+		(*dst_sg) = ses->sg + auth_pagecount;
 	} else {
 		(*auth_sg) = NULL;
+		(*dst_sg) = ses->sg;
 	}
 
 	rc = __get_userbuf(caop->dst, kcaop->dst_len, 1, dst_pagecount,
-	                   ses->pages + auth_pagecount, ses->sg, kcaop->task, kcaop->mm);
+	                   ses->pages + auth_pagecount, *dst_sg, kcaop->task, kcaop->mm);
 	if (unlikely(rc)) {
 		release_user_pages(ses->pages, auth_pagecount);
 		dprintk(1, KERN_ERR,
 			"failed to get user pages for data input\n");
 		return -EINVAL;
 	}
-	
-	(*dst_sg) = ses->sg + auth_pagecount;
 
 	return 0;
 }
diff --git a/drivers/staging/crypto/cryptodev/examples/cipher-aead.c b/drivers/staging/crypto/cryptodev/examples/cipher-aead.c
index a6c45567668e..d04c6af537ce 100644
--- a/drivers/staging/crypto/cryptodev/examples/cipher-aead.c
+++ b/drivers/staging/crypto/cryptodev/examples/cipher-aead.c
@@ -14,13 +14,14 @@
 #include <crypto/cryptodev.h>
 
 #define	DATA_SIZE	(8*1024+11)
+#define AUTH_SIZE       31
 #define	BLOCK_SIZE	16
 #define	KEY_SIZE	16
 
 #define MAC_SIZE 20 /* SHA1 */
 
 static int
-get_sha1_hmac(int cfd, void* key, int key_size, void* data, int data_size, void* mac)
+get_sha1_hmac(int cfd, void* key, int key_size, void* data1, int data1_size, void* data2, int data2_size, void* mac)
 {
 	struct session_op sess;
 	struct crypt_op cryp;
@@ -40,12 +41,26 @@ get_sha1_hmac(int cfd, void* key, int key_size, void* data, int data_size, void*
 
 	/* Encrypt data.in to data.encrypted */
 	cryp.ses = sess.ses;
-	cryp.len = data_size;
-	cryp.src = data;
+	cryp.len = data1_size;
+	cryp.src = data1;
 	cryp.dst = NULL;
 	cryp.iv = NULL;
 	cryp.mac = mac;
 	cryp.op = COP_ENCRYPT;
+	cryp.flags = COP_FLAG_UPDATE;
+	if (ioctl(cfd, CIOCCRYPT, &cryp)) {
+		perror("ioctl(CIOCCRYPT)");
+		return 1;
+	}
+
+	cryp.ses = sess.ses;
+	cryp.len = data2_size;
+	cryp.src = data2;
+	cryp.dst = NULL;
+	cryp.iv = NULL;
+	cryp.mac = mac;
+	cryp.op = COP_ENCRYPT;
+	cryp.flags = COP_FLAG_FINAL;
 	if (ioctl(cfd, CIOCCRYPT, &cryp)) {
 		perror("ioctl(CIOCCRYPT)");
 		return 1;
@@ -77,6 +92,7 @@ test_crypto(int cfd)
 	char ciphertext_raw[DATA_SIZE + 63], *ciphertext;
 	char iv[BLOCK_SIZE];
 	char key[KEY_SIZE];
+	char auth[AUTH_SIZE];
 	unsigned char sha1mac[20];
 	int pad, i;
 
@@ -93,6 +109,7 @@ test_crypto(int cfd)
 
 	memset(key,0x33,  sizeof(key));
 	memset(iv, 0x03,  sizeof(iv));
+	memset(auth, 0xf1,  sizeof(auth));
 
 	/* Get crypto session for AES128 */
 	sess.cipher = CRYPTO_AES_CBC;
@@ -125,7 +142,7 @@ test_crypto(int cfd)
 #endif
 	memset(plaintext, 0x15, DATA_SIZE);
 
-	if (get_sha1_hmac(cfd, sess.mackey, sess.mackeylen, plaintext, DATA_SIZE, sha1mac) != 0) {
+	if (get_sha1_hmac(cfd, sess.mackey, sess.mackeylen, auth, sizeof(auth), plaintext, DATA_SIZE, sha1mac) != 0) {
 		fprintf(stderr, "SHA1 MAC failed\n");
 		return 1;
 	}
@@ -134,6 +151,8 @@ test_crypto(int cfd)
 
 	/* Encrypt data.in to data.encrypted */
 	cao.ses = sess.ses;
+	cao.auth_src = auth;
+	cao.auth_len = sizeof(auth);
 	cao.len = DATA_SIZE;
 	cao.src = plaintext;
 	cao.dst = ciphertext;
-- 
2.1.0

