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 the sample source sa2ul_sha

Part Number: LP-AM243

Tool/software:

サンプル フォルダーの 'examples\security\crypto\sa2ul_sha' は 'abcdefpra' の SHA2-512 および SHA2-256 ハッシュ値を出力します。
この文字列を別の文字列に置き換えると、結果は 'abcdefpra' と同じハッシュ値になります。

ハッシュ値は置換された文字列のハッシュ値になると思います。

しかし、なぜ置換前と同じハッシュ値なのでしょうか?


使用したソースファイルを共有します。

crypto_sha.c
/*
 *  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 implementation of SHA */
#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"

/* SHA512 length */
#define APP_SHA512_LENGTH               (64U)
/* SHA256 length */
#define APP_SHA256_LENGTH               (32U)
/* Alignment */
#define APP_SHA_ALIGNMENT_FOR_BUF       (128U)
/*Input and output buf length*/
#define APP_SHA_IN_OUT_BUF_LENGTH       (9U)

/*Inout test buffer for sha computation */
static uint8_t gCryptoShaTestInputBuf[APP_SHA_IN_OUT_BUF_LENGTH] = {"abcdefpra"};
//static uint8_t gCryptoShaTestInputBuf[APP_SHA_IN_OUT_BUF_LENGTH];

/*Output test buffer for sha computation */
uint8_t gCryptoShaTestOutputBuf[APP_SHA_IN_OUT_BUF_LENGTH] __attribute__ ((aligned (APP_SHA_ALIGNMENT_FOR_BUF)));

/* SHA-512 test vectors,this is expected hash for the test buffer */
static uint8_t gCryptoSha512TestSum[APP_SHA512_LENGTH] =
{
    0x1d, 0xa6, 0x8d, 0x60, 0x62, 0x9b, 0x61, 0xf4,
    0xab, 0x16, 0x67, 0x77, 0x2a, 0x21, 0xae, 0x85,
    0xbf, 0x5c, 0x20, 0xdf, 0x5c, 0x38, 0xcc, 0xa5,
    0x29, 0xc6, 0xce, 0x09, 0x22, 0xbe, 0x15, 0x7f,
    0x04, 0x9c, 0x22, 0x0a, 0xab, 0x85, 0xb4, 0x3c,
    0x49, 0x66, 0x12, 0xfa, 0x12, 0xd5, 0x41, 0xac,
    0x78, 0x50, 0x9a, 0x5f, 0x03, 0x4c, 0xc9, 0xcb,
    0x64, 0x39, 0x0a, 0x74, 0x2a, 0xb6, 0xab, 0x43
};

/* SHA-256 test vectors,this is expected hash for the test buffer */
static uint8_t gCryptoSha256TestSum[APP_SHA256_LENGTH] =
{
    0xC2, 0x77, 0xB1, 0x18, 0x8B, 0x34, 0xF5, 0x95,
    0x3A, 0x50, 0xA5, 0x28, 0xE4, 0xD8, 0x68, 0x01,
    0x14, 0xFB, 0xED, 0x82, 0x56, 0x85, 0x6D, 0xA2,
    0xFD, 0x82, 0x35, 0xD5, 0x00, 0x71, 0x96, 0x6F
};

/* Context memory */
static Crypto_Context gCryptoShaContext __attribute__ ((aligned (SA2UL_CACHELINE_ALIGNMENT)));
/* Context Object */
SA2UL_ContextObject  gSa2ulCtxObj __attribute__ ((aligned (SA2UL_CACHELINE_ALIGNMENT)));

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


void crypto_sha(void *args)
{
    int32_t             status;
    Crypto_Handle       shaHandle;
    SA2UL_ContextParams ctxParams;



    Drivers_open();
    Board_driversOpen();

    DebugP_log("[CRYPTO] SHA example started ...\r\n");

    shaHandle = Crypto_open(&gCryptoShaContext);
    DebugP_assert(shaHandle != NULL);

    /* Configure secure context */
    ctxParams.opType    = SA2UL_OP_AUTH;
    ctxParams.hashAlg   = SA2UL_HASH_ALG_SHA2_512;
    ctxParams.inputLen  = sizeof(gCryptoShaTestInputBuf);
    gSa2ulCtxObj.totalLengthInBytes = sizeof(gCryptoShaTestInputBuf);

    status = SA2UL_contextAlloc(gCryptoShaContext.drvHandle, &gSa2ulCtxObj, &ctxParams);
    DebugP_assert(SystemP_SUCCESS == status);

    //debug
//        loop_forever();
        DebugP_log("[DEBUG][Now]gCryptoShaTestInputBuf=[%s]\r\n",gCryptoShaTestInputBuf);
        memset(gCryptoShaTestInputBuf,0x30,APP_SHA_IN_OUT_BUF_LENGTH);
        DebugP_log("[DEBUG][New]gCryptoShaTestInputBuf=[%s]\r\n",gCryptoShaTestInputBuf);
    //debug

    /* Perform SHA operation */
    status = SA2UL_contextProcess(&gSa2ulCtxObj,&gCryptoShaTestInputBuf[0], sizeof(gCryptoShaTestInputBuf), gCryptoShaTestOutputBuf);
    DebugP_assert(SystemP_SUCCESS == status);

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

    /*comparing result with expected test results*/
    if(memcmp(gSa2ulCtxObj.computedHash, gCryptoSha512TestSum, APP_SHA512_LENGTH) != 0)
    {
        DebugP_log("[CRYPTO] SHA-512 example failed!!\r\n");
    }
    else
    {
        DebugP_log("[CRYPTO] SHA-512 example completed!!\r\n");
    }

    /* Configure secure context */
    ctxParams.opType    = SA2UL_OP_AUTH;
    ctxParams.hashAlg   = SA2UL_HASH_ALG_SHA2_256;
    ctxParams.inputLen  = sizeof(gCryptoShaTestInputBuf);
    gSa2ulCtxObj.totalLengthInBytes = sizeof(gCryptoShaTestInputBuf);

    status = SA2UL_contextAlloc(gCryptoShaContext.drvHandle, &gSa2ulCtxObj, &ctxParams);
    DebugP_assert(SystemP_SUCCESS == status);

    /* Perform SHA operation */
    status = SA2UL_contextProcess(&gSa2ulCtxObj,&gCryptoShaTestInputBuf[0], sizeof(gCryptoShaTestInputBuf), gCryptoShaTestOutputBuf);
    DebugP_assert(SystemP_SUCCESS == status);

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

    /*comparing result with expected test results*/
    if(memcmp(gSa2ulCtxObj.computedHash, gCryptoSha256TestSum, APP_SHA256_LENGTH) != 0)
    {
        DebugP_log("[CRYPTO] SHA-256 example failed!!\r\n");
    }
    else
    {
        DebugP_log("[CRYPTO] SHA-256 example completed!!\r\n");
        DebugP_log("All tests have passed!!\r\n");
    }

    /* Close SHA instance */
    status = Crypto_close(shaHandle);
    DebugP_assert(SystemP_SUCCESS == status);

    Board_driversClose();
    Drivers_close();

    return;
}

  • Hi Yukinobu,

    It is our pleasure to help. Would you please, repost this thread in English? I will delete this thread shortly.

    Best regards,

    Qutaiba

  • Hi Qutaiba,

    Sorry. I made a mistake.
    In the examples folder, 'examples\security\crypto\crypto\sa2ul_sha' outputs the SHA2-512 and SHA2-256 hash values for 'abcdefpra'.
    If I replace this string with another string, the result will be the same hash value as 'abcdefpra'.

    I expect the hash value to be the hash value of the string I replaced it with, but why is it the same hash value as it was before I replaced it?

    The device I am using is an LP-AM243 HS-SE, does that have anything to do with it?
    I will share the source file I used.

    8875.crypto_sha.c
    /*
     *  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 implementation of SHA */
    #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"
    
    /* SHA512 length */
    #define APP_SHA512_LENGTH               (64U)
    /* SHA256 length */
    #define APP_SHA256_LENGTH               (32U)
    /* Alignment */
    #define APP_SHA_ALIGNMENT_FOR_BUF       (128U)
    /*Input and output buf length*/
    #define APP_SHA_IN_OUT_BUF_LENGTH       (9U)
    
    /*Inout test buffer for sha computation */
    static uint8_t gCryptoShaTestInputBuf[APP_SHA_IN_OUT_BUF_LENGTH] = {"abcdefpra"};
    //static uint8_t gCryptoShaTestInputBuf[APP_SHA_IN_OUT_BUF_LENGTH];
    
    /*Output test buffer for sha computation */
    uint8_t gCryptoShaTestOutputBuf[APP_SHA_IN_OUT_BUF_LENGTH] __attribute__ ((aligned (APP_SHA_ALIGNMENT_FOR_BUF)));
    
    /* SHA-512 test vectors,this is expected hash for the test buffer */
    static uint8_t gCryptoSha512TestSum[APP_SHA512_LENGTH] =
    {
        0x1d, 0xa6, 0x8d, 0x60, 0x62, 0x9b, 0x61, 0xf4,
        0xab, 0x16, 0x67, 0x77, 0x2a, 0x21, 0xae, 0x85,
        0xbf, 0x5c, 0x20, 0xdf, 0x5c, 0x38, 0xcc, 0xa5,
        0x29, 0xc6, 0xce, 0x09, 0x22, 0xbe, 0x15, 0x7f,
        0x04, 0x9c, 0x22, 0x0a, 0xab, 0x85, 0xb4, 0x3c,
        0x49, 0x66, 0x12, 0xfa, 0x12, 0xd5, 0x41, 0xac,
        0x78, 0x50, 0x9a, 0x5f, 0x03, 0x4c, 0xc9, 0xcb,
        0x64, 0x39, 0x0a, 0x74, 0x2a, 0xb6, 0xab, 0x43
    };
    
    /* SHA-256 test vectors,this is expected hash for the test buffer */
    static uint8_t gCryptoSha256TestSum[APP_SHA256_LENGTH] =
    {
        0xC2, 0x77, 0xB1, 0x18, 0x8B, 0x34, 0xF5, 0x95,
        0x3A, 0x50, 0xA5, 0x28, 0xE4, 0xD8, 0x68, 0x01,
        0x14, 0xFB, 0xED, 0x82, 0x56, 0x85, 0x6D, 0xA2,
        0xFD, 0x82, 0x35, 0xD5, 0x00, 0x71, 0x96, 0x6F
    };
    
    /* Context memory */
    static Crypto_Context gCryptoShaContext __attribute__ ((aligned (SA2UL_CACHELINE_ALIGNMENT)));
    /* Context Object */
    SA2UL_ContextObject  gSa2ulCtxObj __attribute__ ((aligned (SA2UL_CACHELINE_ALIGNMENT)));
    
    void loop_forever(void)
    {
        volatile uint32_t loop = 1;
        while(loop)
            ;
    }
    
    
    void crypto_sha(void *args)
    {
        int32_t             status;
        Crypto_Handle       shaHandle;
        SA2UL_ContextParams ctxParams;
    
    
    
        Drivers_open();
        Board_driversOpen();
    
        DebugP_log("[CRYPTO] SHA example started ...\r\n");
    
        shaHandle = Crypto_open(&gCryptoShaContext);
        DebugP_assert(shaHandle != NULL);
    
        /* Configure secure context */
        ctxParams.opType    = SA2UL_OP_AUTH;
        ctxParams.hashAlg   = SA2UL_HASH_ALG_SHA2_512;
        ctxParams.inputLen  = sizeof(gCryptoShaTestInputBuf);
        gSa2ulCtxObj.totalLengthInBytes = sizeof(gCryptoShaTestInputBuf);
    
        status = SA2UL_contextAlloc(gCryptoShaContext.drvHandle, &gSa2ulCtxObj, &ctxParams);
        DebugP_assert(SystemP_SUCCESS == status);
    
        //debug
    //        loop_forever();
            DebugP_log("[DEBUG][Now]gCryptoShaTestInputBuf=[%s]\r\n",gCryptoShaTestInputBuf);
            memset(gCryptoShaTestInputBuf,0x30,APP_SHA_IN_OUT_BUF_LENGTH);
            DebugP_log("[DEBUG][New]gCryptoShaTestInputBuf=[%s]\r\n",gCryptoShaTestInputBuf);
        //debug
    
        /* Perform SHA operation */
        status = SA2UL_contextProcess(&gSa2ulCtxObj,&gCryptoShaTestInputBuf[0], sizeof(gCryptoShaTestInputBuf), gCryptoShaTestOutputBuf);
        DebugP_assert(SystemP_SUCCESS == status);
    
        /*Function to free secure context configuration*/
        status = SA2UL_contextFree(&gSa2ulCtxObj);
        DebugP_assert(SystemP_SUCCESS == status);
    
        /*comparing result with expected test results*/
        if(memcmp(gSa2ulCtxObj.computedHash, gCryptoSha512TestSum, APP_SHA512_LENGTH) != 0)
        {
            DebugP_log("[CRYPTO] SHA-512 example failed!!\r\n");
        }
        else
        {
            DebugP_log("[CRYPTO] SHA-512 example completed!!\r\n");
        }
    
        /* Configure secure context */
        ctxParams.opType    = SA2UL_OP_AUTH;
        ctxParams.hashAlg   = SA2UL_HASH_ALG_SHA2_256;
        ctxParams.inputLen  = sizeof(gCryptoShaTestInputBuf);
        gSa2ulCtxObj.totalLengthInBytes = sizeof(gCryptoShaTestInputBuf);
    
        status = SA2UL_contextAlloc(gCryptoShaContext.drvHandle, &gSa2ulCtxObj, &ctxParams);
        DebugP_assert(SystemP_SUCCESS == status);
    
        /* Perform SHA operation */
        status = SA2UL_contextProcess(&gSa2ulCtxObj,&gCryptoShaTestInputBuf[0], sizeof(gCryptoShaTestInputBuf), gCryptoShaTestOutputBuf);
        DebugP_assert(SystemP_SUCCESS == status);
    
        /*Function to free secure context configuration*/
        status = SA2UL_contextFree(&gSa2ulCtxObj);
        DebugP_assert(SystemP_SUCCESS == status);
    
        /*comparing result with expected test results*/
        if(memcmp(gSa2ulCtxObj.computedHash, gCryptoSha256TestSum, APP_SHA256_LENGTH) != 0)
        {
            DebugP_log("[CRYPTO] SHA-256 example failed!!\r\n");
        }
        else
        {
            DebugP_log("[CRYPTO] SHA-256 example completed!!\r\n");
            DebugP_log("All tests have passed!!\r\n");
        }
    
        /* Close SHA instance */
        status = Crypto_close(shaHandle);
        DebugP_assert(SystemP_SUCCESS == status);
    
        Board_driversClose();
        Drivers_close();
    
        return;
    }
    

     

  • Hello,

    The input buffer (gCryptoShaTestInputBuf) is mapped to the cached memory region. So, any modifications to it must be followed by it at least Cache Writeback to ensure the original buffer is updated with the latest data available in the Cache.

    Following change on top of your modified "crypto_sha.c" file should give you expected results.

    diff --git a/examples/security/crypto/sa2ul_sha/crypto_sha.c b/examples/security/crypto/sa2ul_sha/crypto_sha.c
    index fb6cdc5..d174ea4 100644
    --- a/examples/security/crypto/sa2ul_sha/crypto_sha.c
    +++ b/examples/security/crypto/sa2ul_sha/crypto_sha.c
    @@ -117,6 +117,7 @@ void crypto_sha(void *args)
     //        loop_forever();
             DebugP_log("[DEBUG][Now]gCryptoShaTestInputBuf=[%s]\r\n",gCryptoShaTestInputBuf);
             memset(gCryptoShaTestInputBuf,0x30,APP_SHA_IN_OUT_BUF_LENGTH);
    +        CacheP_wb(gCryptoShaTestInputBuf, APP_SHA_IN_OUT_BUF_LENGTH, CacheP_TYPE_ALL);
             DebugP_log("[DEBUG][New]gCryptoShaTestInputBuf=[%s]\r\n",gCryptoShaTestInputBuf);
         //debug
     
    

    Following are the logs:

    [CRYPTO] SHA example started ...
    [DEBUG][Now]gCryptoShaTestInputBuf=[abcdefpra]
    [DEBUG][New]gCryptoShaTestInputBuf=[000000000]
    [CRYPTO] SHA-512 example failed!!
    [CRYPTO] SHA-256 example failed!!

    Regards,

    Prashant

  • Hi Prashant,

    Thank you very much. The results were as expected.

    The input buffer (gCryptoShaTestInputBuf) is mapped to the cached memory region.

    You mentioned that it is mapped to a cached memory space,
    How are you mapping to the cache area?
    I would like to change the memory space to be cached,
    Please let me know how.

    Regards,
    Yukinobu

  • Hi Yukinobu,

    /*Inout test buffer for sha computation */
    static uint8_t gCryptoShaTestInputBuf[APP_SHA_IN_OUT_BUF_LENGTH] = {"abcdefpra"};

    This is the definition of the `gCryptoShaTestInputBuf` in the `crypto_sha.c` source file. This array is an initialized global array which means it is allocated to the `.data` section. In the Linker configurations, this `.data` section is then allocated to the single MSRAM memory region which is checked as Cached in the MPU configurations.

    You may make the `gCryptoShaTestInputBuf` array non-cached with the following steps:

    • Create a new memory region, let's say, MSRAM_1.
    • Create a new section, let's say, .data.sha_buffer allocating it to MSRAM1.
    • Allocate the gCryptoShaTestInputBuf to .data.sha_buffer with __attribute__((section(".data.sha_buffer"))).

    Once allocated to non-cached region, you do not need manual cache operations like writebacks.

    Regards,

    Prashant