quickjs-tart

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

test_suite_psa_crypto_low_hash.function (7867B)


      1 /* BEGIN_HEADER */
      2 /*
      3  * Test suite for the PSA hash built-in driver
      4  *
      5  * This test suite exercises some aspects of the built-in PSA driver for
      6  * hash algorithms (psa_crypto_hash.c). This code is mostly tested via
      7  * the application interface (above the PSA API layer) and via tests of
      8  * individual hash modules. The goal of this test suite is to ensure that
      9  * the driver dispatch layer behaves correctly even when not invoked via
     10  * the API layer, but directly from another driver.
     11  *
     12  * This test suite is currently incomplete. It focuses on non-regression
     13  * tests for past bugs or near misses.
     14  */
     15 
     16 #include <psa_crypto_hash.h>
     17 
     18 /* END_HEADER */
     19 
     20 /* BEGIN_DEPENDENCIES
     21  * depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_PSA_BUILTIN_HASH
     22  * END_DEPENDENCIES
     23  */
     24 
     25 /* BEGIN_CASE */
     26 void hash_valid_one_shot(int alg_arg, data_t *input,
     27                          data_t *expected)
     28 {
     29     psa_algorithm_t alg = alg_arg;
     30     uint8_t *output = NULL;
     31     size_t output_size = expected->len;
     32     size_t length = SIZE_MAX;
     33 
     34     /* Nominal case */
     35     ASSERT_ALLOC(output, output_size);
     36     TEST_EQUAL(mbedtls_psa_hash_compute(alg, input->x, input->len,
     37                                         output, output_size, &length),
     38                PSA_SUCCESS);
     39     ASSERT_COMPARE(expected->x, expected->len, output, length);
     40     mbedtls_free(output);
     41     output = NULL;
     42 
     43     /* Larger output buffer */
     44     output_size = expected->len + 1;
     45     ASSERT_ALLOC(output, output_size);
     46     TEST_EQUAL(mbedtls_psa_hash_compute(alg, input->x, input->len,
     47                                         output, output_size, &length),
     48                PSA_SUCCESS);
     49     ASSERT_COMPARE(expected->x, expected->len, output, length);
     50     mbedtls_free(output);
     51     output = NULL;
     52 
     53     /* We don't test with a smaller output buffer because this isn't
     54      * guaranteed to work: the core must pass a sufficiently large
     55      * output buffer to the driver. */
     56 
     57 exit:
     58     mbedtls_free(output);
     59 }
     60 /* END_CASE */
     61 
     62 /* BEGIN_CASE */
     63 void hash_valid_multipart(int alg_arg,
     64                           data_t *input1, data_t *expected1,
     65                           data_t *input2, data_t *expected2)
     66 {
     67     psa_algorithm_t alg = alg_arg;
     68     uint8_t *output = NULL;
     69     size_t output_size = expected1->len;
     70     size_t length = SIZE_MAX;
     71     mbedtls_psa_hash_operation_t operation0; // original
     72     memset(&operation0, 0, sizeof(operation0));
     73     mbedtls_psa_hash_operation_t clone_start; // cloned after setup
     74     memset(&clone_start, 0, sizeof(clone_start));
     75     mbedtls_psa_hash_operation_t clone_middle; // cloned between updates
     76     memset(&clone_middle, 0, sizeof(clone_middle));
     77     mbedtls_psa_hash_operation_t clone_end; // cloned before finish
     78     memset(&clone_end, 0, sizeof(clone_end));
     79     mbedtls_psa_hash_operation_t clone_more; // cloned before finish
     80     memset(&clone_more, 0, sizeof(clone_more));
     81 
     82     /* Nominal case with two update calls */
     83     ASSERT_ALLOC(output, output_size);
     84     TEST_EQUAL(mbedtls_psa_hash_setup(&operation0, alg),
     85                PSA_SUCCESS);
     86     TEST_EQUAL(mbedtls_psa_hash_clone(&operation0, &clone_start),
     87                PSA_SUCCESS);
     88     TEST_EQUAL(mbedtls_psa_hash_update(&operation0, input1->x, input1->len),
     89                PSA_SUCCESS);
     90     TEST_EQUAL(mbedtls_psa_hash_clone(&operation0, &clone_middle),
     91                PSA_SUCCESS);
     92     TEST_EQUAL(mbedtls_psa_hash_update(&operation0, input2->x, input2->len),
     93                PSA_SUCCESS);
     94     TEST_EQUAL(mbedtls_psa_hash_clone(&operation0, &clone_end),
     95                PSA_SUCCESS);
     96     TEST_EQUAL(mbedtls_psa_hash_finish(&operation0,
     97                                        output, output_size, &length),
     98                PSA_SUCCESS);
     99     ASSERT_COMPARE(expected2->x, expected2->len, output, length);
    100 
    101     /* Nominal case with an operation cloned after setup */
    102     memset(output, 0, output_size);
    103     TEST_EQUAL(mbedtls_psa_hash_update(&clone_start, input1->x, input1->len),
    104                PSA_SUCCESS);
    105     TEST_EQUAL(mbedtls_psa_hash_finish(&clone_start,
    106                                        output, output_size, &length),
    107                PSA_SUCCESS);
    108     ASSERT_COMPARE(expected1->x, expected1->len, output, length);
    109 
    110     /* Nominal case with an operation cloned between updates */
    111     memset(output, 0, output_size);
    112     TEST_EQUAL(mbedtls_psa_hash_update(&clone_middle, input2->x, input2->len),
    113                PSA_SUCCESS);
    114     TEST_EQUAL(mbedtls_psa_hash_finish(&clone_middle,
    115                                        output, output_size, &length),
    116                PSA_SUCCESS);
    117     ASSERT_COMPARE(expected2->x, expected2->len, output, length);
    118 
    119     /* Nominal case with an operation cloned before finish */
    120     TEST_EQUAL(mbedtls_psa_hash_clone(&clone_end, &clone_more),
    121                PSA_SUCCESS);
    122     memset(output, 0, output_size);
    123     TEST_EQUAL(mbedtls_psa_hash_finish(&clone_end,
    124                                        output, output_size, &length),
    125                PSA_SUCCESS);
    126     ASSERT_COMPARE(expected2->x, expected2->len, output, length);
    127     mbedtls_free(output);
    128     output = NULL;
    129 
    130     /* Larger output buffer */
    131     TEST_EQUAL(mbedtls_psa_hash_clone(&clone_more, &clone_end),
    132                PSA_SUCCESS);
    133     output_size = expected2->len + 1;
    134     ASSERT_ALLOC(output, output_size);
    135     TEST_EQUAL(mbedtls_psa_hash_finish(&clone_end,
    136                                        output, output_size, &length),
    137                PSA_SUCCESS);
    138     ASSERT_COMPARE(expected2->x, expected2->len, output, length);
    139     mbedtls_free(output);
    140     output = NULL;
    141 
    142     /* We don't test with a smaller output buffer because this isn't
    143      * guaranteed to work: the core must pass a sufficiently large
    144      * output buffer to the driver. */
    145 
    146     /* Nominal case again after an error in a cloned operation */
    147     output_size = expected2->len;
    148     ASSERT_ALLOC(output, output_size);
    149     TEST_EQUAL(mbedtls_psa_hash_finish(&clone_more,
    150                                        output, output_size, &length),
    151                PSA_SUCCESS);
    152     ASSERT_COMPARE(expected2->x, expected2->len, output, length);
    153     mbedtls_free(output);
    154     output = NULL;
    155 
    156 exit:
    157     mbedtls_free(output);
    158     mbedtls_psa_hash_abort(&operation0);
    159     mbedtls_psa_hash_abort(&clone_start);
    160     mbedtls_psa_hash_abort(&clone_middle);
    161     mbedtls_psa_hash_abort(&clone_end);
    162     mbedtls_psa_hash_abort(&clone_more);
    163 }
    164 /* END_CASE */
    165 
    166 /* BEGIN_CASE */
    167 void hash_empty(int alg_arg, data_t *expected)
    168 {
    169     psa_algorithm_t alg = alg_arg;
    170     uint8_t *output = NULL;
    171     size_t output_size = expected->len;
    172     size_t length = SIZE_MAX;
    173     mbedtls_psa_hash_operation_t operation;
    174     memset(&operation, 0, sizeof(operation));
    175 
    176     ASSERT_ALLOC(output, output_size);
    177 
    178     /* One-shot */
    179     TEST_EQUAL(mbedtls_psa_hash_compute(alg, NULL, 0,
    180                                         output, output_size, &length),
    181                PSA_SUCCESS);
    182     ASSERT_COMPARE(expected->x, expected->len, output, length);
    183 
    184     /* Multipart, no update */
    185     memset(output, 0, output_size);
    186     TEST_EQUAL(mbedtls_psa_hash_setup(&operation, alg),
    187                PSA_SUCCESS);
    188     TEST_EQUAL(mbedtls_psa_hash_finish(&operation,
    189                                        output, output_size, &length),
    190                PSA_SUCCESS);
    191     ASSERT_COMPARE(expected->x, expected->len, output, length);
    192 
    193     /* Multipart, one update */
    194     memset(output, 0, output_size);
    195     memset(&operation, 0, sizeof(operation));
    196     TEST_EQUAL(mbedtls_psa_hash_setup(&operation, alg),
    197                PSA_SUCCESS);
    198     TEST_EQUAL(mbedtls_psa_hash_update(&operation, NULL, 0),
    199                PSA_SUCCESS);
    200     TEST_EQUAL(mbedtls_psa_hash_finish(&operation,
    201                                        output, output_size, &length),
    202                PSA_SUCCESS);
    203     ASSERT_COMPARE(expected->x, expected->len, output, length);
    204 
    205 exit:
    206     mbedtls_free(output);
    207     mbedtls_psa_hash_abort(&operation);
    208 }
    209 /* END_CASE */