summaryrefslogtreecommitdiff
path: root/test/regression/nx_secure_test/nx_secure_des_error_checking_test.c
blob: 5419aff808ff09bcac77989dcd63227138c64b87 (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
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation                                */
/* Copyright (c) 2026 Eclipse ThreadX contributors                         */
/*                                                                         */
/* This program and the accompanying materials are made available under    */
/* the terms of the MIT License which is available at                      */
/* https://opensource.org/licenses/MIT.                                    */
/*                                                                         */
/* SPDX-License-Identifier: MIT                                            */
/***************************************************************************/


#include <stdio.h>
#include "nx_crypto_des.h"
#include "nx_crypto_method_self_test.h"

#ifndef NX_CRYPTO_STANDALONE_ENABLE
#include "nx_secure_tls.h"
#endif
#include "tls_test_utility.h"

#define MAXIMUM_KEY_BITS 256

/* Define software DES method. */
static NX_CRYPTO_METHOD test_crypto_method_des =
{
    NX_CRYPTO_ENCRYPTION_DES_CBC,             /* DES crypto algorithm filled at runtime*/
    0,                                        /* Key size in bits                       */
    NX_CRYPTO_DES_IV_LEN_IN_BITS,             /* IV size in bits                        */
    0,                                        /* ICV size in bits, not used.            */
    NX_CRYPTO_DES_BLOCK_SIZE_IN_BITS,         /* Block size in bytes.                   */
    sizeof(NX_CRYPTO_DES),                    /* Metadata size in bytes                 */
    _nx_crypto_method_des_init,               /* DES initialization routine.            */
    NX_CRYPTO_NULL,                           /* DES cleanup routine, not used.         */
    _nx_crypto_method_des_operation           /* DES operation                          */

};

extern NX_CRYPTO_METHOD crypto_method_des;
/* DES context. */
static NX_CRYPTO_DES _des_ctx;

/* Input to hold plain plus nonce. */
static UCHAR key[8];

/* IV. */
static UCHAR iv[8] = {0, 1, 2, 3, 4, 5, 6, 7};

#ifndef NX_CRYPTO_STANDALONE_ENABLE
static TX_THREAD thread_0;
#endif

static VOID thread_0_entry(ULONG thread_input);

static UINT count = 0;

static UINT test_nx_crypto_init_failed(NX_CRYPTO_METHOD *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, VOID **handler, VOID *crypto_metadata, ULONG crypto_metadata_size)
{
    return 233;
}

static UINT test_nx_crypto_operation_failed(UINT op, VOID *handler, struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
{
    if (!count)
        return 233;

    count--;
    return _nx_crypto_method_des_operation(op, handler, method, key, key_size_in_bits, input, input_length_in_byte, iv_ptr, output, output_length_in_byte, crypto_metadata, crypto_metadata_size, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
}

static UINT test_nx_crypto_operation_succeed(UINT op, VOID *handler, struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
{
    if (!count)
        return 0;

    count--;
    return _nx_crypto_method_des_operation(op, handler, method, key, key_size_in_bits, input, input_length_in_byte, iv_ptr, output, output_length_in_byte, crypto_metadata, crypto_metadata_size, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
}

#ifdef CTEST
void test_application_define(void *first_unused_memory);
void test_application_define(void *first_unused_memory)
#else
void nx_secure_des_error_checking_test_application_define(void *first_unused_memory)
#endif
{
#ifndef NX_CRYPTO_STANDALONE_ENABLE
    tx_thread_create(&thread_0, "Thread 0", thread_0_entry, 0,
                     first_unused_memory, 4096,
                     16, 16, 4, TX_AUTO_START);
#else
    thread_0_entry(0);
#endif
}

static VOID thread_0_entry(ULONG thread_input)
{
UINT i, status, backup;
UCHAR input[8];
UCHAR output[8];
VOID *handle;
NX_CRYPTO_METHOD test_method;

    /* Print out test information banner.  */
    printf("NetX Secure Test:   DES Error Checking Test............................");

    /* Set key and IV. */
    for (i = 0; i < 8; i++)
    {
        key[i] = i;
        input[i] = i;
    }

    /* NULL method pointer. */
    status = _nx_crypto_method_des_init(NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
    EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);

    /* NULL key pointer. */
    status = _nx_crypto_method_des_init(&test_crypto_method_des, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
    EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);

    /* NULL metadata pointer. */
    status = _nx_crypto_method_des_init(&test_crypto_method_des, (UCHAR *)key, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
    EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);

    /* Invalid key size. */
    status = _nx_crypto_method_des_init(&test_crypto_method_des, (UCHAR *)key, 0, NX_CRYPTO_NULL, &_des_ctx, 0);
    EXPECT_EQ(NX_CRYPTO_SIZE_ERROR, status);

    /* Metadata size is not enough. */
    status = _nx_crypto_method_des_init(&test_crypto_method_des, (UCHAR *)key, NX_CRYPTO_DES_KEY_LEN_IN_BITS, NX_CRYPTO_NULL, &_des_ctx, 0);
    EXPECT_EQ(NX_CRYPTO_SIZE_ERROR, status);

    /* Metadata address is not 4-byte aligned. */
    status = _nx_crypto_method_des_init(&test_crypto_method_des, (UCHAR *)key, NX_CRYPTO_DES_KEY_LEN_IN_BITS, NX_CRYPTO_NULL, (VOID *)0x03, sizeof(NX_CRYPTO_DES));
    EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);

    /* NULL crypto_metadata pointer. */
    status = _nx_crypto_method_des_cleanup(NX_CRYPTO_NULL);
    EXPECT_EQ(NX_CRYPTO_SUCCESS, status);

    /* Invalid operation id. */
    status = test_crypto_method_des.nx_crypto_operation(NX_CRYPTO_AUTHENTICATE, NX_CRYPTO_NULL,
                                               &test_crypto_method_des, /* method */
                                               (UCHAR *)key, NX_CRYPTO_DES_KEY_LEN_IN_BITS, /* key */
                                               NX_CRYPTO_NULL, 0, /* input */
                                               (UCHAR *)iv, /* iv */
                                               (UCHAR *)output, sizeof(output), /* output */
                                               &_des_ctx, sizeof(_des_ctx), /* metadata */
                                               NX_CRYPTO_NULL, NX_CRYPTO_NULL);
    EXPECT_EQ(NX_CRYPTO_NOT_SUCCESSFUL, status);

    /* NULL method pointer. */
    status = test_crypto_method_des.nx_crypto_operation(NX_CRYPTO_ENCRYPT, NX_CRYPTO_NULL,
                                               NX_CRYPTO_NULL, /* method */
                                               (UCHAR *)key, NX_CRYPTO_DES_KEY_LEN_IN_BITS, /* key */
                                               NX_CRYPTO_NULL, 0, /* input */
                                               (UCHAR *)iv, /* iv */
                                               (UCHAR *)output, sizeof(output), /* output */
                                               &_des_ctx, sizeof(_des_ctx), /* metadata */
                                               NX_CRYPTO_NULL, NX_CRYPTO_NULL);
    EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);

    /* NULL metadata pointer. */
    status = test_crypto_method_des.nx_crypto_operation(NX_CRYPTO_ENCRYPT, NX_CRYPTO_NULL,
                                               &test_crypto_method_des, /* method */
                                               (UCHAR *)key, NX_CRYPTO_DES_KEY_LEN_IN_BITS, /* key */
                                               NX_CRYPTO_NULL, 0, /* input */
                                               (UCHAR *)iv, /* iv */
                                               (UCHAR *)output, sizeof(output), /* output */
                                               NX_CRYPTO_NULL, sizeof(_des_ctx), /* metadata */
                                               NX_CRYPTO_NULL, NX_CRYPTO_NULL);
    EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);

    /* Metadata address is not 4-byte aligned. */
    status = test_crypto_method_des.nx_crypto_operation(NX_CRYPTO_ENCRYPT, NX_CRYPTO_NULL,
                                               &test_crypto_method_des, /* method */
                                               (UCHAR *)key, NX_CRYPTO_DES_KEY_LEN_IN_BITS, /* key */
                                               NX_CRYPTO_NULL, 0, /* input */
                                               (UCHAR *)iv, /* iv */
                                               (UCHAR *)output, sizeof(output), /* output */
                                               (VOID *)0x03, sizeof(_des_ctx), /* metadata */
                                               NX_CRYPTO_NULL, NX_CRYPTO_NULL);
    EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);

    /* Metadata size is not enough. */
    status = test_crypto_method_des.nx_crypto_operation(NX_CRYPTO_ENCRYPT, NX_CRYPTO_NULL,
                                               &test_crypto_method_des, /* method */
                                               (UCHAR *)key, NX_CRYPTO_DES_KEY_LEN_IN_BITS, /* key */
                                               NX_CRYPTO_NULL, 0, /* input */
                                               (UCHAR *)iv, /* iv */
                                               (UCHAR *)output, sizeof(output), /* output */
                                               &_des_ctx, 0, /* metadata */
                                               NX_CRYPTO_NULL, NX_CRYPTO_NULL);
    EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);

    /* Invalid algorithm id. */
    backup = test_crypto_method_des.nx_crypto_algorithm;
    test_crypto_method_des.nx_crypto_algorithm = 0;
    status = test_crypto_method_des.nx_crypto_operation(NX_CRYPTO_ENCRYPT, NX_CRYPTO_NULL,
                                               &test_crypto_method_des, /* method */
                                               (UCHAR *)key, NX_CRYPTO_DES_KEY_LEN_IN_BITS, /* key */
                                               NX_CRYPTO_NULL, 0, /* input */
                                               (UCHAR *)iv, /* iv */
                                               (UCHAR *)output, sizeof(output), /* output */
                                               &_des_ctx, sizeof(_des_ctx), /* metadata */
                                               NX_CRYPTO_NULL, NX_CRYPTO_NULL);
    test_crypto_method_des.nx_crypto_algorithm = backup;
    EXPECT_EQ(NX_CRYPTO_NOT_SUCCESSFUL, status);

#ifdef NX_CRYPTO_SELF_TEST
    /* Tests for _nx_crypto_method_self_test_des. */

    /* NULL method pointer. */
    status = _nx_crypto_method_self_test_des(NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
    EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);

    /* nx_crypto_init and nx_crypto_operation are both NULL. */
    test_method.nx_crypto_init = NX_CRYPTO_NULL;
    test_method.nx_crypto_operation = NX_CRYPTO_NULL;
    status = _nx_crypto_method_self_test_des(&test_method, NX_CRYPTO_NULL, 0);
    EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);

    /* nx_crypto_init failed. */
    test_method.nx_crypto_init = test_nx_crypto_init_failed;
    status = _nx_crypto_method_self_test_des(&test_method, NX_CRYPTO_NULL, 0);
    EXPECT_EQ(233, status);

    /* nx_crypto_operation failed. */
    test_method.nx_crypto_init = NX_CRYPTO_NULL;
    test_method.nx_crypto_operation = test_nx_crypto_operation_failed;
    status = _nx_crypto_method_self_test_des(&test_method, NX_CRYPTO_NULL, 0);
    EXPECT_EQ(233, status);

    /* NX_CRYPTO_MEMCMP failed. */
    test_method.nx_crypto_init = NX_CRYPTO_NULL;
    test_method.nx_crypto_operation = test_nx_crypto_operation_succeed;
    status = _nx_crypto_method_self_test_des(&test_method, &_des_ctx, sizeof(_des_ctx));
    EXPECT_EQ(NX_CRYPTO_NOT_SUCCESSFUL, status);

    /* nx_crypto_operation NX_CRYPTO_DECRYPT failed. */
    count = 1;
    test_method = crypto_method_des;
    test_method.nx_crypto_operation = test_nx_crypto_operation_failed;
    status = _nx_crypto_method_self_test_des(&test_method, &_des_ctx, sizeof(_des_ctx));
    EXPECT_EQ(233, status);

    /* NX_CRYPTO_MEMCMP failed at the second time. */
    count = 1;
    test_method = crypto_method_des;
    test_method.nx_crypto_operation = test_nx_crypto_operation_succeed;
    status = _nx_crypto_method_self_test_des(&test_method, &_des_ctx, sizeof(_des_ctx));
    EXPECT_EQ(NX_CRYPTO_NOT_SUCCESSFUL, status);

    /* nx_crypto_cleanup is NULL. */
    test_method = crypto_method_des;
    test_method.nx_crypto_cleanup = NX_CRYPTO_NULL;
    status = _nx_crypto_method_self_test_des(&test_method, &_des_ctx, sizeof(_des_ctx));
    EXPECT_EQ(NX_CRYPTO_SUCCESS, status);

#endif /* NX_CRYPTO_SELF_TEST */

    printf("SUCCESS!\n");
    test_control_return(0);
}