summaryrefslogtreecommitdiffstats
path: root/src/spdk/intel-ipsec-mb/LibTestApp/customop_test.c
blob: 711d9bcf5a7e1c55dd0b4791bb37345a31e2dc84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*****************************************************************************
 Copyright (c) 2017-2018, Intel Corporation

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

     * Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
     * Neither the name of Intel Corporation nor the names of its contributors
       may be used to endorse or promote products derived from this software
       without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <intel-ipsec-mb.h>

#include "customop_test.h"

#define DIM(_a)	(sizeof(_a) / sizeof(_a[0]))

#ifdef DEBUG
#ifdef _WIN32
#define TRACE(fmt, ...)	fprintf(stderr, "%s:%d "fmt, \
                                __FUNCTION__, __LINE__, __VA_ARGS__)
#else
#define TRACE(fmt, ...)	fprintf(stderr, "%s:%d "fmt, \
                                __func__, __LINE__, __VA_ARGS__)
#endif
#else
# define TRACE(fmt, ...)
#endif

struct cipher_attr_s {
        const char *name;
        JOB_CIPHER_MODE mode;
        unsigned key_len;
        unsigned iv_len;
};

struct auth_attr_s {
        const char *name;
        JOB_HASH_ALG hash;
        unsigned tag_len;
};

struct test_vec_s {
        uint8_t iv[16];
        uint8_t txt[64];
        uint8_t tag[32];
        uint8_t verify[32];

        DECLARE_ALIGNED(uint8_t enc_key[16*16], 64);
        DECLARE_ALIGNED(uint8_t dec_key[16*16], 64);
        uint8_t ipad[256];
        uint8_t opad[256];
        const struct cipher_attr_s *cipher;
        const struct auth_attr_s *auth;

        unsigned seq;
};

/*
 * addon cipher function
 */
static int
cipher_addon(struct JOB_AES_HMAC *job)
{
#ifdef DEBUG
        struct test_vec_s *node = job->user_data;
#endif

        TRACE("Seq:%u Cipher Addon cipher:%s auth:%s\n",
              node->seq, node->cipher->name, node->auth->name);

        if (job->cipher_direction == ENCRYPT)
                memset(job->dst, 1, job->msg_len_to_cipher_in_bytes);
        else
                memset(job->dst, 2, job->msg_len_to_cipher_in_bytes);

        return 0;	/* success */
}

/*
 * addon hash function
 */
static int
hash_addon(struct JOB_AES_HMAC *job)
{
#ifdef DEBUG
        struct test_vec_s *node = job->user_data;
#endif

        TRACE("Seq:%u Auth Addon cipher:%s auth:%s\n",
              node->seq, node->cipher->name, node->auth->name);

        memset(job->auth_tag_output, 3, job->auth_tag_output_len_in_bytes);
        return 0;	/* success */
}

/*
 * test cipher functions
 */
static const struct auth_attr_s auth_attr_tab[] = {
        { "SHA1", SHA1, 12 },
        { "SHA224", SHA_224, 14 },
        { "SHA256", SHA_256, 16 },
        { "SHA384", SHA_384, 24 },
        { "SHA512", SHA_512, 32 },
        { "MD5", MD5, 12 },
        { "CUSTOM_HASH", CUSTOM_HASH, 16 }
};

/*
 * test hash functions
 */
static const struct cipher_attr_s cipher_attr_tab[] = {
        { "CBC128", CBC, 16, 16 },
        { "CBC192", CBC, 24, 16 },
        { "CBC256", CBC, 32, 16 },
        { "CUSTOM_CIPHER", CUSTOM_CIPHER, 32, 12 },
        { "CTR128", CNTR, 16, 12 },
        { "CTR192", CNTR, 24, 12 },
        { "CTR256", CNTR, 32, 12 }
};

static int
job_check(const struct JOB_AES_HMAC *job)
{
#ifdef DEBUG
        struct test_vec_s *done = job->user_data;
#endif

        TRACE("done Seq:%u Cipher:%s Auth:%s\n",
              done->seq, done->cipher->name, done->auth->name);

        if (job->status != STS_COMPLETED) {
                TRACE("failed job status:%d\n", job->status);
                return -1;
        }
        if (job->cipher_mode == CUSTOM_CIPHER) {
                if (job->cipher_direction == ENCRYPT) {
                        unsigned i;

                        for (i = 0; i < job->msg_len_to_cipher_in_bytes; i++) {
                                if (job->dst[i] != 1) {
                                        TRACE("NG add-on encryption %u\n", i);
                                        return -1;
                                }
                        }
                        TRACE("Addon encryption passes Seq:%u\n", done->seq);
                } else {
                        unsigned i;

                        for (i = 0; i < job->msg_len_to_cipher_in_bytes; i++) {
                                if (job->dst[i] != 2) {
                                        TRACE("NG add-on decryption %u\n", i);
                                        return -1;
                                }
                        }
                        TRACE("Addon decryption passes Seq:%u\n", done->seq);
                }
        }

        if (job->hash_alg == CUSTOM_HASH) {
                unsigned i;

                for (i = 0; i < job->auth_tag_output_len_in_bytes; i++) {
                        if (job->auth_tag_output[i] != 3) {
                                TRACE("NG add-on hashing %u\n", i);
                                return -1;
                        }
                }
                TRACE("Addon hashing passes Seq:%u\n", done->seq);
        }
        return 0;
}


int
customop_test(struct MB_MGR *mgr)
{
        struct test_vec_s test_tab[DIM(cipher_attr_tab) * DIM(auth_attr_tab)];
        struct JOB_AES_HMAC *job;
        unsigned i, j, seq;
        int result = 0;

        memset(test_tab, 0, sizeof(test_tab));
        for (i = 0, seq = 0; i < DIM(cipher_attr_tab); i++) {
                for (j = 0; j < DIM(auth_attr_tab); j++) {
                        assert(seq < DIM(test_tab));
                        test_tab[seq].seq = seq;
                        test_tab[seq].cipher = &cipher_attr_tab[i];
                        test_tab[seq].auth = &auth_attr_tab[j];
                        seq++;
                }
        }

        /* encryption */
        for (i = 0; i < seq; i++) {
                struct test_vec_s *node = &test_tab[i];

                while ((job = IMB_GET_NEXT_JOB(mgr)) == NULL) {
                        job = IMB_FLUSH_JOB(mgr);
                        result |= job_check(job);
                }

                job->cipher_func = cipher_addon;
                job->hash_func = hash_addon;

                job->aes_enc_key_expanded = node->enc_key;
                job->aes_dec_key_expanded = node->dec_key;
                job->aes_key_len_in_bytes = node->cipher->key_len;
                job->src = node->txt;
                job->dst = node->txt;
                job->cipher_start_src_offset_in_bytes = 16;
                job->msg_len_to_cipher_in_bytes = sizeof(node->txt);
                job->hash_start_src_offset_in_bytes = 0;
                job->msg_len_to_hash_in_bytes =
                        sizeof(node->txt) + sizeof(node->iv);
                job->iv = node->iv;
                job->iv_len_in_bytes = node->cipher->iv_len;
                job->auth_tag_output = node->tag;
                job->auth_tag_output_len_in_bytes = node->auth->tag_len;

                job->u.HMAC._hashed_auth_key_xor_ipad = node->ipad;
                job->u.HMAC._hashed_auth_key_xor_opad = node->opad;
                job->cipher_mode = node->cipher->mode;
                job->cipher_direction = ENCRYPT;
                job->chain_order = CIPHER_HASH;
                job->hash_alg = node->auth->hash;
                job->user_data = node;

                job = IMB_SUBMIT_JOB(mgr);
                while (job) {
                        result |= job_check(job);
                        job = IMB_GET_COMPLETED_JOB(mgr);
                }
        }

        while ((job = IMB_FLUSH_JOB(mgr)) != NULL)
                result |= job_check(job);

        /* decryption */
        for (i = 0; i < seq; i++) {
                struct test_vec_s *node = &test_tab[i];

                while ((job = IMB_GET_NEXT_JOB(mgr)) == NULL) {
                        job = IMB_FLUSH_JOB(mgr);
                        result |= job_check(job);
                }

                job->cipher_func = cipher_addon;
                job->hash_func = hash_addon;

                job->aes_enc_key_expanded = node->enc_key;
                job->aes_dec_key_expanded = node->dec_key;
                job->aes_key_len_in_bytes = node->cipher->key_len;
                job->src = node->txt;
                job->dst = node->txt;
                job->cipher_start_src_offset_in_bytes = 16;
                job->msg_len_to_cipher_in_bytes = sizeof(node->txt);
                job->hash_start_src_offset_in_bytes = 0;
                job->msg_len_to_hash_in_bytes =
                        sizeof(node->txt) + sizeof(node->iv);
                job->iv = node->iv;
                job->iv_len_in_bytes = node->cipher->iv_len;
                job->auth_tag_output = node->tag;
                job->auth_tag_output_len_in_bytes = node->auth->tag_len;

                job->u.HMAC._hashed_auth_key_xor_ipad = node->ipad;
                job->u.HMAC._hashed_auth_key_xor_opad = node->opad;
                job->cipher_mode = node->cipher->mode;
                job->cipher_direction = DECRYPT;
                job->chain_order = HASH_CIPHER;
                job->hash_alg = node->auth->hash;
                job->user_data = node;

                job = IMB_SUBMIT_JOB(mgr);
                while (job) {
                        result |= job_check(job);
                        job = IMB_GET_COMPLETED_JOB(mgr);
                }
        }

        while ((job = IMB_FLUSH_JOB(mgr)) != NULL)
                result |= job_check(job);

        if (result)
                fprintf(stdout, "Custom cipher/auth test failed!\n");
        else
                fprintf(stdout, "Custom cipher/auth test passed\n");

        return result;
}