]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/aes/xts_128_enc_ossl_perf.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / aes / xts_128_enc_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_xts.h"
34 #include "test.h"
35
36 #include <openssl/evp.h>
37
38 //#define CACHED_TEST
39 #ifdef CACHED_TEST
40 // Cached test, loop many times over small dataset
41 # define TEST_LEN 8*1024
42 # define TEST_LOOPS 400000
43 # define TEST_TYPE_STR "_warm"
44 #else
45 // Uncached test. Pull from large mem base.
46 # define GT_L3_CACHE 32*1024*1024 /* some number > last level cache */
47 # define TEST_LEN (2 * GT_L3_CACHE)
48 # define TEST_LOOPS 50
49 # define TEST_TYPE_STR "_cold"
50 #endif
51
52 #define TEST_MEM TEST_LEN
53
54 void xts128_mk_rand_data(unsigned char *k1, unsigned char *k2, unsigned char *k3,
55 unsigned char *p, int n)
56 {
57 int i;
58 for (i = 0; i < 16; i++) {
59 *k1++ = rand();
60 *k2++ = rand();
61 *k3++ = rand();
62 }
63 for (i = 0; i < n; i++)
64 *p++ = rand();
65
66 }
67
68 static inline
69 int openssl_aes_128_xts_enc(EVP_CIPHER_CTX * ctx, unsigned char *key, unsigned char *iv,
70 int len, unsigned char *pt, unsigned char *ct)
71 {
72 int outlen, tmplen;
73 if (!EVP_EncryptInit_ex(ctx, EVP_aes_128_xts(), NULL, key, iv))
74 printf("\n ERROR!! \n");
75 if (!EVP_EncryptUpdate(ctx, ct, &outlen, (const unsigned char *)pt, len))
76 printf("\n ERROR!! \n");
77 if (!EVP_EncryptFinal_ex(ctx, ct + outlen, &tmplen))
78 printf("\n ERROR!! \n");
79
80 return 0;
81 }
82
83 int main(void)
84 {
85 int i;
86
87 unsigned char key1[16], key2[16], tinit[16];
88 unsigned char *pt, *ct, *refct;
89 struct perf start, stop;
90 unsigned char keyssl[32]; /* SSL takes both keys together */
91
92 /* Initialise our cipher context, which can use same input vectors */
93 EVP_CIPHER_CTX ctx;
94 EVP_CIPHER_CTX_init(&ctx);
95
96 printf("aes_xts_128_enc_perf:\n");
97
98 pt = malloc(TEST_LEN);
99 ct = malloc(TEST_LEN);
100 refct = malloc(TEST_LEN);
101
102 if (NULL == pt || NULL == ct || NULL == refct) {
103 printf("malloc of testsize failed\n");
104 return -1;
105 }
106
107 xts128_mk_rand_data(key1, key2, tinit, pt, TEST_LEN);
108
109 /* Set up key for the SSL engine */
110 for (i = 0; i < 16; i++) {
111 keyssl[i] = key1[i];
112 keyssl[i + 16] = key2[i];
113 }
114
115 /* Encrypt and compare output */
116 XTS_AES_128_enc(key2, key1, tinit, TEST_LEN, pt, ct);
117 openssl_aes_128_xts_enc(&ctx, keyssl, tinit, TEST_LEN, pt, refct);
118 if (memcmp(ct, refct, TEST_LEN)) {
119 printf("ISA-L and OpenSSL results don't match\n");
120 return -1;
121 }
122
123 /* Time ISA-L encryption */
124 perf_start(&start);
125 for (i = 0; i < TEST_LOOPS; i++)
126 XTS_AES_128_enc(key2, key1, tinit, TEST_LEN, pt, ct);
127 perf_stop(&stop);
128
129 printf("aes_xts_128_enc" TEST_TYPE_STR ": ");
130 perf_print(stop, start, (long long)TEST_LEN * i);
131
132 /* Time OpenSSL encryption */
133 perf_start(&start);
134 for (i = 0; i < TEST_LOOPS; i++)
135 openssl_aes_128_xts_enc(&ctx, keyssl, tinit, TEST_LEN, pt, refct);
136 perf_stop(&stop);
137
138 printf("aes_xts_128_openssl_enc" TEST_TYPE_STR ": ");
139 perf_print(stop, start, (long long)TEST_LEN * i);
140
141 return 0;
142 }