]>
git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/aes/gcm_ossl_perf.c
1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
31 #include <stdlib.h> // for rand
32 #include <string.h> // for memcmp
35 #include "ossl_helper.h"
36 #include "gcm_vectors.h"
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"
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"
52 #define TEST_MEM TEST_LEN
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
];
60 void mk_rand_data(uint8_t * data
, uint32_t size
)
63 for (i
= 0; i
< size
; i
++) {
68 int check_data(uint8_t * test
, uint8_t * expected
, uint64_t len
, int vect
, char *data_name
)
73 mismatch
= memcmp(test
, expected
, len
);
76 printf(" v[%d] expected results don't match %s \t\t", vect
, data_name
);
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
);
91 void aes_gcm_perf(void)
93 struct gcm_data gdata
;
94 struct gcm_data gdata256
;
98 ("AES GCM performace parameters plain text length:%d; IV length:%d; ADD length:%d \n",
99 TEST_LEN
, GCM_IV_LEN
, AAD_LENGTH
);
101 mk_rand_data(key128
, sizeof(key128
));
102 mk_rand_data(key256
, sizeof(key256
));
104 // This is only required once for a given key
105 aesni_gcm128_pre(key128
, &gdata
);
106 aesni_gcm256_pre(key256
, &gdata256
);
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)");
125 struct perf start
, stop
;
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
);
134 printf(" aes_gcm_enc" TEST_TYPE_STR
":\t");
135 perf_print(stop
, start
, (long long)TEST_LEN
* i
);
138 struct perf start
, stop
;
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
,
148 printf("openssl_aes_gcm_enc" TEST_TYPE_STR
":\t");
149 perf_print(stop
, start
, (long long)TEST_LEN
* i
);
152 struct perf start
, stop
;
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)");
162 printf(" aes_gcm_dec" TEST_TYPE_STR
":\t");
163 perf_print(stop
, start
, (long long)TEST_LEN
* i
);
166 struct perf start
, stop
;
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
,
176 printf("openssl_aes_gcm_enc" TEST_TYPE_STR
":\t");
177 perf_print(stop
, start
, (long long)TEST_LEN
* i
);
182 struct perf start
, stop
;
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
);
191 printf(" aes_gcm256_enc" TEST_TYPE_STR
":\t");
192 perf_print(stop
, start
, (long long)TEST_LEN
* i
);
196 struct perf start
, stop
;
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
,
206 printf("openssl_aes_256_gcm_enc" TEST_TYPE_STR
":\t");
207 perf_print(stop
, start
, (long long)TEST_LEN
* i
);
211 struct perf start
, stop
;
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)");
222 printf(" aes_gcm256_dec" TEST_TYPE_STR
":\t");
223 perf_print(stop
, start
, (long long)TEST_LEN
* i
);
226 struct perf start
, stop
;
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
,
236 printf("openssl_aes_256_gcm_enc" TEST_TYPE_STR
":\t");
237 perf_print(stop
, start
, (long long)TEST_LEN
* i
);
243 uint8_t const IVend
[] = GCM_IV_END_MARK
;
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
);
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
269 printf("AES gcm ISA-L vs OpenSSL performance\n");