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.
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 */
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) \
75 memset(_buf
, 0, 56); // clear the buffer
82 int SHA1::size() const {
92 void SHA1::transform(void *data
) {
93 unsigned int a
, b
, c
, d
, e
, tm
;
95 unsigned char *_data
= (unsigned char *)data
;
103 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
104 memcpy(x
, _data
, 64);
108 for (i
= 0, p2
= (unsigned char *)x
;
109 i
< 16; i
++, p2
+= 4) {
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));
207 bool SHA1::readyToGo() const {
212 int SHA1::process(const void *block
, int len
) {
217 unsigned char *_block
= (unsigned char *)block
;
220 // Flush the buffer before proceeding
232 for (; len
&& _count
< 64; --len
, ++cnt
) {
233 _buf
[_count
++] = *_block
++;
235 process(0, 0); // flush the buffer if necessary
250 for (; len
&& _count
< 64; --len
, ++cnt
) {
251 _buf
[_count
++] = *_block
++;
258 const unsigned char *SHA1::hash() {
259 unsigned int t
, msb
, lsb
;
264 return (unsigned char *)_buf
;
272 if ((lsb
= t
<< 6) < t
) {
279 if ((lsb
= t
+ _count
) < t
) {
285 if ((lsb
= t
<< 3) < t
) {
291 _buf
[_count
++] = 0x80;
294 while (_count
< 56) {
298 while (_count
< 64) {
305 _buf
[56] = msb
>> 24;
306 _buf
[57] = msb
>> 16;
309 _buf
[60] = lsb
>> 24;
310 _buf
[61] = lsb
>> 16;
318 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
319 #define X(a) do { *(uint32_t *)p = _h##a; p += 4; } while (0)
321 #define X(a) do { *p++ = _h##a >> 24; *p++ = _h##a >> 16; \
322 *p++ = _h##a >> 8; *p++ = _h##a; } while (0)
335 return (unsigned char *)_buf
;