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