aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-kernel/cryptodev/sdk_patches/0037-rewrite-sha_speed.c-to-reduce-code-duplication.patch
blob: eff6ed9fcb681d55c539a325ee533acb4429520f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
From 344a0243e31f8fc467253404a548eedbb72b35d0 Mon Sep 17 00:00:00 2001
From: Cristian Stoica <cristian.stoica@nxp.com>
Date: Wed, 20 Jan 2016 17:11:49 +0200
Subject: [PATCH 37/38] rewrite sha_speed.c to reduce code duplication

Signed-off-by: Cristian Stoica <cristian.stoica@nxp.com>
---
 tests/sha_speed.c | 131 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 84 insertions(+), 47 deletions(-)

diff --git a/tests/sha_speed.c b/tests/sha_speed.c
index e1dc54b..5f694bd 100644
--- a/tests/sha_speed.c
+++ b/tests/sha_speed.c
@@ -28,6 +28,13 @@
 
 #include <crypto/cryptodev.h>
 
+/* Sizes of buffers to be hashed */
+int buffer_lengths[] = {256, 512, 1024, 2048, 4096, 8192, 65536, 0};
+
+/* Time in seconds allocated for each tested buffer lengths */
+#define BUFFER_TEST_TIME 10
+
+
 static double udifftimeval(struct timeval start, struct timeval end)
 {
 	return (double)(end.tv_usec - start.tv_usec) +
@@ -97,7 +104,7 @@ int hash_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
 	memset(buffer, val++, chunksize);
 
 	must_finish = 0;
-	alarm(5);
+	alarm(BUFFER_TEST_TIME);
 
 	gettimeofday(&start, NULL);
 	do {
@@ -126,73 +133,103 @@ int hash_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
 	return 0;
 }
 
-int main(void)
-{
-	int fd, i, fdc = -1, alignmask = 0;
-	struct session_op sess;
-	char keybuf[32];
+
 #ifdef CIOCGSESSINFO
+int get_alignmask(struct session_op *sess, int fdc)
+{
 	struct session_info_op siop;
+
+	siop.ses = sess->ses;
+	if (ioctl(fdc, CIOCGSESSINFO, &siop) < 0) {
+		perror("ioctl(CIOCGSESSINFO)");
+		/* continue test ignoring CIOCGSESSINFO error */
+		return 0;
+	}
+
+	printf("using algorithm %s with driver %s\n",
+		siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
+
+	return siop.alignmask;
+}
 #endif
 
-	signal(SIGALRM, alarm_handler);
 
-	if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
-		perror("open()");
-		return 1;
-	}
-	if (ioctl(fd, CRIOGET, &fdc)) {
-		perror("ioctl(CRIOGET)");
-		return 1;
-	}
+int hash_session(struct session_op *sess, int fdc)
+{
+	int i;
+	int err;
+	int alignmask;
 
-	fprintf(stderr, "Testing SHA1 Hash: \n");
-	memset(&sess, 0, sizeof(sess));
-	sess.mac = CRYPTO_SHA1;
-	if (ioctl(fdc, CIOCGSESSION, &sess)) {
+	if (ioctl(fdc, CIOCGSESSION, sess)) {
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
+
 #ifdef CIOCGSESSINFO
-	siop.ses = sess.ses;
-	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
-		perror("ioctl(CIOCGSESSINFO)");
-		return 1;
-	}
-	printf("requested hash CRYPTO_SHA1, got %s with driver %s\n",
-			siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
-	alignmask = siop.alignmask;
+	alignmask = get_alignmask(sess, fdc);
+#else
+	alignmask = 0;
 #endif
 
-	for (i = 256; i <= (64 * 1024); i *= 4) {
-		if (hash_data(&sess, fdc, i, alignmask))
-			break;
+	err = 0;
+	for(i = 0; (err == 0) && (buffer_lengths[i] != 0); i++) {
+		err = hash_data(sess, fdc, buffer_lengths[i], alignmask);
 	}
 
-	fprintf(stderr, "\nTesting SHA256 Hash: \n");
-	memset(&sess, 0, sizeof(sess));
-	sess.mac = CRYPTO_SHA2_256;
-	if (ioctl(fdc, CIOCGSESSION, &sess)) {
-		perror("ioctl(CIOCGSESSION)");
+	if (ioctl(fdc, CIOCFSESSION, sess)) {
+		perror("ioctl(CIOCFSESSION)");
 		return 1;
 	}
-#ifdef CIOCGSESSINFO
-	siop.ses = sess.ses;
-	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
-		perror("ioctl(CIOCGSESSINFO)");
+
+	return err;
+}
+
+int test_sha1(struct session_op *sess, int fdc)
+{
+	fprintf(stderr, "Testing SHA1 Hash: \n");
+	memset(sess, 0, sizeof(sess));
+	sess->mac = CRYPTO_SHA1;
+	return hash_session(sess, fdc);
+}
+
+
+int test_sha256(struct session_op *sess, int fdc)
+{
+	fprintf(stderr, "Testing SHA256 Hash: \n");
+	memset(sess, 0, sizeof(sess));
+	sess->mac = CRYPTO_SHA2_256;
+	return hash_session(sess, fdc);
+}
+
+
+int main(void)
+{
+	int fd;
+	int fdc;
+	int err;
+	int i;
+	struct session_op sess;
+
+	signal(SIGALRM, alarm_handler);
+
+	fd = open("/dev/crypto", O_RDWR, 0);
+	if (fd < 0) {
+		perror("open()");
 		return 1;
 	}
-	printf("requested hash CRYPTO_SHA2_256, got %s with driver %s\n",
-			siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
-	alignmask = siop.alignmask;
-#endif
 
-	for (i = 256; i <= (64 * 1024); i *= 4) {
-		if (hash_data(&sess, fdc, i, alignmask))
-			break;
+	err = ioctl(fd, CRIOGET, &fdc);
+	if (err != 0) {
+		perror("ioctl(CRIOGET)");
+		close(fd);
+		return 1;
 	}
 
+	/* run all tests but return an eventual error */
+	err |= test_sha1(&sess, fdc);
+	err |= test_sha256(&sess, fdc);
+
 	close(fdc);
 	close(fd);
-	return 0;
+	return err;
 }
-- 
2.7.0