summaryrefslogtreecommitdiff
path: root/src/block_template.c
diff options
context:
space:
mode:
authorDwayne C. Litzenberger <dlitz@dlitz.net>2009-10-10 13:06:42 -0400
committerDwayne C. Litzenberger <dlitz@dlitz.net>2009-10-11 01:08:13 -0400
commit3b4338d285b9d12caf8863c5cb510264661f2676 (patch)
tree4ceaee2f3b8e2228b71f2c8457d7f8abdefdf9d0 /src/block_template.c
parent0b6982e7dd9c16ba9d86a588391c399e5d43729e (diff)
downloadpycrypto-3b4338d285b9d12caf8863c5cb510264661f2676.tar.gz
block_template.c: Call ALG_Encrypt when using MODE_CTR, rather than duplicating code in ALG_Decrypt
Diffstat (limited to 'src/block_template.c')
-rw-r--r--src/block_template.c62
1 files changed, 5 insertions, 57 deletions
diff --git a/src/block_template.c b/src/block_template.c
index 9f4cdf9..d1620fe 100644
--- a/src/block_template.c
+++ b/src/block_template.c
@@ -437,7 +437,11 @@ ALG_Decrypt(ALGobject *self, PyObject *args)
unsigned char temp[BLOCK_SIZE];
int i, j, len;
PyObject *result;
-
+
+ /* CTR mode decryption is identical to encryption */
+ if (self->mode == MODE_CTR)
+ return ALG_Encrypt(self, args);
+
if (!PyArg_Parse(args, "s#", &str, &len))
return NULL;
if (len==0) /* Handle empty string */
@@ -571,62 +575,6 @@ ALG_Decrypt(ALGobject *self, PyObject *args)
}
break;
- case (MODE_CTR):
- for(i=0; i<len; i+=BLOCK_SIZE)
- {
- if (self->counter_shortcut) {
- /* CTR mode shortcut: If we're using Util.Counter,
- * bypass the normal Python function call mechanism
- * and manipulate the counter directly. */
-
- PCT_CounterObject *ctr = (PCT_CounterObject *)(self->counter);
- if (ctr->buf_size != BLOCK_SIZE) {
- PyErr_Format(PyExc_TypeError,
- "CTR counter function returned "
- "string not of length %i",
- BLOCK_SIZE);
- free(buffer);
- return NULL;
- }
- block_encrypt(&(self->st),
- (unsigned char *)ctr->val,
- temp);
- ctr->inc_func(ctr);
-
- } else {
- PyObject *ctr = PyObject_CallObject(self->counter, NULL);
- if (ctr == NULL) {
- free(buffer);
- return NULL;
- }
- if (!PyString_Check(ctr))
- {
- PyErr_SetString(PyExc_TypeError,
- "CTR counter function didn't return a string");
- Py_DECREF(ctr);
- free(buffer);
- return NULL;
- }
- if (PyString_Size(ctr) != BLOCK_SIZE) {
- PyErr_Format(PyExc_TypeError,
- "CTR counter function returned "
- "string not of length %i",
- BLOCK_SIZE);
- Py_DECREF(ctr);
- free(buffer);
- return NULL;
- }
- block_encrypt(&(self->st), (unsigned char *)PyString_AsString(ctr),
- temp);
- Py_DECREF(ctr);
- }
- for(j=0; j<BLOCK_SIZE; j++)
- {
- buffer[i+j] = str[i+j]^temp[j];
- }
- }
- break;
-
default:
PyErr_Format(PyExc_SystemError,
"Unknown ciphertext feedback mode %i; "