Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / tests / suites / test_suite_blowfish.function
1 /* BEGIN_HEADER */
2 #include "mbedtls/blowfish.h"
3 /* END_HEADER */
4
5 /* BEGIN_DEPENDENCIES
6  * depends_on:MBEDTLS_BLOWFISH_C
7  * END_DEPENDENCIES
8  */
9
10 /* BEGIN_CASE */
11 void blowfish_valid_param( )
12 {
13     TEST_VALID_PARAM( mbedtls_blowfish_free( NULL ) );
14 }
15 /* END_CASE */
16
17 /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
18 void blowfish_invalid_param( )
19 {
20     mbedtls_blowfish_context ctx;
21     unsigned char buf[16] = { 0 };
22     size_t const valid_keylength = sizeof( buf ) * 8;
23     size_t valid_mode = MBEDTLS_BLOWFISH_ENCRYPT;
24     size_t invalid_mode = 42;
25     size_t off;
26     ((void) off);
27
28     TEST_INVALID_PARAM( mbedtls_blowfish_init( NULL ) );
29     TEST_VALID_PARAM( mbedtls_blowfish_free( NULL ) );
30
31     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
32                             mbedtls_blowfish_setkey( NULL,
33                                                      buf,
34                                                      valid_keylength ) );
35     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
36                             mbedtls_blowfish_setkey( &ctx,
37                                                      NULL,
38                                                      valid_keylength ) );
39
40     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
41                             mbedtls_blowfish_crypt_ecb( NULL,
42                                                      valid_mode,
43                                                      buf, buf ) );
44     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
45                             mbedtls_blowfish_crypt_ecb( &ctx,
46                                                         invalid_mode,
47                                                         buf, buf ) );
48     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
49                             mbedtls_blowfish_crypt_ecb( &ctx,
50                                                         valid_mode,
51                                                         NULL, buf ) );
52     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
53                             mbedtls_blowfish_crypt_ecb( &ctx,
54                                                         valid_mode,
55                                                         buf, NULL ) );
56
57 #if defined(MBEDTLS_CIPHER_MODE_CBC)
58     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
59                             mbedtls_blowfish_crypt_cbc( NULL,
60                                                         valid_mode,
61                                                         sizeof( buf ),
62                                                         buf, buf, buf ) );
63     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
64                             mbedtls_blowfish_crypt_cbc( &ctx,
65                                                         invalid_mode,
66                                                         sizeof( buf ),
67                                                         buf, buf, buf ) );
68     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
69                             mbedtls_blowfish_crypt_cbc( &ctx,
70                                                         valid_mode,
71                                                         sizeof( buf ),
72                                                         NULL, buf, buf ) );
73     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
74                             mbedtls_blowfish_crypt_cbc( &ctx,
75                                                         valid_mode,
76                                                         sizeof( buf ),
77                                                         buf, NULL, buf ) );
78     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
79                             mbedtls_blowfish_crypt_cbc( &ctx,
80                                                         valid_mode,
81                                                         sizeof( buf ),
82                                                         buf, buf, NULL ) );
83 #endif /* MBEDTLS_CIPHER_MODE_CBC */
84
85 #if defined(MBEDTLS_CIPHER_MODE_CFB)
86     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
87                             mbedtls_blowfish_crypt_cfb64( NULL,
88                                                           valid_mode,
89                                                           sizeof( buf ),
90                                                           &off, buf,
91                                                           buf, buf ) );
92     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
93                             mbedtls_blowfish_crypt_cfb64( &ctx,
94                                                           invalid_mode,
95                                                           sizeof( buf ),
96                                                           &off, buf,
97                                                           buf, buf ) );
98     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
99                             mbedtls_blowfish_crypt_cfb64( &ctx,
100                                                           valid_mode,
101                                                           sizeof( buf ),
102                                                           NULL, buf,
103                                                           buf, buf ) );
104     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
105                             mbedtls_blowfish_crypt_cfb64( &ctx,
106                                                           valid_mode,
107                                                           sizeof( buf ),
108                                                           &off, NULL,
109                                                           buf, buf ) );
110     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
111                             mbedtls_blowfish_crypt_cfb64( &ctx,
112                                                           valid_mode,
113                                                           sizeof( buf ),
114                                                           &off, buf,
115                                                           NULL, buf ) );
116     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
117                             mbedtls_blowfish_crypt_cfb64( &ctx,
118                                                           valid_mode,
119                                                           sizeof( buf ),
120                                                           &off, buf,
121                                                           buf, NULL ) );
122 #endif /* MBEDTLS_CIPHER_MODE_CFB */
123
124 #if defined(MBEDTLS_CIPHER_MODE_CTR)
125     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
126                             mbedtls_blowfish_crypt_ctr( NULL,
127                                                         sizeof( buf ),
128                                                         &off,
129                                                         buf, buf,
130                                                         buf, buf ) );
131     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
132                             mbedtls_blowfish_crypt_ctr( &ctx,
133                                                         sizeof( buf ),
134                                                         NULL,
135                                                         buf, buf,
136                                                         buf, buf ) );
137     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
138                             mbedtls_blowfish_crypt_ctr( &ctx,
139                                                         sizeof( buf ),
140                                                         &off,
141                                                         NULL, buf,
142                                                         buf, buf ) );
143     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
144                             mbedtls_blowfish_crypt_ctr( &ctx,
145                                                         sizeof( buf ),
146                                                         &off,
147                                                         buf, NULL,
148                                                         buf, buf ) );
149     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
150                             mbedtls_blowfish_crypt_ctr( &ctx,
151                                                         sizeof( buf ),
152                                                         &off,
153                                                         buf, buf,
154                                                         NULL, buf ) );
155     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
156                             mbedtls_blowfish_crypt_ctr( &ctx,
157                                                         sizeof( buf ),
158                                                         &off,
159                                                         buf, buf,
160                                                         buf, NULL ) );
161 #endif /* MBEDTLS_CIPHER_MODE_CTR */
162
163 exit:
164     return;
165 }
166 /* END_CASE */
167
168 /* BEGIN_CASE */
169 void blowfish_encrypt_ecb( data_t * key_str, data_t * src_str,
170                            data_t * hex_dst_string, int setkey_result )
171 {
172     unsigned char output[100];
173     mbedtls_blowfish_context ctx;
174
175     memset(output, 0x00, 100);
176     mbedtls_blowfish_init( &ctx );
177
178
179     TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
180     if( setkey_result == 0 )
181     {
182         TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->x, output ) == 0 );
183
184         TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
185     }
186
187 exit:
188     mbedtls_blowfish_free( &ctx );
189 }
190 /* END_CASE */
191
192 /* BEGIN_CASE */
193 void blowfish_decrypt_ecb( data_t * key_str, data_t * src_str,
194                            data_t * hex_dst_string, int setkey_result )
195 {
196     unsigned char output[100];
197     mbedtls_blowfish_context ctx;
198
199     memset(output, 0x00, 100);
200     mbedtls_blowfish_init( &ctx );
201
202
203     TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
204     if( setkey_result == 0 )
205     {
206         TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->x, output ) == 0 );
207
208         TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
209     }
210
211 exit:
212     mbedtls_blowfish_free( &ctx );
213 }
214 /* END_CASE */
215
216 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
217 void blowfish_encrypt_cbc( data_t * key_str, data_t * iv_str,
218                            data_t * src_str, data_t * hex_dst_string,
219                            int cbc_result )
220 {
221     unsigned char output[100];
222     mbedtls_blowfish_context ctx;
223
224     memset(output, 0x00, 100);
225     mbedtls_blowfish_init( &ctx );
226
227
228     mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
229
230     TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->len , iv_str->x, src_str->x, output ) == cbc_result );
231     if( cbc_result == 0 )
232     {
233
234         TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
235     }
236
237 exit:
238     mbedtls_blowfish_free( &ctx );
239 }
240 /* END_CASE */
241
242 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
243 void blowfish_decrypt_cbc( data_t * key_str, data_t * iv_str,
244                            data_t * src_str, data_t * hex_dst_string,
245                            int cbc_result )
246 {
247     unsigned char output[100];
248     mbedtls_blowfish_context ctx;
249
250     memset(output, 0x00, 100);
251     mbedtls_blowfish_init( &ctx );
252
253
254     mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
255     TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->len , iv_str->x, src_str->x, output ) == cbc_result );
256     if( cbc_result == 0)
257     {
258
259         TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
260     }
261
262 exit:
263     mbedtls_blowfish_free( &ctx );
264 }
265 /* END_CASE */
266
267 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
268 void blowfish_encrypt_cfb64( data_t * key_str, data_t * iv_str,
269                              data_t * src_str, data_t * hex_dst_string
270                              )
271 {
272     unsigned char output[100];
273     mbedtls_blowfish_context ctx;
274     size_t iv_offset = 0;
275
276     memset(output, 0x00, 100);
277     mbedtls_blowfish_init( &ctx );
278
279
280     mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
281     TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->len, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
282
283     TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
284
285 exit:
286     mbedtls_blowfish_free( &ctx );
287 }
288 /* END_CASE */
289
290 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
291 void blowfish_decrypt_cfb64( data_t * key_str, data_t * iv_str,
292                              data_t * src_str, data_t * hex_dst_string
293                              )
294 {
295     unsigned char output[100];
296     mbedtls_blowfish_context ctx;
297     size_t iv_offset = 0;
298
299     memset(output, 0x00, 100);
300     mbedtls_blowfish_init( &ctx );
301
302
303     mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
304     TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->len, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
305
306     TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
307
308 exit:
309     mbedtls_blowfish_free( &ctx );
310 }
311 /* END_CASE */
312
313 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
314 void blowfish_encrypt_ctr( data_t * key_str, data_t * iv_str,
315                            data_t * src_str, data_t * hex_dst_string )
316 {
317     unsigned char stream_str[100];
318     unsigned char output[100];
319     mbedtls_blowfish_context ctx;
320     size_t iv_offset = 0;
321
322     memset(stream_str, 0x00, 100);
323     memset(output, 0x00, 100);
324     mbedtls_blowfish_init( &ctx );
325
326
327     mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
328     TEST_ASSERT( mbedtls_blowfish_crypt_ctr( &ctx, src_str->len, &iv_offset, iv_str->x, stream_str, src_str->x, output ) == 0 );
329
330     TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
331
332 exit:
333     mbedtls_blowfish_free( &ctx );
334 }
335 /* END_CASE */