From 4b6315c325336493ba0520bb2a92bcc5f2f6cd42 Mon Sep 17 00:00:00 2001
From: Nikos Mavrogiannopoulos <nmav@gnutls.org>
Date: Tue, 28 Feb 2012 22:37:17 +0100
Subject: [PATCH 198/291] cryptodev: Tests

---
 .../staging/crypto/cryptodev/tests/async_cipher.c  | 32 +++++++++-------
 .../staging/crypto/cryptodev/tests/async_hmac.c    | 12 +++---
 .../crypto/cryptodev/tests/cipher-aead-srtp.c      | 23 +++++++-----
 .../staging/crypto/cryptodev/tests/cipher-aead.c   | 11 ++++--
 .../staging/crypto/cryptodev/tests/cipher-gcm.c    | 43 +++++++++++++++-------
 drivers/staging/crypto/cryptodev/tests/cipher.c    | 19 +++++++---
 drivers/staging/crypto/cryptodev/tests/hmac.c      | 29 +++++++++------
 7 files changed, 106 insertions(+), 63 deletions(-)

diff --git a/drivers/staging/crypto/cryptodev/tests/async_cipher.c b/drivers/staging/crypto/cryptodev/tests/async_cipher.c
index e89c7f2137f5..96ef7bc3b0a7 100644
--- a/drivers/staging/crypto/cryptodev/tests/async_cipher.c
+++ b/drivers/staging/crypto/cryptodev/tests/async_cipher.c
@@ -15,6 +15,8 @@
 
 #include "testhelper.h"
 
+static int debug = 0;
+
 #define	DATA_SIZE	8*1024
 #define	BLOCK_SIZE	16
 #define	KEY_SIZE	16
@@ -33,7 +35,7 @@ test_crypto(int cfd)
 #endif
 	struct crypt_op cryp;
 
-	printf("running %s\n", __func__);
+	if (debug) printf("running %s\n", __func__);
 
 	memset(&sess, 0, sizeof(sess));
 	memset(&cryp, 0, sizeof(cryp));
@@ -50,7 +52,7 @@ test_crypto(int cfd)
 		return 1;
 	}
 
-	printf("%s: got the session\n", __func__);
+	if (debug) printf("%s: got the session\n", __func__);
 
 #ifdef CIOCGSESSINFO
 	siop.ses = sess.ses;
@@ -77,19 +79,19 @@ test_crypto(int cfd)
 	DO_OR_DIE(do_async_crypt(cfd, &cryp), 0);
 	DO_OR_DIE(do_async_fetch(cfd, &cryp), 0);
 
-	printf("%s: data encrypted\n", __func__);
+	if (debug) printf("%s: data encrypted\n", __func__);
 
 	if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
 		perror("ioctl(CIOCFSESSION)");
 		return 1;
 	}
-	printf("%s: session finished\n", __func__);
+	if (debug) printf("%s: session finished\n", __func__);
 
 	if (ioctl(cfd, CIOCGSESSION, &sess)) {
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
-	printf("%s: got new session\n", __func__);
+	if (debug) printf("%s: got new session\n", __func__);
 
 	/* Decrypt data.encrypted to data.decrypted */
 	cryp.ses = sess.ses;
@@ -102,14 +104,14 @@ test_crypto(int cfd)
 	DO_OR_DIE(do_async_crypt(cfd, &cryp), 0);
 	DO_OR_DIE(do_async_fetch(cfd, &cryp), 0);
 
-	printf("%s: data encrypted\n", __func__);
+	if (debug) printf("%s: data encrypted\n", __func__);
 
 	/* Verify the result */
 	if (memcmp(plaintext, ciphertext, DATA_SIZE) != 0) {
 		fprintf(stderr,
 			"FAIL: Decrypted data are different from the input data.\n");
 		return 1;
-	} else
+	} else if (debug)
 		printf("Test passed\n");
 
 	/* Finish crypto session */
@@ -196,7 +198,7 @@ static int test_aes(int cfd)
 	cryp1.op = COP_ENCRYPT;
 
 	DO_OR_DIE(do_async_crypt(cfd, &cryp1), 0);
-	printf("cryp1 written out\n");
+	if (debug) printf("cryp1 written out\n");
 
 	memset(iv2, 0x0, sizeof(iv2));
 
@@ -209,11 +211,11 @@ static int test_aes(int cfd)
 	cryp2.op = COP_ENCRYPT;
 
 	DO_OR_DIE(do_async_crypt(cfd, &cryp2), 0);
-	printf("cryp2 written out\n");
+	if (debug) printf("cryp2 written out\n");
 
 	DO_OR_DIE(do_async_fetch(cfd, &cryp1), 0);
 	DO_OR_DIE(do_async_fetch(cfd, &cryp2), 0);
-	printf("cryp1 + cryp2 successfully read\n");
+	if (debug) printf("cryp1 + cryp2 successfully read\n");
 
 	/* Verify the result */
 	if (memcmp(plaintext1, ciphertext1, BLOCK_SIZE) != 0) {
@@ -235,7 +237,7 @@ static int test_aes(int cfd)
 		printf("\n");
 		return 1;
 	} else {
-		printf("result 1 passed\n");
+		if (debug) printf("result 1 passed\n");
 	}
 
 	/* Test 2 */
@@ -260,10 +262,10 @@ static int test_aes(int cfd)
 		printf("\n");
 		return 1;
 	} else {
-		printf("result 2 passed\n");
+		if (debug) printf("result 2 passed\n");
 	}
 
-	printf("AES Test passed\n");
+	if (debug) printf("AES Test passed\n");
 
 	/* Finish crypto session */
 	if (ioctl(cfd, CIOCFSESSION, &sess1.ses)) {
@@ -279,9 +281,11 @@ static int test_aes(int cfd)
 }
 
 int
-main()
+main(int argc, char** argv)
 {
 	int fd = -1, cfd = -1;
+	
+	if (argc > 1) debug = 1;
 
 	/* Open the crypto device */
 	fd = open("/dev/crypto", O_RDWR, 0);
diff --git a/drivers/staging/crypto/cryptodev/tests/async_hmac.c b/drivers/staging/crypto/cryptodev/tests/async_hmac.c
index be0275219246..4083e897bdb1 100644
--- a/drivers/staging/crypto/cryptodev/tests/async_hmac.c
+++ b/drivers/staging/crypto/cryptodev/tests/async_hmac.c
@@ -16,6 +16,8 @@
 
 #include "testhelper.h"
 
+static int debug = 0;
+
 #define	DATA_SIZE	4096
 #define	BLOCK_SIZE	16
 #define	KEY_SIZE	16
@@ -72,7 +74,7 @@ test_crypto(int cfd)
 		puts("\n");
 		fprintf(stderr, "HASH test 1: failed\n");
 	} else {
-		fprintf(stderr, "HASH test 1: passed\n");
+		if (debug) fprintf(stderr, "HASH test 1: passed\n");
 	}
 
 	if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
@@ -109,7 +111,7 @@ test_crypto(int cfd)
 		puts("\n");
 		fprintf(stderr, "HMAC test 1: failed\n");
 	} else {
-		fprintf(stderr, "HMAC test 1: passed\n");
+		if (debug) fprintf(stderr, "HMAC test 1: passed\n");
 	}
 
 	if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
@@ -156,14 +158,14 @@ test_crypto(int cfd)
 		fprintf(stderr,
 			"FAIL: Decrypted data are different from the input data.\n");
 		return 1;
-	} else
+	} else if (debug) 
 		printf("Crypt Test: passed\n");
 
 	if (memcmp(mac, oldmac, 20) != 0) {
 		fprintf(stderr,
 			"FAIL: Hash in decrypted data different than in encrypted.\n");
 		return 1;
-	} else
+	} else if (debug)
 		printf("HMAC Test 2: passed\n");
 
 	/* Finish crypto session */
@@ -230,7 +232,7 @@ test_extras(int cfd)
 		puts("\n");
 		fprintf(stderr, "HASH test [update]: failed\n");
 	} else {
-		fprintf(stderr, "HASH test [update]: passed\n");
+		if (debug) fprintf(stderr, "HASH test [update]: passed\n");
 	}
 
 	memset(mac, 0, sizeof(mac));
diff --git a/drivers/staging/crypto/cryptodev/tests/cipher-aead-srtp.c b/drivers/staging/crypto/cryptodev/tests/cipher-aead-srtp.c
index e420a40ce986..fae04e736750 100644
--- a/drivers/staging/crypto/cryptodev/tests/cipher-aead-srtp.c
+++ b/drivers/staging/crypto/cryptodev/tests/cipher-aead-srtp.c
@@ -22,6 +22,8 @@
 
 #define MAC_SIZE 20 /* SHA1 */
 
+static int debug = 0;
+
 static int
 get_sha1_hmac(int cfd, void* key, int key_size, void* data, int data_size, void* mac)
 {
@@ -65,11 +67,11 @@ get_sha1_hmac(int cfd, void* key, int key_size, void* data, int data_size, void*
 static void print_buf(char* desc, unsigned char* buf, int size)
 {
 int i;
-	fputs(desc, stdout);
+	fputs(desc, stderr);
 	for (i=0;i<size;i++) {
-		printf("%.2x", (uint8_t)buf[i]);
+		fprintf(stderr, "%.2x", (uint8_t)buf[i]);
 	}
-	fputs("\n", stdout);
+	fputs("\n", stderr);
 }
 
 static int
@@ -115,7 +117,9 @@ test_crypto(int cfd)
 		perror("ioctl(CIOCGSESSINFO)");
 		return 1;
 	}
-	printf("requested cipher CRYPTO_AES_CBC/HMAC-SHA1, got %s with driver %s\n",
+	
+	if (debug)
+		printf("requested cipher CRYPTO_AES_CBC/HMAC-SHA1, got %s with driver %s\n",
 			siop.cipher_info.cra_name, siop.cipher_info.cra_driver_name);
 
 	plaintext = (char *)(((unsigned long)plaintext_raw + siop.alignmask) & ~siop.alignmask);
@@ -207,8 +211,7 @@ test_crypto(int cfd)
 		return 1;
 	}
 
-	printf("Test passed\n");
-
+	if (debug) printf("Test passed\n");
 
 	/* Finish crypto session */
 	if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
@@ -349,7 +352,7 @@ test_encrypt_decrypt(int cfd)
 		return 1;
 	}
 
-	printf("Test passed\n");
+	if (debug) printf("Test passed\n");
 
 
 	/* Finish crypto session */
@@ -475,7 +478,7 @@ test_encrypt_decrypt_error(int cfd, int err)
 			return 1;
 		}
 
-		printf("Test passed\n");
+		if (debug) printf("Test passed\n");
 		return 0;
 	}
 
@@ -513,9 +516,11 @@ test_encrypt_decrypt_error(int cfd, int err)
 }
 
 int
-main()
+main(int argc, char** argv)
 {
 	int fd = -1, cfd = -1;
+	
+	if (argc > 1) debug = 1;
 
 	/* Open the crypto device */
 	fd = open("/dev/crypto", O_RDWR, 0);
diff --git a/drivers/staging/crypto/cryptodev/tests/cipher-aead.c b/drivers/staging/crypto/cryptodev/tests/cipher-aead.c
index eba5ddcfa6cc..164327f6942c 100644
--- a/drivers/staging/crypto/cryptodev/tests/cipher-aead.c
+++ b/drivers/staging/crypto/cryptodev/tests/cipher-aead.c
@@ -20,6 +20,8 @@
 
 #define MAC_SIZE 20 /* SHA1 */
 
+static int debug = 0;
+
 static int
 get_sha1_hmac(int cfd, void* key, int key_size, void* data1, int data1_size, void* data2, int data2_size, void* mac)
 {
@@ -127,7 +129,8 @@ test_crypto(int cfd)
 		perror("ioctl(CIOCGSESSINFO)");
 		return 1;
 	}
-	printf("requested cipher CRYPTO_AES_CBC/HMAC-SHA1, got %s with driver %s\n",
+	if (debug)
+		printf("requested cipher CRYPTO_AES_CBC/HMAC-SHA1, got %s with driver %s\n",
 			siop.cipher_info.cra_name, siop.cipher_info.cra_driver_name);
 
 	plaintext = (char *)(((unsigned long)plaintext_raw + siop.alignmask) & ~siop.alignmask);
@@ -224,7 +227,7 @@ test_crypto(int cfd)
 			return 1;
 		}
 
-	printf("Test passed\n");
+	if (debug) printf("Test passed\n");
 
 
 	/* Finish crypto session */
@@ -373,7 +376,7 @@ test_encrypt_decrypt(int cfd)
 		return 1;
 	}
 
-	printf("Test passed\n");
+	if (debug) printf("Test passed\n");
 
 
 	/* Finish crypto session */
@@ -501,7 +504,7 @@ test_encrypt_decrypt_error(int cfd, int err)
 			return 1;
 		}
 
-		printf("Test passed\n");
+		if (debug) printf("Test passed\n");
 		return 0;
 	}
 
diff --git a/drivers/staging/crypto/cryptodev/tests/cipher-gcm.c b/drivers/staging/crypto/cryptodev/tests/cipher-gcm.c
index 33a1e2aef19e..599e70c98971 100644
--- a/drivers/staging/crypto/cryptodev/tests/cipher-gcm.c
+++ b/drivers/staging/crypto/cryptodev/tests/cipher-gcm.c
@@ -20,6 +20,8 @@
 
 #define my_perror(x) {fprintf(stderr, "%s: %d\n", __func__, __LINE__); perror(x); }
 
+static int debug = 0;
+
 static void print_buf(char *desc, const unsigned char *buf, int size)
 {
 	int i;
@@ -99,8 +101,10 @@ static int test_crypto(int cfd)
 
 	/* Get crypto session for AES128 */
 
-	fprintf(stdout, "Tests on AES-GCM vectors: ");
-	fflush(stdout);
+	if (debug) {
+		fprintf(stdout, "Tests on AES-GCM vectors: ");
+		fflush(stdout);
+	}
 	for (i = 0;
 	     i < sizeof(aes_gcm_vectors) / sizeof(aes_gcm_vectors[0]);
 	     i++) {
@@ -167,8 +171,11 @@ static int test_crypto(int cfd)
 		}
 
 	}
-	fprintf(stdout, "ok\n");
-	fprintf(stdout, "\n");
+	
+	if (debug) {
+		fprintf(stdout, "ok\n");
+		fprintf(stdout, "\n");
+	}
 
 	/* Finish crypto session */
 	if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
@@ -195,8 +202,10 @@ static int test_encrypt_decrypt(int cfd)
 	struct crypt_auth_op cao;
 	struct session_info_op siop;
 
-	fprintf(stdout, "Tests on AES-GCM encryption/decryption: ");
-	fflush(stdout);
+	if (debug) {
+		fprintf(stdout, "Tests on AES-GCM encryption/decryption: ");
+		fflush(stdout);
+	}
 
 	memset(&sess, 0, sizeof(sess));
 	memset(&cao, 0, sizeof(cao));
@@ -317,8 +326,10 @@ static int test_encrypt_decrypt(int cfd)
 		return 1;
 	}
 
-	fprintf(stdout, "ok\n");
-	fprintf(stdout, "\n");
+	if (debug) {
+		fprintf(stdout, "ok\n");
+		fprintf(stdout, "\n");
+	}
 
 	return 0;
 }
@@ -337,8 +348,10 @@ static int test_encrypt_decrypt_error(int cfd, int err)
 	struct crypt_auth_op cao;
 	struct session_info_op siop;
 
-	fprintf(stdout, "Tests on AES-GCM tag verification: ");
-	fflush(stdout);
+	if (debug) {
+		fprintf(stdout, "Tests on AES-GCM tag verification: ");
+		fflush(stdout);
+	}
 
 	memset(&sess, 0, sizeof(sess));
 	memset(&cao, 0, sizeof(cao));
@@ -443,8 +456,10 @@ static int test_encrypt_decrypt_error(int cfd, int err)
 			return 1;
 		}
 
-		fprintf(stdout, "ok\n");
-		fprintf(stdout, "\n");
+		if (debug) {
+			fprintf(stdout, "ok\n");
+			fprintf(stdout, "\n");
+		}
 		return 0;
 	}
 
@@ -459,10 +474,12 @@ static int test_encrypt_decrypt_error(int cfd, int err)
 	return 1;
 }
 
-int main()
+int main(int argc, char** argv)
 {
 	int fd = -1, cfd = -1;
 
+	if (argc > 1) debug = 1;
+
 	/* Open the crypto device */
 	fd = open("/dev/crypto", O_RDWR, 0);
 	if (fd < 0) {
diff --git a/drivers/staging/crypto/cryptodev/tests/cipher.c b/drivers/staging/crypto/cryptodev/tests/cipher.c
index 007d3fefa020..07144f2a1680 100644
--- a/drivers/staging/crypto/cryptodev/tests/cipher.c
+++ b/drivers/staging/crypto/cryptodev/tests/cipher.c
@@ -12,6 +12,8 @@
 #include <sys/ioctl.h>
 #include <crypto/cryptodev.h>
 
+static int debug = 0;
+
 #define	DATA_SIZE	8*1024
 #define	BLOCK_SIZE	16
 #define	KEY_SIZE	16
@@ -51,7 +53,8 @@ test_crypto(int cfd)
 		perror("ioctl(CIOCGSESSINFO)");
 		return 1;
 	}
-	printf("requested cipher CRYPTO_AES_CBC, got %s with driver %s\n",
+	if (debug)
+		printf("requested cipher CRYPTO_AES_CBC, got %s with driver %s\n",
 			siop.cipher_info.cra_name, siop.cipher_info.cra_driver_name);
 
 	plaintext = (char *)(((unsigned long)plaintext_raw + siop.alignmask) & ~siop.alignmask);
@@ -90,7 +93,8 @@ test_crypto(int cfd)
 		perror("ioctl(CIOCGSESSINFO)");
 		return 1;
 	}
-	printf("requested cipher CRYPTO_AES_CBC, got %s with driver %s\n",
+	if (debug)
+		printf("requested cipher CRYPTO_AES_CBC, got %s with driver %s\n",
 			siop.cipher_info.cra_name, siop.cipher_info.cra_driver_name);
 #endif
 
@@ -125,7 +129,7 @@ test_crypto(int cfd)
 		}
 		printf("\n");
 		return 1;
-	} else
+	} else if (debug)
 		printf("Test passed\n");
 
 	/* Finish crypto session */
@@ -218,7 +222,8 @@ static int test_aes(int cfd)
 		perror("ioctl(CIOCGSESSINFO)");
 		return 1;
 	}
-	printf("requested cipher CRYPTO_AES_CBC, got %s with driver %s\n",
+	if (debug)
+		printf("requested cipher CRYPTO_AES_CBC, got %s with driver %s\n",
 			siop.cipher_info.cra_name, siop.cipher_info.cra_driver_name);
 
 	plaintext2 = (char *)(((unsigned long)plaintext2_raw + siop.alignmask) & ~siop.alignmask);
@@ -260,7 +265,7 @@ static int test_aes(int cfd)
 		return 1;
 	}
 
-	printf("AES Test passed\n");
+	if (debug) printf("AES Test passed\n");
 
 	/* Finish crypto session */
 	if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
@@ -272,10 +277,12 @@ static int test_aes(int cfd)
 }
 
 int
-main()
+main(int argc, char** argv)
 {
 	int fd = -1, cfd = -1;
 
+	if (argc > 1) debug = 1;
+
 	/* Open the crypto device */
 	fd = open("/dev/crypto", O_RDWR, 0);
 	if (fd < 0) {
diff --git a/drivers/staging/crypto/cryptodev/tests/hmac.c b/drivers/staging/crypto/cryptodev/tests/hmac.c
index 549e9462e483..1d9349ea557e 100644
--- a/drivers/staging/crypto/cryptodev/tests/hmac.c
+++ b/drivers/staging/crypto/cryptodev/tests/hmac.c
@@ -13,6 +13,8 @@
 #include <sys/ioctl.h>
 #include <crypto/cryptodev.h>
 
+static int debug = 0;
+
 #define	DATA_SIZE	4096
 #define	BLOCK_SIZE	16
 #define	KEY_SIZE	16
@@ -61,7 +63,7 @@ test_crypto(int cfd)
 		perror("ioctl(CIOCGSESSINFO)");
 		return 1;
 	}
-	printf("requested mac CRYPTO_SHA1, got %s with driver %s\n",
+	if (debug) printf("requested mac CRYPTO_SHA1, got %s with driver %s\n",
 			siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
 #endif
 
@@ -83,7 +85,7 @@ test_crypto(int cfd)
 		puts("\n");
 		fprintf(stderr, "HASH test 1: failed\n");
 	} else {
-		fprintf(stderr, "HASH test 1: passed\n");
+		if (debug) fprintf(stderr, "HASH test 1: passed\n");
 	}
 
 	/* MD5-HMAC test */
@@ -104,7 +106,8 @@ test_crypto(int cfd)
 		perror("ioctl(CIOCGSESSINFO)");
 		return 1;
 	}
-	printf("requested mac CRYPTO_MD5_HMAC, got %s with driver %s\n",
+	if (debug)
+		printf("requested mac CRYPTO_MD5_HMAC, got %s with driver %s\n",
 			siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
 #endif
 
@@ -126,7 +129,7 @@ test_crypto(int cfd)
 		puts("\n");
 		fprintf(stderr, "HMAC test 1: failed\n");
 	} else {
-		fprintf(stderr, "HMAC test 1: passed\n");
+		if (debug) fprintf(stderr, "HMAC test 1: passed\n");
 	}
 
 	/* Hash and encryption in one step test */
@@ -147,7 +150,8 @@ test_crypto(int cfd)
 		perror("ioctl(CIOCGSESSINFO)");
 		return 1;
 	}
-	printf("requested cipher CRYPTO_AES_CBC and mac CRYPTO_SHA1_HMAC,"
+	if (debug)
+		printf("requested cipher CRYPTO_AES_CBC and mac CRYPTO_SHA1_HMAC,"
 	       " got cipher %s with driver %s and hash %s with driver %s\n",
 			siop.cipher_info.cra_name, siop.cipher_info.cra_driver_name,
 			siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
@@ -182,15 +186,13 @@ test_crypto(int cfd)
 		fprintf(stderr,
 			"FAIL: Decrypted data are different from the input data.\n");
 		return 1;
-	} else
-		printf("Crypt Test: passed\n");
+	} else if (debug) printf("Crypt Test: passed\n");
 
 	if (memcmp(mac, oldmac, 20) != 0) {
 		fprintf(stderr,
 			"FAIL: Hash in decrypted data different than in encrypted.\n");
 		return 1;
-	} else
-		printf("HMAC Test 2: passed\n");
+	} else if (debug) printf("HMAC Test 2: passed\n");
 
 	/* Finish crypto session */
 	if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
@@ -237,7 +239,8 @@ test_extras(int cfd)
 		perror("ioctl(CIOCGSESSINFO)");
 		return 1;
 	}
-	printf("requested mac CRYPTO_SHA1, got %s with driver %s\n",
+	if (debug)
+		printf("requested mac CRYPTO_SHA1, got %s with driver %s\n",
 			siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
 #endif
 
@@ -271,7 +274,7 @@ test_extras(int cfd)
 		puts("\n");
 		fprintf(stderr, "HASH test [update]: failed\n");
 	} else {
-		fprintf(stderr, "HASH test [update]: passed\n");
+		if (debug) fprintf(stderr, "HASH test [update]: passed\n");
 	}
 
 	memset(mac, 0, sizeof(mac));
@@ -287,9 +290,11 @@ test_extras(int cfd)
 
 
 int
-main()
+main(int argc, char** argv)
 {
 	int fd = -1, cfd = -1;
+	
+	if (argc > 1) debug = 1;
 
 	/* Open the crypto device */
 	fd = open("/dev/crypto", O_RDWR, 0);
-- 
2.1.0

