主题中讨论的其他器件: SysConfig、 AES-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); }