glibc/locale/tst-md5.c
Zack Weinberg f59011763c Import Solar Designer's public domain MD5 for use by localedef.
Locale archives contain embedded MD5 hashes for integrity protection.
glibc's locale-reading code does not check these, but localedef does
generate them.  It was reusing crypt/md5.c for the implementation.
Rather than moving that file over to locale/, import Alexander
Peslyak (aka Solar Designer)'s public domain MD5 implementation, which
is simpler, and in particular, completely agnostic to endianness.  The
API uses different names, because Peslyak wanted to be API-compatible
with openssl, but is otherwise equivalent.

glibc's *tests* of the MD5 core (crypt/md5test.c and crypt/md5test-giant.c)
are transferred to the locale directory, and the new implementation is
verified to pass both.  (The "giant" test takes 90 seconds to run on a
2018-era x86; it was in xtests in crypt and it remains in xtests after
this patch.)  I converted both of them to the new test driver while I
was in there.

crypt/md5c-test.c is a test of MD5 *password hashing*, not of the MD5
core, so it is not moved.

This patch was compile-tested with both --enable-crypt and the default
--disable-crypt.
2023-09-21 16:05:43 -04:00

56 lines
1.5 KiB
C

#include <string.h>
#include "md5.h"
static const struct
{
const char *input;
const char result[16];
} tests[] =
{
{ "",
"\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e" },
{ "a",
"\x0c\xc1\x75\xb9\xc0\xf1\xb6\xa8\x31\xc3\x99\xe2\x69\x77\x26\x61" },
{ "abc",
"\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72" },
{ "message digest",
"\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0" },
{ "abcdefghijklmnopqrstuvwxyz",
"\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1\x3b" },
{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
"\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d\x9f" },
{ "123456789012345678901234567890123456789012345678901234567890"
"12345678901234567890",
"\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6\x7a" }
};
static int
do_test(void)
{
MD5_CTX ctx;
unsigned char sum[16];
int result = 0;
int cnt;
for (cnt = 0; cnt < (int) (sizeof (tests) / sizeof (tests[0])); ++cnt)
{
int i;
MD5_Init (&ctx);
MD5_Update (&ctx, tests[cnt].input, strlen (tests[cnt].input));
MD5_Final (sum, &ctx);
result |= memcmp (tests[cnt].result, sum, 16);
MD5_Init (&ctx);
for (i = 0; tests[cnt].input[i] != '\0'; ++i)
MD5_Update (&ctx, &tests[cnt].input[i], 1);
MD5_Final (sum, &ctx);
result |= memcmp (tests[cnt].result, sum, 16);
}
return result;
}
#include <support/test-driver.c>