From be587ff933fe6cc856a9f0abc675c39bea68e8de Mon Sep 17 00:00:00 2001
From: Phil Sutter <phil.sutter@viprinet.com>
Date: Mon, 17 Jan 2011 18:29:37 +0100
Subject: [PATCH 126/291] cryptodev: export

Signed-off-by: Nikos Mavrogiannopoulos <nmav@gnutls.org>
---
 drivers/staging/crypto/cryptodev/cryptodev.h       |  4 +-
 drivers/staging/crypto/cryptodev/cryptodev_int.h   |  2 -
 drivers/staging/crypto/cryptodev/cryptodev_main.c  |  4 +-
 .../crypto/cryptodev/examples/async_cipher.c       | 45 ++++++++++++++++++----
 .../crypto/cryptodev/examples/async_speed.c        | 25 ++++++++++--
 drivers/staging/crypto/cryptodev/examples/cipher.c | 28 ++++++++++----
 drivers/staging/crypto/cryptodev/examples/speed.c  | 15 +++++++-
 7 files changed, 98 insertions(+), 25 deletions(-)

diff --git a/drivers/staging/crypto/cryptodev/cryptodev.h b/drivers/staging/crypto/cryptodev/cryptodev.h
index 9cb2764ea4d6..c50505b84210 100644
--- a/drivers/staging/crypto/cryptodev/cryptodev.h
+++ b/drivers/staging/crypto/cryptodev/cryptodev.h
@@ -84,8 +84,6 @@ struct session_op {
 	__u8	__user *mackey;
 
 	__u32	ses;		/* session identifier */
-
-	__u16	alignmask;	/* alignment constraints */
 };
 
 struct session_info_op {
@@ -96,6 +94,8 @@ struct session_info_op {
 		char cra_name[CRYPTODEV_MAX_ALG_NAME];
 		char cra_driver_name[CRYPTODEV_MAX_ALG_NAME];
 	} cipher_info, hash_info;
+
+	__u16	alignmask;	/* alignment constraints */
 };
 
 #define	COP_ENCRYPT	0
diff --git a/drivers/staging/crypto/cryptodev/cryptodev_int.h b/drivers/staging/crypto/cryptodev/cryptodev_int.h
index 4d9d6f6479ab..4be66ecfbc94 100644
--- a/drivers/staging/crypto/cryptodev/cryptodev_int.h
+++ b/drivers/staging/crypto/cryptodev/cryptodev_int.h
@@ -103,8 +103,6 @@ struct compat_session_op {
 	compat_uptr_t	mackey;		/* pointer to mac key data */
 
 	uint32_t	ses;		/* session identifier */
-
-	uint16_t	alignmask;	/* alignment constraints */
 };
 
 /* input of CIOCCRYPT */
diff --git a/drivers/staging/crypto/cryptodev/cryptodev_main.c b/drivers/staging/crypto/cryptodev/cryptodev_main.c
index 17155bf3377f..871690b89071 100644
--- a/drivers/staging/crypto/cryptodev/cryptodev_main.c
+++ b/drivers/staging/crypto/cryptodev/cryptodev_main.c
@@ -291,7 +291,7 @@ crypto_create_session(struct fcrypt *fcr, struct session_op *sop)
 		}
 	}
 
-	sop->alignmask = ses_new->alignmask = max(ses_new->cdata.alignmask,
+	ses_new->alignmask = max(ses_new->cdata.alignmask,
 	                                          ses_new->hdata.alignmask);
 	dprintk(2, KERN_DEBUG, "%s: got alignmask %d\n", __func__, ses_new->alignmask);
 
@@ -1036,6 +1036,8 @@ static int get_session_info(struct fcrypt *fcr, struct session_info_op *siop)
 				crypto_ahash_tfm(ses_ptr->hdata.async.s));
 	}
 
+	siop->alignmask = ses_ptr->alignmask;
+
 	mutex_unlock(&ses_ptr->sem);
 	return 0;
 }
diff --git a/drivers/staging/crypto/cryptodev/examples/async_cipher.c b/drivers/staging/crypto/cryptodev/examples/async_cipher.c
index 314c36a00816..e89c7f2137f5 100644
--- a/drivers/staging/crypto/cryptodev/examples/async_cipher.c
+++ b/drivers/staging/crypto/cryptodev/examples/async_cipher.c
@@ -28,6 +28,9 @@ test_crypto(int cfd)
 	char key[KEY_SIZE];
 
 	struct session_op sess;
+#ifdef CIOCGSESSINFO
+	struct session_info_op siop;
+#endif
 	struct crypt_op cryp;
 
 	printf("running %s\n", __func__);
@@ -49,9 +52,18 @@ test_crypto(int cfd)
 
 	printf("%s: got the session\n", __func__);
 
-
-	plaintext = (char *)(((unsigned long)plaintext_raw + sess.alignmask) & ~sess.alignmask);
-	ciphertext = (char *)(((unsigned long)ciphertext_raw + sess.alignmask) & ~sess.alignmask);
+#ifdef CIOCGSESSINFO
+	siop.ses = sess.ses;
+	if (ioctl(cfd, CIOCGSESSINFO, &siop)) {
+		perror("ioctl(CIOCGSESSINFO)");
+		return 1;
+	}
+	plaintext = (char *)(((unsigned long)plaintext_raw + siop.alignmask) & ~siop.alignmask);
+	ciphertext = (char *)(((unsigned long)ciphertext_raw + siop.alignmask) & ~siop.alignmask);
+#else
+	plaintext = plaintext_raw;
+	ciphertext = ciphertext_raw;
+#endif
 	memset(plaintext, 0x15, DATA_SIZE);
 
 	/* Encrypt data.in to data.encrypted */
@@ -122,6 +134,9 @@ static int test_aes(int cfd)
 	char key2[KEY_SIZE];
 
 	struct session_op sess1, sess2;
+#ifdef CIOCGSESSINFO
+	struct session_info_op siop1, siop2;
+#endif
 	struct crypt_op cryp1, cryp2;
 
 	memset(&sess1, 0, sizeof(sess1));
@@ -137,8 +152,16 @@ static int test_aes(int cfd)
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
-
-	plaintext1 = (char *)(((unsigned long)plaintext1_raw + sess1.alignmask) & ~sess1.alignmask);
+#ifdef CIOCGSESSINFO
+	siop1.ses = sess1.ses;
+	if (ioctl(cfd, CIOCGSESSINFO, &siop1)) {
+		perror("ioctl(CIOCGSESSINFO)");
+		return 1;
+	}
+	plaintext1 = (char *)(((unsigned long)plaintext1_raw + siop1.alignmask) & ~siop1.alignmask);
+#else
+	plaintext1 = plaintext1_raw;
+#endif
 	memset(plaintext1, 0x0, BLOCK_SIZE);
 
 	memset(iv1, 0x0, sizeof(iv1));
@@ -152,8 +175,16 @@ static int test_aes(int cfd)
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
-
-	plaintext2 = (char *)(((unsigned long)plaintext2_raw + sess2.alignmask) & ~sess2.alignmask);
+#ifdef CIOCGSESSINFO
+	siop2.ses = sess2.ses;
+	if (ioctl(cfd, CIOCGSESSINFO, &siop2)) {
+		perror("ioctl(CIOCGSESSINFO)");
+		return 1;
+	}
+	plaintext2 = (char *)(((unsigned long)plaintext2_raw + siop2.alignmask) & ~siop2.alignmask);
+#else
+	plaintext2 = plaintext2_raw;
+#endif
 	memcpy(plaintext2, plaintext2_data, BLOCK_SIZE);
 
 	/* Encrypt data.in to data.encrypted */
diff --git a/drivers/staging/crypto/cryptodev/examples/async_speed.c b/drivers/staging/crypto/cryptodev/examples/async_speed.c
index 940d923f4f22..4aea5e386533 100644
--- a/drivers/staging/crypto/cryptodev/examples/async_speed.c
+++ b/drivers/staging/crypto/cryptodev/examples/async_speed.c
@@ -144,8 +144,11 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
 
 int main(void)
 {
-	int fd, i, fdc = -1;
+	int fd, i, fdc = -1, alignmask = 0;
 	struct session_op sess;
+#ifdef CIOCGSESSINFO
+	struct session_info_op siop;
+#endif
 	char keybuf[32];
 
 	signal(SIGALRM, alarm_handler);
@@ -168,9 +171,17 @@ int main(void)
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
+#ifdef CIOCGSESSINFO
+	siop.ses = sess.ses;
+	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
+		perror("ioctl(CIOCGSESSINFO)");
+		return 1;
+	}
+	alignmask = siop.alignmask;
+#endif
 
 	for (i = 256; i <= (64 * 4096); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, sess.alignmask))
+		if (encrypt_data(&sess, fdc, i, alignmask))
 			break;
 	}
 
@@ -184,9 +195,17 @@ int main(void)
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
+#ifdef CIOCGSESSINFO
+	siop.ses = sess.ses;
+	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
+		perror("ioctl(CIOCGSESSINFO)");
+		return 1;
+	}
+	alignmask = siop.alignmask;
+#endif
 
 	for (i = 256; i <= (64 * 1024); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, sess.alignmask))
+		if (encrypt_data(&sess, fdc, i, alignmask))
 			break;
 	}
 
diff --git a/drivers/staging/crypto/cryptodev/examples/cipher.c b/drivers/staging/crypto/cryptodev/examples/cipher.c
index 9f19c5efee50..007d3fefa020 100644
--- a/drivers/staging/crypto/cryptodev/examples/cipher.c
+++ b/drivers/staging/crypto/cryptodev/examples/cipher.c
@@ -53,10 +53,13 @@ test_crypto(int cfd)
 	}
 	printf("requested cipher CRYPTO_AES_CBC, got %s with driver %s\n",
 			siop.cipher_info.cra_name, siop.cipher_info.cra_driver_name);
-#endif
 
-	plaintext = (char *)(((unsigned long)plaintext_raw + sess.alignmask) & ~sess.alignmask);
-	ciphertext = (char *)(((unsigned long)ciphertext_raw + sess.alignmask) & ~sess.alignmask);
+	plaintext = (char *)(((unsigned long)plaintext_raw + siop.alignmask) & ~siop.alignmask);
+	ciphertext = (char *)(((unsigned long)ciphertext_raw + siop.alignmask) & ~siop.alignmask);
+#else
+	plaintext = plaintext_raw;
+	ciphertext = ciphertext_raw;
+#endif
 	memset(plaintext, 0x15, DATA_SIZE);
 
 	/* Encrypt data.in to data.encrypted */
@@ -163,9 +166,16 @@ static int test_aes(int cfd)
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
-
-	plaintext1 = (char *)(((unsigned long)plaintext1_raw + sess.alignmask) & ~sess.alignmask);
-
+#ifdef CIOCGSESSINFO
+	siop.ses = sess.ses;
+	if (ioctl(cfd, CIOCGSESSINFO, &siop)) {
+		perror("ioctl(CIOCGSESSINFO)");
+		return 1;
+	}
+	plaintext1 = (char *)(((unsigned long)plaintext1_raw + siop.alignmask) & ~siop.alignmask);
+#else
+	plaintext1 = plaintext1_raw;
+#endif
 	memset(plaintext1, 0x0, BLOCK_SIZE);
 	memset(iv1, 0x0, sizeof(iv1));
 
@@ -210,9 +220,11 @@ static int test_aes(int cfd)
 	}
 	printf("requested cipher CRYPTO_AES_CBC, got %s with driver %s\n",
 			siop.cipher_info.cra_name, siop.cipher_info.cra_driver_name);
-#endif
 
-	plaintext2 = (char *)(((unsigned long)plaintext2_raw + sess.alignmask) & ~sess.alignmask);
+	plaintext2 = (char *)(((unsigned long)plaintext2_raw + siop.alignmask) & ~siop.alignmask);
+#else
+	plaintext2 = plaintext2_raw;
+#endif
 	memcpy(plaintext2, plaintext2_data, BLOCK_SIZE);
 
 	/* Encrypt data.in to data.encrypted */
diff --git a/drivers/staging/crypto/cryptodev/examples/speed.c b/drivers/staging/crypto/cryptodev/examples/speed.c
index 38b3821681a5..e2985fffb8de 100644
--- a/drivers/staging/crypto/cryptodev/examples/speed.c
+++ b/drivers/staging/crypto/cryptodev/examples/speed.c
@@ -129,8 +129,11 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
 
 int main(int argc, char** argv)
 {
-	int fd, i, fdc = -1;
+	int fd, i, fdc = -1, alignmask = 0;
 	struct session_op sess;
+#ifdef CIOCGSESSINFO
+	struct session_info_op siop;
+#endif
 	char keybuf[32];
 
 	signal(SIGALRM, alarm_handler);
@@ -163,9 +166,17 @@ int main(int argc, char** argv)
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
+#ifdef CIOCGSESSINFO
+	siop.ses = sess.ses;
+	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
+		perror("ioctl(CIOCGSESSINFO)");
+		return 1;
+	}
+	alignmask = siop.alignmask;
+#endif
 
 	for (i = 256; i <= (64 * 4096); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, sess.alignmask))
+		if (encrypt_data(&sess, fdc, i, alignmask))
 			break;
 	}
 
-- 
2.1.0

