|
27 | 27 | #include <bncsutil/bsha1.h> |
28 | 28 | #include <cstring> |
29 | 29 |
|
30 | | -#define USE_NEW_BSHA1 0 |
| 30 | +#define USE_NEW_BSHA1 0 |
31 | 31 |
|
32 | 32 | #define BSHA_IC1 0x67452301lu |
33 | 33 | #define BSHA_IC2 0xEFCDAB89lu |
|
41 | 41 | #define BSHA_OC4 0x359D3E2Alu |
42 | 42 |
|
43 | 43 | #if !USE_NEW_BSHA1 |
44 | | -# define BSHA_COP e = d; d = c; c = ROL(b, 30); b = a; a = g; |
| 44 | +# define BSHA_COP e = d; d = c; c = ROL(b, 30); b = a; a = g; |
45 | 45 | #else |
46 | | -# define BSHA_N_COP t[4] = t[3]; t[3] = t[2]; t[2] = ROL(t[1], 30); \ |
47 | | - t[1] = t[0]; t[0] = x |
| 46 | +# define BSHA_N_COP t[4] = t[3]; t[3] = t[2]; t[2] = ROL(t[1], 30); \ |
| 47 | + t[1] = t[0]; t[0] = x |
48 | 48 | #endif |
49 | 49 |
|
50 | 50 | #if !USE_NEW_BSHA1 |
|
60 | 60 | #else |
61 | 61 |
|
62 | 62 | #define BSHA_N_OP1() x = LSB4(*p++) + ROL(t[0], 5) + t[4] + \ |
63 | | - ((t[1] & t[2]) | (~t[1] & t[3])) + BSHA_OC1; BSHA_N_COP |
| 63 | + ((t[1] & t[2]) | (~t[1] & t[3])) + BSHA_OC1; BSHA_N_COP |
64 | 64 | #define BSHA_N_OP2() x = (t[3] ^ t[2] ^ t[1]) + t[4] + ROL(x, 5) + \ |
65 | | - LSB4(*p++) + BSHA_OC2; BSHA_N_COP |
| 65 | + LSB4(*p++) + BSHA_OC2; BSHA_N_COP |
66 | 66 | #define BSHA_N_OP3() x = LSB4(*p++) + ROL(x, 5) + t[4] + \ |
67 | | - ((t[2] & t[1]) | (t[3] & t[2]) | (t[3] & t[1])) - BSHA_OC3; BSHA_N_COP |
| 67 | + ((t[2] & t[1]) | (t[3] & t[2]) | (t[3] & t[1])) - BSHA_OC3; BSHA_N_COP |
68 | 68 | #define BSHA_N_OP4() x = (t[3] ^ t[2] ^ t[1]) + t[4] + ROL(x, 5) + \ |
69 | | - LSB4(*p++) - BSHA_OC4; BSHA_N_COP |
| 69 | + LSB4(*p++) - BSHA_OC4; BSHA_N_COP |
70 | 70 | #endif |
71 | 71 |
|
72 | 72 | #if USE_NEW_BSHA1 |
73 | 73 | MEXP(void) calcHashBuf(const char* input, unsigned int length, char* result) { |
74 | | - uint32_t vals[5]; |
75 | | - uint32_t t[5]; // a, b, c, d, e |
76 | | - uint32_t buf[0x50]; |
77 | | - uint32_t* p; |
78 | | - uint32_t x; |
79 | | - const char* in = input; |
80 | | - unsigned int i, j; |
81 | | - unsigned int sub_length; |
82 | | - |
83 | | - /* Initializer Values */ |
84 | | - p = vals; |
85 | | - *p++ = BSHA_IC1; |
86 | | - *p++ = BSHA_IC2; |
87 | | - *p++ = BSHA_IC3; |
88 | | - *p++ = BSHA_IC4; |
89 | | - *p++ = BSHA_IC5; |
90 | | - |
91 | | - memset(buf, 0, 320); // zero buf |
92 | | - |
93 | | - /* Process input in chunks. */ |
94 | | - for (i = 0; i < length; i += 0x40) { |
95 | | - sub_length = length - i; |
96 | | - |
97 | | - /* Maximum chunk size is 0x40 (64) bytes. */ |
98 | | - if (sub_length > 0x40) |
99 | | - sub_length = 0x40; |
100 | | - |
101 | | - memcpy(buf, in, sub_length); |
102 | | - in += sub_length; |
103 | | - |
104 | | - /* If necessary, pad with zeroes to 64 bytes. */ |
105 | | - if (sub_length < 0x40) |
106 | | - memset(buf + sub_length, 0, 0x40 - sub_length); |
107 | | - |
108 | | - for (j = 0; j < 64; j++) { |
109 | | - buf[j + 16] = |
110 | | - LSB4(ROL(1, LSB4(buf[j] ^ buf[j+8] ^ buf[j+2] ^ buf[j+13]) % 32)); |
111 | | - } |
112 | | - |
113 | | - memcpy(t, vals, 20); |
114 | | - p = buf; |
115 | | - |
116 | | - /* It's a kind of magic. */ |
117 | | - BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); |
118 | | - BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); |
119 | | - |
120 | | - BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); |
121 | | - BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); |
122 | | - |
123 | | - BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); |
124 | | - BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); |
125 | | - |
126 | | - BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); |
127 | | - BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); |
128 | | - |
129 | | - vals[0] += t[0]; |
130 | | - vals[1] += t[1]; |
131 | | - vals[2] += t[2]; |
132 | | - vals[3] += t[3]; |
133 | | - vals[4] += t[4]; |
134 | | - } |
135 | | - |
136 | | - /* Return result. */ |
137 | | - memcpy(result, vals, 20); |
| 74 | + uint32_t vals[5]; |
| 75 | + uint32_t t[5]; // a, b, c, d, e |
| 76 | + uint32_t buf[0x50]; |
| 77 | + uint32_t* p; |
| 78 | + uint32_t x; |
| 79 | + const char* in = input; |
| 80 | + unsigned int i, j; |
| 81 | + unsigned int sub_length; |
| 82 | + |
| 83 | + /* Initializer Values */ |
| 84 | + p = vals; |
| 85 | + *p++ = BSHA_IC1; |
| 86 | + *p++ = BSHA_IC2; |
| 87 | + *p++ = BSHA_IC3; |
| 88 | + *p++ = BSHA_IC4; |
| 89 | + *p++ = BSHA_IC5; |
| 90 | + |
| 91 | + memset(buf, 0, 320); // zero buf |
| 92 | + |
| 93 | + /* Process input in chunks. */ |
| 94 | + for (i = 0; i < length; i += 0x40) { |
| 95 | + sub_length = length - i; |
| 96 | + |
| 97 | + /* Maximum chunk size is 0x40 (64) bytes. */ |
| 98 | + if (sub_length > 0x40) |
| 99 | + sub_length = 0x40; |
| 100 | + |
| 101 | + memcpy(buf, in, sub_length); |
| 102 | + in += sub_length; |
| 103 | + |
| 104 | + /* If necessary, pad with zeroes to 64 bytes. */ |
| 105 | + if (sub_length < 0x40) |
| 106 | + memset(buf + sub_length, 0, 0x40 - sub_length); |
| 107 | + |
| 108 | + for (j = 0; j < 64; j++) { |
| 109 | + buf[j + 16] = |
| 110 | + LSB4(ROL(1, LSB4(buf[j] ^ buf[j+8] ^ buf[j+2] ^ buf[j+13]) % 32)); |
| 111 | + } |
| 112 | + |
| 113 | + memcpy(t, vals, 20); |
| 114 | + p = buf; |
| 115 | + |
| 116 | + /* It's a kind of magic. */ |
| 117 | + BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); |
| 118 | + BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); BSHA_N_OP1(); |
| 119 | + |
| 120 | + BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); |
| 121 | + BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); BSHA_N_OP2(); |
| 122 | + |
| 123 | + BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); |
| 124 | + BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); BSHA_N_OP3(); |
| 125 | + |
| 126 | + BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); |
| 127 | + BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); BSHA_N_OP4(); |
| 128 | + |
| 129 | + vals[0] += t[0]; |
| 130 | + vals[1] += t[1]; |
| 131 | + vals[2] += t[2]; |
| 132 | + vals[3] += t[3]; |
| 133 | + vals[4] += t[4]; |
| 134 | + } |
| 135 | + |
| 136 | + /* Return result. */ |
| 137 | + memcpy(result, vals, 20); |
138 | 138 | } |
139 | 139 |
|
140 | 140 | #else |
141 | 141 |
|
142 | 142 | MEXP(void) calcHashBuf(const char* input, size_t length, char* result) { |
143 | 143 | int i; |
144 | 144 | uint32_t a, b, c, d, e, g; |
145 | | - uint32_t* ldata; |
| 145 | + uint32_t* ldata; |
146 | 146 | char data[1024]; |
147 | 147 | memset(data, 0, 1024); |
148 | 148 | memcpy(data, input, length); |
149 | 149 | ldata = (uint32_t*) data; |
150 | | - |
| 150 | + |
151 | 151 | for (i = 0; i < 64; i++) { |
152 | 152 | ldata[i + 16] = |
153 | 153 | LSB4(ROL(1, LSB4(ldata[i] ^ ldata[i+8] ^ ldata[i+2] ^ ldata[i+13]) % 32)); |
154 | 154 | } |
155 | | - |
156 | | - //dumpbuf(data, 1024); |
157 | | - |
| 155 | + |
| 156 | + //dumpbuf(data, 1024); |
| 157 | + |
158 | 158 | a = BSHA_IC1; |
159 | 159 | b = BSHA_IC2; |
160 | 160 | c = BSHA_IC3; |
161 | 161 | d = BSHA_IC4; |
162 | 162 | e = BSHA_IC5; |
163 | 163 | g = 0; |
164 | | - |
| 164 | + |
165 | 165 | // Loops unrolled. |
166 | 166 | BSHA_OP1(a, b, c, d, e, *ldata++, g) BSHA_OP1(a, b, c, d, e, *ldata++, g) |
167 | 167 | BSHA_OP1(a, b, c, d, e, *ldata++, g) BSHA_OP1(a, b, c, d, e, *ldata++, g) |
|
0 commit comments