浅析sha-1和sha-256部分源码
#define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
#define F_0_19(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
#define F_20_39(x, y, z) ((x) ^ (y) ^ (z))
#define F_40_59(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
#define F_60_79(x, y, z) ((x) ^ (y) ^ (z))
#define DO_ROUND(F, K) { \
temp = ROTL(a, 5) + F(b, c, d) + e + *(W++) + K; \
e = d; \
d = c; \
c = ROTL(b, 30); \
b = a; \
a = temp; \
}
#define K_0_19 0x5a827999L
#define K_20_39 0x6ed9eba1L
#define K_40_59 0x8f1bbcdcL
#define K_60_79 0xca62c1d6L
The SHA (Secure Hash Algorithm) library.
SHA is a message digest algorithm that can be used to condense an arbitrary
void
SHA1Update (SHA1Context *sc, const void *vdata, uint32_t len)
{
const uint8_t *data = vdata;
uint32_t bufferBytesLeft;
uint32_t bytesToCopy;
int needBurn = 0;
#ifdef SHA1_FAST_COPY
if (sc->bufferLength) {
bufferBytesLeft = 64L - sc->bufferLength;
bytesToCopy = bufferBytesLeft;
if (bytesToCopy > len)
bytesToCopy = len;
memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy);
sc->totalLength += bytesToCopy * 8L; // totalLength应该是bits数目
sc->bufferLength += bytesToCopy;
data += bytesToCopy;
len -= bytesToCopy;
if (sc->bufferLength == 64L) { // 蓄积了64字节raw数据,那么进行组处理
SHA1Guts (sc, sc->buffer.words); // 以4字节word为最小单位进行digest运算,word数据依据大端模式参与
needBurn = 1;
sc->bufferLength = 0L;
}
}
while (len > 63) { // 多组64字节处理
sc->totalLength += 512L; // 64*8=512bits
SHA1Guts (sc, data);
needBurn = 1;
data += 64L;
len -= 64L;
}
if (len) {
memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); // 非64字节整倍数
sc->totalLength += len * 8L;
sc->bufferLength += len;
}
#else /* SHA1_FAST_COPY */
while (len) {
bufferBytesLeft = 64L - sc->bufferLength;
bytesToCopy = bufferBytesLeft;
if (bytesToCopy > len)
bytesToCopy = len;
memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy);
sc->totalLength += bytesToCopy * 8L;
sc->bufferLength += bytesToCopy;
data += bytesToCopy;
len -= bytesToCopy;
if (sc->bufferLength == 64L) { // 一次只处理一组64字节raw数据
SHA1Guts (sc, sc->buffer.words);
needBurn = 1;
sc->bufferLength = 0L;
}
}
#endif /* SHA1_FAST_COPY */
if (needBurn)
burnStack (sizeof (uint32_t[86]) + sizeof (uint32_t *[5]) + sizeof (int));
}
static void
SHA1Guts (SHA1Context *sc, const uint32_t *cbuf)
{
uint32_t buf[80];
uint32_t *W, *W3, *W8, *W14, *W16;
uint32_t a, b, c, d, e, temp;
int i;
W = buf;
// 从sc->buffer.words缓冲区中的16个word以大端模式拷贝到本地局部变量前16个word地址中
for (i = 15; i >= 0; i--) {
*(W++) = BYTESWAP(*cbuf); // 如果cpu为小端模式,那么需要将word转为大端格式数据
cbuf++;
}
W16 = &buf[0];
W14 = &buf[2];
W8 = &buf[8];
W3 = &buf[13];
// 相关运算,填充17-80之间这64个word空间,这样前0-15为原始数据,16-79为打散数据[luther.gliethttp]
for (i = 63; i >= 0; i--) { // 进行全相关运算,数据打散,4字节为最小单位,64+16=80个word
*W = *(W3++) ^ *(W8++) ^ *(W14++) ^ *(W16++);
*W = ROTL(*W, 1);
W++;
}
// 至此80个word信息摘要数据源准备完毕,接下来使用这80个word数据计算出
// 一组新的a,b,c,d,e作为sc->hash[]数组的最终hash值[luther.gliethttp]
// ok,从sc->hash[]中获取a,b,c,d,e
// (hash初始值在SHA1Init()中被建立,之后每16个word数据将执行本函数SHA1Guts
// 从上一组16word计算的hash结果中接续计算新的a,b,c,d,e,这样前一组16words和
// 此次的16个words就由a,b,c,d,e关联上了,
// 于是sha就完成了对数据流进行digest摘要的工作)
a = sc->hash[0]; // hash初始向量或上一组16word数据计算出的结果[luther.gliethttp]
b = sc->hash[1];
c = sc->hash[2];
d = sc->hash[3];
e = sc->hash[4];
W = buf; // W指向了这80个word,下面将使用W指向的80个word计算新的a,b,c,d,e
#ifndef SHA1_UNROLL
#define SHA1_UNROLL 20 不论是SHA1_UNROLL定义为1还是20会执行DO_ROUND函数20次
// 如果为1表示20次将使用for循环执行得到,代码占用空间降低,但是效率下降
// 如果为20表示20次将使用直接顺序写入20条DO_ROUND函数实现,代码占用空间增加,但是效率提高[luther.gliethttp]
// 因为这样20*4=80个word数据才能遍历完成.
#endif /* !SHA1_UNROLL */
//printf("SHA1_UNROLL=%d\r\n", SHA1_UNROLL);
#if SHA1_UNROLL == 1 // 滚1次
for (i = 19; i >= 0; i--)
DO_ROUND(F_0_19, K_0_19); // 将W[0-19]与abcde参与运算,根据W[0]运算出temp,然后e=d,d=c,c=ROTL(b,30),b=a,a=temp,就像渗水一样,从a一层层的将上一层数据渗给下一层,轮换出一组新的足够打散的a,b,c,d,e
for (i = 19; i >= 0; i--)
DO_ROUND(F_20_39, K_20_39);
for (i = 19; i >= 0; i--)
DO_ROUND(F_40_59, K_40_59);
for (i = 19; i >= 0; i--)
DO_ROUND(F_60_79, K_60_79);
#elif SHA1_UNROLL == 2
......
#else /* SHA1_UNROLL */
#error SHA1_UNROLL must be 1, 2, 4, 5, 10 or 20!
#endif
sc->hash[0] += a;
sc->hash[1] += b;
sc->hash[2] += c;
sc->hash[3] += d;
sc->hash[4] += e;
}
阅读(1821) | 评论(0) | 转发(0) |