27#include "dbus-internals.h"
29#include "dbus-marshal-basic.h"
89#ifndef DOXYGEN_SHOULD_SKIP_THIS
93#define SHA_DATASIZE 64
94#define SHA_DIGESTSIZE 20
101#define f1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
102#define f2(x,y,z) ( x ^ y ^ z )
104#define f3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
105#define f4(x,y,z) ( x ^ y ^ z )
109#define K1 0x5A827999L
110#define K2 0x6ED9EBA1L
111#define K3 0x8F1BBCDCL
112#define K4 0xCA62C1D6L
116#define h0init 0x67452301L
117#define h1init 0xEFCDAB89L
118#define h2init 0x98BADCFEL
119#define h3init 0x10325476L
120#define h4init 0xC3D2E1F0L
126#define ROTL(n,X) ( ( ( X ) << n ) | ( ( X ) >> ( 32 - n ) ) )
142#define expand(W,i) ( W[ i & 15 ] = ROTL( 1, ( W[ i & 15 ] ^ W[ (i - 14) & 15 ] ^ \
143 W[ (i - 8) & 15 ] ^ W[ (i - 3) & 15 ] ) ) )
159#define subRound(a, b, c, d, e, f, k, data) \
160 ( e += ROTL( 5, a ) + f( b, c, d ) + k + data, b = ROTL( 30, b ) )
172SHATransform(dbus_uint32_t *digest, dbus_uint32_t *data)
174 dbus_uint32_t A, B, C, D, E;
175 dbus_uint32_t eData[16];
183 memmove (eData, data, SHA_DATASIZE);
186 subRound (A, B, C, D, E, f1, K1, eData[0]);
187 subRound (E, A, B, C, D, f1, K1, eData[1]);
188 subRound (D, E, A, B, C, f1, K1, eData[2]);
189 subRound (C, D, E, A, B, f1, K1, eData[3]);
190 subRound (B, C, D, E, A, f1, K1, eData[4]);
191 subRound (A, B, C, D, E, f1, K1, eData[5]);
192 subRound (E, A, B, C, D, f1, K1, eData[6]);
193 subRound (D, E, A, B, C, f1, K1, eData[7]);
194 subRound (C, D, E, A, B, f1, K1, eData[8]);
195 subRound (B, C, D, E, A, f1, K1, eData[9]);
196 subRound (A, B, C, D, E, f1, K1, eData[10]);
197 subRound (E, A, B, C, D, f1, K1, eData[11]);
198 subRound (D, E, A, B, C, f1, K1, eData[12]);
199 subRound (C, D, E, A, B, f1, K1, eData[13]);
200 subRound (B, C, D, E, A, f1, K1, eData[14]);
201 subRound (A, B, C, D, E, f1, K1, eData[15]);
202 subRound (E, A, B, C, D, f1, K1, expand ( eData, 16) );
203 subRound (D, E, A, B, C, f1, K1, expand ( eData, 17) );
204 subRound (C, D, E, A, B, f1, K1, expand ( eData, 18) );
205 subRound (B, C, D, E, A, f1, K1, expand ( eData, 19) );
207 subRound (A, B, C, D, E, f2, K2, expand ( eData, 20) );
208 subRound (E, A, B, C, D, f2, K2, expand ( eData, 21) );
209 subRound (D, E, A, B, C, f2, K2, expand ( eData, 22) );
210 subRound (C, D, E, A, B, f2, K2, expand ( eData, 23) );
211 subRound (B, C, D, E, A, f2, K2, expand ( eData, 24) );
212 subRound (A, B, C, D, E, f2, K2, expand ( eData, 25) );
213 subRound (E, A, B, C, D, f2, K2, expand ( eData, 26) );
214 subRound (D, E, A, B, C, f2, K2, expand ( eData, 27) );
215 subRound (C, D, E, A, B, f2, K2, expand ( eData, 28) );
216 subRound (B, C, D, E, A, f2, K2, expand ( eData, 29) );
217 subRound (A, B, C, D, E, f2, K2, expand ( eData, 30) );
218 subRound (E, A, B, C, D, f2, K2, expand ( eData, 31) );
219 subRound (D, E, A, B, C, f2, K2, expand ( eData, 32) );
220 subRound (C, D, E, A, B, f2, K2, expand ( eData, 33) );
221 subRound (B, C, D, E, A, f2, K2, expand ( eData, 34) );
222 subRound (A, B, C, D, E, f2, K2, expand ( eData, 35) );
223 subRound (E, A, B, C, D, f2, K2, expand ( eData, 36) );
224 subRound (D, E, A, B, C, f2, K2, expand ( eData, 37) );
225 subRound (C, D, E, A, B, f2, K2, expand ( eData, 38) );
226 subRound (B, C, D, E, A, f2, K2, expand ( eData, 39) );
228 subRound (A, B, C, D, E, f3, K3, expand ( eData, 40) );
229 subRound (E, A, B, C, D, f3, K3, expand ( eData, 41) );
230 subRound (D, E, A, B, C, f3, K3, expand ( eData, 42) );
231 subRound (C, D, E, A, B, f3, K3, expand ( eData, 43) );
232 subRound (B, C, D, E, A, f3, K3, expand ( eData, 44) );
233 subRound (A, B, C, D, E, f3, K3, expand ( eData, 45) );
234 subRound (E, A, B, C, D, f3, K3, expand ( eData, 46) );
235 subRound (D, E, A, B, C, f3, K3, expand ( eData, 47) );
236 subRound (C, D, E, A, B, f3, K3, expand ( eData, 48) );
237 subRound (B, C, D, E, A, f3, K3, expand ( eData, 49) );
238 subRound (A, B, C, D, E, f3, K3, expand ( eData, 50) );
239 subRound (E, A, B, C, D, f3, K3, expand ( eData, 51) );
240 subRound (D, E, A, B, C, f3, K3, expand ( eData, 52) );
241 subRound (C, D, E, A, B, f3, K3, expand ( eData, 53) );
242 subRound (B, C, D, E, A, f3, K3, expand ( eData, 54) );
243 subRound (A, B, C, D, E, f3, K3, expand ( eData, 55) );
244 subRound (E, A, B, C, D, f3, K3, expand ( eData, 56) );
245 subRound (D, E, A, B, C, f3, K3, expand ( eData, 57) );
246 subRound (C, D, E, A, B, f3, K3, expand ( eData, 58) );
247 subRound (B, C, D, E, A, f3, K3, expand ( eData, 59) );
249 subRound (A, B, C, D, E, f4, K4, expand ( eData, 60) );
250 subRound (E, A, B, C, D, f4, K4, expand ( eData, 61) );
251 subRound (D, E, A, B, C, f4, K4, expand ( eData, 62) );
252 subRound (C, D, E, A, B, f4, K4, expand ( eData, 63) );
253 subRound (B, C, D, E, A, f4, K4, expand ( eData, 64) );
254 subRound (A, B, C, D, E, f4, K4, expand ( eData, 65) );
255 subRound (E, A, B, C, D, f4, K4, expand ( eData, 66) );
256 subRound (D, E, A, B, C, f4, K4, expand ( eData, 67) );
257 subRound (C, D, E, A, B, f4, K4, expand ( eData, 68) );
258 subRound (B, C, D, E, A, f4, K4, expand ( eData, 69) );
259 subRound (A, B, C, D, E, f4, K4, expand ( eData, 70) );
260 subRound (E, A, B, C, D, f4, K4, expand ( eData, 71) );
261 subRound (D, E, A, B, C, f4, K4, expand ( eData, 72) );
262 subRound (C, D, E, A, B, f4, K4, expand ( eData, 73) );
263 subRound (B, C, D, E, A, f4, K4, expand ( eData, 74) );
264 subRound (A, B, C, D, E, f4, K4, expand ( eData, 75) );
265 subRound (E, A, B, C, D, f4, K4, expand ( eData, 76) );
266 subRound (D, E, A, B, C, f4, K4, expand ( eData, 77) );
267 subRound (C, D, E, A, B, f4, K4, expand ( eData, 78) );
268 subRound (B, C, D, E, A, f4, K4, expand ( eData, 79) );
281#ifdef WORDS_BIGENDIAN
282#define swap_words(buffer, byte_count)
285swap_words (dbus_uint32_t *buffer,
288 byte_count /=
sizeof (dbus_uint32_t);
291 *buffer = DBUS_UINT32_SWAP_LE_BE (*buffer);
301 context->
digest[0] = h0init;
302 context->
digest[1] = h1init;
303 context->
digest[2] = h2init;
304 context->
digest[3] = h3init;
305 context->
digest[4] = h4init;
313 const unsigned char *buffer,
317 unsigned int dataCount;
321 if (( context->
count_lo = tmp + ( ( dbus_uint32_t) count << 3) ) < tmp)
326 dataCount = (int) (tmp >> 3) & 0x3F;
331 unsigned char *p = (
unsigned char *) context->
data + dataCount;
333 dataCount = SHA_DATASIZE - dataCount;
334 if (count < dataCount)
336 memmove (p, buffer, count);
339 memmove (p, buffer, dataCount);
340 swap_words (context->
data, SHA_DATASIZE);
341 SHATransform (context->
digest, context->
data);
347 while (count >= SHA_DATASIZE)
349 memmove (context->
data, buffer, SHA_DATASIZE);
350 swap_words (context->
data, SHA_DATASIZE);
351 SHATransform (context->
digest, context->
data);
352 buffer += SHA_DATASIZE;
353 count -= SHA_DATASIZE;
357 memmove (context->
data, buffer, count);
368 unsigned char *data_p;
372 count = (count >> 3) & 0x3F;
376 data_p = (
unsigned char *) context->
data + count;
380 count = SHA_DATASIZE - 1 - count;
386 memset (data_p, 0, count);
387 swap_words (context->
data, SHA_DATASIZE);
388 SHATransform (context->
digest, context->
data);
391 memset (context->
data, 0, SHA_DATASIZE - 8);
395 memset (data_p, 0, count - 8);
401 swap_words (context->
data, SHA_DATASIZE - 8);
402 SHATransform (context->
digest, context->
data);
403 swap_words (context->
digest, SHA_DIGESTSIZE);
404 memmove (digest, context->
digest, SHA_DIGESTSIZE);
436 unsigned int inputLen;
437 const unsigned char *input;
442 sha_append (context, input, inputLen);
460 unsigned char digest[20];
462 sha_finish (context, digest);
#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_IN...
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
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.
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
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.