D-Bus  1.13.7
dbus-sha.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sha.c SHA-1 implementation
3  *
4  * Copyright (C) 2003 Red Hat Inc.
5  * Copyright (C) 1995 A. M. Kuchling
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include <config.h>
26 #include "dbus-internals.h"
27 #include "dbus-sha.h"
28 #include "dbus-marshal-basic.h" /* for byteswap routines */
29 #include <dbus/dbus-test-tap.h>
30 #include <string.h>
31 
32 /* The following comments have the history of where this code
33  * comes from. I actually copied it from GNet in GNOME CVS.
34  * - hp@redhat.com
35  */
36 
37 /*
38  * sha.h : Implementation of the Secure Hash Algorithm
39  *
40  * Part of the Python Cryptography Toolkit, version 1.0.0
41  *
42  * Copyright (C) 1995, A.M. Kuchling
43  *
44  * Distribute and use freely; there are no restrictions on further
45  * dissemination and usage except those imposed by the laws of your
46  * country of residence.
47  *
48  */
49 
50 /* SHA: NIST's Secure Hash Algorithm */
51 
52 /* Based on SHA code originally posted to sci.crypt by Peter Gutmann
53  in message <30ajo5$oe8@ccu2.auckland.ac.nz>.
54  Modified to test for endianness on creation of SHA objects by AMK.
55  Also, the original specification of SHA was found to have a weakness
56  by NSA/NIST. This code implements the fixed version of SHA.
57 */
58 
59 /* Here's the first paragraph of Peter Gutmann's posting:
60 
61 The following is my SHA (FIPS 180) code updated to allow use of the "fixed"
62 SHA, thanks to Jim Gillogly and an anonymous contributor for the information on
63 what's changed in the new version. The fix is a simple change which involves
64 adding a single rotate in the initial expansion function. It is unknown
65 whether this is an optimal solution to the problem which was discovered in the
66 SHA or whether it's simply a bandaid which fixes the problem with a minimum of
67 effort (for example the reengineering of a great many Capstone chips).
68 */
69 
89 #ifndef DOXYGEN_SHOULD_SKIP_THIS
90 
91 /* The SHA block size and message digest sizes, in bytes */
92 
93 #define SHA_DATASIZE 64
94 #define SHA_DIGESTSIZE 20
95 
96 /* The SHA f()-functions. The f1 and f3 functions can be optimized to
97  save one boolean operation each - thanks to Rich Schroeppel,
98  rcs@cs.arizona.edu for discovering this */
99 
100 /*#define f1(x,y,z) ( ( x & y ) | ( ~x & z ) ) // Rounds 0-19 */
101 #define f1(x,y,z) ( z ^ ( x & ( y ^ z ) ) ) /* Rounds 0-19 */
102 #define f2(x,y,z) ( x ^ y ^ z ) /* Rounds 20-39 */
103 /*#define f3(x,y,z) ( ( x & y ) | ( x & z ) | ( y & z ) ) // Rounds 40-59 */
104 #define f3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) ) /* Rounds 40-59 */
105 #define f4(x,y,z) ( x ^ y ^ z ) /* Rounds 60-79 */
106 
107 /* The SHA Mysterious Constants */
108 
109 #define K1 0x5A827999L /* Rounds 0-19 */
110 #define K2 0x6ED9EBA1L /* Rounds 20-39 */
111 #define K3 0x8F1BBCDCL /* Rounds 40-59 */
112 #define K4 0xCA62C1D6L /* Rounds 60-79 */
113 
114 /* SHA initial values */
115 
116 #define h0init 0x67452301L
117 #define h1init 0xEFCDAB89L
118 #define h2init 0x98BADCFEL
119 #define h3init 0x10325476L
120 #define h4init 0xC3D2E1F0L
121 
122 /* Note that it may be necessary to add parentheses to these macros if they
123  are to be called with expressions as arguments */
124 /* 32-bit rotate left - kludged with shifts */
125 
126 #define ROTL(n,X) ( ( ( X ) << n ) | ( ( X ) >> ( 32 - n ) ) )
127 
128 /* The initial expanding function. The hash function is defined over an
129  80-word expanded input array W, where the first 16 are copies of the input
130  data, and the remaining 64 are defined by
131 
132  W[ i ] = W[ i - 16 ] ^ W[ i - 14 ] ^ W[ i - 8 ] ^ W[ i - 3 ]
133 
134  This implementation generates these values on the fly in a circular
135  buffer - thanks to Colin Plumb, colin@nyx10.cs.du.edu for this
136  optimization.
137 
138  The updated SHA changes the expanding function by adding a rotate of 1
139  bit. Thanks to Jim Gillogly, jim@rand.org, and an anonymous contributor
140  for this information */
141 
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 ] ) ) )
144 
145 
146 /* The prototype SHA sub-round. The fundamental sub-round is:
147 
148  a' = e + ROTL( 5, a ) + f( b, c, d ) + k + data;
149  b' = a;
150  c' = ROTL( 30, b );
151  d' = c;
152  e' = d;
153 
154  but this is implemented by unrolling the loop 5 times and renaming the
155  variables ( e, a, b, c, d ) = ( a', b', c', d', e' ) each iteration.
156  This code is then replicated 20 times for each of the 4 functions, using
157  the next 20 values from the W[] array each time */
158 
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 ) )
161 
162 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */
163 
164 /* Perform the SHA transformation. Note that this code, like MD5, seems to
165  break some optimizing compilers due to the complexity of the expressions
166  and the size of the basic block. It may be necessary to split it into
167  sections, e.g. based on the four subrounds
168 
169  Note that this corrupts the context->data area */
170 
171 static void
172 SHATransform(dbus_uint32_t *digest, dbus_uint32_t *data)
173 {
174  dbus_uint32_t A, B, C, D, E; /* Local vars */
175  dbus_uint32_t eData[16]; /* Expanded data */
176 
177  /* Set up first buffer and local data buffer */
178  A = digest[0];
179  B = digest[1];
180  C = digest[2];
181  D = digest[3];
182  E = digest[4];
183  memmove (eData, data, SHA_DATASIZE);
184 
185  /* Heavy mangling, in 4 sub-rounds of 20 interations each. */
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) );
206 
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) );
227 
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) );
248 
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) );
269 
270  /* Build message digest */
271  digest[0] += A;
272  digest[1] += B;
273  digest[2] += C;
274  digest[3] += D;
275  digest[4] += E;
276 }
277 
278 /* When run on a little-endian CPU we need to perform byte reversal on an
279  array of longwords. */
280 
281 #ifdef WORDS_BIGENDIAN
282 #define swap_words(buffer, byte_count)
283 #else
284 static void
285 swap_words (dbus_uint32_t *buffer,
286  int byte_count)
287 {
288  byte_count /= sizeof (dbus_uint32_t);
289  while (byte_count--)
290  {
291  *buffer = DBUS_UINT32_SWAP_LE_BE (*buffer);
292  ++buffer;
293  }
294 }
295 #endif
296 
297 static void
298 sha_init (DBusSHAContext *context)
299 {
300  /* Set the h-vars to their initial values */
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;
306 
307  /* Initialise bit count */
308  context->count_lo = context->count_hi = 0;
309 }
310 
311 static void
312 sha_append (DBusSHAContext *context,
313  const unsigned char *buffer,
314  unsigned int count)
315 {
316  dbus_uint32_t tmp;
317  unsigned int dataCount;
318 
319  /* Update bitcount */
320  tmp = context->count_lo;
321  if (( context->count_lo = tmp + ( ( dbus_uint32_t) count << 3) ) < tmp)
322  context->count_hi++; /* Carry from low to high */
323  context->count_hi += count >> 29;
324 
325  /* Get count of bytes already in data */
326  dataCount = (int) (tmp >> 3) & 0x3F;
327 
328  /* Handle any leading odd-sized chunks */
329  if (dataCount)
330  {
331  unsigned char *p = (unsigned char *) context->data + dataCount;
332 
333  dataCount = SHA_DATASIZE - dataCount;
334  if (count < dataCount)
335  {
336  memmove (p, buffer, count);
337  return;
338  }
339  memmove (p, buffer, dataCount);
340  swap_words (context->data, SHA_DATASIZE);
341  SHATransform (context->digest, context->data);
342  buffer += dataCount;
343  count -= dataCount;
344  }
345 
346  /* Process data in SHA_DATASIZE chunks */
347  while (count >= SHA_DATASIZE)
348  {
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;
354  }
355 
356  /* Handle any remaining bytes of data. */
357  memmove (context->data, buffer, count);
358 }
359 
360 
361 /* Final wrapup - pad to SHA_DATASIZE-byte boundary with the bit pattern
362  1 0* (64-bit count of bits processed, MSB-first) */
363 
364 static void
365 sha_finish (DBusSHAContext *context, unsigned char digest[20])
366 {
367  int count;
368  unsigned char *data_p;
369 
370  /* Compute number of bytes mod 64 */
371  count = (int) context->count_lo;
372  count = (count >> 3) & 0x3F;
373 
374  /* Set the first char of padding to 0x80. This is safe since there is
375  always at least one byte free */
376  data_p = (unsigned char *) context->data + count;
377  *data_p++ = 0x80;
378 
379  /* Bytes of padding needed to make 64 bytes */
380  count = SHA_DATASIZE - 1 - count;
381 
382  /* Pad out to 56 mod 64 */
383  if (count < 8)
384  {
385  /* Two lots of padding: Pad the first block to 64 bytes */
386  memset (data_p, 0, count);
387  swap_words (context->data, SHA_DATASIZE);
388  SHATransform (context->digest, context->data);
389 
390  /* Now fill the next block with 56 bytes */
391  memset (context->data, 0, SHA_DATASIZE - 8);
392  }
393  else
394  /* Pad block to 56 bytes */
395  memset (data_p, 0, count - 8);
396 
397  /* Append length in bits and transform */
398  context->data[14] = context->count_hi;
399  context->data[15] = context->count_lo;
400 
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);
405 }
406  /* End of internals */
408 
420 void
422 {
423  sha_init (context);
424 }
425 
432 void
434  const DBusString *data)
435 {
436  unsigned int inputLen;
437  const unsigned char *input;
438 
439  input = (const unsigned char*) _dbus_string_get_const_data (data);
440  inputLen = _dbus_string_get_length (data);
441 
442  sha_append (context, input, inputLen);
443 }
444 
458  DBusString *results)
459 {
460  unsigned char digest[20];
461 
462  sha_finish (context, digest);
463 
464  if (!_dbus_string_append_len (results, (const char *) digest, 20))
465  return FALSE;
466 
467  /* some kind of security paranoia, though it seems pointless
468  * to me given the nonzeroed stuff flying around
469  */
470  _DBUS_ZERO(*context);
471 
472  return TRUE;
473 }
474 
485  DBusString *ascii_output)
486 {
487  DBusSHAContext context;
488  DBusString digest;
489 
490  _dbus_sha_init (&context);
491 
492  _dbus_sha_update (&context, data);
493 
494  if (!_dbus_string_init (&digest))
495  return FALSE;
496 
497  if (!_dbus_sha_final (&context, &digest))
498  goto error;
499 
500  if (!_dbus_string_hex_encode (&digest, 0, ascii_output,
501  _dbus_string_get_length (ascii_output)))
502  goto error;
503 
504  _dbus_string_free (&digest);
505 
506  return TRUE;
507 
508  error:
509  _dbus_string_free (&digest);
510  return FALSE;
511 }
512  /* end of exported functions */
514 
515 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
516 #include "dbus-test.h"
517 #include <stdio.h>
518 
519 static dbus_bool_t
520 check_sha_binary (const unsigned char *input,
521  int input_len,
522  const char *expected)
523 {
524  DBusString input_str;
525  DBusString expected_str;
526  DBusString results;
527 
528  _dbus_string_init_const_len (&input_str, (const char *) input, input_len);
529  _dbus_string_init_const (&expected_str, expected);
530 
531  if (!_dbus_string_init (&results))
532  _dbus_test_fatal ("no memory for SHA-1 results");
533 
534  if (!_dbus_sha_compute (&input_str, &results))
535  _dbus_test_fatal ("no memory for SHA-1 results");
536 
537  if (!_dbus_string_equal (&expected_str, &results))
538  {
539  _dbus_warn ("Expected hash %s got %s for SHA-1 sum",
540  expected,
541  _dbus_string_get_const_data (&results));
542  _dbus_string_free (&results);
543  return FALSE;
544  }
545 
546  _dbus_string_free (&results);
547  return TRUE;
548 }
549 
550 static dbus_bool_t
551 check_sha_str (const char *input,
552  const char *expected)
553 {
554  return check_sha_binary ((unsigned char *) input, strlen (input), expected);
555 }
556 
557 static dbus_bool_t
558 decode_compact_string (const DBusString *line,
559  DBusString *decoded)
560 {
561  int n_bits;
562  dbus_bool_t current_b;
563  int offset;
564  int next;
565  long val;
566  int length_bytes;
567 
568  offset = 0;
569  next = 0;
570 
571  if (!_dbus_string_parse_int (line, offset, &val, &next))
572  {
573  fprintf (stderr, "could not parse length at start of compact string: %s\n",
574  _dbus_string_get_const_data (line));
575  return FALSE;
576  }
577 
578  _dbus_string_skip_blank (line, next, &next);
579 
580  offset = next;
581  if (!_dbus_string_parse_int (line, offset, &val, &next))
582  {
583  fprintf (stderr, "could not parse start bit 'b' in compact string: %s\n",
584  _dbus_string_get_const_data (line));
585  return FALSE;
586  }
587 
588  if (!(val == 0 || val == 1))
589  {
590  fprintf (stderr, "the value 'b' must be 0 or 1, see sha-1/Readme.txt\n");
591  return FALSE;
592  }
593 
594  _dbus_string_skip_blank (line, next, &next);
595 
596  current_b = val;
597  n_bits = 0;
598 
599  while (next < _dbus_string_get_length (line))
600  {
601  int total_bits;
602 
603  offset = next;
604 
605  if (_dbus_string_get_byte (line, offset) == '^')
606  break;
607 
608  if (!_dbus_string_parse_int (line, offset, &val, &next))
609  {
610  fprintf (stderr, "could not parse bit count in compact string\n");
611  return FALSE;
612  }
613 
614  /* We now append "val" copies of "current_b" bits to the string */
615  total_bits = n_bits + val;
616  while (n_bits < total_bits)
617  {
618  int byte_containing_next_bit = n_bits / 8;
619  int bit_containing_next_bit = 7 - (n_bits % 8);
620  unsigned char old_byte;
621 
622  if (byte_containing_next_bit >= _dbus_string_get_length (decoded))
623  {
624  if (!_dbus_string_set_length (decoded, byte_containing_next_bit + 1))
625  _dbus_test_fatal ("no memory to extend to next byte");
626  }
627 
628  old_byte = _dbus_string_get_byte (decoded, byte_containing_next_bit);
629  old_byte |= current_b << bit_containing_next_bit;
630 
631 #if 0
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);
635 #endif
636 
637  _dbus_string_set_byte (decoded, byte_containing_next_bit, old_byte);
638 
639  ++n_bits;
640  }
641 
642  _dbus_string_skip_blank (line, next, &next);
643 
644  current_b = !current_b;
645  }
646 
647  length_bytes = (n_bits / 8 + ((n_bits % 8) ? 1 : 0));
648 
649  if (_dbus_string_get_length (decoded) != length_bytes)
650  {
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));
653  return FALSE;
654  }
655  else
656  return TRUE;
657 }
658 
659 static dbus_bool_t
660 get_next_expected_result (DBusString *results,
661  DBusString *result)
662 {
663  DBusString line;
664  dbus_bool_t retval;
665 
666  retval = FALSE;
667 
668  if (!_dbus_string_init (&line))
669  _dbus_test_fatal ("no memory");
670 
671  next_iteration:
672  while (_dbus_string_pop_line (results, &line))
673  {
674  _dbus_string_delete_leading_blanks (&line);
675 
676  if (_dbus_string_get_length (&line) == 0)
677  goto next_iteration;
678  else if (_dbus_string_starts_with_c_str (&line, "#"))
679  goto next_iteration;
680  else if (_dbus_string_starts_with_c_str (&line, "H>"))
681  {
682  /* don't print */
683  }
684  else if (_dbus_string_starts_with_c_str (&line, "D>") ||
685  _dbus_string_starts_with_c_str (&line, "<D"))
686  goto next_iteration;
687  else
688  {
689  int i;
690 
691  if (!_dbus_string_move (&line, 0, result, 0))
692  _dbus_test_fatal ("no memory");
693 
694  i = 0;
695  while (i < _dbus_string_get_length (result))
696  {
697  unsigned char c = _dbus_string_get_byte (result, i);
698 
699  switch (c)
700  {
701  case 'A':
702  _dbus_string_set_byte (result, i, 'a');
703  break;
704  case 'B':
705  _dbus_string_set_byte (result, i, 'b');
706  break;
707  case 'C':
708  _dbus_string_set_byte (result, i, 'c');
709  break;
710  case 'D':
711  _dbus_string_set_byte (result, i, 'd');
712  break;
713  case 'E':
714  _dbus_string_set_byte (result, i, 'e');
715  break;
716  case 'F':
717  _dbus_string_set_byte (result, i, 'f');
718  break;
719  case '^':
720  case ' ':
721  _dbus_string_delete (result, i, 1);
722  --i; /* to offset ++i below */
723  break;
724  default:
725  if ((c < '0' || c > '9') && (c < 'a' || c > 'f'))
726  _dbus_test_fatal ("invalid SHA-1 test script");
727  }
728 
729  ++i;
730  }
731 
732  break;
733  }
734  }
735 
736  retval = TRUE;
737 
738  /* out: */
739  _dbus_string_free (&line);
740  return retval;
741 }
742 
743 static dbus_bool_t
744 process_test_data (const char *test_data_dir)
745 {
746  DBusString tests_file;
747  DBusString results_file;
748  DBusString tests;
749  DBusString results;
750  DBusString line;
751  DBusString tmp;
752  int line_no;
753  dbus_bool_t retval;
754  int success_count;
755  DBusError error = DBUS_ERROR_INIT;
756 
757  retval = FALSE;
758 
759  if (!_dbus_string_init (&tests_file))
760  _dbus_test_fatal ("no memory");
761 
762  if (!_dbus_string_init (&results_file))
763  _dbus_test_fatal ("no memory");
764 
765  if (!_dbus_string_init (&tests))
766  _dbus_test_fatal ("no memory");
767 
768  if (!_dbus_string_init (&results))
769  _dbus_test_fatal ("no memory");
770 
771  if (!_dbus_string_init (&line))
772  _dbus_test_fatal ("no memory");
773 
774  if (!_dbus_string_append (&tests_file, test_data_dir))
775  _dbus_test_fatal ("no memory");
776 
777  if (!_dbus_string_append (&results_file, test_data_dir))
778  _dbus_test_fatal ("no memory");
779 
780  _dbus_string_init_const (&tmp, "sha-1/byte-messages.sha1");
781  if (!_dbus_concat_dir_and_file (&tests_file, &tmp))
782  _dbus_test_fatal ("no memory");
783 
784  _dbus_string_init_const (&tmp, "sha-1/byte-hashes.sha1");
785  if (!_dbus_concat_dir_and_file (&results_file, &tmp))
786  _dbus_test_fatal ("no memory");
787 
788  if (!_dbus_file_get_contents (&tests, &tests_file, &error))
789  {
790  fprintf (stderr, "could not load test data file %s: %s\n",
791  _dbus_string_get_const_data (&tests_file),
792  error.message);
793  dbus_error_free (&error);
794  goto out;
795  }
796 
797  if (!_dbus_file_get_contents (&results, &results_file, &error))
798  {
799  fprintf (stderr, "could not load results data file %s: %s\n",
800  _dbus_string_get_const_data (&results_file), error.message);
801  dbus_error_free (&error);
802  goto out;
803  }
804 
805  success_count = 0;
806  line_no = 0;
807  next_iteration:
808  while (_dbus_string_pop_line (&tests, &line))
809  {
810  line_no += 1;
811 
812  _dbus_string_delete_leading_blanks (&line);
813 
814  if (_dbus_string_get_length (&line) == 0)
815  goto next_iteration;
816  else if (_dbus_string_starts_with_c_str (&line, "#"))
817  goto next_iteration;
818  else if (_dbus_string_starts_with_c_str (&line, "H>"))
819  {
820  _dbus_test_diag ("SHA-1: %s", _dbus_string_get_const_data (&line));
821 
822  if (_dbus_string_find (&line, 0, "Type 3", NULL))
823  {
824  /* See sha-1/Readme.txt - the "Type 3" tests are
825  * random seeds, rather than data to be hashed.
826  * we'd have to do a little bit more implementation
827  * to use those tests.
828  */
829 
830  _dbus_test_diag (" (ending tests due to Type 3 tests seen - this is normal)");
831  break;
832  }
833  }
834  else if (_dbus_string_starts_with_c_str (&line, "D>") ||
835  _dbus_string_starts_with_c_str (&line, "<D"))
836  goto next_iteration;
837  else
838  {
839  DBusString test;
840  DBusString result;
841  DBusString next_line;
842  DBusString expected;
843  dbus_bool_t success;
844 
845  success = FALSE;
846 
847  if (!_dbus_string_init (&next_line))
848  _dbus_test_fatal ("no memory");
849 
850  if (!_dbus_string_init (&expected))
851  _dbus_test_fatal ("no memory");
852 
853  if (!_dbus_string_init (&test))
854  _dbus_test_fatal ("no memory");
855 
856  if (!_dbus_string_init (&result))
857  _dbus_test_fatal ("no memory");
858 
859  /* the "compact strings" are "^"-terminated not
860  * newline-terminated so readahead to find the
861  * "^"
862  */
863  while (!_dbus_string_find (&line, 0, "^", NULL) &&
864  _dbus_string_pop_line (&tests, &next_line))
865  {
866  if (!_dbus_string_append_byte (&line, ' ') ||
867  !_dbus_string_move (&next_line, 0, &line,
868  _dbus_string_get_length (&line)))
869  _dbus_test_fatal ("no memory");
870  }
871 
872  if (!decode_compact_string (&line, &test))
873  {
874  fprintf (stderr, "Failed to decode line %d as a compact string\n",
875  line_no);
876  goto failure;
877  }
878 
879  if (!_dbus_sha_compute (&test, &result))
880  _dbus_test_fatal ("no memory for SHA-1 result");
881 
882  if (!get_next_expected_result (&results, &expected))
883  {
884  fprintf (stderr, "Failed to read an expected result\n");
885  goto failure;
886  }
887 
888  if (!_dbus_string_equal (&result, &expected))
889  {
890  fprintf (stderr, " for line %d got hash %s expected %s\n",
891  line_no,
892  _dbus_string_get_const_data (&result),
893  _dbus_string_get_const_data (&expected));
894  goto failure;
895  }
896  else
897  {
898  success_count += 1;
899  }
900 
901  success = TRUE;
902 
903  failure:
904  _dbus_string_free (&test);
905  _dbus_string_free (&result);
906  _dbus_string_free (&next_line);
907  _dbus_string_free (&expected);
908 
909  if (!success)
910  goto out;
911  }
912  }
913 
914  retval = TRUE;
915 
916  _dbus_test_diag ("Passed the %d SHA-1 tests in the test file",
917  success_count);
918 
919  out:
920  _dbus_string_free (&tests_file);
921  _dbus_string_free (&results_file);
922  _dbus_string_free (&tests);
923  _dbus_string_free (&results);
924  _dbus_string_free (&line);
925 
926  return retval;
927 }
928 
936 _dbus_sha_test (const char *test_data_dir)
937 {
938  unsigned char all_bytes[256];
939  int i;
940 
941  if (test_data_dir != NULL)
942  {
943  if (!process_test_data (test_data_dir))
944  return FALSE;
945  }
946  else
947  _dbus_test_diag ("No test data dir");
948 
949  i = 0;
950  while (i < 256)
951  {
952  all_bytes[i] = i;
953  ++i;
954  }
955 
956  if (!check_sha_binary (all_bytes, 256,
957  "4916d6bdb7f78e6803698cab32d1586ea457dfc8"))
958  return FALSE;
959 
960 #define CHECK(input,expected) if (!check_sha_str (input, expected)) return FALSE
961 
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");
971 
972  return TRUE;
973 }
974 
975 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
dbus_bool_t _dbus_sha_final(DBusSHAContext *context, DBusString *results)
SHA finalization.
Definition: dbus-sha.c:457
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:952
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.
Definition: dbus-sysdeps.c:444
const char * message
public error message field
Definition: dbus-errors.h:51
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_uint32_t count_lo
64-bit bit count
Definition: dbus-sha.h:40
void _dbus_sha_init(DBusSHAContext *context)
Initializes the SHA context.
Definition: dbus-sha.c:421
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition: dbus-string.c:2030
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.
Definition: dbus-string.c:2276
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.
Definition: dbus-string.c:2205
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
void dbus_error_free(DBusError *error)
Frees an error that&#39;s been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
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.
Definition: dbus-string.c:175
dbus_uint32_t data[16]
SHA data buffer.
Definition: dbus-sha.h:42
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...
Definition: dbus-string.c:1621
dbus_uint32_t digest[5]
Message digest.
Definition: dbus-sha.h:39
Struct storing state of the SHA algorithm.
Definition: dbus-sha.h:37
dbus_bool_t _dbus_string_move(DBusString *source, int start, DBusString *dest, int insert_at)
Moves the end of one string into another string.
Definition: dbus-string.c:1276
dbus_uint32_t count_hi
No clue.
Definition: dbus-sha.h:41
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
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).
Definition: dbus-string.c:1820
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...
Definition: dbus-string.c:1926
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.
Definition: dbus-string.c:1210
Object representing an exception.
Definition: dbus-errors.h:48
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.
Definition: dbus-string.c:1174
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...
Definition: dbus-string.c:264
#define TRUE
Expands to "1".
void _dbus_sha_update(DBusSHAContext *context, const DBusString *data)
Feeds more data into an existing shasum computation.
Definition: dbus-sha.c:433
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.
Definition: dbus-string.c:1154
#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...
Definition: dbus-sha.c:484
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:819
#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.
Definition: dbus-string.c:210