From 460a33f6f7949320e3df5c9f5f12b70bf1310412 Mon Sep 17 00:00:00 2001 From: Dwayne Litzenberger Date: Mon, 9 Jun 2014 05:14:13 -0700 Subject: Allow block ciphers to specify which modes they support --- src/AES.c | 6 ++++++ src/AESNI.c | 6 ++++++ src/ARC2.c | 6 ++++++ src/Blowfish.c | 6 ++++++ src/CAST.c | 6 ++++++ src/DES.c | 6 ++++++ src/block_template.c | 31 +++++++++++++++++++++++-------- src/pycrypto_common.h | 5 +++++ 8 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/AES.c b/src/AES.c index 1e705da..2f686d4 100644 --- a/src/AES.c +++ b/src/AES.c @@ -32,6 +32,12 @@ #define BLOCK_SIZE 16 #define KEY_SIZE 0 +#define HAS_ENCRYPT 1 +#define HAS_MODE_CBC 1 +#define HAS_MODE_CFB 1 +#define HAS_MODE_OFB 1 +#define HAS_MODE_CTR 1 + #define MAXKC (256/32) #define MAXKB (256/8) #define MAXNR 14 diff --git a/src/AESNI.c b/src/AESNI.c index 50f0cd6..b8e0030 100644 --- a/src/AESNI.c +++ b/src/AESNI.c @@ -33,6 +33,12 @@ #define BLOCK_SIZE 16 #define KEY_SIZE 0 +#define HAS_ENCRYPT 1 +#define HAS_MODE_CBC 1 +#define HAS_MODE_CFB 1 +#define HAS_MODE_OFB 1 +#define HAS_MODE_CTR 1 + #define MAXKC (256/32) #define MAXKB (256/8) #define MAXNR 14 diff --git a/src/ARC2.c b/src/ARC2.c index 71858f6..d6c6f17 100644 --- a/src/ARC2.c +++ b/src/ARC2.c @@ -49,6 +49,12 @@ #define KEY_SIZE 0 #define PCT_ARC2_MODULE /* Defined to get ARC2's additional keyword arguments */ +#define HAS_ENCRYPT 1 +#define HAS_MODE_CBC 1 +#define HAS_MODE_CFB 1 +#define HAS_MODE_OFB 1 +#define HAS_MODE_CTR 1 + typedef uint32_t U32; typedef uint16_t U16; typedef uint8_t U8; diff --git a/src/Blowfish.c b/src/Blowfish.c index f1ab55a..cd0a54a 100644 --- a/src/Blowfish.c +++ b/src/Blowfish.c @@ -35,6 +35,12 @@ #define BLOCK_SIZE 8 /* 64-bit block size */ #define KEY_SIZE 0 /* variable key size */ +#define HAS_ENCRYPT 1 +#define HAS_MODE_CBC 1 +#define HAS_MODE_CFB 1 +#define HAS_MODE_OFB 1 +#define HAS_MODE_CTR 1 + #define BLOWFISH_MAGIC 0xf9d565deu typedef struct { uint32_t magic; diff --git a/src/CAST.c b/src/CAST.c index d7b00f3..9476683 100644 --- a/src/CAST.c +++ b/src/CAST.c @@ -48,6 +48,12 @@ #define BLOCK_SIZE 8 #define KEY_SIZE 0 +#define HAS_ENCRYPT 1 +#define HAS_MODE_CBC 1 +#define HAS_MODE_CFB 1 +#define HAS_MODE_OFB 1 +#define HAS_MODE_CTR 1 + /* adjust these according to your compiler/platform. On some machines uint32 will have to be a long. It's OK if uint32 is more than 32 bits. */ typedef uint32_t uint32; diff --git a/src/DES.c b/src/DES.c index 65171fb..c0c6f02 100644 --- a/src/DES.c +++ b/src/DES.c @@ -37,6 +37,12 @@ #include +#define HAS_ENCRYPT 1 +#define HAS_MODE_CBC 1 +#define HAS_MODE_CFB 1 +#define HAS_MODE_OFB 1 +#define HAS_MODE_CTR 1 + typedef struct { symmetric_key sk; } block_state; diff --git a/src/block_template.c b/src/block_template.c index 99aee43..fcd5265 100644 --- a/src/block_template.c +++ b/src/block_template.c @@ -134,17 +134,30 @@ ALGnew(PyObject *self, PyObject *args, PyObject *kwdict) return NULL; } - if (modeMODE_CTR) - { - PyErr_Format(PyExc_ValueError, - "Unknown cipher feedback mode %i", - mode); - return NULL; - } - if (mode == MODE_PGP) { + switch (mode) { + case MODE_ECB: +#ifdef HAS_MODE_CBC + case MODE_CBC: +#endif +#ifdef HAS_MODE_CFB + case MODE_CFB: +#endif +#ifdef HAS_MODE_OFB + case MODE_OFB: +#endif +#ifdef HAS_MODE_CTR + case MODE_CTR: +#endif + break; + case MODE_PGP: PyErr_Format(PyExc_ValueError, "MODE_PGP is not supported anymore"); return NULL; + default: + PyErr_Format(PyExc_ValueError, + "Unknown or unsupported cipher feedback mode %i", + mode); + return NULL; } if (KEY_SIZE!=0 && keylen!=KEY_SIZE) { @@ -611,7 +624,9 @@ ALG_Decrypt(ALGobject *self, PyObject *args) /* ALG object methods */ static PyMethodDef ALGmethods[] = { +#ifdef HAS_ENCRYPT {"encrypt", (PyCFunction) ALG_Encrypt, METH_O, ALG_Encrypt__doc__}, +#endif {"decrypt", (PyCFunction) ALG_Decrypt, METH_O, ALG_Decrypt__doc__}, {NULL, NULL} /* sentinel */ }; diff --git a/src/pycrypto_common.h b/src/pycrypto_common.h index 3182ec1..626543e 100644 --- a/src/pycrypto_common.h +++ b/src/pycrypto_common.h @@ -37,5 +37,10 @@ # error "stdint.h and inttypes.h not found" #endif +#undef HAS_ENCRYPT +#undef HAS_MODE_CBC +#undef HAS_MODE_CFB +#undef HAS_MODE_OFB +#undef HAS_MODE_CTR #endif /* PYCRYPTO_COMMON_H */ -- cgit v1.2.1