1 #include <stdio.h>
2 #include "nx_crypto_ec.h"
3 #include "tls_test_utility.h"
4 #ifndef NX_CRYPTO_STANDALONE_ENABLE
5 #include "nx_secure_tls.h"
6 #endif
7 
8 #define MAXIMUM_KEY_BITS 256
9 
10 extern NX_CRYPTO_CONST NX_CRYPTO_EC _nx_crypto_ec_secp192r1;
11 static UCHAR scratch_buffer[1024];
12 
13 #ifndef NX_CRYPTO_STANDALONE_ENABLE
14 static TX_THREAD thread_0;
15 #endif
16 
17 static VOID thread_0_entry(ULONG thread_input);
18 
19 #ifdef CTEST
20 void test_application_define(void *first_unused_memory);
test_application_define(void * first_unused_memory)21 void test_application_define(void *first_unused_memory)
22 #else
23 void nx_secure_ec_additional_test_application_define(void *first_unused_memory)
24 #endif
25 {
26 #ifndef NX_CRYPTO_STANDALONE_ENABLE
27     tx_thread_create(&thread_0, "Thread 0", thread_0_entry, 0,
28                      first_unused_memory, 4096,
29                      16, 16, 4, TX_AUTO_START);
30 #else
31     thread_0_entry(0);
32 #endif
33 }
34 
thread_0_entry(ULONG thread_input)35 static VOID thread_0_entry(ULONG thread_input)
36 {
37 UINT status, huge_number_size;
38 NX_CRYPTO_EC_POINT point, point1;
39 UCHAR *scratch_ptr, buffer[256];
40 NX_CRYPTO_HUGE_NUMBER value;
41 NX_CRYPTO_EC test_ec;
42 
43     /* Print out test information banner.  */
44     printf("NetX Secure Test:   EC Additional Test.................................");
45 
46     scratch_ptr = scratch_buffer;
47     NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&(point.nx_crypto_ec_point_x), scratch_ptr, 64);
48     NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&(point.nx_crypto_ec_point_y), scratch_ptr, 64);
49     NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&(point1.nx_crypto_ec_point_x), scratch_ptr, 64);
50     NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&(point1.nx_crypto_ec_point_y), scratch_ptr, 64);
51 
52     point.nx_crypto_ec_point_type = NX_CRYPTO_EC_POINT_AFFINE;
53     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&(point.nx_crypto_ec_point_x), 0);
54     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&(point.nx_crypto_ec_point_y), 1);
55 
56     status = _nx_crypto_ec_point_is_infinite(&point);
57     EXPECT_EQ(NX_CRYPTO_FALSE, status);
58 
59     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&(point.nx_crypto_ec_point_y), 0);
60     status = _nx_crypto_ec_point_is_infinite(&point);
61     EXPECT_EQ(NX_CRYPTO_TRUE, status);
62 
63     _nx_crypto_ec_point_set_infinite(&point);
64 
65     /* Compressed format is not supported. */
66     buffer[0] = 0;
67     status = _nx_crypto_ec_point_setup(&point, buffer, 1);
68     EXPECT_EQ(NX_CRYPTO_FORMAT_NOT_SUPPORTED, status);
69 
70     /* ec_point_x _nx_crypto_huge_number_extract_fixed_size failed. */
71     point.nx_crypto_ec_point_x.nx_crypto_huge_number_data[63] = 0xff;
72     point.nx_crypto_ec_point_x.nx_crypto_huge_number_size = 64;
73     _nx_crypto_ec_point_extract_uncompressed((NX_CRYPTO_EC *)&_nx_crypto_ec_secp192r1, &point, buffer, 50, &huge_number_size);
74     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&(point.nx_crypto_ec_point_x), 0);
75 
76     /* ec_point_y _nx_crypto_huge_number_extract_fixed_size failed. */
77     point.nx_crypto_ec_point_y.nx_crypto_huge_number_data[62] = 0xff;
78     point.nx_crypto_ec_point_y.nx_crypto_huge_number_data[63] = 0;
79     point.nx_crypto_ec_point_y.nx_crypto_huge_number_size = 64;
80     _nx_crypto_ec_point_extract_uncompressed((NX_CRYPTO_EC *)&_nx_crypto_ec_secp192r1, &point, buffer, 50, &huge_number_size);
81 
82     /* byte_stream_size < 1 + (clen << 1) */
83     _nx_crypto_ec_point_extract_uncompressed((NX_CRYPTO_EC *)&_nx_crypto_ec_secp192r1, &point, buffer, 0, &huge_number_size);
84 
85     /* ec_point is infinite. */
86     point.nx_crypto_ec_point_type = NX_CRYPTO_EC_POINT_AFFINE;
87     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&(point.nx_crypto_ec_point_x), 0);
88     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&(point.nx_crypto_ec_point_y), 0);
89     _nx_crypto_ec_point_fp_projective_to_affine((NX_CRYPTO_EC *)&_nx_crypto_ec_secp192r1, &point, (VOID *)buffer);
90 
91 #ifdef NX_CRYPTO_SELF_TEST
92     /* The value and curve -> nx_crypto_ec_field.fp are equal. */
93     test_ec = _nx_crypto_ec_secp192r1;
94     test_ec.nx_crypto_ec_field.fp.nx_crypto_huge_number_data = (VOID *)buffer;
95     NX_CRYPTO_MEMCPY(buffer, _nx_crypto_ec_secp192r1.nx_crypto_ec_field.fp.nx_crypto_huge_number_data, _nx_crypto_ec_secp192r1.nx_crypto_ec_field.fp.nx_crypto_huge_number_size);
96     _nx_crypto_ec_secp192r1_reduce(&test_ec, &test_ec.nx_crypto_ec_field.fp, (VOID *)buffer);
97     _nx_crypto_ec_secp224r1_reduce(&test_ec, &test_ec.nx_crypto_ec_field.fp, (VOID *)buffer);
98     _nx_crypto_ec_secp256r1_reduce(&test_ec, &test_ec.nx_crypto_ec_field.fp, (VOID *)buffer);
99     _nx_crypto_ec_secp384r1_reduce(&test_ec, &test_ec.nx_crypto_ec_field.fp, (VOID *)buffer);
100     _nx_crypto_ec_secp521r1_reduce(&test_ec, &test_ec.nx_crypto_ec_field.fp, (VOID *)buffer);
101 
102     /* Invoke _nx_crypto_ec_subtract_digit_reduce. */
103     NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&value, scratch_ptr, 64);
104     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&value, 0);
105     _nx_crypto_ec_subtract_digit_reduce(&test_ec, &value, 2, NX_CRYPTO_NULL);
106 
107     value.nx_crypto_huge_number_size = 2;
108     value.nx_crypto_huge_number_data[1] = 1;
109     value.nx_crypto_huge_number_data[0] = 0;
110     _nx_crypto_ec_subtract_digit_reduce(&test_ec, &value, 2, NX_CRYPTO_NULL);
111 
112     value.nx_crypto_huge_number_size = 2;
113     value.nx_crypto_huge_number_data[1] = 1;
114     value.nx_crypto_huge_number_data[0] = 3;
115     _nx_crypto_ec_subtract_digit_reduce(&test_ec, &value, 2, NX_CRYPTO_NULL);
116 
117     /* _nx_crypto_ec_fp_affine_add, left == right. */
118     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&(test_ec.nx_crypto_ec_field.fp), 23);
119     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&(point.nx_crypto_ec_point_x), 23);
120     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&(point.nx_crypto_ec_point_y), 23);
121     _nx_crypto_ec_fp_affine_add(&test_ec, &point, &point, (HN_UBASE *)scratch_ptr);
122 
123     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&(point1.nx_crypto_ec_point_x), 23);
124     NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(&(point1.nx_crypto_ec_point_y), 24);
125     _nx_crypto_ec_fp_affine_add(&test_ec, &point, &point1, (HN_UBASE *)scratch_ptr);
126 
127 #endif /* NX_CRYPTO_SELF_TEST */
128 
129     printf("SUCCESS!\n");
130     test_control_return(0);
131 }
132