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 ) )   171 SHATransform(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)   284 swap_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)))
   514 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   515 #include "dbus-test.h"   519 check_sha_binary (
const unsigned char *input,
   521                   const char          *expected)
   538       _dbus_warn (
"Expected hash %s got %s for SHA-1 sum\n",
   540                   _dbus_string_get_const_data (&results));
   550 check_sha_str (
const char *input,
   551                const char *expected)
   553   return check_sha_binary (input, strlen (input), expected);
   557 decode_compact_string (
const DBusString *line,
   572       fprintf (stderr, 
"could not parse length at start of compact string: %s\n",
   573                _dbus_string_get_const_data (line));
   582       fprintf (stderr, 
"could not parse start bit 'b' in compact string: %s\n",
   583                _dbus_string_get_const_data (line));
   587   if (!(val == 0 || val == 1))
   589       fprintf (stderr, 
"the value 'b' must be 0 or 1, see sha-1/Readme.txt\n");
   598   while (next < _dbus_string_get_length (line))
   604       if (_dbus_string_get_byte (line, offset) == 
'^')
   609           fprintf (stderr, 
"could not parse bit count in compact string\n");
   614       total_bits = n_bits + val;
   615       while (n_bits < total_bits)
   617           int byte_containing_next_bit = n_bits / 8;
   618           int bit_containing_next_bit = 7 - (n_bits % 8);
   619           unsigned char old_byte;
   621           if (byte_containing_next_bit >= _dbus_string_get_length (decoded))
   627           old_byte = _dbus_string_get_byte (decoded, byte_containing_next_bit);
   628           old_byte |= current_b << bit_containing_next_bit;
   631           printf (
"Appending bit %d to byte %d at bit %d resulting in byte 0x%x\n",
   632                   current_b, byte_containing_next_bit,
   633                   bit_containing_next_bit, old_byte);
   636           _dbus_string_set_byte (decoded, byte_containing_next_bit, old_byte);
   643       current_b = !current_b;
   646   length_bytes = (n_bits / 8 + ((n_bits % 8) ? 1 : 0));
   648   if (_dbus_string_get_length (decoded) != length_bytes)
   650       fprintf (stderr, 
"Expected length %d bytes %d bits for compact string, got %d bytes\n",
   651                length_bytes, n_bits, _dbus_string_get_length (decoded));
   659 get_next_expected_result (
DBusString *results,
   673       _dbus_string_delete_leading_blanks (&line);
   675       if (_dbus_string_get_length (&line) == 0)
   694           while (i < _dbus_string_get_length (result))
   696               switch (_dbus_string_get_byte (result, i))
   699                   _dbus_string_set_byte (result, i, 
'a');
   702                   _dbus_string_set_byte (result, i, 
'b');
   705                   _dbus_string_set_byte (result, i, 
'c');
   708                   _dbus_string_set_byte (result, i, 
'd');
   711                   _dbus_string_set_byte (result, i, 
'e');
   714                   _dbus_string_set_byte (result, i, 
'f');
   738 process_test_data (
const char *test_data_dir)
   784       fprintf (stderr, 
"could not load test data file %s: %s\n",
   785                _dbus_string_get_const_data (&tests_file),
   793       fprintf (stderr, 
"could not load results data file %s: %s\n",
   794                _dbus_string_get_const_data (&results_file), error.
message);
   806       _dbus_string_delete_leading_blanks (&line);
   808       if (_dbus_string_get_length (&line) == 0)
   814           printf (
"SHA-1: %s\n", _dbus_string_get_const_data (&line));
   824               printf (
" (ending tests due to Type 3 tests seen - this is normal)\n");
   862                                       _dbus_string_get_length (&line)))
   866           if (!decode_compact_string (&line, &test))
   868               fprintf (stderr, 
"Failed to decode line %d as a compact string\n",
   876           if (!get_next_expected_result (&results, &expected))
   878               fprintf (stderr, 
"Failed to read an expected result\n");
   884               fprintf (stderr, 
" for line %d got hash %s expected %s\n",
   886                        _dbus_string_get_const_data (&result),
   887                        _dbus_string_get_const_data (&expected));
   910   printf (
"Passed the %d SHA-1 tests in the test file\n",
   930 _dbus_sha_test (
const char *test_data_dir)
   932   unsigned char all_bytes[256];
   935   if (test_data_dir != 
NULL)
   937       if (!process_test_data (test_data_dir))
   941     printf (
"No test data dir\n");
   950   if (!check_sha_binary (all_bytes, 256,
   951                          "4916d6bdb7f78e6803698cab32d1586ea457dfc8"))
   954 #define CHECK(input,expected) if (!check_sha_str (input, expected)) return FALSE   956   CHECK (
"", 
"da39a3ee5e6b4b0d3255bfef95601890afd80709");
   957   CHECK (
"a", 
"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8");
   958   CHECK (
"abc", 
"a9993e364706816aba3e25717850c26c9cd0d89d");
   959   CHECK (
"message digest", 
"c12252ceda8be8994d5fa0290a47231c1d16aae3");
   960   CHECK (
"abcdefghijklmnopqrstuvwxyz", 
"32d10c7b8cf96570ca04ce37f2a19d84240d3a89");
   961   CHECK (
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
   962          "761c457bf73b14d27e9e9265c46f4b4dda11f940");
   963   CHECK (
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
   964          "50abf5706a150990a08b2c5ea40fa0e585554732");
 dbus_bool_t _dbus_sha_final(DBusSHAContext *context, DBusString *results)
SHA finalization. 
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString. 
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_parse_int(const DBusString *str, int start, long *value_return, int *end_return)
Parses an integer contained in a DBusString. 
const char * message
public error message field 
#define NULL
A null pointer, defined appropriately for C or C++. 
dbus_uint32_t count_lo
64-bit bit count 
void _dbus_sha_init(DBusSHAContext *context)
Initializes the SHA context. 
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality. 
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_bool_t _dbus_string_starts_with_c_str(const DBusString *a, const char *c_str)
Checks whether a string starts with the given C string. 
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack. 
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
dbus_bool_t _dbus_file_get_contents(DBusString *str, const DBusString *filename, DBusError *error)
Appends the contents of the given file to the string, returning error code. 
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory. 
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string. 
dbus_uint32_t data[16]
SHA data buffer. 
dbus_bool_t _dbus_string_find(const DBusString *str, int start, const char *substr, int *found)
Finds the given substring in the string, returning TRUE and filling in the byte index where the subst...
dbus_uint32_t digest[5]
Message digest. 
Struct storing state of the SHA algorithm. 
dbus_bool_t _dbus_string_move(DBusString *source, int start, DBusString *dest, int insert_at)
Moves the end of one string into another string. 
dbus_uint32_t count_hi
No clue. 
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE. 
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string. 
void _dbus_string_skip_blank(const DBusString *str, int start, int *end)
Skips blanks from start, storing the first non-blank in *end (blank is space or tab). 
dbus_bool_t _dbus_string_pop_line(DBusString *source, DBusString *dest)
Assigns a newline-terminated or \r\n-terminated line from the front of the string to the given dest s...
void _dbus_warn(const char *format,...)
Prints a warning message to stderr. 
void _dbus_string_delete(DBusString *str, int start, int len)
Deletes a segment of a DBusString with length len starting at start. 
Object representing an exception. 
dbus_bool_t _dbus_string_append_byte(DBusString *str, unsigned char byte)
Appends a single byte to the string, returning FALSE if not enough memory. 
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(). 
#define TRUE
Expands to "1". 
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called. 
void _dbus_sha_update(DBusSHAContext *context, const DBusString *data)
Feeds more data into an existing shasum computation. 
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. 
#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...
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string. 
#define _DBUS_ZERO(object)
Sets all bits in an object to zero. 
void _dbus_string_init_const_len(DBusString *str, const char *value, int len)
Initializes a constant string with a length.