Commit b46a759e authored by Niels Möller's avatar Niels Möller
Browse files

* src/testsuite/testutils.c (test_hash): New function.

(test_mac): New function.
(test_sign): New function.
(do_bad_random): New function, stupid randomness source for the
test programs to use.

Rev: src/testsuite/testutils.c:1.2
Rev: src/testsuite/testutils.h:1.2
parent c8b31849
#include "testutils.h"
#include "algorithms.h"
#include "format.h"
#include "randomness.h"
#include "sexp.h"
#include "spki.h"
#include "werror.h"
#include "xalloc.h"
#include "nettle/knuth-lfib.h"
/* -1 means invalid */
static const signed char hex_digits[0x100] =
{
......@@ -127,3 +134,105 @@ test_cipher(const char *name, struct crypto_algorithm *algorithm,
KILL(c);
lsh_string_free(x);
}
void
test_hash(const char *name,
const struct hash_algorithm *algorithm,
const struct lsh_string *data,
const struct lsh_string *digest)
{
(void) name;
if (!lsh_string_eq(hash_string(algorithm, data, 0), digest))
FAIL();
}
void
test_mac(const char *name,
struct mac_algorithm *algorithm,
const struct lsh_string *key,
const struct lsh_string *data,
const struct lsh_string *digest)
{
(void) name;
if (!lsh_string_eq(mac_string(algorithm, key, 0, data, 0),
digest))
FAIL();
}
struct bad_random
{
struct randomness super;
struct knuth_lfib_ctx *ctx;
};
static void
do_bad_random(struct randomness *r, UINT32 length, UINT8 *dst)
{
struct bad_random *self = (struct bad_random *) r;
knuth_lfib_random(self->ctx, length, dst);
}
void
test_sign(const char *name,
const struct lsh_string *key_exp,
struct lsh_string *msg,
const struct lsh_string *signature)
{
struct alist *algorithms;
struct sexp *key;
struct sexp *sign;
struct signer *s;
struct verifier *v;
struct knuth_lfib_ctx ctx;
struct bad_random r = { { STACK_HEADER, RANDOM_GOOD /* a lie */,
do_bad_random, NULL },
&ctx
};
knuth_lfib_init(&ctx, time(NULL));
algorithms = all_signature_algorithms(&r.super);
(void) name;
#if 0
werror("%xi\n", key_exp);
werror("%S\n", key_exp);
#endif
key = string_to_sexp(SEXP_TRANSPORT, key_exp, 0);
if (!key)
FAIL();
s = spki_make_signer(algorithms, key, NULL);
if (!s)
FAIL();
sign = SIGN_SPKI(s, msg->length, msg->data);
if (signature
&& !lsh_string_eq(signature, sexp_format(sign, SEXP_CANONICAL, 0)))
FAIL();
v = SIGNER_GET_VERIFIER(s);
if (!v)
/* Can't create verifier */
FAIL();
if (!VERIFY_SPKI(v, msg->length, msg->data, sign))
/* Unexpected verification failure. */
FAIL();
/* Modify message slightly. */
if (msg->length < 10)
FAIL();
msg->data[5] ^= 0x40;
if (VERIFY_SPKI(v, msg->length, msg->data, sign))
/* Unexpected verification success. */
FAIL();
KILL(v);
KILL(s);
KILL(key);
}
......@@ -5,6 +5,7 @@
#include "algorithms.h"
#include "crypto.h"
#include "format.h"
#include <inttypes.h>
#include <stdlib.h>
......@@ -22,8 +23,28 @@ test_cipher(const char *name, struct crypto_algorithm *algorithm,
const struct lsh_string *plain,
const struct lsh_string *cipher,
const struct lsh_string *iv);
void
test_hash(const char *name,
const struct hash_algorithm *algorithm,
const struct lsh_string *data,
const struct lsh_string *digest);
void
test_mac(const char *name,
struct mac_algorithm *algorithm,
const struct lsh_string *key,
const struct lsh_string *data,
const struct lsh_string *digest);
void
test_sign(const char *name,
const struct lsh_string *key_exp,
struct lsh_string *msg,
const struct lsh_string *signature);
#define H(x) decode_hex(x)
#define S(x) make_string(x)
#define FAIL() abort()
#define SKIP() exit(77)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment