]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /********************************************************************** |
2 | Copyright(c) 2011-2016 Intel Corporation All rights reserved. | |
3 | ||
4 | Redistribution and use in source and binary forms, with or without | |
1e59de90 | 5 | modification, are permitted provided that the following conditions |
7c673cae FG |
6 | are met: |
7 | * Redistributions of source code must retain the above copyright | |
8 | notice, this list of conditions and the following disclaimer. | |
9 | * Redistributions in binary form must reproduce the above copyright | |
10 | notice, this list of conditions and the following disclaimer in | |
11 | the documentation and/or other materials provided with the | |
12 | distribution. | |
13 | * Neither the name of Intel Corporation nor the names of its | |
14 | contributors may be used to endorse or promote products derived | |
15 | from this software without specific prior written permission. | |
16 | ||
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | **********************************************************************/ | |
29 | ||
30 | #include <stdio.h> | |
31 | #include <stdlib.h> // for rand | |
32 | #include <string.h> // for memcmp | |
33 | #include <aes_cbc.h> | |
34 | #include <test.h> | |
35 | #include "ossl_helper.h" | |
36 | ||
37 | //#define CACHED_TEST | |
38 | #ifdef CACHED_TEST | |
39 | // Cached test, loop many times over small dataset | |
40 | # define TEST_LEN 8*1024 | |
41 | # define TEST_LOOPS 400000 | |
42 | # define TEST_TYPE_STR "_warm" | |
43 | #else | |
44 | // Uncached test. Pull from large mem base. | |
45 | # define GT_L3_CACHE 32*1024*1024 /* some number > last level cache */ | |
46 | # define TEST_LEN (2 * GT_L3_CACHE) | |
47 | # define TEST_LOOPS 50 | |
48 | # define TEST_TYPE_STR "_cold" | |
49 | #endif | |
50 | #ifndef TEST_SEED | |
51 | # define TEST_SEED 0x1234 | |
52 | #endif | |
53 | ||
54 | static unsigned char const ic[] = { | |
55 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, | |
56 | 0x0e, 0x0f | |
57 | }; | |
58 | ||
59 | static unsigned char *plaintext, *cbc_plaintext, *cyphertext, *ossl_plaintext, | |
60 | *ossl_cyphertext; | |
61 | static uint8_t test_key[CBC_256_BITS]; | |
62 | ||
63 | void mk_rand_data(uint8_t * data, uint32_t size) | |
64 | { | |
65 | unsigned int i; | |
66 | for (i = 0; i < size; i++) { | |
67 | *data++ = rand(); | |
68 | } | |
69 | } | |
70 | ||
71 | int aes_128_perf(uint8_t * key) | |
72 | { | |
1e59de90 | 73 | int i, ret; |
7c673cae FG |
74 | |
75 | /* Initialize our cipher context, which can use same input vectors */ | |
76 | uint8_t *iv = NULL; | |
77 | struct cbc_key_data *key_data = NULL; | |
78 | ||
1e59de90 TL |
79 | ret = posix_memalign((void **)&iv, 16, (CBC_IV_DATA_LEN)); |
80 | if (ret) { | |
81 | printf("alloc error: Fail"); | |
82 | return 1; | |
83 | } | |
84 | ret = posix_memalign((void **)&key_data, 16, (sizeof(*key_data))); | |
85 | if (ret) { | |
86 | printf("alloc error: Fail"); | |
87 | return 1; | |
88 | } | |
7c673cae FG |
89 | if ((NULL == iv) || (NULL == key_data)) |
90 | return 1; | |
91 | ||
92 | memcpy(iv, ic, CBC_IV_DATA_LEN); | |
93 | ||
94 | aes_cbc_precomp(key, 128, key_data); | |
95 | aes_cbc_enc_128(plaintext, iv, key_data->enc_keys, cyphertext, TEST_LEN); | |
96 | openssl_aes_128_cbc_enc(key, iv, TEST_LEN, plaintext, ossl_cyphertext); | |
97 | ||
98 | { | |
99 | struct perf start, stop; | |
100 | ||
101 | perf_start(&start); | |
102 | for (i = 0; i < TEST_LOOPS; i++) { | |
103 | aes_cbc_enc_128(plaintext, iv, key_data->enc_keys, | |
104 | plaintext, TEST_LEN); | |
105 | } | |
106 | ||
107 | perf_stop(&stop); | |
108 | printf("ISA-L__aes_cbc_128_encode" TEST_TYPE_STR ": "); | |
109 | perf_print(stop, start, (long long)TEST_LEN * i); | |
110 | } | |
111 | { | |
112 | struct perf start, stop; | |
113 | ||
114 | perf_start(&start); | |
115 | for (i = 0; i < TEST_LOOPS; i++) { | |
116 | openssl_aes_128_cbc_enc(key, iv, TEST_LEN, plaintext, plaintext); | |
117 | } | |
118 | ||
119 | perf_stop(&stop); | |
120 | printf("OpenSSL_aes_cbc_128_encode" TEST_TYPE_STR ": "); | |
121 | perf_print(stop, start, (long long)TEST_LEN * i); | |
122 | } | |
123 | ||
124 | { | |
125 | struct perf start, stop; | |
126 | ||
127 | perf_start(&start); | |
128 | for (i = 0; i < TEST_LOOPS; i++) { | |
129 | aes_cbc_dec_128(cyphertext, iv, key_data->dec_keys, | |
130 | cbc_plaintext, TEST_LEN); | |
131 | } | |
132 | ||
133 | perf_stop(&stop); | |
134 | printf("ISA-L__aes_cbc_128_decode" TEST_TYPE_STR ": "); | |
135 | perf_print(stop, start, (long long)TEST_LEN * i); | |
136 | } | |
137 | { | |
138 | struct perf start, stop; | |
139 | ||
140 | perf_start(&start); | |
141 | for (i = 0; i < TEST_LOOPS; i++) { | |
142 | openssl_aes_128_cbc_dec(key, iv, TEST_LEN, | |
143 | ossl_cyphertext, ossl_plaintext); | |
144 | } | |
145 | ||
146 | perf_stop(&stop); | |
147 | printf("OpenSSL_aes_cbc_128_decode" TEST_TYPE_STR ": "); | |
148 | perf_print(stop, start, (long long)TEST_LEN * i); | |
149 | } | |
150 | printf("\n"); | |
151 | return 0; | |
152 | } | |
153 | ||
154 | int aes_192_perf(uint8_t * key) | |
155 | { | |
1e59de90 | 156 | int i, ret; |
7c673cae FG |
157 | uint8_t *iv = NULL; |
158 | struct cbc_key_data *key_data = NULL; | |
159 | ||
1e59de90 TL |
160 | ret = posix_memalign((void **)&iv, 16, (CBC_IV_DATA_LEN)); |
161 | if (ret) { | |
162 | printf("alloc error: Fail"); | |
163 | return 1; | |
164 | } | |
165 | ret = posix_memalign((void **)&key_data, 16, (sizeof(*key_data))); | |
166 | if (ret) { | |
167 | printf("alloc error: Fail"); | |
168 | return 1; | |
169 | } | |
7c673cae FG |
170 | if ((NULL == iv) || (NULL == key_data)) |
171 | return 1; | |
172 | ||
173 | memcpy(iv, ic, CBC_IV_DATA_LEN); | |
174 | aes_cbc_precomp(key, 192, key_data); | |
175 | aes_cbc_enc_192(plaintext, iv, key_data->enc_keys, cyphertext, TEST_LEN); | |
176 | openssl_aes_192_cbc_enc(key, iv, TEST_LEN, plaintext, ossl_cyphertext); | |
177 | ||
178 | { | |
179 | struct perf start, stop; | |
180 | ||
181 | perf_start(&start); | |
182 | for (i = 0; i < TEST_LOOPS; i++) { | |
183 | aes_cbc_enc_192(plaintext, iv, key_data->enc_keys, | |
184 | cyphertext, TEST_LEN); | |
185 | } | |
186 | ||
187 | perf_stop(&stop); | |
188 | printf("ISA-L__aes_cbc_192_encode" TEST_TYPE_STR ": "); | |
189 | perf_print(stop, start, (long long)TEST_LEN * i); | |
190 | } | |
191 | { | |
192 | struct perf start, stop; | |
193 | ||
194 | perf_start(&start); | |
195 | for (i = 0; i < TEST_LOOPS; i++) { | |
196 | openssl_aes_192_cbc_enc(key, iv, TEST_LEN, plaintext, ossl_cyphertext); | |
197 | } | |
198 | ||
199 | perf_stop(&stop); | |
200 | printf("OpenSSL_aes_cbc_192_encode" TEST_TYPE_STR ": "); | |
201 | perf_print(stop, start, (long long)TEST_LEN * i); | |
202 | } | |
203 | ||
204 | { | |
205 | struct perf start, stop; | |
206 | ||
207 | perf_start(&start); | |
208 | for (i = 0; i < TEST_LOOPS; i++) { | |
209 | aes_cbc_dec_192(cyphertext, iv, key_data->dec_keys, | |
210 | cbc_plaintext, TEST_LEN); | |
211 | } | |
212 | ||
213 | perf_stop(&stop); | |
214 | printf("ISA-L__aes_cbc_192_decode" TEST_TYPE_STR ": "); | |
215 | perf_print(stop, start, (long long)TEST_LEN * i); | |
216 | } | |
217 | { | |
218 | struct perf start, stop; | |
219 | ||
220 | perf_start(&start); | |
221 | for (i = 0; i < TEST_LOOPS; i++) { | |
222 | openssl_aes_192_cbc_dec(key, iv, TEST_LEN, | |
223 | ossl_cyphertext, ossl_plaintext); | |
224 | } | |
225 | ||
226 | perf_stop(&stop); | |
227 | printf("OpenSSL_aes_cbc_192_decode" TEST_TYPE_STR ": "); | |
228 | perf_print(stop, start, (long long)TEST_LEN * i); | |
229 | } | |
230 | printf("\n"); | |
231 | return 0; | |
232 | } | |
233 | ||
234 | int aes_256_perf(uint8_t * key) | |
235 | { | |
1e59de90 | 236 | int i, ret; |
7c673cae FG |
237 | uint8_t *iv = NULL; |
238 | struct cbc_key_data *key_data = NULL; | |
239 | ||
1e59de90 TL |
240 | ret = posix_memalign((void **)&iv, 16, (CBC_IV_DATA_LEN)); |
241 | if (ret) { | |
242 | printf("alloc error: Fail"); | |
243 | return 1; | |
244 | } | |
245 | ret = posix_memalign((void **)&key_data, 16, (sizeof(*key_data))); | |
246 | if (ret) { | |
247 | printf("alloc error: Fail"); | |
248 | return 1; | |
249 | } | |
7c673cae FG |
250 | if ((NULL == iv) || (NULL == key_data)) |
251 | return 1; | |
252 | ||
253 | aes_cbc_precomp(key, 256, key_data); | |
254 | memcpy(iv, ic, CBC_IV_DATA_LEN); | |
255 | aes_cbc_enc_256(plaintext, iv, key_data->enc_keys, cyphertext, TEST_LEN); | |
256 | openssl_aes_256_cbc_enc(key, iv, TEST_LEN, plaintext, ossl_cyphertext); | |
257 | ||
258 | { | |
259 | struct perf start, stop; | |
260 | ||
261 | perf_start(&start); | |
262 | for (i = 0; i < TEST_LOOPS; i++) { | |
263 | aes_cbc_enc_256(plaintext, iv, key_data->enc_keys, | |
264 | cyphertext, TEST_LEN); | |
265 | } | |
266 | ||
267 | perf_stop(&stop); | |
1e59de90 | 268 | printf("ISA-L__aes_cbc_256_encode" TEST_TYPE_STR ": "); |
7c673cae FG |
269 | perf_print(stop, start, (long long)TEST_LEN * i); |
270 | } | |
271 | { | |
272 | struct perf start, stop; | |
273 | ||
274 | perf_start(&start); | |
275 | for (i = 0; i < TEST_LOOPS; i++) { | |
276 | openssl_aes_256_cbc_enc(key, iv, TEST_LEN, plaintext, ossl_cyphertext); | |
277 | } | |
278 | ||
279 | perf_stop(&stop); | |
280 | printf("OpenSSL_aes_cbc_256_encode" TEST_TYPE_STR ": "); | |
281 | perf_print(stop, start, (long long)TEST_LEN * i); | |
282 | } | |
283 | ||
284 | { | |
285 | struct perf start, stop; | |
286 | ||
287 | perf_start(&start); | |
288 | for (i = 0; i < TEST_LOOPS; i++) { | |
289 | aes_cbc_dec_256(cyphertext, iv, key_data->dec_keys, | |
290 | cbc_plaintext, TEST_LEN); | |
291 | } | |
292 | ||
293 | perf_stop(&stop); | |
1e59de90 | 294 | printf("ISA-L__aes_cbc_256_decode" TEST_TYPE_STR ": "); |
7c673cae FG |
295 | perf_print(stop, start, (long long)TEST_LEN * i); |
296 | } | |
297 | { | |
298 | struct perf start, stop; | |
299 | ||
300 | perf_start(&start); | |
301 | for (i = 0; i < TEST_LOOPS; i++) { | |
302 | openssl_aes_256_cbc_dec(key, iv, TEST_LEN, | |
303 | ossl_cyphertext, ossl_plaintext); | |
304 | } | |
305 | ||
306 | perf_stop(&stop); | |
307 | printf("OpenSSL_aes_cbc_256_decode" TEST_TYPE_STR ": "); | |
308 | perf_print(stop, start, (long long)TEST_LEN * i); | |
309 | } | |
310 | printf("\n"); | |
311 | return 0; | |
312 | } | |
313 | ||
314 | int main(void) | |
315 | { | |
316 | uint32_t OK = 0; | |
317 | ||
318 | srand(TEST_SEED); | |
319 | ||
320 | plaintext = malloc(TEST_LEN); | |
321 | cbc_plaintext = malloc(TEST_LEN); | |
322 | cyphertext = malloc(TEST_LEN); | |
323 | ossl_plaintext = malloc(TEST_LEN); | |
324 | ossl_cyphertext = malloc(TEST_LEN); | |
325 | if (NULL == plaintext || NULL == cyphertext || NULL == cbc_plaintext | |
326 | || NULL == ossl_plaintext || NULL == ossl_cyphertext) { | |
327 | printf("malloc of testsize:0x%x failed\n", TEST_LEN); | |
328 | return 1; | |
329 | } | |
330 | ||
331 | mk_rand_data(plaintext, TEST_LEN); | |
332 | mk_rand_data(test_key, sizeof(test_key)); | |
333 | printf("AES CBC ISA-L vs OpenSSL performance:\n"); | |
334 | OK += aes_128_perf(test_key); | |
335 | OK += aes_192_perf(test_key); | |
336 | OK += aes_256_perf(test_key); | |
337 | ||
338 | return OK; | |
339 | } |