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