Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / tests / suites / test_suite_des.function
1 /* BEGIN_HEADER */
2 #include "mbedtls/des.h"
3 /* END_HEADER */
4
5 /* BEGIN_DEPENDENCIES
6  * depends_on:MBEDTLS_DES_C
7  * END_DEPENDENCIES
8  */
9
10 /* BEGIN_CASE */
11 void des_check_weak( data_t * key, int ret )
12 {
13     TEST_ASSERT( mbedtls_des_key_check_weak( key->x ) == ret );
14 }
15 /* END_CASE */
16
17 /* BEGIN_CASE */
18 void des_encrypt_ecb( data_t * key_str, data_t * src_str,
19                       data_t * hex_dst_string )
20 {
21     unsigned char output[100];
22     mbedtls_des_context ctx;
23
24     memset(output, 0x00, 100);
25     mbedtls_des_init( &ctx );
26
27
28     mbedtls_des_setkey_enc( &ctx, key_str->x );
29     TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str->x, output ) == 0 );
30
31     TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
32
33 exit:
34     mbedtls_des_free( &ctx );
35 }
36 /* END_CASE */
37
38 /* BEGIN_CASE */
39 void des_decrypt_ecb( data_t * key_str, data_t * src_str,
40                       data_t * hex_dst_string )
41 {
42     unsigned char output[100];
43     mbedtls_des_context ctx;
44
45     memset(output, 0x00, 100);
46     mbedtls_des_init( &ctx );
47
48
49     mbedtls_des_setkey_dec( &ctx, key_str->x );
50     TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str->x, output ) == 0 );
51
52     TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
53
54 exit:
55     mbedtls_des_free( &ctx );
56 }
57 /* END_CASE */
58
59 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
60 void des_encrypt_cbc( data_t * key_str, data_t * iv_str,
61                       data_t * src_str, data_t * hex_dst_string,
62                       int cbc_result )
63 {
64     unsigned char output[100];
65     mbedtls_des_context ctx;
66
67     memset(output, 0x00, 100);
68     mbedtls_des_init( &ctx );
69
70
71     mbedtls_des_setkey_enc( &ctx, key_str->x );
72     TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
73     if( cbc_result == 0 )
74     {
75
76         TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
77     }
78
79 exit:
80     mbedtls_des_free( &ctx );
81 }
82 /* END_CASE */
83
84 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
85 void des_decrypt_cbc( data_t * key_str, data_t * iv_str,
86                       data_t * src_str, data_t * hex_dst_string,
87                       int cbc_result )
88 {
89     unsigned char output[100];
90     mbedtls_des_context ctx;
91
92     memset(output, 0x00, 100);
93     mbedtls_des_init( &ctx );
94
95
96     mbedtls_des_setkey_dec( &ctx, key_str->x );
97     TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
98     if( cbc_result == 0 )
99     {
100
101         TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
102     }
103
104 exit:
105     mbedtls_des_free( &ctx );
106 }
107 /* END_CASE */
108
109 /* BEGIN_CASE */
110 void des3_encrypt_ecb( int key_count, data_t * key_str,
111                        data_t * src_str, data_t * hex_dst_string )
112 {
113     unsigned char output[100];
114     mbedtls_des3_context ctx;
115
116     memset(output, 0x00, 100);
117     mbedtls_des3_init( &ctx );
118
119
120     if( key_count == 2 )
121         mbedtls_des3_set2key_enc( &ctx, key_str->x );
122     else if( key_count == 3 )
123         mbedtls_des3_set3key_enc( &ctx, key_str->x );
124     else
125         TEST_ASSERT( 0 );
126
127     TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str->x, output ) == 0 );
128
129     TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
130
131 exit:
132     mbedtls_des3_free( &ctx );
133 }
134 /* END_CASE */
135
136 /* BEGIN_CASE */
137 void des3_decrypt_ecb( int key_count, data_t * key_str,
138                        data_t * src_str, data_t * hex_dst_string )
139 {
140     unsigned char output[100];
141     mbedtls_des3_context ctx;
142
143     memset(output, 0x00, 100);
144     mbedtls_des3_init( &ctx );
145
146
147     if( key_count == 2 )
148         mbedtls_des3_set2key_dec( &ctx, key_str->x );
149     else if( key_count == 3 )
150         mbedtls_des3_set3key_dec( &ctx, key_str->x );
151     else
152         TEST_ASSERT( 0 );
153
154     TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str->x, output ) == 0 );
155
156     TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
157
158 exit:
159     mbedtls_des3_free( &ctx );
160 }
161 /* END_CASE */
162
163 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
164 void des3_encrypt_cbc( int key_count, data_t * key_str,
165                        data_t * iv_str, data_t * src_str,
166                        data_t * hex_dst_string, int cbc_result )
167 {
168     unsigned char output[100];
169     mbedtls_des3_context ctx;
170
171     memset(output, 0x00, 100);
172     mbedtls_des3_init( &ctx );
173
174
175     if( key_count == 2 )
176         mbedtls_des3_set2key_enc( &ctx, key_str->x );
177     else if( key_count == 3 )
178         mbedtls_des3_set3key_enc( &ctx, key_str->x );
179     else
180         TEST_ASSERT( 0 );
181
182     TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
183
184     if( cbc_result == 0 )
185     {
186
187         TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
188     }
189
190 exit:
191     mbedtls_des3_free( &ctx );
192 }
193 /* END_CASE */
194
195 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
196 void des3_decrypt_cbc( int key_count, data_t * key_str,
197                        data_t * iv_str, data_t * src_str,
198                        data_t * hex_dst_string, int cbc_result )
199 {
200     unsigned char output[100];
201     mbedtls_des3_context ctx;
202
203     memset(output, 0x00, 100);
204     mbedtls_des3_init( &ctx );
205
206
207     if( key_count == 2 )
208         mbedtls_des3_set2key_dec( &ctx, key_str->x );
209     else if( key_count == 3 )
210         mbedtls_des3_set3key_dec( &ctx, key_str->x );
211     else
212         TEST_ASSERT( 0 );
213
214     TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
215
216     if( cbc_result == 0 )
217     {
218
219         TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
220     }
221
222 exit:
223     mbedtls_des3_free( &ctx );
224 }
225 /* END_CASE */
226
227 /* BEGIN_CASE */
228 void des_key_parity_run(  )
229 {
230     int i, j, cnt;
231     unsigned char key[MBEDTLS_DES_KEY_SIZE];
232     unsigned int parity;
233
234     memset( key, 0, MBEDTLS_DES_KEY_SIZE );
235     cnt = 0;
236
237     // Iterate through all possible byte values
238     //
239     for( i = 0; i < 32; i++ )
240     {
241         for( j = 0; j < 8; j++ )
242             key[j] = cnt++;
243
244         // Set the key parity according to the table
245         //
246         mbedtls_des_key_set_parity( key );
247
248         // Check the parity with a function
249         //
250         for( j = 0; j < 8; j++ )
251         {
252             parity = key[j] ^ ( key[j] >> 4 );
253             parity = parity ^
254                     ( parity >> 1 ) ^
255                     ( parity >> 2 ) ^
256                     ( parity >> 3 );
257             parity &= 1;
258
259             if( parity != 1 )
260                 TEST_ASSERT( 0 );
261         }
262
263         // Check the parity with the table
264         //
265         TEST_ASSERT( mbedtls_des_key_check_key_parity( key ) == 0 );
266     }
267 }
268 /* END_CASE */
269
270 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
271 void des_selftest(  )
272 {
273     TEST_ASSERT( mbedtls_des_self_test( 1 ) == 0 );
274 }
275 /* END_CASE */