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