testmd4.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* This is derived from material copyright RSA Data Security, Inc.
  17. * Their notice is reproduced below in its entirety.
  18. *
  19. * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
  20. * rights reserved.
  21. *
  22. * RSA Data Security, Inc. makes no representations concerning either
  23. * the merchantability of this software or the suitability of this
  24. * software for any particular purpose. It is provided "as is"
  25. * without express or implied warranty of any kind.
  26. *
  27. * These notices must be retained in any copies of any part of this
  28. * documentation and/or software.
  29. */
  30. #include <assert.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include "apr_errno.h"
  34. #include "apr_md4.h"
  35. #include "apr_file_io.h"
  36. #include "abts.h"
  37. #include "testutil.h"
  38. static struct {
  39. const char *string;
  40. const char *md4sum;
  41. } md4sums[] =
  42. {
  43. /*
  44. * Taken from the old md4 test suite.
  45. * MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
  46. * MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
  47. * MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
  48. * MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
  49. * MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
  50. * MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
  51. * MD4 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
  52. *
  53. */
  54. {"",
  55. "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89\xc0"},
  56. {"a",
  57. "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb\x24"},
  58. {"abc",
  59. "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72\x9d"},
  60. {"message digest",
  61. "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01\x4b"},
  62. {"abcdefghijklmnopqrstuvwxyz",
  63. "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d\xa9"},
  64. {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  65. "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0\xe4"},
  66. {"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
  67. "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05\x36"}
  68. };
  69. static int num_sums = sizeof(md4sums) / sizeof(md4sums[0]);
  70. static int count;
  71. #if 0
  72. static int MDStringComp(const void *string, const void *sum)
  73. {
  74. apr_md4_ctx_t context;
  75. unsigned char digest[APR_MD4_DIGESTSIZE];
  76. unsigned int len = strlen(string);
  77. apr_md4_init(&context);
  78. apr_md4_update(&context, (unsigned char *)string, len);
  79. apr_md4_final(digest, &context);
  80. return (memcmp(digest, sum, APR_MD4_DIGESTSIZE));
  81. }
  82. #endif
  83. static void test_md4sum(abts_case *tc, void *data)
  84. {
  85. apr_md4_ctx_t context;
  86. unsigned char digest[APR_MD4_DIGESTSIZE];
  87. const void *string = md4sums[count].string;
  88. const void *sum = md4sums[count].md4sum;
  89. unsigned int len = strlen(string);
  90. ABTS_ASSERT(tc, "apr_md4_init", (apr_md4_init(&context) == 0));
  91. ABTS_ASSERT(tc, "apr_md4_update",
  92. (apr_md4_update(&context,
  93. (unsigned char *)string, len) == 0));
  94. ABTS_ASSERT(tc, "apr_md4_final", (apr_md4_final(digest, &context) ==0));
  95. ABTS_ASSERT(tc, "check for correct md4 digest",
  96. (memcmp(digest, sum, APR_MD4_DIGESTSIZE) == 0));
  97. }
  98. abts_suite *testmd4(abts_suite *suite)
  99. {
  100. suite = ADD_SUITE(suite);
  101. for (count=0; count < num_sums; count++) {
  102. abts_run_test(suite, test_md4sum, NULL);
  103. }
  104. return suite;
  105. }