blob: 887de555d4ea202166ad4398d5fd4cf29648d5bf [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>
41
42#define MUNCH_SIZE INT_MAX
43
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
47
48#ifndef HASH_OBJ_CONSTRUCTOR
49#define HASH_OBJ_CONSTRUCTOR 0
50#endif
51
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000052/* Minimum OpenSSL version needed to support sha224 and higher. */
53#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x00908000)
54#define _OPENSSL_SUPPORTS_SHA2
55#endif
Gregory P. Smith3f61d612009-05-04 00:45:33 +000056
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000057typedef struct {
58 PyObject_HEAD
59 PyObject *name; /* name of this hash algorithm */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000060 EVP_MD_CTX ctx; /* OpenSSL message digest context */
61#ifdef WITH_THREAD
62 PyThread_type_lock lock; /* OpenSSL context lock */
63#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000064} EVPobject;
65
66
67static PyTypeObject EVPtype;
68
69
70#define DEFINE_CONSTS_FOR_NEW(Name) \
71 static PyObject *CONST_ ## Name ## _name_obj; \
72 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
73 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
74
Neal Norwitzf0459142006-01-07 21:20:24 +000075DEFINE_CONSTS_FOR_NEW(md5)
76DEFINE_CONSTS_FOR_NEW(sha1)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000077#ifdef _OPENSSL_SUPPORTS_SHA2
Neal Norwitzf0459142006-01-07 21:20:24 +000078DEFINE_CONSTS_FOR_NEW(sha224)
79DEFINE_CONSTS_FOR_NEW(sha256)
80DEFINE_CONSTS_FOR_NEW(sha384)
81DEFINE_CONSTS_FOR_NEW(sha512)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000082#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000083
84
85static EVPobject *
86newEVPobject(PyObject *name)
87{
88 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
89
90 /* save the name for .name to return */
91 if (retval != NULL) {
92 Py_INCREF(name);
93 retval->name = name;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000094#ifdef WITH_THREAD
95 retval->lock = NULL;
96#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000097 }
98
99 return retval;
100}
101
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000102static void
103EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
104{
105 unsigned int process;
106 const unsigned char *cp = (const unsigned char *)vp;
107 while (0 < len) {
108 if (len > (Py_ssize_t)MUNCH_SIZE)
109 process = MUNCH_SIZE;
110 else
111 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
112 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
113 len -= process;
114 cp += process;
115 }
116}
117
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000118/* Internal methods for a hash object */
119
120static void
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000121EVP_dealloc(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000122{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000123#ifdef WITH_THREAD
124 if (self->lock != NULL)
125 PyThread_free_lock(self->lock);
126#endif
127 EVP_MD_CTX_cleanup(&self->ctx);
128 Py_XDECREF(self->name);
129 PyObject_Del(self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000130}
131
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000132static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
133{
134 ENTER_HASHLIB(self);
135 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
136 LEAVE_HASHLIB(self);
137}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000138
139/* External methods for a hash object */
140
141PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
142
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000143
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000144static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000145EVP_copy(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000146{
147 EVPobject *newobj;
148
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000149 if ( (newobj = newEVPobject(self->name))==NULL)
150 return NULL;
151
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000152 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000153 return (PyObject *)newobj;
154}
155
156PyDoc_STRVAR(EVP_digest__doc__,
157"Return the digest value as a string of binary data.");
158
159static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000160EVP_digest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000161{
162 unsigned char digest[EVP_MAX_MD_SIZE];
163 EVP_MD_CTX temp_ctx;
164 PyObject *retval;
165 unsigned int digest_size;
166
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000167 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000168 digest_size = EVP_MD_CTX_size(&temp_ctx);
Neal Norwitzf0459142006-01-07 21:20:24 +0000169 EVP_DigestFinal(&temp_ctx, digest, NULL);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000170
Christian Heimes72b710a2008-05-26 13:28:38 +0000171 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000172 EVP_MD_CTX_cleanup(&temp_ctx);
173 return retval;
174}
175
176PyDoc_STRVAR(EVP_hexdigest__doc__,
177"Return the digest value as a string of hexadecimal digits.");
178
179static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000180EVP_hexdigest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000181{
182 unsigned char digest[EVP_MAX_MD_SIZE];
183 EVP_MD_CTX temp_ctx;
184 PyObject *retval;
185 char *hex_digest;
186 unsigned int i, j, digest_size;
187
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000188 /* Get the raw (binary) digest value */
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000189 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000190 digest_size = EVP_MD_CTX_size(&temp_ctx);
191 EVP_DigestFinal(&temp_ctx, digest, NULL);
192
193 EVP_MD_CTX_cleanup(&temp_ctx);
194
Guido van Rossumf8953072007-07-10 13:20:29 +0000195 /* Allocate a new buffer */
196 hex_digest = PyMem_Malloc(digest_size * 2 + 1);
197 if (!hex_digest)
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000198 return PyErr_NoMemory();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000199
200 /* Make hex version of the digest */
201 for(i=j=0; i<digest_size; i++) {
202 char c;
203 c = (digest[i] >> 4) & 0xf;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000204 c = (c>9) ? c+'a'-10 : c + '0';
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000205 hex_digest[j++] = c;
206 c = (digest[i] & 0xf);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000207 c = (c>9) ? c+'a'-10 : c + '0';
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000208 hex_digest[j++] = c;
209 }
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
491/*
492 * This macro generates constructor function definitions for specific
493 * hash algorithms. These constructors are much faster than calling
494 * the generic one passing it a python string and are noticably
495 * faster than calling a python new() wrapper. Thats important for
496 * code that wants to make hashes of a bunch of small strings.
497 */
498#define GEN_CONSTRUCTOR(NAME) \
499 static PyObject * \
500 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
501 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000502 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000503 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000504 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000505 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000506 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000507 return NULL; \
508 } \
509 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000510 if (data_obj) \
Gregory P. Smith365a1862009-02-12 07:35:29 +0000511 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000512 \
513 ret_obj = EVPnew( \
514 CONST_ ## NAME ## _name_obj, \
515 NULL, \
516 CONST_new_ ## NAME ## _ctx_p, \
517 (unsigned char*)view.buf, \
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000518 view.len); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000519 \
520 if (data_obj) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000521 PyBuffer_Release(&view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000522 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000523 }
524
525/* a PyMethodDef structure for the constructor */
526#define CONSTRUCTOR_METH_DEF(NAME) \
527 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
528 PyDoc_STR("Returns a " #NAME \
529 " hash object; optionally initialized with a string") \
530 }
531
532/* used in the init function to setup a constructor */
533#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Neal Norwitzd6d2f2f2007-08-23 20:28:10 +0000534 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000535 if (EVP_get_digestbyname(#NAME)) { \
536 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
537 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
538 } \
539} while (0);
540
541GEN_CONSTRUCTOR(md5)
542GEN_CONSTRUCTOR(sha1)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000543#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000544GEN_CONSTRUCTOR(sha224)
545GEN_CONSTRUCTOR(sha256)
546GEN_CONSTRUCTOR(sha384)
547GEN_CONSTRUCTOR(sha512)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000548#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000549
550/* List of functions exported by this module */
551
552static struct PyMethodDef EVP_functions[] = {
553 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
554 CONSTRUCTOR_METH_DEF(md5),
555 CONSTRUCTOR_METH_DEF(sha1),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000556#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000557 CONSTRUCTOR_METH_DEF(sha224),
558 CONSTRUCTOR_METH_DEF(sha256),
559 CONSTRUCTOR_METH_DEF(sha384),
560 CONSTRUCTOR_METH_DEF(sha512),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000561#endif
562 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000563};
564
565
566/* Initialize this module. */
567
Martin v. Löwis1a214512008-06-11 05:26:20 +0000568
569static struct PyModuleDef _hashlibmodule = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000570 PyModuleDef_HEAD_INIT,
571 "_hashlib",
572 NULL,
573 -1,
574 EVP_functions,
575 NULL,
576 NULL,
577 NULL,
578 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000579};
580
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000581PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000582PyInit__hashlib(void)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000583{
584 PyObject *m;
585
586 OpenSSL_add_all_digests();
587
588 /* TODO build EVP_functions openssl_* entries dynamically based
589 * on what hashes are supported rather than listing many
590 * but having some be unsupported. Only init appropriate
591 * constants. */
592
Christian Heimes90aa7642007-12-19 02:45:37 +0000593 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000594 if (PyType_Ready(&EVPtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000595 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000596
Martin v. Löwis1a214512008-06-11 05:26:20 +0000597 m = PyModule_Create(&_hashlibmodule);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000598 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000599 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000600
601#if HASH_OBJ_CONSTRUCTOR
602 Py_INCREF(&EVPtype);
603 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
604#endif
605
606 /* these constants are used by the convenience constructors */
607 INIT_CONSTRUCTOR_CONSTANTS(md5);
608 INIT_CONSTRUCTOR_CONSTANTS(sha1);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000609#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000610 INIT_CONSTRUCTOR_CONSTANTS(sha224);
611 INIT_CONSTRUCTOR_CONSTANTS(sha256);
612 INIT_CONSTRUCTOR_CONSTANTS(sha384);
613 INIT_CONSTRUCTOR_CONSTANTS(sha512);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000614#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +0000615 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000616}