diff options
author | Dwayne C. Litzenberger <dlitz@dlitz.net> | 2009-10-12 16:11:21 -0400 |
---|---|---|
committer | Dwayne C. Litzenberger <dlitz@dlitz.net> | 2009-10-12 16:43:18 -0400 |
commit | 2150aca61f27dc80981a914d2b11c038e84b0c87 (patch) | |
tree | 506097d893e2804b1b919a4ad08d6a4c50b032df | |
parent | acf6183a53e2ce8849bc27afd51991babe69338d (diff) | |
download | pycrypto-2150aca61f27dc80981a914d2b11c038e84b0c87.tar.gz |
Release the global interpreter lock during encryption, decryption, and hashing.
These are the easy ones. We don't release the GIL on cipher initialization,
hash initialization, or hash finalization, because those functions might make
Python API calls, and we would need to add a mechism for re-acquiring the GIL
in those cases.
-rw-r--r-- | src/Blowfish.c | 11 | ||||
-rw-r--r-- | src/DES.c | 9 | ||||
-rw-r--r-- | src/_counter.c | 27 | ||||
-rw-r--r-- | src/_counter.h | 1 | ||||
-rw-r--r-- | src/block_template.c | 14 | ||||
-rw-r--r-- | src/hash_template.c | 7 | ||||
-rw-r--r-- | src/stream_template.c | 4 |
7 files changed, 37 insertions, 36 deletions
diff --git a/src/Blowfish.c b/src/Blowfish.c index a353fb6..f0d8594 100644 --- a/src/Blowfish.c +++ b/src/Blowfish.c @@ -26,6 +26,7 @@ * http://www.schneier.com/paper-blowfish-fse.html */ +#include <assert.h> #include <stdint.h> #include <string.h> #include "Python.h" @@ -133,10 +134,7 @@ static void Blowfish_encrypt(Blowfish_state *self, const unsigned char *in, unsi uint32_t xL, xR; /* Make sure the object is initialized */ - if (self->magic != BLOWFISH_MAGIC) { - PyErr_SetString(PyExc_AssertionError, "state not initialized"); - return; - } + assert(self->magic == BLOWFISH_MAGIC); /* big endian */ xL = bytes_to_word(in); @@ -154,10 +152,7 @@ static void Blowfish_decrypt(Blowfish_state *self, const unsigned char *in, unsi uint32_t xL, xR; /* Make sure the object is initialized */ - if (self->magic != BLOWFISH_MAGIC) { - PyErr_SetString(PyExc_AssertionError, "state not initialized"); - return; - } + assert(self->magic == BLOWFISH_MAGIC); /* big endian */ xL = bytes_to_word(in); @@ -33,6 +33,7 @@ #undef DES /* this is needed because tomcrypt_custom.h defines DES to an empty string */ +#include <assert.h> #include "Python.h" typedef struct { @@ -82,9 +83,7 @@ static void block_encrypt(block_state *self, unsigned char *in, unsigned char *o #else rc = des_ecb_encrypt(in, out, &self->sk); #endif - if (rc != CRYPT_OK) { - ltcseterr(rc); - } + assert(rc == CRYPT_OK); } static void block_decrypt(block_state *self, unsigned char *in, unsigned char *out) @@ -95,9 +94,7 @@ static void block_decrypt(block_state *self, unsigned char *in, unsigned char *o #else rc = des_ecb_decrypt(in, out, &self->sk); #endif - if (rc != CRYPT_OK) { - ltcseterr(rc); - } + assert(rc == CRYPT_OK); } #ifdef PCT_DES3_MODULE diff --git a/src/_counter.c b/src/_counter.c index 5e0e928..8a5522a 100644 --- a/src/_counter.c +++ b/src/_counter.c @@ -148,8 +148,11 @@ _CounterObject_next_value(PCT_CounterObject *self, int little_endian) PyObject *y = NULL; PyObject *x = NULL; - if (!self->check_wraparound_func(self)) + if (self->carry && !self->allow_wraparound) { + PyErr_SetString(PyExc_OverflowError, + "counter wrapped without allow_wraparound"); goto err_out; + } eight = PyInt_FromLong(8); if (!eight) @@ -265,8 +268,11 @@ CounterObject_call(PCT_CounterObject *self, PyObject *args, PyObject *kwargs) { PyObject *retval; - if (!self->check_wraparound_func(self)) + if (self->carry && !self->allow_wraparound) { + PyErr_SetString(PyExc_OverflowError, + "counter wrapped without allow_wraparound"); return NULL; + } retval = (PyObject *)PyString_FromStringAndSize((const char *)self->val, self->buf_size); @@ -275,17 +281,6 @@ CounterObject_call(PCT_CounterObject *self, PyObject *args, PyObject *kwargs) return retval; } -static int -CounterObject_check_wraparound(PCT_CounterObject *self) -{ - if (self->carry && !self->allow_wraparound) { - PyErr_SetString(PyExc_OverflowError, - "counter wrapped without allow_wraparound"); - return 0; - } - return 1; -} - static PyMethodDef CounterLEObject_methods[] = { {"next_value", (PyCFunction)CounterLEObject_next_value, METH_VARARGS, "Get the numerical value of next value of the counter."}, @@ -409,9 +404,6 @@ CounterLE_new(PyObject *self, PyObject *args, PyObject *kwargs) /* Set the inc_func pointer */ obj->inc_func = (void (*)(void *))CounterLEObject_increment; - /* Set the check_wraparound_func pointer */ - obj->check_wraparound_func = (int (*)(void *))CounterObject_check_wraparound; - /* Return the object */ return (PyObject *)obj; } @@ -438,9 +430,6 @@ CounterBE_new(PyObject *self, PyObject *args, PyObject *kwargs) /* Set the inc_func pointer */ obj->inc_func = (void (*)(void *))CounterBEObject_increment; - /* Set the check_wraparound_func pointer */ - obj->check_wraparound_func = (int (*)(void *))CounterObject_check_wraparound; - /* Return the object */ return (PyObject *)obj; } diff --git a/src/_counter.h b/src/_counter.h index af401cd..faaf63f 100644 --- a/src/_counter.h +++ b/src/_counter.h @@ -39,7 +39,6 @@ typedef struct { int shortcut_disabled; /* This gets set to a non-zero value when the shortcut mechanism is disabled */ int carry; /* This gets set by Counter*Object_increment when the counter wraps around */ int allow_wraparound; /* When this is false, we raise OverflowError on next_value() or __call__() when the counter wraps around */ - int (*check_wraparound_func)(void *); /* Pointer to the functon that checks for overflow. Returns zero and sets OverflowError if the check fails. */ } PCT_CounterObject; #endif /* PCT__COUNTER_H */ diff --git a/src/block_template.c b/src/block_template.c index 6b9a338..ec03f94 100644 --- a/src/block_template.c +++ b/src/block_template.c @@ -275,6 +275,7 @@ ALG_Encrypt(ALGobject *self, PyObject *args) _MODULE_STRING " encrypt"); return NULL; } + Py_BEGIN_ALLOW_THREADS; switch(self->mode) { case(MODE_ECB): @@ -409,11 +410,15 @@ ALG_Encrypt(ALGobject *self, PyObject *args) * and manipulate the counter directly. */ PCT_CounterObject *ctr = (PCT_CounterObject *)(self->counter); - if (!ctr->check_wraparound_func(ctr)) { + if (ctr->carry && !ctr->allow_wraparound) { + Py_BLOCK_THREADS; + PyErr_SetString(PyExc_OverflowError, + "counter wrapped without allow_wraparound"); free(buffer); return NULL; } if (ctr->buf_size != BLOCK_SIZE) { + Py_BLOCK_THREADS; PyErr_Format(PyExc_TypeError, "CTR counter function returned " "string not of length %i", @@ -426,6 +431,7 @@ ALG_Encrypt(ALGobject *self, PyObject *args) self->IV); ctr->inc_func(ctr); } else { + Py_BLOCK_THREADS; PyObject *ctr = PyObject_CallObject(self->counter, NULL); if (ctr == NULL) { free(buffer); @@ -448,9 +454,12 @@ ALG_Encrypt(ALGobject *self, PyObject *args) free(buffer); return NULL; } + Py_UNBLOCK_THREADS; block_encrypt(&(self->st), (unsigned char *)PyString_AsString(ctr), self->IV); + Py_BLOCK_THREADS; Py_DECREF(ctr); + Py_UNBLOCK_THREADS; } /* Move the pointer to the start of the keystream block */ @@ -466,6 +475,7 @@ ALG_Encrypt(ALGobject *self, PyObject *args) free(buffer); return NULL; } + Py_END_ALLOW_THREADS; result=PyString_FromStringAndSize((char *) buffer, len); free(buffer); return(result); @@ -518,6 +528,7 @@ ALG_Decrypt(ALGobject *self, PyObject *args) " decrypt"); return NULL; } + Py_BEGIN_ALLOW_THREADS; switch(self->mode) { case(MODE_ECB): @@ -628,6 +639,7 @@ ALG_Decrypt(ALGobject *self, PyObject *args) free(buffer); return NULL; } + Py_END_ALLOW_THREADS; result=PyString_FromStringAndSize((char *) buffer, len); free(buffer); return(result); diff --git a/src/hash_template.c b/src/hash_template.c index ba081f7..78e37be 100644 --- a/src/hash_template.c +++ b/src/hash_template.c @@ -149,7 +149,9 @@ ALG_update(ALGobject *self, PyObject *args) if (!PyArg_ParseTuple(args, "s#", &cp, &len)) return NULL; + Py_BEGIN_ALLOW_THREADS; hash_update(&(self->st), cp, len); + Py_END_ALLOW_THREADS; Py_INCREF(Py_None); return Py_None; @@ -220,8 +222,11 @@ ALG_new(PyObject *self, PyObject *args) Py_DECREF(new); return NULL; } - if (cp) + if (cp) { + Py_BEGIN_ALLOW_THREADS; hash_update(&(new->st), cp, len); + Py_END_ALLOW_THREADS; + } return (PyObject *)new; } diff --git a/src/stream_template.c b/src/stream_template.c index 81e4c32..e1db5b1 100644 --- a/src/stream_template.c +++ b/src/stream_template.c @@ -143,8 +143,10 @@ ALG_Encrypt(ALGobject *self, PyObject *args) _MODULE_STRING " encrypt"); return NULL; } + Py_BEGIN_ALLOW_THREADS; memcpy(buffer, str, len); stream_encrypt(&(self->st), buffer, len); + Py_END_ALLOW_THREADS; result = PyString_FromStringAndSize((char *)buffer, len); free(buffer); return (result); @@ -173,8 +175,10 @@ ALG_Decrypt(ALGobject *self, PyObject *args) _MODULE_STRING " decrypt"); return NULL; } + Py_BEGIN_ALLOW_THREADS; memcpy(buffer, str, len); stream_decrypt(&(self->st), buffer, len); + Py_END_ALLOW_THREADS; result = PyString_FromStringAndSize((char *)buffer, len); free(buffer); return (result); |