TI sha256在cc26xx上返回错误的值

时间:2017-10-04 09:32:07

标签: c sha contiki texas-instruments

我正在尝试在TI cc26xx传感器标签上实施SHA256。我使用的是此处提供的TI_SHA_256 API:http://www.ti.com/tool/crypto。 我的主要测试代码如下:

...
/* 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
----------------

我的问题如下:为什么我的代码返回错误的哈希值?

1 个答案:

答案 0 :(得分:0)

您的代码计算数组if (Input.GetKey (KeyCode.LeftArrow)) { pos.x -= Time.deltaTime * speed; transform.position = pos; } if (Input.GetKeyDown (KeyCode.Space)) { Debug.Log ("Space key up"); pos.y += Time.deltaTime * speed; transform.position = pos; } 的前半部分(0x200=512位或64字节)的SHA-256哈希值,初始化为M

但是,您的数组0xe8 , 0x9d , ... , 0x76包含M类型的元素,这意味着在内存中它看起来像这样(假设是大端架构):

uint32_t

当您可以检入在线计算器时,其哈希为000000e80000009d000000a1000000d1000000c70000004d000000ee000000160000007500000030000000070000009a00000019000000d10000005d00000076 。这是代码中91da5dd56cbfcca985fcf37390ae73e09e27a4d9c42b100c6e746091eda68da7函数计算的正确值。