[git] GCRYPT - branch, LIBGCRYPT-1-6-BRANCH, updated. libgcrypt-1.6.2-4-g0c2d144

by Werner Koch cvs at cvs.gnupg.org
Wed Oct 8 14:54:48 CEST 2014


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "The GNU crypto library".

The branch, LIBGCRYPT-1-6-BRANCH has been updated
       via  0c2d1443124dc6e65bd7f980f79aa2a6e33a82da (commit)
      from  d4b86782debb93773ed1ccb9f8c1a230ff6e84f8 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 0c2d1443124dc6e65bd7f980f79aa2a6e33a82da
Author: Werner Koch <wk at gnupg.org>
Date:   Wed Oct 8 14:41:21 2014 +0200

    Fix prime test for 2 and lower and add check command to mpicalc.
    
    * cipher/primegen.c (check_prime): Return true for the small primes.
    (_gcry_prime_check): Return correct values for 2 and lower numbers.
    
    * src/mpicalc.c (do_primecheck): New.
    (main): Add command 'P'.
    (main): Allow for larger input data.
    --
    (cherry picked from commit 5c906e2cdb14e93fb4915fdc69c7353a5fa35709)

diff --git a/cipher/primegen.c b/cipher/primegen.c
index dd1f2ea..e46bf18 100644
--- a/cipher/primegen.c
+++ b/cipher/primegen.c
@@ -882,7 +882,7 @@ check_prime( gcry_mpi_t prime, gcry_mpi_t val_2, int rm_rounds,
   for (i=0; (x = small_prime_numbers[i]); i++ )
     {
       if ( mpi_divisible_ui( prime, x ) )
-        return 0;
+        return !mpi_cmp_ui (prime, x);
     }
 
   /* A quick Fermat test. */
@@ -1183,19 +1183,20 @@ _gcry_prime_generate (gcry_mpi_t *prime, unsigned int prime_bits,
 gcry_err_code_t
 _gcry_prime_check (gcry_mpi_t x, unsigned int flags)
 {
-  gcry_err_code_t rc = 0;
-  gcry_mpi_t val_2 = mpi_alloc_set_ui (2); /* Used by the Fermat test. */
-
   (void)flags;
 
+  switch (mpi_cmp_ui (x, 2))
+    {
+    case 0:  return 0;                /* 2 is a prime */
+    case -1: return GPG_ERR_NO_PRIME; /* Only numbers > 1 are primes.  */
+    }
+
   /* We use 64 rounds because the prime we are going to test is not
      guaranteed to be a random one. */
-  if (! check_prime (x, val_2, 64, NULL, NULL))
-    rc = GPG_ERR_NO_PRIME;
-
-  mpi_free (val_2);
+  if (check_prime (x, mpi_const (MPI_C_TWO), 64, NULL, NULL))
+    return 0;
 
-  return rc;
+  return GPG_ERR_NO_PRIME;
 }
 
 /* Find a generator for PRIME where the factorization of (prime-1) is
diff --git a/src/mpicalc.c b/src/mpicalc.c
index b2b4335..f1fbbef 100644
--- a/src/mpicalc.c
+++ b/src/mpicalc.c
@@ -254,6 +254,23 @@ do_nbits (void)
 }
 
 
+static void
+do_primecheck (void)
+{
+  gpg_error_t err;
+
+  if (stackidx < 1)
+    {
+      fputs ("stack underflow\n", stderr);
+      return;
+    }
+  err = gcry_prime_check (stack[stackidx - 1], 0);
+  mpi_set_ui (stack[stackidx - 1], !err);
+  if (err && gpg_err_code (err) != GPG_ERR_NO_PRIME)
+    fprintf (stderr, "checking prime failed: %s\n", gpg_strerror (err));
+}
+
+
 static int
 my_getc (void)
 {
@@ -295,6 +312,7 @@ print_help (void)
          "d   dup item      [-1] := [0]               {+1}\n"
          "r   reverse       [0] := [1], [1] := [0]    {0}\n"
          "b   # of bits     [0] := nbits([0])         {0}\n"
+         "P   prime check   [0] := is_prime([0])?1:0  {0}\n"
          "c   clear stack\n"
          "p   print top item\n"
          "f   print the stack\n"
@@ -313,7 +331,7 @@ main (int argc, char **argv)
   int print_config = 0;
   int i, c;
   int state = 0;
-  char strbuf[1000];
+  char strbuf[4096];
   int stridx = 0;
 
   if (argc)
@@ -508,6 +526,9 @@ main (int argc, char **argv)
                 case 'b':
                   do_nbits ();
                   break;
+                case 'P':
+                  do_primecheck ();
+                  break;
 		case 'c':
 		  for (i = 0; i < stackidx; i++)
                     {

-----------------------------------------------------------------------

Summary of changes:
 cipher/primegen.c |   19 ++++++++++---------
 src/mpicalc.c     |   23 ++++++++++++++++++++++-
 2 files changed, 32 insertions(+), 10 deletions(-)


hooks/post-receive
-- 
The GNU crypto library
http://git.gnupg.org




More information about the Gnupg-commits mailing list