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.

LP-AM243: About Encryption Performance

Part Number: LP-AM243

Tool/software:

Hi,

I modified the sample program and measured the time it took to encrypt 65KB of data in three parts.

examples\security\crypto\sa2ul_aes\crypto_aes_cbc_256

The third encryption took the longest.



Why does it take so long even though it is the smallest size?

Regards,
Yukinobu

  • Hi Yukinobu,

    Can you please share the "crypto_aes_cbc_256.c" source file you are using to profile the code?

    Thanks!

  • Hi Prashant,

    The source file cannot be shared due to an error.




    Is there any way to share them?

    Regards,
    Yukinobu

  • Hi Yukinobu,

    You could copy the file contents & attach it as text using "Insert -> Code".

  • Hi Prashant,

    The source file contents are attached.

    /*
    * Copyright (C) 2021 Texas Instruments Incorporated
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions
    * are met:
    *
    * Redistributions of source code must retain the above copyright
    * notice, this list of conditions and the following disclaimer.
    *
    * Redistributions in binary form must reproduce the above copyright
    * notice, this list of conditions and the following disclaimer in the
    * documentation and/or other materials provided with the
    * distribution.
    *
    * Neither the name of Texas Instruments Incorporated nor the names of
    * its contributors may be used to endorse or promote products derived
    * from this software without specific prior written permission.
    *
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    */

    /* This example demonstrates the AES 256 cbc Encryption and Decryptions. */
    #include <stdio.h>
    #include <string.h>
    #include <kernel/dpl/DebugP.h>
    #include "ti_drivers_config.h"
    #include "ti_drivers_open_close.h"
    #include "ti_board_open_close.h"

    #define MyDebug 1

    #ifndef MyDebug
    /* Input or output length*/
    #define APP_CRYPTO_AES_CBC_256_INOUT_LENGTH (16U)
    #else
    /* Input or output length*/
    //#define APP_CRYPTO_AES_CBC_256_INOUT_LENGTH ((0xFFF0))
    #define APP_CRYPTO_AES_CBC_256_INOUT_LENGTH (1024*32U)
    #endif

    /* Aes max key length*/
    #define APP_CRYPTO_AES_CBC_256_MAX_KEY_LENGTH (32U)
    /* Aes max IV length*/
    #define APP_CRYPTO_AES_CBC_256_MAXIV_LENGTH (16U)
    /* Aes key length in bites*/
    #define APP_CRYPTO_AES_CBC_256_KEY_LENGTH_IN_BITS (256U)


    const char* pmsgheader = "[LP-AM243x][CBC256]";


    //debug start
    #define APP_CRYPTO_AES_CBC_COMMON ((300*1024))
    //#define APP_PLANE_TEST_MAX_LENGTH APP_CRYPTO_AES_CBC_COMMON
    //#define APP_ENCRYPTION_OUT_LENGTH APP_CRYPTO_AES_CBC_COMMON
    //#define APP_DECRYPTION_OUT_LENGTH APP_CRYPTO_AES_CBC_COMMON

    //static uint8_t PlnB[APP_PLANE_TEST_MAX_LENGTH] __attribute__ ((section(".aes_mem")));
    //static uint8_t EncB[APP_ENCRYPTION_OUT_LENGTH] __attribute__ ((section(".aes_mem")));
    //static uint8_t DecB[APP_DECRYPTION_OUT_LENGTH] __attribute__ ((section(".aes_mem")));
    static uint8_t PlnB[APP_CRYPTO_AES_CBC_COMMON] = {0};
    static uint8_t EncB[APP_CRYPTO_AES_CBC_COMMON] = {0};
    static uint8_t DecB[APP_CRYPTO_AES_CBC_COMMON] = {0};

    //debug end


    #ifndef MyDebug
    // OUTPUT
    //static uint8_t OutB[APP_CRYPTO_AES_CBC_256_INOUT_LENGTH] =
    //{
    // 0x21, 0x79, 0x9A, 0x8B, 0x33, 0xA0, 0xE3, 0x72,
    // 0x4F, 0x37, 0x96, 0x43, 0x5F, 0x57, 0x27, 0x96
    //};
    #endif

    // INPUT
    /* Input buffer for encryption or decryption */
    #ifndef MyDebug
    static uint8_t gCryptoAesCbc256InputBuf[APP_CRYPTO_AES_CBC_256_INOUT_LENGTH] =
    {
    0x81, 0xEA, 0x5B, 0xA4, 0x69, 0x45, 0xC1, 0x70,
    0x5F, 0x6F, 0x89, 0x77, 0x88, 0x68, 0xCC, 0x67
    };
    #else
    //static uint8_t gCryptoAesCbc256InputBuf[APP_CRYPTO_AES_CBC_256_INOUT_LENGTH] __attribute__ ((section(".aes_mem"))) = {0};
    static uint8_t gCryptoAesCbc256InputBuf[APP_CRYPTO_AES_CBC_256_INOUT_LENGTH] = {0};
    #endif

    // KEY
    /* The AES algorithm encrypts and decrypts data in blocks of 128 bits. It can do this using 128-bit or 256-bit keys */
    #ifndef MyDebug
    static uint8_t gCryptoAesCbc256Key[APP_CRYPTO_AES_CBC_256_MAX_KEY_LENGTH] =
    {
    0x33, 0xA3, 0x66, 0x46, 0xFE, 0x56, 0xF7, 0x0D,
    0xC0, 0xC5, 0x1A, 0x31, 0x17, 0xE6, 0x39, 0xF1,
    0x82, 0xDE, 0xF8, 0xCA, 0xB5, 0xC0, 0x66, 0x71,
    0xEE, 0xA0, 0x40, 0x7C, 0x48, 0xA9, 0xC7, 0x57
    };
    #else
    static uint8_t gCryptoAesCbc256Key[APP_CRYPTO_AES_CBC_256_MAX_KEY_LENGTH] = {0};
    #endif

    // IV
    /* Initialization vector (IV) is an arbitrary number that can be used along with a secret key for data encryption/decryption. */
    #ifndef MyDebug
    static uint8_t gCryptoAesCbc256Iv[APP_CRYPTO_AES_CBC_256_MAXIV_LENGTH] =
    {
    0x7C, 0xE2, 0xAB, 0xAF, 0x8B, 0xEF, 0x23, 0xC4,
    0x81, 0x6D, 0xC8, 0xCE, 0x84, 0x20, 0x48, 0xA7
    };
    #else
    static uint8_t gCryptoAesCbc256Iv[APP_CRYPTO_AES_CBC_256_MAXIV_LENGTH] = {0};
    #endif

    #ifndef MyDebug
    /* Encryption output buf */
    uint8_t gCryptoAesCbc256EncResultBuf[APP_CRYPTO_AES_CBC_256_INOUT_LENGTH] __attribute__ ((aligned (SA2UL_CACHELINE_ALIGNMENT)));
    /* Decryption output buf */
    uint8_t gCryptoAesCbc256DecResultBuf[APP_CRYPTO_AES_CBC_256_INOUT_LENGTH] __attribute__ ((aligned (SA2UL_CACHELINE_ALIGNMENT))) = {"AbCdEfGhI"};
    #else
    /* Encryption output buf */
    //uint8_t gCryptoAesCbc256EncResultBuf[APP_CRYPTO_AES_CBC_256_INOUT_LENGTH] __attribute__ ((section(".aes_mem"))) __attribute__ ((aligned (SA2UL_CACHELINE_ALIGNMENT)));
    /* Decryption output buf */
    //uint8_t gCryptoAesCbc256DecResultBuf[APP_CRYPTO_AES_CBC_256_INOUT_LENGTH] __attribute__ ((section(".aes_mem"))) __attribute__ ((aligned (SA2UL_CACHELINE_ALIGNMENT))) = {"AbCdEfGhI"};

    /* Encryption output buf */
    uint8_t gCryptoAesCbc256EncResultBuf[APP_CRYPTO_AES_CBC_256_INOUT_LENGTH];
    /* Decryption output buf */
    uint8_t gCryptoAesCbc256DecResultBuf[APP_CRYPTO_AES_CBC_256_INOUT_LENGTH];

    #endif

    /* Context memory */
    static Crypto_Context gCryptoAesCbcContext __attribute__ ((aligned (SA2UL_CACHELINE_ALIGNMENT)));

    /* Context Object */
    SA2UL_ContextObject gSa2ulCtxObj __attribute__ ((aligned (SA2UL_CACHELINE_ALIGNMENT)));


    //static uint8_t aes_in[1024*1024] __attribute__((section(".aes_mem"))) = {"#AES TEST#"};
    //static uint8_t ddr_in[1024*1024] __attribute__((section(".ddr_test"))) = {"#DDR TEST#"};


    void loop_forever(void)
    {
    volatile uint32_t loop = 1;
    while(loop)
    ;
    }

    void crypto_aes_cbc_256(void *args)
    {
    int32_t status;
    Crypto_Handle aesHandle;
    SA2UL_ContextParams ctxParams;

    //debug
    int32_t num,len,bcnt;

    Drivers_open();
    Board_driversOpen();


    #if MyDebug //debug
    // loop_forever();
    DebugP_log("\r\n");

    // DebugP_log("[CRYPTO] gCryptoAesCbc256InputBuf[0x%x] = [ %d ]\r\n",&gCryptoAesCbc256InputBuf[0],sizeof(gCryptoAesCbc256InputBuf));
    // DebugP_log("[CRYPTO] gCryptoAesCbc256EncResultBuf[0x%x] = [ %d ]\r\n",&gCryptoAesCbc256EncResultBuf[0],sizeof(gCryptoAesCbc256EncResultBuf));
    // DebugP_log("[CRYPTO] gCryptoAesCbc256DecResultBuf[0x%x] = [ %d ]\r\n",&gCryptoAesCbc256DecResultBuf[0],sizeof(gCryptoAesCbc256DecResultBuf));
    // DebugP_log("[CRYPTO] DDR MEM[0x%x] = [ %s ]\r\n",&ddr_in[0],&ddr_in[0]);

    //INPUT
    FILE *fp;
    #if 1
    fp=fopen("test.bin","r+b");
    size_t rsize = fread(&PlnB[0],APP_CRYPTO_AES_CBC_COMMON,1,fp);
    memset(&PlnB[0],0x77,APP_CRYPTO_AES_CBC_COMMON);
    rsize = APP_CRYPTO_AES_CBC_COMMON;
    DebugP_log("[CRYPTO] TEXT SIZE = [ %d ]\r\n",rsize);
    fclose(fp);
    #endif
    // memset(&gCryptoAesCbc256InputBuf[0],0x77,sizeof(gCryptoAesCbc256InputBuf));
    //KEY
    // FILE *fp;
    fp=fopen("key.bin","r+b");
    fread(&gCryptoAesCbc256Key[0],sizeof(gCryptoAesCbc256Key),1,fp);
    fclose(fp);
    CacheP_wb(gCryptoAesCbc256Key, sizeof(gCryptoAesCbc256Key), CacheP_TYPE_ALLD);
    CacheP_inv(gCryptoAesCbc256Key, sizeof(gCryptoAesCbc256Key), CacheP_TYPE_ALLD);

    //IV
    // FILE *fp;
    fp=fopen("iv.bin","r+b");
    fread(&gCryptoAesCbc256Iv[0],sizeof(gCryptoAesCbc256Iv),1,fp);
    fclose(fp);
    CacheP_wb(gCryptoAesCbc256Iv, sizeof(gCryptoAesCbc256Iv), CacheP_TYPE_ALLD);
    CacheP_inv(gCryptoAesCbc256Iv, sizeof(gCryptoAesCbc256Iv), CacheP_TYPE_ALLD);

    #endif //debug

    for(int n=0;n<7;n++){
    DebugP_log("[CRYPTO] AES CBC-256 example started ...\r\n");
    DebugP_log("[CRYPTO] AES CBC-256 %dKB - %dKB \r\n",(n*APP_CRYPTO_AES_CBC_COMMON)/1024,((n+1)*APP_CRYPTO_AES_CBC_COMMON)/1024);


    aesHandle = Crypto_open(&gCryptoAesCbcContext);
    DebugP_assert(aesHandle != NULL);

    /* Configure secure context */
    ctxParams.opType = SA2UL_OP_ENC;
    ctxParams.encAlg = SA2UL_ENC_ALG_AES;
    ctxParams.encMode = SA2UL_ENC_MODE_CBC;
    ctxParams.encKeySize = SA2UL_ENC_KEYSIZE_256;
    ctxParams.encDirection = SA2UL_ENC_DIR_ENCRYPT;
    memcpy( &ctxParams.key[0], &gCryptoAesCbc256Key[0], APP_CRYPTO_AES_CBC_256_MAX_KEY_LENGTH);
    memcpy( &ctxParams.iv[0], &gCryptoAesCbc256Iv[0], SA2UL_MAX_IV_SIZE_BYTES);
    // ctxParams.inputLen = sizeof(gCryptoAesCbc256InputBuf);
    // gSa2ulCtxObj.totalLengthInBytes = sizeof(gCryptoAesCbc256InputBuf);

    // TEST start
    num = rsize / APP_CRYPTO_AES_CBC_256_INOUT_LENGTH;
    bcnt = rsize % APP_CRYPTO_AES_CBC_256_INOUT_LENGTH;
    num += rsize % APP_CRYPTO_AES_CBC_256_INOUT_LENGTH ? 1 : 0;

    len = APP_CRYPTO_AES_CBC_256_INOUT_LENGTH;
    for(int i=0;i < num;i++){
    if(i == num-1 && bcnt != 0){
    len=bcnt;
    }
    ctxParams.inputLen = len;
    gSa2ulCtxObj.totalLengthInBytes = len;
    memset(&gCryptoAesCbc256InputBuf[0],0x00,sizeof(gCryptoAesCbc256InputBuf));
    memset(&gCryptoAesCbc256EncResultBuf[0],0x00,sizeof(gCryptoAesCbc256EncResultBuf));
    memset(&gCryptoAesCbc256DecResultBuf[0],0x00,sizeof(gCryptoAesCbc256DecResultBuf));
    /* Function to configure secure context */
    status = SA2UL_contextAlloc(gCryptoAesCbcContext.drvHandle, &gSa2ulCtxObj, &ctxParams);
    DebugP_assert(SystemP_SUCCESS == status);

    memcpy(gCryptoAesCbc256InputBuf,&PlnB[i*APP_CRYPTO_AES_CBC_256_INOUT_LENGTH],len);
    /* Perform cache writeback */
    CacheP_wb(gCryptoAesCbc256InputBuf, sizeof(gCryptoAesCbc256InputBuf), CacheP_TYPE_ALLD);
    CacheP_inv(gCryptoAesCbc256InputBuf, sizeof(gCryptoAesCbc256InputBuf), CacheP_TYPE_ALLD);

    /* Encryption */
    /* Function to transfer and receive data buffer */
    DebugP_log("%s:%llu>>> [S]Encryption[%d] size = %d\r\n", pmsgheader, ClockP_getTimeUsec(),i,len );
    status = SA2UL_contextProcess(&gSa2ulCtxObj,&gCryptoAesCbc256InputBuf[0], len, gCryptoAesCbc256EncResultBuf);
    DebugP_log("%s:%llu>>> [E]Encryption[%d] size = %d\r\n", pmsgheader, ClockP_getTimeUsec(),i,len );
    DebugP_assert(SystemP_SUCCESS == status);

    memcpy(&EncB[i*APP_CRYPTO_AES_CBC_256_INOUT_LENGTH],&gCryptoAesCbc256EncResultBuf[0],len);
    #if 0
    // DIFF
    if(memcmp(gCryptoAesCbc256EncResultBuf, OutB, sizeof(OutB)) != 0)
    {
    DebugP_log("[CRYPTO] AES CBC-256 example failed!!\r\n");
    }

    else
    {
    DebugP_log("[CRYPTO] AES CBC-256 example completed!!\r\n");
    DebugP_log("All tests have passed!!\r\n");
    }
    #endif

    /* Function to free secure context configuration*/
    status = SA2UL_contextFree(&gSa2ulCtxObj);
    DebugP_assert(SystemP_SUCCESS == status);
    }

    ctxParams.encDirection = SA2UL_ENC_DIR_DECRYPT;
    len = APP_CRYPTO_AES_CBC_256_INOUT_LENGTH;

    for(int i=0;i < num;i++){
    if(i == num-1 && bcnt != 0){
    len=bcnt;
    }
    ctxParams.inputLen = len;
    gSa2ulCtxObj.totalLengthInBytes = len;
    /* Function to configure secure context */
    status = SA2UL_contextAlloc(gCryptoAesCbcContext.drvHandle, &gSa2ulCtxObj, &ctxParams);
    DebugP_assert(SystemP_SUCCESS == status);

    memcpy(&gCryptoAesCbc256EncResultBuf[0],&EncB[i*APP_CRYPTO_AES_CBC_256_INOUT_LENGTH],len);

    CacheP_wb(gCryptoAesCbc256EncResultBuf, sizeof(gCryptoAesCbc256EncResultBuf), CacheP_TYPE_ALLD);
    CacheP_inv(gCryptoAesCbc256EncResultBuf, sizeof(gCryptoAesCbc256EncResultBuf), CacheP_TYPE_ALLD);

    /* Decryption */
    /* Function to transfer and receive data buffer */
    DebugP_log("%s:%llu>>> [S]Decryption[%d] size = %d\r\n", pmsgheader, ClockP_getTimeUsec(),i,len );
    status = SA2UL_contextProcess(&gSa2ulCtxObj,&gCryptoAesCbc256EncResultBuf[0], len, gCryptoAesCbc256DecResultBuf);
    DebugP_log("%s:%llu>>> [E]Decryption[%d] size = %d\r\n", pmsgheader, ClockP_getTimeUsec(),i,len );
    DebugP_assert(SystemP_SUCCESS == status);

    memcpy(&DecB[i*APP_CRYPTO_AES_CBC_256_INOUT_LENGTH],gCryptoAesCbc256DecResultBuf,len);

    /* Function to free secure context configuration*/
    status = SA2UL_contextFree(&gSa2ulCtxObj);
    DebugP_assert(SystemP_SUCCESS == status);
    }
    //debug

    // loop_forever();

    /* Close AES instance */
    status = Crypto_close(aesHandle);
    DebugP_assert(SystemP_SUCCESS == status);


    //debug
    // if(memcmp(&PlnB[0],&DecB[0],sizeof(DecB))){
    // DebugP_log("[CRYPTO] AES CBC-256 example failed!!\r\n");
    // }

    //debug

    #if 0
    /* Comparing result with expected test results */
    if(memcmp(gCryptoAesCbc256DecResultBuf, gCryptoAesCbc256InputBuf, APP_CRYPTO_AES_CBC_256_INOUT_LENGTH) != 0)
    {
    for(int i=0;i<APP_CRYPTO_AES_CBC_256_INOUT_LENGTH;i++){
    if(gCryptoAesCbc256DecResultBuf[i]!=gCryptoAesCbc256InputBuf[i]){
    DebugP_log("[CRYPTO] gCryptoAesCbc256DecResultBuf[%d]=0x%02x gCryptoAesCbc256InputBuf[%d]=0x%02x\r\n",
    i+1,gCryptoAesCbc256DecResultBuf[i],i+1,gCryptoAesCbc256InputBuf[i]);
    }
    }
    DebugP_log("[CRYPTO] AES CBC-256 example failed!!\r\n");
    }

    #else

    DebugP_log("%s:%llu>>> [S]Comparing size = %d\r\n", pmsgheader, ClockP_getTimeUsec(),APP_CRYPTO_AES_CBC_COMMON );
    /* Comparing result with expected test results */
    if(memcmp(PlnB, DecB, APP_CRYPTO_AES_CBC_COMMON) != 0)
    {
    #if 0
    for(int i=0;i<APP_CRYPTO_AES_CBC_COMMON;i++){
    if(PlnB[i]!=DecB[i]){
    DebugP_log("[CRYPTO] PlnB[%d]=0x%02x DecB[%d]=0x%02x\r\n",
    i+1,PlnB[i],i+1,DecB[i]);
    }
    }
    #endif
    DebugP_log("[CRYPTO] AES CBC-256 example failed!!\r\n");
    }
    #endif
    else
    {
    DebugP_log("%s:%llu>>> [E]Comparing size = %d\r\n", pmsgheader, ClockP_getTimeUsec(),APP_CRYPTO_AES_CBC_COMMON );
    DebugP_log("[CRYPTO] AES CBC-256 example completed!!\r\n");
    DebugP_log("All tests have passed!!\r\n");
    }
    }//for(int n=0;n<7;n++)
    Board_driversClose();
    Drivers_close();

    return;
    }




    Regards,
    Yukinobu

  • Hi Yukinobu,

    Thanks for sharing the code!!

    Unfortunately, it is not very much readable & too customized to your environment. So, I just took the default example & tested the encryption timing for 1KB of data with the following modification

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    diff --git a/examples/security/crypto/sa2ul_aes/crypto_aes_cbc_256/crypto_aes_cbc_256.c b/examples/security/crypto/sa2ul_aes/crypto_aes_cbc_256/crypto_aes_cbc_256.c
    index df58a90c..c6a7bba0 100644
    --- a/examples/security/crypto/sa2ul_aes/crypto_aes_cbc_256/crypto_aes_cbc_256.c
    +++ b/examples/security/crypto/sa2ul_aes/crypto_aes_cbc_256/crypto_aes_cbc_256.c
    @@ -37,9 +37,10 @@
    #include "ti_drivers_config.h"
    #include "ti_drivers_open_close.h"
    #include "ti_board_open_close.h"
    +#include "test.h"
    /* Input or output length*/
    -#define APP_CRYPTO_AES_CBC_256_INOUT_LENGTH (16U)
    +#define APP_CRYPTO_AES_CBC_256_INOUT_LENGTH (TEST_BIN_H_SIZE_IN_BYTES)
    /* Aes max key length*/
    #define APP_CRYPTO_AES_CBC_256_MAX_KEY_LENGTH (32U)
    /* Aes max IV length*/
    @@ -48,11 +49,7 @@
    #define APP_CRYPTO_AES_CBC_256_KEY_LENGTH_IN_BITS (256U)
    /* Input buffer for encryption or decryption */
    -static uint8_t gCryptoAesCbc256InputBuf[APP_CRYPTO_AES_CBC_256_INOUT_LENGTH] =
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    I did not see the timing reported by you as it took just 27us.

    Fullscreen
    1
    2
    3
    4
    [CRYPTO] AES CBC-256 example started ...
    Encryption: size = 1024, start = 6778, end = 6805, diff = 27
    [CRYPTO] AES CBC-256 example completed!!
    All tests have passed!!
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Regards,

    Prashant

  • Hi Prashant,

    There was a problem with the way I output logs.
    After reviewing the way I output logs, I was able to get the content I expected.

    Regards,
    Yukinobu