Lines 19-25
Link Here
|
19 |
/* EVP is the preferred interface to hashing in OpenSSL */ |
19 |
/* EVP is the preferred interface to hashing in OpenSSL */ |
20 |
#include <openssl/evp.h> |
20 |
#include <openssl/evp.h> |
21 |
|
21 |
|
|
|
22 |
#define MUNCH_SIZE INT_MAX |
22 |
|
23 |
|
|
|
24 |
|
23 |
#ifndef HASH_OBJ_CONSTRUCTOR |
25 |
#ifndef HASH_OBJ_CONSTRUCTOR |
24 |
#define HASH_OBJ_CONSTRUCTOR 0 |
26 |
#define HASH_OBJ_CONSTRUCTOR 0 |
25 |
#endif |
27 |
#endif |
Lines 164-172
Link Here
|
164 |
if (!PyArg_ParseTuple(args, "s#:update", &cp, &len)) |
166 |
if (!PyArg_ParseTuple(args, "s#:update", &cp, &len)) |
165 |
return NULL; |
167 |
return NULL; |
166 |
|
168 |
|
|
|
169 |
if (len > 0 && len <= MUNCH_SIZE) { |
167 |
EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t, |
170 |
EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t, |
168 |
unsigned int)); |
171 |
unsigned int)); |
169 |
|
172 |
} else { |
|
|
173 |
Py_ssize_t offset = 0; |
174 |
while (len) { |
175 |
unsigned int process = len > MUNCH_SIZE ? MUNCH_SIZE : len; |
176 |
EVP_DigestUpdate(&self->ctx, cp + offset, process); |
177 |
len -= process; |
178 |
offset += process; |
179 |
} |
180 |
} |
170 |
Py_INCREF(Py_None); |
181 |
Py_INCREF(Py_None); |
171 |
return Py_None; |
182 |
return Py_None; |
172 |
} |
183 |
} |
Lines 255-264
Link Here
|
255 |
self->name = name_obj; |
266 |
self->name = name_obj; |
256 |
Py_INCREF(self->name); |
267 |
Py_INCREF(self->name); |
257 |
|
268 |
|
258 |
if (cp && len) |
269 |
if (cp && len) { |
|
|
270 |
if (len > 0 && len <= MUNCH_SIZE) { |
259 |
EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t, |
271 |
EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t, |
260 |
unsigned int)); |
272 |
unsigned int)); |
261 |
|
273 |
} else { |
|
|
274 |
Py_ssize_t offset = 0; |
275 |
while (len) { |
276 |
unsigned int process = len > MUNCH_SIZE ? MUNCH_SIZE : len; |
277 |
EVP_DigestUpdate(&self->ctx, cp + offset, process); |
278 |
len -= process; |
279 |
offset += process; |
280 |
} |
281 |
} |
282 |
} |
283 |
|
262 |
return 0; |
284 |
return 0; |
263 |
} |
285 |
} |
264 |
#endif |
286 |
#endif |
Lines 328-334
Link Here
|
328 |
static PyObject * |
350 |
static PyObject * |
329 |
EVPnew(PyObject *name_obj, |
351 |
EVPnew(PyObject *name_obj, |
330 |
const EVP_MD *digest, const EVP_MD_CTX *initial_ctx, |
352 |
const EVP_MD *digest, const EVP_MD_CTX *initial_ctx, |
331 |
const unsigned char *cp, unsigned int len) |
353 |
const unsigned char *cp, Py_ssize_t len) |
332 |
{ |
354 |
{ |
333 |
EVPobject *self; |
355 |
EVPobject *self; |
334 |
|
356 |
|
Lines 346-353
Link Here
|
346 |
EVP_DigestInit(&self->ctx, digest); |
368 |
EVP_DigestInit(&self->ctx, digest); |
347 |
} |
369 |
} |
348 |
|
370 |
|
349 |
if (cp && len) |
371 |
if (cp && len) { |
350 |
EVP_DigestUpdate(&self->ctx, cp, len); |
372 |
if (len > 0 && len <= MUNCH_SIZE) { |
|
|
373 |
EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t, |
374 |
unsigned int)); |
375 |
} else { |
376 |
Py_ssize_t offset = 0; |
377 |
while (len) { |
378 |
unsigned int process = len > MUNCH_SIZE ? MUNCH_SIZE : len; |
379 |
EVP_DigestUpdate(&self->ctx, cp + offset, process); |
380 |
len -= process; |
381 |
offset += process; |
382 |
} |
383 |
} |
384 |
} |
351 |
|
385 |
|
352 |
return (PyObject *)self; |
386 |
return (PyObject *)self; |
353 |
} |
387 |
} |
Lines 384-391
Link Here
|
384 |
|
418 |
|
385 |
digest = EVP_get_digestbyname(name); |
419 |
digest = EVP_get_digestbyname(name); |
386 |
|
420 |
|
387 |
return EVPnew(name_obj, digest, NULL, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t, |
421 |
return EVPnew(name_obj, digest, NULL, cp, len); |
388 |
unsigned int)); |
|
|
389 |
} |
422 |
} |
390 |
|
423 |
|
391 |
/* |
424 |
/* |
Lines 410-416
Link Here
|
410 |
CONST_ ## NAME ## _name_obj, \ |
443 |
CONST_ ## NAME ## _name_obj, \ |
411 |
NULL, \ |
444 |
NULL, \ |
412 |
CONST_new_ ## NAME ## _ctx_p, \ |
445 |
CONST_new_ ## NAME ## _ctx_p, \ |
413 |
cp, Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int)); \ |
446 |
cp, len); \ |
414 |
} |
447 |
} |
415 |
|
448 |
|
416 |
/* a PyMethodDef structure for the constructor */ |
449 |
/* a PyMethodDef structure for the constructor */ |