]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/aes/xts_128_rand.c
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / aes / xts_128_rand.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 <aes_keyexp.h>
35
36 #define TEST_LEN (1024*1024)
37 #define TEST_SIZE (4096)
38 #ifndef RANDOMS
39 # define RANDOMS 10
40 #endif
41
42 void mk_rand_data(unsigned char *k1, unsigned char *k2, unsigned char *k3, unsigned char *p,
43 int n)
44 {
45 int i;
46 for (i = 0; i < 16; i++) {
47 *k1++ = rand();
48 *k2++ = rand();
49 *k3++ = rand();
50 }
51 for (i = 0; i < n; i++)
52 *p++ = rand();
53
54 }
55
56 int main(void)
57 {
58 int t, n;
59
60 unsigned char key1[16], key2[16], tinit[16];
61 unsigned char *pt, *ct, *dt;
62
63 int align, size, min_size;
64 unsigned char *efence_pt;
65 unsigned char *efence_ct;
66 unsigned char *efence_dt;
67
68 unsigned char *origin_pt;
69 unsigned char *origin_ct;
70 unsigned char *origin_dt;
71
72 unsigned char key1_exp_enc[16 * 11], key1_exp_dec[16 * 11];
73 unsigned char key2_exp_tw[16 * 11];
74 int i;
75
76 printf("aes_xts_128 enc/dec rand test, %d sets of %d max: ", RANDOMS, TEST_LEN);
77 pt = malloc(TEST_LEN);
78 ct = malloc(TEST_LEN);
79 dt = malloc(TEST_LEN);
80
81 if (NULL == pt || NULL == ct || NULL == dt) {
82 printf("malloc of testsize failed\n");
83 return -1;
84 }
85
86 mk_rand_data(key1, key2, tinit, pt, TEST_LEN);
87 XTS_AES_128_enc(key2, key1, tinit, TEST_LEN, pt, ct);
88 XTS_AES_128_dec(key2, key1, tinit, TEST_LEN, ct, dt);
89
90 if (memcmp(pt, dt, TEST_LEN)) {
91 printf("fail\n");
92 return -1;
93 }
94 putchar('.');
95
96 // Do tests with random data, keys and message size
97 for (t = 0; t < RANDOMS; t++) {
98 n = rand() % (TEST_LEN);
99 if (n < 17)
100 continue;
101
102 mk_rand_data(key1, key2, tinit, pt, n);
103 XTS_AES_128_enc(key2, key1, tinit, n, pt, ct);
104 XTS_AES_128_dec(key2, key1, tinit, n, ct, dt);
105
106 if (memcmp(pt, dt, n)) {
107 printf("fail rand %d, size %d\n", t, n);
108 return -1;
109 }
110 putchar('.');
111 fflush(0);
112 }
113
114 // Run tests at end of buffer for Electric Fence
115 align = 1;
116 min_size = 16;
117 for (size = 0; size <= TEST_SIZE - min_size; size += align) {
118
119 // Line up TEST_SIZE from end
120 efence_pt = pt + TEST_LEN - TEST_SIZE + size;
121 efence_ct = ct + TEST_LEN - TEST_SIZE + size;
122 efence_dt = dt + TEST_LEN - TEST_SIZE + size;
123
124 mk_rand_data(key1, key2, tinit, efence_pt, TEST_SIZE - size);
125 XTS_AES_128_enc(key2, key1, tinit, TEST_SIZE - size, efence_pt, efence_ct);
126 XTS_AES_128_dec(key2, key1, tinit, TEST_SIZE - size, efence_ct, efence_dt);
127
128 if (memcmp(efence_pt, efence_dt, TEST_SIZE - size)) {
129 printf("efence: fail size %d\n", TEST_SIZE - size);
130 return -1;
131 }
132 putchar('.');
133 fflush(0);
134 }
135
136 origin_pt = malloc(TEST_LEN);
137 origin_ct = malloc(TEST_LEN);
138 origin_dt = malloc(TEST_LEN);
139 if (NULL == origin_pt || NULL == origin_ct || NULL == origin_dt) {
140 printf("malloc of testsize failed\n");
141 return -1;
142 }
143 // For data lengths from 0 to 15 bytes, the functions return without any error
144 // codes, without reading or writing any data.
145 for (size = TEST_SIZE - min_size + align; size <= TEST_SIZE; size += align) {
146
147 // Line up TEST_SIZE from end
148 efence_pt = pt + TEST_LEN - TEST_SIZE + size;
149 efence_ct = ct + TEST_LEN - TEST_SIZE + size;
150 efence_dt = dt + TEST_LEN - TEST_SIZE + size;
151
152 mk_rand_data(key1, key2, tinit, efence_pt, TEST_SIZE - size);
153 memcpy(efence_ct, efence_pt, TEST_SIZE - size);
154 memcpy(efence_dt, efence_pt, TEST_SIZE - size);
155 memcpy(origin_pt, efence_pt, TEST_SIZE - size);
156 memcpy(origin_ct, efence_ct, TEST_SIZE - size);
157 memcpy(origin_dt, efence_dt, TEST_SIZE - size);
158
159 XTS_AES_128_enc(key2, key1, tinit, TEST_SIZE - size, efence_pt, efence_ct);
160 XTS_AES_128_dec(key2, key1, tinit, TEST_SIZE - size, efence_ct, efence_dt);
161
162 if (memcmp(efence_pt, origin_pt, TEST_SIZE - size)) {
163 printf("efence_pt: fail size %d\n", TEST_SIZE - size);
164 return -1;
165 }
166 if (memcmp(efence_ct, origin_ct, TEST_SIZE - size)) {
167 printf("efence_ct: fail size %d\n", TEST_SIZE - size);
168 return -1;
169 }
170 if (memcmp(efence_dt, origin_dt, TEST_SIZE - size)) {
171 printf("efence_dt: fail size %d\n", TEST_SIZE - size);
172 return -1;
173 }
174 putchar('.');
175 fflush(0);
176 }
177
178 for (i = 0; i < 16 * 11; i++) {
179 key2_exp_tw[i] = rand();
180 }
181
182 for (size = 0; size <= TEST_SIZE - min_size; size += align) {
183
184 // Line up TEST_SIZE from end
185 efence_pt = pt + TEST_LEN - TEST_SIZE + size;
186 efence_ct = ct + TEST_LEN - TEST_SIZE + size;
187 efence_dt = dt + TEST_LEN - TEST_SIZE + size;
188
189 mk_rand_data(key1, key2, tinit, efence_pt, TEST_SIZE - size);
190 aes_keyexp_128(key1, key1_exp_enc, key1_exp_dec);
191
192 XTS_AES_128_enc_expanded_key(key2_exp_tw, key1_exp_enc, tinit,
193 TEST_SIZE - size, efence_pt, efence_ct);
194 XTS_AES_128_dec_expanded_key(key2_exp_tw, key1_exp_dec, tinit,
195 TEST_SIZE - size, efence_ct, efence_dt);
196
197 if (memcmp(efence_pt, efence_dt, TEST_SIZE - size)) {
198 printf("efence_expanded_key: fail size %d\n", TEST_SIZE - size);
199 return -1;
200 }
201 putchar('.');
202 fflush(0);
203 }
204
205 // For data lengths from 0 to 15 bytes, the functions return without any error
206 // codes, without reading or writing any data.
207 for (size = TEST_SIZE - min_size + align; size <= TEST_SIZE; size += align) {
208
209 // Line up TEST_SIZE from end
210 efence_pt = pt + TEST_LEN - TEST_SIZE + size;
211 efence_ct = ct + TEST_LEN - TEST_SIZE + size;
212 efence_dt = dt + TEST_LEN - TEST_SIZE + size;
213
214 mk_rand_data(key1, key2, tinit, efence_pt, TEST_SIZE - size);
215 memcpy(efence_ct, efence_pt, TEST_SIZE - size);
216 memcpy(efence_dt, efence_pt, TEST_SIZE - size);
217 memcpy(origin_pt, efence_pt, TEST_SIZE - size);
218 memcpy(origin_ct, efence_ct, TEST_SIZE - size);
219 memcpy(origin_dt, efence_dt, TEST_SIZE - size);
220
221 aes_keyexp_128(key1, key1_exp_enc, key1_exp_dec);
222
223 XTS_AES_128_enc_expanded_key(key2_exp_tw, key1_exp_enc, tinit,
224 TEST_SIZE - size, efence_pt, efence_ct);
225 XTS_AES_128_dec_expanded_key(key2_exp_tw, key1_exp_dec, tinit,
226 TEST_SIZE - size, efence_ct, efence_dt);
227
228 if (memcmp(efence_pt, origin_pt, TEST_SIZE - size)) {
229 printf("efence_expanded_key for pt: fail size %d\n", TEST_SIZE - size);
230 return -1;
231 }
232 if (memcmp(efence_ct, origin_ct, TEST_SIZE - size)) {
233 printf("efence_expanded_key for ct: fail size %d\n", TEST_SIZE - size);
234 return -1;
235 }
236 if (memcmp(efence_dt, origin_dt, TEST_SIZE - size)) {
237 printf("efence_expanded_key for dt: fail size %d\n", TEST_SIZE - size);
238 return -1;
239 }
240 putchar('.');
241 fflush(0);
242 }
243
244 printf("Pass\n");
245
246 return 0;
247 }