26 #include "dbus-internals.h" 28 #include "dbus-marshal-basic.h" 29 #include <dbus/dbus-test-tap.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 ) ) 172 SHATransform(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) 285 swap_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;
439 input = (
const unsigned char*) _dbus_string_get_const_data (data);
440 inputLen = _dbus_string_get_length (data);
442 sha_append (context, input, inputLen);
460 unsigned char digest[20];
462 sha_finish (context, digest);
501 _dbus_string_get_length (ascii_output)))
515 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 516 #include "dbus-test.h" 520 check_sha_binary (
const unsigned char *input,
522 const char *expected)
532 _dbus_test_fatal (
"no memory for SHA-1 results");
535 _dbus_test_fatal (
"no memory for SHA-1 results");
539 _dbus_warn (
"Expected hash %s got %s for SHA-1 sum",
541 _dbus_string_get_const_data (&results));
551 check_sha_str (
const char *input,
552 const char *expected)
554 return check_sha_binary ((
unsigned char *) input, strlen (input), expected);
558 decode_compact_string (
const DBusString *line,
573 fprintf (stderr,
"could not parse length at start of compact string: %s\n",
574 _dbus_string_get_const_data (line));
583 fprintf (stderr,
"could not parse start bit 'b' in compact string: %s\n",
584 _dbus_string_get_const_data (line));
588 if (!(val == 0 || val == 1))
590 fprintf (stderr,
"the value 'b' must be 0 or 1, see sha-1/Readme.txt\n");
599 while (next < _dbus_string_get_length (line))
605 if (_dbus_string_get_byte (line, offset) ==
'^')
610 fprintf (stderr,
"could not parse bit count in compact string\n");
615 total_bits = n_bits + val;
616 while (n_bits < total_bits)
618 int byte_containing_next_bit = n_bits / 8;
619 int bit_containing_next_bit = 7 - (n_bits % 8);
620 unsigned char old_byte;
622 if (byte_containing_next_bit >= _dbus_string_get_length (decoded))
625 _dbus_test_fatal (
"no memory to extend to next byte");
628 old_byte = _dbus_string_get_byte (decoded, byte_containing_next_bit);
629 old_byte |= current_b << bit_containing_next_bit;
632 _dbus_test_diag (
"Appending bit %d to byte %d at bit %d resulting in byte 0x%x",
633 current_b, byte_containing_next_bit,
634 bit_containing_next_bit, old_byte);
637 _dbus_string_set_byte (decoded, byte_containing_next_bit, old_byte);
644 current_b = !current_b;
647 length_bytes = (n_bits / 8 + ((n_bits % 8) ? 1 : 0));
649 if (_dbus_string_get_length (decoded) != length_bytes)
651 fprintf (stderr,
"Expected length %d bytes %d bits for compact string, got %d bytes\n",
652 length_bytes, n_bits, _dbus_string_get_length (decoded));
660 get_next_expected_result (
DBusString *results,
669 _dbus_test_fatal (
"no memory");
674 _dbus_string_delete_leading_blanks (&line);
676 if (_dbus_string_get_length (&line) == 0)
692 _dbus_test_fatal (
"no memory");
695 while (i < _dbus_string_get_length (result))
697 unsigned char c = _dbus_string_get_byte (result, i);
702 _dbus_string_set_byte (result, i,
'a');
705 _dbus_string_set_byte (result, i,
'b');
708 _dbus_string_set_byte (result, i,
'c');
711 _dbus_string_set_byte (result, i,
'd');
714 _dbus_string_set_byte (result, i,
'e');
717 _dbus_string_set_byte (result, i,
'f');
725 if ((c < '0' || c >
'9') && (c < 'a' || c >
'f'))
726 _dbus_test_fatal (
"invalid SHA-1 test script");
744 process_test_data (
const char *test_data_dir)
760 _dbus_test_fatal (
"no memory");
763 _dbus_test_fatal (
"no memory");
766 _dbus_test_fatal (
"no memory");
769 _dbus_test_fatal (
"no memory");
772 _dbus_test_fatal (
"no memory");
775 _dbus_test_fatal (
"no memory");
778 _dbus_test_fatal (
"no memory");
782 _dbus_test_fatal (
"no memory");
786 _dbus_test_fatal (
"no memory");
790 fprintf (stderr,
"could not load test data file %s: %s\n",
791 _dbus_string_get_const_data (&tests_file),
799 fprintf (stderr,
"could not load results data file %s: %s\n",
800 _dbus_string_get_const_data (&results_file), error.
message);
812 _dbus_string_delete_leading_blanks (&line);
814 if (_dbus_string_get_length (&line) == 0)
820 _dbus_test_diag (
"SHA-1: %s", _dbus_string_get_const_data (&line));
830 _dbus_test_diag (
" (ending tests due to Type 3 tests seen - this is normal)");
848 _dbus_test_fatal (
"no memory");
851 _dbus_test_fatal (
"no memory");
854 _dbus_test_fatal (
"no memory");
857 _dbus_test_fatal (
"no memory");
868 _dbus_string_get_length (&line)))
869 _dbus_test_fatal (
"no memory");
872 if (!decode_compact_string (&line, &test))
874 fprintf (stderr,
"Failed to decode line %d as a compact string\n",
880 _dbus_test_fatal (
"no memory for SHA-1 result");
882 if (!get_next_expected_result (&results, &expected))
884 fprintf (stderr,
"Failed to read an expected result\n");
890 fprintf (stderr,
" for line %d got hash %s expected %s\n",
892 _dbus_string_get_const_data (&result),
893 _dbus_string_get_const_data (&expected));
916 _dbus_test_diag (
"Passed the %d SHA-1 tests in the test file",
936 _dbus_sha_test (
const char *test_data_dir)
938 unsigned char all_bytes[256];
941 if (test_data_dir !=
NULL)
943 if (!process_test_data (test_data_dir))
947 _dbus_test_diag (
"No test data dir");
956 if (!check_sha_binary (all_bytes, 256,
957 "4916d6bdb7f78e6803698cab32d1586ea457dfc8"))
960 #define CHECK(input,expected) if (!check_sha_str (input, expected)) return FALSE 962 CHECK (
"",
"da39a3ee5e6b4b0d3255bfef95601890afd80709");
963 CHECK (
"a",
"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8");
964 CHECK (
"abc",
"a9993e364706816aba3e25717850c26c9cd0d89d");
965 CHECK (
"message digest",
"c12252ceda8be8994d5fa0290a47231c1d16aae3");
966 CHECK (
"abcdefghijklmnopqrstuvwxyz",
"32d10c7b8cf96570ca04ce37f2a19d84240d3a89");
967 CHECK (
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
968 "761c457bf73b14d27e9e9265c46f4b4dda11f940");
969 CHECK (
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
970 "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(), and fills it with the same contents as #_DBUS_STRING_I...
#define TRUE
Expands to "1".
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.