2017-10-04 114 views
0

我正试图在TI cc26xx传感器标签上实现SHA256。我正在使用此处提供的TI_SHA_256 API:http://www.ti.com/tool/crypto。 我的主要测试代码如下:cc26xx上的TI sha256返回错误的值

... 
/* Crypto APIs Header */ 
#include "lib/TI_SHA_256.h" 
#include <stdio.h> 

uint32_t M[32] = { 0xe8 , 0x9d , 0xa1 , 0xd1 , 0xc7 , 0x4d , 0xee , 0x16 , 0x75 , 0x30 , 0x07 , 0x9a , 0x19 , 0xd1 , 0x5d , 0x76, 
        0x12 , 0x97 , 0xe4 , 0xb6 , 0xc8 , 0x03 , 0x38 , 0x1a , 0x41 , 0x6d , 0xac , 0x92 , 0xbf , 0x63 , 0x51 , 0x7a }; 
uint32_t Ha[8]; 
uint32_t *H_Array; 
volatile uint64_t L = 0x800; 

void uint32_print(char name[], uint32_t *data, int c) 
{ 
    uint32_t i; 
    if (c == 1){ 
     printf(name); 
     for(i = 0; i < 32; i++) { 
     printf("%x", data[i]); 
     } 
     printf("\r\n"); 
    } 
    else { 
     printf(name); 
     for(i = 0; i < 8; i++) { 
     printf("%x ", data[i]); 
     } 
     printf("\r\n"); 
    } 
} 

/*---------------------------------------------------------------------------*/ 
PROCESS(test_process, "Test process"); 
/*---------------------------------------------------------------------------*/ 
PROCESS_THREAD(test_process, ev, data) 
{ 
    PROCESS_BEGIN(); 

    printf("\r\n ---------------- TEST OF SHA API ------------------------ \r\n"); 
    uint32_print("Original text:", M, 1); 
    L= 0x200; 
    SHA_256(M, L, Ha, 1); 
    uint32_print("text:", M, 1); 
    uint32_print("Hashed text: ", Ha, 2); 
    printf("\r\n ---------------- \r\n"); 

    PROCESS_END(); 
} 
AUTOSTART_PROCESSES(&test_process); 

根据各种在线SHA256计算器等(http://www.fileformat.info/tool/hash.htm),校验应该是: 73411b58707db59d6bc3cd854850eca62058d0d9f74a1ea8260d5ccdd9ac5f87

凡为我的代码打印如下:

---------------- TEST OF SHA API ------------------------ 
text:e89da1d1c74dee16753079a19d15d761297e4b6c83381a416dac92bf63517a 
text:e89da1d1c74dee16753079a19d15d768000000000000000000000200 
Hashed text: 91da5dd5 6cbfcca9 85fcf373 90ae73e0 9e27a4d9 c42b100c 6e746091 eda68da7 
---------------- 

我的问题是:为什么我的代码返回错误的哈希总和?

回答

0

您的代码计算阵列M的前半部分(0x200=512位或64字节)的SHA-256哈希,初始化为0xe8 , 0x9d , ... , 0x76

然而,你的阵列Muint32_t类型的元素,这意味着在内存中,它看起来像这样(假设大端架构):

000000e80000009d000000a1000000d1000000c70000004d000000ee000000160000007500000030000000070000009a00000019000000d10000005d00000076 

正如人们可以在你的网上计算器查询,哈希这是91da5dd56cbfcca985fcf37390ae73e09e27a4d9c42b100c6e746091eda68da7。这是代码中由SHA_256()函数计算出的正确值。

+0

谢谢你,我很欣赏这个解释。 – kogito