quanhaiqiang 发表于 2011-7-27 20:49:40

ds28e01的MAC用的是不是标准的SHA1算法 ?

最近用到DS28e01,在写ds28e01(Copy Scratchpad)失败,原因是我用SHA算法计算出来发给ds28e01的MAC跟ds28e01内部SHA引擎计算的MAC不一样,不知道是怎么回事?请高手指点,谢谢了.

quanhaiqiang 发表于 2011-7-28 10:52:30

回复【楼主位】quanhaiqiang
-----------------------------------------------------------------------

以下是我的ds28e01(Copy Scratchpad)写数据的函数,比如把unsigned char wdatato={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38
};这8个字节写到以0x60地址开始的存储空间,就调用protect_write_data(0x60, chip_num, secret, wdatato);
但结果写失败,向ds28e01传完MAC之后读一个字节老是读到temp=0xFF,失败。请高手指点,谢谢了。
unsigned char protect_write_data(unsigned char address,unsigned char* chip, unsigned char* secret, unsigned char* data)
{
unsigned char sha_input, err, i, page, temp, mac_array, scratch;
err=1;
page = address & 0xE0;
//Build array needed for MAC calculation
//See DS8E01 datasheet for details
//M0
for(i = 0; i < 4; ++i)
    sha_input = *(secret + i);
//M1 - M7
if(page == 0x80)
{
    for(i = 0; i < 8; i++)
      sha_input = *(secret + i);
    if(err)
      err = read_block((page + 8), (sha_input + 12), 20);
    sha_input = 0xFF; sha_input = 0xFF; sha_input = 0xFF; sha_input = 0xFF;
}
else
{
    if(err)
      err = read_block(page, (sha_input + 4), 28);
}
//Write data to the scratch pad
if(err)
    err = WriteScratch(address, data, 8);
    //M8 - M9
    if(err)
    {
      err = ReadScratch(scratch);
      if(err)
      {
      for(i = 3; i < 11; i++)
          sha_input = scratch;
          //sha_input = data;
          //M10
        sha_input = 0x00 | (address >> 5);
          //M10 - M11
        for(i = 0; i < 7; i++)
          sha_input = *(chip + i);
      //M12
        for(i = 4; i < 8; i++)
          sha_input = *(secret + i);
      //M13-M15
      sha_input = 0xFF; sha_input = 0xFF; sha_input = 0xFF; sha_input = 0x80;
        sha_input = 0x00; sha_input = 0x00; sha_input = 0x00; sha_input = 0x00;
        sha_input = 0x00; sha_input = 0x00; sha_input = 0x01; sha_input = 0xB8;
      //Generate the MAC using SHA-1 algorithm
        SHA1_Perform(sha_input, 64, mac_array);
      //DS2432的SHA1算法跟标准的不一样,它没有进行最后的一步加初始值?
      changeform(mac_array);
      if(err)
      {
          //Copy the scratchpad to memory
          //注意:不管什么情况,在进行存储或SHA功能命令前都得复+ROM命令
          Dallas_RES(); //DS28E01-100复位初始化
          Read_SerialNum(); //ROM功能命令
          Dallas_write_byte(0x55); //写Copy scratchpad命令
          for(i = 0; i < 3; i++)
          Dallas_write_byte(scratch);
          delay_ms(1); //内部SHA1计算需要1ms左右
          for(i = 0; i < 20; i++)
          Dallas_write_byte(mac_array);
          delay_ms(11);
          temp = Dallas_read_byte();
          //主机至少应读一个字节,读到0xAA表示复制成功;读到0x00失败,原因是计算出的MAC与主机发
          //送的MAC不匹配;读到oxFF也失败,原因是写保护或授权码不正确。
          if(temp == 0 && err)
          err = 0;
          else if(temp == 0xFF && err)
          err = 0;
      }
      }
    }
return err;
}
页: [1]
查看完整版本: ds28e01的MAC用的是不是标准的SHA1算法 ?