]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/aes/gcm_ossl_perf.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / aes / gcm_ossl_perf.c
1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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_gcm.h>
34 #include <test.h>
35 #include "ossl_helper.h"
36 #include "gcm_vectors.h"
37
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
51 #define AAD_LENGTH 16
52 #define TEST_MEM TEST_LEN
53
54 static unsigned char *plaintext, *gcm_plaintext, *cyphertext, *ossl_plaintext,
55 *ossl_cyphertext, *gcm_tag, *ossl_tag, *IV, *AAD;
56 static uint8_t key128[GCM_128_KEY_LEN];
57 static uint8_t key256[GCM_256_KEY_LEN];
58 uint8_t iv_len = 0;
59
60 void mk_rand_data(uint8_t * data, uint32_t size)
61 {
62 unsigned int i;
63 for (i = 0; i < size; i++) {
64 *data++ = rand();
65 }
66 }
67
68 int check_data(uint8_t * test, uint8_t * expected, uint64_t len, int vect, char *data_name)
69 {
70 int mismatch;
71 int OK = 1;
72
73 mismatch = memcmp(test, expected, len);
74 if (mismatch) {
75 OK = 0;
76 printf(" v[%d] expected results don't match %s \t\t", vect, data_name);
77 {
78 uint64_t a;
79 for (a = 0; a < len; a++) {
80 if (test[a] != expected[a]) {
81 printf(" '%x' != '%x' at %lx of %lx\n",
82 test[a], expected[a], a, len);
83 break;
84 }
85 }
86 }
87 }
88 return OK;
89 }
90
91 void aes_gcm_perf(void)
92 {
93 struct gcm_data gdata;
94 struct gcm_data gdata256;
95 int i;
96
97 printf
98 ("AES GCM performace parameters plain text length:%d; IV length:%d; ADD length:%d \n",
99 TEST_LEN, GCM_IV_LEN, AAD_LENGTH);
100
101 mk_rand_data(key128, sizeof(key128));
102 mk_rand_data(key256, sizeof(key256));
103
104 // This is only required once for a given key
105 aesni_gcm128_pre(key128, &gdata);
106 aesni_gcm256_pre(key256, &gdata256);
107
108 // Preload code cache
109 aesni_gcm128_enc(&gdata, cyphertext, plaintext, TEST_LEN, IV, AAD, AAD_LENGTH,
110 gcm_tag, MAX_TAG_LEN);
111 openssl_aes_gcm_enc(key128, IV, iv_len, AAD, AAD_LENGTH, ossl_tag, MAX_TAG_LEN,
112 plaintext, TEST_LEN, ossl_cyphertext);
113 check_data(cyphertext, ossl_cyphertext, TEST_LEN, 0,
114 "ISA-L vs OpenSSL 128 key cypher text (C)");
115 check_data(gcm_tag, ossl_tag, MAX_TAG_LEN, 0, "ISA-L vs OpenSSL 128 tag (T)");
116 aesni_gcm256_enc(&gdata256, cyphertext, plaintext, TEST_LEN, IV, AAD, AAD_LENGTH,
117 gcm_tag, MAX_TAG_LEN);
118 openssl_aes_256_gcm_enc(key256, IV, iv_len, AAD, AAD_LENGTH, ossl_tag, MAX_TAG_LEN,
119 plaintext, TEST_LEN, ossl_cyphertext);
120 check_data(cyphertext, ossl_cyphertext, TEST_LEN, 0,
121 "ISA-L vs OpenSSL 256 cypher text (C)");
122 check_data(gcm_tag, ossl_tag, MAX_TAG_LEN, 0, "ISA-L vs OpenSSL 256 tag (T)");
123
124 {
125 struct perf start, stop;
126
127 perf_start(&start);
128 for (i = 0; i < TEST_LOOPS; i++) {
129 aesni_gcm128_enc(&gdata, cyphertext, plaintext, TEST_LEN, IV, AAD,
130 AAD_LENGTH, gcm_tag, MAX_TAG_LEN);
131 }
132
133 perf_stop(&stop);
134 printf(" aes_gcm_enc" TEST_TYPE_STR ":\t");
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_gcm_enc(key128, IV, iv_len, AAD, AAD_LENGTH,
143 ossl_tag, MAX_TAG_LEN, plaintext, TEST_LEN,
144 cyphertext);
145 }
146
147 perf_stop(&stop);
148 printf("openssl_aes_gcm_enc" TEST_TYPE_STR ":\t");
149 perf_print(stop, start, (long long)TEST_LEN * i);
150 }
151 {
152 struct perf start, stop;
153
154 perf_start(&start);
155 for (i = 0; i < TEST_LOOPS; i++) {
156 aesni_gcm128_dec(&gdata, plaintext, cyphertext, TEST_LEN, IV,
157 AAD, AAD_LENGTH, gcm_tag, MAX_TAG_LEN);
158 check_data(gcm_tag, gcm_tag, MAX_TAG_LEN, 0, "ISA-L check of tag (T)");
159 }
160
161 perf_stop(&stop);
162 printf(" aes_gcm_dec" TEST_TYPE_STR ":\t");
163 perf_print(stop, start, (long long)TEST_LEN * i);
164 }
165 {
166 struct perf start, stop;
167
168 perf_start(&start);
169 for (i = 0; i < TEST_LOOPS; i++) {
170 openssl_aes_gcm_dec(key128, IV, iv_len, AAD, AAD_LENGTH,
171 ossl_tag, MAX_TAG_LEN, cyphertext, TEST_LEN,
172 plaintext);
173 }
174
175 perf_stop(&stop);
176 printf("openssl_aes_gcm_enc" TEST_TYPE_STR ":\t");
177 perf_print(stop, start, (long long)TEST_LEN * i);
178 }
179
180 printf("\n");
181 {
182 struct perf start, stop;
183
184 perf_start(&start);
185 for (i = 0; i < TEST_LOOPS; i++) {
186 aesni_gcm256_enc(&gdata256, cyphertext, plaintext, TEST_LEN, IV, AAD,
187 AAD_LENGTH, gcm_tag, MAX_TAG_LEN);
188 }
189
190 perf_stop(&stop);
191 printf(" aes_gcm256_enc" TEST_TYPE_STR ":\t");
192 perf_print(stop, start, (long long)TEST_LEN * i);
193 }
194
195 {
196 struct perf start, stop;
197
198 perf_start(&start);
199 for (i = 0; i < TEST_LOOPS; i++) {
200 openssl_aes_256_gcm_enc(key256, IV, iv_len, AAD, AAD_LENGTH,
201 ossl_tag, MAX_TAG_LEN, plaintext, TEST_LEN,
202 cyphertext);
203 }
204
205 perf_stop(&stop);
206 printf("openssl_aes_256_gcm_enc" TEST_TYPE_STR ":\t");
207 perf_print(stop, start, (long long)TEST_LEN * i);
208 }
209
210 {
211 struct perf start, stop;
212
213 perf_start(&start);
214 for (i = 0; i < TEST_LOOPS; i++) {
215 aesni_gcm256_dec(&gdata256, plaintext, cyphertext, TEST_LEN, IV,
216 AAD, AAD_LENGTH, gcm_tag, MAX_TAG_LEN);
217 check_data(gcm_tag, gcm_tag, MAX_TAG_LEN, 0,
218 "ISA-L check of 256 tag (T)");
219 }
220
221 perf_stop(&stop);
222 printf(" aes_gcm256_dec" TEST_TYPE_STR ":\t");
223 perf_print(stop, start, (long long)TEST_LEN * i);
224 }
225 {
226 struct perf start, stop;
227
228 perf_start(&start);
229 for (i = 0; i < TEST_LOOPS; i++) {
230 openssl_aes_256_gcm_dec(key256, IV, iv_len, AAD, AAD_LENGTH,
231 ossl_tag, MAX_TAG_LEN, cyphertext, TEST_LEN,
232 plaintext);
233 }
234
235 perf_stop(&stop);
236 printf("openssl_aes_256_gcm_enc" TEST_TYPE_STR ":\t");
237 perf_print(stop, start, (long long)TEST_LEN * i);
238 }
239 }
240
241 int main(void)
242 {
243 uint8_t const IVend[] = GCM_IV_END_MARK;
244 uint32_t OK = 1;
245
246 plaintext = malloc(TEST_LEN);
247 gcm_plaintext = malloc(TEST_LEN);
248 cyphertext = malloc(TEST_LEN);
249 ossl_plaintext = malloc(TEST_LEN + 16);
250 ossl_cyphertext = malloc(TEST_LEN);
251 gcm_tag = malloc(MAX_TAG_LEN);
252 ossl_tag = malloc(MAX_TAG_LEN);
253 AAD = malloc(AAD_LENGTH);
254 IV = malloc(GCM_IV_LEN);
255 if ((NULL == plaintext) || (NULL == cyphertext) || (NULL == gcm_plaintext)
256 || (NULL == ossl_plaintext) || (NULL == ossl_cyphertext)
257 || (NULL == gcm_tag) || (NULL == ossl_tag) || (NULL == AAD) || (NULL == IV)) {
258 printf("malloc of testsize:0x%x failed\n", TEST_LEN);
259 return -1;
260 }
261
262 mk_rand_data(plaintext, TEST_LEN);
263 mk_rand_data(AAD, AAD_LENGTH);
264 mk_rand_data(IV, GCM_IV_LEN);
265 memcpy(&IV[GCM_IV_END_START], IVend, sizeof(IVend));
266 iv_len = GCM_IV_LEN - sizeof(IVend); //end marker not part of IV length
267
268 aes_gcm_perf();
269 printf("AES gcm ISA-L vs OpenSSL performance\n");
270
271 return !OK;
272 }