mdc2test.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 1995-2020 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. /*
  10. * MDC2 low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/provider.h>
  16. #include <openssl/params.h>
  17. #include <openssl/types.h>
  18. #include <openssl/core_names.h>
  19. #include "internal/nelem.h"
  20. #include "testutil.h"
  21. #if defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_MDC2)
  22. # define OPENSSL_NO_MDC2
  23. #endif
  24. #ifndef OPENSSL_NO_MDC2
  25. # include <openssl/evp.h>
  26. # include <openssl/mdc2.h>
  27. # ifdef CHARSET_EBCDIC
  28. # include <openssl/ebcdic.h>
  29. # endif
  30. static unsigned char pad1[16] = {
  31. 0x42, 0xE5, 0x0C, 0xD2, 0x24, 0xBA, 0xCE, 0xBA,
  32. 0x76, 0x0B, 0xDD, 0x2B, 0xD4, 0x09, 0x28, 0x1A
  33. };
  34. static unsigned char pad2[16] = {
  35. 0x2E, 0x46, 0x79, 0xB5, 0xAD, 0xD9, 0xCA, 0x75,
  36. 0x35, 0xD8, 0x7A, 0xFE, 0xAB, 0x33, 0xBE, 0xE2
  37. };
  38. static int test_mdc2(void)
  39. {
  40. int testresult = 0;
  41. unsigned int pad_type = 2;
  42. unsigned char md[MDC2_DIGEST_LENGTH];
  43. EVP_MD_CTX *c;
  44. static char text[] = "Now is the time for all ";
  45. size_t tlen = strlen(text), i = 0;
  46. OSSL_PROVIDER *prov = NULL;
  47. OSSL_PARAM params[2];
  48. params[i++] = OSSL_PARAM_construct_uint(OSSL_DIGEST_PARAM_PAD_TYPE,
  49. &pad_type),
  50. params[i++] = OSSL_PARAM_construct_end();
  51. prov = OSSL_PROVIDER_load(NULL, "legacy");
  52. # ifdef CHARSET_EBCDIC
  53. ebcdic2ascii(text, text, tlen);
  54. # endif
  55. c = EVP_MD_CTX_new();
  56. if (!TEST_ptr(c)
  57. || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))
  58. || !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))
  59. || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
  60. || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad1, MDC2_DIGEST_LENGTH)
  61. || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL)))
  62. goto end;
  63. if (!TEST_int_gt(EVP_MD_CTX_set_params(c, params), 0)
  64. || !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))
  65. || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
  66. || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad2, MDC2_DIGEST_LENGTH))
  67. goto end;
  68. testresult = 1;
  69. end:
  70. EVP_MD_CTX_free(c);
  71. OSSL_PROVIDER_unload(prov);
  72. return testresult;
  73. }
  74. #endif
  75. int setup_tests(void)
  76. {
  77. #ifndef OPENSSL_NO_MDC2
  78. ADD_TEST(test_mdc2);
  79. #endif
  80. return 1;
  81. }