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.

CC2642R: 使用AES_CBC 加密 在AESWriteToKeyStore时出错

Part Number: CC2642R
Other Parts Discussed in Thread: SYSCONFIG

我使用cc2642,进行AES_CBC加密操作,按照sysconfig中的example,完成了初始化、设置参数等步骤,与例子使用相同的key、iv、input。

#include <ti/drivers/AESCBC.h>
#include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
...
AESCBC_Handle handle;
CryptoKey cryptoKey;
int_fast16_t encryptionResult;
// For example purposes only. Generate IVs in a non-static way in practice.
// Test vector 0 from NIST CAPV set CBCMMT128
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 keyingMaterial[16] =    {0x1f, 0x8e, 0x49, 0x73, 0x95, 0x3f, 0x3f, 0xb0,
                                 0xbd, 0x6b, 0x16, 0x66, 0x2e, 0x9a, 0x3c, 0x17};
// The ciphertext should be the following after the encryption operation:
//  0x0f, 0x61, 0xc4, 0xd4, 0x4c, 0x51, 0x47, 0xc0
//  0x3c, 0x19, 0x5a, 0xd7, 0xe2, 0xcc, 0x12, 0xb2
handle = AESCBC_open(0, NULL);
if (handle == NULL) {
    // handle error
}
CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
AESCBC_OneStepOperation operation;
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);
if (encryptionResult != AESCBC_STATUS_SUCCESS) {
    // handle error
}
AESCBC_close(handle);

在运行完encryptionResult = AESCBC_oneStepEncrypt(handle, &operation);后,结果是失败AESCBC_STATUS_ERROR状态,未能完成加密。

经过单步调试,发现程序跑到AESCBCCC26XX.c中的AESWriteToKeyStore(keyingMaterial, keyLength, AES_KEY_AREA_6) != AES_SUCCESS这个判断失败的。

/*
     * Load the key from RAM or flash into the key store at
     * a hardcoded and reserved location
     */
    if (AESWriteToKeyStore(keyingMaterial, keyLength, AES_KEY_AREA_6) != AES_SUCCESS)
    {
        return AESCBC_STATUS_ERROR;
    }

代码不能找到AESWriteToKeyStore这个函数(编译是通过的)。请问这个是SDK的问题还是算法使用设置的问题?

  • 查看AESCBCCC26XX.c,发现头文件中路径的aes.h是有

    #include DeviceFamily_constructPath(driverlib/aes.h)

    声明如下:

    //*****************************************************************************
    //
    //! \brief Transfer a key from main memory to a key area within the key store.
    //!
    //!     The crypto DMA transfers the key and function does not return until
    //!     the operation completes.
    //!     The keyStore can only contain valid keys of one \c aesKeyLength at
    //!     any one point in time. The keyStore cannot contain both 128-bit and
    //!     256-bit keys simultaneously. When a key of a different \c aesKeyLength
    //!     from the previous \c aesKeyLength is loaded, all previous keys are
    //!     invalidated.
    //!
    //! \param [in] aesKey Pointer to key. Does not need to be word-aligned.
    //!
    //! \param [in] aesKeyLength The key size in bytes.
    //!                          Currently, 128-bit, 192-bit, and 256-bit keys are supported.
    //! - \ref AES_128_KEY_LENGTH_BYTES
    //! - \ref AES_192_KEY_LENGTH_BYTES
    //! - \ref AES_256_KEY_LENGTH_BYTES
    //!
    //! \param [in] keyStoreArea The key store area to transfer the key to.
    //!                          When using 128-bit keys, only the specified key store
    //!                          area will be occupied.
    //!                          When using 256-bit or 192-bit keys, two consecutive
    //!                          key areas are used to store the key.
    //! - \ref AES_KEY_AREA_0
    //! - \ref AES_KEY_AREA_1
    //! - \ref AES_KEY_AREA_2
    //! - \ref AES_KEY_AREA_3
    //! - \ref AES_KEY_AREA_4
    //! - \ref AES_KEY_AREA_5
    //! - \ref AES_KEY_AREA_6
    //! - \ref AES_KEY_AREA_7
    //!
    //!     When using 256-bit or 192-bit keys, the 8 \c keyStoreArea's are
    //!     split into four sets of two. Selecting any \c keyStoreArea automatically
    //!     occupies the second \c keyStoreArea of the tuples below:
    //!
    //! - (\ref AES_KEY_AREA_0, \ref AES_KEY_AREA_1)
    //! - (\ref AES_KEY_AREA_2, \ref AES_KEY_AREA_3)
    //! - (\ref AES_KEY_AREA_4, \ref AES_KEY_AREA_5)
    //! - (\ref AES_KEY_AREA_6, \ref AES_KEY_AREA_7)
    //!
    //!     For example: if \c keyStoreArea == \ref AES_KEY_AREA_2,
    //!     both \ref AES_KEY_AREA_2 and \ref AES_KEY_AREA_3 are occupied.
    //!     If \c keyStoreArea == \ref AES_KEY_AREA_5, both \ref AES_KEY_AREA_4 and \ref AES_KEY_AREA_5 are occupied.
    //!
    //! \return Returns a status code depending on the result of the transfer.
    //!         If there was an error in the read process itself, an error is
    //!         returned.
    //!         Otherwise, a success code is returned.
    //! - \ref AES_KEYSTORE_ERROR
    //! - \ref AES_SUCCESS
    //!
    //! \sa AESReadFromKeyStore
    //
    //*****************************************************************************
    extern uint32_t AESWriteToKeyStore(const uint8_t *aesKey, uint32_t aesKeyLength, uint32_t keyStoreArea);

    在代码中直接查看AESWriteToKeyStore的定义是显示找不到的。

    运行时,返回了错误。我更换了另一个cc2642的硬件,问题仍在。

  • 在函数说明中看,似乎错误是因为in the read process itself,没有更多的提示了,read过程出了什么问题呢?

  • 您好,请提供一下您这边使用的sdk版本以及ccs版本号,提供您这边使用的是哪个demo

  • 感谢回复,我查到了我的问题了,使用加密时,我将整个流程做了个封装,传入的key在封装函数中使用sizeof计算长度,得到的是4(即key指针长度),并不是key数组的长度,导致计算出错

  • 不客气,有问题随时来论坛交流,谢谢