delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kwalletd / backend / sha1.cc
blob8f4984868fbf0bce7c2aa9bcdccb3a234d0cd244
1 /* This file is part of the KDE project
2 Copyright (C) 2001 George Staikos <staikos@kde.org>
3 Based heavily on SHA1 code from GPG 1.0.3 (C) 1998 FSF
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "sha1.h"
23 #include <config-kwalletbackend.h>
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_BITYPES_H
26 #include <sys/bitypes.h> /* For uintXX_t on Tru64 */
27 #endif
28 #ifdef HAVE_STDINT_H
29 #include <stdint.h>
30 #endif
32 #include <string.h>
33 #include <QtCore/QBool>
35 // FIXME: this can be optimized to one instruction on most cpus.
36 #define rol(x,y) ((x << y) | (x >> (32-y)))
39 #define K1 0x5a827999L
40 #define K2 0x6ed9eba1L
41 #define K3 0x8f1bbcdcL
42 #define K4 0xca62c1d6L
43 #define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
44 #define F2(x,y,z) ( x ^ y ^ z )
45 #define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
46 #define F4(x,y,z) ( x ^ y ^ z )
48 #define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \
49 ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
50 , (x[i&0x0f] = (tm << 1) | (tm >> 31)))
52 #define R(a,b,c,d,e,f,k,m) do { e += rol(a, 5) \
53 + f(b, c, d) \
54 + k \
55 + m; \
56 b = rol(b, 30); \
57 } while(0)
60 SHA1::SHA1() {
61 _hashlen = 160;
62 _init = false;
63 reset();
67 int SHA1::reset() {
68 _h0 = 0x67452301;
69 _h1 = 0xefcdab89;
70 _h2 = 0x98badcfe;
71 _h3 = 0x10325476;
72 _h4 = 0xc3d2e1f0;
73 _nblocks = 0;
74 _count = 0;
75 memset(_buf, 0, 56); // clear the buffer
77 _init = true;
78 return 0;
82 int SHA1::size() const {
83 return _hashlen;
87 SHA1::~SHA1() {
92 void SHA1::transform(void *data) {
93 unsigned int a, b, c, d, e, tm;
94 unsigned int x[16];
95 unsigned char *_data = (unsigned char *)data;
97 a = _h0;
98 b = _h1;
99 c = _h2;
100 d = _h3;
101 e = _h4;
103 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
104 memcpy(x, _data, 64);
105 #else
106 int i;
107 unsigned char *p2;
108 for (i = 0, p2 = (unsigned char *)x;
109 i < 16; i++, p2 += 4) {
110 p2[3] = *_data++;
111 p2[2] = *_data++;
112 p2[1] = *_data++;
113 p2[0] = *_data++;
115 #endif
117 R(a, b, c, d, e, F1, K1, x[ 0]);
118 R(e, a, b, c, d, F1, K1, x[ 1]);
119 R(d, e, a, b, c, F1, K1, x[ 2]);
120 R(c, d, e, a, b, F1, K1, x[ 3]);
121 R(b, c, d, e, a, F1, K1, x[ 4]);
122 R(a, b, c, d, e, F1, K1, x[ 5]);
123 R(e, a, b, c, d, F1, K1, x[ 6]);
124 R(d, e, a, b, c, F1, K1, x[ 7]);
125 R(c, d, e, a, b, F1, K1, x[ 8]);
126 R(b, c, d, e, a, F1, K1, x[ 9]);
127 R(a, b, c, d, e, F1, K1, x[10]);
128 R(e, a, b, c, d, F1, K1, x[11]);
129 R(d, e, a, b, c, F1, K1, x[12]);
130 R(c, d, e, a, b, F1, K1, x[13]);
131 R(b, c, d, e, a, F1, K1, x[14]);
132 R(a, b, c, d, e, F1, K1, x[15]);
133 R(e, a, b, c, d, F1, K1, M(16));
134 R(d, e, a, b, c, F1, K1, M(17));
135 R(c, d, e, a, b, F1, K1, M(18));
136 R(b, c, d, e, a, F1, K1, M(19));
137 R(a, b, c, d, e, F2, K2, M(20));
138 R(e, a, b, c, d, F2, K2, M(21));
139 R(d, e, a, b, c, F2, K2, M(22));
140 R(c, d, e, a, b, F2, K2, M(23));
141 R(b, c, d, e, a, F2, K2, M(24));
142 R(a, b, c, d, e, F2, K2, M(25));
143 R(e, a, b, c, d, F2, K2, M(26));
144 R(d, e, a, b, c, F2, K2, M(27));
145 R(c, d, e, a, b, F2, K2, M(28));
146 R(b, c, d, e, a, F2, K2, M(29));
147 R(a, b, c, d, e, F2, K2, M(30));
148 R(e, a, b, c, d, F2, K2, M(31));
149 R(d, e, a, b, c, F2, K2, M(32));
150 R(c, d, e, a, b, F2, K2, M(33));
151 R(b, c, d, e, a, F2, K2, M(34));
152 R(a, b, c, d, e, F2, K2, M(35));
153 R(e, a, b, c, d, F2, K2, M(36));
154 R(d, e, a, b, c, F2, K2, M(37));
155 R(c, d, e, a, b, F2, K2, M(38));
156 R(b, c, d, e, a, F2, K2, M(39));
157 R(a, b, c, d, e, F3, K3, M(40));
158 R(e, a, b, c, d, F3, K3, M(41));
159 R(d, e, a, b, c, F3, K3, M(42));
160 R(c, d, e, a, b, F3, K3, M(43));
161 R(b, c, d, e, a, F3, K3, M(44));
162 R(a, b, c, d, e, F3, K3, M(45));
163 R(e, a, b, c, d, F3, K3, M(46));
164 R(d, e, a, b, c, F3, K3, M(47));
165 R(c, d, e, a, b, F3, K3, M(48));
166 R(b, c, d, e, a, F3, K3, M(49));
167 R(a, b, c, d, e, F3, K3, M(50));
168 R(e, a, b, c, d, F3, K3, M(51));
169 R(d, e, a, b, c, F3, K3, M(52));
170 R(c, d, e, a, b, F3, K3, M(53));
171 R(b, c, d, e, a, F3, K3, M(54));
172 R(a, b, c, d, e, F3, K3, M(55));
173 R(e, a, b, c, d, F3, K3, M(56));
174 R(d, e, a, b, c, F3, K3, M(57));
175 R(c, d, e, a, b, F3, K3, M(58));
176 R(b, c, d, e, a, F3, K3, M(59));
177 R(a, b, c, d, e, F4, K4, M(60));
178 R(e, a, b, c, d, F4, K4, M(61));
179 R(d, e, a, b, c, F4, K4, M(62));
180 R(c, d, e, a, b, F4, K4, M(63));
181 R(b, c, d, e, a, F4, K4, M(64));
182 R(a, b, c, d, e, F4, K4, M(65));
183 R(e, a, b, c, d, F4, K4, M(66));
184 R(d, e, a, b, c, F4, K4, M(67));
185 R(c, d, e, a, b, F4, K4, M(68));
186 R(b, c, d, e, a, F4, K4, M(69));
187 R(a, b, c, d, e, F4, K4, M(70));
188 R(e, a, b, c, d, F4, K4, M(71));
189 R(d, e, a, b, c, F4, K4, M(72));
190 R(c, d, e, a, b, F4, K4, M(73));
191 R(b, c, d, e, a, F4, K4, M(74));
192 R(a, b, c, d, e, F4, K4, M(75));
193 R(e, a, b, c, d, F4, K4, M(76));
194 R(d, e, a, b, c, F4, K4, M(77));
195 R(c, d, e, a, b, F4, K4, M(78));
196 R(b, c, d, e, a, F4, K4, M(79));
198 _h0 += a;
199 _h1 += b;
200 _h2 += c;
201 _h3 += d;
202 _h4 += e;
207 bool SHA1::readyToGo() const {
208 return _init;
212 int SHA1::process(const void *block, int len) {
213 if (!_init) {
214 return -1;
217 unsigned char *_block = (unsigned char *)block;
219 int cnt = 0;
220 // Flush the buffer before proceeding
221 if (_count == 64) {
222 transform(_buf);
223 _count = 0;
224 _nblocks++;
227 if (!_block) {
228 return 0;
231 if (_count) {
232 for (; len && _count < 64; --len, ++cnt) {
233 _buf[_count++] = *_block++;
235 process(0, 0); // flush the buffer if necessary
236 if (!len) {
237 return cnt;
241 while (len >= 64) {
242 transform(_block);
243 _count = 0;
244 _nblocks++;
245 len -= 64;
246 cnt += 64;
247 _block += 64;
250 for (; len && _count < 64; --len, ++cnt) {
251 _buf[_count++] = *_block++;
254 return cnt;
258 const unsigned char *SHA1::hash() {
259 unsigned int t, msb, lsb;
260 unsigned char *p;
263 if (!_init) {
264 return (unsigned char *)_buf;
267 process(0, 0);
269 msb = 0;
270 t = _nblocks;
272 if ((lsb = t << 6) < t) {
273 msb++;
276 msb += t >> 26;
277 t = lsb;
279 if ((lsb = t + _count) < t) {
280 msb++;
283 t = lsb;
285 if ((lsb = t << 3) < t) {
286 msb++;
289 msb += t >> 29;
291 _buf[_count++] = 0x80;
293 if (_count < 56) {
294 while (_count < 56) {
295 _buf[_count++] = 0;
297 } else {
298 while (_count < 64) {
299 _buf[_count++] = 0;
301 process(0, 0);
302 memset(_buf, 0, 56);
305 _buf[56] = msb >> 24;
306 _buf[57] = msb >> 16;
307 _buf[58] = msb >> 8;
308 _buf[59] = msb;
309 _buf[60] = lsb >> 24;
310 _buf[61] = lsb >> 16;
311 _buf[62] = lsb >> 8;
312 _buf[63] = lsb;
314 transform(_buf);
316 p = _buf;
318 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
319 #define X(a) do { *(uint32_t *)p = _h##a; p += 4; } while (0)
320 #else
321 #define X(a) do { *p++ = _h##a >> 24; *p++ = _h##a >> 16; \
322 *p++ = _h##a >> 8; *p++ = _h##a; } while (0)
323 #endif
325 X(0);
326 X(1);
327 X(2);
328 X(3);
329 X(4);
331 #undef X
333 _init = false;
335 return (unsigned char *)_buf;