blob: 61b6e03c341a2763263692668781674b45adf216 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/md.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker17373852011-01-06 14:20:01 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
6 * depends_on:POLARSSL_MD_C
7 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020011void md_process( )
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010012{
13 const int *md_type_ptr;
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020014 const md_info_t *info;
15 md_context_t ctx;
16 unsigned char buf[150];
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010017
Paul Bakkerd2a2d612014-07-01 15:45:49 +020018 md_init( &ctx );
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020019
20 /*
21 * Very minimal testing of md_process, just make sure the various
22 * xxx_process_wrap() function pointers are valid. (Testing that they
23 * indeed do the right thing whould require messing with the internal
24 * state of the underlying md/sha context.)
25 *
26 * Also tests that md_list() only returns valid MDs.
27 */
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010028 for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ )
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020029 {
Paul Bakker94b916c2014-04-17 16:07:20 +020030 info = md_info_from_type( *md_type_ptr );
31 TEST_ASSERT( info != NULL );
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +010032 TEST_ASSERT( md_setup( &ctx, info, 0 ) == 0 );
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020033 TEST_ASSERT( md_process( &ctx, buf ) == 0 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +020034 md_free( &ctx );
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020035 }
Paul Bakkerbd51b262014-07-10 15:26:12 +020036
37exit:
38 md_free( &ctx );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010039}
40/* END_CASE */
41
42/* BEGIN_CASE */
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020043void md_null_args( )
44{
45 md_context_t ctx;
46 const md_info_t *info = md_info_from_type( *( md_list() ) );
47 unsigned char buf[1] = { 0 };
48
Paul Bakkerd2a2d612014-07-01 15:45:49 +020049 md_init( &ctx );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020050
51 TEST_ASSERT( md_get_size( NULL ) == 0 );
52
53 TEST_ASSERT( md_get_type( NULL ) == POLARSSL_MD_NONE );
54
55 TEST_ASSERT( md_info_from_string( NULL ) == NULL );
56
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +010057 TEST_ASSERT( md_setup( &ctx, NULL, 0 ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
58 TEST_ASSERT( md_setup( NULL, info, 0 ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020059
60 TEST_ASSERT( md_starts( NULL ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
61 TEST_ASSERT( md_starts( &ctx ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
62
63 TEST_ASSERT( md_update( NULL, buf, 1 ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
64 TEST_ASSERT( md_update( &ctx, buf, 1 ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
65
66 TEST_ASSERT( md_finish( NULL, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
67 TEST_ASSERT( md_finish( &ctx, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
68
69 TEST_ASSERT( md( NULL, buf, 1, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
70
71 TEST_ASSERT( md_file( NULL, "", buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
72
73 TEST_ASSERT( md_hmac_starts( NULL, buf, 1 )
74 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
75 TEST_ASSERT( md_hmac_starts( &ctx, buf, 1 )
76 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
77
78 TEST_ASSERT( md_hmac_update( NULL, buf, 1 )
79 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
80 TEST_ASSERT( md_hmac_update( &ctx, buf, 1 )
81 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
82
83 TEST_ASSERT( md_hmac_finish( NULL, buf )
84 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
85 TEST_ASSERT( md_hmac_finish( &ctx, buf )
86 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
87
88 TEST_ASSERT( md_hmac_reset( NULL ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
89 TEST_ASSERT( md_hmac_reset( &ctx ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
90
91 TEST_ASSERT( md_hmac( NULL, buf, 1, buf, 1, buf )
92 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
93
94 TEST_ASSERT( md_process( NULL, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
95 TEST_ASSERT( md_process( &ctx, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
96}
97/* END_CASE */
98
99/* BEGIN_CASE */
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100100void md_info( int md_type, char *md_name, int md_size )
101{
102 const md_info_t *md_info;
103 const int *md_type_ptr;
104 int found;
105
Paul Bakker94b916c2014-04-17 16:07:20 +0200106 md_info = md_info_from_type( md_type );
107 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100108 TEST_ASSERT( md_info == md_info_from_string( md_name ) );
109
110 TEST_ASSERT( md_get_type( md_info ) == (md_type_t) md_type );
111 TEST_ASSERT( md_get_size( md_info ) == (unsigned char) md_size );
112
113 found = 0;
114 for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ )
115 if( *md_type_ptr == md_type )
116 found = 1;
117 TEST_ASSERT( found == 1 );
118}
119/* END_CASE */
120
121/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200122void md_text( char *text_md_name, char *text_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000123{
124 char md_name[100];
125 unsigned char src_str[1000];
126 unsigned char hash_str[1000];
127 unsigned char output[100];
128 const md_info_t *md_info = NULL;
129
130 memset(md_name, 0x00, 100);
131 memset(src_str, 0x00, 1000);
132 memset(hash_str, 0x00, 1000);
133 memset(output, 0x00, 100);
134
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200135 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
136 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000137 md_info = md_info_from_string(md_name);
138 TEST_ASSERT( md_info != NULL );
139
140 TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) );
141 hexify( hash_str, output, md_get_size(md_info) );
142
Paul Bakker33b43f12013-08-20 11:48:36 +0200143 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000144}
Paul Bakker33b43f12013-08-20 11:48:36 +0200145/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000146
Paul Bakker33b43f12013-08-20 11:48:36 +0200147/* BEGIN_CASE */
148void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000149{
150 char md_name[100];
151 unsigned char src_str[10000];
152 unsigned char hash_str[10000];
153 unsigned char output[100];
154 int src_len;
155 const md_info_t *md_info = NULL;
156
157 memset(md_name, 0x00, 100);
158 memset(src_str, 0x00, 10000);
159 memset(hash_str, 0x00, 10000);
160 memset(output, 0x00, 100);
161
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200162 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000163 md_info = md_info_from_string(md_name);
164 TEST_ASSERT( md_info != NULL );
165
Paul Bakker33b43f12013-08-20 11:48:36 +0200166 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000167 TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) );
168
169 hexify( hash_str, output, md_get_size(md_info) );
170
Paul Bakker33b43f12013-08-20 11:48:36 +0200171 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000172}
Paul Bakker33b43f12013-08-20 11:48:36 +0200173/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000174
Paul Bakker33b43f12013-08-20 11:48:36 +0200175/* BEGIN_CASE */
176void md_text_multi( char *text_md_name, char *text_src_string,
177 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000178{
179 char md_name[100];
180 unsigned char src_str[1000];
181 unsigned char hash_str[1000];
182 unsigned char output[100];
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200183
Paul Bakker17373852011-01-06 14:20:01 +0000184 const md_info_t *md_info = NULL;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200185 md_context_t ctx;
186
187 md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000188
189 memset(md_name, 0x00, 100);
190 memset(src_str, 0x00, 1000);
191 memset(hash_str, 0x00, 1000);
192 memset(output, 0x00, 100);
193
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200194 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
195 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000196 md_info = md_info_from_string(md_name);
197 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100198 TEST_ASSERT ( 0 == md_setup( &ctx, md_info, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000199
Paul Bakker562535d2011-01-20 16:42:01 +0000200 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000201 TEST_ASSERT ( ctx.md_ctx != NULL );
202 TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
203 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200204
Paul Bakker17373852011-01-06 14:20:01 +0000205 hexify( hash_str, output, md_get_size(md_info) );
206
Paul Bakker33b43f12013-08-20 11:48:36 +0200207 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200208
209exit:
210 md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000211}
Paul Bakker33b43f12013-08-20 11:48:36 +0200212/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000213
Paul Bakker33b43f12013-08-20 11:48:36 +0200214/* BEGIN_CASE */
215void md_hex_multi( char *text_md_name, char *hex_src_string,
216 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000217{
218 char md_name[100];
219 unsigned char src_str[10000];
220 unsigned char hash_str[10000];
221 unsigned char output[100];
222 int src_len;
223 const md_info_t *md_info = NULL;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200224 md_context_t ctx;
225
226 md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000227
228 memset(md_name, 0x00, 100);
229 memset(src_str, 0x00, 10000);
230 memset(hash_str, 0x00, 10000);
231 memset(output, 0x00, 100);
232
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200233 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000234 md_info = md_info_from_string(md_name);
235 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100236 TEST_ASSERT ( 0 == md_setup( &ctx, md_info, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000237
Paul Bakker33b43f12013-08-20 11:48:36 +0200238 src_len = unhexify( src_str, hex_src_string );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200239
Paul Bakker562535d2011-01-20 16:42:01 +0000240 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000241 TEST_ASSERT ( ctx.md_ctx != NULL );
242 TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
243 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200244
Paul Bakker17373852011-01-06 14:20:01 +0000245 hexify( hash_str, output, md_get_size(md_info) );
246
Paul Bakker33b43f12013-08-20 11:48:36 +0200247 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200248
249exit:
250 md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000251}
Paul Bakker33b43f12013-08-20 11:48:36 +0200252/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000253
Paul Bakker33b43f12013-08-20 11:48:36 +0200254/* BEGIN_CASE */
255void md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
256 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000257{
258 char md_name[100];
259 unsigned char src_str[10000];
260 unsigned char key_str[10000];
261 unsigned char hash_str[10000];
262 unsigned char output[100];
263 int key_len, src_len;
264 const md_info_t *md_info = NULL;
265
266 memset(md_name, 0x00, 100);
267 memset(src_str, 0x00, 10000);
268 memset(key_str, 0x00, 10000);
269 memset(hash_str, 0x00, 10000);
270 memset(output, 0x00, 100);
271
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200272 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000273 md_info = md_info_from_string( md_name );
274 TEST_ASSERT( md_info != NULL );
275
Paul Bakker33b43f12013-08-20 11:48:36 +0200276 key_len = unhexify( key_str, hex_key_string );
277 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000278
279 TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
280 hexify( hash_str, output, md_get_size(md_info) );
281
Paul Bakker33b43f12013-08-20 11:48:36 +0200282 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000283}
Paul Bakker33b43f12013-08-20 11:48:36 +0200284/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000285
Paul Bakker33b43f12013-08-20 11:48:36 +0200286/* BEGIN_CASE */
287void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string,
288 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000289{
290 char md_name[100];
291 unsigned char src_str[10000];
292 unsigned char key_str[10000];
293 unsigned char hash_str[10000];
294 unsigned char output[100];
295 int key_len, src_len;
296 const md_info_t *md_info = NULL;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200297 md_context_t ctx;
298
299 md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000300
301 memset(md_name, 0x00, 100);
302 memset(src_str, 0x00, 10000);
303 memset(key_str, 0x00, 10000);
304 memset(hash_str, 0x00, 10000);
305 memset(output, 0x00, 100);
306
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200307 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000308 md_info = md_info_from_string( md_name );
309 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100310 TEST_ASSERT ( 0 == md_setup( &ctx, md_info, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000311
Paul Bakker33b43f12013-08-20 11:48:36 +0200312 key_len = unhexify( key_str, hex_key_string );
313 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000314
Paul Bakker562535d2011-01-20 16:42:01 +0000315 TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000316 TEST_ASSERT ( ctx.md_ctx != NULL );
317 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
318 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100319
320 hexify( hash_str, output, md_get_size(md_info) );
321 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
322
323 /* Test again, for reset() */
324 memset(hash_str, 0x00, 10000);
325 memset(output, 0x00, 100);
326
327 TEST_ASSERT ( 0 == md_hmac_reset( &ctx ) );
328 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
329 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200330
Paul Bakker17373852011-01-06 14:20:01 +0000331 hexify( hash_str, output, md_get_size(md_info) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200332 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200333
334exit:
335 md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000336}
Paul Bakker33b43f12013-08-20 11:48:36 +0200337/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200338
339/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200340void md_file( char *text_md_name, char *filename, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000341{
342 char md_name[100];
343 unsigned char hash_str[1000];
344 unsigned char output[100];
345 const md_info_t *md_info = NULL;
346
347 memset(md_name, 0x00, 100);
348 memset(hash_str, 0x00, 1000);
349 memset(output, 0x00, 100);
350
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200351 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000352 md_info = md_info_from_string( md_name );
353 TEST_ASSERT( md_info != NULL );
354
Paul Bakker33b43f12013-08-20 11:48:36 +0200355 md_file( md_info, filename, output);
Paul Bakker17373852011-01-06 14:20:01 +0000356 hexify( hash_str, output, md_get_size(md_info) );
357
Paul Bakker33b43f12013-08-20 11:48:36 +0200358 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000359}
Paul Bakker33b43f12013-08-20 11:48:36 +0200360/* END_CASE */