blob: 7bae762e95b1501d260fe3ecba518221261f2b09 [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 */
Azim Khanf1aaec92017-05-30 14:23:15 +010012void mbedtls_cmac_self_test( )
Simon Butcherd812fa62016-10-05 14:13:31 +010013{
14 TEST_ASSERT( mbedtls_cmac_self_test( 1 ) == 0 );
15}
16/* END_CASE */
17
18/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010019void mbedtls_cmac_null_args( )
Simon Butcher33183fd2016-10-10 21:41:03 +010020{
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,
Simon Butcherbd8d2212016-10-11 12:05:51 +010082 test_data, 16,
83 test_output ) ==
84 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Simon Butcher33183fd2016-10-10 21:41:03 +010085
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
Andres AG8abc6b82016-10-11 15:41:40 +010096exit:
97 mbedtls_cipher_free( &ctx );
Simon Butcher33183fd2016-10-10 21:41:03 +010098}
99/* END_CASE */
100
101/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100102void mbedtls_cmac_setkey( int cipher_type, int key_size, int result )
Simon Butcherd812fa62016-10-05 14:13:31 +0100103{
104 const mbedtls_cipher_info_t *cipher_info;
105 unsigned char key[32];
106 unsigned char buf[16];
107 unsigned char tmp[16];
108
109 memset( key, 0x2A, sizeof( key ) );
110 TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
111
112 TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
113 != NULL );
114
Janos Follathd4443582016-10-12 10:00:42 +0100115 memset( buf, 0x2A, sizeof( buf ) );
Simon Butcher33183fd2016-10-10 21:41:03 +0100116 TEST_ASSERT( ( result == mbedtls_cipher_cmac( cipher_info, key, key_size,
117 buf, 16, tmp ) ) != 0 );
118}
119/* END_CASE */
120
121/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100122void mbedtls_cmac_multiple_blocks( int cipher_type, uint8_t * key,
123 uint32_t key_len, int keybits,
124 int block_size, uint8_t * block1,
125 uint32_t block1_len, int block1_len,
126 uint8_t * block2, uint32_t block2_len,
127 int block2_len, uint8_t * block3,
128 uint32_t block3_len, int block3_len,
129 uint8_t * block4, uint32_t block4_len,
130 int block4_len, uint8_t * expected_result,
131 uint32_t expected_result_len )
Simon Butcher33183fd2016-10-10 21:41:03 +0100132{
Simon Butcher33183fd2016-10-10 21:41:03 +0100133 const mbedtls_cipher_info_t *cipher_info;
134 mbedtls_cipher_context_t ctx;
135 unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
136
137 /* Convert the test parameters to binary data */
Simon Butcher33183fd2016-10-10 21:41:03 +0100138
Andres AG8abc6b82016-10-11 15:41:40 +0100139 mbedtls_cipher_init( &ctx );
140
Simon Butcher33183fd2016-10-10 21:41:03 +0100141 /* Validate the test inputs */
142 TEST_ASSERT( block1_len <= 100 );
143 TEST_ASSERT( block2_len <= 100 );
144 TEST_ASSERT( block3_len <= 100 );
145 TEST_ASSERT( block4_len <= 100 );
146
147 /* Set up */
148 TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
149 != NULL );
150
Simon Butcher33183fd2016-10-10 21:41:03 +0100151 TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
152
153 TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
154 (const unsigned char*)key,
155 keybits ) == 0 );
156
157 /* Multiple partial and complete blocks. A negative length means skip the
158 * update operation */
159 if( block1_len >= 0)
160 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
161 (unsigned char*)block1,
162 block1_len ) == 0);
163
164 if( block2_len >= 0 )
165 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
166 (unsigned char*)block2,
167 block2_len ) == 0);
168
169 if( block3_len >= 0 )
170 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
171 (unsigned char*)block3,
172 block3_len ) == 0);
173
174 if( block4_len >= 0 )
175 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
176 (unsigned char*)block4,
177 block4_len ) == 0);
178
179 TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
180
181 TEST_ASSERT( memcmp( output, expected_result, block_size ) == 0 );
182
Simon Butcherbd8d2212016-10-11 12:05:51 +0100183exit:
Simon Butcher33183fd2016-10-10 21:41:03 +0100184 mbedtls_cipher_free( &ctx );
185}
186/* END_CASE */
187
188/* BEGIN_CASE */
189void mbedtls_cmac_multiple_operations_same_key( int cipher_type,
Azim Khanf1aaec92017-05-30 14:23:15 +0100190 uint8_t * key,
191 uint32_t key_len, int keybits,
192 int block_size,
193 uint8_t * block_a1,
194 uint32_t block_a1_len,
195 int block_a1_len,
196 uint8_t * block_a2,
197 uint32_t block_a2_len,
198 int block_a2_len,
199 uint8_t * block_a3,
200 uint32_t block_a3_len,
201 int block_a3_len,
202 uint8_t * expected_result_a,
203 uint32_t expected_result_a_len,
204 uint8_t * block_b1,
205 uint32_t block_b1_len,
206 int block_b1_len,
207 uint8_t * block_b2,
208 uint32_t block_b2_len,
209 int block_b2_len,
210 uint8_t * block_b3,
211 uint32_t block_b3_len,
212 int block_b3_len,
213 uint8_t * expected_result_b,
214 uint32_t expected_result_b_len
215 )
Simon Butcher33183fd2016-10-10 21:41:03 +0100216{
Simon Butcher33183fd2016-10-10 21:41:03 +0100217 const mbedtls_cipher_info_t *cipher_info;
218 mbedtls_cipher_context_t ctx;
219 unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
220
221 /* Convert the test parameters to binary data */
Simon Butcher33183fd2016-10-10 21:41:03 +0100222
Simon Butcher33183fd2016-10-10 21:41:03 +0100223
Simon Butcher33183fd2016-10-10 21:41:03 +0100224
Andres AG8abc6b82016-10-11 15:41:40 +0100225 mbedtls_cipher_init( &ctx );
226
Simon Butcher33183fd2016-10-10 21:41:03 +0100227 /* Validate the test inputs */
228 TEST_ASSERT( block_a1_len <= 100 );
229 TEST_ASSERT( block_a2_len <= 100 );
230 TEST_ASSERT( block_a3_len <= 100 );
231
232 TEST_ASSERT( block_b1_len <= 100 );
233 TEST_ASSERT( block_b2_len <= 100 );
234 TEST_ASSERT( block_b3_len <= 100 );
235
236 /* Set up */
237 TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
238 != NULL );
239
Simon Butcher33183fd2016-10-10 21:41:03 +0100240 TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
241
242 TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
243 (const unsigned char*)key,
244 keybits ) == 0 );
245
246 /* Sequence A */
247
248 /* Multiple partial and complete blocks. A negative length means skip the
249 * update operation */
Simon Butcherbd8d2212016-10-11 12:05:51 +0100250 if( block_a1_len >= 0 )
Simon Butcher33183fd2016-10-10 21:41:03 +0100251 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
252 (unsigned char*)block_a1,
253 block_a1_len ) == 0);
254
255 if( block_a2_len >= 0 )
256 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
257 (unsigned char*)block_a2,
258 block_a2_len ) == 0);
259
260 if( block_a3_len >= 0 )
261 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
262 (unsigned char*)block_a3,
263 block_a3_len ) == 0);
264
265 TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
266
267 TEST_ASSERT( memcmp( output, expected_result_a, block_size ) == 0 );
268
269 TEST_ASSERT( mbedtls_cipher_cmac_reset( &ctx ) == 0 );
270
271 /* Sequence B */
272
273 /* Multiple partial and complete blocks. A negative length means skip the
274 * update operation */
275 if( block_b1_len >= 0)
276 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
277 (unsigned char*)block_b1,
278 block_b1_len ) == 0);
279
280 if( block_b2_len >= 0 )
281 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
282 (unsigned char*)block_b2,
283 block_b2_len ) == 0);
284
285 if( block_b3_len >= 0 )
286 TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
287 (unsigned char*)block_b3,
288 block_b3_len ) == 0);
289
290 TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
291
292 TEST_ASSERT( memcmp( output, expected_result_b, block_size ) == 0 );
293
Simon Butcherbd8d2212016-10-11 12:05:51 +0100294exit:
Simon Butcher33183fd2016-10-10 21:41:03 +0100295 mbedtls_cipher_free( &ctx );
Simon Butcherd812fa62016-10-05 14:13:31 +0100296}
297/* END_CASE */
298