]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
(test_dsa): New function.
authorNiels Möller <nisse@lysator.liu.se>
Wed, 9 Oct 2002 07:37:35 +0000 (09:37 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Wed, 9 Oct 2002 07:37:35 +0000 (09:37 +0200)
Rev: src/nettle/testsuite/testutils.c:1.14
Rev: src/nettle/testsuite/testutils.h:1.12

testsuite/testutils.c
testsuite/testutils.h

index a954a37437bb56a235cd27657d95cf5e078dea8c..8b96771faf0b3f60ee777d39b7eb1efdc909c1db 100644 (file)
@@ -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 */
 
index d1f9f7648de3dbc5b5a5a3941b0f6b4e98f254ea..1bd50549d88c85505768b3a36e1e51ba5401c4c8 100644 (file)
@@ -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))