summaryrefslogtreecommitdiffstats
path: root/src/crypto/isa-l/isa-l_crypto/aes/gcm_ossl_perf.c
blob: a9e9c5914dfa906c65e813d3b45ed4c915c3c57a (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**********************************************************************
  Copyright(c) 2011-2016 Intel Corporation All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the
      distribution.
    * Neither the name of Intel Corporation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************/

#include <stdio.h>
#include <stdlib.h>		// for rand
#include <string.h>		// for memcmp
#include <aes_gcm.h>
#include <test.h>
#include "ossl_helper.h"
#include "gcm_vectors.h"

#ifdef CACHED_TEST
// Cached test, loop many times over small dataset
# define TEST_LEN     8*1024
# define TEST_LOOPS   400000
# define TEST_TYPE_STR "_warm"
#else
// Uncached test.  Pull from large mem base.
#  define GT_L3_CACHE  32*1024*1024	/* some number > last level cache */
#  define TEST_LEN     (2 * GT_L3_CACHE)
#  define TEST_LOOPS   50
#  define TEST_TYPE_STR "_cold"
#endif

#define AAD_LENGTH   16
#define TEST_MEM TEST_LEN

static unsigned char *plaintext, *gcm_plaintext, *cyphertext, *ossl_plaintext,
    *ossl_cyphertext, *gcm_tag, *ossl_tag, *IV, *AAD;
static uint8_t key128[GCM_128_KEY_LEN];
static uint8_t key256[GCM_256_KEY_LEN];
uint8_t iv_len = 0;

void mk_rand_data(uint8_t * data, uint32_t size)
{
	unsigned int i;
	for (i = 0; i < size; i++) {
		*data++ = rand();
	}
}

int check_data(uint8_t * test, uint8_t * expected, uint64_t len, int vect, char *data_name)
{
	int mismatch;
	int OK = 1;

	mismatch = memcmp(test, expected, len);
	if (mismatch) {
		OK = 0;
		printf("  v[%d] expected results don't match %s \t\t", vect, data_name);
		{
			uint64_t a;
			for (a = 0; a < len; a++) {
				if (test[a] != expected[a]) {
					printf(" '%x' != '%x' at %lx of %lx\n",
					       test[a], expected[a], a, len);
					break;
				}
			}
		}
	}
	return OK;
}

void aes_gcm_perf(void)
{
	struct gcm_key_data gkey, gkey256;
	struct gcm_context_data gctx;
	int i;

	printf
	    ("AES GCM performance parameters plain text length:%d; IV length:%d; ADD length:%d \n",
	     TEST_LEN, GCM_IV_LEN, AAD_LENGTH);

	mk_rand_data(key128, sizeof(key128));
	mk_rand_data(key256, sizeof(key256));

	// This is only required once for a given key
	aes_gcm_pre_128(key128, &gkey);
	aes_gcm_pre_256(key256, &gkey256);

	// Preload code cache
	aes_gcm_enc_128(&gkey, &gctx, cyphertext, plaintext, TEST_LEN, IV, AAD, AAD_LENGTH,
			gcm_tag, MAX_TAG_LEN);
	openssl_aes_gcm_enc(key128, IV, iv_len, AAD, AAD_LENGTH, ossl_tag, MAX_TAG_LEN,
			    plaintext, TEST_LEN, ossl_cyphertext);
	check_data(cyphertext, ossl_cyphertext, TEST_LEN, 0,
		   "ISA-L vs OpenSSL 128 key cypher text (C)");
	check_data(gcm_tag, ossl_tag, MAX_TAG_LEN, 0, "ISA-L vs OpenSSL 128 tag (T)");
	aes_gcm_enc_256(&gkey256, &gctx, cyphertext, plaintext, TEST_LEN, IV, AAD, AAD_LENGTH,
			gcm_tag, MAX_TAG_LEN);
	openssl_aes_256_gcm_enc(key256, IV, iv_len, AAD, AAD_LENGTH, ossl_tag, MAX_TAG_LEN,
				plaintext, TEST_LEN, ossl_cyphertext);
	check_data(cyphertext, ossl_cyphertext, TEST_LEN, 0,
		   "ISA-L vs OpenSSL 256 cypher text (C)");
	check_data(gcm_tag, ossl_tag, MAX_TAG_LEN, 0, "ISA-L vs OpenSSL 256 tag (T)");

	{
		struct perf start, stop;

		perf_start(&start);
		for (i = 0; i < TEST_LOOPS; i++) {
			aes_gcm_enc_128(&gkey, &gctx, cyphertext, plaintext, TEST_LEN, IV, AAD,
					AAD_LENGTH, gcm_tag, MAX_TAG_LEN);
		}

		perf_stop(&stop);
		printf("        aes_gcm_enc" TEST_TYPE_STR ":\t");
		perf_print(stop, start, (long long)TEST_LEN * i);
	}
	{
		struct perf start, stop;

		perf_start(&start);
		for (i = 0; i < TEST_LOOPS; i++) {
			openssl_aes_gcm_enc(key128, IV, iv_len, AAD, AAD_LENGTH,
					    ossl_tag, MAX_TAG_LEN, plaintext, TEST_LEN,
					    cyphertext);
		}

		perf_stop(&stop);
		printf("openssl_aes_gcm_enc" TEST_TYPE_STR ":\t");
		perf_print(stop, start, (long long)TEST_LEN * i);
	}
	{
		struct perf start, stop;

		perf_start(&start);
		for (i = 0; i < TEST_LOOPS; i++) {
			aes_gcm_dec_128(&gkey, &gctx, plaintext, cyphertext, TEST_LEN, IV,
					AAD, AAD_LENGTH, gcm_tag, MAX_TAG_LEN);
			check_data(gcm_tag, gcm_tag, MAX_TAG_LEN, 0, "ISA-L check of tag (T)");
		}

		perf_stop(&stop);
		printf("        aes_gcm_dec" TEST_TYPE_STR ":\t");
		perf_print(stop, start, (long long)TEST_LEN * i);
	}
	{
		struct perf start, stop;

		perf_start(&start);
		for (i = 0; i < TEST_LOOPS; i++) {
			openssl_aes_gcm_dec(key128, IV, iv_len, AAD, AAD_LENGTH,
					    ossl_tag, MAX_TAG_LEN, cyphertext, TEST_LEN,
					    plaintext);
		}

		perf_stop(&stop);
		printf("openssl_aes_gcm_dec" TEST_TYPE_STR ":\t");
		perf_print(stop, start, (long long)TEST_LEN * i);
	}

	printf("\n");
	{
		struct perf start, stop;

		perf_start(&start);
		for (i = 0; i < TEST_LOOPS; i++) {
			aes_gcm_enc_256(&gkey256, &gctx, cyphertext, plaintext, TEST_LEN, IV,
					AAD, AAD_LENGTH, gcm_tag, MAX_TAG_LEN);
		}

		perf_stop(&stop);
		printf("         aes_gcm256_enc" TEST_TYPE_STR ":\t");
		perf_print(stop, start, (long long)TEST_LEN * i);
	}

	{
		struct perf start, stop;

		perf_start(&start);
		for (i = 0; i < TEST_LOOPS; i++) {
			openssl_aes_256_gcm_enc(key256, IV, iv_len, AAD, AAD_LENGTH,
						ossl_tag, MAX_TAG_LEN, plaintext, TEST_LEN,
						cyphertext);
		}

		perf_stop(&stop);
		printf("openssl_aes_256_gcm_enc" TEST_TYPE_STR ":\t");
		perf_print(stop, start, (long long)TEST_LEN * i);
	}

	{
		struct perf start, stop;

		perf_start(&start);
		for (i = 0; i < TEST_LOOPS; i++) {
			aes_gcm_dec_256(&gkey256, &gctx, plaintext, cyphertext, TEST_LEN, IV,
					AAD, AAD_LENGTH, gcm_tag, MAX_TAG_LEN);
			check_data(gcm_tag, gcm_tag, MAX_TAG_LEN, 0,
				   "ISA-L check of 256 tag (T)");
		}

		perf_stop(&stop);
		printf("         aes_gcm256_dec" TEST_TYPE_STR ":\t");
		perf_print(stop, start, (long long)TEST_LEN * i);
	}
	{
		struct perf start, stop;

		perf_start(&start);
		for (i = 0; i < TEST_LOOPS; i++) {
			openssl_aes_256_gcm_dec(key256, IV, iv_len, AAD, AAD_LENGTH,
						ossl_tag, MAX_TAG_LEN, cyphertext, TEST_LEN,
						plaintext);
		}

		perf_stop(&stop);
		printf("openssl_aes_256_gcm_dec" TEST_TYPE_STR ":\t");
		perf_print(stop, start, (long long)TEST_LEN * i);
	}
}

int main(void)
{
	uint8_t const IVend[] = GCM_IV_END_MARK;
	uint32_t OK = 1;

	plaintext = malloc(TEST_LEN);
	gcm_plaintext = malloc(TEST_LEN);
	cyphertext = malloc(TEST_LEN);
	ossl_plaintext = malloc(TEST_LEN + 16);
	ossl_cyphertext = malloc(TEST_LEN);
	gcm_tag = malloc(MAX_TAG_LEN);
	ossl_tag = malloc(MAX_TAG_LEN);
	AAD = malloc(AAD_LENGTH);
	IV = malloc(GCM_IV_LEN);
	if ((NULL == plaintext) || (NULL == cyphertext) || (NULL == gcm_plaintext)
	    || (NULL == ossl_plaintext) || (NULL == ossl_cyphertext)
	    || (NULL == gcm_tag) || (NULL == ossl_tag) || (NULL == AAD) || (NULL == IV)) {
		printf("malloc of testsize:0x%x failed\n", TEST_LEN);
		return -1;
	}

	mk_rand_data(plaintext, TEST_LEN);
	mk_rand_data(AAD, AAD_LENGTH);
	mk_rand_data(IV, GCM_IV_LEN);
	memcpy(&IV[GCM_IV_END_START], IVend, sizeof(IVend));
	iv_len = GCM_IV_LEN - sizeof(IVend);	//end marker not part of IV length

	aes_gcm_perf();
	printf("AES gcm ISA-L vs OpenSSL performance\n");

	return !OK;
}