| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1 | BEGIN_HEADER | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 2 | #include <polarssl/md.h> | 
|  | 3 | #include <polarssl/md2.h> | 
|  | 4 | #include <polarssl/md4.h> | 
|  | 5 | #include <polarssl/md5.h> | 
|  | 6 | #include <polarssl/sha1.h> | 
|  | 7 | #include <polarssl/sha2.h> | 
|  | 8 | #include <polarssl/sha4.h> | 
|  | 9 | END_HEADER | 
|  | 10 |  | 
|  | 11 | BEGIN_CASE | 
|  | 12 | md_text:text_md_name:text_src_string:hex_hash_string | 
|  | 13 | { | 
|  | 14 | char md_name[100]; | 
|  | 15 | unsigned char src_str[1000]; | 
|  | 16 | unsigned char hash_str[1000]; | 
|  | 17 | unsigned char output[100]; | 
|  | 18 | const md_info_t *md_info = NULL; | 
|  | 19 |  | 
|  | 20 | memset(md_name, 0x00, 100); | 
|  | 21 | memset(src_str, 0x00, 1000); | 
|  | 22 | memset(hash_str, 0x00, 1000); | 
|  | 23 | memset(output, 0x00, 100); | 
|  | 24 |  | 
|  | 25 | strcpy( (char *) src_str, {text_src_string} ); | 
|  | 26 |  | 
|  | 27 | strncpy( (char *) md_name, {text_md_name}, 100 ); | 
|  | 28 | md_info = md_info_from_string(md_name); | 
|  | 29 | TEST_ASSERT( md_info != NULL ); | 
|  | 30 |  | 
|  | 31 | TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) ); | 
|  | 32 | hexify( hash_str, output, md_get_size(md_info) ); | 
|  | 33 |  | 
|  | 34 | TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 ); | 
|  | 35 | } | 
|  | 36 | END_CASE | 
|  | 37 |  | 
|  | 38 | BEGIN_CASE | 
|  | 39 | md_hex:text_md_name:hex_src_string:hex_hash_string | 
|  | 40 | { | 
|  | 41 | char md_name[100]; | 
|  | 42 | unsigned char src_str[10000]; | 
|  | 43 | unsigned char hash_str[10000]; | 
|  | 44 | unsigned char output[100]; | 
|  | 45 | int src_len; | 
|  | 46 | const md_info_t *md_info = NULL; | 
|  | 47 |  | 
|  | 48 | memset(md_name, 0x00, 100); | 
|  | 49 | memset(src_str, 0x00, 10000); | 
|  | 50 | memset(hash_str, 0x00, 10000); | 
|  | 51 | memset(output, 0x00, 100); | 
|  | 52 |  | 
|  | 53 | strncpy( (char *) md_name, {text_md_name}, 100 ); | 
|  | 54 | md_info = md_info_from_string(md_name); | 
|  | 55 | TEST_ASSERT( md_info != NULL ); | 
|  | 56 |  | 
|  | 57 | src_len = unhexify( src_str, {hex_src_string} ); | 
|  | 58 | TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) ); | 
|  | 59 |  | 
|  | 60 | hexify( hash_str, output, md_get_size(md_info) ); | 
|  | 61 |  | 
|  | 62 | TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 ); | 
|  | 63 | } | 
|  | 64 | END_CASE | 
|  | 65 |  | 
|  | 66 | BEGIN_CASE | 
|  | 67 | md_text_multi:text_md_name:text_src_string:hex_hash_string | 
|  | 68 | { | 
|  | 69 | char md_name[100]; | 
|  | 70 | unsigned char src_str[1000]; | 
|  | 71 | unsigned char hash_str[1000]; | 
|  | 72 | unsigned char output[100]; | 
|  | 73 |  | 
|  | 74 | const md_info_t *md_info = NULL; | 
|  | 75 | md_context_t ctx = MD_CONTEXT_T_INIT; | 
|  | 76 |  | 
|  | 77 | memset(md_name, 0x00, 100); | 
|  | 78 | memset(src_str, 0x00, 1000); | 
|  | 79 | memset(hash_str, 0x00, 1000); | 
|  | 80 | memset(output, 0x00, 100); | 
|  | 81 |  | 
|  | 82 | strcpy( (char *) src_str, {text_src_string} ); | 
|  | 83 |  | 
|  | 84 | strncpy( (char *) md_name, {text_md_name}, 100 ); | 
|  | 85 | md_info = md_info_from_string(md_name); | 
|  | 86 | TEST_ASSERT( md_info != NULL ); | 
| Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 87 | TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) ); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 88 |  | 
| Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 89 | TEST_ASSERT ( 0 == md_starts( &ctx ) ); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 90 | TEST_ASSERT ( ctx.md_ctx != NULL ); | 
|  | 91 | TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) ); | 
|  | 92 | TEST_ASSERT ( 0 == md_finish( &ctx, output ) ); | 
|  | 93 | TEST_ASSERT ( 0 == md_free_ctx( &ctx ) ); | 
|  | 94 |  | 
|  | 95 | hexify( hash_str, output, md_get_size(md_info) ); | 
|  | 96 |  | 
|  | 97 | TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 ); | 
|  | 98 | } | 
|  | 99 | END_CASE | 
|  | 100 |  | 
|  | 101 | BEGIN_CASE | 
|  | 102 | md_hex_multi:text_md_name:hex_src_string:hex_hash_string | 
|  | 103 | { | 
|  | 104 | char md_name[100]; | 
|  | 105 | unsigned char src_str[10000]; | 
|  | 106 | unsigned char hash_str[10000]; | 
|  | 107 | unsigned char output[100]; | 
|  | 108 | int src_len; | 
|  | 109 | const md_info_t *md_info = NULL; | 
|  | 110 | md_context_t ctx = MD_CONTEXT_T_INIT; | 
|  | 111 |  | 
|  | 112 | memset(md_name, 0x00, 100); | 
|  | 113 | memset(src_str, 0x00, 10000); | 
|  | 114 | memset(hash_str, 0x00, 10000); | 
|  | 115 | memset(output, 0x00, 100); | 
|  | 116 |  | 
|  | 117 | strncpy( (char *) md_name, {text_md_name}, 100 ); | 
|  | 118 | md_info = md_info_from_string(md_name); | 
|  | 119 | TEST_ASSERT( md_info != NULL ); | 
| Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 120 | TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) ); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 121 |  | 
|  | 122 | src_len = unhexify( src_str, {hex_src_string} ); | 
|  | 123 |  | 
| Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 124 | TEST_ASSERT ( 0 == md_starts( &ctx ) ); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 125 | TEST_ASSERT ( ctx.md_ctx != NULL ); | 
|  | 126 | TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) ); | 
|  | 127 | TEST_ASSERT ( 0 == md_finish( &ctx, output ) ); | 
|  | 128 | TEST_ASSERT ( 0 == md_free_ctx( &ctx ) ); | 
|  | 129 |  | 
|  | 130 | hexify( hash_str, output, md_get_size(md_info) ); | 
|  | 131 |  | 
|  | 132 | TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 ); | 
|  | 133 | } | 
|  | 134 | END_CASE | 
|  | 135 |  | 
|  | 136 | BEGIN_CASE | 
|  | 137 | md_hmac:text_md_name:trunc_size:hex_key_string:hex_src_string:hex_hash_string | 
|  | 138 | { | 
|  | 139 | char md_name[100]; | 
|  | 140 | unsigned char src_str[10000]; | 
|  | 141 | unsigned char key_str[10000]; | 
|  | 142 | unsigned char hash_str[10000]; | 
|  | 143 | unsigned char output[100]; | 
|  | 144 | int key_len, src_len; | 
|  | 145 | const md_info_t *md_info = NULL; | 
|  | 146 |  | 
|  | 147 | memset(md_name, 0x00, 100); | 
|  | 148 | memset(src_str, 0x00, 10000); | 
|  | 149 | memset(key_str, 0x00, 10000); | 
|  | 150 | memset(hash_str, 0x00, 10000); | 
|  | 151 | memset(output, 0x00, 100); | 
|  | 152 |  | 
|  | 153 | strncpy( (char *) md_name, {text_md_name}, 100 ); | 
|  | 154 | md_info = md_info_from_string( md_name ); | 
|  | 155 | TEST_ASSERT( md_info != NULL ); | 
|  | 156 |  | 
|  | 157 | key_len = unhexify( key_str, {hex_key_string} ); | 
|  | 158 | src_len = unhexify( src_str, {hex_src_string} ); | 
|  | 159 |  | 
|  | 160 | TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 ); | 
|  | 161 | hexify( hash_str, output, md_get_size(md_info) ); | 
|  | 162 |  | 
|  | 163 | TEST_ASSERT( strncmp( (char *) hash_str, {hex_hash_string}, {trunc_size} * 2 ) == 0 ); | 
|  | 164 | } | 
|  | 165 | END_CASE | 
|  | 166 |  | 
|  | 167 | BEGIN_CASE | 
|  | 168 | md_hmac_multi:text_md_name:trunc_size:hex_key_string:hex_src_string:hex_hash_string | 
|  | 169 | { | 
|  | 170 | char md_name[100]; | 
|  | 171 | unsigned char src_str[10000]; | 
|  | 172 | unsigned char key_str[10000]; | 
|  | 173 | unsigned char hash_str[10000]; | 
|  | 174 | unsigned char output[100]; | 
|  | 175 | int key_len, src_len; | 
|  | 176 | const md_info_t *md_info = NULL; | 
|  | 177 | md_context_t ctx = MD_CONTEXT_T_INIT; | 
|  | 178 |  | 
|  | 179 | memset(md_name, 0x00, 100); | 
|  | 180 | memset(src_str, 0x00, 10000); | 
|  | 181 | memset(key_str, 0x00, 10000); | 
|  | 182 | memset(hash_str, 0x00, 10000); | 
|  | 183 | memset(output, 0x00, 100); | 
|  | 184 |  | 
|  | 185 | strncpy( (char *) md_name, {text_md_name}, 100 ); | 
|  | 186 | md_info = md_info_from_string( md_name ); | 
|  | 187 | TEST_ASSERT( md_info != NULL ); | 
| Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 188 | TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) ); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 189 |  | 
|  | 190 | key_len = unhexify( key_str, {hex_key_string} ); | 
|  | 191 | src_len = unhexify( src_str, {hex_src_string} ); | 
|  | 192 |  | 
| Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 193 | TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) ); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 194 | TEST_ASSERT ( ctx.md_ctx != NULL ); | 
|  | 195 | TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) ); | 
|  | 196 | TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) ); | 
|  | 197 | TEST_ASSERT ( 0 == md_free_ctx( &ctx ) ); | 
|  | 198 |  | 
|  | 199 | hexify( hash_str, output, md_get_size(md_info) ); | 
|  | 200 |  | 
|  | 201 | TEST_ASSERT( strncmp( (char *) hash_str, {hex_hash_string}, {trunc_size} * 2 ) == 0 ); | 
|  | 202 | } | 
|  | 203 | END_CASE | 
|  | 204 | BEGIN_CASE | 
|  | 205 | md_file:text_md_name:filename:hex_hash_string | 
|  | 206 | { | 
|  | 207 | char md_name[100]; | 
|  | 208 | unsigned char hash_str[1000]; | 
|  | 209 | unsigned char output[100]; | 
|  | 210 | const md_info_t *md_info = NULL; | 
|  | 211 |  | 
|  | 212 | memset(md_name, 0x00, 100); | 
|  | 213 | memset(hash_str, 0x00, 1000); | 
|  | 214 | memset(output, 0x00, 100); | 
|  | 215 |  | 
|  | 216 | strncpy( (char *) md_name, {text_md_name}, 100 ); | 
|  | 217 | md_info = md_info_from_string( md_name ); | 
|  | 218 | TEST_ASSERT( md_info != NULL ); | 
|  | 219 |  | 
|  | 220 | md_file( md_info, {filename}, output); | 
|  | 221 | hexify( hash_str, output, md_get_size(md_info) ); | 
|  | 222 |  | 
|  | 223 | TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 ); | 
|  | 224 | } | 
|  | 225 | END_CASE |