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