Fix issue number in comment.
[python.git] / Modules / _hashopenssl.c
blob004d5a3189183e83b41907f09e19797bfbefe147
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"
19 #ifdef WITH_THREAD
20 #include "pythread.h"
21 #define ENTER_HASHLIB(obj) \
22 if ((obj)->lock) { \
23 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
24 Py_BEGIN_ALLOW_THREADS \
25 PyThread_acquire_lock((obj)->lock, 1); \
26 Py_END_ALLOW_THREADS \
27 } \
29 #define LEAVE_HASHLIB(obj) \
30 if ((obj)->lock) { \
31 PyThread_release_lock((obj)->lock); \
33 #else
34 #define ENTER_HASHLIB(obj)
35 #define LEAVE_HASHLIB(obj)
36 #endif
38 /* EVP is the preferred interface to hashing in OpenSSL */
39 #include <openssl/evp.h>
41 #define MUNCH_SIZE INT_MAX
43 /* TODO(gps): We should probably make this a module or EVPobject attribute
44 * to allow the user to optimize based on the platform they're using. */
45 #define HASHLIB_GIL_MINSIZE 2048
47 #ifndef HASH_OBJ_CONSTRUCTOR
48 #define HASH_OBJ_CONSTRUCTOR 0
49 #endif
52 typedef struct {
53 PyObject_HEAD
54 PyObject *name; /* name of this hash algorithm */
55 EVP_MD_CTX ctx; /* OpenSSL message digest context */
56 #ifdef WITH_THREAD
57 PyThread_type_lock lock; /* OpenSSL context lock */
58 #endif
59 } EVPobject;
62 static PyTypeObject EVPtype;
65 #define DEFINE_CONSTS_FOR_NEW(Name) \
66 static PyObject *CONST_ ## Name ## _name_obj; \
67 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
68 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
70 DEFINE_CONSTS_FOR_NEW(md5)
71 DEFINE_CONSTS_FOR_NEW(sha1)
72 DEFINE_CONSTS_FOR_NEW(sha224)
73 DEFINE_CONSTS_FOR_NEW(sha256)
74 DEFINE_CONSTS_FOR_NEW(sha384)
75 DEFINE_CONSTS_FOR_NEW(sha512)
78 static EVPobject *
79 newEVPobject(PyObject *name)
81 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
83 /* save the name for .name to return */
84 if (retval != NULL) {
85 Py_INCREF(name);
86 retval->name = name;
87 #ifdef WITH_THREAD
88 retval->lock = NULL;
89 #endif
92 return retval;
95 static void
96 EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
98 unsigned int process;
99 const unsigned char *cp = (const unsigned char *)vp;
100 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 = PyString_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 /* Create a new string */
190 /* NOTE: not thread safe! modifying an already created string object */
191 /* (not a problem because we hold the GIL by default) */
192 retval = PyString_FromStringAndSize(NULL, digest_size * 2);
193 if (!retval)
194 return NULL;
195 hex_digest = PyString_AsString(retval);
196 if (!hex_digest) {
197 Py_DECREF(retval);
198 return NULL;
201 /* Make hex version of the digest */
202 for(i=j=0; i<digest_size; i++) {
203 char c;
204 c = (digest[i] >> 4) & 0xf;
205 c = (c>9) ? c+'a'-10 : c + '0';
206 hex_digest[j++] = c;
207 c = (digest[i] & 0xf);
208 c = (c>9) ? c+'a'-10 : c + '0';
209 hex_digest[j++] = c;
211 return retval;
214 PyDoc_STRVAR(EVP_update__doc__,
215 "Update this hash object's state with the provided string.");
217 static PyObject *
218 EVP_update(EVPobject *self, PyObject *args)
220 Py_buffer view;
222 if (!PyArg_ParseTuple(args, "s*:update", &view))
223 return NULL;
225 #ifdef WITH_THREAD
226 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
227 self->lock = PyThread_allocate_lock();
228 /* fail? lock = NULL and we fail over to non-threaded code. */
231 if (self->lock != NULL) {
232 Py_BEGIN_ALLOW_THREADS
233 PyThread_acquire_lock(self->lock, 1);
234 EVP_hash(self, view.buf, view.len);
235 PyThread_release_lock(self->lock);
236 Py_END_ALLOW_THREADS
238 else
239 #endif
241 EVP_hash(self, view.buf, view.len);
244 PyBuffer_Release(&view);
246 Py_RETURN_NONE;
249 static PyMethodDef EVP_methods[] = {
250 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
251 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
252 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
253 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
254 {NULL, NULL} /* sentinel */
257 static PyObject *
258 EVP_get_block_size(EVPobject *self, void *closure)
260 long block_size;
261 block_size = EVP_MD_CTX_block_size(&self->ctx);
262 return PyLong_FromLong(block_size);
265 static PyObject *
266 EVP_get_digest_size(EVPobject *self, void *closure)
268 long size;
269 size = EVP_MD_CTX_size(&self->ctx);
270 return PyLong_FromLong(size);
273 static PyMemberDef EVP_members[] = {
274 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
275 {NULL} /* Sentinel */
278 static PyGetSetDef EVP_getseters[] = {
279 {"digest_size",
280 (getter)EVP_get_digest_size, NULL,
281 NULL,
282 NULL},
283 {"block_size",
284 (getter)EVP_get_block_size, NULL,
285 NULL,
286 NULL},
287 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
288 * the old sha module also supported 'digestsize'. ugh. */
289 {"digestsize",
290 (getter)EVP_get_digest_size, NULL,
291 NULL,
292 NULL},
293 {NULL} /* Sentinel */
297 static PyObject *
298 EVP_repr(PyObject *self)
300 char buf[100];
301 PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
302 PyString_AsString(((EVPobject *)self)->name), self);
303 return PyString_FromString(buf);
306 #if HASH_OBJ_CONSTRUCTOR
307 static int
308 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
310 static char *kwlist[] = {"name", "string", NULL};
311 PyObject *name_obj = NULL;
312 Py_buffer view = { 0 };
313 char *nameStr;
314 const EVP_MD *digest;
316 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s*:HASH", kwlist,
317 &name_obj, &view)) {
318 return -1;
321 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
322 PyErr_SetString(PyExc_TypeError, "name must be a string");
323 PyBuffer_Release(&view);
324 return -1;
327 digest = EVP_get_digestbyname(nameStr);
328 if (!digest) {
329 PyErr_SetString(PyExc_ValueError, "unknown hash function");
330 PyBuffer_Release(&view);
331 return -1;
333 EVP_DigestInit(&self->ctx, digest);
335 self->name = name_obj;
336 Py_INCREF(self->name);
338 if (view.obj) {
339 if (view.len >= HASHLIB_GIL_MINSIZE) {
340 Py_BEGIN_ALLOW_THREADS
341 EVP_hash(self, view.buf, view.len);
342 Py_END_ALLOW_THREADS
343 } else {
344 EVP_hash(self, view.buf, view.len);
346 PyBuffer_Release(&view);
349 return 0;
351 #endif
354 PyDoc_STRVAR(hashtype_doc,
355 "A hash represents the object used to calculate a checksum of a\n\
356 string of information.\n\
358 Methods:\n\
360 update() -- updates the current digest with an additional string\n\
361 digest() -- return the current digest value\n\
362 hexdigest() -- return the current digest as a string of hexadecimal digits\n\
363 copy() -- return a copy of the current hash object\n\
365 Attributes:\n\
367 name -- the hash algorithm being used by this object\n\
368 digest_size -- number of bytes in this hashes output\n");
370 static PyTypeObject EVPtype = {
371 PyVarObject_HEAD_INIT(NULL, 0)
372 "_hashlib.HASH", /*tp_name*/
373 sizeof(EVPobject), /*tp_basicsize*/
374 0, /*tp_itemsize*/
375 /* methods */
376 (destructor)EVP_dealloc, /*tp_dealloc*/
377 0, /*tp_print*/
378 0, /*tp_getattr*/
379 0, /*tp_setattr*/
380 0, /*tp_compare*/
381 EVP_repr, /*tp_repr*/
382 0, /*tp_as_number*/
383 0, /*tp_as_sequence*/
384 0, /*tp_as_mapping*/
385 0, /*tp_hash*/
386 0, /*tp_call*/
387 0, /*tp_str*/
388 0, /*tp_getattro*/
389 0, /*tp_setattro*/
390 0, /*tp_as_buffer*/
391 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
392 hashtype_doc, /*tp_doc*/
393 0, /*tp_traverse*/
394 0, /*tp_clear*/
395 0, /*tp_richcompare*/
396 0, /*tp_weaklistoffset*/
397 0, /*tp_iter*/
398 0, /*tp_iternext*/
399 EVP_methods, /* tp_methods */
400 EVP_members, /* tp_members */
401 EVP_getseters, /* tp_getset */
402 #if 1
403 0, /* tp_base */
404 0, /* tp_dict */
405 0, /* tp_descr_get */
406 0, /* tp_descr_set */
407 0, /* tp_dictoffset */
408 #endif
409 #if HASH_OBJ_CONSTRUCTOR
410 (initproc)EVP_tp_init, /* tp_init */
411 #endif
414 static PyObject *
415 EVPnew(PyObject *name_obj,
416 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
417 const unsigned char *cp, Py_ssize_t len)
419 EVPobject *self;
421 if (!digest && !initial_ctx) {
422 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
423 return NULL;
426 if ((self = newEVPobject(name_obj)) == NULL)
427 return NULL;
429 if (initial_ctx) {
430 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
431 } else {
432 EVP_DigestInit(&self->ctx, digest);
435 if (cp && len) {
436 if (len >= HASHLIB_GIL_MINSIZE) {
437 Py_BEGIN_ALLOW_THREADS
438 EVP_hash(self, cp, len);
439 Py_END_ALLOW_THREADS
440 } else {
441 EVP_hash(self, cp, len);
445 return (PyObject *)self;
449 /* The module-level function: new() */
451 PyDoc_STRVAR(EVP_new__doc__,
452 "Return a new hash object using the named algorithm.\n\
453 An optional string argument may be provided and will be\n\
454 automatically hashed.\n\
456 The MD5 and SHA1 algorithms are always supported.\n");
458 static PyObject *
459 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
461 static char *kwlist[] = {"name", "string", NULL};
462 PyObject *name_obj = NULL;
463 Py_buffer view = { 0 };
464 PyObject *ret_obj;
465 char *name;
466 const EVP_MD *digest;
468 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|s*:new", kwlist,
469 &name_obj, &view)) {
470 return NULL;
473 if (!PyArg_Parse(name_obj, "s", &name)) {
474 PyErr_SetString(PyExc_TypeError, "name must be a string");
475 return NULL;
478 digest = EVP_get_digestbyname(name);
480 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
481 view.len);
482 PyBuffer_Release(&view);
484 return ret_obj;
488 * This macro generates constructor function definitions for specific
489 * hash algorithms. These constructors are much faster than calling
490 * the generic one passing it a python string and are noticably
491 * faster than calling a python new() wrapper. Thats important for
492 * code that wants to make hashes of a bunch of small strings.
494 #define GEN_CONSTRUCTOR(NAME) \
495 static PyObject * \
496 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
498 Py_buffer view = { 0 }; \
499 PyObject *ret_obj; \
501 if (!PyArg_ParseTuple(args, "|s*:" #NAME , &view)) { \
502 return NULL; \
505 ret_obj = EVPnew( \
506 CONST_ ## NAME ## _name_obj, \
507 NULL, \
508 CONST_new_ ## NAME ## _ctx_p, \
509 (unsigned char*)view.buf, view.len); \
510 PyBuffer_Release(&view); \
511 return ret_obj; \
514 /* a PyMethodDef structure for the constructor */
515 #define CONSTRUCTOR_METH_DEF(NAME) \
516 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
517 PyDoc_STR("Returns a " #NAME \
518 " hash object; optionally initialized with a string") \
521 /* used in the init function to setup a constructor */
522 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
523 CONST_ ## NAME ## _name_obj = PyString_FromString(#NAME); \
524 if (EVP_get_digestbyname(#NAME)) { \
525 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
526 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
528 } while (0);
530 GEN_CONSTRUCTOR(md5)
531 GEN_CONSTRUCTOR(sha1)
532 GEN_CONSTRUCTOR(sha224)
533 GEN_CONSTRUCTOR(sha256)
534 GEN_CONSTRUCTOR(sha384)
535 GEN_CONSTRUCTOR(sha512)
537 /* List of functions exported by this module */
539 static struct PyMethodDef EVP_functions[] = {
540 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
541 CONSTRUCTOR_METH_DEF(md5),
542 CONSTRUCTOR_METH_DEF(sha1),
543 CONSTRUCTOR_METH_DEF(sha224),
544 CONSTRUCTOR_METH_DEF(sha256),
545 CONSTRUCTOR_METH_DEF(sha384),
546 CONSTRUCTOR_METH_DEF(sha512),
547 {NULL, NULL} /* Sentinel */
551 /* Initialize this module. */
553 PyMODINIT_FUNC
554 init_hashlib(void)
556 PyObject *m;
558 OpenSSL_add_all_digests();
560 /* TODO build EVP_functions openssl_* entries dynamically based
561 * on what hashes are supported rather than listing many
562 * but having some be unsupported. Only init appropriate
563 * constants. */
565 Py_TYPE(&EVPtype) = &PyType_Type;
566 if (PyType_Ready(&EVPtype) < 0)
567 return;
569 m = Py_InitModule("_hashlib", EVP_functions);
570 if (m == NULL)
571 return;
573 #if HASH_OBJ_CONSTRUCTOR
574 Py_INCREF(&EVPtype);
575 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
576 #endif
578 /* these constants are used by the convenience constructors */
579 INIT_CONSTRUCTOR_CONSTANTS(md5);
580 INIT_CONSTRUCTOR_CONSTANTS(sha1);
581 INIT_CONSTRUCTOR_CONSTANTS(sha224);
582 INIT_CONSTRUCTOR_CONSTANTS(sha256);
583 INIT_CONSTRUCTOR_CONSTANTS(sha384);
584 INIT_CONSTRUCTOR_CONSTANTS(sha512);