Fix the transient refleaks in test_zipimport_support.
[python.git] / Modules / _hashopenssl.c
blobba18aa27edec9d622548664c22102865d8f8ae94
1 /* Module that wraps all OpenSSL hash algorithms */
3 /*
4 * Copyright (C) 2005-2009 Gregory P. Smith (greg@krypto.org)
5 * Licensed to PSF under a Contributor Agreement.
7 * Derived from a skeleton of shamodule.c containing work performed by:
9 * Andrew Kuchling (amk@amk.ca)
10 * Greg Stein (gstein@lyra.org)
14 #define PY_SSIZE_T_CLEAN
16 #include "Python.h"
17 #include "structmember.h"
18 #include "hashlib.h"
20 #ifdef WITH_THREAD
21 #include "pythread.h"
22 #define ENTER_HASHLIB(obj) \
23 if ((obj)->lock) { \
24 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
25 Py_BEGIN_ALLOW_THREADS \
26 PyThread_acquire_lock((obj)->lock, 1); \
27 Py_END_ALLOW_THREADS \
28 } \
30 #define LEAVE_HASHLIB(obj) \
31 if ((obj)->lock) { \
32 PyThread_release_lock((obj)->lock); \
34 #else
35 #define ENTER_HASHLIB(obj)
36 #define LEAVE_HASHLIB(obj)
37 #endif
39 /* EVP is the preferred interface to hashing in OpenSSL */
40 #include <openssl/evp.h>
42 #define MUNCH_SIZE INT_MAX
44 /* TODO(gps): We should probably make this a module or EVPobject attribute
45 * to allow the user to optimize based on the platform they're using. */
46 #define HASHLIB_GIL_MINSIZE 2048
48 #ifndef HASH_OBJ_CONSTRUCTOR
49 #define HASH_OBJ_CONSTRUCTOR 0
50 #endif
53 typedef struct {
54 PyObject_HEAD
55 PyObject *name; /* name of this hash algorithm */
56 EVP_MD_CTX ctx; /* OpenSSL message digest context */
57 #ifdef WITH_THREAD
58 PyThread_type_lock lock; /* OpenSSL context lock */
59 #endif
60 } EVPobject;
63 static PyTypeObject EVPtype;
66 #define DEFINE_CONSTS_FOR_NEW(Name) \
67 static PyObject *CONST_ ## Name ## _name_obj; \
68 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
69 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
71 DEFINE_CONSTS_FOR_NEW(md5)
72 DEFINE_CONSTS_FOR_NEW(sha1)
73 DEFINE_CONSTS_FOR_NEW(sha224)
74 DEFINE_CONSTS_FOR_NEW(sha256)
75 DEFINE_CONSTS_FOR_NEW(sha384)
76 DEFINE_CONSTS_FOR_NEW(sha512)
79 static EVPobject *
80 newEVPobject(PyObject *name)
82 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
84 /* save the name for .name to return */
85 if (retval != NULL) {
86 Py_INCREF(name);
87 retval->name = name;
88 #ifdef WITH_THREAD
89 retval->lock = NULL;
90 #endif
93 return retval;
96 static void
97 EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
99 unsigned int process;
100 const unsigned char *cp = (const unsigned char *)vp;
101 while (0 < len)
103 if (len > (Py_ssize_t)MUNCH_SIZE)
104 process = MUNCH_SIZE;
105 else
106 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
107 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
108 len -= process;
109 cp += process;
113 /* Internal methods for a hash object */
115 static void
116 EVP_dealloc(EVPobject *self)
118 #ifdef WITH_THREAD
119 if (self->lock != NULL)
120 PyThread_free_lock(self->lock);
121 #endif
122 EVP_MD_CTX_cleanup(&self->ctx);
123 Py_XDECREF(self->name);
124 PyObject_Del(self);
127 static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
129 ENTER_HASHLIB(self);
130 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
131 LEAVE_HASHLIB(self);
134 /* External methods for a hash object */
136 PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
139 static PyObject *
140 EVP_copy(EVPobject *self, PyObject *unused)
142 EVPobject *newobj;
144 if ( (newobj = newEVPobject(self->name))==NULL)
145 return NULL;
147 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
148 return (PyObject *)newobj;
151 PyDoc_STRVAR(EVP_digest__doc__,
152 "Return the digest value as a string of binary data.");
154 static PyObject *
155 EVP_digest(EVPobject *self, PyObject *unused)
157 unsigned char digest[EVP_MAX_MD_SIZE];
158 EVP_MD_CTX temp_ctx;
159 PyObject *retval;
160 unsigned int digest_size;
162 locked_EVP_MD_CTX_copy(&temp_ctx, self);
163 digest_size = EVP_MD_CTX_size(&temp_ctx);
164 EVP_DigestFinal(&temp_ctx, digest, NULL);
166 retval = PyString_FromStringAndSize((const char *)digest, digest_size);
167 EVP_MD_CTX_cleanup(&temp_ctx);
168 return retval;
171 PyDoc_STRVAR(EVP_hexdigest__doc__,
172 "Return the digest value as a string of hexadecimal digits.");
174 static PyObject *
175 EVP_hexdigest(EVPobject *self, PyObject *unused)
177 unsigned char digest[EVP_MAX_MD_SIZE];
178 EVP_MD_CTX temp_ctx;
179 PyObject *retval;
180 char *hex_digest;
181 unsigned int i, j, digest_size;
183 /* Get the raw (binary) digest value */
184 locked_EVP_MD_CTX_copy(&temp_ctx, self);
185 digest_size = EVP_MD_CTX_size(&temp_ctx);
186 EVP_DigestFinal(&temp_ctx, digest, NULL);
188 EVP_MD_CTX_cleanup(&temp_ctx);
190 /* Create a new string */
191 /* NOTE: not thread safe! modifying an already created string object */
192 /* (not a problem because we hold the GIL by default) */
193 retval = PyString_FromStringAndSize(NULL, digest_size * 2);
194 if (!retval)
195 return NULL;
196 hex_digest = PyString_AsString(retval);
197 if (!hex_digest) {
198 Py_DECREF(retval);
199 return NULL;
202 /* Make hex version of the digest */
203 for(i=j=0; i<digest_size; i++) {
204 char c;
205 c = (digest[i] >> 4) & 0xf;
206 c = (c>9) ? c+'a'-10 : c + '0';
207 hex_digest[j++] = c;
208 c = (digest[i] & 0xf);
209 c = (c>9) ? c+'a'-10 : c + '0';
210 hex_digest[j++] = c;
212 return retval;
215 PyDoc_STRVAR(EVP_update__doc__,
216 "Update this hash object's state with the provided string.");
218 static PyObject *
219 EVP_update(EVPobject *self, PyObject *args)
221 PyObject *obj;
222 Py_buffer view;
224 if (!PyArg_ParseTuple(args, "O:update", &obj))
225 return NULL;
227 GET_BUFFER_VIEW_OR_ERROUT(obj, &view, NULL);
229 #ifdef WITH_THREAD
230 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
231 self->lock = PyThread_allocate_lock();
232 /* fail? lock = NULL and we fail over to non-threaded code. */
235 if (self->lock != NULL) {
236 Py_BEGIN_ALLOW_THREADS
237 PyThread_acquire_lock(self->lock, 1);
238 EVP_hash(self, view.buf, view.len);
239 PyThread_release_lock(self->lock);
240 Py_END_ALLOW_THREADS
241 } else {
242 EVP_hash(self, view.buf, view.len);
244 #else
245 EVP_hash(self, view.buf, view.len);
246 #endif
248 PyBuffer_Release(&view);
250 Py_INCREF(Py_None);
251 return Py_None;
254 static PyMethodDef EVP_methods[] = {
255 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
256 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
257 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
258 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
259 {NULL, NULL} /* sentinel */
262 static PyObject *
263 EVP_get_block_size(EVPobject *self, void *closure)
265 long block_size;
266 block_size = EVP_MD_CTX_block_size(&self->ctx);
267 return PyLong_FromLong(block_size);
270 static PyObject *
271 EVP_get_digest_size(EVPobject *self, void *closure)
273 long size;
274 size = EVP_MD_CTX_size(&self->ctx);
275 return PyLong_FromLong(size);
278 static PyMemberDef EVP_members[] = {
279 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
280 {NULL} /* Sentinel */
283 static PyGetSetDef EVP_getseters[] = {
284 {"digest_size",
285 (getter)EVP_get_digest_size, NULL,
286 NULL,
287 NULL},
288 {"block_size",
289 (getter)EVP_get_block_size, NULL,
290 NULL,
291 NULL},
292 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
293 * the old sha module also supported 'digestsize'. ugh. */
294 {"digestsize",
295 (getter)EVP_get_digest_size, NULL,
296 NULL,
297 NULL},
298 {NULL} /* Sentinel */
302 static PyObject *
303 EVP_repr(PyObject *self)
305 char buf[100];
306 PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
307 PyString_AsString(((EVPobject *)self)->name), self);
308 return PyString_FromString(buf);
311 #if HASH_OBJ_CONSTRUCTOR
312 static int
313 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
315 static char *kwlist[] = {"name", "string", NULL};
316 PyObject *name_obj = NULL;
317 PyObject *data_obj = NULL;
318 Py_buffer view;
319 char *nameStr;
320 const EVP_MD *digest;
322 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
323 &name_obj, &data_obj)) {
324 return -1;
327 if (data_obj)
328 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, -1);
330 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
331 PyErr_SetString(PyExc_TypeError, "name must be a string");
332 if (data_obj)
333 PyBuffer_Release(&view);
334 return -1;
337 digest = EVP_get_digestbyname(nameStr);
338 if (!digest) {
339 PyErr_SetString(PyExc_ValueError, "unknown hash function");
340 if (data_obj)
341 PyBuffer_Release(&view);
342 return -1;
344 EVP_DigestInit(&self->ctx, digest);
346 self->name = name_obj;
347 Py_INCREF(self->name);
349 if (data_obj) {
350 if (view.len >= HASHLIB_GIL_MINSIZE) {
351 Py_BEGIN_ALLOW_THREADS
352 EVP_hash(self, view.buf, view.len);
353 Py_END_ALLOW_THREADS
354 } else {
355 EVP_hash(self, view.buf, view.len);
357 PyBuffer_Release(&view);
360 return 0;
362 #endif
365 PyDoc_STRVAR(hashtype_doc,
366 "A hash represents the object used to calculate a checksum of a\n\
367 string of information.\n\
369 Methods:\n\
371 update() -- updates the current digest with an additional string\n\
372 digest() -- return the current digest value\n\
373 hexdigest() -- return the current digest as a string of hexadecimal digits\n\
374 copy() -- return a copy of the current hash object\n\
376 Attributes:\n\
378 name -- the hash algorithm being used by this object\n\
379 digest_size -- number of bytes in this hashes output\n");
381 static PyTypeObject EVPtype = {
382 PyVarObject_HEAD_INIT(NULL, 0)
383 "_hashlib.HASH", /*tp_name*/
384 sizeof(EVPobject), /*tp_basicsize*/
385 0, /*tp_itemsize*/
386 /* methods */
387 (destructor)EVP_dealloc, /*tp_dealloc*/
388 0, /*tp_print*/
389 0, /*tp_getattr*/
390 0, /*tp_setattr*/
391 0, /*tp_compare*/
392 EVP_repr, /*tp_repr*/
393 0, /*tp_as_number*/
394 0, /*tp_as_sequence*/
395 0, /*tp_as_mapping*/
396 0, /*tp_hash*/
397 0, /*tp_call*/
398 0, /*tp_str*/
399 0, /*tp_getattro*/
400 0, /*tp_setattro*/
401 0, /*tp_as_buffer*/
402 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
403 hashtype_doc, /*tp_doc*/
404 0, /*tp_traverse*/
405 0, /*tp_clear*/
406 0, /*tp_richcompare*/
407 0, /*tp_weaklistoffset*/
408 0, /*tp_iter*/
409 0, /*tp_iternext*/
410 EVP_methods, /* tp_methods */
411 EVP_members, /* tp_members */
412 EVP_getseters, /* tp_getset */
413 #if 1
414 0, /* tp_base */
415 0, /* tp_dict */
416 0, /* tp_descr_get */
417 0, /* tp_descr_set */
418 0, /* tp_dictoffset */
419 #endif
420 #if HASH_OBJ_CONSTRUCTOR
421 (initproc)EVP_tp_init, /* tp_init */
422 #endif
425 static PyObject *
426 EVPnew(PyObject *name_obj,
427 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
428 const unsigned char *cp, Py_ssize_t len)
430 EVPobject *self;
432 if (!digest && !initial_ctx) {
433 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
434 return NULL;
437 if ((self = newEVPobject(name_obj)) == NULL)
438 return NULL;
440 if (initial_ctx) {
441 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
442 } else {
443 EVP_DigestInit(&self->ctx, digest);
446 if (cp && len) {
447 if (len >= HASHLIB_GIL_MINSIZE) {
448 Py_BEGIN_ALLOW_THREADS
449 EVP_hash(self, cp, len);
450 Py_END_ALLOW_THREADS
451 } else {
452 EVP_hash(self, cp, len);
456 return (PyObject *)self;
460 /* The module-level function: new() */
462 PyDoc_STRVAR(EVP_new__doc__,
463 "Return a new hash object using the named algorithm.\n\
464 An optional string argument may be provided and will be\n\
465 automatically hashed.\n\
467 The MD5 and SHA1 algorithms are always supported.\n");
469 static PyObject *
470 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
472 static char *kwlist[] = {"name", "string", NULL};
473 PyObject *name_obj = NULL;
474 PyObject *data_obj = NULL;
475 Py_buffer view = { 0 };
476 PyObject *ret_obj;
477 char *name;
478 const EVP_MD *digest;
480 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
481 &name_obj, &data_obj)) {
482 return NULL;
485 if (!PyArg_Parse(name_obj, "s", &name)) {
486 PyErr_SetString(PyExc_TypeError, "name must be a string");
487 return NULL;
490 if (data_obj)
491 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
493 digest = EVP_get_digestbyname(name);
495 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
496 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
498 if (data_obj)
499 PyBuffer_Release(&view);
500 return ret_obj;
504 * This macro generates constructor function definitions for specific
505 * hash algorithms. These constructors are much faster than calling
506 * the generic one passing it a python string and are noticably
507 * faster than calling a python new() wrapper. Thats important for
508 * code that wants to make hashes of a bunch of small strings.
510 #define GEN_CONSTRUCTOR(NAME) \
511 static PyObject * \
512 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
514 PyObject *data_obj = NULL; \
515 Py_buffer view = { 0 }; \
516 PyObject *ret_obj; \
518 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
519 return NULL; \
522 if (data_obj) \
523 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL); \
525 ret_obj = EVPnew( \
526 CONST_ ## NAME ## _name_obj, \
527 NULL, \
528 CONST_new_ ## NAME ## _ctx_p, \
529 (unsigned char*)view.buf, \
530 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); \
532 if (data_obj) \
533 PyBuffer_Release(&view); \
534 return ret_obj; \
537 /* a PyMethodDef structure for the constructor */
538 #define CONSTRUCTOR_METH_DEF(NAME) \
539 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
540 PyDoc_STR("Returns a " #NAME \
541 " hash object; optionally initialized with a string") \
544 /* used in the init function to setup a constructor */
545 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
546 CONST_ ## NAME ## _name_obj = PyString_FromString(#NAME); \
547 if (EVP_get_digestbyname(#NAME)) { \
548 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
549 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
551 } while (0);
553 GEN_CONSTRUCTOR(md5)
554 GEN_CONSTRUCTOR(sha1)
555 GEN_CONSTRUCTOR(sha224)
556 GEN_CONSTRUCTOR(sha256)
557 GEN_CONSTRUCTOR(sha384)
558 GEN_CONSTRUCTOR(sha512)
560 /* List of functions exported by this module */
562 static struct PyMethodDef EVP_functions[] = {
563 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
564 CONSTRUCTOR_METH_DEF(md5),
565 CONSTRUCTOR_METH_DEF(sha1),
566 CONSTRUCTOR_METH_DEF(sha224),
567 CONSTRUCTOR_METH_DEF(sha256),
568 CONSTRUCTOR_METH_DEF(sha384),
569 CONSTRUCTOR_METH_DEF(sha512),
570 {NULL, NULL} /* Sentinel */
574 /* Initialize this module. */
576 PyMODINIT_FUNC
577 init_hashlib(void)
579 PyObject *m;
581 OpenSSL_add_all_digests();
583 /* TODO build EVP_functions openssl_* entries dynamically based
584 * on what hashes are supported rather than listing many
585 * but having some be unsupported. Only init appropriate
586 * constants. */
588 Py_TYPE(&EVPtype) = &PyType_Type;
589 if (PyType_Ready(&EVPtype) < 0)
590 return;
592 m = Py_InitModule("_hashlib", EVP_functions);
593 if (m == NULL)
594 return;
596 #if HASH_OBJ_CONSTRUCTOR
597 Py_INCREF(&EVPtype);
598 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
599 #endif
601 /* these constants are used by the convenience constructors */
602 INIT_CONSTRUCTOR_CONSTANTS(md5);
603 INIT_CONSTRUCTOR_CONSTANTS(sha1);
604 INIT_CONSTRUCTOR_CONSTANTS(sha224);
605 INIT_CONSTRUCTOR_CONSTANTS(sha256);
606 INIT_CONSTRUCTOR_CONSTANTS(sha384);
607 INIT_CONSTRUCTOR_CONSTANTS(sha512);