26#include "dbus-internals.h"
28#include "dbus-marshal-basic.h"
88#ifndef DOXYGEN_SHOULD_SKIP_THIS
92#define SHA_DATASIZE 64
93#define SHA_DIGESTSIZE 20
100#define f1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
101#define f2(x,y,z) ( x ^ y ^ z )
103#define f3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
104#define f4(x,y,z) ( x ^ y ^ z )
108#define K1 0x5A827999L
109#define K2 0x6ED9EBA1L
110#define K3 0x8F1BBCDCL
111#define K4 0xCA62C1D6L
115#define h0init 0x67452301L
116#define h1init 0xEFCDAB89L
117#define h2init 0x98BADCFEL
118#define h3init 0x10325476L
119#define h4init 0xC3D2E1F0L
125#define ROTL(n,X) ( ( ( X ) << n ) | ( ( X ) >> ( 32 - n ) ) )
141#define expand(W,i) ( W[ i & 15 ] = ROTL( 1, ( W[ i & 15 ] ^ W[ (i - 14) & 15 ] ^ \
142 W[ (i - 8) & 15 ] ^ W[ (i - 3) & 15 ] ) ) )
158#define subRound(a, b, c, d, e, f, k, data) \
159 ( e += ROTL( 5, a ) + f( b, c, d ) + k + data, b = ROTL( 30, b ) )
171SHATransform(dbus_uint32_t *digest, dbus_uint32_t *data)
173 dbus_uint32_t A, B, C, D, E;
174 dbus_uint32_t eData[16];
182 memmove (eData, data, SHA_DATASIZE);
185 subRound (A, B, C, D, E, f1, K1, eData[0]);
186 subRound (E, A, B, C, D, f1, K1, eData[1]);
187 subRound (D, E, A, B, C, f1, K1, eData[2]);
188 subRound (C, D, E, A, B, f1, K1, eData[3]);
189 subRound (B, C, D, E, A, f1, K1, eData[4]);
190 subRound (A, B, C, D, E, f1, K1, eData[5]);
191 subRound (E, A, B, C, D, f1, K1, eData[6]);
192 subRound (D, E, A, B, C, f1, K1, eData[7]);
193 subRound (C, D, E, A, B, f1, K1, eData[8]);
194 subRound (B, C, D, E, A, f1, K1, eData[9]);
195 subRound (A, B, C, D, E, f1, K1, eData[10]);
196 subRound (E, A, B, C, D, f1, K1, eData[11]);
197 subRound (D, E, A, B, C, f1, K1, eData[12]);
198 subRound (C, D, E, A, B, f1, K1, eData[13]);
199 subRound (B, C, D, E, A, f1, K1, eData[14]);
200 subRound (A, B, C, D, E, f1, K1, eData[15]);
201 subRound (E, A, B, C, D, f1, K1, expand ( eData, 16) );
202 subRound (D, E, A, B, C, f1, K1, expand ( eData, 17) );
203 subRound (C, D, E, A, B, f1, K1, expand ( eData, 18) );
204 subRound (B, C, D, E, A, f1, K1, expand ( eData, 19) );
206 subRound (A, B, C, D, E, f2, K2, expand ( eData, 20) );
207 subRound (E, A, B, C, D, f2, K2, expand ( eData, 21) );
208 subRound (D, E, A, B, C, f2, K2, expand ( eData, 22) );
209 subRound (C, D, E, A, B, f2, K2, expand ( eData, 23) );
210 subRound (B, C, D, E, A, f2, K2, expand ( eData, 24) );
211 subRound (A, B, C, D, E, f2, K2, expand ( eData, 25) );
212 subRound (E, A, B, C, D, f2, K2, expand ( eData, 26) );
213 subRound (D, E, A, B, C, f2, K2, expand ( eData, 27) );
214 subRound (C, D, E, A, B, f2, K2, expand ( eData, 28) );
215 subRound (B, C, D, E, A, f2, K2, expand ( eData, 29) );
216 subRound (A, B, C, D, E, f2, K2, expand ( eData, 30) );
217 subRound (E, A, B, C, D, f2, K2, expand ( eData, 31) );
218 subRound (D, E, A, B, C, f2, K2, expand ( eData, 32) );
219 subRound (C, D, E, A, B, f2, K2, expand ( eData, 33) );
220 subRound (B, C, D, E, A, f2, K2, expand ( eData, 34) );
221 subRound (A, B, C, D, E, f2, K2, expand ( eData, 35) );
222 subRound (E, A, B, C, D, f2, K2, expand ( eData, 36) );
223 subRound (D, E, A, B, C, f2, K2, expand ( eData, 37) );
224 subRound (C, D, E, A, B, f2, K2, expand ( eData, 38) );
225 subRound (B, C, D, E, A, f2, K2, expand ( eData, 39) );
227 subRound (A, B, C, D, E, f3, K3, expand ( eData, 40) );
228 subRound (E, A, B, C, D, f3, K3, expand ( eData, 41) );
229 subRound (D, E, A, B, C, f3, K3, expand ( eData, 42) );
230 subRound (C, D, E, A, B, f3, K3, expand ( eData, 43) );
231 subRound (B, C, D, E, A, f3, K3, expand ( eData, 44) );
232 subRound (A, B, C, D, E, f3, K3, expand ( eData, 45) );
233 subRound (E, A, B, C, D, f3, K3, expand ( eData, 46) );
234 subRound (D, E, A, B, C, f3, K3, expand ( eData, 47) );
235 subRound (C, D, E, A, B, f3, K3, expand ( eData, 48) );
236 subRound (B, C, D, E, A, f3, K3, expand ( eData, 49) );
237 subRound (A, B, C, D, E, f3, K3, expand ( eData, 50) );
238 subRound (E, A, B, C, D, f3, K3, expand ( eData, 51) );
239 subRound (D, E, A, B, C, f3, K3, expand ( eData, 52) );
240 subRound (C, D, E, A, B, f3, K3, expand ( eData, 53) );
241 subRound (B, C, D, E, A, f3, K3, expand ( eData, 54) );
242 subRound (A, B, C, D, E, f3, K3, expand ( eData, 55) );
243 subRound (E, A, B, C, D, f3, K3, expand ( eData, 56) );
244 subRound (D, E, A, B, C, f3, K3, expand ( eData, 57) );
245 subRound (C, D, E, A, B, f3, K3, expand ( eData, 58) );
246 subRound (B, C, D, E, A, f3, K3, expand ( eData, 59) );
248 subRound (A, B, C, D, E, f4, K4, expand ( eData, 60) );
249 subRound (E, A, B, C, D, f4, K4, expand ( eData, 61) );
250 subRound (D, E, A, B, C, f4, K4, expand ( eData, 62) );
251 subRound (C, D, E, A, B, f4, K4, expand ( eData, 63) );
252 subRound (B, C, D, E, A, f4, K4, expand ( eData, 64) );
253 subRound (A, B, C, D, E, f4, K4, expand ( eData, 65) );
254 subRound (E, A, B, C, D, f4, K4, expand ( eData, 66) );
255 subRound (D, E, A, B, C, f4, K4, expand ( eData, 67) );
256 subRound (C, D, E, A, B, f4, K4, expand ( eData, 68) );
257 subRound (B, C, D, E, A, f4, K4, expand ( eData, 69) );
258 subRound (A, B, C, D, E, f4, K4, expand ( eData, 70) );
259 subRound (E, A, B, C, D, f4, K4, expand ( eData, 71) );
260 subRound (D, E, A, B, C, f4, K4, expand ( eData, 72) );
261 subRound (C, D, E, A, B, f4, K4, expand ( eData, 73) );
262 subRound (B, C, D, E, A, f4, K4, expand ( eData, 74) );
263 subRound (A, B, C, D, E, f4, K4, expand ( eData, 75) );
264 subRound (E, A, B, C, D, f4, K4, expand ( eData, 76) );
265 subRound (D, E, A, B, C, f4, K4, expand ( eData, 77) );
266 subRound (C, D, E, A, B, f4, K4, expand ( eData, 78) );
267 subRound (B, C, D, E, A, f4, K4, expand ( eData, 79) );
280#ifdef WORDS_BIGENDIAN
281#define swap_words(buffer, byte_count)
284swap_words (dbus_uint32_t *buffer,
287 byte_count /=
sizeof (dbus_uint32_t);
290 *buffer = DBUS_UINT32_SWAP_LE_BE (*buffer);
300 context->
digest[0] = h0init;
301 context->
digest[1] = h1init;
302 context->
digest[2] = h2init;
303 context->
digest[3] = h3init;
304 context->
digest[4] = h4init;
312 const unsigned char *buffer,
316 unsigned int dataCount;
320 if (( context->
count_lo = tmp + ( ( dbus_uint32_t) count << 3) ) < tmp)
325 dataCount = (int) (tmp >> 3) & 0x3F;
330 unsigned char *p = (
unsigned char *) context->
data + dataCount;
332 dataCount = SHA_DATASIZE - dataCount;
333 if (count < dataCount)
335 memmove (p, buffer, count);
338 memmove (p, buffer, dataCount);
339 swap_words (context->
data, SHA_DATASIZE);
340 SHATransform (context->
digest, context->
data);
346 while (count >= SHA_DATASIZE)
348 memmove (context->
data, buffer, SHA_DATASIZE);
349 swap_words (context->
data, SHA_DATASIZE);
350 SHATransform (context->
digest, context->
data);
351 buffer += SHA_DATASIZE;
352 count -= SHA_DATASIZE;
356 memmove (context->
data, buffer, count);
367 unsigned char *data_p;
371 count = (count >> 3) & 0x3F;
375 data_p = (
unsigned char *) context->
data + count;
379 count = SHA_DATASIZE - 1 - count;
385 memset (data_p, 0, count);
386 swap_words (context->
data, SHA_DATASIZE);
387 SHATransform (context->
digest, context->
data);
390 memset (context->
data, 0, SHA_DATASIZE - 8);
394 memset (data_p, 0, count - 8);
400 swap_words (context->
data, SHA_DATASIZE - 8);
401 SHATransform (context->
digest, context->
data);
402 swap_words (context->
digest, SHA_DIGESTSIZE);
403 memmove (digest, context->
digest, SHA_DIGESTSIZE);
435 unsigned int inputLen;
436 const unsigned char *input;
438 input = (
const unsigned char*) _dbus_string_get_const_data (data);
439 inputLen = _dbus_string_get_length (data);
441 sha_append (context, input, inputLen);
459 unsigned char digest[20];
461 sha_finish (context, digest);
500 _dbus_string_get_length (ascii_output)))
#define _DBUS_ZERO(object)
Sets all bits in an object to zero.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
dbus_bool_t _dbus_sha_compute(const DBusString *data, DBusString *ascii_output)
Computes the ASCII hex-encoded shasum of the given data and appends it to the output string.
void _dbus_sha_init(DBusSHAContext *context)
Initializes the SHA context.
void _dbus_sha_update(DBusSHAContext *context, const DBusString *data)
Feeds more data into an existing shasum computation.
dbus_bool_t _dbus_sha_final(DBusSHAContext *context, DBusString *results)
SHA finalization.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
dbus_bool_t _dbus_string_append_len(DBusString *str, const char *buffer, int len)
Appends block of bytes with the given length to a DBusString.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
dbus_bool_t _dbus_string_hex_encode(const DBusString *source, int start, DBusString *dest, int insert_at)
Encodes a string in hex, the way MD5 and SHA-1 are usually encoded.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Struct storing state of the SHA algorithm.
dbus_uint32_t digest[5]
Message digest.
dbus_uint32_t data[16]
SHA data buffer.
dbus_uint32_t count_lo
64-bit bit count
dbus_uint32_t count_hi
No clue.