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 #ifndef GT_L3_CACHE 39 #define GT_L3_CACHE 32 * 1024 * 1024 /* some number > last level cache */ 40 #endif 41 42 #if !defined(COLD_TEST) && !defined(TEST_CUSTOM) 43 // Cached test, loop many times over small dataset 44 #define TEST_LEN 8 * 1024 45 #define TEST_LOOPS 400000 46 #define TEST_TYPE_STR "_warm" 47 #elif defined(COLD_TEST) 48 // Uncached test. Pull from large mem base. 49 #define TEST_LEN (2 * GT_L3_CACHE) 50 #define TEST_LOOPS 50 51 #define TEST_TYPE_STR "_cold" 52 #endif 53 54 #define AAD_LENGTH 16 55 #define TEST_MEM TEST_LEN 56 57 static unsigned char *plaintext, *gcm_plaintext, *ciphertext, *ossl_plaintext, *ossl_ciphertext, 58 *gcm_tag, *ossl_tag, *IV, *AAD; 59 static uint8_t key128[ISAL_GCM_128_KEY_LEN]; 60 static uint8_t key256[ISAL_GCM_256_KEY_LEN]; 61 uint8_t iv_len = 0; 62 63 void 64 mk_rand_data(uint8_t *data, uint32_t size) 65 { 66 unsigned int i; 67 for (i = 0; i < size; i++) { 68 *data++ = rand(); 69 } 70 } 71 72 int 73 check_data(uint8_t *test, uint8_t *expected, uint64_t len, int vect, char *data_name) 74 { 75 int mismatch; 76 int OK = 1; 77 78 mismatch = memcmp(test, expected, len); 79 if (mismatch) { 80 OK = 0; 81 printf(" v[%d] expected results don't match %s \t\t", vect, data_name); 82 { 83 uint64_t a; 84 for (a = 0; a < len; a++) { 85 if (test[a] != expected[a]) { 86 printf(" '%x' != '%x' at %lx of %lx\n", test[a], 87 expected[a], a, len); 88 break; 89 } 90 } 91 } 92 } 93 return OK; 94 } 95 96 void 97 aes_gcm_perf(void) 98 { 99 struct gcm_key_data gkey, gkey256; 100 struct gcm_context_data gctx; 101 int i; 102 103 printf("AES GCM performance parameters plain text length:%d; IV length:%d; ADD length:%d " 104 "\n", 105 TEST_LEN, ISAL_GCM_IV_LEN, AAD_LENGTH); 106 107 mk_rand_data(key128, sizeof(key128)); 108 mk_rand_data(key256, sizeof(key256)); 109 110 // This is only required once for a given key 111 isal_aes_gcm_pre_128(key128, &gkey); 112 isal_aes_gcm_pre_256(key256, &gkey256); 113 114 // Preload code cache 115 isal_aes_gcm_enc_128(&gkey, &gctx, ciphertext, plaintext, TEST_LEN, IV, AAD, AAD_LENGTH, 116 gcm_tag, ISAL_GCM_MAX_TAG_LEN); 117 openssl_aes_gcm_enc(key128, IV, iv_len, AAD, AAD_LENGTH, ossl_tag, ISAL_GCM_MAX_TAG_LEN, 118 plaintext, TEST_LEN, ossl_ciphertext); 119 check_data(ciphertext, ossl_ciphertext, TEST_LEN, 0, 120 "ISA-L vs OpenSSL 128 key cypher text (C)"); 121 check_data(gcm_tag, ossl_tag, ISAL_GCM_MAX_TAG_LEN, 0, "ISA-L vs OpenSSL 128 tag (T)"); 122 isal_aes_gcm_enc_256(&gkey256, &gctx, ciphertext, plaintext, TEST_LEN, IV, AAD, AAD_LENGTH, 123 gcm_tag, ISAL_GCM_MAX_TAG_LEN); 124 openssl_aes_256_gcm_enc(key256, IV, iv_len, AAD, AAD_LENGTH, ossl_tag, ISAL_GCM_MAX_TAG_LEN, 125 plaintext, TEST_LEN, ossl_ciphertext); 126 check_data(ciphertext, ossl_ciphertext, TEST_LEN, 0, 127 "ISA-L vs OpenSSL 256 cypher text (C)"); 128 check_data(gcm_tag, ossl_tag, ISAL_GCM_MAX_TAG_LEN, 0, "ISA-L vs OpenSSL 256 tag (T)"); 129 130 { 131 struct perf start, stop; 132 133 perf_start(&start); 134 for (i = 0; i < TEST_LOOPS; i++) { 135 isal_aes_gcm_enc_128(&gkey, &gctx, ciphertext, plaintext, TEST_LEN, IV, AAD, 136 AAD_LENGTH, gcm_tag, ISAL_GCM_MAX_TAG_LEN); 137 } 138 139 perf_stop(&stop); 140 printf(" isal_aes_gcm_enc" TEST_TYPE_STR ":\t"); 141 perf_print(stop, start, (long long) TEST_LEN * i); 142 } 143 { 144 struct perf start, stop; 145 146 perf_start(&start); 147 for (i = 0; i < TEST_LOOPS; i++) { 148 openssl_aes_gcm_enc(key128, IV, iv_len, AAD, AAD_LENGTH, ossl_tag, 149 ISAL_GCM_MAX_TAG_LEN, plaintext, TEST_LEN, ciphertext); 150 } 151 152 perf_stop(&stop); 153 printf("openssl_aes_gcm_enc" TEST_TYPE_STR ":\t"); 154 perf_print(stop, start, (long long) TEST_LEN * i); 155 } 156 { 157 struct perf start, stop; 158 159 perf_start(&start); 160 for (i = 0; i < TEST_LOOPS; i++) { 161 isal_aes_gcm_dec_128(&gkey, &gctx, plaintext, ciphertext, TEST_LEN, IV, AAD, 162 AAD_LENGTH, gcm_tag, ISAL_GCM_MAX_TAG_LEN); 163 check_data(gcm_tag, gcm_tag, ISAL_GCM_MAX_TAG_LEN, 0, 164 "ISA-L check of tag (T)"); 165 } 166 167 perf_stop(&stop); 168 printf(" isal_aes_gcm_dec" TEST_TYPE_STR ":\t"); 169 perf_print(stop, start, (long long) TEST_LEN * i); 170 } 171 { 172 struct perf start, stop; 173 174 perf_start(&start); 175 for (i = 0; i < TEST_LOOPS; i++) { 176 openssl_aes_gcm_dec(key128, IV, iv_len, AAD, AAD_LENGTH, ossl_tag, 177 ISAL_GCM_MAX_TAG_LEN, ciphertext, TEST_LEN, plaintext); 178 } 179 180 perf_stop(&stop); 181 printf("openssl_aes_gcm_dec" TEST_TYPE_STR ":\t"); 182 perf_print(stop, start, (long long) TEST_LEN * i); 183 } 184 185 printf("\n"); 186 { 187 struct perf start, stop; 188 189 perf_start(&start); 190 for (i = 0; i < TEST_LOOPS; i++) { 191 isal_aes_gcm_enc_256(&gkey256, &gctx, ciphertext, plaintext, TEST_LEN, IV, 192 AAD, AAD_LENGTH, gcm_tag, ISAL_GCM_MAX_TAG_LEN); 193 } 194 195 perf_stop(&stop); 196 printf(" aes_gcm256_enc" TEST_TYPE_STR ":\t"); 197 perf_print(stop, start, (long long) TEST_LEN * i); 198 } 199 200 { 201 struct perf start, stop; 202 203 perf_start(&start); 204 for (i = 0; i < TEST_LOOPS; i++) { 205 openssl_aes_256_gcm_enc(key256, IV, iv_len, AAD, AAD_LENGTH, ossl_tag, 206 ISAL_GCM_MAX_TAG_LEN, plaintext, TEST_LEN, 207 ciphertext); 208 } 209 210 perf_stop(&stop); 211 printf("openssl_aes_256_gcm_enc" TEST_TYPE_STR ":\t"); 212 perf_print(stop, start, (long long) TEST_LEN * i); 213 } 214 215 { 216 struct perf start, stop; 217 218 perf_start(&start); 219 for (i = 0; i < TEST_LOOPS; i++) { 220 isal_aes_gcm_dec_256(&gkey256, &gctx, plaintext, ciphertext, TEST_LEN, IV, 221 AAD, AAD_LENGTH, gcm_tag, ISAL_GCM_MAX_TAG_LEN); 222 check_data(gcm_tag, gcm_tag, ISAL_GCM_MAX_TAG_LEN, 0, 223 "ISA-L check of 256 tag (T)"); 224 } 225 226 perf_stop(&stop); 227 printf(" aes_gcm256_dec" TEST_TYPE_STR ":\t"); 228 perf_print(stop, start, (long long) TEST_LEN * i); 229 } 230 { 231 struct perf start, stop; 232 233 perf_start(&start); 234 for (i = 0; i < TEST_LOOPS; i++) { 235 openssl_aes_256_gcm_dec(key256, IV, iv_len, AAD, AAD_LENGTH, ossl_tag, 236 ISAL_GCM_MAX_TAG_LEN, ciphertext, TEST_LEN, 237 plaintext); 238 } 239 240 perf_stop(&stop); 241 printf("openssl_aes_256_gcm_dec" TEST_TYPE_STR ":\t"); 242 perf_print(stop, start, (long long) TEST_LEN * i); 243 } 244 } 245 246 int 247 main(void) 248 { 249 uint32_t OK = 1; 250 251 plaintext = malloc(TEST_LEN); 252 gcm_plaintext = malloc(TEST_LEN); 253 ciphertext = malloc(TEST_LEN); 254 ossl_plaintext = malloc(TEST_LEN + 16); 255 ossl_ciphertext = malloc(TEST_LEN); 256 gcm_tag = malloc(ISAL_GCM_MAX_TAG_LEN); 257 ossl_tag = malloc(ISAL_GCM_MAX_TAG_LEN); 258 AAD = malloc(AAD_LENGTH); 259 IV = malloc(ISAL_GCM_IV_LEN); 260 if ((NULL == plaintext) || (NULL == ciphertext) || (NULL == gcm_plaintext) || 261 (NULL == ossl_plaintext) || (NULL == ossl_ciphertext) || (NULL == gcm_tag) || 262 (NULL == ossl_tag) || (NULL == AAD) || (NULL == IV)) { 263 printf("malloc of testsize:0x%x failed\n", TEST_LEN); 264 return -1; 265 } 266 267 mk_rand_data(plaintext, TEST_LEN); 268 mk_rand_data(AAD, AAD_LENGTH); 269 mk_rand_data(IV, ISAL_GCM_IV_LEN); 270 iv_len = ISAL_GCM_IV_LEN; 271 272 aes_gcm_perf(); 273 printf("AES gcm ISA-L vs OpenSSL performance\n"); 274 275 return !OK; 276 } 277