xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/mdc2test.c (revision 8fbed61efdd901c0e09614c9f45356aeeab23fe3)
1c7da899bSchristos /*
2*8fbed61eSchristos  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3c7da899bSchristos  *
4*8fbed61eSchristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5c7da899bSchristos  * this file except in compliance with the License.  You can obtain a copy
6c7da899bSchristos  * in the file LICENSE in the source distribution or at
7c7da899bSchristos  * https://www.openssl.org/source/license.html
8c7da899bSchristos  */
9c7da899bSchristos 
10*8fbed61eSchristos /*
11*8fbed61eSchristos  * MDC2 low level APIs are deprecated for public use, but still ok for
12*8fbed61eSchristos  * internal use.
13*8fbed61eSchristos  */
14*8fbed61eSchristos #include "internal/deprecated.h"
15c7da899bSchristos 
16*8fbed61eSchristos #include <string.h>
17*8fbed61eSchristos #include <openssl/provider.h>
18*8fbed61eSchristos #include <openssl/params.h>
19*8fbed61eSchristos #include <openssl/types.h>
20*8fbed61eSchristos #include <openssl/core_names.h>
21e0ea3921Schristos #include "internal/nelem.h"
22e0ea3921Schristos #include "testutil.h"
23c7da899bSchristos 
24c7da899bSchristos #if defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_MDC2)
25c7da899bSchristos # define OPENSSL_NO_MDC2
26c7da899bSchristos #endif
27c7da899bSchristos 
28e0ea3921Schristos #ifndef OPENSSL_NO_MDC2
29c7da899bSchristos # include <openssl/evp.h>
30c7da899bSchristos # include <openssl/mdc2.h>
31c7da899bSchristos 
32c7da899bSchristos # ifdef CHARSET_EBCDIC
33c7da899bSchristos #  include <openssl/ebcdic.h>
34c7da899bSchristos # endif
35c7da899bSchristos 
36c7da899bSchristos static unsigned char pad1[16] = {
37c7da899bSchristos     0x42, 0xE5, 0x0C, 0xD2, 0x24, 0xBA, 0xCE, 0xBA,
38c7da899bSchristos     0x76, 0x0B, 0xDD, 0x2B, 0xD4, 0x09, 0x28, 0x1A
39c7da899bSchristos };
40c7da899bSchristos 
41c7da899bSchristos static unsigned char pad2[16] = {
42c7da899bSchristos     0x2E, 0x46, 0x79, 0xB5, 0xAD, 0xD9, 0xCA, 0x75,
43c7da899bSchristos     0x35, 0xD8, 0x7A, 0xFE, 0xAB, 0x33, 0xBE, 0xE2
44c7da899bSchristos };
45c7da899bSchristos 
test_mdc2(void)46e0ea3921Schristos static int test_mdc2(void)
47c7da899bSchristos {
48e0ea3921Schristos     int testresult = 0;
49*8fbed61eSchristos     unsigned int pad_type = 2;
50c7da899bSchristos     unsigned char md[MDC2_DIGEST_LENGTH];
51c7da899bSchristos     EVP_MD_CTX *c;
52c7da899bSchristos     static char text[] = "Now is the time for all ";
53*8fbed61eSchristos     size_t tlen = strlen(text), i = 0;
54*8fbed61eSchristos     OSSL_PROVIDER *prov = NULL;
55*8fbed61eSchristos     OSSL_PARAM params[2];
56c7da899bSchristos 
57*8fbed61eSchristos     params[i++] = OSSL_PARAM_construct_uint(OSSL_DIGEST_PARAM_PAD_TYPE,
58*8fbed61eSchristos                                             &pad_type),
59*8fbed61eSchristos     params[i++] = OSSL_PARAM_construct_end();
60*8fbed61eSchristos 
61*8fbed61eSchristos     prov = OSSL_PROVIDER_load(NULL, "legacy");
62c7da899bSchristos # ifdef CHARSET_EBCDIC
63e0ea3921Schristos     ebcdic2ascii(text, text, tlen);
64c7da899bSchristos # endif
65c7da899bSchristos 
66c7da899bSchristos     c = EVP_MD_CTX_new();
67e0ea3921Schristos     if (!TEST_ptr(c)
68e0ea3921Schristos         || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))
69e0ea3921Schristos         || !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))
70e0ea3921Schristos         || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
71e0ea3921Schristos         || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad1, MDC2_DIGEST_LENGTH)
72e0ea3921Schristos         || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL)))
73e0ea3921Schristos         goto end;
74c7da899bSchristos 
75*8fbed61eSchristos     if (!TEST_int_gt(EVP_MD_CTX_set_params(c, params), 0)
76*8fbed61eSchristos         || !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))
77e0ea3921Schristos         || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
78e0ea3921Schristos         || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad2, MDC2_DIGEST_LENGTH))
79e0ea3921Schristos         goto end;
80c7da899bSchristos 
81e0ea3921Schristos     testresult = 1;
82e0ea3921Schristos  end:
83c7da899bSchristos     EVP_MD_CTX_free(c);
84*8fbed61eSchristos     OSSL_PROVIDER_unload(prov);
85e0ea3921Schristos     return testresult;
86c7da899bSchristos }
87c7da899bSchristos #endif
88e0ea3921Schristos 
setup_tests(void)89e0ea3921Schristos int setup_tests(void)
90e0ea3921Schristos {
91e0ea3921Schristos #ifndef OPENSSL_NO_MDC2
92e0ea3921Schristos     ADD_TEST(test_mdc2);
93e0ea3921Schristos #endif
94e0ea3921Schristos     return 1;
95e0ea3921Schristos }
96