1 /*****************************************************************************
2 Copyright (c) 2017-2018, Intel Corporation
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
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.
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 *****************************************************************************/
32 #include <intel-ipsec-mb.h>
34 #include "customop_test.h"
36 #define DIM(_a) (sizeof(_a) / sizeof(_a[0]))
40 #define TRACE(fmt, ...) fprintf(stderr, "%s:%d "fmt, \
41 __FUNCTION__, __LINE__, __VA_ARGS__)
43 #define TRACE(fmt, ...) fprintf(stderr, "%s:%d "fmt, \
44 __func__, __LINE__, __VA_ARGS__)
47 # define TRACE(fmt, ...)
50 struct cipher_attr_s
{
69 DECLARE_ALIGNED(uint8_t enc_key
[16*16], 64);
70 DECLARE_ALIGNED(uint8_t dec_key
[16*16], 64);
73 const struct cipher_attr_s
*cipher
;
74 const struct auth_attr_s
*auth
;
80 * addon cipher function
83 cipher_addon(struct JOB_AES_HMAC
*job
)
86 struct test_vec_s
*node
= job
->user_data
;
89 TRACE("Seq:%u Cipher Addon cipher:%s auth:%s\n",
90 node
->seq
, node
->cipher
->name
, node
->auth
->name
);
92 if (job
->cipher_direction
== ENCRYPT
)
93 memset(job
->dst
, 1, job
->msg_len_to_cipher_in_bytes
);
95 memset(job
->dst
, 2, job
->msg_len_to_cipher_in_bytes
);
97 return 0; /* success */
101 * addon hash function
104 hash_addon(struct JOB_AES_HMAC
*job
)
107 struct test_vec_s
*node
= job
->user_data
;
110 TRACE("Seq:%u Auth Addon cipher:%s auth:%s\n",
111 node
->seq
, node
->cipher
->name
, node
->auth
->name
);
113 memset(job
->auth_tag_output
, 3, job
->auth_tag_output_len_in_bytes
);
114 return 0; /* success */
118 * test cipher functions
120 static const struct auth_attr_s auth_attr_tab
[] = {
121 { "SHA1", SHA1
, 12 },
122 { "SHA224", SHA_224
, 14 },
123 { "SHA256", SHA_256
, 16 },
124 { "SHA384", SHA_384
, 24 },
125 { "SHA512", SHA_512
, 32 },
127 { "CUSTOM_HASH", CUSTOM_HASH
, 16 }
131 * test hash functions
133 static const struct cipher_attr_s cipher_attr_tab
[] = {
134 { "CBC128", CBC
, 16, 16 },
135 { "CBC192", CBC
, 24, 16 },
136 { "CBC256", CBC
, 32, 16 },
137 { "CUSTOM_CIPHER", CUSTOM_CIPHER
, 32, 12 },
138 { "CTR128", CNTR
, 16, 12 },
139 { "CTR192", CNTR
, 24, 12 },
140 { "CTR256", CNTR
, 32, 12 }
144 job_check(const struct JOB_AES_HMAC
*job
)
147 struct test_vec_s
*done
= job
->user_data
;
150 TRACE("done Seq:%u Cipher:%s Auth:%s\n",
151 done
->seq
, done
->cipher
->name
, done
->auth
->name
);
153 if (job
->status
!= STS_COMPLETED
) {
154 TRACE("failed job status:%d\n", job
->status
);
157 if (job
->cipher_mode
== CUSTOM_CIPHER
) {
158 if (job
->cipher_direction
== ENCRYPT
) {
161 for (i
= 0; i
< job
->msg_len_to_cipher_in_bytes
; i
++) {
162 if (job
->dst
[i
] != 1) {
163 TRACE("NG add-on encryption %u\n", i
);
167 TRACE("Addon encryption passes Seq:%u\n", done
->seq
);
171 for (i
= 0; i
< job
->msg_len_to_cipher_in_bytes
; i
++) {
172 if (job
->dst
[i
] != 2) {
173 TRACE("NG add-on decryption %u\n", i
);
177 TRACE("Addon decryption passes Seq:%u\n", done
->seq
);
181 if (job
->hash_alg
== CUSTOM_HASH
) {
184 for (i
= 0; i
< job
->auth_tag_output_len_in_bytes
; i
++) {
185 if (job
->auth_tag_output
[i
] != 3) {
186 TRACE("NG add-on hashing %u\n", i
);
190 TRACE("Addon hashing passes Seq:%u\n", done
->seq
);
197 customop_test(struct MB_MGR
*mgr
)
199 struct test_vec_s test_tab
[DIM(cipher_attr_tab
) * DIM(auth_attr_tab
)];
200 struct JOB_AES_HMAC
*job
;
204 for (i
= 0, seq
= 0; i
< DIM(cipher_attr_tab
); i
++) {
205 for (j
= 0; j
< DIM(auth_attr_tab
); j
++) {
206 assert(seq
< DIM(test_tab
));
207 test_tab
[seq
].seq
= seq
;
208 test_tab
[seq
].cipher
= &cipher_attr_tab
[i
];
209 test_tab
[seq
].auth
= &auth_attr_tab
[j
];
215 for (i
= 0; i
< seq
; i
++) {
216 struct test_vec_s
*node
= &test_tab
[i
];
218 while ((job
= IMB_GET_NEXT_JOB(mgr
)) == NULL
) {
219 job
= IMB_FLUSH_JOB(mgr
);
220 result
|= job_check(job
);
223 job
->cipher_func
= cipher_addon
;
224 job
->hash_func
= hash_addon
;
226 job
->aes_enc_key_expanded
= node
->enc_key
;
227 job
->aes_dec_key_expanded
= node
->dec_key
;
228 job
->aes_key_len_in_bytes
= node
->cipher
->key_len
;
229 job
->src
= node
->txt
;
230 job
->dst
= node
->txt
;
231 job
->cipher_start_src_offset_in_bytes
= 16;
232 job
->msg_len_to_cipher_in_bytes
= sizeof(node
->txt
);
233 job
->hash_start_src_offset_in_bytes
= 0;
234 job
->msg_len_to_hash_in_bytes
=
235 sizeof(node
->txt
) + sizeof(node
->iv
);
237 job
->iv_len_in_bytes
= node
->cipher
->iv_len
;
238 job
->auth_tag_output
= node
->tag
;
239 job
->auth_tag_output_len_in_bytes
= node
->auth
->tag_len
;
241 job
->u
.HMAC
._hashed_auth_key_xor_ipad
= node
->ipad
;
242 job
->u
.HMAC
._hashed_auth_key_xor_opad
= node
->opad
;
243 job
->cipher_mode
= node
->cipher
->mode
;
244 job
->cipher_direction
= ENCRYPT
;
245 job
->chain_order
= CIPHER_HASH
;
246 job
->hash_alg
= node
->auth
->hash
;
247 job
->user_data
= node
;
249 job
= IMB_SUBMIT_JOB(mgr
);
251 result
|= job_check(job
);
252 job
= IMB_GET_COMPLETED_JOB(mgr
);
256 while ((job
= IMB_FLUSH_JOB(mgr
)) != NULL
)
257 result
|= job_check(job
);
260 for (i
= 0; i
< seq
; i
++) {
261 struct test_vec_s
*node
= &test_tab
[i
];
263 while ((job
= IMB_GET_NEXT_JOB(mgr
)) == NULL
) {
264 job
= IMB_FLUSH_JOB(mgr
);
265 result
|= job_check(job
);
268 job
->cipher_func
= cipher_addon
;
269 job
->hash_func
= hash_addon
;
271 job
->aes_enc_key_expanded
= node
->enc_key
;
272 job
->aes_dec_key_expanded
= node
->dec_key
;
273 job
->aes_key_len_in_bytes
= node
->cipher
->key_len
;
274 job
->src
= node
->txt
;
275 job
->dst
= node
->txt
;
276 job
->cipher_start_src_offset_in_bytes
= 16;
277 job
->msg_len_to_cipher_in_bytes
= sizeof(node
->txt
);
278 job
->hash_start_src_offset_in_bytes
= 0;
279 job
->msg_len_to_hash_in_bytes
=
280 sizeof(node
->txt
) + sizeof(node
->iv
);
282 job
->iv_len_in_bytes
= node
->cipher
->iv_len
;
283 job
->auth_tag_output
= node
->tag
;
284 job
->auth_tag_output_len_in_bytes
= node
->auth
->tag_len
;
286 job
->u
.HMAC
._hashed_auth_key_xor_ipad
= node
->ipad
;
287 job
->u
.HMAC
._hashed_auth_key_xor_opad
= node
->opad
;
288 job
->cipher_mode
= node
->cipher
->mode
;
289 job
->cipher_direction
= DECRYPT
;
290 job
->chain_order
= HASH_CIPHER
;
291 job
->hash_alg
= node
->auth
->hash
;
292 job
->user_data
= node
;
294 job
= IMB_SUBMIT_JOB(mgr
);
296 result
|= job_check(job
);
297 job
= IMB_GET_COMPLETED_JOB(mgr
);
301 while ((job
= IMB_FLUSH_JOB(mgr
)) != NULL
)
302 result
|= job_check(job
);
305 fprintf(stdout
, "Custom cipher/auth test failed!\n");
307 fprintf(stdout
, "Custom cipher/auth test passed\n");