How to use psa_import_key correctly

88 Views Asked by At

I need to import a JSON element which in this case is public_key_alice I store it in an array called keyp

const char *cla = cJSON_GetArrayItem(public_key_alice, 0);
if (cJSON_IsString(cla))
                        {
                            const char *keyp= cJSON_GetStringValue(cla);
                            uint8_t byte_array[65]; 
                            psa_key_handle_t key;
                            ESP_LOGI(TAG, "Public %s", keyp);
                            printf("%s \n",keyp);

Previously I have declared a variable called key_alice which is where I am going to store it.

uint8_t key_alice[65];

An example of what keyp would contain would be

04f69591c8dbdada28997766390c9f983e0a63cfd33b4f2b54b45d921f825c97984dae23354f9e8d0148b6206954faf684a860a2285a17a1686b23e13abf19de81

I use this to transform from hexadecimal to bytes so I can use it later.

for (size_t i = 0; i < sizeof(key_alice); i++)
                            {
                                sscanf(clavep + (2 * i), "%2hhx", &key_alice[i]);
                            }

And I call psa_import_key but I get the following error

PSA_ERROR_INVALID_ARGUMENT

The attributes I use are as follows

psa_key_attributes_t attributes;
attributes = psa_key_attributes_init();
    psa_set_key_usage_flags(&attributes,PSA_KEY_USAGE_DERIVE);
    psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_VOLATILE);
    psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
    psa_set_key_type(&attributes,PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
    psa_set_key_bits(&attributes,256);

How do I fix the error?

psa_impor_key needs to be output as succes for later use to generate a shared key.

0

There are 0 best solutions below