summaryrefslogtreecommitdiff
path: root/cipher/primegen.c
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 /cipher/primegen.c
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.
Diffstat (limited to 'cipher/primegen.c')
-rw-r--r--cipher/primegen.c19
1 files changed, 10 insertions, 9 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