From 91de2fbedc64f8c1bafcf711b99bf49e52c25973 Mon Sep 17 00:00:00 2001
From: Nikos Mavrogiannopoulos <nmav@gnutls.org>
Date: Sun, 11 Dec 2011 12:03:43 +0100
Subject: [PATCH 167/291] cryptodev: generate

---
 drivers/staging/crypto/cryptodev/lib/Makefile    | 14 ++++--
 drivers/staging/crypto/cryptodev/lib/combo.c     |  8 ++--
 drivers/staging/crypto/cryptodev/lib/hash.c      |  3 ++
 drivers/staging/crypto/cryptodev/lib/main.c      | 39 ++-------------
 drivers/staging/crypto/cryptodev/lib/threshold.c | 61 ++++++++++++++++++++++++
 drivers/staging/crypto/cryptodev/lib/threshold.h | 10 ++++
 6 files changed, 92 insertions(+), 43 deletions(-)
 create mode 100644 drivers/staging/crypto/cryptodev/lib/threshold.c
 create mode 100644 drivers/staging/crypto/cryptodev/lib/threshold.h

diff --git a/drivers/staging/crypto/cryptodev/lib/Makefile b/drivers/staging/crypto/cryptodev/lib/Makefile
index 460b4ba41efe..af87795b8dcc 100644
--- a/drivers/staging/crypto/cryptodev/lib/Makefile
+++ b/drivers/staging/crypto/cryptodev/lib/Makefile
@@ -1,9 +1,15 @@
-CFLAGS=-g -O2 -Wall -DDEBUG
+CFLAGS=-g -O2 -Wall
 
 all: benchmark
 
-benchmark: benchmark.c hash.c main.c combo.c
-	gcc $(CFLAGS) -o $@ $^ -lssl
+benchmark: main.c libthreshold.a
+	gcc $(CFLAGS) -DDEBUG -o $@ $^ -lssl libthreshold.a
+
+.o:
+	gcc $(CCFLAGS) -c $< -o $@
+
+libthreshold.a: benchmark.o hash.o threshold.o combo.o
+	ar  rcs $@ $^
 
 clean:
-	rm -f *.o *~ benchmark
+	rm -f *.o *~ benchmark libthreshold.a
diff --git a/drivers/staging/crypto/cryptodev/lib/combo.c b/drivers/staging/crypto/cryptodev/lib/combo.c
index 29160ffd4749..6d649cd18157 100644
--- a/drivers/staging/crypto/cryptodev/lib/combo.c
+++ b/drivers/staging/crypto/cryptodev/lib/combo.c
@@ -39,9 +39,11 @@ int aead_ctx_init(struct cryptodev_ctx* ctx, int cipher, int hash, void* key, in
 		perror("ioctl(CIOCGSESSINFO)");
 		return -1;
 	}
+#ifdef DEBUG
 	printf("Got %s-%s with drivers %s and %s\n",
 			siop.cipher_info.cra_name, siop.hash_info.cra_name, 
 			siop.cipher_info.cra_driver_name, siop.hash_info.cra_driver_name);
+#endif
 	/*printf("Alignmask is %x\n", (unsigned int)siop.alignmask);*/
 	ctx->alignmask = siop.alignmask;
 #endif
@@ -147,13 +149,13 @@ int aead_test(int cipher, int mac, void* ukey, int ukey_size,
 	
 		t2 = (double)counted/(double)elapsed;
 		
+#ifdef DEBUG
+		printf("%d: kernel: %.4f bytes/msec, user: %.4f bytes/msec\n", sizes[i], t1, t2);
+#endif
 		if (t1 > t2) {
 			ret = sizes[i];
 			goto finish;
 		}
-#ifdef DEBUG
-		printf("%d: kernel: %.4f bytes/msec, user: %.4f bytes/msec\n", sizes[i], t1, t2);
-#endif
 	}
 	
 	ret = -1;
diff --git a/drivers/staging/crypto/cryptodev/lib/hash.c b/drivers/staging/crypto/cryptodev/lib/hash.c
index e4d669373166..a90e2262cba5 100644
--- a/drivers/staging/crypto/cryptodev/lib/hash.c
+++ b/drivers/staging/crypto/cryptodev/lib/hash.c
@@ -30,13 +30,16 @@ int hash_ctx_init(struct cryptodev_ctx* ctx, int hash, int cfd)
 	}
 
 #ifdef CIOCGSESSINFO
+	memset(&siop, 0, sizeof(siop));
 	siop.ses = ctx->sess.ses;
 	if (ioctl(ctx->cfd, CIOCGSESSINFO, &siop)) {
 		perror("ioctl(CIOCGSESSINFO)");
 		return -1;
 	}
+#ifdef DEBUG
 	printf("Got %s with driver %s\n",
 			siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
+#endif
 	/*printf("Alignmask is %x\n", (unsigned int)siop.alignmask);*/
 	ctx->alignmask = siop.alignmask;
 #endif
diff --git a/drivers/staging/crypto/cryptodev/lib/main.c b/drivers/staging/crypto/cryptodev/lib/main.c
index 4e0474b65edb..49ca05be1d8e 100644
--- a/drivers/staging/crypto/cryptodev/lib/main.c
+++ b/drivers/staging/crypto/cryptodev/lib/main.c
@@ -10,50 +10,17 @@
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <crypto/cryptodev.h>
-#include <openssl/sha.h>
-#include <openssl/aes.h>
-#include "hash.h"
-
-void sha_hash(void* text, int size, void* digest)
-{
-SHA_CTX ctx;
-
-	SHA_Init(&ctx);
-	
-	SHA_Update(&ctx, text, size);
-	
-	SHA_Final(digest, &ctx);
-}
-
-void aes_sha_combo(void* ctx, void* plaintext, void* ciphertext, int size, void* tag)
-{
-SHA_CTX sha_ctx;
-uint8_t iv[16];
-AES_KEY* key = ctx;
-
-	SHA_Init(&sha_ctx);
-	
-	SHA_Update(&sha_ctx, plaintext, size);
-	
-	SHA_Final(tag, &sha_ctx);
-	
-	AES_cbc_encrypt(plaintext, ciphertext, size, key, iv, 1);
-}
+#include "threshold.h"
 
 int main()
 {
 int ret;
-AES_KEY key;
-uint8_t ukey[16];
 
-	ret = hash_test(CRYPTO_SHA1, sha_hash);
+	ret = get_sha1_threshold();
 	if (ret > 0)
 		printf("SHA1 in kernel outperforms user-space after %d input bytes\n", ret);
 	
-	memset(ukey, 0xaf, sizeof(ukey));
-	AES_set_encrypt_key(ukey, 16, &key);
-	
-	ret = aead_test(CRYPTO_AES_CBC, CRYPTO_SHA1, ukey, 16, &key, aes_sha_combo);
+	ret = get_aes_sha1_threshold();
 	if (ret > 0)
 		printf("AES-SHA1 in kernel outperforms user-space after %d input bytes\n", ret);
 	
diff --git a/drivers/staging/crypto/cryptodev/lib/threshold.c b/drivers/staging/crypto/cryptodev/lib/threshold.c
new file mode 100644
index 000000000000..f1215a3e6c6c
--- /dev/null
+++ b/drivers/staging/crypto/cryptodev/lib/threshold.c
@@ -0,0 +1,61 @@
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <crypto/cryptodev.h>
+#include <openssl/aes.h>
+#include <openssl/engine.h>
+#include <openssl/hmac.h>
+#include <openssl/evp.h>
+#include "hash.h"
+#include "threshold.h"
+
+void sha_hash(void* text, int size, void* digest)
+{
+SHA_CTX ctx;
+
+	SHA_Init(&ctx);
+	
+	SHA_Update(&ctx, text, size);
+	
+	SHA_Final(digest, &ctx);
+}
+
+void aes_sha_combo(void* ctx, void* plaintext, void* ciphertext, int size, void* tag)
+{
+uint8_t iv[16];
+AES_KEY* key = ctx;
+HMAC_CTX hctx;
+unsigned int rlen = 20;
+
+	HMAC_CTX_init(&hctx);
+	HMAC_Init_ex(&hctx, iv, 16, EVP_sha1(), NULL);
+		
+	HMAC_Update(&hctx, plaintext, size);
+	
+	HMAC_Final(&hctx, tag, &rlen);
+	HMAC_CTX_cleanup(&hctx);
+
+	AES_cbc_encrypt(plaintext, ciphertext, size, key, iv, 1);
+}
+
+int get_sha1_threshold()
+{
+	return hash_test(CRYPTO_SHA1, sha_hash);
+}
+
+int get_aes_sha1_threshold()
+{
+AES_KEY key;
+uint8_t ukey[16];
+
+	ENGINE_load_builtin_engines();
+        ENGINE_register_all_complete();
+        
+	memset(ukey, 0xaf, sizeof(ukey));
+	AES_set_encrypt_key(ukey, 16*8, &key);
+
+	return aead_test(CRYPTO_AES_CBC, CRYPTO_SHA1, ukey, 16, &key, aes_sha_combo);
+}
+
diff --git a/drivers/staging/crypto/cryptodev/lib/threshold.h b/drivers/staging/crypto/cryptodev/lib/threshold.h
new file mode 100644
index 000000000000..6c11019b2ee6
--- /dev/null
+++ b/drivers/staging/crypto/cryptodev/lib/threshold.h
@@ -0,0 +1,10 @@
+/* Return the number of bytes after which the
+ * kernel operation is more efficient to use.
+ * If return value is -1, then kernel operation
+ * cannot, or shouldn't be used, because it is always
+ * slower.
+ *
+ * Running time ~= 1.2 seconds per call.
+ */
+int get_sha1_threshold();
+int get_aes_sha1_threshold();
-- 
2.1.0

