blob: 338f8af2f5e0b10cc57cdd00c5b445ec948cbfba [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
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é-Gonnardedb242f2014-04-02 17:52:04 +020032 TEST_ASSERT( md_init_ctx( &ctx, info ) == 0 );
33 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 }
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
Paul Bakkerd2a2d612014-07-01 15:45:49 +020046 md_init( &ctx );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020047
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];
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200180
Paul Bakker17373852011-01-06 14:20:01 +0000181 const md_info_t *md_info = NULL;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200182 md_context_t ctx;
183
184 md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000185
186 memset(md_name, 0x00, 100);
187 memset(src_str, 0x00, 1000);
188 memset(hash_str, 0x00, 1000);
189 memset(output, 0x00, 100);
190
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200191 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
192 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000193 md_info = md_info_from_string(md_name);
194 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000195 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000196
Paul Bakker562535d2011-01-20 16:42:01 +0000197 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000198 TEST_ASSERT ( ctx.md_ctx != NULL );
199 TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
200 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200201 md_free( &ctx );
202
Paul Bakker17373852011-01-06 14:20:01 +0000203 hexify( hash_str, output, md_get_size(md_info) );
204
Paul Bakker33b43f12013-08-20 11:48:36 +0200205 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000206}
Paul Bakker33b43f12013-08-20 11:48:36 +0200207/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000208
Paul Bakker33b43f12013-08-20 11:48:36 +0200209/* BEGIN_CASE */
210void md_hex_multi( char *text_md_name, char *hex_src_string,
211 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000212{
213 char md_name[100];
214 unsigned char src_str[10000];
215 unsigned char hash_str[10000];
216 unsigned char output[100];
217 int src_len;
218 const md_info_t *md_info = NULL;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200219 md_context_t ctx;
220
221 md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000222
223 memset(md_name, 0x00, 100);
224 memset(src_str, 0x00, 10000);
225 memset(hash_str, 0x00, 10000);
226 memset(output, 0x00, 100);
227
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200228 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000229 md_info = md_info_from_string(md_name);
230 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000231 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000232
Paul Bakker33b43f12013-08-20 11:48:36 +0200233 src_len = unhexify( src_str, hex_src_string );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200234
Paul Bakker562535d2011-01-20 16:42:01 +0000235 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000236 TEST_ASSERT ( ctx.md_ctx != NULL );
237 TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
238 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200239 md_free( &ctx );
240
Paul Bakker17373852011-01-06 14:20:01 +0000241 hexify( hash_str, output, md_get_size(md_info) );
242
Paul Bakker33b43f12013-08-20 11:48:36 +0200243 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000244}
Paul Bakker33b43f12013-08-20 11:48:36 +0200245/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000246
Paul Bakker33b43f12013-08-20 11:48:36 +0200247/* BEGIN_CASE */
248void md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
249 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000250{
251 char md_name[100];
252 unsigned char src_str[10000];
253 unsigned char key_str[10000];
254 unsigned char hash_str[10000];
255 unsigned char output[100];
256 int key_len, src_len;
257 const md_info_t *md_info = NULL;
258
259 memset(md_name, 0x00, 100);
260 memset(src_str, 0x00, 10000);
261 memset(key_str, 0x00, 10000);
262 memset(hash_str, 0x00, 10000);
263 memset(output, 0x00, 100);
264
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200265 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000266 md_info = md_info_from_string( md_name );
267 TEST_ASSERT( md_info != NULL );
268
Paul Bakker33b43f12013-08-20 11:48:36 +0200269 key_len = unhexify( key_str, hex_key_string );
270 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000271
272 TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
273 hexify( hash_str, output, md_get_size(md_info) );
274
Paul Bakker33b43f12013-08-20 11:48:36 +0200275 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000276}
Paul Bakker33b43f12013-08-20 11:48:36 +0200277/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000278
Paul Bakker33b43f12013-08-20 11:48:36 +0200279/* BEGIN_CASE */
280void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string,
281 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000282{
283 char md_name[100];
284 unsigned char src_str[10000];
285 unsigned char key_str[10000];
286 unsigned char hash_str[10000];
287 unsigned char output[100];
288 int key_len, src_len;
289 const md_info_t *md_info = NULL;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200290 md_context_t ctx;
291
292 md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000293
294 memset(md_name, 0x00, 100);
295 memset(src_str, 0x00, 10000);
296 memset(key_str, 0x00, 10000);
297 memset(hash_str, 0x00, 10000);
298 memset(output, 0x00, 100);
299
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200300 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000301 md_info = md_info_from_string( md_name );
302 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000303 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000304
Paul Bakker33b43f12013-08-20 11:48:36 +0200305 key_len = unhexify( key_str, hex_key_string );
306 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000307
Paul Bakker562535d2011-01-20 16:42:01 +0000308 TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000309 TEST_ASSERT ( ctx.md_ctx != NULL );
310 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
311 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100312
313 hexify( hash_str, output, md_get_size(md_info) );
314 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
315
316 /* Test again, for reset() */
317 memset(hash_str, 0x00, 10000);
318 memset(output, 0x00, 100);
319
320 TEST_ASSERT ( 0 == md_hmac_reset( &ctx ) );
321 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
322 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200323 md_free( &ctx );
Paul Bakker33b43f12013-08-20 11:48:36 +0200324
Paul Bakker17373852011-01-06 14:20:01 +0000325 hexify( hash_str, output, md_get_size(md_info) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200326 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000327}
Paul Bakker33b43f12013-08-20 11:48:36 +0200328/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200329
330/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200331void md_file( char *text_md_name, char *filename, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000332{
333 char md_name[100];
334 unsigned char hash_str[1000];
335 unsigned char output[100];
336 const md_info_t *md_info = NULL;
337
338 memset(md_name, 0x00, 100);
339 memset(hash_str, 0x00, 1000);
340 memset(output, 0x00, 100);
341
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200342 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000343 md_info = md_info_from_string( md_name );
344 TEST_ASSERT( md_info != NULL );
345
Paul Bakker33b43f12013-08-20 11:48:36 +0200346 md_file( md_info, filename, output);
Paul Bakker17373852011-01-06 14:20:01 +0000347 hexify( hash_str, output, md_get_size(md_info) );
348
Paul Bakker33b43f12013-08-20 11:48:36 +0200349 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000350}
Paul Bakker33b43f12013-08-20 11:48:36 +0200351/* END_CASE */