quickjs-tart

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

test_suite_psa_crypto_generate_key.function (1512B)


      1 /* BEGIN_HEADER */
      2 
      3 #include "psa/crypto.h"
      4 #include "test/psa_crypto_helpers.h"
      5 
      6 #define INVALID_KEY_ID mbedtls_svc_key_id_make(0, 0xfedcba98)
      7 
      8 /* END_HEADER */
      9 
     10 /* BEGIN_DEPENDENCIES
     11  * depends_on:MBEDTLS_PSA_CRYPTO_C
     12  * END_DEPENDENCIES
     13  */
     14 
     15 /* BEGIN_CASE */
     16 void generate_key(int key_type_arg, int bits_arg, int expected_status_arg)
     17 {
     18     psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
     19     mbedtls_svc_key_id_t key_id = INVALID_KEY_ID;
     20 
     21     // key lifetime, usage flags, algorithm are irrelevant for this test
     22     psa_key_type_t key_type = key_type_arg;
     23     size_t bits = bits_arg;
     24     psa_status_t expected_status = expected_status_arg;
     25 
     26     PSA_ASSERT(psa_crypto_init());
     27     psa_set_key_type(&attributes, key_type);
     28     psa_set_key_bits(&attributes, bits);
     29     TEST_EQUAL(psa_generate_key(&attributes, &key_id),
     30                expected_status);
     31 
     32     // Verify attributes of the created key on success
     33     if (expected_status == PSA_SUCCESS) {
     34         psa_reset_key_attributes(&attributes);
     35         PSA_ASSERT(psa_get_key_attributes(key_id, &attributes));
     36         TEST_EQUAL(psa_get_key_lifetime(&attributes), PSA_KEY_LIFETIME_VOLATILE);
     37         TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
     38         TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
     39         TEST_EQUAL(psa_get_key_type(&attributes), key_type);
     40         TEST_EQUAL(psa_get_key_bits(&attributes), bits);
     41     }
     42 
     43 exit:
     44     psa_reset_key_attributes(&attributes);
     45     psa_destroy_key(key_id);
     46     PSA_DONE();
     47 }
     48 /* END_CASE */