Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / tests / suites / test_suite_nist_kw.function
1 /* BEGIN_HEADER */
2 #include "mbedtls/nist_kw.h"
3 /* END_HEADER */
4
5 /* BEGIN_DEPENDENCIES
6  * depends_on:MBEDTLS_NIST_KW_C
7  * END_DEPENDENCIES
8  */
9
10 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */
11 void mbedtls_nist_kw_self_test( )
12 {
13     TEST_ASSERT( mbedtls_nist_kw_self_test( 1 ) == 0 );
14 }
15 /* END_CASE */
16
17 /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
18 void mbedtls_nist_kw_mix_contexts( )
19 {
20     mbedtls_nist_kw_context ctx1, ctx2;
21     unsigned char key[16];
22     unsigned char plaintext[32];
23     unsigned char ciphertext1[40];
24     unsigned char ciphertext2[40];
25     size_t output_len, i;
26
27     memset( plaintext, 0, sizeof( plaintext ) );
28     memset( ciphertext1, 0, sizeof( ciphertext1 ) );
29     memset( ciphertext2, 0, sizeof( ciphertext2 ) );
30     memset( key, 0, sizeof( key ) );
31
32     /*
33      * 1. Check wrap and unwrap with two separate contexts
34      */
35     mbedtls_nist_kw_init( &ctx1 );
36     mbedtls_nist_kw_init( &ctx2 );
37
38     TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx1,
39                                          MBEDTLS_CIPHER_ID_AES,
40                                          key, sizeof( key ) * 8,
41                                          1 ) == 0 );
42
43     TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx1, MBEDTLS_KW_MODE_KW,
44                                        plaintext, sizeof( plaintext ),
45                                        ciphertext1, &output_len,
46                                        sizeof( ciphertext1 ) ) == 0 );
47     TEST_ASSERT( output_len == sizeof( ciphertext1 ) );
48
49     TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx2,
50                                          MBEDTLS_CIPHER_ID_AES,
51                                          key, sizeof( key ) * 8,
52                                          0 ) == 0 );
53
54     TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx2, MBEDTLS_KW_MODE_KW,
55                                          ciphertext1, output_len,
56                                          plaintext, &output_len,
57                                          sizeof( plaintext ) ) == 0 );
58
59     TEST_ASSERT( output_len == sizeof( plaintext ) );
60     for( i = 0; i < sizeof( plaintext ); i++ )
61     {
62         TEST_ASSERT( plaintext[i] == 0 );
63     }
64     mbedtls_nist_kw_free( &ctx1 );
65     mbedtls_nist_kw_free( &ctx2 );
66
67     /*
68      * 2. Check wrapping with two modes, on same context
69      */
70     mbedtls_nist_kw_init( &ctx1 );
71     mbedtls_nist_kw_init( &ctx2 );
72     output_len = sizeof( ciphertext1 );
73
74     TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx1,
75                                          MBEDTLS_CIPHER_ID_AES,
76                                          key, sizeof( key ) * 8,
77                                          1 ) == 0 );
78
79     TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx1, MBEDTLS_KW_MODE_KW,
80                                        plaintext, sizeof( plaintext ),
81                                        ciphertext1, &output_len,
82                                        sizeof( ciphertext1 ) ) == 0 );
83     TEST_ASSERT( output_len == sizeof( ciphertext1 ) );
84
85     TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx1, MBEDTLS_KW_MODE_KWP,
86                                        plaintext, sizeof( plaintext ),
87                                        ciphertext2, &output_len,
88                                        sizeof( ciphertext2 ) ) == 0 );
89
90     TEST_ASSERT( output_len == sizeof( ciphertext2 ) );
91
92     TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx2,
93                                          MBEDTLS_CIPHER_ID_AES,
94                                          key, sizeof( key ) * 8,
95                                          0 ) == 0 );
96
97     TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx2, MBEDTLS_KW_MODE_KW,
98                                          ciphertext1, sizeof( ciphertext1 ),
99                                          plaintext, &output_len,
100                                          sizeof( plaintext ) ) == 0 );
101
102     TEST_ASSERT( output_len == sizeof( plaintext ) );
103
104     for( i = 0; i < sizeof( plaintext ); i++ )
105     {
106         TEST_ASSERT( plaintext[i] == 0 );
107     }
108
109     TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx2, MBEDTLS_KW_MODE_KWP,
110                                          ciphertext2, sizeof( ciphertext2 ),
111                                          plaintext, &output_len,
112                                          sizeof( plaintext ) ) == 0 );
113
114     TEST_ASSERT( output_len == sizeof( plaintext ) );
115
116     for( i = 0; i < sizeof( plaintext ); i++ )
117     {
118         TEST_ASSERT( plaintext[i] == 0 );
119     }
120
121 exit:
122     mbedtls_nist_kw_free( &ctx1 );
123     mbedtls_nist_kw_free( &ctx2 );
124 }
125 /* END_CASE */
126
127 /* BEGIN_CASE */
128 void mbedtls_nist_kw_setkey( int cipher_id, int key_size,
129                              int is_wrap, int result )
130 {
131     mbedtls_nist_kw_context ctx;
132     unsigned char key[32];
133     int ret;
134
135     mbedtls_nist_kw_init( &ctx );
136
137     memset( key, 0x2A, sizeof( key ) );
138     TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
139
140     ret = mbedtls_nist_kw_setkey( &ctx, cipher_id, key, key_size, is_wrap );
141     TEST_ASSERT( ret == result );
142
143 exit:
144     mbedtls_nist_kw_free( &ctx );
145 }
146 /* END_CASE */
147
148 /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
149 void nist_kw_plaintext_lengths( int in_len, int out_len, int mode, int res )
150 {
151     mbedtls_nist_kw_context ctx;
152     unsigned char key[16];
153     unsigned char *plaintext = NULL;
154     unsigned char *ciphertext = NULL;
155     size_t output_len = out_len;
156
157     mbedtls_nist_kw_init( &ctx );
158
159     memset( key, 0, sizeof( key ) );
160
161     if( in_len != 0 )
162     {
163         plaintext = mbedtls_calloc( 1, in_len );
164         TEST_ASSERT( plaintext != NULL );
165     }
166
167     if( out_len != 0 )
168     {
169         ciphertext = mbedtls_calloc( 1, output_len );
170         TEST_ASSERT( ciphertext != NULL );
171     }
172
173     memset( plaintext, 0, in_len );
174     memset( ciphertext, 0, output_len );
175
176
177     TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
178                                          key, 8 * sizeof( key ), 1 ) == 0 );
179
180     TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx, mode, plaintext, in_len,
181                                       ciphertext, &output_len,
182                                       output_len ) == res );
183     if( res == 0 )
184     {
185         if( mode == MBEDTLS_KW_MODE_KWP )
186             TEST_ASSERT( output_len == (size_t) in_len + 8 -
187                          ( in_len % 8 ) + 8 );
188         else
189             TEST_ASSERT( output_len == (size_t) in_len + 8 );
190     }
191     else
192     {
193         TEST_ASSERT( output_len == 0 );
194     }
195
196 exit:
197     mbedtls_free( ciphertext );
198     mbedtls_free( plaintext );
199     mbedtls_nist_kw_free( &ctx );
200 }
201 /* END_CASE */
202
203 /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
204 void nist_kw_ciphertext_lengths( int in_len, int out_len, int mode, int res )
205 {
206     mbedtls_nist_kw_context ctx;
207     unsigned char key[16];
208     unsigned char *plaintext = NULL;
209     unsigned char *ciphertext = NULL;
210     int unwrap_ret;
211     size_t output_len = out_len;
212
213     mbedtls_nist_kw_init( &ctx );
214
215     memset( key, 0, sizeof( key ) );
216
217     if( out_len != 0 )
218     {
219         plaintext = mbedtls_calloc( 1, output_len );
220         TEST_ASSERT( plaintext != NULL );
221     }
222     if( in_len != 0 )
223     {
224         ciphertext = mbedtls_calloc( 1, in_len );
225         TEST_ASSERT( ciphertext != NULL );
226     }
227
228     memset( plaintext, 0, output_len );
229     memset( ciphertext, 0, in_len );
230
231
232     TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
233                                          key, 8 * sizeof( key ), 0 ) == 0 );
234     unwrap_ret = mbedtls_nist_kw_unwrap( &ctx, mode, ciphertext, in_len,
235                                          plaintext, &output_len,
236                                          output_len );
237
238     if( res == 0 )
239         TEST_ASSERT( unwrap_ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
240     else
241         TEST_ASSERT( unwrap_ret == res );
242
243     TEST_ASSERT( output_len == 0 );
244
245 exit:
246     mbedtls_free( ciphertext );
247     mbedtls_free( plaintext );
248     mbedtls_nist_kw_free( &ctx );
249 }
250 /* END_CASE */
251
252 /* BEGIN_CASE */
253 void mbedtls_nist_kw_wrap( int cipher_id, int mode,
254                            char *key_hex, char *msg_hex,
255                            char *result_hex )
256 {
257     unsigned char key[32];
258     unsigned char msg[512];
259     unsigned char result[528];
260     unsigned char expected_result[528];
261     mbedtls_nist_kw_context ctx;
262     size_t key_len, msg_len, output_len, result_len, i, padlen;
263
264     mbedtls_nist_kw_init( &ctx );
265
266     memset( key, 0x00, sizeof( key ) );
267     memset( msg, 0x00, sizeof( msg ) );
268     memset( result, '+', sizeof( result ) );
269
270     key_len = unhexify( key, key_hex );
271     msg_len = unhexify( msg, msg_hex );
272     result_len = unhexify( expected_result, result_hex );
273     output_len = sizeof( result );
274
275     TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, cipher_id, key, key_len * 8, 1 )
276                  == 0 );
277
278     /* Test with input == output */
279     TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx, mode, msg, msg_len,
280                  result, &output_len, sizeof( result ) ) == 0 );
281
282     TEST_ASSERT( output_len == result_len );
283
284     TEST_ASSERT( memcmp( expected_result, result, result_len ) == 0 );
285
286     padlen = ( msg_len % 8 != 0 ) ? 8 - (msg_len % 8 ) : 0;
287     /* Check that the function didn't write beyond the end of the buffer. */
288     for( i = msg_len + 8 + padlen; i < sizeof( result ); i++ )
289     {
290         TEST_ASSERT( result[i] == '+' );
291     }
292
293 exit:
294     mbedtls_nist_kw_free( &ctx );
295 }
296 /* END_CASE */
297
298 /* BEGIN_CASE */
299 void mbedtls_nist_kw_unwrap( int cipher_id, int mode,
300                              char *key_hex, char *msg_hex,
301                              char *result_hex, int expected_ret )
302 {
303     unsigned char key[32];
304     unsigned char msg[528];
305     unsigned char result[528];
306     unsigned char expected_result[528];
307     mbedtls_nist_kw_context ctx;
308     size_t key_len, msg_len, output_len, result_len, i;
309
310     mbedtls_nist_kw_init( &ctx );
311
312     memset( key, 0x00, sizeof( key ) );
313     memset( msg, 0x00, sizeof( msg ) );
314     memset( result, '+', sizeof( result ) );
315     memset( expected_result, 0x00, sizeof( expected_result ) );
316
317     key_len = unhexify( key, key_hex );
318     msg_len = unhexify( msg, msg_hex );
319     result_len = unhexify( expected_result, result_hex );
320     output_len = sizeof( result );
321
322     TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, cipher_id, key, key_len * 8, 0 )
323                  == 0 );
324
325     /* Test with input == output */
326     TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx, mode, msg, msg_len,
327                  result, &output_len, sizeof( result ) ) == expected_ret );
328     if( expected_ret == 0 )
329     {
330         TEST_ASSERT( output_len == result_len );
331         TEST_ASSERT( memcmp( expected_result, result, result_len ) == 0 );
332     }
333     else
334     {
335         TEST_ASSERT( output_len == 0 );
336     }
337
338     /* Check that the function didn't write beyond the end of the buffer. */
339     for( i = msg_len - 8; i < sizeof( result ); i++ )
340     {
341         TEST_ASSERT( result[i] == '+' );
342     }
343
344 exit:
345     mbedtls_nist_kw_free( &ctx );
346 }
347 /* END_CASE */