微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 硬件电路设计 > TI电源管理交流 > 请教SHA-1/HAMC问题

请教SHA-1/HAMC问题

时间:10-02 整理:3721RD 点击:

MCU和BQ34Z100通讯,现在需要再MCU实现SHA-1/HAMC功能,参考TISHA程序(见附件)设计,得到数据不对。

其中,关于SHA有许多疑问,用BQStudio BQ40Z50的SHA功能计算正确

MCU计算结果错误,

SHA计算:

void SHA1_authenticate(void)
{
int i; // Used for doing two times the SHA1 as required by the bq26100
int t; // Used for the indexes 0 through 79
UINT32 temp; // Used as the temp variable during the loop in which the
// working variables A, B, C1, D and E are update
//step1
// The 20 bytes of random message that are given to the bq26100 are arranged
// in 32-bit words so that the microcontroller can compute the SHA1/HMAC
Random[0] = (UINT32)(Message[16])*0x00000001 +
(UINT32)(Message[17])*0x00000100 +
(UINT32)(Message[18])*0x00010000 +
(UINT32)(Message[19])*0x01000000;
Random[1] = (UINT32)(Message[12])*0x00000001 +
(UINT32)(Message[13])*0x00000100 +
(UINT32)(Message[14])*0x00010000 +
(UINT32)(Message[15])*0x01000000;
Random[2] = (UINT32)(Message[ 8])*0x00000001 +
(UINT32)(Message[ 9])*0x00000100 +
(UINT32)(Message[10])*0x00010000 +
(UINT32)(Message[11])*0x01000000;
Random[3] = (UINT32)(Message[ 4])*0x00000001 +
(UINT32)(Message[ 5])*0x00000100 +
(UINT32)(Message[ 6])*0x00010000 +
(UINT32)(Message[ 7])*0x01000000;
Random[4] = (UINT32)(Message[ 0])*0x00000001 +
(UINT32)(Message[ 1])*0x00000100 +
(UINT32)(Message[ 2])*0x00010000 +
(UINT32)(Message[ 3])*0x01000000;
// The SHA1 is computed two times so that it complies with the bq26100 spec
for (i = 0; i <= 1; i++)
{
// Work Schedule
// The first four Working schedule variables Ws[0-3], are based on the key
// that is implied that the bq26100 contains
Ws[0] = (UINT32)(Key[12])*0x00000001 +
(UINT32)(Key[13])*0x00000100 +
(UINT32)(Key[14])*0x00010000 +
(UINT32)(Key[15])*0x01000000;
Ws[1] = (UINT32)(Key[ 8])*0x00000001 +
(UINT32)(Key[ 9])*0x00000100 +
(UINT32)(Key[10])*0x00010000 +
(UINT32)(Key[11])*0x01000000;
Ws[2] = (UINT32)(Key[ 4])*0x00000001 +
(UINT32)(Key[ 5])*0x00000100 +
(UINT32)(Key[ 6])*0x00010000 +
(UINT32)(Key[ 7])*0x01000000;
Ws[3] = (UINT32)(Key[ 0])*0x00000001 +
(UINT32)(Key[ 1])*0x00000100 +
(UINT32)(Key[ 2])*0x00010000 +
(UINT32)(Key[ 3])*0x01000000;
// On the first run of the SHA1 the random message is used
if (i == 0)
{
Ws[4] = Random[0];
Ws[5] = Random[1];
Ws[6] = Random[2];
Ws[7] = Random[3];
Ws[8] = Random[4];
}
// On the second run of the SHA1, H(Kd || M) is used
else
{
Ws[4] = H[0];
Ws[5] = H[1];
Ws[6] = H[2];
Ws[7] = H[3];
Ws[8] = H[4];
}
// The Work schedule variables Ws[9-15] remain the same regardless of
// which run of the SHA1. These values are as required by bq26100.
Ws[9] = 0x80000000;
Ws[10] = 0x00000000;
Ws[11] = 0x00000000;
Ws[12] = 0x00000000;
Ws[13] = 0x00000000;
Ws[14] = 0x00000000;
Ws[15] = 0x00000120;

// The Work schedule variables Ws[16-79] are determined by the W(t) func
for (t = 16; t <= 79; t++)
Ws[t] = W(t);
// Working Variables, always start the same regardless of which SHA1 run 常量字
A = 0x67452301;
B = 0xEFCDAB89;
C1 = 0x98BADCFE;
D = 0x10325476;
E = 0xC3D2E1F0;
// Hash reads, always start the same regardless of what SHA1 run
H[0] = A;
H[1] = B;
H[2] = C1;
H[3] = D;
H[4] = E;
// Loop to change working variables A, B, C1, D and E
// This is defined by FIPS 180-2 document
for (t = 0; t <= 79; t++)
{
temp = Rotl(A,5) + f(B,C1,D,t) + E + K(t) + Ws[t];
E = D;
D = C1;
C1 = Rotl(B,30);
B = A;
A = temp;
}
// 160-Bit SHA-1 Digest
H[0] = (A + H[0]);
H[1] = (B + H[1]);
H[2] = (C1 + H[2]);
H[3] = (D + H[3]);
H[4] = (E + H[4]);
}
}

其中,Ws[9] = 0x80000000;
Ws[10] = 0x00000000;
Ws[11] = 0x00000000;
Ws[12] = 0x00000000;
Ws[13] = 0x00000000;
Ws[14] = 0x00000000;
Ws[15] = 0x00000120;

是固定值么。望高手解答。

有示例代码, 另外sha-1按照TI芯片的设计是要迭代两次的。 

http://www.ti.com/lit/pdf/slva413 

http://www.ti.com/lit/pdf/slua389

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top