This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] CC2745R10-Q1:使用 PSA 密钥库的加密操作

Guru**** 2328790 points
Other Parts Discussed in Thread: CC2745R10-Q1, SYSCONFIG, AES-128
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/wireless-connectivity/bluetooth-group/bluetooth/f/bluetooth-forum/1499014/cc2745r10-q1-cryptographic-operations-using-psa-keystore

器件型号:CC2745R10-Q1
主题中讨论的其他器件: SysConfigAES-128

工具/软件:

当我尝试使用以下源代码执行加密操作时、PSA_CRYPTO ()的返回值为-147 (PSA_ERROR_HARDWARE_FAILURE)。
之后、PSA_IMPORT_KEY()的返回值变为-141 (PSA_ERROR_INSUFFICIENT_MEMORY )、并且无法导入。 原因是什么?如何解决?
该代码重写了示例代码 empty.c

所使用的环境如下所示:
・Bord:CC2745R10-Q1
μ・调试器:XDS110 SDK:SimpleLink Lowpower f3 ver.8.40.2
・IDE : Code Composer Studio v.12.8.1.
・syscongig :见下文

 

#include <ti/drivers/cryptoutils/cryptokey/CryptoKeyKeyStore_PSA.h>
#include <ti/drivers/cryptoutils/cryptokey/CryptoKeyKeyStore_PSA_init.h>
#include <ti/drivers/cryptoutils/cryptokey/CryptoKeyKeyStore_PSA_helpers.h>

#include <ti/drivers/AESCBC.h>

void *mainThread(void *arg0)
{
    uint8_t keyingMaterial[16] = {0x1f, 0x8e, 0x49, 0x73, 0x95, 0x3f, 0x3f, 0xb0,
                                  0xbd, 0x6b, 0x16, 0x66, 0x2e, 0x9a, 0x3c, 0x17};
    uint8_t KeyExportData[16] = {0};
    size_t ExportDataLength;
    CryptoKey cryptoKey;
    mbedtls_svc_key_id_t keyID;
    int_fast16_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;

    AESCBC_Handle handle;
    AESCBC_OneStepOperation operation;
    int_fast16_t encryptionResult;

    uint8_t iv[16] =                {0x2f, 0xe2, 0xb3, 0x33, 0xce, 0xda, 0x8f, 0x98,
                                     0xf4, 0xa9, 0x9b, 0x40, 0xd2, 0xcd, 0x34, 0xa8};
    uint8_t plaintext[16] =         {0x45, 0xcf, 0x12, 0x96, 0x4f, 0xc8, 0x24, 0xab,
                                     0x76, 0x61, 0x6a, 0xe2, 0xf4, 0xbf, 0x08, 0x22};
    uint8_t ciphertext[sizeof(plaintext)];

    uint8_t errFlg = 0;


    status = psa_crypto_init();

    // Assign key attributes
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
    psa_set_key_algorithm(&attributes, PSA_ALG_CBC_NO_PADDING);
    psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
    psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT);
    psa_set_key_bits(&attributes, 128);
    // Set key ID
    GET_KEY_ID(keyID, PSA_KEY_ID_USER_MIN);
    psa_set_key_id(&attributes, keyID);
    // Import the keyingMaterial
    status = psa_import_key(&attributes, keyingMaterial, sizeof(keyingMaterial), &keyID);
    if (status != PSA_SUCCESS)
    {
        errFlg = 1;
    }

    // Initialize cryptoKey for crypto operations
    KeyStore_PSA_initKeyHSM(&cryptoKey, keyID, sizeof(keyingMaterial), NULL);
    // Use the cryptoKey for AESCCM operations

    handle = AESCBC_open(0, NULL);
    AESCBC_OneStepOperation_init(&operation);
    operation.key               = &cryptoKey;
    operation.input             = plaintext;
    operation.output            = ciphertext;
    operation.inputLength       = sizeof(plaintext);
    operation.iv                = iv;
    encryptionResult = AESCBC_oneStepEncrypt(handle, &operation);
    AESCBC_close(handle);

    status = psa_export_key(keyID, KeyExportData, 16, &ExportDataLength);

    status = psa_import_key(&attributes, KeyExportData, sizeof(keyingMaterial), &keyID);

    handle = AESCBC_open(0, NULL);
    AESCBC_OneStepOperation_init(&operation);
    operation.key               = &cryptoKey;
    operation.input             = plaintext;
    operation.output            = ciphertext;
    operation.inputLength       = sizeof(plaintext);
    operation.iv                = iv;
    encryptionResult = AESCBC_oneStepEncrypt(handle, &operation);
    AESCBC_close(handle);

}

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好:

    因此、您提供的代码中需要注意以下几点:

    1. 不应将 PSA 和 SimpleLink 驱动程序混合使用。 例如、您使用的是 PSA、但我也看到了 AESCBC_Handle 和 AESCBC_OneStepOperation 类型、这些类型都来自 SimpleLink 驱动程序。 PSA 实际上是围绕 SimpleLink 加密驱动器的包装器、因此可能会导致一些问题。

    2.不能直接调用密钥库 API。 需要使用 PSA 处理所有密钥管理。

    Unknown 说:
    当我尝试使用以下源代码执行加密操作时、PSA_crypto_init ()的返回值为-147 (PSA_ERROR_HARDWARE_FAILURE)。

    这可能是因为 HSM 固件未加载到器件上。 加载 HSM 固件、并告诉我它是否仍然失败。

    Unknown 说:
    之后、PSA_IMPORT_KEY ()的返回值变为-141 (PSA_ERROR_INSUFFICIENT_MEMORY )、无法导入。 原因是什么?如何解决?

    这也可能是因为上面的错误。 您是否会向我发送有关 PSA +密钥库的 SysConfig 设置快照?

    此外、SDK 中确实有一个示例显示了 psaAeadEncryption: \examples\rtos\LP_EM_CC2745R10-Q1\drivers\psaAeadEncrypt

    此致、

    Nima Behmanesh

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好:

    No. 1.
    我之所以这样说、是因为有人说 AES-CBC 不支持使用 PSA 的 API 的操作。
    如果不应混淆 SimpleLink 和 PSA API、这是否意味着无法将用于 AES-CBC 操作的密钥存储在 PSA 密钥库中?

    第2号
    我原以为我要为所有内容调用 PSA API、但此源代码中是否有地方直接调用密钥库 API?

    我尝试加载 HSM、但仍然失败。
    误差内容也相同。
    请让我知道我可以采取哪些其他措施。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好:

    No. 1.
    我之所以这样说、是因为有人说 AES-CBC 不支持使用 PSA 的 API 的操作。
    如果不应混淆 SimpleLink 和 PSA API、这是否意味着无法将用于 AES-CBC 操作的密钥存储在 PSA 密钥库中?

    您使用的是哪个 SDK 版本? 稍后添加了支持、因此这些发行说明/文档可能是旧的。

    No. 2.
    我以为我在为所有内容调用 PSA API、但这个源代码中是否有地方直接调用密钥库 API?

    是的、在提供的代码的第50行、您正在调用密钥库 API。

    我编写了以下代码、使用 PSA 执行 AES-CBC 操作。 在运行该工程之前、需要对该工程的 SysConfig 文件进行一些更改:

    需要更改的只是易失性 AES 密钥的数量。 请注意、我将 AES-128明文密钥更改为  1.我看到您使用的是持久存储、SysConfig 将进行更新、使其显示为易失性/非易失性。  

    /* For usleep() */
    #include <unistd.h>
    #include <stdint.h>
    #include <stddef.h>
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <third_party/psa_crypto/include/psa/crypto.h>
    #include <ti/drivers/cryptoutils/hsm/HSMLPF3.h>
    
    /* Driver configuration */
    #include "ti_drivers_config.h"
    
    #define KEY_LIFETIME PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(PSA_KEY_PERSISTENCE_DEFAULT, PSA_KEY_LOCATION_LOCAL_STORAGE)
    
    uint8_t keyingMaterial[16] = { 0x1f, 0x8e, 0x49, 0x73, 0x95, 0x3f, 0x3f, 0xb0,
                                   0xbd, 0x6b, 0x16, 0x66, 0x2e, 0x9a, 0x3c, 0x17
                                 };
    uint8_t iv[16] =                {0x2f, 0xe2, 0xb3, 0x33, 0xce, 0xda, 0x8f, 0x98,
                                     0xf4, 0xa9, 0x9b, 0x40, 0xd2, 0xcd, 0x34, 0xa8};
    
    uint8_t plaintext[16] =         {0x45, 0xcf, 0x12, 0x96, 0x4f, 0xc8, 0x24, 0xab,
                                     0x76, 0x61, 0x6a, 0xe2, 0xf4, 0xbf, 0x08, 0x22};
    
    uint8_t ciphertext[sizeof(plaintext)];
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        psa_status_t status;
        psa_key_id_t key_id;
        int_fast16_t ret;
    
        status = psa_crypto_init();
        if (status != PSA_SUCCESS)
        {
            while(1);
        }
    
        ret = HSMLPF3_provisionHUK();
        if (ret != HSMLPF3_STATUS_SUCCESS)
        {
            while(1);
        }
    
    
        psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    
        psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
        psa_set_key_algorithm(&attributes, PSA_ALG_CBC_NO_PADDING);
        psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
    
        psa_set_key_lifetime(&attributes, KEY_LIFETIME);
        psa_set_key_bits(&attributes, 128);
    
        key_id = PSA_KEY_ID_USER_MIN;
        psa_set_key_id(&attributes, key_id);
    
        status = psa_import_key(&attributes, keyingMaterial, sizeof(keyingMaterial), &key_id);
        if (status != PSA_SUCCESS)
        {
            while(1);
        }
    
        psa_cipher_operation_t op = PSA_CIPHER_OPERATION_INIT;
        status = psa_cipher_encrypt_setup(&op, key_id, PSA_ALG_CBC_NO_PADDING);
        if (status != PSA_SUCCESS)
        {
            while(1);
        }
    
        status = psa_cipher_set_iv(&op, iv, sizeof(iv));
        if (status != PSA_SUCCESS)
        {
            while(1);
        }
    
        size_t cipher_length = 0;
    
        status = psa_cipher_update(&op,
                                   plaintext,
                                   sizeof(plaintext),
                                   ciphertext,
                                   sizeof(plaintext),
                                   &cipher_length);
    
        if (status != PSA_SUCCESS)
        {
            while(1);
        }
    
        status = psa_cipher_finish(&op,
                                   ciphertext,
                                   sizeof(plaintext),
                                   &cipher_length);
        if (status != PSA_SUCCESS)
        {
            while(1);
        }
    
        while(1);
    }
    
    

    如果您尝试上述代码、您仍然会看到硬件故障吗?  

    此致、

    Nima Behmanesh

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    非常感谢。

    我能够使用您提供的源代码执行加密操作。