From 19e4248ef467e07509686d06fa1650ece17d1770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se> Date: Wed, 9 Oct 2002 09:37:35 +0200 Subject: [PATCH] (test_dsa): New function. Rev: src/nettle/testsuite/testutils.c:1.14 Rev: src/nettle/testsuite/testutils.h:1.12 --- testsuite/testutils.c | 60 +++++++++++++++++++++++++++++++++++++++++++ testsuite/testutils.h | 4 +++ 2 files changed, 64 insertions(+) diff --git a/testsuite/testutils.c b/testsuite/testutils.c index a954a374..8b96771f 100644 --- a/testsuite/testutils.c +++ b/testsuite/testutils.c @@ -3,6 +3,7 @@ #include "testutils.h" #include "cbc.h" +#include "knuth-lfib.h" #include <ctype.h> #include <stdio.h> @@ -376,6 +377,9 @@ test_rsa_sha1(struct rsa_public_key *pub, mpz_clear(signature); } +#undef SIGN +#undef VERIFY + void test_rsa_key(struct rsa_public_key *pub, struct rsa_private_key *key) @@ -446,5 +450,61 @@ test_rsa_key(struct rsa_public_key *pub, mpz_clear(tmp); mpz_clear(phi); } + +#define DSA_VERIFY(key, hash, msg, signature) ( \ + sha1_update(hash, LDATA(msg)), \ + dsa_verify(key, hash, signature) \ +) + +void +test_dsa(struct dsa_private_key *key) +{ + struct sha1_ctx sha1; + struct dsa_signature signature; + struct knuth_lfib_ctx lfib; + + sha1_init(&sha1); + dsa_signature_init(&signature); + knuth_lfib_init(&lfib, 1111); + + sha1_update(&sha1, LDATA("The magic words are squeamish ossifrage")); + dsa_sign(key, + &lfib, (nettle_random_func) knuth_lfib_random, + &sha1, &signature); + + if (verbose) + { + fprintf(stderr, "dsa signature: "); + mpz_out_str(stderr, 16, signature.r); + fprintf(stderr, ", "); + mpz_out_str(stderr, 16, signature.s); + fprintf(stderr, "\n"); + } + +#if 0 + if (mpz_cmp(signature, expected)) + FAIL(); +#endif + + /* Try bad data */ + if (DSA_VERIFY(&key->pub, &sha1, + "The magick words are squeamish ossifrage", &signature)) + FAIL(); + + /* Try correct data */ + if (!DSA_VERIFY(&key->pub, &sha1, + "The magic words are squeamish ossifrage", &signature)) + FAIL(); + + /* Try bad signature */ + mpz_togglebit(signature.r, 17); + + if (DSA_VERIFY(&key->pub, &sha1, + "The magic words are squeamish ossifrage", &signature)) + FAIL(); + + dsa_signature_clear(&signature); +} + #endif /* WITH_PUBLIC_KEY */ diff --git a/testsuite/testutils.h b/testsuite/testutils.h index d1f9f764..1bd50549 100644 --- a/testsuite/testutils.h +++ b/testsuite/testutils.h @@ -17,6 +17,7 @@ #if WITH_PUBLIC_KEY # include "rsa.h" +# include "dsa.h" #endif #include "nettle-meta.h" @@ -86,6 +87,9 @@ void test_rsa_key(struct rsa_public_key *pub, struct rsa_private_key *key); +void +test_dsa(struct dsa_private_key *key); + #endif /* WITH_PUBLIC_KEY */ #define H2(d, s) decode_hex((d), (s)) -- GitLab