blob: 1931154b1443ab62f18d769df0d9944f2bd842f1 [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é-Gonnardf3013832014-03-29 15:54:50 +010011void md_list( )
12{
13 const int *md_type_ptr;
14
15 for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ )
16 TEST_ASSERT( md_info_from_type( *md_type_ptr ) != NULL );
17}
18/* END_CASE */
19
20/* BEGIN_CASE */
21void md_info( int md_type, char *md_name, int md_size )
22{
23 const md_info_t *md_info;
24 const int *md_type_ptr;
25 int found;
26
27 TEST_ASSERT( ( md_info = md_info_from_type( md_type ) ) != NULL );
28 TEST_ASSERT( md_info == md_info_from_string( md_name ) );
29
30 TEST_ASSERT( md_get_type( md_info ) == (md_type_t) md_type );
31 TEST_ASSERT( md_get_size( md_info ) == (unsigned char) md_size );
32
33 found = 0;
34 for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ )
35 if( *md_type_ptr == md_type )
36 found = 1;
37 TEST_ASSERT( found == 1 );
38}
39/* END_CASE */
40
41/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020042void md_text( char *text_md_name, char *text_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +000043{
44 char md_name[100];
45 unsigned char src_str[1000];
46 unsigned char hash_str[1000];
47 unsigned char output[100];
48 const md_info_t *md_info = NULL;
49
50 memset(md_name, 0x00, 100);
51 memset(src_str, 0x00, 1000);
52 memset(hash_str, 0x00, 1000);
53 memset(output, 0x00, 100);
54
Paul Bakker33b43f12013-08-20 11:48:36 +020055 strcpy( (char *) src_str, text_src_string );
Paul Bakker17373852011-01-06 14:20:01 +000056
Paul Bakker33b43f12013-08-20 11:48:36 +020057 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +000058 md_info = md_info_from_string(md_name);
59 TEST_ASSERT( md_info != NULL );
60
61 TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) );
62 hexify( hash_str, output, md_get_size(md_info) );
63
Paul Bakker33b43f12013-08-20 11:48:36 +020064 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +000065}
Paul Bakker33b43f12013-08-20 11:48:36 +020066/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +000067
Paul Bakker33b43f12013-08-20 11:48:36 +020068/* BEGIN_CASE */
69void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +000070{
71 char md_name[100];
72 unsigned char src_str[10000];
73 unsigned char hash_str[10000];
74 unsigned char output[100];
75 int src_len;
76 const md_info_t *md_info = NULL;
77
78 memset(md_name, 0x00, 100);
79 memset(src_str, 0x00, 10000);
80 memset(hash_str, 0x00, 10000);
81 memset(output, 0x00, 100);
82
Paul Bakker33b43f12013-08-20 11:48:36 +020083 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +000084 md_info = md_info_from_string(md_name);
85 TEST_ASSERT( md_info != NULL );
86
Paul Bakker33b43f12013-08-20 11:48:36 +020087 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +000088 TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) );
89
90 hexify( hash_str, output, md_get_size(md_info) );
91
Paul Bakker33b43f12013-08-20 11:48:36 +020092 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +000093}
Paul Bakker33b43f12013-08-20 11:48:36 +020094/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +000095
Paul Bakker33b43f12013-08-20 11:48:36 +020096/* BEGIN_CASE */
97void md_text_multi( char *text_md_name, char *text_src_string,
98 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +000099{
100 char md_name[100];
101 unsigned char src_str[1000];
102 unsigned char hash_str[1000];
103 unsigned char output[100];
104
105 const md_info_t *md_info = NULL;
106 md_context_t ctx = MD_CONTEXT_T_INIT;
107
108 memset(md_name, 0x00, 100);
109 memset(src_str, 0x00, 1000);
110 memset(hash_str, 0x00, 1000);
111 memset(output, 0x00, 100);
112
Paul Bakker33b43f12013-08-20 11:48:36 +0200113 strcpy( (char *) src_str, text_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000114
Paul Bakker33b43f12013-08-20 11:48:36 +0200115 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000116 md_info = md_info_from_string(md_name);
117 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000118 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000119
Paul Bakker562535d2011-01-20 16:42:01 +0000120 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000121 TEST_ASSERT ( ctx.md_ctx != NULL );
122 TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
123 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
124 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
125
126 hexify( hash_str, output, md_get_size(md_info) );
127
Paul Bakker33b43f12013-08-20 11:48:36 +0200128 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000129}
Paul Bakker33b43f12013-08-20 11:48:36 +0200130/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000131
Paul Bakker33b43f12013-08-20 11:48:36 +0200132/* BEGIN_CASE */
133void md_hex_multi( char *text_md_name, char *hex_src_string,
134 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000135{
136 char md_name[100];
137 unsigned char src_str[10000];
138 unsigned char hash_str[10000];
139 unsigned char output[100];
140 int src_len;
141 const md_info_t *md_info = NULL;
142 md_context_t ctx = MD_CONTEXT_T_INIT;
143
144 memset(md_name, 0x00, 100);
145 memset(src_str, 0x00, 10000);
146 memset(hash_str, 0x00, 10000);
147 memset(output, 0x00, 100);
148
Paul Bakker33b43f12013-08-20 11:48:36 +0200149 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000150 md_info = md_info_from_string(md_name);
151 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000152 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000153
Paul Bakker33b43f12013-08-20 11:48:36 +0200154 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000155
Paul Bakker562535d2011-01-20 16:42:01 +0000156 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000157 TEST_ASSERT ( ctx.md_ctx != NULL );
158 TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
159 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
160 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
161
162 hexify( hash_str, output, md_get_size(md_info) );
163
Paul Bakker33b43f12013-08-20 11:48:36 +0200164 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000165}
Paul Bakker33b43f12013-08-20 11:48:36 +0200166/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000167
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* BEGIN_CASE */
169void md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
170 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000171{
172 char md_name[100];
173 unsigned char src_str[10000];
174 unsigned char key_str[10000];
175 unsigned char hash_str[10000];
176 unsigned char output[100];
177 int key_len, src_len;
178 const md_info_t *md_info = NULL;
179
180 memset(md_name, 0x00, 100);
181 memset(src_str, 0x00, 10000);
182 memset(key_str, 0x00, 10000);
183 memset(hash_str, 0x00, 10000);
184 memset(output, 0x00, 100);
185
Paul Bakker33b43f12013-08-20 11:48:36 +0200186 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000187 md_info = md_info_from_string( md_name );
188 TEST_ASSERT( md_info != NULL );
189
Paul Bakker33b43f12013-08-20 11:48:36 +0200190 key_len = unhexify( key_str, hex_key_string );
191 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000192
193 TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
194 hexify( hash_str, output, md_get_size(md_info) );
195
Paul Bakker33b43f12013-08-20 11:48:36 +0200196 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000197}
Paul Bakker33b43f12013-08-20 11:48:36 +0200198/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000199
Paul Bakker33b43f12013-08-20 11:48:36 +0200200/* BEGIN_CASE */
201void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string,
202 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000203{
204 char md_name[100];
205 unsigned char src_str[10000];
206 unsigned char key_str[10000];
207 unsigned char hash_str[10000];
208 unsigned char output[100];
209 int key_len, src_len;
210 const md_info_t *md_info = NULL;
211 md_context_t ctx = MD_CONTEXT_T_INIT;
212
213 memset(md_name, 0x00, 100);
214 memset(src_str, 0x00, 10000);
215 memset(key_str, 0x00, 10000);
216 memset(hash_str, 0x00, 10000);
217 memset(output, 0x00, 100);
218
Paul Bakker33b43f12013-08-20 11:48:36 +0200219 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000220 md_info = md_info_from_string( md_name );
221 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000222 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000223
Paul Bakker33b43f12013-08-20 11:48:36 +0200224 key_len = unhexify( key_str, hex_key_string );
225 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000226
Paul Bakker562535d2011-01-20 16:42:01 +0000227 TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000228 TEST_ASSERT ( ctx.md_ctx != NULL );
229 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
230 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100231
232 hexify( hash_str, output, md_get_size(md_info) );
233 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
234
235 /* Test again, for reset() */
236 memset(hash_str, 0x00, 10000);
237 memset(output, 0x00, 100);
238
239 TEST_ASSERT ( 0 == md_hmac_reset( &ctx ) );
240 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
241 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000242 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200243
Paul Bakker17373852011-01-06 14:20:01 +0000244 hexify( hash_str, output, md_get_size(md_info) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200245 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100246
247 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000248}
Paul Bakker33b43f12013-08-20 11:48:36 +0200249/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200250
251/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200252void md_file( char *text_md_name, char *filename, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000253{
254 char md_name[100];
255 unsigned char hash_str[1000];
256 unsigned char output[100];
257 const md_info_t *md_info = NULL;
258
259 memset(md_name, 0x00, 100);
260 memset(hash_str, 0x00, 1000);
261 memset(output, 0x00, 100);
262
Paul Bakker33b43f12013-08-20 11:48:36 +0200263 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000264 md_info = md_info_from_string( md_name );
265 TEST_ASSERT( md_info != NULL );
266
Paul Bakker33b43f12013-08-20 11:48:36 +0200267 md_file( md_info, filename, output);
Paul Bakker17373852011-01-06 14:20:01 +0000268 hexify( hash_str, output, md_get_size(md_info) );
269
Paul Bakker33b43f12013-08-20 11:48:36 +0200270 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000271}
Paul Bakker33b43f12013-08-20 11:48:36 +0200272/* END_CASE */