blob: 9f064434c334d60f9dbaadd0345c8554d0db4781 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker17373852011-01-06 14:20:01 +00002#include <polarssl/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
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020018 memset( &ctx, 0, sizeof ctx );
19
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é-Gonnardedb242f2014-04-02 17:52:04 +020032 TEST_ASSERT( md_init_ctx( &ctx, info ) == 0 );
33 TEST_ASSERT( md_process( &ctx, buf ) == 0 );
34 TEST_ASSERT( md_free_ctx( &ctx ) == 0 );
35 }
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010036}
37/* END_CASE */
38
39/* BEGIN_CASE */
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020040void md_null_args( )
41{
42 md_context_t ctx;
43 const md_info_t *info = md_info_from_type( *( md_list() ) );
44 unsigned char buf[1] = { 0 };
45
46 memset( &ctx, 0, sizeof( md_context_t ) );
47
48 TEST_ASSERT( md_get_size( NULL ) == 0 );
49
50 TEST_ASSERT( md_get_type( NULL ) == POLARSSL_MD_NONE );
51
52 TEST_ASSERT( md_info_from_string( NULL ) == NULL );
53
54 TEST_ASSERT( md_init_ctx( &ctx, NULL ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
55 TEST_ASSERT( md_init_ctx( NULL, info ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
56
57 TEST_ASSERT( md_starts( NULL ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
58 TEST_ASSERT( md_starts( &ctx ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
59
60 TEST_ASSERT( md_update( NULL, buf, 1 ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
61 TEST_ASSERT( md_update( &ctx, buf, 1 ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
62
63 TEST_ASSERT( md_finish( NULL, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
64 TEST_ASSERT( md_finish( &ctx, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
65
66 TEST_ASSERT( md( NULL, buf, 1, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
67
68 TEST_ASSERT( md_file( NULL, "", buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
69
70 TEST_ASSERT( md_hmac_starts( NULL, buf, 1 )
71 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
72 TEST_ASSERT( md_hmac_starts( &ctx, buf, 1 )
73 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
74
75 TEST_ASSERT( md_hmac_update( NULL, buf, 1 )
76 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
77 TEST_ASSERT( md_hmac_update( &ctx, buf, 1 )
78 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
79
80 TEST_ASSERT( md_hmac_finish( NULL, buf )
81 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
82 TEST_ASSERT( md_hmac_finish( &ctx, buf )
83 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
84
85 TEST_ASSERT( md_hmac_reset( NULL ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
86 TEST_ASSERT( md_hmac_reset( &ctx ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
87
88 TEST_ASSERT( md_hmac( NULL, buf, 1, buf, 1, buf )
89 == POLARSSL_ERR_MD_BAD_INPUT_DATA );
90
91 TEST_ASSERT( md_process( NULL, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
92 TEST_ASSERT( md_process( &ctx, buf ) == POLARSSL_ERR_MD_BAD_INPUT_DATA );
93}
94/* END_CASE */
95
96/* BEGIN_CASE */
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010097void md_info( int md_type, char *md_name, int md_size )
98{
99 const md_info_t *md_info;
100 const int *md_type_ptr;
101 int found;
102
Paul Bakker94b916c2014-04-17 16:07:20 +0200103 md_info = md_info_from_type( md_type );
104 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100105 TEST_ASSERT( md_info == md_info_from_string( md_name ) );
106
107 TEST_ASSERT( md_get_type( md_info ) == (md_type_t) md_type );
108 TEST_ASSERT( md_get_size( md_info ) == (unsigned char) md_size );
109
110 found = 0;
111 for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ )
112 if( *md_type_ptr == md_type )
113 found = 1;
114 TEST_ASSERT( found == 1 );
115}
116/* END_CASE */
117
118/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200119void md_text( char *text_md_name, char *text_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000120{
121 char md_name[100];
122 unsigned char src_str[1000];
123 unsigned char hash_str[1000];
124 unsigned char output[100];
125 const md_info_t *md_info = NULL;
126
127 memset(md_name, 0x00, 100);
128 memset(src_str, 0x00, 1000);
129 memset(hash_str, 0x00, 1000);
130 memset(output, 0x00, 100);
131
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200132 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
133 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000134 md_info = md_info_from_string(md_name);
135 TEST_ASSERT( md_info != NULL );
136
137 TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) );
138 hexify( hash_str, output, md_get_size(md_info) );
139
Paul Bakker33b43f12013-08-20 11:48:36 +0200140 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000141}
Paul Bakker33b43f12013-08-20 11:48:36 +0200142/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000143
Paul Bakker33b43f12013-08-20 11:48:36 +0200144/* BEGIN_CASE */
145void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000146{
147 char md_name[100];
148 unsigned char src_str[10000];
149 unsigned char hash_str[10000];
150 unsigned char output[100];
151 int src_len;
152 const md_info_t *md_info = NULL;
153
154 memset(md_name, 0x00, 100);
155 memset(src_str, 0x00, 10000);
156 memset(hash_str, 0x00, 10000);
157 memset(output, 0x00, 100);
158
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200159 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000160 md_info = md_info_from_string(md_name);
161 TEST_ASSERT( md_info != NULL );
162
Paul Bakker33b43f12013-08-20 11:48:36 +0200163 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000164 TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) );
165
166 hexify( hash_str, output, md_get_size(md_info) );
167
Paul Bakker33b43f12013-08-20 11:48:36 +0200168 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000169}
Paul Bakker33b43f12013-08-20 11:48:36 +0200170/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000171
Paul Bakker33b43f12013-08-20 11:48:36 +0200172/* BEGIN_CASE */
173void md_text_multi( char *text_md_name, char *text_src_string,
174 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000175{
176 char md_name[100];
177 unsigned char src_str[1000];
178 unsigned char hash_str[1000];
179 unsigned char output[100];
180
181 const md_info_t *md_info = NULL;
182 md_context_t ctx = MD_CONTEXT_T_INIT;
183
184 memset(md_name, 0x00, 100);
185 memset(src_str, 0x00, 1000);
186 memset(hash_str, 0x00, 1000);
187 memset(output, 0x00, 100);
188
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200189 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
190 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000191 md_info = md_info_from_string(md_name);
192 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000193 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000194
Paul Bakker562535d2011-01-20 16:42:01 +0000195 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000196 TEST_ASSERT ( ctx.md_ctx != NULL );
197 TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
198 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
199 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
200
201 hexify( hash_str, output, md_get_size(md_info) );
202
Paul Bakker33b43f12013-08-20 11:48:36 +0200203 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000204}
Paul Bakker33b43f12013-08-20 11:48:36 +0200205/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000206
Paul Bakker33b43f12013-08-20 11:48:36 +0200207/* BEGIN_CASE */
208void md_hex_multi( char *text_md_name, char *hex_src_string,
209 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000210{
211 char md_name[100];
212 unsigned char src_str[10000];
213 unsigned char hash_str[10000];
214 unsigned char output[100];
215 int src_len;
216 const md_info_t *md_info = NULL;
217 md_context_t ctx = MD_CONTEXT_T_INIT;
218
219 memset(md_name, 0x00, 100);
220 memset(src_str, 0x00, 10000);
221 memset(hash_str, 0x00, 10000);
222 memset(output, 0x00, 100);
223
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200224 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000225 md_info = md_info_from_string(md_name);
226 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000227 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000228
Paul Bakker33b43f12013-08-20 11:48:36 +0200229 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000230
Paul Bakker562535d2011-01-20 16:42:01 +0000231 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000232 TEST_ASSERT ( ctx.md_ctx != NULL );
233 TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
234 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
235 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
236
237 hexify( hash_str, output, md_get_size(md_info) );
238
Paul Bakker33b43f12013-08-20 11:48:36 +0200239 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000240}
Paul Bakker33b43f12013-08-20 11:48:36 +0200241/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000242
Paul Bakker33b43f12013-08-20 11:48:36 +0200243/* BEGIN_CASE */
244void md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
245 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000246{
247 char md_name[100];
248 unsigned char src_str[10000];
249 unsigned char key_str[10000];
250 unsigned char hash_str[10000];
251 unsigned char output[100];
252 int key_len, src_len;
253 const md_info_t *md_info = NULL;
254
255 memset(md_name, 0x00, 100);
256 memset(src_str, 0x00, 10000);
257 memset(key_str, 0x00, 10000);
258 memset(hash_str, 0x00, 10000);
259 memset(output, 0x00, 100);
260
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200261 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000262 md_info = md_info_from_string( md_name );
263 TEST_ASSERT( md_info != NULL );
264
Paul Bakker33b43f12013-08-20 11:48:36 +0200265 key_len = unhexify( key_str, hex_key_string );
266 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000267
268 TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
269 hexify( hash_str, output, md_get_size(md_info) );
270
Paul Bakker33b43f12013-08-20 11:48:36 +0200271 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000272}
Paul Bakker33b43f12013-08-20 11:48:36 +0200273/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000274
Paul Bakker33b43f12013-08-20 11:48:36 +0200275/* BEGIN_CASE */
276void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string,
277 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000278{
279 char md_name[100];
280 unsigned char src_str[10000];
281 unsigned char key_str[10000];
282 unsigned char hash_str[10000];
283 unsigned char output[100];
284 int key_len, src_len;
285 const md_info_t *md_info = NULL;
286 md_context_t ctx = MD_CONTEXT_T_INIT;
287
288 memset(md_name, 0x00, 100);
289 memset(src_str, 0x00, 10000);
290 memset(key_str, 0x00, 10000);
291 memset(hash_str, 0x00, 10000);
292 memset(output, 0x00, 100);
293
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200294 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000295 md_info = md_info_from_string( md_name );
296 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000297 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000298
Paul Bakker33b43f12013-08-20 11:48:36 +0200299 key_len = unhexify( key_str, hex_key_string );
300 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000301
Paul Bakker562535d2011-01-20 16:42:01 +0000302 TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000303 TEST_ASSERT ( ctx.md_ctx != NULL );
304 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
305 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100306
307 hexify( hash_str, output, md_get_size(md_info) );
308 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
309
310 /* Test again, for reset() */
311 memset(hash_str, 0x00, 10000);
312 memset(output, 0x00, 100);
313
314 TEST_ASSERT ( 0 == md_hmac_reset( &ctx ) );
315 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
316 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000317 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200318
Paul Bakker17373852011-01-06 14:20:01 +0000319 hexify( hash_str, output, md_get_size(md_info) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200320 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000321}
Paul Bakker33b43f12013-08-20 11:48:36 +0200322/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200323
324/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200325void md_file( char *text_md_name, char *filename, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000326{
327 char md_name[100];
328 unsigned char hash_str[1000];
329 unsigned char output[100];
330 const md_info_t *md_info = NULL;
331
332 memset(md_name, 0x00, 100);
333 memset(hash_str, 0x00, 1000);
334 memset(output, 0x00, 100);
335
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200336 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000337 md_info = md_info_from_string( md_name );
338 TEST_ASSERT( md_info != NULL );
339
Paul Bakker33b43f12013-08-20 11:48:36 +0200340 md_file( md_info, filename, output);
Paul Bakker17373852011-01-06 14:20:01 +0000341 hexify( hash_str, output, md_get_size(md_info) );
342
Paul Bakker33b43f12013-08-20 11:48:36 +0200343 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000344}
Paul Bakker33b43f12013-08-20 11:48:36 +0200345/* END_CASE */