blob: d37689e0e613764808e5b1cf8db0faeeebc43fae [file] [log] [blame]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001/* Module that wraps all OpenSSL hash algorithms */
2
3/*
Benjamin Peterson46a99002010-01-09 18:45:30 +00004 * Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00005 * Licensed to PSF under a Contributor Agreement.
6 *
7 * Derived from a skeleton of shamodule.c containing work performed by:
8 *
9 * Andrew Kuchling (amk@amk.ca)
10 * Greg Stein (gstein@lyra.org)
11 *
12 */
13
Thomas Wouters9bc844e2006-03-01 21:50:07 +000014#define PY_SSIZE_T_CLEAN
15
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000016#include "Python.h"
17#include "structmember.h"
Gregory P. Smith365a1862009-02-12 07:35:29 +000018#include "hashlib.h"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000019
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000020#ifdef WITH_THREAD
Gregory P. Smith3f61d612009-05-04 00:45:33 +000021#include "pythread.h"
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000022 #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 } \
29 }
30 #define LEAVE_HASHLIB(obj) \
31 if ((obj)->lock) { \
32 PyThread_release_lock((obj)->lock); \
33 }
34#else
35 #define ENTER_HASHLIB(obj)
36 #define LEAVE_HASHLIB(obj)
37#endif
38
Gregory P. Smith3f61d612009-05-04 00:45:33 +000039/* EVP is the preferred interface to hashing in OpenSSL */
40#include <openssl/evp.h>
Gregory P. Smith13b55292010-09-06 08:30:23 +000041/* We use the object interface to discover what hashes OpenSSL supports. */
42#include <openssl/objects.h>
Gregory P. Smith3f61d612009-05-04 00:45:33 +000043
44#define MUNCH_SIZE INT_MAX
45
46/* TODO(gps): We should probably make this a module or EVPobject attribute
47 * to allow the user to optimize based on the platform they're using. */
48#define HASHLIB_GIL_MINSIZE 2048
49
50#ifndef HASH_OBJ_CONSTRUCTOR
51#define HASH_OBJ_CONSTRUCTOR 0
52#endif
53
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000054/* Minimum OpenSSL version needed to support sha224 and higher. */
55#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x00908000)
56#define _OPENSSL_SUPPORTS_SHA2
57#endif
Gregory P. Smith3f61d612009-05-04 00:45:33 +000058
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000059typedef struct {
60 PyObject_HEAD
61 PyObject *name; /* name of this hash algorithm */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000062 EVP_MD_CTX ctx; /* OpenSSL message digest context */
63#ifdef WITH_THREAD
64 PyThread_type_lock lock; /* OpenSSL context lock */
65#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000066} EVPobject;
67
68
69static PyTypeObject EVPtype;
70
71
72#define DEFINE_CONSTS_FOR_NEW(Name) \
73 static PyObject *CONST_ ## Name ## _name_obj; \
74 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
75 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
76
Neal Norwitzf0459142006-01-07 21:20:24 +000077DEFINE_CONSTS_FOR_NEW(md5)
78DEFINE_CONSTS_FOR_NEW(sha1)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000079#ifdef _OPENSSL_SUPPORTS_SHA2
Neal Norwitzf0459142006-01-07 21:20:24 +000080DEFINE_CONSTS_FOR_NEW(sha224)
81DEFINE_CONSTS_FOR_NEW(sha256)
82DEFINE_CONSTS_FOR_NEW(sha384)
83DEFINE_CONSTS_FOR_NEW(sha512)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000084#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000085
86
87static EVPobject *
88newEVPobject(PyObject *name)
89{
90 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
91
92 /* save the name for .name to return */
93 if (retval != NULL) {
94 Py_INCREF(name);
95 retval->name = name;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000096#ifdef WITH_THREAD
97 retval->lock = NULL;
98#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000099 }
100
101 return retval;
102}
103
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000104static void
105EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
106{
107 unsigned int process;
108 const unsigned char *cp = (const unsigned char *)vp;
109 while (0 < len) {
110 if (len > (Py_ssize_t)MUNCH_SIZE)
111 process = MUNCH_SIZE;
112 else
113 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
114 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
115 len -= process;
116 cp += process;
117 }
118}
119
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000120/* Internal methods for a hash object */
121
122static void
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000123EVP_dealloc(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000124{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000125#ifdef WITH_THREAD
126 if (self->lock != NULL)
127 PyThread_free_lock(self->lock);
128#endif
129 EVP_MD_CTX_cleanup(&self->ctx);
130 Py_XDECREF(self->name);
131 PyObject_Del(self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000132}
133
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000134static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
135{
136 ENTER_HASHLIB(self);
137 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
138 LEAVE_HASHLIB(self);
139}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000140
141/* External methods for a hash object */
142
143PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
144
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000145
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000146static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000147EVP_copy(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000148{
149 EVPobject *newobj;
150
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000151 if ( (newobj = newEVPobject(self->name))==NULL)
152 return NULL;
153
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000154 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000155 return (PyObject *)newobj;
156}
157
158PyDoc_STRVAR(EVP_digest__doc__,
159"Return the digest value as a string of binary data.");
160
161static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000162EVP_digest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000163{
164 unsigned char digest[EVP_MAX_MD_SIZE];
165 EVP_MD_CTX temp_ctx;
166 PyObject *retval;
167 unsigned int digest_size;
168
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000169 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000170 digest_size = EVP_MD_CTX_size(&temp_ctx);
Neal Norwitzf0459142006-01-07 21:20:24 +0000171 EVP_DigestFinal(&temp_ctx, digest, NULL);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000172
Christian Heimes72b710a2008-05-26 13:28:38 +0000173 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000174 EVP_MD_CTX_cleanup(&temp_ctx);
175 return retval;
176}
177
178PyDoc_STRVAR(EVP_hexdigest__doc__,
179"Return the digest value as a string of hexadecimal digits.");
180
181static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000182EVP_hexdigest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000183{
184 unsigned char digest[EVP_MAX_MD_SIZE];
185 EVP_MD_CTX temp_ctx;
186 PyObject *retval;
187 char *hex_digest;
188 unsigned int i, j, digest_size;
189
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000190 /* Get the raw (binary) digest value */
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000191 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000192 digest_size = EVP_MD_CTX_size(&temp_ctx);
193 EVP_DigestFinal(&temp_ctx, digest, NULL);
194
195 EVP_MD_CTX_cleanup(&temp_ctx);
196
Guido van Rossumf8953072007-07-10 13:20:29 +0000197 /* Allocate a new buffer */
198 hex_digest = PyMem_Malloc(digest_size * 2 + 1);
199 if (!hex_digest)
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000200 return PyErr_NoMemory();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000201
202 /* Make hex version of the digest */
203 for(i=j=0; i<digest_size; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200204 unsigned char c;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000205 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200206 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000207 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200208 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000209 }
Guido van Rossumf8953072007-07-10 13:20:29 +0000210 retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2);
211 PyMem_Free(hex_digest);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000212 return retval;
213}
214
215PyDoc_STRVAR(EVP_update__doc__,
216"Update this hash object's state with the provided string.");
217
218static PyObject *
219EVP_update(EVPobject *self, PyObject *args)
220{
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000221 PyObject *obj;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000222 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000223
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000224 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000225 return NULL;
226
Gregory P. Smith365a1862009-02-12 07:35:29 +0000227 GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000228
229#ifdef WITH_THREAD
230 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
231 self->lock = PyThread_allocate_lock();
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000232 /* fail? lock = NULL and we fail over to non-threaded code. */
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000233 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000234
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000235 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);
243 }
244#else
245 EVP_hash(self, view.buf, view.len);
246#endif
247
248 PyBuffer_Release(&view);
249 Py_RETURN_NONE;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000250}
251
252static PyMethodDef EVP_methods[] = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000253 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
254 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000255 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000256 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
257 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000258};
259
260static PyObject *
261EVP_get_block_size(EVPobject *self, void *closure)
262{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000263 long block_size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000264 block_size = EVP_MD_CTX_block_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000265 return PyLong_FromLong(block_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000266}
267
268static PyObject *
269EVP_get_digest_size(EVPobject *self, void *closure)
270{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000271 long size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000272 size = EVP_MD_CTX_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000273 return PyLong_FromLong(size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000274}
275
276static PyMemberDef EVP_members[] = {
277 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
278 {NULL} /* Sentinel */
279};
280
281static PyGetSetDef EVP_getseters[] = {
282 {"digest_size",
283 (getter)EVP_get_digest_size, NULL,
284 NULL,
285 NULL},
286 {"block_size",
287 (getter)EVP_get_block_size, NULL,
288 NULL,
289 NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000290 {NULL} /* Sentinel */
291};
292
293
294static PyObject *
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000295EVP_repr(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000296{
Victor Stinner3f1af5c2010-03-12 17:00:41 +0000297 return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000298}
299
300#if HASH_OBJ_CONSTRUCTOR
301static int
302EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
303{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000304 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000305 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000306 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000307 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000308 char *nameStr;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000309 const EVP_MD *digest;
310
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000311 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
312 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000313 return -1;
314 }
315
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000316 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000317 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000318
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000319 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
320 PyErr_SetString(PyExc_TypeError, "name must be a string");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000321 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000322 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000323 return -1;
324 }
325
326 digest = EVP_get_digestbyname(nameStr);
327 if (!digest) {
328 PyErr_SetString(PyExc_ValueError, "unknown hash function");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000329 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000330 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000331 return -1;
332 }
333 EVP_DigestInit(&self->ctx, digest);
334
335 self->name = name_obj;
336 Py_INCREF(self->name);
337
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000338 if (data_obj) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000339 if (view.len >= HASHLIB_GIL_MINSIZE) {
340 Py_BEGIN_ALLOW_THREADS
341 EVP_hash(self, view.buf, view.len);
342 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000343 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000344 EVP_hash(self, view.buf, view.len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000345 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000346 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000347 }
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000348
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000349 return 0;
350}
351#endif
352
353
354PyDoc_STRVAR(hashtype_doc,
355"A hash represents the object used to calculate a checksum of a\n\
356string of information.\n\
357\n\
358Methods:\n\
359\n\
360update() -- updates the current digest with an additional string\n\
361digest() -- return the current digest value\n\
362hexdigest() -- return the current digest as a string of hexadecimal digits\n\
363copy() -- return a copy of the current hash object\n\
364\n\
365Attributes:\n\
366\n\
367name -- the hash algorithm being used by this object\n\
368digest_size -- number of bytes in this hashes output\n");
369
370static PyTypeObject EVPtype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000371 PyVarObject_HEAD_INIT(NULL, 0)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000372 "_hashlib.HASH", /*tp_name*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000373 sizeof(EVPobject), /*tp_basicsize*/
374 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000375 /* methods */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000376 (destructor)EVP_dealloc, /*tp_dealloc*/
377 0, /*tp_print*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000378 0, /*tp_getattr*/
379 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000380 0, /*tp_reserved*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000381 (reprfunc)EVP_repr, /*tp_repr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000382 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*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000394 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 */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000401 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
412};
413
414static PyObject *
415EVPnew(PyObject *name_obj,
416 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000417 const unsigned char *cp, Py_ssize_t len)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000418{
419 EVPobject *self;
420
421 if (!digest && !initial_ctx) {
422 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
423 return NULL;
424 }
425
426 if ((self = newEVPobject(name_obj)) == NULL)
427 return NULL;
428
429 if (initial_ctx) {
430 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
431 } else {
432 EVP_DigestInit(&self->ctx, digest);
433 }
434
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000435 if (cp && len) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000436 if (len >= HASHLIB_GIL_MINSIZE) {
437 Py_BEGIN_ALLOW_THREADS
438 EVP_hash(self, cp, len);
439 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000440 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000441 EVP_hash(self, cp, len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000442 }
443 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000444
445 return (PyObject *)self;
446}
447
448
449/* The module-level function: new() */
450
451PyDoc_STRVAR(EVP_new__doc__,
452"Return a new hash object using the named algorithm.\n\
453An optional string argument may be provided and will be\n\
454automatically hashed.\n\
455\n\
456The MD5 and SHA1 algorithms are always supported.\n");
457
458static PyObject *
459EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
460{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000461 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000462 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000463 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000464 Py_buffer view = { 0 };
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000465 PyObject *ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000466 char *name;
467 const EVP_MD *digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000468
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000469 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
470 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000471 return NULL;
472 }
473
474 if (!PyArg_Parse(name_obj, "s", &name)) {
475 PyErr_SetString(PyExc_TypeError, "name must be a string");
476 return NULL;
477 }
478
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000479 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000480 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000481
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000482 digest = EVP_get_digestbyname(name);
483
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000484 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000485
486 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000487 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000488 return ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000489}
490
Gregory P. Smith13b55292010-09-06 08:30:23 +0000491
492/* State for our callback function so that it can accumulate a result. */
493typedef struct _internal_name_mapper_state {
494 PyObject *set;
495 int error;
496} _InternalNameMapperState;
497
498
499/* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
500static void
501_openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg)
502{
503 _InternalNameMapperState *state = (_InternalNameMapperState *)arg;
504 PyObject *py_name;
505
506 assert(state != NULL);
507 if (openssl_obj_name == NULL)
508 return;
509 /* Ignore aliased names, they pollute the list and OpenSSL appears to
510 * have a its own definition of alias as the resulting list still
511 * contains duplicate and alternate names for several algorithms. */
512 if (openssl_obj_name->alias)
513 return;
514
515 py_name = PyUnicode_FromString(openssl_obj_name->name);
516 if (py_name == NULL) {
517 state->error = 1;
518 } else {
519 if (PySet_Add(state->set, py_name) != 0) {
520 Py_DECREF(py_name);
521 state->error = 1;
522 }
523 }
524}
525
526
527/* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */
528static PyObject*
529generate_hash_name_list(void)
530{
531 _InternalNameMapperState state;
532 state.set = PyFrozenSet_New(NULL);
533 if (state.set == NULL)
534 return NULL;
535 state.error = 0;
536
537 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state);
538
539 if (state.error) {
540 Py_DECREF(state.set);
541 return NULL;
542 }
543 return state.set;
544}
545
546
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000547/*
548 * This macro generates constructor function definitions for specific
549 * hash algorithms. These constructors are much faster than calling
550 * the generic one passing it a python string and are noticably
551 * faster than calling a python new() wrapper. Thats important for
552 * code that wants to make hashes of a bunch of small strings.
553 */
554#define GEN_CONSTRUCTOR(NAME) \
555 static PyObject * \
556 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
557 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000558 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000559 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000560 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000561 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000562 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000563 return NULL; \
564 } \
565 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000566 if (data_obj) \
Gregory P. Smith365a1862009-02-12 07:35:29 +0000567 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000568 \
569 ret_obj = EVPnew( \
570 CONST_ ## NAME ## _name_obj, \
571 NULL, \
572 CONST_new_ ## NAME ## _ctx_p, \
573 (unsigned char*)view.buf, \
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000574 view.len); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000575 \
576 if (data_obj) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000577 PyBuffer_Release(&view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000578 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000579 }
580
581/* a PyMethodDef structure for the constructor */
582#define CONSTRUCTOR_METH_DEF(NAME) \
583 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
584 PyDoc_STR("Returns a " #NAME \
585 " hash object; optionally initialized with a string") \
586 }
587
588/* used in the init function to setup a constructor */
589#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Neal Norwitzd6d2f2f2007-08-23 20:28:10 +0000590 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000591 if (EVP_get_digestbyname(#NAME)) { \
592 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
593 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
594 } \
595} while (0);
596
597GEN_CONSTRUCTOR(md5)
598GEN_CONSTRUCTOR(sha1)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000599#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000600GEN_CONSTRUCTOR(sha224)
601GEN_CONSTRUCTOR(sha256)
602GEN_CONSTRUCTOR(sha384)
603GEN_CONSTRUCTOR(sha512)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000604#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000605
606/* List of functions exported by this module */
607
608static struct PyMethodDef EVP_functions[] = {
609 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
610 CONSTRUCTOR_METH_DEF(md5),
611 CONSTRUCTOR_METH_DEF(sha1),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000612#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000613 CONSTRUCTOR_METH_DEF(sha224),
614 CONSTRUCTOR_METH_DEF(sha256),
615 CONSTRUCTOR_METH_DEF(sha384),
616 CONSTRUCTOR_METH_DEF(sha512),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000617#endif
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000618 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000619};
620
621
622/* Initialize this module. */
623
Martin v. Löwis1a214512008-06-11 05:26:20 +0000624
625static struct PyModuleDef _hashlibmodule = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000626 PyModuleDef_HEAD_INIT,
627 "_hashlib",
628 NULL,
629 -1,
630 EVP_functions,
631 NULL,
632 NULL,
633 NULL,
634 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000635};
636
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000637PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000638PyInit__hashlib(void)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000639{
Gregory P. Smith13b55292010-09-06 08:30:23 +0000640 PyObject *m, *openssl_md_meth_names;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000641
642 OpenSSL_add_all_digests();
643
644 /* TODO build EVP_functions openssl_* entries dynamically based
645 * on what hashes are supported rather than listing many
646 * but having some be unsupported. Only init appropriate
647 * constants. */
648
Christian Heimes90aa7642007-12-19 02:45:37 +0000649 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000650 if (PyType_Ready(&EVPtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000651 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000652
Martin v. Löwis1a214512008-06-11 05:26:20 +0000653 m = PyModule_Create(&_hashlibmodule);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000654 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000655 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000656
Gregory P. Smith13b55292010-09-06 08:30:23 +0000657 openssl_md_meth_names = generate_hash_name_list();
658 if (openssl_md_meth_names == NULL) {
659 Py_DECREF(m);
660 return NULL;
661 }
662 if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) {
663 Py_DECREF(m);
664 return NULL;
665 }
666
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000667#if HASH_OBJ_CONSTRUCTOR
668 Py_INCREF(&EVPtype);
669 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
670#endif
671
672 /* these constants are used by the convenience constructors */
673 INIT_CONSTRUCTOR_CONSTANTS(md5);
674 INIT_CONSTRUCTOR_CONSTANTS(sha1);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000675#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000676 INIT_CONSTRUCTOR_CONSTANTS(sha224);
677 INIT_CONSTRUCTOR_CONSTANTS(sha256);
678 INIT_CONSTRUCTOR_CONSTANTS(sha384);
679 INIT_CONSTRUCTOR_CONSTANTS(sha512);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000680#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +0000681 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000682}