2014-07-10 14:51:07 +00:00
|
|
|
|
/*-
|
2014-07-10 17:15:54 +00:00
|
|
|
|
* Copyright (c) 2012 The University of Oslo
|
2014-07-10 14:51:07 +00:00
|
|
|
|
* Copyright (c) 2012-2014 Dag-Erling Smørgrav
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
|
* permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "cryb/impl.h"
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
2015-10-04 07:49:48 +00:00
|
|
|
|
#include <cryb/test.h>
|
2014-07-10 14:51:07 +00:00
|
|
|
|
|
|
|
|
|
#if WITH_RSAREF
|
|
|
|
|
|
|
|
|
|
#include <rsaref.h>
|
|
|
|
|
|
|
|
|
|
#define MD2_DIGEST_LEN 16
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
t_md2_complete(const void *msg, size_t msglen, uint8_t *digest)
|
|
|
|
|
{
|
|
|
|
|
MD2_CTX ctx;
|
|
|
|
|
|
|
|
|
|
MD2Init(&ctx);
|
|
|
|
|
MD2Update(&ctx, (unsigned char *)(uintptr_t)msg, msglen);
|
|
|
|
|
MD2Final(digest, &ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
#include <cryb/md2.h>
|
|
|
|
|
|
|
|
|
|
#define t_md2_complete(msg, msglen, digest) \
|
|
|
|
|
md2_complete(msg, msglen, digest)
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Test vectors from RFC 1319
|
|
|
|
|
*/
|
|
|
|
|
static struct t_vector {
|
|
|
|
|
const char *msg;
|
|
|
|
|
const uint8_t digest[MD2_DIGEST_LEN];
|
|
|
|
|
} t_md2_vectors[] = {
|
|
|
|
|
{
|
|
|
|
|
"",
|
|
|
|
|
{
|
|
|
|
|
0x83, 0x50, 0xe5, 0xa3, 0xe2, 0x4c, 0x15, 0x3d,
|
|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
|
|
{
|
|
|
|
|
"1234567890123456789012345678901234567890"
|
|
|
|
|
"1234567890123456789012345678901234567890",
|
|
|
|
|
{
|
|
|
|
|
0xd5, 0x97, 0x6f, 0x79, 0xd8, 0x3d, 0x3a, 0x0d,
|
|
|
|
|
0xc9, 0x80, 0x6c, 0x3c, 0x66, 0xf3, 0xef, 0xd8,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Unit test: compute the MD2 sum of the specified string and compare it
|
|
|
|
|
* to the expected result.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
t_md2_vector(char **desc CRYB_UNUSED, void *arg)
|
|
|
|
|
{
|
|
|
|
|
struct t_vector *vector = (struct t_vector *)arg;
|
|
|
|
|
uint8_t digest[MD2_DIGEST_LEN];
|
|
|
|
|
|
|
|
|
|
t_md2_complete(vector->msg, strlen(vector->msg), digest);
|
2014-07-11 22:26:23 +00:00
|
|
|
|
return (t_compare_mem(vector->digest, digest, MD2_DIGEST_LEN));
|
2014-07-10 14:51:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-11 00:10:48 +00:00
|
|
|
|
|
|
|
|
|
#if !defined(WITH_RSAREF)
|
|
|
|
|
/*
|
|
|
|
|
* Various corner cases and error conditions
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
t_md2_short_updates(char **desc CRYB_UNUSED, void *arg)
|
|
|
|
|
{
|
|
|
|
|
struct t_vector *vector = (struct t_vector *)arg;
|
|
|
|
|
uint8_t digest[MD2_DIGEST_LEN];
|
|
|
|
|
md2_ctx ctx;
|
|
|
|
|
int i, len;
|
|
|
|
|
|
|
|
|
|
md2_init(&ctx);
|
|
|
|
|
len = strlen(vector->msg);
|
|
|
|
|
for (i = 0; i + 5 < len; i += 5)
|
|
|
|
|
md2_update(&ctx, vector->msg + i, 5);
|
|
|
|
|
md2_update(&ctx, vector->msg + i, len - i);
|
|
|
|
|
md2_final(&ctx, digest);
|
|
|
|
|
return (memcmp(digest, vector->digest, MD2_DIGEST_LEN) == 0);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2014-07-10 14:51:07 +00:00
|
|
|
|
/*
|
|
|
|
|
* Performance test: measure the time spent computing the MD2 sum of a
|
|
|
|
|
* message of the specified length.
|
|
|
|
|
*/
|
|
|
|
|
#define T_PERF_ITERATIONS 1000
|
|
|
|
|
static int
|
|
|
|
|
t_md2_perf(char **desc, void *arg)
|
|
|
|
|
{
|
|
|
|
|
struct timespec ts, te;
|
|
|
|
|
unsigned long ns;
|
|
|
|
|
uint8_t digest[MD2_DIGEST_LEN];
|
|
|
|
|
size_t msglen = *(size_t *)arg;
|
2015-08-19 11:27:48 +00:00
|
|
|
|
char msg[msglen];
|
2014-07-10 14:51:07 +00:00
|
|
|
|
|
2014-10-30 12:56:47 +00:00
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
2014-07-10 14:51:07 +00:00
|
|
|
|
for (int i = 0; i < T_PERF_ITERATIONS; ++i)
|
|
|
|
|
t_md2_complete(msg, msglen, digest);
|
2014-10-30 12:56:47 +00:00
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &te);
|
2014-07-10 14:51:07 +00:00
|
|
|
|
ns = te.tv_sec * 1000000000LU + te.tv_nsec;
|
|
|
|
|
ns -= ts.tv_sec * 1000000000LU + ts.tv_nsec;
|
2015-08-19 11:27:48 +00:00
|
|
|
|
asprintf(desc, "%zu bytes: %d iterations in %'lu ns",
|
2014-07-10 14:51:07 +00:00
|
|
|
|
msglen, T_PERF_ITERATIONS, ns);
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
* Boilerplate
|
|
|
|
|
*/
|
|
|
|
|
|
2016-03-15 13:29:44 +00:00
|
|
|
|
static int
|
2014-07-10 14:51:07 +00:00
|
|
|
|
t_prepare(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
|
|
(void)argc;
|
|
|
|
|
(void)argv;
|
|
|
|
|
n = sizeof t_md2_vectors / sizeof t_md2_vectors[0];
|
|
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
|
t_add_test(t_md2_vector, &t_md2_vectors[i],
|
|
|
|
|
"RFC 1321 test vector %d", i + 1);
|
|
|
|
|
#if !defined(WITH_RSAREF)
|
|
|
|
|
/*
|
|
|
|
|
* Run test vector 7 (which is 80 characters long) 5 characters at
|
|
|
|
|
* a time. This tests a) appending data to an underfull block and
|
|
|
|
|
* b) appending more data to an underfull block than it has room
|
|
|
|
|
* for (since 64 % 5 != 0). Test vector 7 already exercised the
|
|
|
|
|
* code path for computing a block directly from source (without
|
|
|
|
|
* copying it in), and all the test vectors except vector 1
|
|
|
|
|
* exercised the general case of copying a small amount of data in
|
|
|
|
|
* without crossing the block boundary.
|
|
|
|
|
*/
|
|
|
|
|
t_add_test(t_md2_short_updates, &t_md2_vectors[6],
|
|
|
|
|
"multiple short updates");
|
|
|
|
|
#endif
|
2014-12-29 13:22:53 +00:00
|
|
|
|
if (t_str_is_true(getenv("CRYB_PERFTEST"))) {
|
2014-07-11 00:18:34 +00:00
|
|
|
|
static size_t one = 1, thousand = 1000, million = 1000000;
|
|
|
|
|
t_add_test(t_md2_perf, &one,
|
|
|
|
|
"performance test (1 byte)");
|
|
|
|
|
t_add_test(t_md2_perf, &thousand,
|
|
|
|
|
"performance test (1,000 bytes)");
|
|
|
|
|
t_add_test(t_md2_perf, &million,
|
|
|
|
|
"performance test (1,000,000 bytes)");
|
|
|
|
|
}
|
2014-07-10 14:51:07 +00:00
|
|
|
|
return (0);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-15 13:29:44 +00:00
|
|
|
|
int
|
|
|
|
|
main(int argc, char *argv[])
|
2014-07-10 14:51:07 +00:00
|
|
|
|
{
|
2016-03-15 13:29:44 +00:00
|
|
|
|
|
|
|
|
|
t_main(t_prepare, NULL, argc, argv);
|
2014-07-10 14:51:07 +00:00
|
|
|
|
}
|