mbed TLS Build
tests/suites/test_suite_des.function@0:cdf462088d13, 2017-01-05 (annotated)
- Committer:
- markrad
- Date:
- Thu Jan 05 00:18:44 2017 +0000
- Revision:
- 0:cdf462088d13
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /* BEGIN_HEADER */ |
markrad | 0:cdf462088d13 | 2 | #include "mbedtls/des.h" |
markrad | 0:cdf462088d13 | 3 | /* END_HEADER */ |
markrad | 0:cdf462088d13 | 4 | |
markrad | 0:cdf462088d13 | 5 | /* BEGIN_DEPENDENCIES |
markrad | 0:cdf462088d13 | 6 | * depends_on:MBEDTLS_DES_C |
markrad | 0:cdf462088d13 | 7 | * END_DEPENDENCIES |
markrad | 0:cdf462088d13 | 8 | */ |
markrad | 0:cdf462088d13 | 9 | |
markrad | 0:cdf462088d13 | 10 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 11 | void des_check_weak( char *key_hex, int ret ) |
markrad | 0:cdf462088d13 | 12 | { |
markrad | 0:cdf462088d13 | 13 | unsigned char key[MBEDTLS_DES_KEY_SIZE]; |
markrad | 0:cdf462088d13 | 14 | |
markrad | 0:cdf462088d13 | 15 | memset( key, 0, sizeof key ); |
markrad | 0:cdf462088d13 | 16 | |
markrad | 0:cdf462088d13 | 17 | unhexify( key, key_hex ); |
markrad | 0:cdf462088d13 | 18 | |
markrad | 0:cdf462088d13 | 19 | TEST_ASSERT( mbedtls_des_key_check_weak( key ) == ret ); |
markrad | 0:cdf462088d13 | 20 | } |
markrad | 0:cdf462088d13 | 21 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 22 | |
markrad | 0:cdf462088d13 | 23 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 24 | void des_encrypt_ecb( char *hex_key_string, char *hex_src_string, |
markrad | 0:cdf462088d13 | 25 | char *hex_dst_string ) |
markrad | 0:cdf462088d13 | 26 | { |
markrad | 0:cdf462088d13 | 27 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 28 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 29 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 30 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 31 | mbedtls_des_context ctx; |
markrad | 0:cdf462088d13 | 32 | |
markrad | 0:cdf462088d13 | 33 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 34 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 35 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 36 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 37 | mbedtls_des_init( &ctx ); |
markrad | 0:cdf462088d13 | 38 | |
markrad | 0:cdf462088d13 | 39 | unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 40 | unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 41 | |
markrad | 0:cdf462088d13 | 42 | mbedtls_des_setkey_enc( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 43 | TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str, output ) == 0 ); |
markrad | 0:cdf462088d13 | 44 | hexify( dst_str, output, 8 ); |
markrad | 0:cdf462088d13 | 45 | |
markrad | 0:cdf462088d13 | 46 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 47 | |
markrad | 0:cdf462088d13 | 48 | exit: |
markrad | 0:cdf462088d13 | 49 | mbedtls_des_free( &ctx ); |
markrad | 0:cdf462088d13 | 50 | } |
markrad | 0:cdf462088d13 | 51 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 52 | |
markrad | 0:cdf462088d13 | 53 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 54 | void des_decrypt_ecb( char *hex_key_string, char *hex_src_string, |
markrad | 0:cdf462088d13 | 55 | char *hex_dst_string ) |
markrad | 0:cdf462088d13 | 56 | { |
markrad | 0:cdf462088d13 | 57 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 58 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 59 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 60 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 61 | mbedtls_des_context ctx; |
markrad | 0:cdf462088d13 | 62 | |
markrad | 0:cdf462088d13 | 63 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 64 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 65 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 66 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 67 | mbedtls_des_init( &ctx ); |
markrad | 0:cdf462088d13 | 68 | |
markrad | 0:cdf462088d13 | 69 | unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 70 | unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 71 | |
markrad | 0:cdf462088d13 | 72 | mbedtls_des_setkey_dec( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 73 | TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str, output ) == 0 ); |
markrad | 0:cdf462088d13 | 74 | hexify( dst_str, output, 8 ); |
markrad | 0:cdf462088d13 | 75 | |
markrad | 0:cdf462088d13 | 76 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 77 | |
markrad | 0:cdf462088d13 | 78 | exit: |
markrad | 0:cdf462088d13 | 79 | mbedtls_des_free( &ctx ); |
markrad | 0:cdf462088d13 | 80 | } |
markrad | 0:cdf462088d13 | 81 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 82 | |
markrad | 0:cdf462088d13 | 83 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
markrad | 0:cdf462088d13 | 84 | void des_encrypt_cbc( char *hex_key_string, char *hex_iv_string, |
markrad | 0:cdf462088d13 | 85 | char *hex_src_string, char *hex_dst_string, int cbc_result ) |
markrad | 0:cdf462088d13 | 86 | { |
markrad | 0:cdf462088d13 | 87 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 88 | unsigned char iv_str[100]; |
markrad | 0:cdf462088d13 | 89 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 90 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 91 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 92 | mbedtls_des_context ctx; |
markrad | 0:cdf462088d13 | 93 | int src_len; |
markrad | 0:cdf462088d13 | 94 | |
markrad | 0:cdf462088d13 | 95 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 96 | memset(iv_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 97 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 98 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 99 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 100 | mbedtls_des_init( &ctx ); |
markrad | 0:cdf462088d13 | 101 | |
markrad | 0:cdf462088d13 | 102 | unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 103 | unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 104 | src_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 105 | |
markrad | 0:cdf462088d13 | 106 | mbedtls_des_setkey_enc( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 107 | TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result ); |
markrad | 0:cdf462088d13 | 108 | if( cbc_result == 0 ) |
markrad | 0:cdf462088d13 | 109 | { |
markrad | 0:cdf462088d13 | 110 | hexify( dst_str, output, src_len ); |
markrad | 0:cdf462088d13 | 111 | |
markrad | 0:cdf462088d13 | 112 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 113 | } |
markrad | 0:cdf462088d13 | 114 | |
markrad | 0:cdf462088d13 | 115 | exit: |
markrad | 0:cdf462088d13 | 116 | mbedtls_des_free( &ctx ); |
markrad | 0:cdf462088d13 | 117 | } |
markrad | 0:cdf462088d13 | 118 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 119 | |
markrad | 0:cdf462088d13 | 120 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
markrad | 0:cdf462088d13 | 121 | void des_decrypt_cbc( char *hex_key_string, char *hex_iv_string, |
markrad | 0:cdf462088d13 | 122 | char *hex_src_string, char *hex_dst_string, int cbc_result ) |
markrad | 0:cdf462088d13 | 123 | { |
markrad | 0:cdf462088d13 | 124 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 125 | unsigned char iv_str[100]; |
markrad | 0:cdf462088d13 | 126 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 127 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 128 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 129 | mbedtls_des_context ctx; |
markrad | 0:cdf462088d13 | 130 | int src_len; |
markrad | 0:cdf462088d13 | 131 | |
markrad | 0:cdf462088d13 | 132 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 133 | memset(iv_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 134 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 135 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 136 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 137 | mbedtls_des_init( &ctx ); |
markrad | 0:cdf462088d13 | 138 | |
markrad | 0:cdf462088d13 | 139 | unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 140 | unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 141 | src_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 142 | |
markrad | 0:cdf462088d13 | 143 | mbedtls_des_setkey_dec( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 144 | TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result ); |
markrad | 0:cdf462088d13 | 145 | if( cbc_result == 0 ) |
markrad | 0:cdf462088d13 | 146 | { |
markrad | 0:cdf462088d13 | 147 | hexify( dst_str, output, src_len ); |
markrad | 0:cdf462088d13 | 148 | |
markrad | 0:cdf462088d13 | 149 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 150 | } |
markrad | 0:cdf462088d13 | 151 | |
markrad | 0:cdf462088d13 | 152 | exit: |
markrad | 0:cdf462088d13 | 153 | mbedtls_des_free( &ctx ); |
markrad | 0:cdf462088d13 | 154 | } |
markrad | 0:cdf462088d13 | 155 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 156 | |
markrad | 0:cdf462088d13 | 157 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 158 | void des3_encrypt_ecb( int key_count, char *hex_key_string, |
markrad | 0:cdf462088d13 | 159 | char *hex_src_string, char *hex_dst_string ) |
markrad | 0:cdf462088d13 | 160 | { |
markrad | 0:cdf462088d13 | 161 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 162 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 163 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 164 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 165 | mbedtls_des3_context ctx; |
markrad | 0:cdf462088d13 | 166 | |
markrad | 0:cdf462088d13 | 167 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 168 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 169 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 170 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 171 | mbedtls_des3_init( &ctx ); |
markrad | 0:cdf462088d13 | 172 | |
markrad | 0:cdf462088d13 | 173 | unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 174 | unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 175 | |
markrad | 0:cdf462088d13 | 176 | if( key_count == 2 ) |
markrad | 0:cdf462088d13 | 177 | mbedtls_des3_set2key_enc( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 178 | else if( key_count == 3 ) |
markrad | 0:cdf462088d13 | 179 | mbedtls_des3_set3key_enc( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 180 | else |
markrad | 0:cdf462088d13 | 181 | TEST_ASSERT( 0 ); |
markrad | 0:cdf462088d13 | 182 | |
markrad | 0:cdf462088d13 | 183 | TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str, output ) == 0 ); |
markrad | 0:cdf462088d13 | 184 | hexify( dst_str, output, 8 ); |
markrad | 0:cdf462088d13 | 185 | |
markrad | 0:cdf462088d13 | 186 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 187 | |
markrad | 0:cdf462088d13 | 188 | exit: |
markrad | 0:cdf462088d13 | 189 | mbedtls_des3_free( &ctx ); |
markrad | 0:cdf462088d13 | 190 | } |
markrad | 0:cdf462088d13 | 191 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 192 | |
markrad | 0:cdf462088d13 | 193 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 194 | void des3_decrypt_ecb( int key_count, char *hex_key_string, |
markrad | 0:cdf462088d13 | 195 | char *hex_src_string, char *hex_dst_string ) |
markrad | 0:cdf462088d13 | 196 | { |
markrad | 0:cdf462088d13 | 197 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 198 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 199 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 200 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 201 | mbedtls_des3_context ctx; |
markrad | 0:cdf462088d13 | 202 | |
markrad | 0:cdf462088d13 | 203 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 204 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 205 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 206 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 207 | mbedtls_des3_init( &ctx ); |
markrad | 0:cdf462088d13 | 208 | |
markrad | 0:cdf462088d13 | 209 | unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 210 | unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 211 | |
markrad | 0:cdf462088d13 | 212 | if( key_count == 2 ) |
markrad | 0:cdf462088d13 | 213 | mbedtls_des3_set2key_dec( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 214 | else if( key_count == 3 ) |
markrad | 0:cdf462088d13 | 215 | mbedtls_des3_set3key_dec( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 216 | else |
markrad | 0:cdf462088d13 | 217 | TEST_ASSERT( 0 ); |
markrad | 0:cdf462088d13 | 218 | |
markrad | 0:cdf462088d13 | 219 | TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str, output ) == 0 ); |
markrad | 0:cdf462088d13 | 220 | hexify( dst_str, output, 8 ); |
markrad | 0:cdf462088d13 | 221 | |
markrad | 0:cdf462088d13 | 222 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 223 | |
markrad | 0:cdf462088d13 | 224 | exit: |
markrad | 0:cdf462088d13 | 225 | mbedtls_des3_free( &ctx ); |
markrad | 0:cdf462088d13 | 226 | } |
markrad | 0:cdf462088d13 | 227 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 228 | |
markrad | 0:cdf462088d13 | 229 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
markrad | 0:cdf462088d13 | 230 | void des3_encrypt_cbc( int key_count, char *hex_key_string, |
markrad | 0:cdf462088d13 | 231 | char *hex_iv_string, char *hex_src_string, |
markrad | 0:cdf462088d13 | 232 | char *hex_dst_string, int cbc_result ) |
markrad | 0:cdf462088d13 | 233 | { |
markrad | 0:cdf462088d13 | 234 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 235 | unsigned char iv_str[100]; |
markrad | 0:cdf462088d13 | 236 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 237 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 238 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 239 | mbedtls_des3_context ctx; |
markrad | 0:cdf462088d13 | 240 | int src_len; |
markrad | 0:cdf462088d13 | 241 | |
markrad | 0:cdf462088d13 | 242 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 243 | memset(iv_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 244 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 245 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 246 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 247 | mbedtls_des3_init( &ctx ); |
markrad | 0:cdf462088d13 | 248 | |
markrad | 0:cdf462088d13 | 249 | unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 250 | unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 251 | src_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 252 | |
markrad | 0:cdf462088d13 | 253 | if( key_count == 2 ) |
markrad | 0:cdf462088d13 | 254 | mbedtls_des3_set2key_enc( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 255 | else if( key_count == 3 ) |
markrad | 0:cdf462088d13 | 256 | mbedtls_des3_set3key_enc( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 257 | else |
markrad | 0:cdf462088d13 | 258 | TEST_ASSERT( 0 ); |
markrad | 0:cdf462088d13 | 259 | |
markrad | 0:cdf462088d13 | 260 | TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result ); |
markrad | 0:cdf462088d13 | 261 | |
markrad | 0:cdf462088d13 | 262 | if( cbc_result == 0 ) |
markrad | 0:cdf462088d13 | 263 | { |
markrad | 0:cdf462088d13 | 264 | hexify( dst_str, output, src_len ); |
markrad | 0:cdf462088d13 | 265 | |
markrad | 0:cdf462088d13 | 266 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 267 | } |
markrad | 0:cdf462088d13 | 268 | |
markrad | 0:cdf462088d13 | 269 | exit: |
markrad | 0:cdf462088d13 | 270 | mbedtls_des3_free( &ctx ); |
markrad | 0:cdf462088d13 | 271 | } |
markrad | 0:cdf462088d13 | 272 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 273 | |
markrad | 0:cdf462088d13 | 274 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
markrad | 0:cdf462088d13 | 275 | void des3_decrypt_cbc( int key_count, char *hex_key_string, |
markrad | 0:cdf462088d13 | 276 | char *hex_iv_string, char *hex_src_string, |
markrad | 0:cdf462088d13 | 277 | char *hex_dst_string, int cbc_result ) |
markrad | 0:cdf462088d13 | 278 | { |
markrad | 0:cdf462088d13 | 279 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 280 | unsigned char iv_str[100]; |
markrad | 0:cdf462088d13 | 281 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 282 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 283 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 284 | mbedtls_des3_context ctx; |
markrad | 0:cdf462088d13 | 285 | int src_len; |
markrad | 0:cdf462088d13 | 286 | |
markrad | 0:cdf462088d13 | 287 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 288 | memset(iv_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 289 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 290 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 291 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 292 | mbedtls_des3_init( &ctx ); |
markrad | 0:cdf462088d13 | 293 | |
markrad | 0:cdf462088d13 | 294 | unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 295 | unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 296 | src_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 297 | |
markrad | 0:cdf462088d13 | 298 | if( key_count == 2 ) |
markrad | 0:cdf462088d13 | 299 | mbedtls_des3_set2key_dec( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 300 | else if( key_count == 3 ) |
markrad | 0:cdf462088d13 | 301 | mbedtls_des3_set3key_dec( &ctx, key_str ); |
markrad | 0:cdf462088d13 | 302 | else |
markrad | 0:cdf462088d13 | 303 | TEST_ASSERT( 0 ); |
markrad | 0:cdf462088d13 | 304 | |
markrad | 0:cdf462088d13 | 305 | TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result ); |
markrad | 0:cdf462088d13 | 306 | |
markrad | 0:cdf462088d13 | 307 | if( cbc_result == 0 ) |
markrad | 0:cdf462088d13 | 308 | { |
markrad | 0:cdf462088d13 | 309 | hexify( dst_str, output, src_len ); |
markrad | 0:cdf462088d13 | 310 | |
markrad | 0:cdf462088d13 | 311 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 312 | } |
markrad | 0:cdf462088d13 | 313 | |
markrad | 0:cdf462088d13 | 314 | exit: |
markrad | 0:cdf462088d13 | 315 | mbedtls_des3_free( &ctx ); |
markrad | 0:cdf462088d13 | 316 | } |
markrad | 0:cdf462088d13 | 317 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 318 | |
markrad | 0:cdf462088d13 | 319 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 320 | void des_key_parity_run() |
markrad | 0:cdf462088d13 | 321 | { |
markrad | 0:cdf462088d13 | 322 | int i, j, cnt; |
markrad | 0:cdf462088d13 | 323 | unsigned char key[MBEDTLS_DES_KEY_SIZE]; |
markrad | 0:cdf462088d13 | 324 | unsigned int parity; |
markrad | 0:cdf462088d13 | 325 | |
markrad | 0:cdf462088d13 | 326 | memset( key, 0, MBEDTLS_DES_KEY_SIZE ); |
markrad | 0:cdf462088d13 | 327 | cnt = 0; |
markrad | 0:cdf462088d13 | 328 | |
markrad | 0:cdf462088d13 | 329 | // Iterate through all possible byte values |
markrad | 0:cdf462088d13 | 330 | // |
markrad | 0:cdf462088d13 | 331 | for( i = 0; i < 32; i++ ) |
markrad | 0:cdf462088d13 | 332 | { |
markrad | 0:cdf462088d13 | 333 | for( j = 0; j < 8; j++ ) |
markrad | 0:cdf462088d13 | 334 | key[j] = cnt++; |
markrad | 0:cdf462088d13 | 335 | |
markrad | 0:cdf462088d13 | 336 | // Set the key parity according to the table |
markrad | 0:cdf462088d13 | 337 | // |
markrad | 0:cdf462088d13 | 338 | mbedtls_des_key_set_parity( key ); |
markrad | 0:cdf462088d13 | 339 | |
markrad | 0:cdf462088d13 | 340 | // Check the parity with a function |
markrad | 0:cdf462088d13 | 341 | // |
markrad | 0:cdf462088d13 | 342 | for( j = 0; j < 8; j++ ) |
markrad | 0:cdf462088d13 | 343 | { |
markrad | 0:cdf462088d13 | 344 | parity = key[j] ^ ( key[j] >> 4 ); |
markrad | 0:cdf462088d13 | 345 | parity = parity ^ |
markrad | 0:cdf462088d13 | 346 | ( parity >> 1 ) ^ |
markrad | 0:cdf462088d13 | 347 | ( parity >> 2 ) ^ |
markrad | 0:cdf462088d13 | 348 | ( parity >> 3 ); |
markrad | 0:cdf462088d13 | 349 | parity &= 1; |
markrad | 0:cdf462088d13 | 350 | |
markrad | 0:cdf462088d13 | 351 | if( parity != 1 ) |
markrad | 0:cdf462088d13 | 352 | TEST_ASSERT( 0 ); |
markrad | 0:cdf462088d13 | 353 | } |
markrad | 0:cdf462088d13 | 354 | |
markrad | 0:cdf462088d13 | 355 | // Check the parity with the table |
markrad | 0:cdf462088d13 | 356 | // |
markrad | 0:cdf462088d13 | 357 | TEST_ASSERT( mbedtls_des_key_check_key_parity( key ) == 0 ); |
markrad | 0:cdf462088d13 | 358 | } |
markrad | 0:cdf462088d13 | 359 | } |
markrad | 0:cdf462088d13 | 360 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 361 | |
markrad | 0:cdf462088d13 | 362 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
markrad | 0:cdf462088d13 | 363 | void des_selftest() |
markrad | 0:cdf462088d13 | 364 | { |
markrad | 0:cdf462088d13 | 365 | TEST_ASSERT( mbedtls_des_self_test( 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 366 | } |
markrad | 0:cdf462088d13 | 367 | /* END_CASE */ |