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