Merged revisions 78875 via svnmerge from
[python/dscho.git] / Modules / _hashopenssl.c
blobee149e117b1470c2e3c504051ac06592ad8e7580
1 /* Module that wraps all OpenSSL hash algorithms */
3 /*
4 * Copyright (C) 2005-2010 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) {
102 if (len > (Py_ssize_t)MUNCH_SIZE)
103 process = MUNCH_SIZE;
104 else
105 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
106 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
107 len -= process;
108 cp += process;
112 /* Internal methods for a hash object */
114 static void
115 EVP_dealloc(EVPobject *self)
117 #ifdef WITH_THREAD
118 if (self->lock != NULL)
119 PyThread_free_lock(self->lock);
120 #endif
121 EVP_MD_CTX_cleanup(&self->ctx);
122 Py_XDECREF(self->name);
123 PyObject_Del(self);
126 static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
128 ENTER_HASHLIB(self);
129 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
130 LEAVE_HASHLIB(self);
133 /* External methods for a hash object */
135 PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
138 static PyObject *
139 EVP_copy(EVPobject *self, PyObject *unused)
141 EVPobject *newobj;
143 if ( (newobj = newEVPobject(self->name))==NULL)
144 return NULL;
146 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
147 return (PyObject *)newobj;
150 PyDoc_STRVAR(EVP_digest__doc__,
151 "Return the digest value as a string of binary data.");
153 static PyObject *
154 EVP_digest(EVPobject *self, PyObject *unused)
156 unsigned char digest[EVP_MAX_MD_SIZE];
157 EVP_MD_CTX temp_ctx;
158 PyObject *retval;
159 unsigned int digest_size;
161 locked_EVP_MD_CTX_copy(&temp_ctx, self);
162 digest_size = EVP_MD_CTX_size(&temp_ctx);
163 EVP_DigestFinal(&temp_ctx, digest, NULL);
165 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
166 EVP_MD_CTX_cleanup(&temp_ctx);
167 return retval;
170 PyDoc_STRVAR(EVP_hexdigest__doc__,
171 "Return the digest value as a string of hexadecimal digits.");
173 static PyObject *
174 EVP_hexdigest(EVPobject *self, PyObject *unused)
176 unsigned char digest[EVP_MAX_MD_SIZE];
177 EVP_MD_CTX temp_ctx;
178 PyObject *retval;
179 char *hex_digest;
180 unsigned int i, j, digest_size;
182 /* Get the raw (binary) digest value */
183 locked_EVP_MD_CTX_copy(&temp_ctx, self);
184 digest_size = EVP_MD_CTX_size(&temp_ctx);
185 EVP_DigestFinal(&temp_ctx, digest, NULL);
187 EVP_MD_CTX_cleanup(&temp_ctx);
189 /* Allocate a new buffer */
190 hex_digest = PyMem_Malloc(digest_size * 2 + 1);
191 if (!hex_digest)
192 return PyErr_NoMemory();
194 /* Make hex version of the digest */
195 for(i=j=0; i<digest_size; i++) {
196 char c;
197 c = (digest[i] >> 4) & 0xf;
198 c = (c>9) ? c+'a'-10 : c + '0';
199 hex_digest[j++] = c;
200 c = (digest[i] & 0xf);
201 c = (c>9) ? c+'a'-10 : c + '0';
202 hex_digest[j++] = c;
204 retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2);
205 PyMem_Free(hex_digest);
206 return retval;
209 PyDoc_STRVAR(EVP_update__doc__,
210 "Update this hash object's state with the provided string.");
212 static PyObject *
213 EVP_update(EVPobject *self, PyObject *args)
215 PyObject *obj;
216 Py_buffer view;
218 if (!PyArg_ParseTuple(args, "O:update", &obj))
219 return NULL;
221 GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
223 #ifdef WITH_THREAD
224 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
225 self->lock = PyThread_allocate_lock();
226 /* fail? lock = NULL and we fail over to non-threaded code. */
229 if (self->lock != NULL) {
230 Py_BEGIN_ALLOW_THREADS
231 PyThread_acquire_lock(self->lock, 1);
232 EVP_hash(self, view.buf, view.len);
233 PyThread_release_lock(self->lock);
234 Py_END_ALLOW_THREADS
235 } else {
236 EVP_hash(self, view.buf, view.len);
238 #else
239 EVP_hash(self, view.buf, view.len);
240 #endif
242 PyBuffer_Release(&view);
243 Py_RETURN_NONE;
246 static PyMethodDef EVP_methods[] = {
247 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
248 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
249 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
250 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
251 {NULL, NULL} /* sentinel */
254 static PyObject *
255 EVP_get_block_size(EVPobject *self, void *closure)
257 long block_size;
258 block_size = EVP_MD_CTX_block_size(&self->ctx);
259 return PyLong_FromLong(block_size);
262 static PyObject *
263 EVP_get_digest_size(EVPobject *self, void *closure)
265 long size;
266 size = EVP_MD_CTX_size(&self->ctx);
267 return PyLong_FromLong(size);
270 static PyMemberDef EVP_members[] = {
271 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
272 {NULL} /* Sentinel */
275 static PyGetSetDef EVP_getseters[] = {
276 {"digest_size",
277 (getter)EVP_get_digest_size, NULL,
278 NULL,
279 NULL},
280 {"block_size",
281 (getter)EVP_get_block_size, NULL,
282 NULL,
283 NULL},
284 {NULL} /* Sentinel */
288 static PyObject *
289 EVP_repr(EVPobject *self)
291 return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self);
294 #if HASH_OBJ_CONSTRUCTOR
295 static int
296 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
298 static char *kwlist[] = {"name", "string", NULL};
299 PyObject *name_obj = NULL;
300 PyObject *data_obj = NULL;
301 Py_buffer view;
302 char *nameStr;
303 const EVP_MD *digest;
305 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
306 &name_obj, &data_obj)) {
307 return -1;
310 if (data_obj)
311 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
313 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
314 PyErr_SetString(PyExc_TypeError, "name must be a string");
315 if (data_obj)
316 PyBuffer_Release(&view);
317 return -1;
320 digest = EVP_get_digestbyname(nameStr);
321 if (!digest) {
322 PyErr_SetString(PyExc_ValueError, "unknown hash function");
323 if (data_obj)
324 PyBuffer_Release(&view);
325 return -1;
327 EVP_DigestInit(&self->ctx, digest);
329 self->name = name_obj;
330 Py_INCREF(self->name);
332 if (data_obj) {
333 if (view.len >= HASHLIB_GIL_MINSIZE) {
334 Py_BEGIN_ALLOW_THREADS
335 EVP_hash(self, view.buf, view.len);
336 Py_END_ALLOW_THREADS
337 } else {
338 EVP_hash(self, view.buf, view.len);
340 PyBuffer_Release(&view);
343 return 0;
345 #endif
348 PyDoc_STRVAR(hashtype_doc,
349 "A hash represents the object used to calculate a checksum of a\n\
350 string of information.\n\
352 Methods:\n\
354 update() -- updates the current digest with an additional string\n\
355 digest() -- return the current digest value\n\
356 hexdigest() -- return the current digest as a string of hexadecimal digits\n\
357 copy() -- return a copy of the current hash object\n\
359 Attributes:\n\
361 name -- the hash algorithm being used by this object\n\
362 digest_size -- number of bytes in this hashes output\n");
364 static PyTypeObject EVPtype = {
365 PyVarObject_HEAD_INIT(NULL, 0)
366 "_hashlib.HASH", /*tp_name*/
367 sizeof(EVPobject), /*tp_basicsize*/
368 0, /*tp_itemsize*/
369 /* methods */
370 (destructor)EVP_dealloc, /*tp_dealloc*/
371 0, /*tp_print*/
372 0, /*tp_getattr*/
373 0, /*tp_setattr*/
374 0, /*tp_reserved*/
375 (reprfunc)EVP_repr, /*tp_repr*/
376 0, /*tp_as_number*/
377 0, /*tp_as_sequence*/
378 0, /*tp_as_mapping*/
379 0, /*tp_hash*/
380 0, /*tp_call*/
381 0, /*tp_str*/
382 0, /*tp_getattro*/
383 0, /*tp_setattro*/
384 0, /*tp_as_buffer*/
385 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
386 hashtype_doc, /*tp_doc*/
387 0, /*tp_traverse*/
388 0, /*tp_clear*/
389 0, /*tp_richcompare*/
390 0, /*tp_weaklistoffset*/
391 0, /*tp_iter*/
392 0, /*tp_iternext*/
393 EVP_methods, /* tp_methods */
394 EVP_members, /* tp_members */
395 EVP_getseters, /* tp_getset */
396 #if 1
397 0, /* tp_base */
398 0, /* tp_dict */
399 0, /* tp_descr_get */
400 0, /* tp_descr_set */
401 0, /* tp_dictoffset */
402 #endif
403 #if HASH_OBJ_CONSTRUCTOR
404 (initproc)EVP_tp_init, /* tp_init */
405 #endif
408 static PyObject *
409 EVPnew(PyObject *name_obj,
410 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
411 const unsigned char *cp, Py_ssize_t len)
413 EVPobject *self;
415 if (!digest && !initial_ctx) {
416 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
417 return NULL;
420 if ((self = newEVPobject(name_obj)) == NULL)
421 return NULL;
423 if (initial_ctx) {
424 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
425 } else {
426 EVP_DigestInit(&self->ctx, digest);
429 if (cp && len) {
430 if (len >= HASHLIB_GIL_MINSIZE) {
431 Py_BEGIN_ALLOW_THREADS
432 EVP_hash(self, cp, len);
433 Py_END_ALLOW_THREADS
434 } else {
435 EVP_hash(self, cp, len);
439 return (PyObject *)self;
443 /* The module-level function: new() */
445 PyDoc_STRVAR(EVP_new__doc__,
446 "Return a new hash object using the named algorithm.\n\
447 An optional string argument may be provided and will be\n\
448 automatically hashed.\n\
450 The MD5 and SHA1 algorithms are always supported.\n");
452 static PyObject *
453 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
455 static char *kwlist[] = {"name", "string", NULL};
456 PyObject *name_obj = NULL;
457 PyObject *data_obj = NULL;
458 Py_buffer view = { 0 };
459 PyObject *ret_obj;
460 char *name;
461 const EVP_MD *digest;
463 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
464 &name_obj, &data_obj)) {
465 return NULL;
468 if (!PyArg_Parse(name_obj, "s", &name)) {
469 PyErr_SetString(PyExc_TypeError, "name must be a string");
470 return NULL;
473 if (data_obj)
474 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
476 digest = EVP_get_digestbyname(name);
478 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len);
480 if (data_obj)
481 PyBuffer_Release(&view);
482 return ret_obj;
486 * This macro generates constructor function definitions for specific
487 * hash algorithms. These constructors are much faster than calling
488 * the generic one passing it a python string and are noticably
489 * faster than calling a python new() wrapper. Thats important for
490 * code that wants to make hashes of a bunch of small strings.
492 #define GEN_CONSTRUCTOR(NAME) \
493 static PyObject * \
494 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
496 PyObject *data_obj = NULL; \
497 Py_buffer view = { 0 }; \
498 PyObject *ret_obj; \
500 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
501 return NULL; \
504 if (data_obj) \
505 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
507 ret_obj = EVPnew( \
508 CONST_ ## NAME ## _name_obj, \
509 NULL, \
510 CONST_new_ ## NAME ## _ctx_p, \
511 (unsigned char*)view.buf, \
512 view.len); \
514 if (data_obj) \
515 PyBuffer_Release(&view); \
516 return ret_obj; \
519 /* a PyMethodDef structure for the constructor */
520 #define CONSTRUCTOR_METH_DEF(NAME) \
521 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
522 PyDoc_STR("Returns a " #NAME \
523 " hash object; optionally initialized with a string") \
526 /* used in the init function to setup a constructor */
527 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
528 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
529 if (EVP_get_digestbyname(#NAME)) { \
530 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
531 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
533 } while (0);
535 GEN_CONSTRUCTOR(md5)
536 GEN_CONSTRUCTOR(sha1)
537 GEN_CONSTRUCTOR(sha224)
538 GEN_CONSTRUCTOR(sha256)
539 GEN_CONSTRUCTOR(sha384)
540 GEN_CONSTRUCTOR(sha512)
542 /* List of functions exported by this module */
544 static struct PyMethodDef EVP_functions[] = {
545 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
546 CONSTRUCTOR_METH_DEF(md5),
547 CONSTRUCTOR_METH_DEF(sha1),
548 CONSTRUCTOR_METH_DEF(sha224),
549 CONSTRUCTOR_METH_DEF(sha256),
550 CONSTRUCTOR_METH_DEF(sha384),
551 CONSTRUCTOR_METH_DEF(sha512),
552 {NULL, NULL} /* Sentinel */
556 /* Initialize this module. */
559 static struct PyModuleDef _hashlibmodule = {
560 PyModuleDef_HEAD_INIT,
561 "_hashlib",
562 NULL,
564 EVP_functions,
565 NULL,
566 NULL,
567 NULL,
568 NULL
571 PyMODINIT_FUNC
572 PyInit__hashlib(void)
574 PyObject *m;
576 OpenSSL_add_all_digests();
578 /* TODO build EVP_functions openssl_* entries dynamically based
579 * on what hashes are supported rather than listing many
580 * but having some be unsupported. Only init appropriate
581 * constants. */
583 Py_TYPE(&EVPtype) = &PyType_Type;
584 if (PyType_Ready(&EVPtype) < 0)
585 return NULL;
587 m = PyModule_Create(&_hashlibmodule);
588 if (m == NULL)
589 return NULL;
591 #if HASH_OBJ_CONSTRUCTOR
592 Py_INCREF(&EVPtype);
593 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
594 #endif
596 /* these constants are used by the convenience constructors */
597 INIT_CONSTRUCTOR_CONSTANTS(md5);
598 INIT_CONSTRUCTOR_CONSTANTS(sha1);
599 INIT_CONSTRUCTOR_CONSTANTS(sha224);
600 INIT_CONSTRUCTOR_CONSTANTS(sha256);
601 INIT_CONSTRUCTOR_CONSTANTS(sha384);
602 INIT_CONSTRUCTOR_CONSTANTS(sha512);
603 return m;