Commit dc3f8c4a authored by Niels Möller's avatar Niels Möller

* testsuite/testutils.c (test_rsa_md5, test_rsa_sha1): Moved

functions here...
* testsuite/rsa-test.c: ...from here.

Rev: src/nettle/testsuite/rsa-test.c:1.4
Rev: src/nettle/testsuite/testutils.c:1.7
Rev: src/nettle/testsuite/testutils.h:1.6
parent 4ef9d25c
#include "testutils.h"
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#if HAVE_LIBGMP
# include "rsa.h"
#endif
#define SIGN(key, hash, msg, signature) do { \
hash##_update(&hash, LDATA(msg)); \
rsa_##hash##_sign(key, &hash, signature); \
} while(0)
#define VERIFY(key, hash, msg, signature) ( \
hash##_update(&hash, LDATA(msg)), \
rsa_##hash##_verify(key, &hash, signature) \
)
#if HAVE_LIBGMP
/* Missing in current gmp */
static void
mpz_togglebit (mpz_t x, unsigned long int bit)
{
if (mpz_tstbit(x, bit))
mpz_clrbit(x, bit);
else
mpz_setbit(x, bit);
}
#endif /* HAVE_LIBGMP */
static void
test_rsa_md5(struct rsa_public_key *pub,
struct rsa_private_key *key,
mpz_t expected)
{
struct md5_ctx md5;
mpz_t signature;
md5_init(&md5);
mpz_init(signature);
SIGN(key, md5, "The magic words are squeamish ossifrage", signature);
if (verbose)
{
fprintf(stderr, "rsa-md5 signature: ");
mpz_out_str(stderr, 16, signature);
fprintf(stderr, "\n");
}
if (mpz_cmp(signature, expected))
FAIL();
/* Try bad data */
if (VERIFY(pub, md5,
"The magick words are squeamish ossifrage", signature))
FAIL();
/* Try correct data */
if (!VERIFY(pub, md5,
"The magic words are squeamish ossifrage", signature))
FAIL();
/* Try bad signature */
mpz_togglebit(signature, 17);
if (VERIFY(pub, md5,
"The magic words are squeamish ossifrage", signature))
FAIL();
mpz_clear(signature);
}
static void
test_rsa_sha1(struct rsa_public_key *pub,
struct rsa_private_key *key,
mpz_t expected)
{
struct sha1_ctx sha1;
mpz_t signature;
sha1_init(&sha1);
mpz_init(signature);
SIGN(key, sha1, "The magic words are squeamish ossifrage", signature);
if (verbose)
{
fprintf(stderr, "rsa-sha1 signature: ");
mpz_out_str(stderr, 16, signature);
fprintf(stderr, "\n");
}
if (mpz_cmp(signature, expected))
FAIL();
/* Try bad data */
if (VERIFY(pub, sha1,
"The magick words are squeamish ossifrage", signature))
FAIL();
/* Try correct data */
if (!VERIFY(pub, sha1,
"The magic words are squeamish ossifrage", signature))
FAIL();
/* Try bad signature */
mpz_togglebit(signature, 17);
if (VERIFY(pub, sha1,
"The magic words are squeamish ossifrage", signature))
FAIL();
mpz_clear(signature);
}
int
test_main(void)
{
......
/* testutils.c */
#include "testutils.h"
#include "cbc.h"
......@@ -220,3 +221,112 @@ test_hash(const struct nettle_hash *hash,
if (buffer[hash->digest_size - 1])
FAIL();
}
#if HAVE_LIBGMP
#define SIGN(key, hash, msg, signature) do { \
hash##_update(&hash, LDATA(msg)); \
rsa_##hash##_sign(key, &hash, signature); \
} while(0)
#define VERIFY(key, hash, msg, signature) ( \
hash##_update(&hash, LDATA(msg)), \
rsa_##hash##_verify(key, &hash, signature) \
)
/* Missing in current gmp */
static void
mpz_togglebit (mpz_t x, unsigned long int bit)
{
if (mpz_tstbit(x, bit))
mpz_clrbit(x, bit);
else
mpz_setbit(x, bit);
}
void
test_rsa_md5(struct rsa_public_key *pub,
struct rsa_private_key *key,
mpz_t expected)
{
struct md5_ctx md5;
mpz_t signature;
md5_init(&md5);
mpz_init(signature);
SIGN(key, md5, "The magic words are squeamish ossifrage", signature);
if (verbose)
{
fprintf(stderr, "rsa-md5 signature: ");
mpz_out_str(stderr, 16, signature);
fprintf(stderr, "\n");
}
if (mpz_cmp(signature, expected))
FAIL();
/* Try bad data */
if (VERIFY(pub, md5,
"The magick words are squeamish ossifrage", signature))
FAIL();
/* Try correct data */
if (!VERIFY(pub, md5,
"The magic words are squeamish ossifrage", signature))
FAIL();
/* Try bad signature */
mpz_togglebit(signature, 17);
if (VERIFY(pub, md5,
"The magic words are squeamish ossifrage", signature))
FAIL();
mpz_clear(signature);
}
void
test_rsa_sha1(struct rsa_public_key *pub,
struct rsa_private_key *key,
mpz_t expected)
{
struct sha1_ctx sha1;
mpz_t signature;
sha1_init(&sha1);
mpz_init(signature);
SIGN(key, sha1, "The magic words are squeamish ossifrage", signature);
if (verbose)
{
fprintf(stderr, "rsa-sha1 signature: ");
mpz_out_str(stderr, 16, signature);
fprintf(stderr, "\n");
}
if (mpz_cmp(signature, expected))
FAIL();
/* Try bad data */
if (VERIFY(pub, sha1,
"The magick words are squeamish ossifrage", signature))
FAIL();
/* Try correct data */
if (!VERIFY(pub, sha1,
"The magic words are squeamish ossifrage", signature))
FAIL();
/* Try bad signature */
mpz_togglebit(signature, 17);
if (VERIFY(pub, sha1,
"The magic words are squeamish ossifrage", signature))
FAIL();
mpz_clear(signature);
}
#endif /* HAVE_LIBGMP */
#ifndef NETTLE_TESTUTILS_H_INCLUDED
#define NETTLE_TESTUTILS_H_INCLUDED
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#if HAVE_LIBGMP
# include "rsa.h"
#endif
#include "nettle-meta.h"
......@@ -49,6 +58,18 @@ test_hash(const struct nettle_hash *hash,
const uint8_t *data,
const uint8_t *digest);
#if HAVE_LIBGMP
void
test_rsa_md5(struct rsa_public_key *pub,
struct rsa_private_key *key,
mpz_t expected);
void
test_rsa_sha1(struct rsa_public_key *pub,
struct rsa_private_key *key,
mpz_t expected);
#endif /* HAVE_LIBGMP */
#define H2(d, s) decode_hex((d), (s))
#define H(x) decode_hex_dup(x)
#define HL(x) decode_hex_length(x), decode_hex_dup(x)
......
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