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