summaryrefslogtreecommitdiff
path: root/test/regression/nx_secure_test/nx_secure_tls_test_init_utility.c
blob: c395c1dbdc83b3cb9ea9cdf1d575b57756be4da5 (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
317
318
/***************************************************************************/
/* 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 "nx_secure_tls_api.h"
#include "nx_crypto.h"

// Utility function to print buffer
void print_buffer(const UCHAR* buf, ULONG size)
{
UINT i;
    printf("Buffer of size: %ld. Data:\n", size);
    if(buf)
    {
        for(i = 0; i < size; ++i)
        {
            printf("%02x ", (UINT)buf[i]);
            if((i+1) % 8 == 0)
            {
                printf("\n");
            }
        }
    }
    else
    {
        printf("NULL buffer passed as number\n");
    }
    printf("\n");
}

/* Define cryptographic methods for use with TLS. */

/* Declare the NONE method:  encrypt / hash method not config */
static NX_CRYPTO_METHOD crypto_method_none =
{
    NX_CRYPTO_NONE,                           /* Name of the crypto algorithm          */
    0,                                        /* Key size in bits, not used            */
    0,                                        /* IV size in bits, not used             */
    0,                                        /* ICV size in bits, not used            */
    0,                                        /* Block size in bytes                   */
    0,                                        /* Metadata size in bytes                */
    NX_NULL,                                  /* Initialization routine, not used      */
    NX_NULL,                                  /* Cleanup routine, not used             */
    NX_NULL                                   /* NULL operation                        */
};


/* Declare the NULL encrypt */
static NX_CRYPTO_METHOD crypto_method_null =
{
    NX_CRYPTO_ENCRYPTION_NULL,                /* Name of the crypto algorithm          */
    0,                                        /* Key size in bits, not used            */
    0,                                        /* IV size in bits, not used             */
    0,                                        /* ICV size in bits, not used            */
    4,                                        /* Block size in bytes                   */
    0,                                        /* Metadata size in bytes                */
    NX_NULL,                                  /* Initialization routine, not used      */
    NX_NULL,                                  /* Cleanup routine, not used             */
    NX_NULL                                   /* NULL operation                        */
};

/* Declare the AES-CBC 128 encrytion method. */
static NX_CRYPTO_METHOD crypto_method_aes_cbc_128 =
{
    NX_CRYPTO_ENCRYPTION_AES_CBC,             /* AES crypto algorithm                   */
    NX_CRYPTO_AES_128_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_operation           /* AES-CBC operation                      */
};

/* Declare the AES-CBC 256 encryption method */
static NX_CRYPTO_METHOD crypto_method_aes_cbc_256 =
{
    NX_CRYPTO_ENCRYPTION_AES_CBC,             /* AES crypto algorithm                   */
    NX_CRYPTO_AES_256_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_operation           /* AES-CBC operation                      */
};

/* Declare the HMAC SHA1 authentication method */
static NX_CRYPTO_METHOD crypto_method_hmac_sha1 =
{
    TLS_HASH_SHA_1,                               /* HMAC SHA1 algorithm                   */
    NX_CRYPTO_HMAC_SHA1_KEY_LEN_IN_BITS,          /* Key size in bits                      */
    0,                                            /* IV size in bits, not used             */
    NX_CRYPTO_AUTHENTICATION_ICV_TRUNC_BITS,      /* 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                   */
};

/* Declare the HMAC SHA256 authentication method */
static NX_CRYPTO_METHOD crypto_method_hmac_sha256 =
{
    TLS_HASH_SHA_256,                             /* HMAC SHA256 algorithm                 */
    NX_CRYPTO_HMAC_SHA256_KEY_LEN_IN_BITS,        /* Key size in bits                      */
    0,                                            /* IV size in bits, not used             */
    NX_CRYPTO_AUTHENTICATION_ICV_TRUNC_BITS,      /* Transmitted ICV size in bits          */
    0,                                            /* Block size in bytes, not used         */
    sizeof(NX_SHA256_HMAC),                       /* Metadata size in bytes                */
    NX_NULL,                                      /* Initialization routine, not used      */
    NX_NULL,                                      /* Cleanup routine, not used             */
    _nx_crypto_method_hmac_sha256_operation       /* HMAC SHA256 operation                 */
};

/* Declare the HMAC MD5 authentication method */
static NX_CRYPTO_METHOD crypto_method_hmac_md5 =
{
    TLS_HASH_MD5,                                  /* HMAC MD5 algorithm                    */
    NX_CRYPTO_HMAC_MD5_KEY_LEN_IN_BITS,            /* Key size in bits                      */
    0,                                             /* IV size in bits, not used             */
    NX_CRYPTO_AUTHENTICATION_ICV_TRUNC_BITS,       /* 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                    */
};

/* Declare the RSA public cipher method. */
static NX_CRYPTO_METHOD crypto_method_rsa =
{
    TLS_CIPHER_RSA,                           /* RSA crypto algorithm                   */
    0,                                        /* Key size in bits                       */
    0,                                        /* IV size in bits                        */
    0,                                        /* ICV size in bits, not used.            */
    0,                                        /* Block size in bytes.                   */
    sizeof(NX_CRYPTO_RSA),                    /* Metadata size in bytes                 */
    _nx_crypto_method_rsa_init,               /* RSA initialization routine.            */
    NX_NULL,                                  /* RSA cleanup routine, not used.         */
    _nx_crypto_method_rsa_operation           /* RSA operation                          */

};

/* Declare the public NULL cipher (not to be confused with the NULL methods above). This
 * is used as a placeholder in ciphersuites that do not use a cipher method for a
 * particular operation (e.g. some PSK ciphersuites don't use a public-key algorithm
 * like RSA).
 */
static NX_CRYPTO_METHOD crypto_method_public_null =
{
    TLS_CIPHER_NULL,                          /* Name of the crypto algorithm          */
    0,                                        /* Key size in bits, not used            */
    0,                                        /* IV size in bits, not used             */
    0,                                        /* ICV size in bits, not used            */
    1,                                        /* Block size in bytes                   */
    0,                                        /* Metadata size in bytes                */
    _nx_crypto_method_null_init,              /* Initialization routine, not used      */
    NX_NULL,                                  /* Cleanup routine, not used             */
    _nx_crypto_method_null_operation          /* NULL operation                        */
};



/* Declare the MD5 hash method */
static NX_CRYPTO_METHOD crypto_method_md5 =
{
    TLS_HASH_MD5,                                  /* MD5 algorithm                         */
    0,                                             /* Key size in bits                      */
    0,                                             /* IV size in bits, not used             */
    0,                                             /* Transmitted ICV size in bits          */
    16,                                            /* Block size in bytes                   */
    sizeof(NX_MD5),                                /* Metadata size in bytes                */
    NX_NULL,                                       /* Initialization routine, not used      */
    NX_NULL,                                       /* Cleanup routine, not used             */
    _nx_crypto_method_md5_operation                /* MD5 operation                         */
};

/* Declare the SHA1 hash method */
static NX_CRYPTO_METHOD crypto_method_sha1 =
{
    TLS_HASH_SHA_1,                                /* SHA1 algorithm                        */
    0,                                             /* Key size in bits                      */
    0,                                             /* IV size in bits, not used             */
    0,                                             /* Transmitted ICV size in bits          */
    20,                                            /* Block size in bytes                   */
    sizeof(NX_SHA1),                               /* Metadata size in bytes                */
    NX_NULL,                                       /* Initialization routine, not used      */
    NX_NULL,                                       /* Cleanup routine, not used             */
    _nx_crypto_method_sha1_operation               /* SHA1 operation                        */
};

/* Declare the SHA256 hash method */
static NX_CRYPTO_METHOD crypto_method_sha256 =
{
    TLS_HASH_SHA_256,                              /* SHA256 algorithm                      */
    0,                                             /* Key size in bits                      */
    0,                                             /* IV size in bits, not used             */
    0,                                             /* Transmitted ICV size in bits          */
    32,                                            /* Block size in bytes                   */
    sizeof(NX_SHA256),                             /* Metadata size in bytes                */
    NX_NULL,                                       /* Initialization routine, not used      */
    NX_NULL,                                       /* Cleanup routine, not used             */
    _nx_crypto_method_sha256_operation             /* SHA256 operation                      */
};


/* Declare the supported encryption methods.
   This table is used in constructing the NX_CRYPTO method list. */
static NX_CRYPTO_METHOD    *encryption_method_table[] =
{
    &crypto_method_aes_cbc_128,
    &crypto_method_aes_cbc_256,
    &crypto_method_null
};

/* Declare the supported authentication methods.
   This table is used in constructing the NX_CRYPTO method list. */
static NX_CRYPTO_METHOD    *authentication_method_table[] =
{
    &crypto_method_hmac_sha1,
    &crypto_method_hmac_sha256,
    &crypto_method_hmac_md5,
    &crypto_method_none
};

/* Declare the supported public cipher methods.
   This table is used in constructing the NX_CRYPTO method list. */
static NX_CRYPTO_METHOD    *public_cipher_method_table[] =
{
    &crypto_method_rsa,
    &crypto_method_public_null,
    &crypto_method_null
};

/* Declare the supported hash methods.
   This table is used in constructing the NX_CRYPTO method list. */
static NX_CRYPTO_METHOD    *hash_method_table[] =
{
    &crypto_method_md5,
    &crypto_method_sha1,
    &crypto_method_sha256
};

static NX_SECURE_METHODS    tls_methods =
{
    encryption_method_table,
    3,
    authentication_method_table,
    4,
    public_cipher_method_table,
    2,
    hash_method_table,
    3
};

/* Define scratch space for cryptographic methods. */
static CHAR crypto_metadata[2*sizeof(NX_AES)];
static CHAR authentication_metadata[2 * sizeof(NX_SHA256_HMAC)];
static CHAR public_cipher_metadata[2 * sizeof(NX_CRYPTO_RSA)];
static CHAR handshake_hash_metadata[2 * (sizeof(NX_MD5) + sizeof(NX_SHA1) + sizeof(NX_SHA256))];

static CHAR crypto_metadata2[2*sizeof(NX_AES)];
static CHAR authentication_metadata2[2 * sizeof(NX_SHA256_HMAC)];
static CHAR public_cipher_metadata2[2 * sizeof(NX_CRYPTO_RSA)];
static CHAR handshake_hash_metadata2[2 * (sizeof(NX_MD5) + sizeof(NX_SHA1) + sizeof(NX_SHA256))];

static UINT init_number = 0;

void nx_secure_tls_test_init_utility(NX_SECURE_TLS_SESSION *tls_session)
{
UINT status;

    /* Create a TLS session for our socket.  */
    if(init_number == 0)
    {
    status =  nx_secure_tls_session_create(tls_session,
                                           &tls_methods,
                                           crypto_metadata,
                                           sizeof(crypto_metadata),
                                           authentication_metadata,
                                           sizeof(authentication_metadata),
                                           public_cipher_metadata,
                                           sizeof(public_cipher_metadata),
                                           handshake_hash_metadata,
                                           sizeof(handshake_hash_metadata));
      init_number++;
    }
    else
    {
    status =  nx_secure_tls_session_create(tls_session,
                                           &tls_methods,
                                           crypto_metadata2,
                                           sizeof(crypto_metadata2),
                                           authentication_metadata2,
                                           sizeof(authentication_metadata2),
                                           public_cipher_metadata2,
                                           sizeof(public_cipher_metadata2),
                                           handshake_hash_metadata2,
                                           sizeof(handshake_hash_metadata2));
 
    }
    
    if(status != NX_SUCCESS)
    {
        printf("Failure in TLS initialization. \n");
    }
}