2 #include "mbedtls/nist_kw.h"
6 * depends_on:MBEDTLS_NIST_KW_C
10 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */
11 void mbedtls_nist_kw_self_test( )
13 TEST_ASSERT( mbedtls_nist_kw_self_test( 1 ) == 0 );
17 /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
18 void mbedtls_nist_kw_mix_contexts( )
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];
27 memset( plaintext, 0, sizeof( plaintext ) );
28 memset( ciphertext1, 0, sizeof( ciphertext1 ) );
29 memset( ciphertext2, 0, sizeof( ciphertext2 ) );
30 memset( key, 0, sizeof( key ) );
33 * 1. Check wrap and unwrap with two separate contexts
35 mbedtls_nist_kw_init( &ctx1 );
36 mbedtls_nist_kw_init( &ctx2 );
38 TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx1,
39 MBEDTLS_CIPHER_ID_AES,
40 key, sizeof( key ) * 8,
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 ) );
49 TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx2,
50 MBEDTLS_CIPHER_ID_AES,
51 key, sizeof( key ) * 8,
54 TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx2, MBEDTLS_KW_MODE_KW,
55 ciphertext1, output_len,
56 plaintext, &output_len,
57 sizeof( plaintext ) ) == 0 );
59 TEST_ASSERT( output_len == sizeof( plaintext ) );
60 for( i = 0; i < sizeof( plaintext ); i++ )
62 TEST_ASSERT( plaintext[i] == 0 );
64 mbedtls_nist_kw_free( &ctx1 );
65 mbedtls_nist_kw_free( &ctx2 );
68 * 2. Check wrapping with two modes, on same context
70 mbedtls_nist_kw_init( &ctx1 );
71 mbedtls_nist_kw_init( &ctx2 );
72 output_len = sizeof( ciphertext1 );
74 TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx1,
75 MBEDTLS_CIPHER_ID_AES,
76 key, sizeof( key ) * 8,
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 ) );
85 TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx1, MBEDTLS_KW_MODE_KWP,
86 plaintext, sizeof( plaintext ),
87 ciphertext2, &output_len,
88 sizeof( ciphertext2 ) ) == 0 );
90 TEST_ASSERT( output_len == sizeof( ciphertext2 ) );
92 TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx2,
93 MBEDTLS_CIPHER_ID_AES,
94 key, sizeof( key ) * 8,
97 TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx2, MBEDTLS_KW_MODE_KW,
98 ciphertext1, sizeof( ciphertext1 ),
99 plaintext, &output_len,
100 sizeof( plaintext ) ) == 0 );
102 TEST_ASSERT( output_len == sizeof( plaintext ) );
104 for( i = 0; i < sizeof( plaintext ); i++ )
106 TEST_ASSERT( plaintext[i] == 0 );
109 TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx2, MBEDTLS_KW_MODE_KWP,
110 ciphertext2, sizeof( ciphertext2 ),
111 plaintext, &output_len,
112 sizeof( plaintext ) ) == 0 );
114 TEST_ASSERT( output_len == sizeof( plaintext ) );
116 for( i = 0; i < sizeof( plaintext ); i++ )
118 TEST_ASSERT( plaintext[i] == 0 );
122 mbedtls_nist_kw_free( &ctx1 );
123 mbedtls_nist_kw_free( &ctx2 );
128 void mbedtls_nist_kw_setkey( int cipher_id, int key_size,
129 int is_wrap, int result )
131 mbedtls_nist_kw_context ctx;
132 unsigned char key[32];
135 mbedtls_nist_kw_init( &ctx );
137 memset( key, 0x2A, sizeof( key ) );
138 TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
140 ret = mbedtls_nist_kw_setkey( &ctx, cipher_id, key, key_size, is_wrap );
141 TEST_ASSERT( ret == result );
144 mbedtls_nist_kw_free( &ctx );
148 /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
149 void nist_kw_plaintext_lengths( int in_len, int out_len, int mode, int res )
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;
157 mbedtls_nist_kw_init( &ctx );
159 memset( key, 0, sizeof( key ) );
163 plaintext = mbedtls_calloc( 1, in_len );
164 TEST_ASSERT( plaintext != NULL );
169 ciphertext = mbedtls_calloc( 1, output_len );
170 TEST_ASSERT( ciphertext != NULL );
173 memset( plaintext, 0, in_len );
174 memset( ciphertext, 0, output_len );
177 TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
178 key, 8 * sizeof( key ), 1 ) == 0 );
180 TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx, mode, plaintext, in_len,
181 ciphertext, &output_len,
182 output_len ) == res );
185 if( mode == MBEDTLS_KW_MODE_KWP )
186 TEST_ASSERT( output_len == (size_t) in_len + 8 -
187 ( in_len % 8 ) + 8 );
189 TEST_ASSERT( output_len == (size_t) in_len + 8 );
193 TEST_ASSERT( output_len == 0 );
197 mbedtls_free( ciphertext );
198 mbedtls_free( plaintext );
199 mbedtls_nist_kw_free( &ctx );
203 /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
204 void nist_kw_ciphertext_lengths( int in_len, int out_len, int mode, int res )
206 mbedtls_nist_kw_context ctx;
207 unsigned char key[16];
208 unsigned char *plaintext = NULL;
209 unsigned char *ciphertext = NULL;
211 size_t output_len = out_len;
213 mbedtls_nist_kw_init( &ctx );
215 memset( key, 0, sizeof( key ) );
219 plaintext = mbedtls_calloc( 1, output_len );
220 TEST_ASSERT( plaintext != NULL );
224 ciphertext = mbedtls_calloc( 1, in_len );
225 TEST_ASSERT( ciphertext != NULL );
228 memset( plaintext, 0, output_len );
229 memset( ciphertext, 0, in_len );
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,
239 TEST_ASSERT( unwrap_ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
241 TEST_ASSERT( unwrap_ret == res );
243 TEST_ASSERT( output_len == 0 );
246 mbedtls_free( ciphertext );
247 mbedtls_free( plaintext );
248 mbedtls_nist_kw_free( &ctx );
253 void mbedtls_nist_kw_wrap( int cipher_id, int mode,
254 char *key_hex, char *msg_hex,
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;
264 mbedtls_nist_kw_init( &ctx );
266 memset( key, 0x00, sizeof( key ) );
267 memset( msg, 0x00, sizeof( msg ) );
268 memset( result, '+', sizeof( result ) );
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 );
275 TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, cipher_id, key, key_len * 8, 1 )
278 /* Test with input == output */
279 TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx, mode, msg, msg_len,
280 result, &output_len, sizeof( result ) ) == 0 );
282 TEST_ASSERT( output_len == result_len );
284 TEST_ASSERT( memcmp( expected_result, result, result_len ) == 0 );
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++ )
290 TEST_ASSERT( result[i] == '+' );
294 mbedtls_nist_kw_free( &ctx );
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 )
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;
310 mbedtls_nist_kw_init( &ctx );
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 ) );
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 );
322 TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, cipher_id, key, key_len * 8, 0 )
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 )
330 TEST_ASSERT( output_len == result_len );
331 TEST_ASSERT( memcmp( expected_result, result, result_len ) == 0 );
335 TEST_ASSERT( output_len == 0 );
338 /* Check that the function didn't write beyond the end of the buffer. */
339 for( i = msg_len - 8; i < sizeof( result ); i++ )
341 TEST_ASSERT( result[i] == '+' );
345 mbedtls_nist_kw_free( &ctx );