xref: /netbsd-src/crypto/external/bsd/openssl.old/dist/test/mdc2test.c (revision 4724848cf0da353df257f730694b7882798e5daf)
1*4724848cSchristos /*
2*4724848cSchristos  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos  *
4*4724848cSchristos  * Licensed under the OpenSSL license (the "License").  You may not use
5*4724848cSchristos  * this file except in compliance with the License.  You can obtain a copy
6*4724848cSchristos  * in the file LICENSE in the source distribution or at
7*4724848cSchristos  * https://www.openssl.org/source/license.html
8*4724848cSchristos  */
9*4724848cSchristos 
10*4724848cSchristos #include <string.h>
11*4724848cSchristos 
12*4724848cSchristos #include "internal/nelem.h"
13*4724848cSchristos #include "testutil.h"
14*4724848cSchristos 
15*4724848cSchristos #if defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_MDC2)
16*4724848cSchristos # define OPENSSL_NO_MDC2
17*4724848cSchristos #endif
18*4724848cSchristos 
19*4724848cSchristos #ifndef OPENSSL_NO_MDC2
20*4724848cSchristos # include <openssl/evp.h>
21*4724848cSchristos # include <openssl/mdc2.h>
22*4724848cSchristos 
23*4724848cSchristos # ifdef CHARSET_EBCDIC
24*4724848cSchristos #  include <openssl/ebcdic.h>
25*4724848cSchristos # endif
26*4724848cSchristos 
27*4724848cSchristos static unsigned char pad1[16] = {
28*4724848cSchristos     0x42, 0xE5, 0x0C, 0xD2, 0x24, 0xBA, 0xCE, 0xBA,
29*4724848cSchristos     0x76, 0x0B, 0xDD, 0x2B, 0xD4, 0x09, 0x28, 0x1A
30*4724848cSchristos };
31*4724848cSchristos 
32*4724848cSchristos static unsigned char pad2[16] = {
33*4724848cSchristos     0x2E, 0x46, 0x79, 0xB5, 0xAD, 0xD9, 0xCA, 0x75,
34*4724848cSchristos     0x35, 0xD8, 0x7A, 0xFE, 0xAB, 0x33, 0xBE, 0xE2
35*4724848cSchristos };
36*4724848cSchristos 
test_mdc2(void)37*4724848cSchristos static int test_mdc2(void)
38*4724848cSchristos {
39*4724848cSchristos     int testresult = 0;
40*4724848cSchristos     unsigned char md[MDC2_DIGEST_LENGTH];
41*4724848cSchristos     EVP_MD_CTX *c;
42*4724848cSchristos     static char text[] = "Now is the time for all ";
43*4724848cSchristos     size_t tlen = strlen(text);
44*4724848cSchristos 
45*4724848cSchristos # ifdef CHARSET_EBCDIC
46*4724848cSchristos     ebcdic2ascii(text, text, tlen);
47*4724848cSchristos # endif
48*4724848cSchristos 
49*4724848cSchristos     c = EVP_MD_CTX_new();
50*4724848cSchristos     if (!TEST_ptr(c)
51*4724848cSchristos         || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))
52*4724848cSchristos         || !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))
53*4724848cSchristos         || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
54*4724848cSchristos         || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad1, MDC2_DIGEST_LENGTH)
55*4724848cSchristos         || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL)))
56*4724848cSchristos         goto end;
57*4724848cSchristos 
58*4724848cSchristos     /* FIXME: use a ctl function? */
59*4724848cSchristos     ((MDC2_CTX *)EVP_MD_CTX_md_data(c))->pad_type = 2;
60*4724848cSchristos     if (!TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))
61*4724848cSchristos         || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
62*4724848cSchristos         || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad2, MDC2_DIGEST_LENGTH))
63*4724848cSchristos         goto end;
64*4724848cSchristos 
65*4724848cSchristos     testresult = 1;
66*4724848cSchristos  end:
67*4724848cSchristos     EVP_MD_CTX_free(c);
68*4724848cSchristos     return testresult;
69*4724848cSchristos }
70*4724848cSchristos #endif
71*4724848cSchristos 
setup_tests(void)72*4724848cSchristos int setup_tests(void)
73*4724848cSchristos {
74*4724848cSchristos #ifndef OPENSSL_NO_MDC2
75*4724848cSchristos     ADD_TEST(test_mdc2);
76*4724848cSchristos #endif
77*4724848cSchristos     return 1;
78*4724848cSchristos }
79