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