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 ) )
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)
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;
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;
441 sha_append (context, input, inputLen);
459 unsigned char digest[20];
461 sha_finish (context, digest);
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",
550 check_sha_str (
const char *input,
551 const char *expected)
553 return check_sha_binary ((
unsigned char *) 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",
582 fprintf (stderr,
"could not parse start bit 'b' in compact string: %s\n",
587 if (!(val == 0 || val == 1))
589 fprintf (stderr,
"the value 'b' must be 0 or 1, see sha-1/Readme.txt\n");
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;
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);
643 current_b = !current_b;
646 length_bytes = (n_bits / 8 + ((n_bits % 8) ? 1 : 0));
650 fprintf (stderr,
"Expected length %d bytes %d bits for compact string, got %d bytes\n",
659 get_next_expected_result (
DBusString *results,
673 _dbus_string_delete_leading_blanks (&line);
724 if ((c < '0' || c >
'9') && (c < 'a' || c >
'f'))
743 process_test_data (
const char *test_data_dir)
789 fprintf (stderr,
"could not load test data file %s: %s\n",
798 fprintf (stderr,
"could not load results data file %s: %s\n",
811 _dbus_string_delete_leading_blanks (&line);
829 printf (
" (ending tests due to Type 3 tests seen - this is normal)\n");
871 if (!decode_compact_string (&line, &test))
873 fprintf (stderr,
"Failed to decode line %d as a compact string\n",
881 if (!get_next_expected_result (&results, &expected))
883 fprintf (stderr,
"Failed to read an expected result\n");
889 fprintf (stderr,
" for line %d got hash %s expected %s\n",
915 printf (
"Passed the %d SHA-1 tests in the test file\n",
935 _dbus_sha_test (
const char *test_data_dir)
937 unsigned char all_bytes[256];
940 if (test_data_dir !=
NULL)
942 if (!process_test_data (test_data_dir))
946 printf (
"No test data dir\n");
955 if (!check_sha_binary (all_bytes, 256,
956 "4916d6bdb7f78e6803698cab32d1586ea457dfc8"))
959 #define CHECK(input,expected) if (!check_sha_str (input, expected)) return FALSE
961 CHECK (
"",
"da39a3ee5e6b4b0d3255bfef95601890afd80709");
962 CHECK (
"a",
"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8");
963 CHECK (
"abc",
"a9993e364706816aba3e25717850c26c9cd0d89d");
964 CHECK (
"message digest",
"c12252ceda8be8994d5fa0290a47231c1d16aae3");
965 CHECK (
"abcdefghijklmnopqrstuvwxyz",
"32d10c7b8cf96570ca04ce37f2a19d84240d3a89");
966 CHECK (
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
967 "761c457bf73b14d27e9e9265c46f4b4dda11f940");
968 CHECK (
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
969 "50abf5706a150990a08b2c5ea40fa0e585554732");