5 /* externally accessible functions */
7 #define SHSH(n, v) ((((v) << (n)) & 0xffffffff) | ((v) >> (32 - (n))))
9 /* this is ALMOST canonical sha1 - i took some shortcuts to make it smaller
10 * when handling the last 64 bytes in a buffer to just keep it short. it
11 * still generates decent hashes either way...
14 e_sha1_sum(unsigned char *data, int size, unsigned char *dst)
16 unsigned int digest[5], word[80], wa, wb, wc, wd, we, t;
17 unsigned char buf[64], *d;
19 const unsigned int magic[4] =
28 digest[0] = 0x67452301;
29 digest[1] = 0xefcdab89;
30 digest[2] = 0x98badcfe;
31 digest[3] = 0x10325476;
32 digest[4] = 0xc3d2e1f0;
34 memset(buf, 0, sizeof(buf));
35 for (left = size, d = data; left > 0; left--, d++)
37 if ((idx == 0) && (left < 64))
40 buf[60] = (size >> 24) & 0xff;
41 buf[61] = (size >> 16) & 0xff;
42 buf[62] = (size >> 8) & 0xff;
43 buf[63] = (size ) & 0xff;
47 if ((idx == 64) || (left == 1))
49 if ((left == 1) && (idx < 64)) buf[idx] = 0x80;
50 for (i = 0; i < 16; i++)
52 word[i] = (unsigned int)buf[(i * 4) ] << 24;
53 word[i] |= (unsigned int)buf[(i * 4) + 1] << 16;
54 word[i] |= (unsigned int)buf[(i * 4) + 2] << 8;
55 word[i] |= (unsigned int)buf[(i * 4) + 3];
57 for (i = 16; i < 80; i++)
59 word[i - 3 ] ^ word[i - 8 ] ^
60 word[i - 14] ^ word[i - 16]);
66 for (i = 0; i < 80; i++)
69 t = SHSH(5, wa) + ((wb & wc) | ((~wb) & wd)) +
70 we + word[i] + magic[0];
72 t = SHSH(5, wa) + (wb ^ wc ^ wd) +
73 we + word[i] + magic[1];
75 t = SHSH(5, wa) + ((wb & wc) | (wb & wd) | (wc & wd)) +
76 we + word[i] + magic[2];
78 t = SHSH(5, wa) + (wb ^ wc ^ wd) +
79 we + word[i] + magic[3];
95 t = htonl(digest[0]); digest[0] = t;
96 t = htonl(digest[1]); digest[1] = t;
97 t = htonl(digest[2]); digest[2] = t;
98 t = htonl(digest[3]); digest[3] = t;
99 t = htonl(digest[4]); digest[4] = t;
101 memcpy(dst, digest, 5 * 4);
105 /* local subsystem functions */