summaryrefslogtreecommitdiff
path: root/src/cpuid.c
diff options
context:
space:
mode:
authorSebastian Ramacher <sebastian@ramacher.at>2013-02-04 14:44:29 +0100
committerDwayne Litzenberger <dlitz@dlitz.net>2013-04-21 20:41:18 -0700
commite1ce77b1673db76fb46d87effa7b1a1dc083d9b7 (patch)
tree3c999461384918aa9b1c2f10813db7211e2534b1 /src/cpuid.c
parent1dd8353cc490f954677285415ec01e253f84b93d (diff)
downloadpycrypto-e1ce77b1673db76fb46d87effa7b1a1dc083d9b7.tar.gz
Initial AES-NI support
Diffstat (limited to 'src/cpuid.c')
-rw-r--r--src/cpuid.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/cpuid.c b/src/cpuid.c
new file mode 100644
index 0000000..e4d8b27
--- /dev/null
+++ b/src/cpuid.c
@@ -0,0 +1,100 @@
+/*
+ * cpuid.c: check CPU capabilities
+ *
+ * Written in 2013 by Sebastian Ramacher <sebastian@ramacher.at>
+ *
+ * ===================================================================
+ * The contents of this file are dedicated to the public domain. To
+ * the extent that dedication to the public domain is not available,
+ * everyone is granted a worldwide, perpetual, royalty-free,
+ * non-exclusive license to exercise all rights associated with the
+ * contents of this file for any purpose whatsoever.
+ * No rights are reserved.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ * ===================================================================
+ */
+#include "Python.h"
+#include <stdint.h>
+#include "config.h"
+#ifdef HAVE_CPUID_H
+#include <cpuid.h>
+#endif
+
+#include "pycrypto_compat.h"
+
+/*
+ * The have_aes_ni Python function
+ */
+
+static char have_aes_ni__doc__[] =
+"have_aes_ni() -> bool\n"
+"\n"
+"Return whether AES-NI instructions are available.\n";
+
+static PyObject *
+have_aes_ni(PyObject *self, PyObject *args)
+{
+#ifndef HAVE_CPUID_H
+ Py_INCREF(Py_False);
+ return Py_False;
+#else
+ uint32_t eax, ebx, ecx, edx;
+ /* call cpuid to check if AES-NI instructions are available */
+ if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
+ if (ecx & bit_AES) {
+ Py_INCREF(Py_True);
+ return Py_True;
+ }
+ }
+ Py_INCREF(Py_False);
+ return Py_False;
+#endif
+}
+
+/*
+ * Module-level method table and module initialization function
+ */
+
+static PyMethodDef cpuid_methods[] = {
+ {"have_aes_ni", have_aes_ni, METH_NOARGS, have_aes_ni__doc__},
+ {NULL, NULL, 0, NULL} /* end-of-list sentinel value */
+};
+
+#ifdef IS_PY3K
+static struct PyModuleDef moduledef = {
+ PyModuleDef_HEAD_INIT,
+ "cpuid",
+ NULL,
+ -1,
+ cpuid_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+};
+#endif
+
+PyMODINIT_FUNC
+#ifdef IS_PY3K
+PyInit_cpuid(void)
+#else
+initcpuid(void)
+#endif
+{
+ /* Initialize the module */
+#ifdef IS_PY3K
+ return PyModule_Create(&moduledef);
+#else
+ Py_InitModule("cpuid", cpuid_methods);
+#endif
+}
+
+/* vim:set ts=4 sw=4 sts=4 expandtab: */