]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/intel-ipsec-mb/LibTestApp/do_test.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / intel-ipsec-mb / LibTestApp / do_test.h
1 /*
2 * Copyright (c) 2012-2018, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * 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 the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdint.h>
32
33 #ifndef DO_TEST_H
34 #define DO_TEST_H
35
36 static unsigned char key[] = {
37 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
38 0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
39 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
40 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
41 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
42 0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
43 0xa0
44 };
45
46 static unsigned char text[] = {
47 0x53,0x61,0x6d,0x70,0x6c,0x65,0x20,0x23,0x34
48 };
49
50 static unsigned char hmac12[] = {
51 0x9e,0xa8,0x86,0xef,0xe2,0x68,0xdb,0xec,0xce,0x42,0x0c,0x75
52 };
53
54 #define KEYSIZE sizeof(key)
55 #define TEXTSIZE sizeof(text);
56
57 static unsigned char plain[] = {
58 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
59 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
60 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
61 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
62 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
63 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
64 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
65 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
66 };
67
68 static unsigned char key128[] = {
69 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
70 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
71 };
72
73 /* static unsigned char key256[] = { */
74 /* 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe, */
75 /* 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81, */
76 /* 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7, */
77 /* 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 */
78 /* }; */
79
80 static unsigned char ic[] = {
81 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
82 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
83 };
84
85 static unsigned char cipherCBC128[] = {
86 0x76,0x49,0xab,0xac,0x81,0x19,0xb2,0x46,
87 0xce,0xe9,0x8e,0x9b,0x12,0xe9,0x19,0x7d,
88 0x50,0x86,0xcb,0x9b,0x50,0x72,0x19,0xee,
89 0x95,0xdb,0x11,0x3a,0x91,0x76,0x78,0xb2,
90 0x73,0xbe,0xd6,0xb8,0xe3,0xc1,0x74,0x3b,
91 0x71,0x16,0xe6,0x9e,0x22,0x22,0x95,0x16,
92 0x3f,0xf1,0xca,0xa1,0x68,0x1f,0xac,0x09,
93 0x12,0x0e,0xca,0x30,0x75,0x86,0xe1,0xa7
94 };
95
96 /* static unsigned char cipherCBC256[] = { */
97 /* 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba, */
98 /* 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6, */
99 /* 0x9c,0xfc,0x4e,0x96,0x7e,0xdb,0x80,0x8d, */
100 /* 0x67,0x9f,0x77,0x7b,0xc6,0x70,0x2c,0x7d, */
101 /* 0x39,0xf2,0x33,0x69,0xa9,0xd9,0xba,0xcf, */
102 /* 0xa5,0x30,0xe2,0x63,0x04,0x23,0x14,0x61, */
103 /* 0xb2,0xeb,0x05,0xe2,0xc3,0x9b,0xe9,0xfc, */
104 /* 0xda,0x6c,0x19,0x07,0x8c,0x6a,0x9d,0x1b */
105 /* }; */
106
107 #define NUMBLOCKS 4
108 #define NUMBYTES (NUMBLOCKS * 16)
109
110
111 static int
112 known_answer_test(MB_MGR *mb_mgr)
113 {
114 uint8_t test_buf[NUMBYTES];
115 uint8_t buf[64];
116 DECLARE_ALIGNED(uint32_t enc_keys[15*4], 16);
117 DECLARE_ALIGNED(uint32_t dec_keys[15*4], 16);
118 DECLARE_ALIGNED(uint8_t ipad_hash[5*4], 16);
119 DECLARE_ALIGNED(uint8_t opad_hash[5*4], 16);
120 JOB_AES_HMAC *job;
121 uint8_t iv[16];
122 uint8_t digest[12];
123 uint32_t i;
124
125 /* compute ipad hash */
126 for (i=0; i<64; i++)
127 buf[i] = 0x36;
128 for (i=0; i<KEYSIZE; i++)
129 buf[i] ^= key[i];
130 IMB_SHA1_ONE_BLOCK(mb_mgr, buf, ipad_hash);
131
132 /* compute opad hash */
133 for (i=0; i<64; i++)
134 buf[i] = 0x5c;
135 for (i=0; i<KEYSIZE; i++)
136 buf[i] ^= key[i];
137 IMB_SHA1_ONE_BLOCK(mb_mgr, buf, opad_hash);
138
139
140 /* Expand key */
141 IMB_AES_KEYEXP_128(mb_mgr, key128, enc_keys, dec_keys);
142
143
144 /* test AES128 Dec */
145 job = IMB_GET_NEXT_JOB(mb_mgr);
146
147 job->aes_enc_key_expanded = enc_keys;
148 job->aes_dec_key_expanded = dec_keys;
149 job->cipher_direction = DECRYPT;
150 job->chain_order = HASH_CIPHER;
151 job->dst = test_buf;
152 job->aes_key_len_in_bytes = 16;
153 job->auth_tag_output = digest;
154 job->auth_tag_output_len_in_bytes = 12;
155 memcpy(iv, ic, sizeof(iv));
156 job->iv = iv;
157 job->iv_len_in_bytes = 16;
158 job->src = cipherCBC128;
159 job->cipher_start_src_offset_in_bytes = 0;
160 job->msg_len_to_cipher_in_bytes = NUMBYTES;
161 job->hash_start_src_offset_in_bytes = text - job->src;
162 job->msg_len_to_hash_in_bytes = TEXTSIZE;
163 job->u.HMAC._hashed_auth_key_xor_ipad = ipad_hash;
164 job->u.HMAC._hashed_auth_key_xor_opad = opad_hash;
165 job->cipher_mode = CBC;
166 job->hash_alg = SHA1;
167
168 job = IMB_SUBMIT_JOB(mb_mgr);
169 if (job) {
170 printf("Unexpected return from submit_job\n");
171 return 1;
172 }
173 job = IMB_FLUSH_JOB(mb_mgr);
174 if (!job) {
175 printf("Unexpected null return from flush_job\n");
176 return 1;
177 }
178 for (i=0; i<NUMBYTES; i++) {
179 if (test_buf[i] != plain[i]) {
180 printf("AES128 Dec mismatch on byte %d\n", i);
181 return 1;
182 }
183 }
184
185 for (i=0; i<12; i++) {
186 if (digest[i] != hmac12[i]) {
187 printf("HMAC/SHA1 mismatch on byte %d\n", i);
188 return 1;
189 }
190 }
191 printf("Known answer passes\n");
192 return 0;
193 }
194
195 static void
196 test_aux_func(MB_MGR *mgr)
197 {
198 /* test aux functions */
199 uint128_t keys[15];
200 static uint8_t buf[4096+20];
201
202 uint32_t digest1[8];
203 uint64_t digest3[8];
204 DECLARE_ALIGNED(uint32_t k1_exp[15*4], 16);
205 DECLARE_ALIGNED(uint32_t k2[4], 16);
206 DECLARE_ALIGNED(uint32_t k3[4], 16);
207
208 printf("Testing aux funcs\n");
209
210 IMB_SHA1_ONE_BLOCK(mgr, buf, digest1);
211 IMB_SHA224_ONE_BLOCK(mgr, buf, digest1);
212 IMB_SHA256_ONE_BLOCK(mgr, buf, digest1);
213 IMB_SHA384_ONE_BLOCK(mgr, buf, digest3);
214 IMB_SHA512_ONE_BLOCK(mgr, buf, digest3);
215 IMB_MD5_ONE_BLOCK(mgr, buf, digest1);
216 IMB_AES_XCBC_KEYEXP(mgr, buf + 1, k1_exp, k2, k3);
217 IMB_AES_KEYEXP_128(mgr, keys, k1_exp, k1_exp);
218 IMB_AES_KEYEXP_192(mgr, keys, k1_exp, k1_exp);
219 IMB_AES_KEYEXP_256(mgr, keys, k1_exp, k1_exp);
220 }
221
222 static int
223 do_test(MB_MGR *mb_mgr)
224 {
225 uint32_t size;
226 JOB_AES_HMAC *job;
227 static uint128_t IV = {0,0};
228 static uint32_t ipad[5], opad[5], digest[3];
229 uint128_t keys[15];
230 static uint8_t buf[4096+20];
231
232 for (size = 32; size < 4096; size += 16) {
233 job = IMB_GET_NEXT_JOB(mb_mgr);
234
235 job->msg_len_to_cipher_in_bytes = size;
236 job->msg_len_to_hash_in_bytes = size + 20;
237 job->hash_start_src_offset_in_bytes = 0;
238 job->cipher_start_src_offset_in_bytes = 20;
239
240 job->auth_tag_output = (uint8_t*) digest;
241 job->auth_tag_output_len_in_bytes = 12;
242 job->u.HMAC._hashed_auth_key_xor_ipad = (uint8_t*)ipad;
243 job->u.HMAC._hashed_auth_key_xor_opad = (uint8_t*)opad;
244
245 job->aes_enc_key_expanded =
246 job->aes_dec_key_expanded = (uint32_t*) keys;
247 job->src = buf;
248 job->dst = buf + 20;
249 job->iv = (uint8_t *) &IV;
250 job->iv_len_in_bytes = 16;
251
252 job->cipher_mode = CBC;
253 job->hash_alg = SHA1;
254
255 switch (rand() % 3) {
256 case 0:
257 job->aes_key_len_in_bytes = 16;
258 break;
259 case 1:
260 job->aes_key_len_in_bytes = 24;
261 break;
262 default:
263 job->aes_key_len_in_bytes = 32;
264 break;
265 }
266
267 switch (rand() % 4) {
268 case 0:
269 job->cipher_direction = ENCRYPT;
270 job->chain_order = HASH_CIPHER;
271 break;
272 case 1:
273 job->cipher_direction = ENCRYPT;
274 job->chain_order = CIPHER_HASH;
275 break;
276 case 2:
277 job->cipher_direction = DECRYPT;
278 job->chain_order = CIPHER_HASH;
279 break;
280 case 3:
281 job->cipher_direction = DECRYPT;
282 job->chain_order = HASH_CIPHER;
283 break;
284 }
285 job = IMB_SUBMIT_JOB(mb_mgr);
286 while (job) {
287 job = IMB_GET_COMPLETED_JOB(mb_mgr);
288 } /* end while (job) */
289 } /* end for size */
290
291 while ((job = IMB_FLUSH_JOB(mb_mgr)) != NULL) {
292 do {
293 job = IMB_GET_COMPLETED_JOB(mb_mgr);
294 } while (job);
295 }
296
297 test_aux_func(mb_mgr);
298
299 return 0;
300 }
301
302 #endif /* DO_TEST_H */