summaryrefslogtreecommitdiff
path: root/test/regression/nx_secure_test/nx_secure_aes_test.c
blob: 1a6c23be7ae5b04b0c4a7ef21b9ed4032f60c6a5 (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
312
313
314
315
316
/***************************************************************************/
/* 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 <stdlib.h>
#include <time.h>

#include "nx_crypto_aes.h"

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

#define MAXIMUM_KEY_BITS 256

#include "nx_secure_aes_test_data.c"

/* Define software AES method. */
NX_CRYPTO_METHOD test_crypto_method_aes =
{
    0,                                        /* AES crypto algorithm filled at runtime */
    0,                                        /* Key size in bits                       */
    NX_CRYPTO_AES_IV_LEN_IN_BITS,             /* IV size in bits                        */
    0,                                        /* ICV size in bits, not used.            */
    NX_CRYPTO_AES_BLOCK_SIZE_IN_BITS >> 3,    /* Block size in bytes.                   */
    sizeof(NX_AES),                           /* Metadata size in bytes                 */
    _nx_crypto_method_aes_init,               /* AES initialization routine.            */
    NX_CRYPTO_NULL,                           /* AES cleanup routine, not used.         */
    _nx_crypto_method_aes_operation           /* AES operation                          */

};

/* AES context. */
static NX_CRYPTO_AES aes_ctx;

/* Input to hold plain plus nonce. */
static ULONG key[(MAXIMUM_KEY_BITS >> 5) + 1];

/* IV. */
static ULONG iv[4];

#define TEST_TEXT_LENGTH (NX_CRYPTO_AES_BLOCK_SIZE * 3)

/* Output. */
static ULONG output[TEST_TEXT_LENGTH >> 2];

/* Buffers for packet chain tests. */
static UCHAR plain_text_buffer[TEST_TEXT_LENGTH];
static UCHAR cipher_text_buffer[TEST_TEXT_LENGTH];

#ifndef NX_CRYPTO_STANDALONE_ENABLE
static TX_THREAD thread_0;
#endif

static VOID thread_0_entry(ULONG thread_input);

#ifdef CTEST
void test_application_define(void *first_unused_memory);
void test_application_define(void *first_unused_memory)
#else
void nx_secure_aes_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, j, status;
UCHAR *nonce;
UINT nonce_len;
NX_CRYPTO_METHOD test_method;

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

    for (i = 0; i < sizeof(aes_data) / sizeof(AES_DATA); i++)
    {

        /* Encryption. */
        memset(output, 0xFF, sizeof(output));

        /* Conjunction of key and nonce for CTR mode. */
        /* It does not affect CBC mode. */
        memcpy(key, aes_data[i].key, aes_data[i].key_len);
        memcpy(key + (aes_data[i].key_len >> 2), aes_data[i].iv, 4);

        if (aes_data[i].algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC)
        {
            memcpy(iv, aes_data[i].iv, sizeof(iv));
        }
        else
        {
            memcpy(iv, aes_data[i].iv + 4, 8);
        }

        /* Set crypto algorithm. */
        test_crypto_method_aes.nx_crypto_algorithm = aes_data[i].algorithm;

        test_crypto_method_aes.nx_crypto_init(&test_crypto_method_aes,
                                              (UCHAR *)key,
                                              (aes_data[i].key_len << 3),
                                              NX_CRYPTO_NULL,
                                              &aes_ctx,
                                              sizeof(aes_ctx));

        test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_ENCRYPT,
                                                   NX_CRYPTO_NULL,
                                                   &test_crypto_method_aes,
                                                   (UCHAR *)key,
                                                   (aes_data[i].key_len << 3),
                                                   aes_data[i].plain,
                                                   aes_data[i].plain_len,
                                                   (UCHAR *)iv,
                                                   (UCHAR *)output,
                                                   sizeof(output),
                                                   &aes_ctx,
                                                   sizeof(aes_ctx),
                                                   NX_CRYPTO_NULL, NX_CRYPTO_NULL);

        EXPECT_EQ(0, memcmp(output, aes_data[i].secret, aes_data[i].secret_len));

        /* Decryption. */
        test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_DECRYPT,
                                                   NX_CRYPTO_NULL,
                                                   &test_crypto_method_aes,
                                                   (UCHAR *)key,
                                                   (aes_data[i].key_len << 3),
                                                   aes_data[i].secret,
                                                   aes_data[i].secret_len,
                                                   (UCHAR *)iv,
                                                   (UCHAR *)output,
                                                   sizeof(output),
                                                   &aes_ctx,
                                                   sizeof(aes_ctx),
                                                   NX_CRYPTO_NULL, NX_CRYPTO_NULL);

        EXPECT_EQ(0, memcmp(output, aes_data[i].plain, aes_data[i].plain_len));
    }

    srand(time(NULL));

    /* Tests for NX_CRYPTO_ENCRYPT_UPDATE and NX_CRYPTO_DECRYPT_UPDATE. */
    for (i = 0; i < sizeof(aes_data) / sizeof(AES_DATA); i++)
    {

        /* Conjunction of key and nonce for CTR mode. */
        /* It does not affect CBC mode. */
        memcpy(key, aes_data[i].key, aes_data[i].key_len);
        memcpy(key + (aes_data[i].key_len >> 2), aes_data[i].iv, 4);

        if (aes_data[i].algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC)
        {
            memcpy(iv, aes_data[i].iv, sizeof(iv));
            nonce = NX_CRYPTO_NULL;
            nonce_len = 0;
        }
        else
        {
            memcpy(iv, aes_data[i].iv + 4, 8);
            nonce = (UCHAR *)key + aes_data[i].key_len;
            nonce_len = 4;
        }

        /* Set crypto algorithm. */
        test_crypto_method_aes.nx_crypto_algorithm = aes_data[i].algorithm;

        /* Generate test data. */
        for (j = 0; j < TEST_TEXT_LENGTH; j++)
        {
            plain_text_buffer[j] = (UCHAR)rand();
        }

        test_crypto_method_aes.nx_crypto_init(&test_crypto_method_aes,
                                              (UCHAR *)key,
                                              (aes_data[i].key_len << 3),
                                              NX_CRYPTO_NULL,
                                              &aes_ctx,
                                              sizeof(aes_ctx));

        test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_ENCRYPT,
                                                   NX_CRYPTO_NULL,
                                                   &test_crypto_method_aes,
                                                   (UCHAR *)key,
                                                   (aes_data[i].key_len << 3),
                                                   plain_text_buffer,
                                                   TEST_TEXT_LENGTH,
                                                   (UCHAR *)iv,
                                                   cipher_text_buffer,
                                                   sizeof(cipher_text_buffer),
                                                   &aes_ctx,
                                                   sizeof(aes_ctx),
                                                   NX_CRYPTO_NULL, NX_CRYPTO_NULL);

        /* Tests for NX_CRYPTO_ENCRYPT_UPDATE and NX_CRYPTO_DECRYPT_UPDATE. */

        /* Encryption. */
        memset(output, 0xFF, sizeof(output));

        /* Initialize the context. */
        test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_ENCRYPT_INITIALIZE,
                                                   NX_CRYPTO_NULL,
                                                   &test_crypto_method_aes,
                                                   NX_CRYPTO_NULL,
                                                   0,
                                                   nonce,
                                                   nonce_len,
                                                   (UCHAR *)iv,
                                                   NX_CRYPTO_NULL,
                                                   0,
                                                   &aes_ctx,
                                                   sizeof(aes_ctx),
                                                   NX_CRYPTO_NULL, NX_CRYPTO_NULL);

        /* Update one block of input. */
        test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_ENCRYPT_UPDATE,
                                                   NX_CRYPTO_NULL,
                                                   &test_crypto_method_aes,
                                                   NX_CRYPTO_NULL,
                                                   0,
                                                   plain_text_buffer,
                                                   NX_CRYPTO_AES_BLOCK_SIZE,
                                                   NX_CRYPTO_NULL,
                                                   (UCHAR *)output,
                                                   sizeof(output),
                                                   &aes_ctx,
                                                   sizeof(aes_ctx),
                                                   NX_CRYPTO_NULL, NX_CRYPTO_NULL);

        /* Update the rest. */
        test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_ENCRYPT_UPDATE,
                                                   NX_CRYPTO_NULL,
                                                   &test_crypto_method_aes,
                                                   NX_CRYPTO_NULL,
                                                   0,
                                                   plain_text_buffer + NX_CRYPTO_AES_BLOCK_SIZE,
                                                   TEST_TEXT_LENGTH - NX_CRYPTO_AES_BLOCK_SIZE,
                                                   NX_CRYPTO_NULL,
                                                   (UCHAR *)output + NX_CRYPTO_AES_BLOCK_SIZE,
                                                   sizeof(output) - NX_CRYPTO_AES_BLOCK_SIZE,
                                                   &aes_ctx,
                                                   sizeof(aes_ctx),
                                                   NX_CRYPTO_NULL, NX_CRYPTO_NULL);

        EXPECT_EQ(0, memcmp(output, cipher_text_buffer, TEST_TEXT_LENGTH));

        /* Decryption. */
        memset(output, 0xFF, sizeof(output));

        /* Initialize the context. */
        test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_DECRYPT_INITIALIZE,
                                                   NX_CRYPTO_NULL,
                                                   &test_crypto_method_aes,
                                                   NX_CRYPTO_NULL,
                                                   0,
                                                   nonce,
                                                   nonce_len,
                                                   (UCHAR *)iv,
                                                   NX_CRYPTO_NULL,
                                                   0,
                                                   &aes_ctx,
                                                   sizeof(aes_ctx),
                                                   NX_CRYPTO_NULL, NX_CRYPTO_NULL);

        /* Update one block of input. */
        test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_DECRYPT_UPDATE,
                                                   NX_CRYPTO_NULL,
                                                   &test_crypto_method_aes,
                                                   NX_CRYPTO_NULL,
                                                   0,
                                                   cipher_text_buffer,
                                                   NX_CRYPTO_AES_BLOCK_SIZE,
                                                   NX_CRYPTO_NULL,
                                                   (UCHAR *)output,
                                                   sizeof(output),
                                                   &aes_ctx,
                                                   sizeof(aes_ctx),
                                                   NX_CRYPTO_NULL, NX_CRYPTO_NULL);

        /* Update the rest. */
        test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_DECRYPT_UPDATE,
                                                   NX_CRYPTO_NULL,
                                                   &test_crypto_method_aes,
                                                   NX_CRYPTO_NULL,
                                                   0,
                                                   cipher_text_buffer + NX_CRYPTO_AES_BLOCK_SIZE,
                                                   TEST_TEXT_LENGTH - NX_CRYPTO_AES_BLOCK_SIZE,
                                                   NX_CRYPTO_NULL,
                                                   (UCHAR *)output + NX_CRYPTO_AES_BLOCK_SIZE,
                                                   sizeof(output) - NX_CRYPTO_AES_BLOCK_SIZE,
                                                   &aes_ctx,
                                                   sizeof(aes_ctx),
                                                   NX_CRYPTO_NULL, NX_CRYPTO_NULL);

        EXPECT_EQ(0, memcmp(output, plain_text_buffer, TEST_TEXT_LENGTH));
    }

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