1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | /* |
41 | Based on the public domain implementation of the SHA-1 algorithm |
42 | Copyright (C) Dominik Reichl <dominik.reichl@t-online.de> |
43 | */ |
44 | #include <QtCore/qendian.h> |
45 | |
46 | #ifdef Q_CC_MSVC |
47 | # include <stdlib.h> |
48 | #endif |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | // Test Vectors (from FIPS PUB 180-1) |
53 | // |
54 | // SHA1("abc") = |
55 | // A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D |
56 | // |
57 | // SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = |
58 | // 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 |
59 | // |
60 | // SHA1(A million repetitions of "a") = |
61 | // 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F |
62 | // |
63 | |
64 | |
65 | // #define or #undef this, if you want the to wipe all |
66 | // temporary variables after processing |
67 | #define SHA1_WIPE_VARIABLES |
68 | |
69 | |
70 | struct Sha1State |
71 | { |
72 | quint32 h0; |
73 | quint32 h1; |
74 | quint32 h2; |
75 | quint32 h3; |
76 | quint32 h4; |
77 | |
78 | quint64 messageSize; |
79 | unsigned char buffer[64]; |
80 | }; |
81 | |
82 | |
83 | typedef union |
84 | { |
85 | quint8 bytes[64]; |
86 | quint32 words[16]; |
87 | } Sha1Chunk; |
88 | |
89 | static inline quint32 rol32(quint32 value, unsigned int shift) |
90 | { |
91 | #ifdef Q_CC_MSVC |
92 | return _rotl(value, shift); |
93 | #else |
94 | return ((value << shift) | (value >> (32 - shift))); |
95 | #endif |
96 | } |
97 | |
98 | static inline quint32 sha1Word(Sha1Chunk *chunk, const uint position) |
99 | { |
100 | return (chunk->words[position & 0xf] = rol32( value: chunk->words[(position+13) & 0xf] |
101 | ^ chunk->words[(position+ 8) & 0xf] |
102 | ^ chunk->words[(position+ 2) & 0xf] |
103 | ^ chunk->words[(position) & 0xf], shift: 1)); |
104 | } |
105 | |
106 | static inline void sha1Round0(Sha1Chunk *chunk, const uint position, |
107 | quint32 &v, quint32 &w, quint32 &x, quint32 &y, quint32 &z) |
108 | { |
109 | z += ((( w & (x ^ y)) ^ y) + chunk->words[position] + 0x5A827999 + rol32(value: v, shift: 5)); |
110 | w = rol32(value: w, shift: 30); |
111 | } |
112 | |
113 | static inline void sha1Round1(Sha1Chunk *chunk, const uint position, |
114 | quint32 &v, quint32 &w, quint32 &x, quint32 &y, quint32 &z) |
115 | { |
116 | z += ((( w & (x ^ y)) ^ y) + sha1Word(chunk,position) + 0x5A827999 + rol32(value: v, shift: 5)); |
117 | w = rol32(value: w, shift: 30); |
118 | } |
119 | |
120 | static inline void sha1Round2(Sha1Chunk *chunk, const uint position, |
121 | quint32 &v, quint32 &w, quint32 &x, quint32 &y, quint32 &z) |
122 | { |
123 | z += (( w ^ x ^ y) + sha1Word(chunk, position) + 0x6ED9EBA1 + rol32(value: v, shift: 5)); |
124 | w = rol32(value: w, shift: 30); |
125 | } |
126 | |
127 | static inline void sha1Round3(Sha1Chunk *chunk, const uint position, |
128 | quint32 &v, quint32 &w, quint32 &x, quint32 &y, quint32 &z) |
129 | { |
130 | z += (((( w | x) & y) | (w & x)) + sha1Word(chunk, position) + 0x8F1BBCDC + rol32(value: v, shift: 5)); |
131 | w = rol32(value: w, shift: 30); |
132 | } |
133 | |
134 | static inline void sha1Round4(Sha1Chunk *chunk, const uint position, |
135 | quint32 &v, quint32 &w, quint32 &x, quint32 &y, quint32 &z) |
136 | { |
137 | z += ((w ^ x ^ y) + sha1Word(chunk, position) + 0xCA62C1D6 + rol32(value: v, shift: 5)); |
138 | w = rol32(value: w, shift: 30); |
139 | } |
140 | |
141 | static inline void sha1ProcessChunk(Sha1State *state, const unsigned char *buffer) |
142 | { |
143 | // Copy state[] to working vars |
144 | quint32 a = state->h0; |
145 | quint32 b = state->h1; |
146 | quint32 c = state->h2; |
147 | quint32 d = state->h3; |
148 | quint32 e = state->h4; |
149 | |
150 | quint8 chunkBuffer[64]; |
151 | memcpy(dest: chunkBuffer, src: buffer, n: 64); |
152 | |
153 | Sha1Chunk *chunk = reinterpret_cast<Sha1Chunk*>(&chunkBuffer); |
154 | |
155 | for (int i = 0; i < 16; ++i) |
156 | chunk->words[i] = qFromBigEndian(source: chunk->words[i]); |
157 | |
158 | sha1Round0(chunk, position: 0, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round0(chunk, position: 1, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round0(chunk, position: 2, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round0(chunk, position: 3, v&: c,w&: d,x&: e,y&: a,z&: b); |
159 | sha1Round0(chunk, position: 4, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round0(chunk, position: 5, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round0(chunk, position: 6, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round0(chunk, position: 7, v&: d,w&: e,x&: a,y&: b,z&: c); |
160 | sha1Round0(chunk, position: 8, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round0(chunk, position: 9, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round0(chunk, position: 10, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round0(chunk, position: 11, v&: e,w&: a,x&: b,y&: c,z&: d); |
161 | sha1Round0(chunk, position: 12, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round0(chunk, position: 13, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round0(chunk, position: 14, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round0(chunk, position: 15, v&: a,w&: b,x&: c,y&: d,z&: e); |
162 | sha1Round1(chunk, position: 16, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round1(chunk, position: 17, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round1(chunk, position: 18, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round1(chunk, position: 19, v&: b,w&: c,x&: d,y&: e,z&: a); |
163 | sha1Round2(chunk, position: 20, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round2(chunk, position: 21, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round2(chunk, position: 22, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round2(chunk, position: 23, v&: c,w&: d,x&: e,y&: a,z&: b); |
164 | sha1Round2(chunk, position: 24, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round2(chunk, position: 25, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round2(chunk, position: 26, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round2(chunk, position: 27, v&: d,w&: e,x&: a,y&: b,z&: c); |
165 | sha1Round2(chunk, position: 28, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round2(chunk, position: 29, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round2(chunk, position: 30, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round2(chunk, position: 31, v&: e,w&: a,x&: b,y&: c,z&: d); |
166 | sha1Round2(chunk, position: 32, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round2(chunk, position: 33, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round2(chunk, position: 34, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round2(chunk, position: 35, v&: a,w&: b,x&: c,y&: d,z&: e); |
167 | sha1Round2(chunk, position: 36, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round2(chunk, position: 37, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round2(chunk, position: 38, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round2(chunk, position: 39, v&: b,w&: c,x&: d,y&: e,z&: a); |
168 | sha1Round3(chunk, position: 40, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round3(chunk, position: 41, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round3(chunk, position: 42, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round3(chunk, position: 43, v&: c,w&: d,x&: e,y&: a,z&: b); |
169 | sha1Round3(chunk, position: 44, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round3(chunk, position: 45, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round3(chunk, position: 46, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round3(chunk, position: 47, v&: d,w&: e,x&: a,y&: b,z&: c); |
170 | sha1Round3(chunk, position: 48, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round3(chunk, position: 49, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round3(chunk, position: 50, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round3(chunk, position: 51, v&: e,w&: a,x&: b,y&: c,z&: d); |
171 | sha1Round3(chunk, position: 52, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round3(chunk, position: 53, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round3(chunk, position: 54, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round3(chunk, position: 55, v&: a,w&: b,x&: c,y&: d,z&: e); |
172 | sha1Round3(chunk, position: 56, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round3(chunk, position: 57, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round3(chunk, position: 58, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round3(chunk, position: 59, v&: b,w&: c,x&: d,y&: e,z&: a); |
173 | sha1Round4(chunk, position: 60, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round4(chunk, position: 61, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round4(chunk, position: 62, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round4(chunk, position: 63, v&: c,w&: d,x&: e,y&: a,z&: b); |
174 | sha1Round4(chunk, position: 64, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round4(chunk, position: 65, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round4(chunk, position: 66, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round4(chunk, position: 67, v&: d,w&: e,x&: a,y&: b,z&: c); |
175 | sha1Round4(chunk, position: 68, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round4(chunk, position: 69, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round4(chunk, position: 70, v&: a,w&: b,x&: c,y&: d,z&: e); sha1Round4(chunk, position: 71, v&: e,w&: a,x&: b,y&: c,z&: d); |
176 | sha1Round4(chunk, position: 72, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round4(chunk, position: 73, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round4(chunk, position: 74, v&: b,w&: c,x&: d,y&: e,z&: a); sha1Round4(chunk, position: 75, v&: a,w&: b,x&: c,y&: d,z&: e); |
177 | sha1Round4(chunk, position: 76, v&: e,w&: a,x&: b,y&: c,z&: d); sha1Round4(chunk, position: 77, v&: d,w&: e,x&: a,y&: b,z&: c); sha1Round4(chunk, position: 78, v&: c,w&: d,x&: e,y&: a,z&: b); sha1Round4(chunk, position: 79, v&: b,w&: c,x&: d,y&: e,z&: a); |
178 | |
179 | // Add the working vars back into state |
180 | state->h0 += a; |
181 | state->h1 += b; |
182 | state->h2 += c; |
183 | state->h3 += d; |
184 | state->h4 += e; |
185 | |
186 | // Wipe variables |
187 | #ifdef SHA1_WIPE_VARIABLES |
188 | a = b = c = d = e = 0; |
189 | memset(s: chunkBuffer, c: 0, n: 64); |
190 | #endif |
191 | } |
192 | |
193 | static inline void sha1InitState(Sha1State *state) |
194 | { |
195 | state->h0 = 0x67452301; |
196 | state->h1 = 0xEFCDAB89; |
197 | state->h2 = 0x98BADCFE; |
198 | state->h3 = 0x10325476; |
199 | state->h4 = 0xC3D2E1F0; |
200 | |
201 | state->messageSize = 0; |
202 | } |
203 | |
204 | static inline void sha1Update(Sha1State *state, const unsigned char *data, qint64 len) |
205 | { |
206 | quint32 rest = static_cast<quint32>(state->messageSize & Q_UINT64_C(63)); |
207 | |
208 | quint64 availableData = static_cast<quint64>(len) + static_cast<quint64>(rest); |
209 | state->messageSize += len; |
210 | |
211 | if (availableData < Q_UINT64_C(64)) { |
212 | memcpy(dest: &state->buffer[rest], src: &data[0], n: len); |
213 | |
214 | } else { |
215 | qint64 i = static_cast<qint64>(64 - rest); |
216 | memcpy(dest: &state->buffer[rest], src: &data[0], n: static_cast<qint32>(i)); |
217 | sha1ProcessChunk(state, buffer: state->buffer); |
218 | |
219 | qint64 lastI = len - ((len + rest) & Q_INT64_C(63)); |
220 | for( ; i < lastI; i += 64) |
221 | sha1ProcessChunk(state, buffer: &data[i]); |
222 | |
223 | memcpy(dest: &state->buffer[0], src: &data[i], n: len - i); |
224 | } |
225 | } |
226 | |
227 | static inline void sha1FinalizeState(Sha1State *state) |
228 | { |
229 | quint64 messageSize = state->messageSize; |
230 | unsigned char sizeInBits[8]; |
231 | qToBigEndian(src: messageSize << 3, dest: sizeInBits); |
232 | |
233 | sha1Update(state, data: (const unsigned char *)"\200" , len: 1); |
234 | |
235 | unsigned char zero[64]; |
236 | memset(s: zero, c: 0, n: 64); |
237 | if (static_cast<int>(messageSize & 63) > 56 - 1) { |
238 | sha1Update(state, data: zero, len: 64 - 1 - static_cast<int>(messageSize & 63)); |
239 | sha1Update(state, data: zero, len: 64 - 8); |
240 | } else { |
241 | sha1Update(state, data: zero, len: 64 - 1 - 8 - static_cast<int>(messageSize & 63)); |
242 | } |
243 | |
244 | sha1Update(state, data: sizeInBits, len: 8); |
245 | #ifdef SHA1_WIPE_VARIABLES |
246 | memset(s: state->buffer, c: 0, n: 64); |
247 | memset(s: zero, c: 0, n: 64); |
248 | state->messageSize = 0; |
249 | #endif |
250 | } |
251 | |
252 | static inline void sha1ToHash(Sha1State *state, unsigned char* buffer) |
253 | { |
254 | qToBigEndian(src: state->h0, dest: buffer); |
255 | qToBigEndian(src: state->h1, dest: buffer + 4); |
256 | qToBigEndian(src: state->h2, dest: buffer + 8); |
257 | qToBigEndian(src: state->h3, dest: buffer + 12); |
258 | qToBigEndian(src: state->h4, dest: buffer + 16); |
259 | } |
260 | |
261 | QT_END_NAMESPACE |
262 | |