md2test.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/provider.h>
  11. #include "internal/nelem.h"
  12. #include "testutil.h"
  13. static OSSL_PROVIDER *prov = NULL;
  14. #ifndef OPENSSL_NO_MD2
  15. # include <openssl/evp.h>
  16. # include <openssl/md2.h>
  17. # ifdef CHARSET_EBCDIC
  18. # include <openssl/ebcdic.h>
  19. # endif
  20. static char *test[] = {
  21. "",
  22. "a",
  23. "abc",
  24. "message digest",
  25. "abcdefghijklmnopqrstuvwxyz",
  26. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  27. "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
  28. };
  29. static char *ret[] = {
  30. "8350e5a3e24c153df2275c9f80692773",
  31. "32ec01ec4a6dac72c0ab96fb34c0b5d1",
  32. "da853b0d3f88d99b30283a69e6ded6bb",
  33. "ab4f496bfb2a530b219ff33031fe06b0",
  34. "4e8ddff3650292ab5a4108c3aa47940b",
  35. "da33def2a42df13975352846c30338cd",
  36. "d5976f79d83d3a0dc9806c3c66f3efd8",
  37. };
  38. static int test_md2(int n)
  39. {
  40. char buf[80];
  41. unsigned char md[MD2_DIGEST_LENGTH];
  42. int i;
  43. if (!TEST_true(EVP_Digest((unsigned char *)test[n], strlen(test[n]),
  44. md, NULL, EVP_md2(), NULL)))
  45. return 0;
  46. for (i = 0; i < MD2_DIGEST_LENGTH; i++)
  47. sprintf(&(buf[i * 2]), "%02x", md[i]);
  48. if (!TEST_str_eq(buf, ret[n]))
  49. return 0;
  50. return 1;
  51. }
  52. #endif
  53. int global_init(void)
  54. {
  55. prov = OSSL_PROVIDER_load(NULL, "legacy");
  56. return prov != NULL;
  57. }
  58. void cleanup_tests(void)
  59. {
  60. OSSL_PROVIDER_unload(prov);
  61. }
  62. int setup_tests(void)
  63. {
  64. #ifndef OPENSSL_NO_MD2
  65. ADD_ALL_TESTS(test_md2, OSSL_NELEM(test));
  66. #endif
  67. return 1;
  68. }