n810: Add firmware generator hotplug scripts
[openwrt.git] / package / ead / src / sha1.c
1 /*
2 * SHA transform algorithm, originally taken from code written by
3 * Peter Gutmann, and placed in the public domain.
4 */
5
6 static uint32_t
7 rol32(uint32_t word, int shift)
8 {
9 return (word << shift) | (word >> (32 - shift));
10 }
11
12 /* The SHA f()-functions. */
13
14 #define f1(x,y,z) (z ^ (x & (y ^ z))) /* x ? y : z */
15 #define f2(x,y,z) (x ^ y ^ z) /* XOR */
16 #define f3(x,y,z) ((x & y) + (z & (x ^ y))) /* majority */
17
18 /* The SHA Mysterious Constants */
19
20 #define K1 0x5A827999L /* Rounds 0-19: sqrt(2) * 2^30 */
21 #define K2 0x6ED9EBA1L /* Rounds 20-39: sqrt(3) * 2^30 */
22 #define K3 0x8F1BBCDCL /* Rounds 40-59: sqrt(5) * 2^30 */
23 #define K4 0xCA62C1D6L /* Rounds 60-79: sqrt(10) * 2^30 */
24
25 /**
26 * sha_transform - single block SHA1 transform
27 *
28 * @digest: 160 bit digest to update
29 * @data: 512 bits of data to hash
30 * @W: 80 words of workspace (see note)
31 *
32 * This function generates a SHA1 digest for a single 512-bit block.
33 * Be warned, it does not handle padding and message digest, do not
34 * confuse it with the full FIPS 180-1 digest algorithm for variable
35 * length messages.
36 *
37 * Note: If the hash is security sensitive, the caller should be sure
38 * to clear the workspace. This is left to the caller to avoid
39 * unnecessary clears between chained hashing operations.
40 */
41 static void sha_transform(uint32_t *digest, const unsigned char *in, uint32_t *W)
42 {
43 uint32_t a, b, c, d, e, t, i;
44
45 for (i = 0; i < 16; i++) {
46 int ofs = 4 * i;
47
48 /* word load/store may be unaligned here, so use bytes instead */
49 W[i] =
50 (in[ofs+0] << 24) |
51 (in[ofs+1] << 16) |
52 (in[ofs+2] << 8) |
53 in[ofs+3];
54 }
55
56 for (i = 0; i < 64; i++)
57 W[i+16] = rol32(W[i+13] ^ W[i+8] ^ W[i+2] ^ W[i], 1);
58
59 a = digest[0];
60 b = digest[1];
61 c = digest[2];
62 d = digest[3];
63 e = digest[4];
64
65 for (i = 0; i < 20; i++) {
66 t = f1(b, c, d) + K1 + rol32(a, 5) + e + W[i];
67 e = d; d = c; c = rol32(b, 30); b = a; a = t;
68 }
69
70 for (; i < 40; i ++) {
71 t = f2(b, c, d) + K2 + rol32(a, 5) + e + W[i];
72 e = d; d = c; c = rol32(b, 30); b = a; a = t;
73 }
74
75 for (; i < 60; i ++) {
76 t = f3(b, c, d) + K3 + rol32(a, 5) + e + W[i];
77 e = d; d = c; c = rol32(b, 30); b = a; a = t;
78 }
79
80 for (; i < 80; i ++) {
81 t = f2(b, c, d) + K4 + rol32(a, 5) + e + W[i];
82 e = d; d = c; c = rol32(b, 30); b = a; a = t;
83 }
84
85 digest[0] += a;
86 digest[1] += b;
87 digest[2] += c;
88 digest[3] += d;
89 digest[4] += e;
90 }
91
92 /**
93 * sha_init - initialize the vectors for a SHA1 digest
94 * @buf: vector to initialize
95 */
96 static void sha_init(uint32_t *buf)
97 {
98 buf[0] = 0x67452301;
99 buf[1] = 0xefcdab89;
100 buf[2] = 0x98badcfe;
101 buf[3] = 0x10325476;
102 buf[4] = 0xc3d2e1f0;
103 }
104
This page took 0.046969 seconds and 5 git commands to generate.