summaryrefslogtreecommitdiff
path: root/test/regression/nx_secure_test/nx_secure_crypto_self_test.c
blob: 3461b5f754745044c7a4fbd77dfb80d31dc781f4 (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
/***************************************************************************/
/* 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_secure_tls.h"

#include "nx_crypto_des.h"
#include "nx_crypto_3des.h"
#include "nx_crypto_aes.h"
#include "nx_crypto_hmac_sha1.h"
#include "nx_crypto_hmac_sha5.h"
#include "nx_crypto_hmac_md5.h"

#include "tls_test_utility.h"
#include "nx_secure_crypto_table_self_test.h"

/* Metadata buffer. */
static UCHAR metadata[10240];

static TX_THREAD thread_0;

static VOID thread_0_entry(ULONG thread_input);

extern const NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers;

#ifdef NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK
static NX_CRYPTO_METHOD crypto_method_des_cbc = 
{
    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_NULL,                                  /* DES cleanup routine, not used.         */
    _nx_crypto_method_des_operation           /* DES operation                          */

};
static NX_CRYPTO_METHOD crypto_method_3des_cbc = 
{
    NX_CRYPTO_ENCRYPTION_3DES_CBC,            /* 3DES crypto algorithm filled at runtime*/
    0,                                        /* Key size in bits                       */
    NX_CRYPTO_3DES_IV_LEN_IN_BITS,            /* IV size in bits                        */
    0,                                        /* ICV size in bits, not used.            */
    NX_CRYPTO_3DES_BLOCK_SIZE_IN_BITS,        /* Block size in bytes.                   */
    sizeof(NX_CRYPTO_3DES),                   /* Metadata size in bytes                 */
    _nx_crypto_method_3des_init,              /* 3DES initialization routine.            */
    NX_NULL,                                  /* 3DES cleanup routine, not used.         */
    _nx_crypto_method_3des_operation          /* 3DES operation                          */

};
static NX_CRYPTO_METHOD crypto_method_aes_cbc_192 =
{
    NX_CRYPTO_ENCRYPTION_AES_CBC,                /* AES crypto algorithm                   */
    NX_CRYPTO_AES_192_KEY_LEN_IN_BITS,           /* 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-CBC initialization routine.        */
    NX_NULL,                                     /* AES-CBC cleanup routine, not used.     */
    _nx_crypto_method_aes_cbc_operation          /* AES-CBC operation                      */
};
static NX_CRYPTO_METHOD crypto_method_aes_ctr_128 = 
{
    NX_CRYPTO_ENCRYPTION_AES_CTR,             /* AES crypto algorithm                   */
    NX_CRYPTO_AES_128_KEY_LEN_IN_BITS,        /* Key size in bits                       */
    64,                                       /* 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-CTR initialization routine.        */
    NX_NULL,                                  /* AES-CTR cleanup routine, not used.     */
    _nx_crypto_method_aes_ctr_operation       /* AES-CTR operation                      */
};
static NX_CRYPTO_METHOD crypto_method_aes_ctr_192 = 
{
    NX_CRYPTO_ENCRYPTION_AES_CTR,             /* AES crypto algorithm                   */
    NX_CRYPTO_AES_192_KEY_LEN_IN_BITS,        /* Key size in bits                       */
    64,                                       /* 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-CTR initialization routine.        */
    NX_NULL,                                  /* AES-CTR cleanup routine, not used.     */
    _nx_crypto_method_aes_ctr_operation       /* AES-CTR operation                      */
};
static NX_CRYPTO_METHOD crypto_method_aes_ctr_256 = 
{
    NX_CRYPTO_ENCRYPTION_AES_CTR,             /* AES crypto algorithm                   */
    NX_CRYPTO_AES_256_KEY_LEN_IN_BITS,        /* Key size in bits                       */
    64,                                       /* 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-CTR initialization routine.        */
    NX_NULL,                                  /* AES-CTR cleanup routine, not used.     */
    _nx_crypto_method_aes_ctr_operation       /* AES-CTR operation                      */
};
static NX_CRYPTO_METHOD crypto_method_hmac_sha1_160 =
{
    NX_CRYPTO_AUTHENTICATION_HMAC_SHA1_160,            /* HMAC SHA1 algorithm                   */
    0,                                                 /* Key size in bits                      */
    0,                                                 /* IV size in bits, not used             */
    160,                                               /* Transmitted ICV size in bits          */
    0,                                                 /* Block size in bytes, not used         */
    sizeof(NX_SHA1_HMAC),                              /* Metadata size in bytes                */
    NX_NULL,                                           /* Initialization routine, not used      */
    NX_NULL,                                           /* Cleanup routine, not used             */
    _nx_crypto_method_hmac_sha1_operation              /* HMAC SHA1 operation                   */
};
static NX_CRYPTO_METHOD crypto_method_hmac_sha384 =
{
    NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_384,            /* HMAC SHA384 algorithm                 */
    0,                                                 /* Key size in bits                      */
    0,                                                 /* IV size in bits, not used             */
    384,                                               /* Transmitted ICV size in bits          */
    0,                                                 /* Block size in bytes, not used         */
    sizeof(NX_CRYPTO_SHA512_HMAC),                     /* Metadata size in bytes                */
    NX_NULL,                                           /* Initialization routine, not used      */
    NX_NULL,                                           /* Cleanup routine, not used             */
    _nx_crypto_method_hmac_sha512_operation            /* HMAC SHA384 operation                 */
};
static NX_CRYPTO_METHOD crypto_method_hmac_sha512 =
{
    NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_512,            /* HMAC SHA384 algorithm                 */
    0,                                                 /* Key size in bits                      */
    0,                                                 /* IV size in bits, not used             */
    512,                                               /* Transmitted ICV size in bits          */
    0,                                                 /* Block size in bytes, not used         */
    sizeof(NX_SHA512_HMAC),                            /* Metadata size in bytes                */
    NX_NULL,                                           /* Initialization routine, not used      */
    NX_NULL,                                           /* Cleanup routine, not used             */
    _nx_crypto_method_hmac_sha512_operation            /* HMAC SHA512 operation                 */
};
static NX_CRYPTO_METHOD crypto_method_hmac_md5_128 =
{
    NX_CRYPTO_AUTHENTICATION_HMAC_MD5_128,            /* HMAC MD5 algorithm                    */
    NX_CRYPTO_HMAC_MD5_KEY_LEN_IN_BITS,               /* Key size in bits                      */
    0,                                                /* IV size in bits, not used             */
    128,                                              /* Transmitted ICV size in bits          */
    0,                                                /* Block size in bytes, not used         */
    sizeof(NX_MD5_HMAC),                              /* Metadata size in bytes                */
    NX_NULL,                                          /* Initialization routine, not used      */
    NX_NULL,                                          /* Cleanup routine, not used             */
    _nx_crypto_method_hmac_md5_operation              /* HMAC MD5 operation                    */
};

extern NX_CRYPTO_METHOD crypto_method_rsa;
extern NX_CRYPTO_METHOD crypto_method_aes_cbc_128;
extern NX_CRYPTO_METHOD crypto_method_aes_cbc_256;
extern NX_CRYPTO_METHOD crypto_method_sha1;
extern NX_CRYPTO_METHOD crypto_method_sha256;
extern NX_CRYPTO_METHOD crypto_method_sha384;
extern NX_CRYPTO_METHOD crypto_method_sha512;
extern NX_CRYPTO_METHOD crypto_method_md5;
extern NX_CRYPTO_METHOD crypto_method_hmac_sha1;
extern NX_CRYPTO_METHOD crypto_method_hmac_sha256;
extern NX_CRYPTO_METHOD crypto_method_hmac_md5;
extern NX_CRYPTO_METHOD crypto_method_tls_prf_1;
extern NX_CRYPTO_METHOD crypto_method_tls_prf_sha256;

/* For now we shall be able to test the following algorithms, based on the value in
   nx_crypto_algorithm:
   NX_CRYPTO_KEY_EXCHANGE_RSA (1024/2048/4096 bit key)
   NX_CRYPTO_ENCRYPTION_DES_CBC
   NX_CRYPTO_ENCRYPTION_3DES_CBC
   NX_CRYPTO_ENCRYPTION_AES_CBC (check key_size field)
   NX_CRYPTO_ENCRYPTION_AES_CTR
   NX_CRYPTO_HASH_SHA1
   NX_CRYPTO_HASH_SHA256
   NX_CRYPTO_HASH_SHA384
   NX_CRYPTO_HASH_SHA512
   NX_CRYPTO_HASH_MD5
   NX_CRYPTO_AUTHENTICATION_HMAC_SHA1_96
   NX_CRYPTO_AUTHENTICATION_HMAC_SHA1_160
   NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_256
   NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_384
   NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_512
   NX_CRYPTO_AUTHENTICATION_HMAC_MD5_96
   NX_CRYPTO_AUTHENTICATION_HMAC_MD5_128
   NX_CRYPTO_PRF_HMAC_SHA1
   NX_CRYPTO_PRF_HMAC_SHA2_256
*/
static NX_CRYPTO_METHOD *test_crypto_method[] = {
&crypto_method_rsa,
&crypto_method_des_cbc,
&crypto_method_3des_cbc,
&crypto_method_aes_cbc_128,
&crypto_method_aes_cbc_192,
&crypto_method_aes_cbc_256,
&crypto_method_aes_ctr_128,
&crypto_method_aes_ctr_192,
&crypto_method_aes_ctr_256,
&crypto_method_sha1,
&crypto_method_sha256,
&crypto_method_sha384,
&crypto_method_sha512,
&crypto_method_md5,
&crypto_method_hmac_sha1,
&crypto_method_hmac_sha1_160,
&crypto_method_hmac_sha256,
&crypto_method_hmac_sha384,
&crypto_method_hmac_sha512,
&crypto_method_hmac_md5,
&crypto_method_hmac_md5_128,
&crypto_method_tls_prf_1,
&crypto_method_tls_prf_sha256,
};
static UINT test_crypto_method_size = sizeof(test_crypto_method) / sizeof(NX_CRYPTO_METHOD *);
#endif /* NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK */

extern UINT _nx_secure_crypto_method_self_test(const NX_CRYPTO_METHOD *crypto_method,
                                               VOID *metadata, UINT metadata_size);

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

static VOID thread_0_entry(ULONG thread_input)
{
UINT i;
UINT status;

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

    status = nx_secure_crypto_table_self_test(&nx_crypto_tls_ciphers, metadata, sizeof(metadata));

    EXPECT_EQ(NX_CRYPTO_SUCCESS, status);

#ifdef NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK
    for (i = 0; i < test_crypto_method_size; i++)
    {
        status = _nx_secure_crypto_method_self_test(test_crypto_method[i], metadata, sizeof(metadata));

        EXPECT_EQ(NX_CRYPTO_SUCCESS, status);
    }
#endif /* NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK */

    status = nx_secure_crypto_rng_self_test();

    EXPECT_EQ(NX_CRYPTO_SUCCESS, status);

    printf("SUCCESS!\n");

    test_control_return(0);
}