| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
#ifdef HAVE_CONFIG_H |
|---|
| 16 |
#include "config.h" |
|---|
| 17 |
#endif |
|---|
| 18 |
|
|---|
| 19 |
#ifdef _HAVE_STDC_HEADERS |
|---|
| 20 |
#include <string.h> |
|---|
| 21 |
#endif |
|---|
| 22 |
|
|---|
| 23 |
#include "Python.h" |
|---|
| 24 |
#include "modsupport.h" |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
#define MODE_ECB 1 |
|---|
| 29 |
#define MODE_CBC 2 |
|---|
| 30 |
#define MODE_CFB 3 |
|---|
| 31 |
#define MODE_PGP 4 |
|---|
| 32 |
#define MODE_OFB 5 |
|---|
| 33 |
#define MODE_CTR 6 |
|---|
| 34 |
|
|---|
| 35 |
#define _STR(x) #x |
|---|
| 36 |
#define _XSTR(x) _STR(x) |
|---|
| 37 |
#define _PASTE(x,y) x##y |
|---|
| 38 |
#define _PASTE2(x,y) _PASTE(x,y) |
|---|
| 39 |
#define _MODULE_NAME _PASTE2(init,MODULE_NAME) |
|---|
| 40 |
#define _MODULE_STRING _XSTR(MODULE_NAME) |
|---|
| 41 |
|
|---|
| 42 |
typedef struct |
|---|
| 43 |
{ |
|---|
| 44 |
PyObject_HEAD |
|---|
| 45 |
int mode, count, segment_size; |
|---|
| 46 |
unsigned char IV[BLOCK_SIZE], oldCipher[BLOCK_SIZE]; |
|---|
| 47 |
PyObject *counter; |
|---|
| 48 |
block_state st; |
|---|
| 49 |
} ALGobject; |
|---|
| 50 |
|
|---|
| 51 |
staticforward PyTypeObject ALGtype; |
|---|
| 52 |
|
|---|
| 53 |
#define is_ALGobject(v) ((v)->ob_type == &ALGtype) |
|---|
| 54 |
|
|---|
| 55 |
static ALGobject * |
|---|
| 56 |
newALGobject(void) |
|---|
| 57 |
{ |
|---|
| 58 |
ALGobject * new; |
|---|
| 59 |
new = PyObject_New(ALGobject, &ALGtype); |
|---|
| 60 |
new->mode = MODE_ECB; |
|---|
| 61 |
new->counter = NULL; |
|---|
| 62 |
return new; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
static void |
|---|
| 66 |
ALGdealloc(PyObject *ptr) |
|---|
| 67 |
{ |
|---|
| 68 |
ALGobject *self = (ALGobject *)ptr; |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
Py_XDECREF(self->counter); |
|---|
| 72 |
self->counter = NULL; |
|---|
| 73 |
memset(self->IV, 0, BLOCK_SIZE); |
|---|
| 74 |
memset(self->oldCipher, 0, BLOCK_SIZE); |
|---|
| 75 |
memset((char*)&(self->st), 0, sizeof(block_state)); |
|---|
| 76 |
self->mode = self->count = self->segment_size = 0; |
|---|
| 77 |
PyObject_Del(ptr); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
static char ALGnew__doc__[] = |
|---|
| 82 |
"new(key, [mode], [IV]): Return a new " _MODULE_STRING " encryption object."; |
|---|
| 83 |
|
|---|
| 84 |
static char *kwlist[] = {"key", "mode", "IV", "counter", "segment_size", |
|---|
| 85 |
#ifdef PCT_RC5_MODULE |
|---|
| 86 |
"version", "word_size", "rounds", |
|---|
| 87 |
#endif |
|---|
| 88 |
NULL}; |
|---|
| 89 |
|
|---|
| 90 |
static ALGobject * |
|---|
| 91 |
ALGnew(PyObject *self, PyObject *args, PyObject *kwdict) |
|---|
| 92 |
{ |
|---|
| 93 |
unsigned char *key, *IV; |
|---|
| 94 |
ALGobject * new=NULL; |
|---|
| 95 |
int keylen, IVlen=0, mode=MODE_ECB, segment_size=0; |
|---|
| 96 |
PyObject *counter = NULL; |
|---|
| 97 |
#ifdef PCT_RC5_MODULE |
|---|
| 98 |
int version = 0x10, word_size = 32, rounds = 16; |
|---|
| 99 |
#endif |
|---|
| 100 |
|
|---|
| 101 |
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s#|is#Oi" |
|---|
| 102 |
#ifdef PCT_RC5_MODULE |
|---|
| 103 |
"iii" |
|---|
| 104 |
#endif |
|---|
| 105 |
, kwlist, |
|---|
| 106 |
&key, &keylen, &mode, &IV, &IVlen, |
|---|
| 107 |
&counter, &segment_size |
|---|
| 108 |
#ifdef PCT_RC5_MODULE |
|---|
| 109 |
, &version, &word_size, &rounds |
|---|
| 110 |
#endif |
|---|
| 111 |
)) |
|---|
| 112 |
{ |
|---|
| 113 |
return NULL; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
if (KEY_SIZE!=0 && keylen!=KEY_SIZE) |
|---|
| 117 |
{ |
|---|
| 118 |
PyErr_Format(PyExc_ValueError, |
|---|
| 119 |
"Key must be %i bytes long, not %i", |
|---|
| 120 |
KEY_SIZE, keylen); |
|---|
| 121 |
return NULL; |
|---|
| 122 |
} |
|---|
| 123 |
if (KEY_SIZE==0 && keylen==0) |
|---|
| 124 |
{ |
|---|
| 125 |
PyErr_SetString(PyExc_ValueError, |
|---|
| 126 |
"Key cannot be the null string"); |
|---|
| 127 |
return NULL; |
|---|
| 128 |
} |
|---|
| 129 |
if (IVlen != BLOCK_SIZE && IVlen != 0) |
|---|
| 130 |
{ |
|---|
| 131 |
PyErr_Format(PyExc_ValueError, |
|---|
| 132 |
"IV must be %i bytes long", BLOCK_SIZE); |
|---|
| 133 |
return NULL; |
|---|
| 134 |
} |
|---|
| 135 |
if (mode<MODE_ECB || mode>MODE_CTR) |
|---|
| 136 |
{ |
|---|
| 137 |
PyErr_Format(PyExc_ValueError, |
|---|
| 138 |
"Unknown cipher feedback mode %i", |
|---|
| 139 |
mode); |
|---|
| 140 |
return NULL; |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
if (mode == MODE_CFB) { |
|---|
| 145 |
if (segment_size == 0) segment_size = 8; |
|---|
| 146 |
if (segment_size < 1 || segment_size > BLOCK_SIZE*8) { |
|---|
| 147 |
PyErr_Format(PyExc_ValueError, |
|---|
| 148 |
"segment_size must be multiple of 8 " |
|---|
| 149 |
"between 1 and %i", BLOCK_SIZE); |
|---|
| 150 |
} |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
if (mode == MODE_CTR) { |
|---|
| 154 |
if (!PyCallable_Check(counter)) { |
|---|
| 155 |
PyErr_SetString(PyExc_ValueError, |
|---|
| 156 |
"'counter' parameter must be a callable object"); |
|---|
| 157 |
} |
|---|
| 158 |
} else { |
|---|
| 159 |
if (counter != NULL) { |
|---|
| 160 |
PyErr_SetString(PyExc_ValueError, |
|---|
| 161 |
"'counter' parameter only useful with CTR mode"); |
|---|
| 162 |
} |
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
#ifdef PCT_RC5_MODULE |
|---|
| 167 |
if (version!=0x10) { |
|---|
| 168 |
PyErr_Format(PyExc_ValueError, |
|---|
| 169 |
"RC5: Bad RC5 algorithm version: %i", |
|---|
| 170 |
version); |
|---|
| 171 |
return NULL; |
|---|
| 172 |
} |
|---|
| 173 |
if (word_size!=16 && word_size!=32) { |
|---|
| 174 |
PyErr_Format(PyExc_ValueError, |
|---|
| 175 |
"RC5: Unsupported word size: %i", |
|---|
| 176 |
word_size); |
|---|
| 177 |
return NULL; |
|---|
| 178 |
} |
|---|
| 179 |
if (rounds<0 || 255<rounds) { |
|---|
| 180 |
PyErr_Format(PyExc_ValueError, |
|---|
| 181 |
"RC5: rounds must be between 0 and 255, not %i", |
|---|
| 182 |
rounds); |
|---|
| 183 |
return NULL; |
|---|
| 184 |
} |
|---|
| 185 |
#endif |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
new = newALGobject(); |
|---|
| 189 |
new->segment_size = segment_size; |
|---|
| 190 |
new->counter = counter; |
|---|
| 191 |
Py_XINCREF(counter); |
|---|
| 192 |
#ifdef PCT_RC5_MODULE |
|---|
| 193 |
new->st.version = version; |
|---|
| 194 |
new->st.word_size = word_size; |
|---|
| 195 |
new->st.rounds = rounds; |
|---|
| 196 |
#endif |
|---|
| 197 |
|
|---|
| 198 |
block_init(&(new->st), key, keylen); |
|---|
| 199 |
if (PyErr_Occurred()) |
|---|
| 200 |
{ |
|---|
| 201 |
Py_DECREF(new); |
|---|
| 202 |
return NULL; |
|---|
| 203 |
} |
|---|
| 204 |
memset(new->IV, 0, BLOCK_SIZE); |
|---|
| 205 |
memset(new->oldCipher, 0, BLOCK_SIZE); |
|---|
| 206 |
memcpy(new->IV, IV, IVlen); |
|---|
| 207 |
new->mode = mode; |
|---|
| 208 |
new->count=8; |
|---|
| 209 |
return new; |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
static char ALG_Encrypt__doc__[] = |
|---|
| 213 |
"Encrypt the provided string of binary data."; |
|---|
| 214 |
|
|---|
| 215 |
static PyObject * |
|---|
| 216 |
ALG_Encrypt(ALGobject *self, PyObject *args) |
|---|
| 217 |
{ |
|---|
| 218 |
unsigned char *buffer, *str; |
|---|
| 219 |
unsigned char temp[BLOCK_SIZE]; |
|---|
| 220 |
int i, j, len; |
|---|
| 221 |
PyObject *result; |
|---|
| 222 |
|
|---|
| 223 |
if (!PyArg_Parse(args, "s#", &str, &len)) |
|---|
| 224 |
return NULL; |
|---|
| 225 |
if (len==0) |
|---|
| 226 |
{ |
|---|
| 227 |
return PyString_FromStringAndSize(NULL, 0); |
|---|
| 228 |
} |
|---|
| 229 |
if ( (len % BLOCK_SIZE) !=0 && |
|---|
| 230 |
(self->mode!=MODE_CFB) && (self->mode!=MODE_PGP)) |
|---|
| 231 |
{ |
|---|
| 232 |
PyErr_Format(PyExc_ValueError, |
|---|
| 233 |
"Input strings must be " |
|---|
| 234 |
"a multiple of %i in length", |
|---|
| 235 |
BLOCK_SIZE); |
|---|
| 236 |
return NULL; |
|---|
| 237 |
} |
|---|
| 238 |
if (self->mode == MODE_CFB && |
|---|
| 239 |
(len % (self->segment_size/8) !=0)) { |
|---|
| 240 |
PyErr_Format(PyExc_ValueError, |
|---|
| 241 |
"Input strings must be a multiple of " |
|---|
| 242 |
"the segment size %i in length", |
|---|
| 243 |
self->segment_size/8); |
|---|
| 244 |
return NULL; |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
buffer=malloc(len); |
|---|
| 248 |
if (buffer==NULL) |
|---|
| 249 |
{ |
|---|
| 250 |
PyErr_SetString(PyExc_MemoryError, |
|---|
| 251 |
"No memory available in " |
|---|
| 252 |
_MODULE_STRING " encrypt"); |
|---|
| 253 |
return NULL; |
|---|
| 254 |
} |
|---|
| 255 |
switch(self->mode) |
|---|
| 256 |
{ |
|---|
| 257 |
case(MODE_ECB): |
|---|
| 258 |
for(i=0; i<len; i+=BLOCK_SIZE) |
|---|
| 259 |
{ |
|---|
| 260 |
block_encrypt(&(self->st), str+i, buffer+i); |
|---|
| 261 |
} |
|---|
| 262 |
break; |
|---|
| 263 |
|
|---|
| 264 |
case(MODE_CBC): |
|---|
| 265 |
for(i=0; i<len; i+=BLOCK_SIZE) |
|---|
| 266 |
{ |
|---|
| 267 |
for(j=0; j<BLOCK_SIZE; j++) |
|---|
| 268 |
{ |
|---|
| 269 |
temp[j]=str[i+j]^self->IV[j]; |
|---|
| 270 |
} |
|---|
| 271 |
block_encrypt(&(self->st), temp, buffer+i); |
|---|
| 272 |
memcpy(self->IV, buffer+i, BLOCK_SIZE); |
|---|
| 273 |
} |
|---|
| 274 |
break; |
|---|
| 275 |
|
|---|
| 276 |
case(MODE_CFB): |
|---|
| 277 |
for(i=0; i<len; i+=self->segment_size/8) |
|---|
| 278 |
{ |
|---|
| 279 |
block_encrypt(&(self->st), self->IV, temp); |
|---|
| 280 |
for (j=0; j<self->segment_size/8; j++) { |
|---|
| 281 |
buffer[i+j] = str[i+j] ^ temp[j]; |
|---|
| 282 |
} |
|---|
| 283 |
if (self->segment_size == BLOCK_SIZE * 8) { |
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
memcpy(self->IV, buffer + i, BLOCK_SIZE); |
|---|
| 287 |
} |
|---|
| 288 |
else if ((self->segment_size % 8) == 0) { |
|---|
| 289 |
int sz = self->segment_size/8; |
|---|
| 290 |
memmove(self->IV, self->IV + sz, |
|---|
| 291 |
BLOCK_SIZE-sz); |
|---|
| 292 |
memcpy(self->IV + BLOCK_SIZE - sz, buffer + i, |
|---|
| 293 |
sz); |
|---|
| 294 |
} |
|---|
| 295 |
else { |
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
} |
|---|
| 299 |
} |
|---|
| 300 |
break; |
|---|
| 301 |
|
|---|
| 302 |
case(MODE_PGP): |
|---|
| 303 |
if (len<=BLOCK_SIZE-self->count) |
|---|
| 304 |
{ |
|---|
| 305 |
|
|---|
| 306 |
for(i=0; i<len; i++) |
|---|
| 307 |
buffer[i] = self->IV[self->count+i] ^= str[i]; |
|---|
| 308 |
self->count += len; |
|---|
| 309 |
} |
|---|
| 310 |
else |
|---|
| 311 |
{ |
|---|
| 312 |
int j; |
|---|
| 313 |
for(i=0; i<BLOCK_SIZE-self->count; i++) |
|---|
| 314 |
buffer[i] = self->IV[self->count+i] ^= str[i]; |
|---|
| 315 |
self->count=0; |
|---|
| 316 |
for(; i<len-BLOCK_SIZE; i+=BLOCK_SIZE) |
|---|
| 317 |
{ |
|---|
| 318 |
block_encrypt(&(self->st), self->oldCipher, |
|---|
| 319 |
self->IV); |
|---|
| 320 |
for(j=0; j<BLOCK_SIZE; j++) |
|---|
| 321 |
buffer[i+j] = self->IV[j] ^= str[i+j]; |
|---|
| 322 |
} |
|---|
| 323 |
|
|---|
| 324 |
block_encrypt(&(self->st), self->oldCipher, self->IV); |
|---|
| 325 |
self->count=len-i; |
|---|
| 326 |
for(j=0; j<len-i; j++) |
|---|
| 327 |
{ |
|---|
| 328 |
buffer[i+j] = self->IV[j] ^= str[i+j]; |
|---|
| 329 |
} |
|---|
| 330 |
} |
|---|
| 331 |
break; |
|---|
| 332 |
|
|---|
| 333 |
case(MODE_OFB): |
|---|
| 334 |
for(i=0; i<len; i+=BLOCK_SIZE) |
|---|
| 335 |
{ |
|---|
| 336 |
block_encrypt(&(self->st), self->IV, temp); |
|---|
| 337 |
memcpy(self->IV, temp, BLOCK_SIZE); |
|---|
| 338 |
for(j=0; j<BLOCK_SIZE; j++) |
|---|
| 339 |
{ |
|---|
| 340 |
buffer[i+j] = str[i+j] ^ temp[j]; |
|---|
| 341 |
} |
|---|
| 342 |
} |
|---|
| 343 |
break; |
|---|
| 344 |
|
|---|
| 345 |
case(MODE_CTR): |
|---|
| 346 |
for(i=0; i<len; i+=BLOCK_SIZE) |
|---|
| 347 |
{ |
|---|
| 348 |
PyObject *ctr = PyObject_CallObject(self->counter, NULL); |
|---|
| 349 |
if (ctr == NULL) { |
|---|
| 350 |
free(buffer); |
|---|
| 351 |
return NULL; |
|---|
| 352 |
} |
|---|
| 353 |
if (!PyString_Check(ctr)) |
|---|
| 354 |
{ |
|---|
| 355 |
PyErr_SetString(PyExc_TypeError, |
|---|
| 356 |
"CTR counter function didn't return a string"); |
|---|
| 357 |
Py_DECREF(ctr); |
|---|
| 358 |
free(buffer); |
|---|
| 359 |
return NULL; |
|---|
| 360 |
} |
|---|
| 361 |
if (PyString_Size(ctr) != BLOCK_SIZE) { |
|---|
| 362 |
PyErr_Format(PyExc_TypeError, |
|---|
| 363 |
"CTR counter function returned " |
|---|
| 364 |
"string not of length %i", |
|---|
| 365 |
BLOCK_SIZE); |
|---|
| 366 |
Py_DECREF(ctr); |
|---|
| 367 |
free(buffer); |
|---|
| 368 |
return NULL; |
|---|
| 369 |
} |
|---|
| 370 |
block_encrypt(&(self->st), PyString_AsString(ctr), |
|---|
| 371 |
temp); |
|---|
| 372 |
Py_DECREF(ctr); |
|---|
| 373 |
for(j=0; j<BLOCK_SIZE; j++) |
|---|
| 374 |
{ |
|---|
| 375 |
buffer[i+j] = str[i+j]^temp[j]; |
|---|
| 376 |
} |
|---|
| 377 |
} |
|---|
| 378 |
break; |
|---|
| 379 |
|
|---|
| 380 |
default: |
|---|
| 381 |
PyErr_Format(PyExc_SystemError, |
|---|
| 382 |
"Unknown ciphertext feedback mode %i; " |
|---|
| 383 |
"this shouldn't happen", |
|---|
| 384 |
self->mode); |
|---|
| 385 |
free(buffer); |
|---|
| 386 |
return NULL; |
|---|
| 387 |
} |
|---|
| 388 |
result=PyString_FromStringAndSize(buffer, len); |
|---|
| 389 |
free(buffer); |
|---|
| 390 |
return(result); |
|---|
| 391 |
} |
|---|
| 392 |
|
|---|
| 393 |
static char ALG_Decrypt__doc__[] = |
|---|
| 394 |
"decrypt(string): Decrypt the provided string of binary data."; |
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
static PyObject * |
|---|
| 398 |
ALG_Decrypt(ALGobject *self, PyObject *args) |
|---|
| 399 |
{ |
|---|
| 400 |
unsigned char *buffer, *str; |
|---|
| 401 |
unsigned char temp[BLOCK_SIZE]; |
|---|
| 402 |
int i, j, len; |
|---|
| 403 |
PyObject *result; |
|---|
| 404 |
|
|---|
| 405 |
if (!PyArg_Parse(args, "s#", &str, &len)) |
|---|
| 406 |
return NULL; |
|---|
| 407 |
if (len==0) |
|---|
| 408 |
{ |
|---|
| 409 |
return PyString_FromStringAndSize(NULL, 0); |
|---|
| 410 |
} |
|---|
| 411 |
if ( (len % BLOCK_SIZE) !=0 && |
|---|
| 412 |
(self->mode!=MODE_CFB && self->mode!=MODE_PGP)) |
|---|
| 413 |
{ |
|---|
| 414 |
PyErr_Format(PyExc_ValueError, |
|---|
| 415 |
"Input strings must be " |
|---|
| 416 |
"a multiple of %i in length", |
|---|
| 417 |
BLOCK_SIZE); |
|---|
| 418 |
return NULL; |
|---|
| 419 |
} |
|---|
| 420 |
if (self->mode == MODE_CFB && |
|---|
| 421 |
(len % (self->segment_size/8) !=0)) { |
|---|
| 422 |
PyErr_Format(PyExc_ValueError, |
|---|
| 423 |
"Input strings must be a multiple of " |
|---|
| 424 |
"the segment size %i in length", |
|---|
| 425 |
self->segment_size/8); |
|---|
| 426 |
return NULL; |
|---|
| 427 |
} |
|---|
| 428 |
buffer=malloc(len); |
|---|
| 429 |
if (buffer==NULL) |
|---|
| 430 |
{ |
|---|
| 431 |
PyErr_SetString(PyExc_MemoryError, |
|---|
| 432 |
"No memory available in " _MODULE_STRING |
|---|
| 433 |
" decrypt"); |
|---|
| 434 |
return NULL; |
|---|
| 435 |
} |
|---|
| 436 |
switch(self->mode) |
|---|
| 437 |
{ |
|---|
| 438 |
case(MODE_ECB): |
|---|
| 439 |
for(i=0; i<len; i+=BLOCK_SIZE) |
|---|
| 440 |
{ |
|---|
| 441 |
block_decrypt(&(self->st), str+i, buffer+i); |
|---|
| 442 |
} |
|---|
| 443 |
break; |
|---|
| 444 |
|
|---|
| 445 |
case(MODE_CBC): |
|---|
| 446 |
for(i=0; i<len; i+=BLOCK_SIZE) |
|---|
| 447 |
{ |
|---|
| 448 |
memcpy(self->oldCipher, self->IV, BLOCK_SIZE); |
|---|
| 449 |
block_decrypt(&(self->st), str+i, temp); |
|---|
| 450 |
for(j=0; j<BLOCK_SIZE; j++) |
|---|
| 451 |
{ |
|---|
| 452 |
buffer[i+j]=temp[j]^self->IV[j]; |
|---|
| 453 |
self->IV[j]=str[i+j]; |
|---|
| 454 |
} |
|---|
| 455 |
} |
|---|
| 456 |
break; |
|---|
| 457 |
|
|---|
| 458 |
case(MODE_CFB): |
|---|
| 459 |
for(i=0; i<len; i+=self->segment_size/8) |
|---|
| 460 |
{ |
|---|
| 461 |
block_encrypt(&(self->st), self->IV, temp); |
|---|
| 462 |
for (j=0; j<self->segment_size/8; j++) { |
|---|
| 463 |
buffer[i+j] = str[i+j]^temp[j]; |
|---|
| 464 |
} |
|---|
| 465 |
if (self->segment_size == BLOCK_SIZE * 8) { |
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
memcpy(self->IV, str + i, BLOCK_SIZE); |
|---|
| 469 |
} |
|---|
| 470 |
else if ((self->segment_size % 8) == 0) { |
|---|
| 471 |
int sz = self->segment_size/8; |
|---|
| 472 |
memmove(self->IV, self->IV + sz, |
|---|
| 473 |
BLOCK_SIZE-sz); |
|---|
| 474 |
memcpy(self->IV + BLOCK_SIZE - sz, str + i, |
|---|
| 475 |
sz); |
|---|
| 476 |
} |
|---|
| 477 |
else { |
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 |
} |
|---|
| 481 |
} |
|---|
| 482 |
break; |
|---|
| 483 |
|
|---|
| 484 |
case(MODE_PGP): |
|---|
| 485 |
if (len<=BLOCK_SIZE-self->count) |
|---|
| 486 |
{ |
|---|
| 487 |
|
|---|
| 488 |
unsigned char t; |
|---|
| 489 |
for(i=0; i<len; i++) |
|---|
| 490 |
{ |
|---|
| 491 |
t=self->IV[self->count+i]; |
|---|
| 492 |
buffer[i] = t ^ (self->IV[self->count+i] = str[i]); |
|---|
| 493 |
} |
|---|
| 494 |
self->count += len; |
|---|
| 495 |
} |
|---|
| 496 |
else |
|---|
| 497 |
{ |
|---|
| 498 |
int j; |
|---|
| 499 |
unsigned char t; |
|---|
| 500 |
for(i=0; i<BLOCK_SIZE-self->count; i++) |
|---|
| 501 |
{ |
|---|
| 502 |
t=self->IV[self->count+i]; |
|---|
| 503 |
buffer[i] = t ^ (self->IV[self->count+i] = str[i]); |
|---|
| 504 |
} |
|---|
| 505 |
self->count=0; |
|---|
| 506 |
for(; i<len-BLOCK_SIZE; i+=BLOCK_SIZE) |
|---|
| 507 |
{ |
|---|
| 508 |
block_encrypt(&(self->st), self->oldCipher, self->IV); |
|---|
| 509 |
for(j=0; j<BLOCK_SIZE; j++) |
|---|
| 510 |
{ |
|---|
| 511 |
t=self->IV[j]; |
|---|
| 512 |
buffer[i+j] = t ^ (self->IV[j] = str[i+j]); |
|---|
| 513 |
} |
|---|
| 514 |
} |
|---|
| 515 |
|
|---|
| 516 |
block_encrypt(&(self->st), self->oldCipher, self->IV); |
|---|
| 517 |
self->count=len-i; |
|---|
| 518 |
for(j=0; j<len-i; j++) |
|---|
| 519 |
{ |
|---|
| 520 |
t=self->IV[j]; |
|---|
| 521 |
buffer[i+j] = t ^ (self->IV[j] = str[i+j]); |
|---|
| 522 |
} |
|---|
| 523 |
} |
|---|
| 524 |
break; |
|---|
| 525 |
|
|---|
| 526 |
case (MODE_OFB): |
|---|
| 527 |
for(i=0; i<len; i+=BLOCK_SIZE) |
|---|
| 528 |
{ |
|---|
| 529 |
block_encrypt(&(self->st), self->IV, temp); |
|---|
| 530 |
memcpy(self->IV, temp, BLOCK_SIZE); |
|---|
| 531 |
for(j=0; j<BLOCK_SIZE; j++) |
|---|
| 532 |
{ |
|---|
| 533 |
buffer[i+j] = str[i+j] ^ self->IV[j]; |
|---|
| 534 |
} |
|---|
| 535 |
} |
|---|
| 536 |
break; |
|---|
| 537 |
|
|---|
| 538 |
case (MODE_CTR): |
|---|
| 539 |
for(i=0; i<len; i+=BLOCK_SIZE) |
|---|
| 540 |
{ |
|---|
| 541 |
PyObject *ctr = PyObject_CallObject(self->counter, NULL); |
|---|
| 542 |
if (ctr == NULL) { |
|---|
| 543 |
free(buffer); |
|---|
| 544 |
return NULL; |
|---|
| 545 |
} |
|---|
| 546 |
if (!PyString_Check(ctr)) |
|---|
| 547 |
{ |
|---|
| 548 |
PyErr_SetString(PyExc_TypeError, |
|---|
| 549 |
"CTR counter function didn't return a string"); |
|---|
| 550 |
Py_DECREF(ctr); |
|---|
| 551 |
free(buffer); |
|---|
| 552 |
return NULL; |
|---|
| 553 |
} |
|---|
| 554 |
if (PyString_Size(ctr) != BLOCK_SIZE) { |
|---|
| 555 |
PyErr_SetString(PyExc_TypeError, |
|---|
| 556 |
"CTR counter function returned string of incorrect length"); |
|---|
| 557 |
Py_DECREF(ctr); |
|---|
| 558 |
free(buffer); |
|---|
| 559 |
return NULL; |
|---|
| 560 |
} |
|---|
| 561 |
block_encrypt(&(self->st), PyString_AsString(ctr), temp); |
|---|
| 562 |
Py_DECREF(ctr); |
|---|
| 563 |
for(j=0; j<BLOCK_SIZE; j++) |
|---|
| 564 |
{ |
|---|
| 565 |
buffer[i+j] = str[i+j]^temp[j]; |
|---|
| 566 |
} |
|---|
| 567 |
} |
|---|
| 568 |
break; |
|---|
| 569 |
|
|---|
| 570 |
default: |
|---|
| 571 |
PyErr_Format(PyExc_SystemError, |
|---|
| 572 |
"Unknown ciphertext feedback mode %i; " |
|---|
| 573 |
"this shouldn't happen", |
|---|
| 574 |
self->mode); |
|---|
| 575 |
free(buffer); |
|---|
| 576 |
return NULL; |
|---|
| 577 |
} |
|---|
| 578 |
result=PyString_FromStringAndSize(buffer, len); |
|---|
| 579 |
free(buffer); |
|---|
| 580 |
return(result); |
|---|
| 581 |
} |
|---|
| 582 |
|
|---|
| 583 |
static char ALG_Sync__doc__[] = |
|---|
| 584 |
"sync(): For objects using the PGP feedback mode, this method modifies " |
|---|
| 585 |
"the IV, synchronizing it with the preceding ciphertext."; |
|---|
| 586 |
|
|---|
| 587 |
static PyObject * |
|---|
| 588 |
ALG_Sync(ALGobject *self, PyObject *args) |
|---|
| 589 |
{ |
|---|
| 590 |
if (!PyArg_ParseTuple(args, "")) { |
|---|
| 591 |
return NULL; |
|---|
| 592 |
} |
|---|
| 593 |
|
|---|
| 594 |
if (self->mode!=MODE_PGP) |
|---|
| 595 |
{ |
|---|
| 596 |
PyErr_SetString(PyExc_SystemError, "sync() operation not defined for " |
|---|
| 597 |
"this feedback mode"); |
|---|
| 598 |
return NULL; |
|---|
| 599 |
} |
|---|
| 600 |
|
|---|
| 601 |
if (self->count!=8) |
|---|
| 602 |
{ |
|---|
| 603 |
memmove(self->IV+BLOCK_SIZE-self->count, self->IV, |
|---|
| 604 |
self->count); |
|---|
| 605 |
memcpy(self->IV, self->oldCipher+self->count, |
|---|
| 606 |
BLOCK_SIZE-self->count); |
|---|
| 607 |
self->count=8; |
|---|
| 608 |
} |
|---|
| 609 |
Py_INCREF(Py_None); |
|---|
| 610 |
return Py_None; |
|---|
| 611 |
} |
|---|
| 612 |
|
|---|
| 613 |
#if 0 |
|---|
| 614 |
void PrintState(self, msg) |
|---|
| 615 |
ALGobject *self; |
|---|
| 616 |
char * msg; |
|---|
| 617 |
{ |
|---|
| 618 |
int count; |
|---|
| 619 |
|
|---|
| 620 |
printf("%sing: %i IV ", msg, (int)self->count); |
|---|
| 621 |
for(count=0; count<8; count++) printf("%i ", self->IV[count]); |
|---|
| 622 |
printf("\noldCipher:"); |
|---|
| 623 |
for(count=0; count<8; count++) printf("%i ", self->oldCipher[count]); |
|---|
| 624 |
printf("\n"); |
|---|
| 625 |
} |
|---|
| 626 |
#endif |
|---|
| 627 |
|
|---|
| 628 |
|
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
static PyMethodDef ALGmethods[] = |
|---|
| 632 |
{ |
|---|
| 633 |
{"encrypt", (PyCFunction) ALG_Encrypt, 0, ALG_Encrypt__doc__}, |
|---|
| 634 |
{"decrypt", (PyCFunction) ALG_Decrypt, 0, ALG_Decrypt__doc__}, |
|---|
| 635 |
{"sync", (PyCFunction) ALG_Sync, METH_VARARGS, ALG_Sync__doc__}, |
|---|
| 636 |
{NULL, NULL} |
|---|
| 637 |
}; |
|---|
| 638 |
|
|---|
| 639 |
|
|---|
| 640 |
static int |
|---|
| 641 |
ALGsetattr(PyObject *ptr, char *name, PyObject *v) |
|---|
| 642 |
{ |
|---|
| 643 |
ALGobject *self=(ALGobject *)ptr; |
|---|
| 644 |
if (strcmp(name, "IV") != 0) |
|---|
| 645 |
{ |
|---|
| 646 |
PyErr_Format(PyExc_AttributeError, |
|---|
| 647 |
"non-existent block cipher object attribute '%s'", |
|---|
| 648 |
name); |
|---|
| 649 |
return -1; |
|---|
| 650 |
} |
|---|
| 651 |
if (v==NULL) |
|---|
| 652 |
{ |
|---|
| 653 |
PyErr_SetString(PyExc_AttributeError, |
|---|
| 654 |
"Can't delete IV attribute of block cipher object"); |
|---|
| 655 |
return -1; |
|---|
| 656 |
} |
|---|
| 657 |
if (!PyString_Check(v)) |
|---|
| 658 |
{ |
|---|
| 659 |
PyErr_SetString(PyExc_TypeError, |
|---|
| 660 |
"IV attribute of block cipher object must be string"); |
|---|
| 661 |
return -1; |
|---|
| 662 |
} |
|---|
| 663 |
if (PyString_Size(v)!=BLOCK_SIZE) |
|---|
| 664 |
{ |
|---|
| 665 |
PyErr_Format(PyExc_ValueError, |
|---|
| 666 |
_MODULE_STRING " IV must be %i bytes long", |
|---|
| 667 |
BLOCK_SIZE); |
|---|
| 668 |
return -1; |
|---|
| 669 |
} |
|---|
| 670 |
memcpy(self->IV, PyString_AsString(v), BLOCK_SIZE); |
|---|
| 671 |
return 0; |
|---|
| 672 |
} |
|---|
| 673 |
|
|---|
| 674 |
static PyObject * |
|---|
| 675 |
ALGgetattr(PyObject *s, char *name) |
|---|
| 676 |
{ |
|---|
| 677 |
ALGobject *self = (ALGobject*)s; |
|---|
| 678 |
if (strcmp(name, "IV") == 0) |
|---|
| 679 |
{ |
|---|
| 680 |
return(PyString_FromStringAndSize(self->IV, BLOCK_SIZE)); |
|---|
| 681 |
} |
|---|
| 682 |
if (strcmp(name, "mode") == 0) |
|---|
| 683 |
{ |
|---|
| 684 |
return(PyInt_FromLong((long)(self->mode))); |
|---|
| 685 |
} |
|---|
| 686 |
if (strcmp(name, "block_size") == 0) |
|---|
| 687 |
{ |
|---|
| 688 |
return PyInt_FromLong(BLOCK_SIZE); |
|---|
| 689 |
} |
|---|
| 690 |
if (strcmp(name, "key_size") == 0) |
|---|
| 691 |
{ |
|---|
| 692 |
return PyInt_FromLong(KEY_SIZE); |
|---|
| 693 |
} |
|---|
| 694 |
return Py_FindMethod(ALGmethods, (PyObject *) self, name); |
|---|
| 695 |
} |
|---|
| 696 |
|
|---|
| 697 |
|
|---|
| 698 |
|
|---|
| 699 |
static struct PyMethodDef modulemethods[] = |
|---|
| 700 |
{ |
|---|
| 701 |
{"new", (PyCFunction) ALGnew, METH_VARARGS|METH_KEYWORDS, ALGnew__doc__}, |
|---|
| 702 |
{NULL, NULL} |
|---|
| 703 |
}; |
|---|
| 704 |
|
|---|
| 705 |
static PyTypeObject ALGtype = |
|---|
| 706 |
{ |
|---|
| 707 |
PyObject_HEAD_INIT(NULL) |
|---|
| 708 |
0, |
|---|
| 709 |
_MODULE_STRING, |
|---|
| 710 |
sizeof(ALGobject), |
|---|
| 711 |
0, |
|---|
| 712 |
|
|---|
| 713 |
ALGdealloc, |
|---|
| 714 |
0, |
|---|
| 715 |
ALGgetattr, |
|---|
| 716 |
ALGsetattr, |
|---|
| 717 |
0, |
|---|
| 718 |
(reprfunc) 0, |
|---|
| 719 |
0, |
|---|
| 720 |
}; |
|---|
| 721 |
|
|---|
| 722 |
|
|---|
| 723 |
|
|---|
| 724 |
#if PYTHON_API_VERSION < 1011 |
|---|
| 725 |
#define PyModule_AddIntConstant(m,n,v) {PyObject *o=PyInt_FromLong(v); \ |
|---|
| 726 |
if (o!=NULL) \ |
|---|
| 727 |
{PyDict_SetItemString(PyModule_GetDict(m),n,o); Py_DECREF(o);}} |
|---|
| 728 |
#endif |
|---|
| 729 |
|
|---|
| 730 |
void |
|---|
| 731 |
_MODULE_NAME (void) |
|---|
| 732 |
{ |
|---|
| 733 |
PyObject *m; |
|---|
| 734 |
|
|---|
| 735 |
ALGtype.ob_type = &PyType_Type; |
|---|
| 736 |
|
|---|
| 737 |
|
|---|
| 738 |
m = Py_InitModule("Crypto.Cipher." _MODULE_STRING, modulemethods); |
|---|
| 739 |
|
|---|
| 740 |
PyModule_AddIntConstant(m, "MODE_ECB", MODE_ECB); |
|---|
| 741 |
PyModule_AddIntConstant(m, "MODE_CBC", MODE_CBC); |
|---|
| 742 |
PyModule_AddIntConstant(m, "MODE_CFB", MODE_CFB); |
|---|
| 743 |
PyModule_AddIntConstant(m, "MODE_PGP", MODE_PGP); |
|---|
| 744 |
PyModule_AddIntConstant(m, "MODE_OFB", MODE_OFB); |
|---|
| 745 |
PyModule_AddIntConstant(m, "MODE_CTR", MODE_CTR); |
|---|
| 746 |
PyModule_AddIntConstant(m, "block_size", BLOCK_SIZE); |
|---|
| 747 |
PyModule_AddIntConstant(m, "key_size", KEY_SIZE); |
|---|
| 748 |
|
|---|
| 749 |
|
|---|
| 750 |
if (PyErr_Occurred()) |
|---|
| 751 |
Py_FatalError("can't initialize module " _MODULE_STRING); |
|---|
| 752 |
} |
|---|
| 753 |
|
|---|