2016-05-18 02:20:24 +08:00
|
|
|
/*
|
2022-05-03 18:52:38 +08:00
|
|
|
* Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:05:25 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:20:24 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
2020-01-09 09:18:58 +08:00
|
|
|
/*
|
|
|
|
* MDC2 low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
1998-12-21 18:52:47 +08:00
|
|
|
#include <string.h>
|
2019-04-11 18:27:59 +08:00
|
|
|
#include <openssl/provider.h>
|
|
|
|
#include <openssl/params.h>
|
2019-09-28 06:45:46 +08:00
|
|
|
#include <openssl/types.h>
|
2019-04-11 18:27:59 +08:00
|
|
|
#include <openssl/core_names.h>
|
2017-04-07 11:49:26 +08:00
|
|
|
#include "testutil.h"
|
2002-11-29 02:54:30 +08:00
|
|
|
|
2001-02-20 00:06:34 +08:00
|
|
|
#if defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_MDC2)
|
|
|
|
# define OPENSSL_NO_MDC2
|
1999-04-27 09:14:46 +08:00
|
|
|
#endif
|
|
|
|
|
2017-04-07 11:49:26 +08:00
|
|
|
#ifndef OPENSSL_NO_MDC2
|
2001-06-20 06:30:40 +08:00
|
|
|
# include <openssl/evp.h>
|
2001-07-31 07:57:25 +08:00
|
|
|
# include <openssl/mdc2.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-06-05 05:35:58 +08:00
|
|
|
# ifdef CHARSET_EBCDIC
|
|
|
|
# include <openssl/ebcdic.h>
|
|
|
|
# endif
|
|
|
|
|
1998-12-21 18:52:47 +08:00
|
|
|
static unsigned char pad1[16] = {
|
|
|
|
0x42, 0xE5, 0x0C, 0xD2, 0x24, 0xBA, 0xCE, 0xBA,
|
|
|
|
0x76, 0x0B, 0xDD, 0x2B, 0xD4, 0x09, 0x28, 0x1A
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned char pad2[16] = {
|
|
|
|
0x2E, 0x46, 0x79, 0xB5, 0xAD, 0xD9, 0xCA, 0x75,
|
|
|
|
0x35, 0xD8, 0x7A, 0xFE, 0xAB, 0x33, 0xBE, 0xE2
|
|
|
|
};
|
|
|
|
|
2017-04-07 11:49:26 +08:00
|
|
|
static int test_mdc2(void)
|
1998-12-21 18:52:47 +08:00
|
|
|
{
|
2019-09-05 09:23:57 +08:00
|
|
|
int testresult = 0;
|
|
|
|
unsigned int pad_type = 2;
|
1998-12-21 18:52:47 +08:00
|
|
|
unsigned char md[MDC2_DIGEST_LENGTH];
|
2022-02-18 09:15:50 +08:00
|
|
|
EVP_MD_CTX *c = NULL;
|
2016-04-29 21:46:07 +08:00
|
|
|
static char text[] = "Now is the time for all ";
|
2019-04-11 18:27:59 +08:00
|
|
|
size_t tlen = strlen(text), i = 0;
|
|
|
|
OSSL_PROVIDER *prov = NULL;
|
|
|
|
OSSL_PARAM params[2];
|
|
|
|
|
2019-09-05 09:23:57 +08:00
|
|
|
params[i++] = OSSL_PARAM_construct_uint(OSSL_DIGEST_PARAM_PAD_TYPE,
|
|
|
|
&pad_type),
|
2019-04-11 18:27:59 +08:00
|
|
|
params[i++] = OSSL_PARAM_construct_end();
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2019-04-11 18:27:59 +08:00
|
|
|
prov = OSSL_PROVIDER_load(NULL, "legacy");
|
2022-02-18 09:15:50 +08:00
|
|
|
if (!TEST_ptr(prov))
|
|
|
|
goto end;
|
|
|
|
|
1999-06-05 05:35:58 +08:00
|
|
|
# ifdef CHARSET_EBCDIC
|
2017-04-07 11:49:26 +08:00
|
|
|
ebcdic2ascii(text, text, tlen);
|
1999-06-05 05:35:58 +08:00
|
|
|
# endif
|
|
|
|
|
2015-12-02 07:49:35 +08:00
|
|
|
c = EVP_MD_CTX_new();
|
2017-04-07 11:49:26 +08:00
|
|
|
if (!TEST_ptr(c)
|
|
|
|
|| !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))
|
|
|
|
|| !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))
|
|
|
|
|| !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
|
|
|
|
|| !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad1, MDC2_DIGEST_LENGTH)
|
|
|
|
|| !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL)))
|
|
|
|
goto end;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2019-04-11 18:27:59 +08:00
|
|
|
if (!TEST_int_gt(EVP_MD_CTX_set_params(c, params), 0)
|
|
|
|
|| !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))
|
2017-04-07 11:49:26 +08:00
|
|
|
|| !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
|
|
|
|
|| !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad2, MDC2_DIGEST_LENGTH))
|
|
|
|
goto end;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2017-04-07 11:49:26 +08:00
|
|
|
testresult = 1;
|
|
|
|
end:
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(c);
|
2019-04-11 18:27:59 +08:00
|
|
|
OSSL_PROVIDER_unload(prov);
|
2017-04-07 11:49:26 +08:00
|
|
|
return testresult;
|
1998-12-21 18:52:47 +08:00
|
|
|
}
|
1999-04-27 09:14:46 +08:00
|
|
|
#endif
|
2017-04-07 11:49:26 +08:00
|
|
|
|
2017-07-18 09:48:27 +08:00
|
|
|
int setup_tests(void)
|
2017-04-07 11:49:26 +08:00
|
|
|
{
|
|
|
|
#ifndef OPENSSL_NO_MDC2
|
|
|
|
ADD_TEST(test_mdc2);
|
|
|
|
#endif
|
2017-07-18 09:48:27 +08:00
|
|
|
return 1;
|
2017-04-07 11:49:26 +08:00
|
|
|
}
|