This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf_crypto_shared_secret_compute fails with code 9

Hi,

currently, I'm working on encryption using the crypto module of SDK 12.1.0 for SoftDevice 3.0.0

It is no problem to generate a public key, but if I want to generate the secret, the function nrf_crypto_shared_secret_compute returns with error code 9, i.e. NRF_ERROR_INVALID_LENGTH. From documentation:

NRF_ERROR_INVALID_LENGTH: If the length of any of the provided keys is invalid or the shared secret is bigger than the size of the provided buffer.

Furthermore, the correct lengths of the keys and the secret are given in the documentation, where the curve length for NIST-256 should have 256 bits, i.e. 32 bytes.

  • Public key: 2 * curve length = 64 bytes
  • Private key: 1 * curve length = 32 bytes
  • Shared secret: 2 * curve length = 64 bytes

The used code is as follows

memset(m_secret_key, 0, sizeof(m_secret_key));

uint32_t err_code;
err_code = nrf_crypto_shared_secret_compute(NRF_CRYPTO_CURVE_SECP256R1, &m_crypto_private_key, &m_crypto_public_key, &m_crypto_secret_key);

if (err_code != NRF_SUCCESS)
{
    NRF_LOG_ERROR("nrf_crypto_shared_secret_compute failed (code: %d)\r\n", err_code);
    return err_code;
}

The used arrays are declared as follows

uint8_t m_private_key[32]; // is a random key
uint8_t m_public_key[64]; // has been computed
uint8_t m_secret_key[64];

nrf_crypto_key_t m_crypto_private_key = {
    .p_le_data = m_private_key,
    .len = sizeof(m_private_key)
};
nrf_crypto_key_t m_crypto_public_key = {
    .p_le_data = m_public_key,
    .len = sizeof(m_public_key)
};
nrf_crypto_key_t m_crypto_secret_key = {
    .p_le_data = m_secret_key,
    .len = sizeof(m_secret_key)
};

So, it should work but it does not. Can anybody help me with this problem?

Thank you and best regards!

Related