summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDwayne C. Litzenberger <dlitz@dlitz.net>2011-10-10 12:40:39 -0400
committerDwayne C. Litzenberger <dlitz@dlitz.net>2011-10-10 14:49:26 -0400
commit9cfb332b22e38b9d965bfb691eca67b6b8fa64e3 (patch)
tree6c01ceb39716bacbe3d2a0c756ad3a5adac119f1 /src
parent86c4cf4ea66e926267f53348d22698774a7939a5 (diff)
downloadpycrypto-9cfb332b22e38b9d965bfb691eca67b6b8fa64e3.tar.gz
autoconf: only use side-channel secured mpz_powm_sec if it's available (libgmp 5 or later)
Diffstat (limited to 'src')
-rwxr-xr-xsrc/_fastmath.c32
-rw-r--r--src/config.h.in138
-rw-r--r--src/inc-msvc/config.h3
3 files changed, 164 insertions, 9 deletions
diff --git a/src/_fastmath.c b/src/_fastmath.c
index acfaed0..bd70115 100755
--- a/src/_fastmath.c
+++ b/src/_fastmath.c
@@ -32,6 +32,18 @@
#include <Python.h>
#include <longintrepr.h> /* for conversions */
#include <gmp.h>
+#include "config.h"
+
+/* If available, use mpz_powm_sec to avoid timing attacks.
+ * See the talk by Geremy Condra -
+ * "PyCon 2011: Through the Side Channel: Timing and Implementation Attacks in Python"
+ * http://blip.tv/pycon-us-videos-2009-2010-2011/pycon-2011-through-the-side-channel-timing-and-implementation-attacks-in-python-4897955
+ */
+#if HAVE_DECL_MPZ_POWM_SEC == 1
+#define MPZ_POWM mpz_powm_sec
+#else
+#define MPZ_POWM mpz_powm
+#endif
#define SIEVE_BASE_SIZE (sizeof (sieve_base) / sizeof (sieve_base[0]))
@@ -134,7 +146,7 @@ dsaSign (dsaKey * key, mpz_t m, mpz_t k, mpz_t r, mpz_t s)
return 1;
}
mpz_init (temp);
- mpz_powm_sec (r, key->g, k, key->p);
+ MPZ_POWM (r, key->g, k, key->p);
mpz_mod (r, r, key->q);
mpz_invert (s, k, key->q);
mpz_mul (temp, key->x, r);
@@ -163,8 +175,8 @@ dsaVerify (dsaKey * key, mpz_t m, mpz_t r, mpz_t s)
mpz_mod (u1, u1, key->q);
mpz_mul (u2, r, w);
mpz_mod (u2, u2, key->q);
- mpz_powm_sec (v1, key->g, u1, key->p);
- mpz_powm_sec (v2, key->y, u2, key->p);
+ MPZ_POWM (v1, key->g, u1, key->p);
+ MPZ_POWM (v2, key->y, u2, key->p);
mpz_mul (w, v1, v2);
mpz_mod (w, w, key->p);
mpz_mod (w, w, key->q);
@@ -188,7 +200,7 @@ rsaEncrypt (rsaKey * key, mpz_t v)
{
return 1;
}
- mpz_powm_sec (v, v, key->e, key->n);
+ MPZ_POWM (v, v, key->e, key->n);
return 0;
}
@@ -216,11 +228,11 @@ rsaDecrypt (rsaKey * key, mpz_t v)
/* m1 = c ^ (d mod (p-1)) mod p */
mpz_sub_ui(h, key->p, 1);
mpz_fdiv_r(h, key->d, h);
- mpz_powm_sec(m1, v, h, key->p);
+ MPZ_POWM(m1, v, h, key->p);
/* m2 = c ^ (d mod (q-1)) mod q */
mpz_sub_ui(h, key->q, 1);
mpz_fdiv_r(h, key->d, h);
- mpz_powm_sec(m2, v, h, key->q);
+ MPZ_POWM(m2, v, h, key->q);
/* h = u * ( m2 - m1 ) mod q */
mpz_sub(h, m2, m1);
if (mpz_sgn(h)==-1)
@@ -239,7 +251,7 @@ rsaDecrypt (rsaKey * key, mpz_t v)
}
/* slow */
- mpz_powm_sec (v, v, key->d, key->n);
+ MPZ_POWM (v, v, key->d, key->n);
return 0;
}
@@ -254,7 +266,7 @@ rsaBlind (rsaKey * key, mpz_t v, mpz_t b)
{
return 2;
}
- mpz_powm_sec (b, b, key->e, key->n);
+ MPZ_POWM (b, b, key->e, key->n);
mpz_mul (v, v, b);
mpz_mod (v, v, key->n);
return 0;
@@ -1101,7 +1113,7 @@ rabinMillerTest (mpz_t n, int rounds, PyObject *randfunc)
}
} while (base_was_tested);
mpz_init_set (tested[i], a);
- mpz_powm_sec (z, a, m, n);
+ MPZ_POWM (z, a, m, n);
if ((mpz_cmp_ui (z, 1) == 0) || (mpz_cmp (z, n_1) == 0))
continue;
composite = 1;
@@ -1390,6 +1402,8 @@ init_fastmath (void)
_fastmath_dict = PyModule_GetDict (_fastmath_module);
fastmathError = PyErr_NewException ("_fastmath.error", NULL, NULL);
PyDict_SetItemString (_fastmath_dict, "error", fastmathError);
+
+ PyModule_AddIntConstant(_fastmath_module, "HAVE_DECL_MPZ_POWM_SEC", HAVE_DECL_MPZ_POWM_SEC);
}
diff --git a/src/config.h.in b/src/config.h.in
new file mode 100644
index 0000000..b71dd4e
--- /dev/null
+++ b/src/config.h.in
@@ -0,0 +1,138 @@
+/* src/config.h.in. Generated from configure.ac by autoheader. */
+
+/* Define to 1 if you have the declaration of `mpz_powm', and to 0 if you
+ don't. */
+#undef HAVE_DECL_MPZ_POWM
+
+/* Define to 1 if you have the declaration of `mpz_powm_sec', and to 0 if you
+ don't. */
+#undef HAVE_DECL_MPZ_POWM_SEC
+
+/* Define to 1 if you have the <inttypes.h> header file. */
+#undef HAVE_INTTYPES_H
+
+/* Define to 1 if you have the `' library (-l). */
+#undef HAVE_LIB
+
+/* Define to 1 if you have the <limits.h> header file. */
+#undef HAVE_LIMITS_H
+
+/* Define to 1 if your system has a GNU libc compatible `malloc' function, and
+ to 0 otherwise. */
+#undef HAVE_MALLOC
+
+/* Define to 1 if you have the `memmove' function. */
+#undef HAVE_MEMMOVE
+
+/* Define to 1 if you have the <memory.h> header file. */
+#undef HAVE_MEMORY_H
+
+/* Define to 1 if you have the `memset' function. */
+#undef HAVE_MEMSET
+
+/* Define to 1 if you have the <stddef.h> header file. */
+#undef HAVE_STDDEF_H
+
+/* Define to 1 if you have the <stdint.h> header file. */
+#undef HAVE_STDINT_H
+
+/* Define to 1 if you have the <stdlib.h> header file. */
+#undef HAVE_STDLIB_H
+
+/* Define to 1 if you have the <strings.h> header file. */
+#undef HAVE_STRINGS_H
+
+/* Define to 1 if you have the <string.h> header file. */
+#undef HAVE_STRING_H
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#undef HAVE_SYS_STAT_H
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#undef HAVE_SYS_TYPES_H
+
+/* Define to 1 if you have the <unistd.h> header file. */
+#undef HAVE_UNISTD_H
+
+/* Define to 1 if you have the <wchar.h> header file. */
+#undef HAVE_WCHAR_H
+
+/* Define to the address where bug reports for this package should be sent. */
+#undef PACKAGE_BUGREPORT
+
+/* Define to the full name of this package. */
+#undef PACKAGE_NAME
+
+/* Define to the full name and version of this package. */
+#undef PACKAGE_STRING
+
+/* Define to the one symbol short name of this package. */
+#undef PACKAGE_TARNAME
+
+/* Define to the home page for this package. */
+#undef PACKAGE_URL
+
+/* Define to the version of this package. */
+#undef PACKAGE_VERSION
+
+/* Define to 1 if you have the ANSI C header files. */
+#undef STDC_HEADERS
+
+/* Define for Solaris 2.5.1 so the uint32_t typedef from <sys/synch.h>,
+ <pthread.h>, or <semaphore.h> is not used. If the typedef were allowed, the
+ #define below would cause a syntax error. */
+#undef _UINT32_T
+
+/* Define for Solaris 2.5.1 so the uint64_t typedef from <sys/synch.h>,
+ <pthread.h>, or <semaphore.h> is not used. If the typedef were allowed, the
+ #define below would cause a syntax error. */
+#undef _UINT64_T
+
+/* Define for Solaris 2.5.1 so the uint8_t typedef from <sys/synch.h>,
+ <pthread.h>, or <semaphore.h> is not used. If the typedef were allowed, the
+ #define below would cause a syntax error. */
+#undef _UINT8_T
+
+/* Define to `__inline__' or `__inline' if that's what the C compiler
+ calls it, or to nothing if 'inline' is not supported under any name. */
+#ifndef __cplusplus
+#undef inline
+#endif
+
+/* Define to the type of a signed integer type of width exactly 16 bits if
+ such a type exists and the standard includes do not define it. */
+#undef int16_t
+
+/* Define to the type of a signed integer type of width exactly 32 bits if
+ such a type exists and the standard includes do not define it. */
+#undef int32_t
+
+/* Define to the type of a signed integer type of width exactly 64 bits if
+ such a type exists and the standard includes do not define it. */
+#undef int64_t
+
+/* Define to the type of a signed integer type of width exactly 8 bits if such
+ a type exists and the standard includes do not define it. */
+#undef int8_t
+
+/* Define to rpl_malloc if the replacement function should be used. */
+#undef malloc
+
+/* Define to `unsigned int' if <sys/types.h> does not define. */
+#undef size_t
+
+/* Define to the type of an unsigned integer type of width exactly 16 bits if
+ such a type exists and the standard includes do not define it. */
+#undef uint16_t
+
+/* Define to the type of an unsigned integer type of width exactly 32 bits if
+ such a type exists and the standard includes do not define it. */
+#undef uint32_t
+
+/* Define to the type of an unsigned integer type of width exactly 64 bits if
+ such a type exists and the standard includes do not define it. */
+#undef uint64_t
+
+/* Define to the type of an unsigned integer type of width exactly 8 bits if
+ such a type exists and the standard includes do not define it. */
+#undef uint8_t
diff --git a/src/inc-msvc/config.h b/src/inc-msvc/config.h
new file mode 100644
index 0000000..19ced1d
--- /dev/null
+++ b/src/inc-msvc/config.h
@@ -0,0 +1,3 @@
+/* Define to 1 if you have the declaration of `mpz_powm_sec', and to 0 if you
+ don't. */
+#define HAVE_DECL_MPZ_POWM_SEC 0