blob: fc17ba92bce92b81d281f0aa90dda4ab0d153913 [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 );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020052 TEST_ASSERT( md_get_type( NULL ) == POLARSSL_MD_NONE );
Manuel Pégourié-Gonnard19d644b2015-03-26 12:42:35 +010053 TEST_ASSERT( md_get_name( NULL ) == NULL );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020054
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 );
Manuel Pégourié-Gonnard19d644b2015-03-26 12:42:35 +010096
97 /* Ok, this is not NULL arg but NULL return... */
98 TEST_ASSERT( md_info_from_type( POLARSSL_MD_NONE ) == NULL );
99 TEST_ASSERT( md_info_from_string( "no such md" ) == NULL );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +0200100}
101/* END_CASE */
102
103/* BEGIN_CASE */
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100104void md_info( int md_type, char *md_name, int md_size )
105{
106 const md_info_t *md_info;
107 const int *md_type_ptr;
108 int found;
109
Paul Bakker94b916c2014-04-17 16:07:20 +0200110 md_info = md_info_from_type( md_type );
111 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100112 TEST_ASSERT( md_info == md_info_from_string( md_name ) );
113
114 TEST_ASSERT( md_get_type( md_info ) == (md_type_t) md_type );
115 TEST_ASSERT( md_get_size( md_info ) == (unsigned char) md_size );
Manuel Pégourié-Gonnard19d644b2015-03-26 12:42:35 +0100116 TEST_ASSERT( strcmp( md_get_name( md_info ), md_name ) == 0 );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100117
118 found = 0;
119 for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ )
120 if( *md_type_ptr == md_type )
121 found = 1;
122 TEST_ASSERT( found == 1 );
123}
124/* END_CASE */
125
126/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200127void md_text( char *text_md_name, char *text_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000128{
129 char md_name[100];
130 unsigned char src_str[1000];
131 unsigned char hash_str[1000];
132 unsigned char output[100];
133 const md_info_t *md_info = NULL;
134
135 memset(md_name, 0x00, 100);
136 memset(src_str, 0x00, 1000);
137 memset(hash_str, 0x00, 1000);
138 memset(output, 0x00, 100);
139
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200140 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
141 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000142 md_info = md_info_from_string(md_name);
143 TEST_ASSERT( md_info != NULL );
144
145 TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) );
146 hexify( hash_str, output, md_get_size(md_info) );
147
Paul Bakker33b43f12013-08-20 11:48:36 +0200148 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000149}
Paul Bakker33b43f12013-08-20 11:48:36 +0200150/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000151
Paul Bakker33b43f12013-08-20 11:48:36 +0200152/* BEGIN_CASE */
153void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000154{
155 char md_name[100];
156 unsigned char src_str[10000];
157 unsigned char hash_str[10000];
158 unsigned char output[100];
159 int src_len;
160 const md_info_t *md_info = NULL;
161
162 memset(md_name, 0x00, 100);
163 memset(src_str, 0x00, 10000);
164 memset(hash_str, 0x00, 10000);
165 memset(output, 0x00, 100);
166
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200167 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000168 md_info = md_info_from_string(md_name);
169 TEST_ASSERT( md_info != NULL );
170
Paul Bakker33b43f12013-08-20 11:48:36 +0200171 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000172 TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) );
173
174 hexify( hash_str, output, md_get_size(md_info) );
175
Paul Bakker33b43f12013-08-20 11:48:36 +0200176 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000177}
Paul Bakker33b43f12013-08-20 11:48:36 +0200178/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000179
Paul Bakker33b43f12013-08-20 11:48:36 +0200180/* BEGIN_CASE */
181void md_text_multi( char *text_md_name, char *text_src_string,
182 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000183{
184 char md_name[100];
185 unsigned char src_str[1000];
186 unsigned char hash_str[1000];
187 unsigned char output[100];
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200188
Paul Bakker17373852011-01-06 14:20:01 +0000189 const md_info_t *md_info = NULL;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200190 md_context_t ctx;
191
192 md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000193
194 memset(md_name, 0x00, 100);
195 memset(src_str, 0x00, 1000);
196 memset(hash_str, 0x00, 1000);
197 memset(output, 0x00, 100);
198
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200199 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
200 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000201 md_info = md_info_from_string(md_name);
202 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100203 TEST_ASSERT ( 0 == md_setup( &ctx, md_info, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000204
Paul Bakker562535d2011-01-20 16:42:01 +0000205 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000206 TEST_ASSERT ( ctx.md_ctx != NULL );
207 TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
208 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200209
Paul Bakker17373852011-01-06 14:20:01 +0000210 hexify( hash_str, output, md_get_size(md_info) );
211
Paul Bakker33b43f12013-08-20 11:48:36 +0200212 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200213
214exit:
215 md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000216}
Paul Bakker33b43f12013-08-20 11:48:36 +0200217/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000218
Paul Bakker33b43f12013-08-20 11:48:36 +0200219/* BEGIN_CASE */
220void md_hex_multi( char *text_md_name, char *hex_src_string,
221 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000222{
223 char md_name[100];
224 unsigned char src_str[10000];
225 unsigned char hash_str[10000];
226 unsigned char output[100];
227 int src_len;
228 const md_info_t *md_info = NULL;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200229 md_context_t ctx;
230
231 md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000232
233 memset(md_name, 0x00, 100);
234 memset(src_str, 0x00, 10000);
235 memset(hash_str, 0x00, 10000);
236 memset(output, 0x00, 100);
237
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200238 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000239 md_info = md_info_from_string(md_name);
240 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100241 TEST_ASSERT ( 0 == md_setup( &ctx, md_info, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000242
Paul Bakker33b43f12013-08-20 11:48:36 +0200243 src_len = unhexify( src_str, hex_src_string );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200244
Paul Bakker562535d2011-01-20 16:42:01 +0000245 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000246 TEST_ASSERT ( ctx.md_ctx != NULL );
247 TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
248 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200249
Paul Bakker17373852011-01-06 14:20:01 +0000250 hexify( hash_str, output, md_get_size(md_info) );
251
Paul Bakker33b43f12013-08-20 11:48:36 +0200252 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200253
254exit:
255 md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000256}
Paul Bakker33b43f12013-08-20 11:48:36 +0200257/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000258
Paul Bakker33b43f12013-08-20 11:48:36 +0200259/* BEGIN_CASE */
260void md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
261 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000262{
263 char md_name[100];
264 unsigned char src_str[10000];
265 unsigned char key_str[10000];
266 unsigned char hash_str[10000];
267 unsigned char output[100];
268 int key_len, src_len;
269 const md_info_t *md_info = NULL;
270
271 memset(md_name, 0x00, 100);
272 memset(src_str, 0x00, 10000);
273 memset(key_str, 0x00, 10000);
274 memset(hash_str, 0x00, 10000);
275 memset(output, 0x00, 100);
276
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200277 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000278 md_info = md_info_from_string( md_name );
279 TEST_ASSERT( md_info != NULL );
280
Paul Bakker33b43f12013-08-20 11:48:36 +0200281 key_len = unhexify( key_str, hex_key_string );
282 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000283
284 TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
285 hexify( hash_str, output, md_get_size(md_info) );
286
Paul Bakker33b43f12013-08-20 11:48:36 +0200287 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000288}
Paul Bakker33b43f12013-08-20 11:48:36 +0200289/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000290
Paul Bakker33b43f12013-08-20 11:48:36 +0200291/* BEGIN_CASE */
292void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string,
293 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000294{
295 char md_name[100];
296 unsigned char src_str[10000];
297 unsigned char key_str[10000];
298 unsigned char hash_str[10000];
299 unsigned char output[100];
300 int key_len, src_len;
301 const md_info_t *md_info = NULL;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200302 md_context_t ctx;
303
304 md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000305
306 memset(md_name, 0x00, 100);
307 memset(src_str, 0x00, 10000);
308 memset(key_str, 0x00, 10000);
309 memset(hash_str, 0x00, 10000);
310 memset(output, 0x00, 100);
311
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200312 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000313 md_info = md_info_from_string( md_name );
314 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100315 TEST_ASSERT ( 0 == md_setup( &ctx, md_info, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000316
Paul Bakker33b43f12013-08-20 11:48:36 +0200317 key_len = unhexify( key_str, hex_key_string );
318 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000319
Paul Bakker562535d2011-01-20 16:42:01 +0000320 TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000321 TEST_ASSERT ( ctx.md_ctx != NULL );
322 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
323 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100324
325 hexify( hash_str, output, md_get_size(md_info) );
326 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
327
328 /* Test again, for reset() */
329 memset(hash_str, 0x00, 10000);
330 memset(output, 0x00, 100);
331
332 TEST_ASSERT ( 0 == md_hmac_reset( &ctx ) );
333 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
334 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200335
Paul Bakker17373852011-01-06 14:20:01 +0000336 hexify( hash_str, output, md_get_size(md_info) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200337 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200338
339exit:
340 md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000341}
Paul Bakker33b43f12013-08-20 11:48:36 +0200342/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200343
344/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200345void md_file( char *text_md_name, char *filename, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000346{
347 char md_name[100];
348 unsigned char hash_str[1000];
349 unsigned char output[100];
350 const md_info_t *md_info = NULL;
351
352 memset(md_name, 0x00, 100);
353 memset(hash_str, 0x00, 1000);
354 memset(output, 0x00, 100);
355
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200356 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000357 md_info = md_info_from_string( md_name );
358 TEST_ASSERT( md_info != NULL );
359
Paul Bakker33b43f12013-08-20 11:48:36 +0200360 md_file( md_info, filename, output);
Paul Bakker17373852011-01-06 14:20:01 +0000361 hexify( hash_str, output, md_get_size(md_info) );
362
Paul Bakker33b43f12013-08-20 11:48:36 +0200363 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000364}
Paul Bakker33b43f12013-08-20 11:48:36 +0200365/* END_CASE */