summaryrefslogtreecommitdiff
path: root/mpi/mpi-pow.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2013-07-17 10:18:39 +0200
committerWerner Koch <wk@gnupg.org>2013-07-17 10:18:39 +0200
commit6e1adb05d290aeeb1c230c763970695f4a538526 (patch)
treeb599d80d243bf9a26f261d1436532233e6f67e75 /mpi/mpi-pow.c
parent61b44812728d1feca880a613c685040ba82c05ce (diff)
downloadlibgcrypt-6e1adb05d290aeeb1c230c763970695f4a538526.tar.gz
Fix a special case bug in mpi_powm for e==0.
* mpi/mpi-pow.c (gcry_mpi_powm): For a zero exponent, make sure that the result has been allocated. -- This code triggered the problem: modulus = gcry_mpi_set_ui(NULL, 100); generator = gcry_mpi_set_ui(NULL, 3); exponent = gcry_mpi_set_ui(NULL, 0); result = gcry_mpi_new(0); gcry_mpi_powm(result, generator, exponent, modulus); gcry_mpi_new(0) does not allocate the limb space thus it is not possible to write even into the first limb. Workaround was to use gcry_mpi_new (1) but a real fix is better. Reported-by: Ian Goldberg Signed-off-by: Werner Koch <wk@gnupg.org>
Diffstat (limited to 'mpi/mpi-pow.c')
-rw-r--r--mpi/mpi-pow.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/mpi/mpi-pow.c b/mpi/mpi-pow.c
index 891a7e65..7ec49d73 100644
--- a/mpi/mpi-pow.c
+++ b/mpi/mpi-pow.c
@@ -81,9 +81,14 @@ gcry_mpi_powm (gcry_mpi_t res,
if (!esize)
{
/* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0 depending
- on if MOD equals 1. */
- rp[0] = 1;
+ on if MOD equals 1. */
res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
+ if (res->nlimbs)
+ {
+ RESIZE_IF_NEEDED (res, 1);
+ rp = res->d;
+ rp[0] = 1;
+ }
res->sign = 0;
goto leave;
}