blob: f452afef40d4605a4a03d73694e4ced23b4ac273 [file] [log] [blame]
Simon Butcherd812fa62016-10-05 14:13:31 +01001/* BEGIN_HEADER */
2#include "mbedtls/cipher.h"
3#include "mbedtls/cmac.h"
4/* END_HEADER */
5
6/* BEGIN_DEPENDENCIES
7 * depends_on:MBEDTLS_CMAC_C
8 * END_DEPENDENCIES
9 */
10
11/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
12void mbedtls_cmac_self_test( )
13{
14 TEST_ASSERT( mbedtls_cmac_self_test( 1 ) == 0 );
15}
16/* END_CASE */
17
18/* BEGIN_CASE */
Simon Butcher33183fd2016-10-10 21:41:03 +010019void mbedtls_cmac_null_args( )
20{
21 mbedtls_cipher_context_t ctx;
22 const mbedtls_cipher_info_t *cipher_info;
23 unsigned char test_key[MBEDTLS_CIPHER_BLKSIZE_MAX];
24 unsigned char test_data[MBEDTLS_CIPHER_BLKSIZE_MAX];
25 unsigned char test_output[MBEDTLS_CIPHER_BLKSIZE_MAX];
26
27 mbedtls_cipher_init( &ctx );
28
29 /* Test NULL cipher info */
30 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, test_data, 16 ) ==
31 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
32
33 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
34 TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
35
36 TEST_ASSERT( mbedtls_cipher_cmac_starts( NULL, test_key, 128 ) ==
37 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
38
39 TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx, NULL, 128 ) ==
40 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
41
42 TEST_ASSERT( mbedtls_cipher_cmac_update( NULL, test_data, 16 ) ==
43 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
44
45 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, NULL, 16 ) ==
46 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
47
48 TEST_ASSERT( mbedtls_cipher_cmac_finish( NULL, test_output ) ==
49 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
50
51 TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, NULL ) ==
52 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
53
54 TEST_ASSERT( mbedtls_cipher_cmac_reset( NULL ) ==
55 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
56
57 TEST_ASSERT( mbedtls_cipher_cmac( NULL,
58 test_key, 128,
59 test_data, 16,
60 test_output ) ==
61 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
62
63 TEST_ASSERT( mbedtls_cipher_cmac( cipher_info,
64 NULL, 128,
65 test_data, 16,
66 test_output ) ==
67 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
68
69 TEST_ASSERT( mbedtls_cipher_cmac( cipher_info,
70 test_key, 128,
71 NULL, 16,
72 test_output ) ==
73 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
74
75 TEST_ASSERT( mbedtls_cipher_cmac( cipher_info,
76 test_key, 128,
77 test_data, 16,
78 NULL ) ==
79 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
80
81 TEST_ASSERT( mbedtls_aes_cmac_prf_128( NULL, 16,
82 test_data, 16,
83 test_output ) ==
84 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
85
86 TEST_ASSERT( mbedtls_aes_cmac_prf_128( test_key, 16,
87 NULL, 16,
88 test_output ) ==
89 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
90
91 TEST_ASSERT( mbedtls_aes_cmac_prf_128( test_key, 16,
92 test_data, 16,
93 NULL ) ==
94 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
95
96}
97/* END_CASE */
98
99/* BEGIN_CASE */
Simon Butcherd812fa62016-10-05 14:13:31 +0100100void mbedtls_cmac_setkey( int cipher_type, int key_size,
101 int result )
102{
103 const mbedtls_cipher_info_t *cipher_info;
104 unsigned char key[32];
105 unsigned char buf[16];
106 unsigned char tmp[16];
107
108 memset( key, 0x2A, sizeof( key ) );
109 TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
110
111 TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
112 != NULL );
113
Simon Butcher33183fd2016-10-10 21:41:03 +0100114 TEST_ASSERT( ( result == mbedtls_cipher_cmac( cipher_info, key, key_size,
115 buf, 16, tmp ) ) != 0 );
116}
117/* END_CASE */
118
119/* BEGIN_CASE */
120void mbedtls_cmac_multiple_blocks( int cipher_type,
121 char *key_string, int keybits,
122 int block_size,
123 char *block1_string, int block1_len,
124 char *block2_string, int block2_len,
125 char *block3_string, int block3_len,
126 char *block4_string, int block4_len,
127 char *expected_result_string )
128{
129 unsigned char key[100];
130 unsigned char block1[100];
131 unsigned char block2[100];
132 unsigned char block3[100];
133 unsigned char block4[100];
134 unsigned char expected_result[100];
135 const mbedtls_cipher_info_t *cipher_info;
136 mbedtls_cipher_context_t ctx;
137 unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
138
139 /* Convert the test parameters to binary data */
140 unhexify( key, key_string );
141 unhexify( block1, block1_string );
142 unhexify( block2, block2_string );
143 unhexify( block3, block3_string );
144 unhexify( block4, block4_string );
145 unhexify( expected_result, expected_result_string );
146
147 /* Validate the test inputs */
148 TEST_ASSERT( block1_len <= 100 );
149 TEST_ASSERT( block2_len <= 100 );
150 TEST_ASSERT( block3_len <= 100 );
151 TEST_ASSERT( block4_len <= 100 );
152
153 /* Set up */
154 TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
155 != NULL );
156
157 mbedtls_cipher_init( &ctx );
158
159 TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
160
161 TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
162 (const unsigned char*)key,
163 keybits ) == 0 );
164
165 /* Multiple partial and complete blocks. A negative length means skip the
166 * update operation */
167 if( block1_len >= 0)
168 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
169 (unsigned char*)block1,
170 block1_len ) == 0);
171
172 if( block2_len >= 0 )
173 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
174 (unsigned char*)block2,
175 block2_len ) == 0);
176
177 if( block3_len >= 0 )
178 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
179 (unsigned char*)block3,
180 block3_len ) == 0);
181
182 if( block4_len >= 0 )
183 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
184 (unsigned char*)block4,
185 block4_len ) == 0);
186
187 TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
188
189 TEST_ASSERT( memcmp( output, expected_result, block_size ) == 0 );
190
191 mbedtls_cipher_free( &ctx );
192}
193/* END_CASE */
194
195/* BEGIN_CASE */
196void mbedtls_cmac_multiple_operations_same_key( int cipher_type,
197 char *key_string, int keybits,
198 int block_size,
199 char *block_a1_string, int block_a1_len,
200 char *block_a2_string, int block_a2_len,
201 char *block_a3_string, int block_a3_len,
202 char *expected_result_a_string,
203 char *block_b1_string, int block_b1_len,
204 char *block_b2_string, int block_b2_len,
205 char *block_b3_string, int block_b3_len,
206 char *expected_result_b_string )
207{
208 unsigned char key[100];
209 unsigned char block_a1[100];
210 unsigned char block_a2[100];
211 unsigned char block_a3[100];
212 unsigned char block_b1[100];
213 unsigned char block_b2[100];
214 unsigned char block_b3[100];
215 unsigned char expected_result_a[100], expected_result_b[100];
216 const mbedtls_cipher_info_t *cipher_info;
217 mbedtls_cipher_context_t ctx;
218 unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
219
220 /* Convert the test parameters to binary data */
221 unhexify( key, key_string );
222 unhexify( block_a1, block_a1_string );
223 unhexify( block_a2, block_a2_string );
224 unhexify( block_a3, block_a3_string );
225
226 unhexify( block_b1, block_b1_string );
227 unhexify( block_b2, block_b2_string );
228 unhexify( block_b3, block_b3_string );
229
230 unhexify( expected_result_a, expected_result_a_string );
231 unhexify( expected_result_b, expected_result_b_string );
232
233 /* Validate the test inputs */
234 TEST_ASSERT( block_a1_len <= 100 );
235 TEST_ASSERT( block_a2_len <= 100 );
236 TEST_ASSERT( block_a3_len <= 100 );
237
238 TEST_ASSERT( block_b1_len <= 100 );
239 TEST_ASSERT( block_b2_len <= 100 );
240 TEST_ASSERT( block_b3_len <= 100 );
241
242 /* Set up */
243 TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
244 != NULL );
245
246 mbedtls_cipher_init( &ctx );
247
248 TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
249
250 TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
251 (const unsigned char*)key,
252 keybits ) == 0 );
253
254 /* Sequence A */
255
256 /* Multiple partial and complete blocks. A negative length means skip the
257 * update operation */
258 if( block_a1_len >= 0)
259 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
260 (unsigned char*)block_a1,
261 block_a1_len ) == 0);
262
263 if( block_a2_len >= 0 )
264 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
265 (unsigned char*)block_a2,
266 block_a2_len ) == 0);
267
268 if( block_a3_len >= 0 )
269 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
270 (unsigned char*)block_a3,
271 block_a3_len ) == 0);
272
273 TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
274
275 TEST_ASSERT( memcmp( output, expected_result_a, block_size ) == 0 );
276
277 TEST_ASSERT( mbedtls_cipher_cmac_reset( &ctx ) == 0 );
278
279 /* Sequence B */
280
281 /* Multiple partial and complete blocks. A negative length means skip the
282 * update operation */
283 if( block_b1_len >= 0)
284 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
285 (unsigned char*)block_b1,
286 block_b1_len ) == 0);
287
288 if( block_b2_len >= 0 )
289 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
290 (unsigned char*)block_b2,
291 block_b2_len ) == 0);
292
293 if( block_b3_len >= 0 )
294 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
295 (unsigned char*)block_b3,
296 block_b3_len ) == 0);
297
298 TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
299
300 TEST_ASSERT( memcmp( output, expected_result_b, block_size ) == 0 );
301
302
303 mbedtls_cipher_free( &ctx );
Simon Butcherd812fa62016-10-05 14:13:31 +0100304}
305/* END_CASE */
306