Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 1 | BEGIN_HEADER |
| 2 | #include <polarssl/des.h> |
| 3 | END_HEADER |
| 4 | |
| 5 | BEGIN_CASE |
| 6 | des_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string |
| 7 | { |
| 8 | unsigned char key_str[100]; |
| 9 | unsigned char src_str[100]; |
| 10 | unsigned char dst_str[100]; |
| 11 | unsigned char output[100]; |
| 12 | des_context ctx; |
| 13 | |
| 14 | memset(key_str, 0x00, 100); |
| 15 | memset(src_str, 0x00, 100); |
| 16 | memset(dst_str, 0x00, 100); |
| 17 | memset(output, 0x00, 100); |
| 18 | |
| 19 | unhexify( key_str, {hex_key_string} ); |
| 20 | unhexify( src_str, {hex_src_string} ); |
| 21 | |
| 22 | des_setkey_enc( &ctx, key_str ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 23 | TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 24 | hexify( dst_str, output, 8 ); |
| 25 | |
| 26 | TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 27 | } |
| 28 | END_CASE |
| 29 | |
| 30 | BEGIN_CASE |
| 31 | des_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string |
| 32 | { |
| 33 | unsigned char key_str[100]; |
| 34 | unsigned char src_str[100]; |
| 35 | unsigned char dst_str[100]; |
| 36 | unsigned char output[100]; |
| 37 | des_context ctx; |
| 38 | |
| 39 | memset(key_str, 0x00, 100); |
| 40 | memset(src_str, 0x00, 100); |
| 41 | memset(dst_str, 0x00, 100); |
| 42 | memset(output, 0x00, 100); |
| 43 | |
| 44 | unhexify( key_str, {hex_key_string} ); |
| 45 | unhexify( src_str, {hex_src_string} ); |
| 46 | |
| 47 | des_setkey_dec( &ctx, key_str ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 48 | TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 49 | hexify( dst_str, output, 8 ); |
| 50 | |
| 51 | TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 52 | } |
| 53 | END_CASE |
| 54 | |
| 55 | BEGIN_CASE |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 56 | des_encrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 57 | { |
| 58 | unsigned char key_str[100]; |
| 59 | unsigned char iv_str[100]; |
| 60 | unsigned char src_str[100]; |
| 61 | unsigned char dst_str[100]; |
| 62 | unsigned char output[100]; |
| 63 | des_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 64 | int src_len; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 65 | |
| 66 | memset(key_str, 0x00, 100); |
| 67 | memset(iv_str, 0x00, 100); |
| 68 | memset(src_str, 0x00, 100); |
| 69 | memset(dst_str, 0x00, 100); |
| 70 | memset(output, 0x00, 100); |
| 71 | |
| 72 | unhexify( key_str, {hex_key_string} ); |
| 73 | unhexify( iv_str, {hex_iv_string} ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 74 | src_len = unhexify( src_str, {hex_src_string} ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 75 | |
| 76 | des_setkey_enc( &ctx, key_str ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 77 | TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == {cbc_result} ); |
| 78 | if( {cbc_result} == 0 ) |
| 79 | { |
| 80 | hexify( dst_str, output, src_len ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 81 | |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 82 | TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 83 | } |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 84 | } |
| 85 | END_CASE |
| 86 | |
| 87 | BEGIN_CASE |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 88 | des_decrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 89 | { |
| 90 | unsigned char key_str[100]; |
| 91 | unsigned char iv_str[100]; |
| 92 | unsigned char src_str[100]; |
| 93 | unsigned char dst_str[100]; |
| 94 | unsigned char output[100]; |
| 95 | des_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 96 | int src_len; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 97 | |
| 98 | memset(key_str, 0x00, 100); |
| 99 | memset(iv_str, 0x00, 100); |
| 100 | memset(src_str, 0x00, 100); |
| 101 | memset(dst_str, 0x00, 100); |
| 102 | memset(output, 0x00, 100); |
| 103 | |
| 104 | unhexify( key_str, {hex_key_string} ); |
| 105 | unhexify( iv_str, {hex_iv_string} ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 106 | src_len = unhexify( src_str, {hex_src_string} ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 107 | |
| 108 | des_setkey_dec( &ctx, key_str ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 109 | TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == {cbc_result} ); |
| 110 | if( {cbc_result} == 0 ) |
| 111 | { |
| 112 | hexify( dst_str, output, src_len ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 113 | |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 114 | TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 115 | } |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 116 | } |
| 117 | END_CASE |
| 118 | |
| 119 | BEGIN_CASE |
| 120 | des3_encrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string |
| 121 | { |
| 122 | unsigned char key_str[100]; |
| 123 | unsigned char src_str[100]; |
| 124 | unsigned char dst_str[100]; |
| 125 | unsigned char output[100]; |
| 126 | des3_context ctx; |
| 127 | |
| 128 | memset(key_str, 0x00, 100); |
| 129 | memset(src_str, 0x00, 100); |
| 130 | memset(dst_str, 0x00, 100); |
| 131 | memset(output, 0x00, 100); |
| 132 | |
| 133 | unhexify( key_str, {hex_key_string} ); |
| 134 | unhexify( src_str, {hex_src_string} ); |
| 135 | |
| 136 | if( {key_count} == 2 ) |
| 137 | des3_set2key_enc( &ctx, key_str ); |
| 138 | else if( {key_count} == 3 ) |
| 139 | des3_set3key_enc( &ctx, key_str ); |
| 140 | else |
| 141 | TEST_ASSERT( 0 ); |
| 142 | |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 143 | TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 144 | hexify( dst_str, output, 8 ); |
| 145 | |
| 146 | TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 147 | } |
| 148 | END_CASE |
| 149 | |
| 150 | BEGIN_CASE |
| 151 | des3_decrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string |
| 152 | { |
| 153 | unsigned char key_str[100]; |
| 154 | unsigned char src_str[100]; |
| 155 | unsigned char dst_str[100]; |
| 156 | unsigned char output[100]; |
| 157 | des3_context ctx; |
| 158 | |
| 159 | memset(key_str, 0x00, 100); |
| 160 | memset(src_str, 0x00, 100); |
| 161 | memset(dst_str, 0x00, 100); |
| 162 | memset(output, 0x00, 100); |
| 163 | |
| 164 | unhexify( key_str, {hex_key_string} ); |
| 165 | unhexify( src_str, {hex_src_string} ); |
| 166 | |
| 167 | if( {key_count} == 2 ) |
| 168 | des3_set2key_dec( &ctx, key_str ); |
| 169 | else if( {key_count} == 3 ) |
| 170 | des3_set3key_dec( &ctx, key_str ); |
| 171 | else |
| 172 | TEST_ASSERT( 0 ); |
| 173 | |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 174 | TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 175 | hexify( dst_str, output, 8 ); |
| 176 | |
| 177 | TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 178 | } |
| 179 | END_CASE |
| 180 | |
| 181 | BEGIN_CASE |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 182 | des3_encrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 183 | { |
| 184 | unsigned char key_str[100]; |
| 185 | unsigned char iv_str[100]; |
| 186 | unsigned char src_str[100]; |
| 187 | unsigned char dst_str[100]; |
| 188 | unsigned char output[100]; |
| 189 | des3_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 190 | int src_len; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 191 | |
| 192 | memset(key_str, 0x00, 100); |
| 193 | memset(iv_str, 0x00, 100); |
| 194 | memset(src_str, 0x00, 100); |
| 195 | memset(dst_str, 0x00, 100); |
| 196 | memset(output, 0x00, 100); |
| 197 | |
| 198 | unhexify( key_str, {hex_key_string} ); |
| 199 | unhexify( iv_str, {hex_iv_string} ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 200 | src_len = unhexify( src_str, {hex_src_string} ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 201 | |
| 202 | if( {key_count} == 2 ) |
| 203 | des3_set2key_enc( &ctx, key_str ); |
| 204 | else if( {key_count} == 3 ) |
| 205 | des3_set3key_enc( &ctx, key_str ); |
| 206 | else |
| 207 | TEST_ASSERT( 0 ); |
| 208 | |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 209 | TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == {cbc_result} ); |
| 210 | if( {cbc_result} == 0 ) |
| 211 | { |
| 212 | hexify( dst_str, output, src_len ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 213 | |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 214 | TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 215 | } |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 216 | } |
| 217 | END_CASE |
| 218 | |
| 219 | BEGIN_CASE |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 220 | des3_decrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 221 | { |
| 222 | unsigned char key_str[100]; |
| 223 | unsigned char iv_str[100]; |
| 224 | unsigned char src_str[100]; |
| 225 | unsigned char dst_str[100]; |
| 226 | unsigned char output[100]; |
| 227 | des3_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 228 | int src_len; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 229 | |
| 230 | memset(key_str, 0x00, 100); |
| 231 | memset(iv_str, 0x00, 100); |
| 232 | memset(src_str, 0x00, 100); |
| 233 | memset(dst_str, 0x00, 100); |
| 234 | memset(output, 0x00, 100); |
| 235 | |
| 236 | unhexify( key_str, {hex_key_string} ); |
| 237 | unhexify( iv_str, {hex_iv_string} ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 238 | src_len = unhexify( src_str, {hex_src_string} ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 239 | |
| 240 | if( {key_count} == 2 ) |
| 241 | des3_set2key_dec( &ctx, key_str ); |
| 242 | else if( {key_count} == 3 ) |
| 243 | des3_set3key_dec( &ctx, key_str ); |
| 244 | else |
| 245 | TEST_ASSERT( 0 ); |
| 246 | |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 247 | TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == {cbc_result} ) |
| 248 | if( {cbc_result} == 0 ) |
| 249 | { |
| 250 | hexify( dst_str, output, src_len ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 251 | |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 252 | TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 253 | } |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 254 | } |
| 255 | END_CASE |
| 256 | |
| 257 | BEGIN_CASE |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame^] | 258 | des_key_parity_run: |
| 259 | { |
| 260 | int i, j, cnt; |
| 261 | unsigned char key[DES_KEY_SIZE]; |
| 262 | unsigned int parity; |
| 263 | |
| 264 | memset( key, 0, DES_KEY_SIZE ); |
| 265 | cnt = 0; |
| 266 | |
| 267 | // Iterate through all possible byte values |
| 268 | // |
| 269 | for( i = 0; i < 32; i++ ) |
| 270 | { |
| 271 | for( j = 0; j < 8; j++ ) |
| 272 | key[j] = cnt++; |
| 273 | |
| 274 | // Set the key parity according to the table |
| 275 | // |
| 276 | des_key_set_parity( key ); |
| 277 | |
| 278 | // Check the parity with a function |
| 279 | // |
| 280 | for( j = 0; j < 8; j++ ) |
| 281 | { |
| 282 | parity = key[j] ^ ( key[j] >> 4 ); |
| 283 | parity = parity ^ |
| 284 | ( parity >> 1 ) ^ |
| 285 | ( parity >> 2 ) ^ |
| 286 | ( parity >> 3 ); |
| 287 | parity &= 1; |
| 288 | |
| 289 | if( parity != 1 ) |
| 290 | TEST_ASSERT( 0 ); |
| 291 | } |
| 292 | |
| 293 | // Check the parity with the table |
| 294 | // |
| 295 | TEST_ASSERT( des_key_check_key_parity( key ) == 0 ); |
| 296 | } |
| 297 | } |
| 298 | END_CASE |
| 299 | |
| 300 | BEGIN_CASE |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 301 | des_selftest: |
| 302 | { |
| 303 | TEST_ASSERT( des_self_test( 0 ) == 0 ); |
| 304 | } |
| 305 | END_CASE |