blob: afd2f5e0d4528e2357e221b3e5f98ec659491349 [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 */
40void md_info( int md_type, char *md_name, int md_size )
41{
42 const md_info_t *md_info;
43 const int *md_type_ptr;
44 int found;
45
Paul Bakker94b916c2014-04-17 16:07:20 +020046 md_info = md_info_from_type( md_type );
47 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010048 TEST_ASSERT( md_info == md_info_from_string( md_name ) );
49
50 TEST_ASSERT( md_get_type( md_info ) == (md_type_t) md_type );
51 TEST_ASSERT( md_get_size( md_info ) == (unsigned char) md_size );
52
53 found = 0;
54 for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ )
55 if( *md_type_ptr == md_type )
56 found = 1;
57 TEST_ASSERT( found == 1 );
58}
59/* END_CASE */
60
61/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020062void md_text( char *text_md_name, char *text_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +000063{
64 char md_name[100];
65 unsigned char src_str[1000];
66 unsigned char hash_str[1000];
67 unsigned char output[100];
68 const md_info_t *md_info = NULL;
69
70 memset(md_name, 0x00, 100);
71 memset(src_str, 0x00, 1000);
72 memset(hash_str, 0x00, 1000);
73 memset(output, 0x00, 100);
74
Paul Bakkerdd0aae92014-04-17 16:06:37 +020075 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
76 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +000077 md_info = md_info_from_string(md_name);
78 TEST_ASSERT( md_info != NULL );
79
80 TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) );
81 hexify( hash_str, output, md_get_size(md_info) );
82
Paul Bakker33b43f12013-08-20 11:48:36 +020083 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +000084}
Paul Bakker33b43f12013-08-20 11:48:36 +020085/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +000086
Paul Bakker33b43f12013-08-20 11:48:36 +020087/* BEGIN_CASE */
88void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +000089{
90 char md_name[100];
91 unsigned char src_str[10000];
92 unsigned char hash_str[10000];
93 unsigned char output[100];
94 int src_len;
95 const md_info_t *md_info = NULL;
96
97 memset(md_name, 0x00, 100);
98 memset(src_str, 0x00, 10000);
99 memset(hash_str, 0x00, 10000);
100 memset(output, 0x00, 100);
101
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200102 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000103 md_info = md_info_from_string(md_name);
104 TEST_ASSERT( md_info != NULL );
105
Paul Bakker33b43f12013-08-20 11:48:36 +0200106 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000107 TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) );
108
109 hexify( hash_str, output, md_get_size(md_info) );
110
Paul Bakker33b43f12013-08-20 11:48:36 +0200111 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000112}
Paul Bakker33b43f12013-08-20 11:48:36 +0200113/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000114
Paul Bakker33b43f12013-08-20 11:48:36 +0200115/* BEGIN_CASE */
116void md_text_multi( char *text_md_name, char *text_src_string,
117 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000118{
119 char md_name[100];
120 unsigned char src_str[1000];
121 unsigned char hash_str[1000];
122 unsigned char output[100];
123
124 const md_info_t *md_info = NULL;
125 md_context_t ctx = MD_CONTEXT_T_INIT;
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 );
Paul Bakker562535d2011-01-20 16:42:01 +0000136 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000137
Paul Bakker562535d2011-01-20 16:42:01 +0000138 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000139 TEST_ASSERT ( ctx.md_ctx != NULL );
140 TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
141 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
142 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
143
144 hexify( hash_str, output, md_get_size(md_info) );
145
Paul Bakker33b43f12013-08-20 11:48:36 +0200146 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000147}
Paul Bakker33b43f12013-08-20 11:48:36 +0200148/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000149
Paul Bakker33b43f12013-08-20 11:48:36 +0200150/* BEGIN_CASE */
151void md_hex_multi( char *text_md_name, char *hex_src_string,
152 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000153{
154 char md_name[100];
155 unsigned char src_str[10000];
156 unsigned char hash_str[10000];
157 unsigned char output[100];
158 int src_len;
159 const md_info_t *md_info = NULL;
160 md_context_t ctx = MD_CONTEXT_T_INIT;
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 );
Paul Bakker562535d2011-01-20 16:42:01 +0000170 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000171
Paul Bakker33b43f12013-08-20 11:48:36 +0200172 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000173
Paul Bakker562535d2011-01-20 16:42:01 +0000174 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000175 TEST_ASSERT ( ctx.md_ctx != NULL );
176 TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
177 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
178 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
179
180 hexify( hash_str, output, md_get_size(md_info) );
181
Paul Bakker33b43f12013-08-20 11:48:36 +0200182 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000183}
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000185
Paul Bakker33b43f12013-08-20 11:48:36 +0200186/* BEGIN_CASE */
187void md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
188 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000189{
190 char md_name[100];
191 unsigned char src_str[10000];
192 unsigned char key_str[10000];
193 unsigned char hash_str[10000];
194 unsigned char output[100];
195 int key_len, src_len;
196 const md_info_t *md_info = NULL;
197
198 memset(md_name, 0x00, 100);
199 memset(src_str, 0x00, 10000);
200 memset(key_str, 0x00, 10000);
201 memset(hash_str, 0x00, 10000);
202 memset(output, 0x00, 100);
203
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200204 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000205 md_info = md_info_from_string( md_name );
206 TEST_ASSERT( md_info != NULL );
207
Paul Bakker33b43f12013-08-20 11:48:36 +0200208 key_len = unhexify( key_str, hex_key_string );
209 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000210
211 TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
212 hexify( hash_str, output, md_get_size(md_info) );
213
Paul Bakker33b43f12013-08-20 11:48:36 +0200214 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000215}
Paul Bakker33b43f12013-08-20 11:48:36 +0200216/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000217
Paul Bakker33b43f12013-08-20 11:48:36 +0200218/* BEGIN_CASE */
219void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string,
220 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000221{
222 char md_name[100];
223 unsigned char src_str[10000];
224 unsigned char key_str[10000];
225 unsigned char hash_str[10000];
226 unsigned char output[100];
227 int key_len, src_len;
228 const md_info_t *md_info = NULL;
229 md_context_t ctx = MD_CONTEXT_T_INIT;
230
231 memset(md_name, 0x00, 100);
232 memset(src_str, 0x00, 10000);
233 memset(key_str, 0x00, 10000);
234 memset(hash_str, 0x00, 10000);
235 memset(output, 0x00, 100);
236
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200237 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000238 md_info = md_info_from_string( md_name );
239 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000240 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000241
Paul Bakker33b43f12013-08-20 11:48:36 +0200242 key_len = unhexify( key_str, hex_key_string );
243 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000244
Paul Bakker562535d2011-01-20 16:42:01 +0000245 TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000246 TEST_ASSERT ( ctx.md_ctx != NULL );
247 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
248 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100249
250 hexify( hash_str, output, md_get_size(md_info) );
251 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
252
253 /* Test again, for reset() */
254 memset(hash_str, 0x00, 10000);
255 memset(output, 0x00, 100);
256
257 TEST_ASSERT ( 0 == md_hmac_reset( &ctx ) );
258 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
259 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000260 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200261
Paul Bakker17373852011-01-06 14:20:01 +0000262 hexify( hash_str, output, md_get_size(md_info) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200263 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100264
265 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000266}
Paul Bakker33b43f12013-08-20 11:48:36 +0200267/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200268
269/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200270void md_file( char *text_md_name, char *filename, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000271{
272 char md_name[100];
273 unsigned char hash_str[1000];
274 unsigned char output[100];
275 const md_info_t *md_info = NULL;
276
277 memset(md_name, 0x00, 100);
278 memset(hash_str, 0x00, 1000);
279 memset(output, 0x00, 100);
280
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200281 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000282 md_info = md_info_from_string( md_name );
283 TEST_ASSERT( md_info != NULL );
284
Paul Bakker33b43f12013-08-20 11:48:36 +0200285 md_file( md_info, filename, output);
Paul Bakker17373852011-01-06 14:20:01 +0000286 hexify( hash_str, output, md_get_size(md_info) );
287
Paul Bakker33b43f12013-08-20 11:48:36 +0200288 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000289}
Paul Bakker33b43f12013-08-20 11:48:36 +0200290/* END_CASE */