Skip to main content
BLEuser
Visitor II
February 19, 2022
Question

BlueNRG-LP: API for AES decryption?

  • February 19, 2022
  • 1 reply
  • 734 views

The chip has an API for the AES encryption HW, but I cannot find any access option to the AES decryption HW.

    This topic has been closed for replies.

    1 reply

    Sebastien DENOUAL
    ST Employee
    February 21, 2022

    Hi @BLEuser​ ,

    It exists a specific API for this :

    tBleStatus hci_le_encrypt(uint8_t Key[16],

                 uint8_t Plaintext_Data[16],

                 uint8_t Encrypted_Data[16]);

    This API can be used to request controller to encrypt Plaintext_Data iusing the Key given in the command

     *    and returns the Encrypted_Data to the Host

    Here below is a copy/past of a code example where data is encrypted using AES engine with hci_le_encrypt and then decrypted with sw decryption using crypo lib.

    Particular attention must be paid to the endianness: hci_le_encrypt uses data in little endian format, while cryptolib uses big endian format.

    uint8_t key_le[16] = {0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00};

    uint8_t key_be[16];

    uint8_t plaintext_data_le[16] = {0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00};

    uint8_t plaintext_data_be[16];

    uint8_t encrypted_data_le[16];

    uint8_t encrypted_data_be[16];

    int32_t length;

    int32_t ret_val;

    AESECBctx_stt AESECBctx;

    AESECBctx.mKeySize = CRL_AES128_KEY;

    AESECBctx.mFlags = E_SK_DEFAULT;

    hci_le_encrypt(key_le, plaintext_data_le, encrypted_data_le);

    for(int i = 0; i < 16; i++)

    {

    key_be[i] = key_le[15 - i];

    }

    for(int i = 0; i < 16; i++)

    {

    plaintext_data_be[i] = plaintext_data_le[15-i];

    }

    for(int i = 0; i < 16; i++)

    {

    encrypted_data_be[i] = encrypted_data_le[15-i];

    }

    for(int i = 0; i < sizeof(encrypted_data_le); i++)

    printf("%02X ",encrypted_data_be[i]);

    printf("\n");

    ret_val = AES_ECB_Decrypt_Init(&AESECBctx, key_be, NULL);

    if(ret_val != AES_SUCCESS)

    {

    printf("AES_ECB_Decrypt_Init 0x%02X\n",ret_val);

    while(1);

    }

    ret_val = AES_ECB_Decrypt_Append(&AESECBctx, encrypted_data_be, sizeof(encrypted_data_be), plaintext_data_be, &length);

    if(ret_val != AES_SUCCESS)

    {

    printf("AES_ECB_Decrypt_Append 0x%02X\n",ret_val);

    while(1);

    }

    for(int i = 0; i < sizeof(plaintext_data_be); i++)

    printf("%02X ",plaintext_data_be[i]);

    printf("\n");

    ret_val = AES_ECB_Decrypt_Finish(&AESECBctx, plaintext_data_be, &length);

    if(ret_val != AES_SUCCESS)

    {

    printf("AES_ECB_Decrypt_Finish 0x%02X\n",ret_val);

    while(1);

    }

    Hope it helps.

    Regards,

    Sebastien.