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