1 
2 #include <stdio.h>
3 #include "nx_crypto_hmac_sha2.h"
4 
5 #include "tls_test_utility.h"
6 
7 #define MAXIMUM_PLAIN_BYTES 256
8 
9 #include "nx_secure_hmac_sha224_test_data.c"
10 
11 /* Define software SHA224 method. */
12 static NX_CRYPTO_METHOD test_crypto_method_hmac_sha224 =
13 {
14     NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_224,   /* SHA crypto algorithm                   */
15     0,                                        /* Key size in bits                       */
16     0,                                        /* IV size in bits                        */
17     224,                                      /* ICV size in bits, not used.            */
18     sizeof(NX_CRYPTO_SHA256),                 /* Metadata size in bytes                 */
19     sizeof(NX_CRYPTO_SHA256_HMAC),            /* Metadata size in bytes                 */
20     NX_CRYPTO_NULL,                           /* SHA initialization routine.            */
21     NX_CRYPTO_NULL,                           /* SHA cleanup routine, not used.         */
22     _nx_crypto_method_hmac_sha256_operation,  /* SHA operation                          */
23 };
24 
25 /* SHA context. */
26 static NX_CRYPTO_SHA256_HMAC hmac_sha224_ctx;
27 
28 /* Output. */
29 static ULONG output[7];
30 
31 #ifndef NX_CRYPTO_STANDALONE_ENABLE
32 static TX_THREAD thread_0;
33 #endif
34 
35 static VOID thread_0_entry(ULONG thread_input);
36 
37 #ifdef CTEST
38 void test_application_define(void *first_unused_memory);
test_application_define(void * first_unused_memory)39 void test_application_define(void *first_unused_memory)
40 #else
41 void nx_secure_hmac_sha224_test_application_define(void *first_unused_memory)
42 #endif
43 {
44 #ifndef NX_CRYPTO_STANDALONE_ENABLE
45     tx_thread_create(&thread_0, "Thread 0", thread_0_entry, 0,
46                      first_unused_memory, 4096,
47                      16, 16, 4, TX_AUTO_START);
48 #else
49     thread_0_entry(0);
50 #endif
51 }
52 
thread_0_entry(ULONG thread_input)53 static VOID thread_0_entry(ULONG thread_input)
54 {
55 UINT i;
56 
57     /* Print out test information banner.  */
58     printf("NetX Secure Test:   HMAC SHA224 Test...................................");
59 
60     for (i = 0; i < sizeof(hmac_sha224_data) / sizeof(HMAC_SHA224_DATA); i++)
61     {
62 
63         /* Authentication. */
64         memset(output, 0xFF, sizeof(output));
65 
66         test_crypto_method_hmac_sha224.nx_crypto_operation(NX_CRYPTO_AUTHENTICATE,
67                                                            NX_CRYPTO_NULL,
68                                                            &test_crypto_method_hmac_sha224,
69                                                            hmac_sha224_data[i].key,
70                                                            (hmac_sha224_data[i].key_len << 3),
71                                                            hmac_sha224_data[i].plain,
72                                                            hmac_sha224_data[i].plain_len,
73                                                            NX_CRYPTO_NULL,
74                                                            (UCHAR *)output,
75                                                            sizeof(output),
76                                                            &hmac_sha224_ctx,
77                                                            sizeof(hmac_sha224_ctx),
78                                                            NX_CRYPTO_NULL, NX_CRYPTO_NULL);
79 
80         EXPECT_EQ(0, memcmp(output, hmac_sha224_data[i].secret, sizeof(output)));
81 
82         memset(output, 0xFF, sizeof(output));
83 
84         /* Test HMAC SHA224 with Initialize, Update and Calculate operation.  */
85         test_crypto_method_hmac_sha224.nx_crypto_operation(NX_CRYPTO_HASH_INITIALIZE,
86                                                            NX_CRYPTO_NULL,
87                                                            &test_crypto_method_hmac_sha224,
88                                                            hmac_sha224_data[i].key,
89                                                            (hmac_sha224_data[i].key_len << 3),
90                                                            NX_CRYPTO_NULL,
91                                                            0,
92                                                            NX_CRYPTO_NULL,
93                                                            NX_CRYPTO_NULL,
94                                                            0,
95                                                            &hmac_sha224_ctx,
96                                                            sizeof(hmac_sha224_ctx),
97                                                            NX_CRYPTO_NULL, NX_CRYPTO_NULL);
98 
99         test_crypto_method_hmac_sha224.nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
100                                                            NX_CRYPTO_NULL,
101                                                            &test_crypto_method_hmac_sha224,
102                                                            NX_CRYPTO_NULL,
103                                                            0,
104                                                            hmac_sha224_data[i].plain,
105                                                            hmac_sha224_data[i].plain_len,
106                                                            NX_CRYPTO_NULL,
107                                                            NX_CRYPTO_NULL,
108                                                            0,
109                                                            &hmac_sha224_ctx,
110                                                            sizeof(hmac_sha224_ctx),
111                                                            NX_CRYPTO_NULL, NX_CRYPTO_NULL);
112 
113         test_crypto_method_hmac_sha224.nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE,
114                                                            NX_CRYPTO_NULL,
115                                                            &test_crypto_method_hmac_sha224,
116                                                            NX_CRYPTO_NULL,
117                                                            0,
118                                                            NX_CRYPTO_NULL,
119                                                            0,
120                                                            NX_CRYPTO_NULL,
121                                                            (UCHAR *)output,
122                                                            sizeof(output),
123                                                            &hmac_sha224_ctx,
124                                                            sizeof(hmac_sha224_ctx),
125                                                            NX_CRYPTO_NULL, NX_CRYPTO_NULL);
126 
127         EXPECT_EQ(0, memcmp(output, hmac_sha224_data[i].secret, sizeof(output)));
128     }
129 
130     printf("SUCCESS!\n");
131     test_control_return(0);
132 }
133