/mhash-0.9.9.9/src/frag_test.c

#
C | 135 lines | 81 code | 20 blank | 34 comment | 9 complexity | 41d74553cb610bc24bd4026adf81f939 MD5 | raw file
  1. /* frag_test.c
  2. *
  3. * Copyright (C) 2004 B. Poettering
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /*
  21. * Most modern hash functions work in a block oriented way. In order
  22. * to fit the mhash interface (which accepts an arbitrary amount of
  23. * data at each call of the mhash function) the input data has to be
  24. * cut into equally sized chunks, which subsequently are processed one
  25. * by one.
  26. *
  27. * The following code checks all the hash functions included in the
  28. * mhash library for the proper implementation of the mentioned
  29. * "chunking algorithm". A data stream is generated, cut into pieces
  30. * and fed into two instances of mhash in parallel. As the cutting
  31. * points of the two input streams differ, the two calculated hash
  32. * values match only if the "chunking" algorithm is implemented
  33. * properly.
  34. */
  35. #define MUTILS_USE_MHASH_CONFIG
  36. #include <mhash.h>
  37. #define MAX_DIGEST_SIZE 256
  38. #define MAX_INPUT_SIZE 256
  39. int frag_test(hashid hashid)
  40. {
  41. mutils_word8 digest1[MAX_DIGEST_SIZE]; /* enough space to hold digests */
  42. mutils_word8 digest2[MAX_DIGEST_SIZE];
  43. mutils_word8 buf1[MAX_INPUT_SIZE + 1];
  44. mutils_word8 buf2[MAX_INPUT_SIZE];
  45. MHASH td1, td2;
  46. size_t input_size, double_input, digest_size;
  47. mutils_word32 i;
  48. mutils_word32 offs;
  49. mutils_word32 left;
  50. mutils_word8 val = 0;
  51. input_size = mhash_get_hash_pblock(hashid);
  52. assert(input_size <= MAX_INPUT_SIZE);
  53. digest_size = mhash_get_block_size(hashid);
  54. assert(digest_size <= MAX_DIGEST_SIZE);
  55. td1 = mhash_init(hashid); /* get two mhash instances */
  56. td2 = mhash_init(hashid);
  57. double_input = 2 * input_size;
  58. for (i = offs = 0; i < double_input; i++, val++) /* first part */
  59. {
  60. mutils_memset(buf1, val, input_size + 1); /* the first instance gets framgments */
  61. mhash(td1, buf1, input_size + 1); /* of size (input_size+1) */
  62. left = input_size - offs; /* the second instance gets fragments */
  63. mutils_memset(buf2 + offs, val, left); /* of size input_size */
  64. mhash(td2, buf2, input_size);
  65. offs = (input_size + 1) - left;
  66. mutils_memset(buf2, val, offs);
  67. if (offs == input_size)
  68. {
  69. mhash(td2, buf2, input_size);
  70. offs = 0;
  71. }
  72. }
  73. mhash(td2, buf2, offs);
  74. for (i = offs = 0; i < 2 * input_size; i++, val++) /* second part */
  75. {
  76. mutils_memset(buf1, val, input_size - 1); /* the first instance gets framgments */
  77. mhash(td1, buf1, input_size - 1); /* of size (input_size-1) */
  78. if (offs == 0) /* the second instance gets fragments */
  79. { /* of size input_size */
  80. offs = input_size - 1;
  81. mutils_memset(buf2, val, offs);
  82. }
  83. else
  84. {
  85. left = input_size - offs;
  86. mutils_memset(buf2 + offs, val, left);
  87. mhash(td2, buf2, input_size);
  88. offs = (input_size - 1) - left;
  89. mutils_memset(buf2, val, offs);
  90. }
  91. }
  92. mhash(td2, buf2, offs);
  93. mhash_deinit(td1, digest1); /* return 1 if the calculated hash values match */
  94. mhash_deinit(td2, digest2);
  95. return ! mutils_strncmp(digest1, digest2, digest_size);
  96. }
  97. int main(void)
  98. {
  99. hashid hashid;
  100. const mutils_word8 *s;
  101. int ok, allok = 1;
  102. mutils_word32 total = mhash_count();
  103. for (hashid = 0; hashid <= total; hashid++)
  104. {
  105. if ((s = mhash_get_hash_name_static(hashid)) && mhash_get_hash_pblock(hashid))
  106. {
  107. printf("Checking fragmentation capabilities of %s: ", s);
  108. fflush(stdout);
  109. printf((ok = frag_test(hashid)) ? "OK\n" : "Failed\n");
  110. allok &= ok;
  111. }
  112. }
  113. return allok ? 0 : 1;
  114. }