From a7c80374852c5725fbe341fe1bd77815c70e9194 Mon Sep 17 00:00:00 2001
From: Phil Sutter <phil.sutter@viprinet.com>
Date: Thu, 4 Nov 2010 17:31:16 +0100
Subject: [PATCH 113/291] cryptodev: examples:

For now, only cipher tests were changed. Maybe the same applies to
hashes (not so much because of performance, but certain engines may
crash on unaligned data).

Signed-off-by: Nikos Mavrogiannopoulos <nmav@gnutls.org>
---
 .../crypto/cryptodev/examples/async_cipher.c       | 40 ++++++++++++---------
 .../crypto/cryptodev/examples/async_speed.c        | 19 +++++++---
 drivers/staging/crypto/cryptodev/examples/cipher.c | 42 +++++++++++++---------
 drivers/staging/crypto/cryptodev/examples/speed.c  | 23 ++++++++----
 4 files changed, 79 insertions(+), 45 deletions(-)

diff --git a/drivers/staging/crypto/cryptodev/examples/async_cipher.c b/drivers/staging/crypto/cryptodev/examples/async_cipher.c
index aae937908210..1b58e1b2280d 100644
--- a/drivers/staging/crypto/cryptodev/examples/async_cipher.c
+++ b/drivers/staging/crypto/cryptodev/examples/async_cipher.c
@@ -22,8 +22,8 @@
 static int
 test_crypto(int cfd)
 {
-	char plaintext[DATA_SIZE];
-	char ciphertext[DATA_SIZE];
+	char plaintext_raw[DATA_SIZE + 63], *plaintext;
+	char ciphertext_raw[DATA_SIZE + 63], *ciphertext;
 	char iv[BLOCK_SIZE];
 	char key[KEY_SIZE];
 
@@ -35,7 +35,6 @@ test_crypto(int cfd)
 	memset(&sess, 0, sizeof(sess));
 	memset(&cryp, 0, sizeof(cryp));
 
-	memset(plaintext, 0x15,  sizeof(plaintext));
 	memset(key, 0x33,  sizeof(key));
 	memset(iv, 0x03,  sizeof(iv));
 
@@ -51,9 +50,13 @@ 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);
+	memset(plaintext, 0x15, DATA_SIZE);
+
 	/* Encrypt data.in to data.encrypted */
 	cryp.ses = sess.ses;
-	cryp.len = sizeof(plaintext);
+	cryp.len = DATA_SIZE;
 	cryp.src = plaintext;
 	cryp.dst = ciphertext;
 	cryp.iv = iv;
@@ -78,7 +81,7 @@ test_crypto(int cfd)
 
 	/* Decrypt data.encrypted to data.decrypted */
 	cryp.ses = sess.ses;
-	cryp.len = sizeof(plaintext);
+	cryp.len = DATA_SIZE;
 	cryp.src = ciphertext;
 	cryp.dst = ciphertext;
 	cryp.iv = iv;
@@ -90,7 +93,7 @@ test_crypto(int cfd)
 	printf("%s: data encrypted\n", __func__);
 
 	/* Verify the result */
-	if (memcmp(plaintext, ciphertext, sizeof(plaintext)) != 0) {
+	if (memcmp(plaintext, ciphertext, DATA_SIZE) != 0) {
 		fprintf(stderr,
 			"FAIL: Decrypted data are different from the input data.\n");
 		return 1;
@@ -108,11 +111,12 @@ test_crypto(int cfd)
 
 static int test_aes(int cfd)
 {
-	char plaintext1[BLOCK_SIZE];
+	char plaintext1_raw[BLOCK_SIZE + 63], *plaintext1;
 	char ciphertext1[BLOCK_SIZE] = { 0xdf, 0x55, 0x6a, 0x33, 0x43, 0x8d, 0xb8, 0x7b, 0xc4, 0x1b, 0x17, 0x52, 0xc5, 0x5e, 0x5e, 0x49 };
 	char iv1[BLOCK_SIZE];
 	char key1[KEY_SIZE] = { 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
-	char plaintext2[BLOCK_SIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 };
+	char plaintext2_data[BLOCK_SIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 };
+	char plaintext2_raw[BLOCK_SIZE + 63], *plaintext2;
 	char ciphertext2[BLOCK_SIZE] = { 0xb7, 0x97, 0x2b, 0x39, 0x41, 0xc4, 0x4b, 0x90, 0xaf, 0xa7, 0xb2, 0x64, 0xbf, 0xba, 0x73, 0x87 };
 	char iv2[BLOCK_SIZE];
 	char key2[KEY_SIZE];
@@ -120,16 +124,11 @@ static int test_aes(int cfd)
 	struct session_op sess1, sess2;
 	struct crypt_op cryp1, cryp2;
 
-	printf("running %s\n", __func__);
-
 	memset(&sess1, 0, sizeof(sess1));
 	memset(&sess2, 0, sizeof(sess2));
 	memset(&cryp1, 0, sizeof(cryp1));
 	memset(&cryp2, 0, sizeof(cryp2));
 
-	memset(plaintext1, 0x0, sizeof(plaintext1));
-	memset(iv1, 0x0, sizeof(iv1));
-
 	/* Get crypto session for AES128 */
 	sess1.cipher = CRYPTO_AES_CBC;
 	sess1.keylen = KEY_SIZE;
@@ -139,6 +138,10 @@ static int test_aes(int cfd)
 		return 1;
 	}
 
+	plaintext1 = (char *)(((unsigned long)plaintext1_raw + sess1.alignmask) & ~sess1.alignmask);
+	memset(plaintext1, 0x0, BLOCK_SIZE);
+
+	memset(iv1, 0x0, sizeof(iv1));
 	memset(key2, 0x0, sizeof(key2));
 
 	/* Get second crypto session for AES128 */
@@ -150,9 +153,12 @@ static int test_aes(int cfd)
 		return 1;
 	}
 
+	plaintext2 = (char *)(((unsigned long)plaintext2_raw + sess2.alignmask) & ~sess2.alignmask);
+	memcpy(plaintext2, plaintext2_data, BLOCK_SIZE);
+
 	/* Encrypt data.in to data.encrypted */
 	cryp1.ses = sess1.ses;
-	cryp1.len = sizeof(plaintext1);
+	cryp1.len = BLOCK_SIZE;
 	cryp1.src = plaintext1;
 	cryp1.dst = plaintext1;
 	cryp1.iv = iv1;
@@ -165,7 +171,7 @@ static int test_aes(int cfd)
 
 	/* Encrypt data.in to data.encrypted */
 	cryp2.ses = sess2.ses;
-	cryp2.len = sizeof(plaintext2);
+	cryp2.len = BLOCK_SIZE;
 	cryp2.src = plaintext2;
 	cryp2.dst = plaintext2;
 	cryp2.iv = iv2;
@@ -179,7 +185,7 @@ static int test_aes(int cfd)
 	printf("cryp1 + cryp2 successfully read\n");
 
 	/* Verify the result */
-	if (memcmp(plaintext1, ciphertext1, sizeof(plaintext1)) != 0) {
+	if (memcmp(plaintext1, ciphertext1, BLOCK_SIZE) != 0) {
 		int i;
 		fprintf(stderr,
 			"FAIL: Decrypted data are different from the input data.\n");
@@ -200,7 +206,7 @@ static int test_aes(int cfd)
 	/* Test 2 */
 
 	/* Verify the result */
-	if (memcmp(plaintext2, ciphertext2, sizeof(plaintext2)) != 0) {
+	if (memcmp(plaintext2, ciphertext2, BLOCK_SIZE) != 0) {
 		int i;
 		fprintf(stderr,
 			"FAIL: Decrypted data are different from the input data.\n");
diff --git a/drivers/staging/crypto/cryptodev/examples/async_speed.c b/drivers/staging/crypto/cryptodev/examples/async_speed.c
index 7d98baefddf5..679219ba8e4d 100644
--- a/drivers/staging/crypto/cryptodev/examples/async_speed.c
+++ b/drivers/staging/crypto/cryptodev/examples/async_speed.c
@@ -69,7 +69,7 @@ static void value2human(double bytes, double time, double* data, double* speed,c
 }
 
 
-int encrypt_data(struct session_op *sess, int fdc, int chunksize, int flags)
+int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
 {
 	struct crypt_op cop;
 	char *buffer[64], iv[32];
@@ -86,7 +86,17 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int flags)
 	fflush(stdout);
 
 	for (rc = 0; rc < 64; rc++) {
-		buffer[rc] = malloc(chunksize);
+		if (alignmask) {
+			if (posix_memalign((void **)(buffer + rc), alignmask + 1, chunksize)) {
+				printf("posix_memalign() failed!\n");
+				return 1;
+			}
+		} else {
+			if (!(buffer[rc] = malloc(chunksize))) {
+				perror("malloc()");
+				return 1;
+			}
+		}
 		memset(buffer[rc], val++, chunksize);
 	}
 	pfd.fd = fdc;
@@ -111,7 +121,6 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int flags)
 			cop.len = chunksize;
 			cop.iv = (unsigned char *)iv;
 			cop.op = COP_ENCRYPT;
-			cop.flags = flags;
 			cop.src = cop.dst = (unsigned char *)buffer[bufidx];
 			bufidx = (bufidx + 1) % 64;
 
@@ -171,7 +180,7 @@ int main(void)
 	}
 
 	for (i = 256; i <= (64 * 4096); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, 0))
+		if (encrypt_data(&sess, fdc, i, sess.alignmask))
 			break;
 	}
 
@@ -187,7 +196,7 @@ int main(void)
 	}
 
 	for (i = 256; i <= (64 * 1024); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, 0))
+		if (encrypt_data(&sess, fdc, i, sess.alignmask))
 			break;
 	}
 
diff --git a/drivers/staging/crypto/cryptodev/examples/cipher.c b/drivers/staging/crypto/cryptodev/examples/cipher.c
index b07b25254f1d..17ee4668b315 100644
--- a/drivers/staging/crypto/cryptodev/examples/cipher.c
+++ b/drivers/staging/crypto/cryptodev/examples/cipher.c
@@ -19,8 +19,8 @@
 static int
 test_crypto(int cfd)
 {
-	char plaintext[DATA_SIZE];
-	char ciphertext[DATA_SIZE];
+	char plaintext_raw[DATA_SIZE + 63], *plaintext;
+	char ciphertext_raw[DATA_SIZE + 63], *ciphertext;
 	char iv[BLOCK_SIZE];
 	char key[KEY_SIZE];
 
@@ -30,7 +30,6 @@ test_crypto(int cfd)
 	memset(&sess, 0, sizeof(sess));
 	memset(&cryp, 0, sizeof(cryp));
 
-	memset(plaintext, 0x15,  sizeof(plaintext));
 	memset(key, 0x33,  sizeof(key));
 	memset(iv, 0x03,  sizeof(iv));
 
@@ -43,10 +42,13 @@ test_crypto(int cfd)
 		return 1;
 	}
 
+	plaintext = (char *)(((unsigned long)plaintext_raw + sess.alignmask) & ~sess.alignmask);
+	ciphertext = (char *)(((unsigned long)ciphertext_raw + sess.alignmask) & ~sess.alignmask);
+	memset(plaintext, 0x15, DATA_SIZE);
 
 	/* Encrypt data.in to data.encrypted */
 	cryp.ses = sess.ses;
-	cryp.len = sizeof(plaintext);
+	cryp.len = DATA_SIZE;
 	cryp.src = plaintext;
 	cryp.dst = ciphertext;
 	cryp.iv = iv;
@@ -65,10 +67,10 @@ test_crypto(int cfd)
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
-	
+
 	/* Decrypt data.encrypted to data.decrypted */
 	cryp.ses = sess.ses;
-	cryp.len = sizeof(plaintext);
+	cryp.len = DATA_SIZE;
 	cryp.src = ciphertext;
 	cryp.dst = ciphertext;
 	cryp.iv = iv;
@@ -79,7 +81,7 @@ test_crypto(int cfd)
 	}
 
 	/* Verify the result */
-	if (memcmp(plaintext, ciphertext, sizeof(plaintext)) != 0) {
+	if (memcmp(plaintext, ciphertext, DATA_SIZE) != 0) {
 		fprintf(stderr,
 			"FAIL: Decrypted data are different from the input data.\n");
 		return 1;
@@ -97,11 +99,12 @@ test_crypto(int cfd)
 
 static int test_aes(int cfd)
 {
-	char plaintext1[BLOCK_SIZE];
+	char plaintext1_raw[BLOCK_SIZE + 63], *plaintext1;
 	char ciphertext1[BLOCK_SIZE] = { 0xdf, 0x55, 0x6a, 0x33, 0x43, 0x8d, 0xb8, 0x7b, 0xc4, 0x1b, 0x17, 0x52, 0xc5, 0x5e, 0x5e, 0x49 };
 	char iv1[BLOCK_SIZE];
 	char key1[KEY_SIZE] = { 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
-	char plaintext2[BLOCK_SIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 };
+	char plaintext2_data[BLOCK_SIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 };
+	char plaintext2_raw[BLOCK_SIZE + 63], *plaintext2;
 	char ciphertext2[BLOCK_SIZE] = { 0xb7, 0x97, 0x2b, 0x39, 0x41, 0xc4, 0x4b, 0x90, 0xaf, 0xa7, 0xb2, 0x64, 0xbf, 0xba, 0x73, 0x87 };
 	char iv2[BLOCK_SIZE];
 	char key2[KEY_SIZE];
@@ -112,9 +115,6 @@ static int test_aes(int cfd)
 	memset(&sess, 0, sizeof(sess));
 	memset(&cryp, 0, sizeof(cryp));
 
-	memset(plaintext1, 0x0, sizeof(plaintext1));
-	memset(iv1, 0x0, sizeof(iv1));
-	
 	/* Get crypto session for AES128 */
 	sess.cipher = CRYPTO_AES_CBC;
 	sess.keylen = KEY_SIZE;
@@ -124,9 +124,14 @@ static int test_aes(int cfd)
 		return 1;
 	}
 
+	plaintext1 = (char *)(((unsigned long)plaintext1_raw + sess.alignmask) & ~sess.alignmask);
+
+	memset(plaintext1, 0x0, BLOCK_SIZE);
+	memset(iv1, 0x0, sizeof(iv1));
+
 	/* Encrypt data.in to data.encrypted */
 	cryp.ses = sess.ses;
-	cryp.len = sizeof(plaintext1);
+	cryp.len = BLOCK_SIZE;
 	cryp.src = plaintext1;
 	cryp.dst = plaintext1;
 	cryp.iv = iv1;
@@ -137,7 +142,7 @@ static int test_aes(int cfd)
 	}
 
 	/* Verify the result */
-	if (memcmp(plaintext1, ciphertext1, sizeof(plaintext1)) != 0) {
+	if (memcmp(plaintext1, ciphertext1, BLOCK_SIZE) != 0) {
 		fprintf(stderr,
 			"FAIL: Decrypted data are different from the input data.\n");
 		return 1;
@@ -147,7 +152,7 @@ static int test_aes(int cfd)
 
 	memset(key2, 0x0, sizeof(key2));
 	memset(iv2, 0x0, sizeof(iv2));
-	
+
 	/* Get crypto session for AES128 */
 	sess.cipher = CRYPTO_AES_CBC;
 	sess.keylen = KEY_SIZE;
@@ -157,9 +162,12 @@ static int test_aes(int cfd)
 		return 1;
 	}
 
+	plaintext2 = (char *)(((unsigned long)plaintext2_raw + sess.alignmask) & ~sess.alignmask);
+	memcpy(plaintext2, plaintext2_data, BLOCK_SIZE);
+
 	/* Encrypt data.in to data.encrypted */
 	cryp.ses = sess.ses;
-	cryp.len = sizeof(plaintext2);
+	cryp.len = BLOCK_SIZE;
 	cryp.src = plaintext2;
 	cryp.dst = plaintext2;
 	cryp.iv = iv2;
@@ -170,7 +178,7 @@ static int test_aes(int cfd)
 	}
 
 	/* Verify the result */
-	if (memcmp(plaintext2, ciphertext2, sizeof(plaintext2)) != 0) {
+	if (memcmp(plaintext2, ciphertext2, BLOCK_SIZE) != 0) {
 		fprintf(stderr,
 			"FAIL: Decrypted data are different from the input data.\n");
 		return 1;
diff --git a/drivers/staging/crypto/cryptodev/examples/speed.c b/drivers/staging/crypto/cryptodev/examples/speed.c
index d58c70fe83fe..998772dd7014 100644
--- a/drivers/staging/crypto/cryptodev/examples/speed.c
+++ b/drivers/staging/crypto/cryptodev/examples/speed.c
@@ -65,7 +65,7 @@ static void value2human(double bytes, double time, double* data, double* speed,c
 }
 
 
-int encrypt_data(struct session_op *sess, int fdc, int chunksize, int flags)
+int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
 {
 	struct crypt_op cop;
 	char *buffer, iv[32];
@@ -75,7 +75,18 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int flags)
 	double secs, ddata, dspeed;
 	char metric[16];
 
-	buffer = malloc(chunksize);
+	if (alignmask) {
+		if (posix_memalign((void **)&buffer, alignmask, chunksize)) {
+			printf("posix_memalign() failed!\n");
+			return 1;
+		}
+	} else {
+		if (!(buffer = malloc(chunksize))) {
+			perror("malloc()");
+			return 1;
+		}
+	}
+
 	memset(iv, 0x23, 32);
 
 	printf("\tEncrypting in chunks of %d bytes: ", chunksize);
@@ -93,7 +104,6 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int flags)
 		cop.len = chunksize;
 		cop.iv = (unsigned char *)iv;
 		cop.op = COP_ENCRYPT;
-		cop.flags = flags;
 		cop.src = cop.dst = (unsigned char *)buffer;
 
 		if (ioctl(fdc, CIOCCRYPT, &cop)) {
@@ -105,11 +115,12 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int flags)
 	gettimeofday(&end, NULL);
 
 	secs = udifftimeval(start, end)/ 1000000.0;
-	
+
 	value2human(total, secs, &ddata, &dspeed, metric);
 	printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
 	printf ("%.2f %s/sec\n", dspeed, metric);
 
+	free(buffer);
 	return 0;
 }
 
@@ -129,7 +140,7 @@ int main(void)
 		perror("ioctl(CRIOGET)");
 		return 1;
 	}
-	
+
 	fprintf(stderr, "Testing NULL cipher: \n");
 	memset(&sess, 0, sizeof(sess));
 	sess.cipher = CRYPTO_NULL;
@@ -141,7 +152,7 @@ int main(void)
 	}
 
 	for (i = 256; i <= (64 * 4096); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, 0))
+		if (encrypt_data(&sess, fdc, i, sess.alignmask))
 			break;
 	}
 
-- 
2.1.0

