搜索
bottom↓
回复: 1

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

[复制链接]

出0入0汤圆

发表于 2011-7-27 20:49:40 | 显示全部楼层 |阅读模式
最近用到DS28e01,在写ds28e01(Copy Scratchpad)失败,原因是我用SHA算法计算出来发给ds28e01的MAC跟ds28e01内部SHA引擎计算的MAC不一样,不知道是怎么回事?请高手指点,谢谢了.

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

 楼主| 发表于 2011-7-28 10:52:30 | 显示全部楼层
回复【楼主位】quanhaiqiang
-----------------------------------------------------------------------

以下是我的ds28e01(Copy Scratchpad)写数据的函数,比如把unsigned char wdatato[8]={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[64], err, i, page, temp, mac_array[20], scratch[11];
  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[i + 4] = *(secret + i);
    if(err)
      err = read_block((page + 8), (sha_input + 12), 20);
    sha_input[28] = 0xFF; sha_input[29] = 0xFF; sha_input[30] = 0xFF; sha_input[31] = 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[i + 29] = scratch;
          //sha_input[address - page] = data;
          //M10[31:24]
        sha_input[40] = 0x00 | (address >> 5);
          //M10[23:0] - M11
        for(i = 0; i < 7; i++)
          sha_input[i + 41] = *(chip + i);
        //M12
        for(i = 4; i < 8; i++)
          sha_input[i + 44] = *(secret + i);
        //M13-M15
        sha_input[52] = 0xFF; sha_input[53] = 0xFF; sha_input[54] = 0xFF; sha_input[55] = 0x80;
        sha_input[56] = 0x00; sha_input[57] = 0x00; sha_input[58] = 0x00; sha_input[59] = 0x00;
        sha_input[60] = 0x00; sha_input[61] = 0x00; sha_input[62] = 0x01; sha_input[63] = 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;
}
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-19 00:27

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表