summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2014-10-08 14:41:21 +0200
committerWerner Koch <wk@gnupg.org>2014-10-08 14:46:06 +0200
commit5c906e2cdb14e93fb4915fdc69c7353a5fa35709 (patch)
tree6e251dabcdb3df2a7eb7ea25275ec8d7741cd5cd
parentde0ccd4dce7ec185a678d78878d4538dd609ca0f (diff)
downloadlibgcrypt-5c906e2cdb14e93fb4915fdc69c7353a5fa35709.tar.gz
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.
-rw-r--r--cipher/primegen.c19
-rw-r--r--src/mpicalc.c23
2 files changed, 32 insertions, 10 deletions
diff --git a/cipher/primegen.c b/cipher/primegen.c
index 14a5ccfe..ce6db8dd 100644
--- a/cipher/primegen.c
+++ b/cipher/primegen.c
@@ -868,7 +868,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. */
@@ -1169,19 +1169,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 b2b43351..f1fbbefe 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++)
{