psa_cipher_encrypt_setup() failed with error code -133

I tested AES CBC encryption/decryption on nRF5340 device. But I encountered psa_cipher_encrypt_setup() failed with error code -133. What is meaning of error code -133 and how to fix this issue? Why policy_alg and requested_alg parameters of psa_key_algorithm_permits() are not same?

int nrfAes128Encrypt(void) {
    uint32_t olen;
    psa_status_t status;
    psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;

    printk("Encrypting using AES CBC MODE...\n");

    /* Setup the encryption operation */
    status = psa_cipher_encrypt_setup(&operation, _gAes128KeyHandle, PSA_ALG_CBC_NO_PADDING);
    if (status != PSA_SUCCESS) {
        printk("psa_cipher_encrypt_setup() failed! (Error: %d)\n", status);
        return ERROR_AES_ENCRYPT_FAIL;
    }

    /* Generate an IV */
    status = psa_cipher_generate_iv(&operation, _gIv, sizeof(_gIv), &olen);
    if (status != PSA_SUCCESS) {
        printk("psa_cipher_generate_iv() failed! (Error: %d)\n", status);
        return ERROR_AES_ENCRYPT_FAIL;
    }

    /* Perform the encryption */
    status = psa_cipher_update(&operation, m_plain_text,
                   sizeof(m_plain_text), m_encrypted_text,
                   sizeof(m_encrypted_text), &olen);
    if (status != PSA_SUCCESS) {
        printk("psa_cipher_update() failed! (Error: %d)\n", status);
        return ERROR_AES_ENCRYPT_FAIL;
    }

    /* Finalize the encryption */
    status = psa_cipher_finish(&operation, m_encrypted_text + olen,
                   sizeof(m_encrypted_text) - olen,
                   &olen);
    if (status != PSA_SUCCESS) {
        printk("psa_cipher_finish() failed! (Error: %d)\n", status);
        return ERROR_AES_ENCRYPT_FAIL;
    }

    printk("Encryption successful!\n");
    PRINT_HEX("IV", _gIv, sizeof(_gIv));
    PRINT_HEX("Plaintext", m_plain_text, sizeof(m_plain_text));
    PRINT_HEX("Encrypted text", m_encrypted_text, sizeof(m_encrypted_text));

    /* Clean up cipher operation context */
    psa_cipher_abort(&operation);

    return 0;
}

The following are log messages of RTT Viewer.

00> *** Booting Zephyr OS build v3.3.99-ncs1 ***
00> btInit() e
00> btInit() x
00> Starting AES-CBC-NO-PADDING example...
00> Encrypting using AES CBC MODE...
00> Importing AES key...
00> psa_import_key() nrf e
00> psa_cipher_encrypt_setup() nrf e
00> psa_cipher_setup() nrf e
00> psa_key_algorithm_permits() e key_type=9216, policy_alg=71320576, requested_alg=71319552
00> psa_key_algorithm_permits() e key_type=9216, policy_alg=0, requested_alg=71319552
00> psa_key_policy_permits() failed -133
00> psa_get_and_lock_key_slot_with_policy() failed -133
00> psa_cipher_setup() return -133
00> psa_cipher_encrypt_setup() failed! (Error: -133)

Procedures to reproduce this issue are as follows.

1. Unzipping minimal_log_aes.zip to D:\ncs\v2.4.0\nrf\samples\ folder.

2. cd D:\ncs\v2.4.0\nrf\samples\minimal_log_aes

3. west build -b nrf5340dk_nrf5340_cpuapp

4. west flash

5. Rebooting nRF5340 device and connecting to RTT Viewer.

Related