PSA import key / sign hash not working after SDK update

Hello,

I am currently updating from SDK v2.3.0 to v2.5.2 but I am not able to get the 'psa' functions working correctly (as they did with previous SDKs).

I am trying to import a private key, with oberon driver I get the error -134 = PSA_ERROR_NOT_SUPPORTED,
with cc3xx driver the import itself is working but the next operations (psa_export_public_key and/or psa_sign_hash) will fail with error -147 = PSA_ERROR_HARDWARE_FAILURE

The problem seems familiar to:
     nRF9160 RSA crypto changes from v2.3.0 to v2.4.0 SHA-1 signing
(I was also able to get error 135 in some cases) but the solution (to use CONFIG_PSA_CORE_BUILTIN=y) was removed in SDK v2.5.2

Initially I also thought it's because of the usage of a 512 bit key, which according to upper answer and to
    https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.4.1/nrfxlib/crypto/doc/nrf_oberon.html
is not supported anymore -> therefore we specially changed our server to 2048 bit, but results stayed the same.

I have experimented with variations of following settings, but neither combination was working:
   

CONFIG_CRYPTO=y
CONFIG_NRF_SECURITY=y
CONFIG_MBEDTLS_RSA_C=y
CONFIG_MBEDTLS_PSA_CRYPTO_C=y
CONFIG_PSA_WANT_ALG_RSA_PKCS1V15_SIGN=y
CONFIG_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR=y
CONFIG_PSA_WANT_ALG_SHA_256=y
CONFIG_PSA_WANT_RSA_KEY_SIZE_2048=y

CONFIG_PSA_CRYPTO_DRIVER_OBERON=n
CONFIG_PSA_CRYPTO_DRIVER_CC3XX=y

I am testing with following code:

STATUS_CODE rsa_import_prv_key(void)
{
	/* Configure the key attributes */
	psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
	psa_status_t status;
	size_t olen;

	int rc;
	unsigned char buffer[2048];
	size_t len;

	/* Configure the key attributes */
	psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_EXPORT);
	psa_set_key_lifetime(&key_attributes, PSA_KEY_LIFETIME_VOLATILE);
	psa_set_key_algorithm(&key_attributes, PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256));
	psa_set_key_type(&key_attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
	psa_set_key_bits(&key_attributes, RSA_KEY_BITS);

	rc = base64_decode(buffer, sizeof(buffer), &len, &CONF_API_PRIVATE_KEY[0], strlen(CONF_API_PRIVATE_KEY) );
	if(rc!=0) {
		LOG_ERR("Base64 decode error: %d", rc);
	}

//	PRINT_HEX("Base64 decoded", buffer, sizeof(buffer));

	status = psa_import_key(&key_attributes, buffer, len, &keypair_handle);
	if (status != PSA_SUCCESS) {
		LOG_INF("psa_import_key failed! (Error: %d)", status);
		return STATUS_ERROR;
	}

	/* Export the public key */
	status = psa_export_public_key(keypair_handle, m_pub_key, sizeof(m_pub_key), &olen);
	if (status != PSA_SUCCESS) {
		LOG_INF("psa_export_public_key failed! (Error: %d)", status);
		return STATUS_ERROR;
	}
	
//	PRINT_HEX("Second (exported) puclic key", m_pub_key, sizeof(m_pub_key));

	/* After the key handle is acquired the attributes are not needed */
	psa_reset_key_attributes(&key_attributes);

	return STATUS_OK;
}

And also tested some variations here (for example did I add the 'PSA_KEY_USAGE_EXPORT' flag due to an answer in the forum, this was not necessary in SDK2.3.0).

I was not able to find any solution, although I searched for a long time in the changelogs and in the forum, so I hope for some useful input...

Thank you, best regards,
Bernhard

  • Hi,

    Could you send a full log with the error messages -134 = PSA_ERROR_NOT_SUPPORTED and -147 = PSA_ERROR_HARDWARE_FAILURE?
    Which configurations did you have in both of these failing cases?

    Best regards,
    Dejan

  • Hello Dejan,

    this is my configuration for CC3XX driver:

    prj_sdk_a.conf

    and the console output:

    [00:00:26.890,411] <inf> rsa: psa_export_public_key failed! (Error: -147)
    [00:00:26.910,247] <inf> rsa: psa_sign_hash failed! (Error: -147)

    and this is my configuration for Oberon driver:

    prj_sdk_b.conf

    and the console output:

    [00:00:13.139,404] <inf> rsa: psa_import_key failed! (Error: -134)
    [00:00:13.159,606] <inf> rsa: psa_sign_hash failed! (Error: -136)

    Thanks and best regards,
    Bernhard

  • Hello Dejan,

    I have studied the documentation but it does not give any answers to my problem, the interface has not changed between the two SDK versions and from configuration point of view I have already tested quite a lot.

    Also I do not understand why one driver respons with 'not supported' to my key while it's ok for the other one, although should be supported by both. (If both would fail I would search more for a format error or something like that.)

    Here are some additional settings I have played with in between:

    CONFIG_MBEDTLS_HEAP_SIZE=16384
    
    CONFIG_CRYPTO=y
    
    CONFIG_NRF_SECURITY=y
    #CONFIG_NORDIC_SECURITY_BACKEND=y
    #CONFIG_CC3XX_BACKEND=y
    
    CONFIG_PSA_CRYPTO_DRIVER_OBERON=y
    CONFIG_PSA_CRYPTO_DRIVER_CC3XX=n
    
    CONFIG_MBEDTLS_PSA_CRYPTO_C=y
    CONFIG_MBEDTLS_USE_PSA_CRYPTO=y
    
    CONFIG_MBEDTLS_RSA_C=y
    CONFIG_MBEDTLS_SHA256_C=y
    CONFIG_MBEDTLS_AES_C=y
    CONFIG_MBEDTLS_PKCS1_V15=y
    
    CONFIG_MBEDTLS_PK_PARSE_C=y
    CONFIG_MBEDTLS_TLS_LIBRARY=y
    
    CONFIG_PSA_WANT_ALG_RSA_PKCS1V15_SIGN=y
    CONFIG_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR=y
    CONFIG_PSA_WANT_ALG_SHA_256=y
    CONFIG_PSA_WANT_RSA_KEY_SIZE_2048=y
    
    # test only
    CONFIG_PSA_WANT_ALG_HKDF=y
    CONFIG_PSA_WANT_ALG_PBKDF2_HMAC=y
    CONFIG_PSA_WANT_ALG_TLS12_PRF=y
    CONFIG_PSA_WANT_ALG_TLS12_PSK_TO_MS=y
    CONFIG_PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS=y

    and for CC3XX driver additionaly:

    CONFIG_PSA_USE_CC3XX_SIGNATURE_DRIVER=y
    CONFIG_PSA_USE_CC3XX_HASH_DRIVER=y
    CONFIG_PSA_USE_CC3XX_KEY_PAIR_DRIVER=y
    CONFIG_PSA_USE_CC3XX_CIPHER_DRIVER=y
    CONFIG_PSA_USE_CC3XX_ASYMMETRIC_DRIVER=y

    I also tryd now to switch to SDK v2.6.0 but the device was not booting up anymore, it is really annoying that the SDK versions are not an upgrade from one to each other but rather just a change.

    Hope you could help me anyway.

    Best regards,
    Bernhard

  • Hi Bernhard,

    Could you provide both configuration files and relevant code (as you did in your initial post) when you had everything working as expected?

    What was the most recent NCS version which you used and did not get any issues?

    Best regards,
    Dejan

Related