mdc2test.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "internal/nelem.h"
  11. #include "testutil.h"
  12. #if defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_MDC2)
  13. # define OPENSSL_NO_MDC2
  14. #endif
  15. #ifndef OPENSSL_NO_MDC2
  16. # include <openssl/evp.h>
  17. # include <openssl/mdc2.h>
  18. # ifdef CHARSET_EBCDIC
  19. # include <openssl/ebcdic.h>
  20. # endif
  21. static unsigned char pad1[16] = {
  22. 0x42, 0xE5, 0x0C, 0xD2, 0x24, 0xBA, 0xCE, 0xBA,
  23. 0x76, 0x0B, 0xDD, 0x2B, 0xD4, 0x09, 0x28, 0x1A
  24. };
  25. static unsigned char pad2[16] = {
  26. 0x2E, 0x46, 0x79, 0xB5, 0xAD, 0xD9, 0xCA, 0x75,
  27. 0x35, 0xD8, 0x7A, 0xFE, 0xAB, 0x33, 0xBE, 0xE2
  28. };
  29. static int test_mdc2(void)
  30. {
  31. int testresult = 0;
  32. unsigned char md[MDC2_DIGEST_LENGTH];
  33. EVP_MD_CTX *c;
  34. static char text[] = "Now is the time for all ";
  35. size_t tlen = strlen(text);
  36. # ifdef CHARSET_EBCDIC
  37. ebcdic2ascii(text, text, tlen);
  38. # endif
  39. c = EVP_MD_CTX_new();
  40. if (!TEST_ptr(c)
  41. || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))
  42. || !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))
  43. || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
  44. || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad1, MDC2_DIGEST_LENGTH)
  45. || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL)))
  46. goto end;
  47. /* FIXME: use a ctl function? */
  48. ((MDC2_CTX *)EVP_MD_CTX_md_data(c))->pad_type = 2;
  49. if (!TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))
  50. || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
  51. || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad2, MDC2_DIGEST_LENGTH))
  52. goto end;
  53. testresult = 1;
  54. end:
  55. EVP_MD_CTX_free(c);
  56. return testresult;
  57. }
  58. #endif
  59. int setup_tests(void)
  60. {
  61. #ifndef OPENSSL_NO_MDC2
  62. ADD_TEST(test_mdc2);
  63. #endif
  64. return 1;
  65. }