quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

test_suite_des.function (7143B)


      1 /* BEGIN_HEADER */
      2 #include "mbedtls/des.h"
      3 /* END_HEADER */
      4 
      5 /* BEGIN_DEPENDENCIES
      6  * depends_on:MBEDTLS_DES_C
      7  * END_DEPENDENCIES
      8  */
      9 
     10 /* BEGIN_CASE */
     11 void des_check_weak(data_t *key, int ret)
     12 {
     13     TEST_ASSERT(mbedtls_des_key_check_weak(key->x) == ret);
     14 }
     15 /* END_CASE */
     16 
     17 /* BEGIN_CASE */
     18 void des_encrypt_ecb(data_t *key_str, data_t *src_str, data_t *dst)
     19 {
     20     unsigned char output[100];
     21     mbedtls_des_context ctx;
     22 
     23     memset(output, 0x00, 100);
     24     mbedtls_des_init(&ctx);
     25 
     26 
     27     TEST_ASSERT(mbedtls_des_setkey_enc(&ctx, key_str->x) == 0);
     28     TEST_ASSERT(mbedtls_des_crypt_ecb(&ctx, src_str->x, output) == 0);
     29 
     30     TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
     31 
     32 exit:
     33     mbedtls_des_free(&ctx);
     34 }
     35 /* END_CASE */
     36 
     37 /* BEGIN_CASE */
     38 void des_decrypt_ecb(data_t *key_str, data_t *src_str, data_t *dst)
     39 {
     40     unsigned char output[100];
     41     mbedtls_des_context ctx;
     42 
     43     memset(output, 0x00, 100);
     44     mbedtls_des_init(&ctx);
     45 
     46 
     47     TEST_ASSERT(mbedtls_des_setkey_dec(&ctx, key_str->x) == 0);
     48     TEST_ASSERT(mbedtls_des_crypt_ecb(&ctx, src_str->x, output) == 0);
     49 
     50     TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
     51 
     52 exit:
     53     mbedtls_des_free(&ctx);
     54 }
     55 /* END_CASE */
     56 
     57 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
     58 void des_encrypt_cbc(data_t *key_str, data_t *iv_str,
     59                      data_t *src_str, data_t *dst, int cbc_result)
     60 {
     61     unsigned char output[100];
     62     mbedtls_des_context ctx;
     63 
     64     memset(output, 0x00, 100);
     65     mbedtls_des_init(&ctx);
     66 
     67 
     68     TEST_ASSERT(mbedtls_des_setkey_enc(&ctx, key_str->x) == 0);
     69     TEST_ASSERT(mbedtls_des_crypt_cbc(&ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x,
     70                                       src_str->x, output) == cbc_result);
     71     if (cbc_result == 0) {
     72 
     73         TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
     74                                         dst->len) == 0);
     75     }
     76 
     77 exit:
     78     mbedtls_des_free(&ctx);
     79 }
     80 /* END_CASE */
     81 
     82 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
     83 void des_decrypt_cbc(data_t *key_str, data_t *iv_str,
     84                      data_t *src_str, data_t *dst,
     85                      int cbc_result)
     86 {
     87     unsigned char output[100];
     88     mbedtls_des_context ctx;
     89 
     90     memset(output, 0x00, 100);
     91     mbedtls_des_init(&ctx);
     92 
     93 
     94     TEST_ASSERT(mbedtls_des_setkey_dec(&ctx, key_str->x) == 0);
     95     TEST_ASSERT(mbedtls_des_crypt_cbc(&ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x,
     96                                       src_str->x, output) == cbc_result);
     97     if (cbc_result == 0) {
     98 
     99         TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
    100                                         dst->len) == 0);
    101     }
    102 
    103 exit:
    104     mbedtls_des_free(&ctx);
    105 }
    106 /* END_CASE */
    107 
    108 /* BEGIN_CASE */
    109 void des3_encrypt_ecb(int key_count, data_t *key_str,
    110                       data_t *src_str, data_t *dst)
    111 {
    112     unsigned char output[100];
    113     mbedtls_des3_context ctx;
    114 
    115     memset(output, 0x00, 100);
    116     mbedtls_des3_init(&ctx);
    117 
    118 
    119     if (key_count == 2) {
    120         TEST_ASSERT(mbedtls_des3_set2key_enc(&ctx, key_str->x) == 0);
    121     } else if (key_count == 3) {
    122         TEST_ASSERT(mbedtls_des3_set3key_enc(&ctx, key_str->x) == 0);
    123     } else {
    124         TEST_ASSERT(0);
    125     }
    126 
    127     TEST_ASSERT(mbedtls_des3_crypt_ecb(&ctx, src_str->x, output) == 0);
    128 
    129     TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
    130 
    131 exit:
    132     mbedtls_des3_free(&ctx);
    133 }
    134 /* END_CASE */
    135 
    136 /* BEGIN_CASE */
    137 void des3_decrypt_ecb(int key_count, data_t *key_str,
    138                       data_t *src_str, data_t *dst)
    139 {
    140     unsigned char output[100];
    141     mbedtls_des3_context ctx;
    142 
    143     memset(output, 0x00, 100);
    144     mbedtls_des3_init(&ctx);
    145 
    146 
    147     if (key_count == 2) {
    148         TEST_ASSERT(mbedtls_des3_set2key_dec(&ctx, key_str->x) == 0);
    149     } else if (key_count == 3) {
    150         TEST_ASSERT(mbedtls_des3_set3key_dec(&ctx, key_str->x) == 0);
    151     } else {
    152         TEST_ASSERT(0);
    153     }
    154 
    155     TEST_ASSERT(mbedtls_des3_crypt_ecb(&ctx, src_str->x, output) == 0);
    156 
    157     TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
    158 
    159 exit:
    160     mbedtls_des3_free(&ctx);
    161 }
    162 /* END_CASE */
    163 
    164 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
    165 void des3_encrypt_cbc(int key_count, data_t *key_str,
    166                       data_t *iv_str, data_t *src_str,
    167                       data_t *dst, int cbc_result)
    168 {
    169     unsigned char output[100];
    170     mbedtls_des3_context ctx;
    171 
    172     memset(output, 0x00, 100);
    173     mbedtls_des3_init(&ctx);
    174 
    175 
    176     if (key_count == 2) {
    177         TEST_ASSERT(mbedtls_des3_set2key_enc(&ctx, key_str->x) == 0);
    178     } else if (key_count == 3) {
    179         TEST_ASSERT(mbedtls_des3_set3key_enc(&ctx, key_str->x) == 0);
    180     } else {
    181         TEST_ASSERT(0);
    182     }
    183 
    184     TEST_ASSERT(mbedtls_des3_crypt_cbc(&ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x,
    185                                        src_str->x, output) == cbc_result);
    186 
    187     if (cbc_result == 0) {
    188 
    189         TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
    190                                         src_str->len, dst->len) == 0);
    191     }
    192 
    193 exit:
    194     mbedtls_des3_free(&ctx);
    195 }
    196 /* END_CASE */
    197 
    198 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
    199 void des3_decrypt_cbc(int key_count, data_t *key_str,
    200                       data_t *iv_str, data_t *src_str,
    201                       data_t *dst, int cbc_result)
    202 {
    203     unsigned char output[100];
    204     mbedtls_des3_context ctx;
    205 
    206     memset(output, 0x00, 100);
    207     mbedtls_des3_init(&ctx);
    208 
    209 
    210     if (key_count == 2) {
    211         TEST_ASSERT(mbedtls_des3_set2key_dec(&ctx, key_str->x) == 0);
    212     } else if (key_count == 3) {
    213         TEST_ASSERT(mbedtls_des3_set3key_dec(&ctx, key_str->x) == 0);
    214     } else {
    215         TEST_ASSERT(0);
    216     }
    217 
    218     TEST_ASSERT(mbedtls_des3_crypt_cbc(&ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x,
    219                                        src_str->x, output) == cbc_result);
    220 
    221     if (cbc_result == 0) {
    222 
    223         TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
    224                                         dst->len) == 0);
    225     }
    226 
    227 exit:
    228     mbedtls_des3_free(&ctx);
    229 }
    230 /* END_CASE */
    231 
    232 /* BEGIN_CASE */
    233 void des_key_parity_run()
    234 {
    235     int i, j, cnt;
    236     unsigned char key[MBEDTLS_DES_KEY_SIZE];
    237     unsigned int parity;
    238 
    239     memset(key, 0, MBEDTLS_DES_KEY_SIZE);
    240     cnt = 0;
    241 
    242     // Iterate through all possible byte values
    243     //
    244     for (i = 0; i < 32; i++) {
    245         for (j = 0; j < 8; j++) {
    246             key[j] = cnt++;
    247         }
    248 
    249         // Set the key parity according to the table
    250         //
    251         mbedtls_des_key_set_parity(key);
    252 
    253         // Check the parity with a function
    254         //
    255         for (j = 0; j < 8; j++) {
    256             parity = key[j] ^ (key[j] >> 4);
    257             parity = parity ^
    258                      (parity >> 1) ^
    259                      (parity >> 2) ^
    260                      (parity >> 3);
    261             parity &= 1;
    262 
    263             if (parity != 1) {
    264                 TEST_ASSERT(0);
    265             }
    266         }
    267 
    268         // Check the parity with the table
    269         //
    270         TEST_ASSERT(mbedtls_des_key_check_key_parity(key) == 0);
    271     }
    272 }
    273 /* END_CASE */
    274 
    275 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
    276 void des_selftest()
    277 {
    278     TEST_ASSERT(mbedtls_des_self_test(1) == 0);
    279 }
    280 /* END_CASE */