blob: 0dae5158d51665b10b218f2b772623cd7573041e [file] [log] [blame]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001/* Module that wraps all OpenSSL hash algorithms */
2
3/*
Gregory P. Smith3f61d612009-05-04 00:45:33 +00004 * Copyright (C) 2005-2009 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
52
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000053typedef struct {
54 PyObject_HEAD
55 PyObject *name; /* name of this hash algorithm */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000056 EVP_MD_CTX ctx; /* OpenSSL message digest context */
57#ifdef WITH_THREAD
58 PyThread_type_lock lock; /* OpenSSL context lock */
59#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000060} EVPobject;
61
62
63static PyTypeObject EVPtype;
64
65
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;
70
Neal Norwitzf0459142006-01-07 21:20:24 +000071DEFINE_CONSTS_FOR_NEW(md5)
72DEFINE_CONSTS_FOR_NEW(sha1)
73DEFINE_CONSTS_FOR_NEW(sha224)
74DEFINE_CONSTS_FOR_NEW(sha256)
75DEFINE_CONSTS_FOR_NEW(sha384)
76DEFINE_CONSTS_FOR_NEW(sha512)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000077
78
79static EVPobject *
80newEVPobject(PyObject *name)
81{
82 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
83
84 /* save the name for .name to return */
85 if (retval != NULL) {
86 Py_INCREF(name);
87 retval->name = name;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000088#ifdef WITH_THREAD
89 retval->lock = NULL;
90#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000091 }
92
93 return retval;
94}
95
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000096static void
97EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
98{
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;
109 }
110}
111
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000112/* Internal methods for a hash object */
113
114static void
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000115EVP_dealloc(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000116{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000117#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);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000124}
125
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000126static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
127{
128 ENTER_HASHLIB(self);
129 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
130 LEAVE_HASHLIB(self);
131}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000132
133/* External methods for a hash object */
134
135PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
136
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000137
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000138static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000139EVP_copy(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000140{
141 EVPobject *newobj;
142
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000143 if ( (newobj = newEVPobject(self->name))==NULL)
144 return NULL;
145
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000146 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000147 return (PyObject *)newobj;
148}
149
150PyDoc_STRVAR(EVP_digest__doc__,
151"Return the digest value as a string of binary data.");
152
153static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000154EVP_digest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000155{
156 unsigned char digest[EVP_MAX_MD_SIZE];
157 EVP_MD_CTX temp_ctx;
158 PyObject *retval;
159 unsigned int digest_size;
160
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000161 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000162 digest_size = EVP_MD_CTX_size(&temp_ctx);
Neal Norwitzf0459142006-01-07 21:20:24 +0000163 EVP_DigestFinal(&temp_ctx, digest, NULL);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000164
Christian Heimes72b710a2008-05-26 13:28:38 +0000165 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000166 EVP_MD_CTX_cleanup(&temp_ctx);
167 return retval;
168}
169
170PyDoc_STRVAR(EVP_hexdigest__doc__,
171"Return the digest value as a string of hexadecimal digits.");
172
173static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000174EVP_hexdigest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000175{
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;
181
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000182 /* Get the raw (binary) digest value */
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000183 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000184 digest_size = EVP_MD_CTX_size(&temp_ctx);
185 EVP_DigestFinal(&temp_ctx, digest, NULL);
186
187 EVP_MD_CTX_cleanup(&temp_ctx);
188
Guido van Rossumf8953072007-07-10 13:20:29 +0000189 /* Allocate a new buffer */
190 hex_digest = PyMem_Malloc(digest_size * 2 + 1);
191 if (!hex_digest)
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000192 return PyErr_NoMemory();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000193
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;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000198 c = (c>9) ? c+'a'-10 : c + '0';
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000199 hex_digest[j++] = c;
200 c = (digest[i] & 0xf);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000201 c = (c>9) ? c+'a'-10 : c + '0';
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000202 hex_digest[j++] = c;
203 }
Guido van Rossumf8953072007-07-10 13:20:29 +0000204 retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2);
205 PyMem_Free(hex_digest);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000206 return retval;
207}
208
209PyDoc_STRVAR(EVP_update__doc__,
210"Update this hash object's state with the provided string.");
211
212static PyObject *
213EVP_update(EVPobject *self, PyObject *args)
214{
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000215 PyObject *obj;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000216 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000217
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000218 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000219 return NULL;
220
Gregory P. Smith365a1862009-02-12 07:35:29 +0000221 GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000222
223#ifdef WITH_THREAD
224 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
225 self->lock = PyThread_allocate_lock();
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000226 /* fail? lock = NULL and we fail over to non-threaded code. */
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000227 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000228
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000229 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);
237 }
238#else
239 EVP_hash(self, view.buf, view.len);
240#endif
241
242 PyBuffer_Release(&view);
243 Py_RETURN_NONE;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000244}
245
246static PyMethodDef EVP_methods[] = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000247 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
248 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000249 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000250 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
251 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000252};
253
254static PyObject *
255EVP_get_block_size(EVPobject *self, void *closure)
256{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000257 long block_size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000258 block_size = EVP_MD_CTX_block_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000259 return PyLong_FromLong(block_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000260}
261
262static PyObject *
263EVP_get_digest_size(EVPobject *self, void *closure)
264{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000265 long size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000266 size = EVP_MD_CTX_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000267 return PyLong_FromLong(size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000268}
269
270static PyMemberDef EVP_members[] = {
271 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
272 {NULL} /* Sentinel */
273};
274
275static 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 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
285 * the old sha module also supported 'digestsize'. ugh. */
286 {"digestsize",
287 (getter)EVP_get_digest_size, NULL,
288 NULL,
289 NULL},
290 {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{
297 char buf[100];
298 PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000299 _PyUnicode_AsString(self->name), self);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000300 return PyUnicode_FromString(buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000301}
302
303#if HASH_OBJ_CONSTRUCTOR
304static int
305EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
306{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000307 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000308 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000309 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000310 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000311 char *nameStr;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000312 const EVP_MD *digest;
313
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000314 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
315 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000316 return -1;
317 }
318
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000319 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000320 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000321
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000322 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
323 PyErr_SetString(PyExc_TypeError, "name must be a string");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000324 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000325 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000326 return -1;
327 }
328
329 digest = EVP_get_digestbyname(nameStr);
330 if (!digest) {
331 PyErr_SetString(PyExc_ValueError, "unknown hash function");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000332 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000333 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000334 return -1;
335 }
336 EVP_DigestInit(&self->ctx, digest);
337
338 self->name = name_obj;
339 Py_INCREF(self->name);
340
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000341 if (data_obj) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000342 if (view.len >= HASHLIB_GIL_MINSIZE) {
343 Py_BEGIN_ALLOW_THREADS
344 EVP_hash(self, view.buf, view.len);
345 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000346 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000347 EVP_hash(self, view.buf, view.len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000348 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000349 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000350 }
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000351
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000352 return 0;
353}
354#endif
355
356
357PyDoc_STRVAR(hashtype_doc,
358"A hash represents the object used to calculate a checksum of a\n\
359string of information.\n\
360\n\
361Methods:\n\
362\n\
363update() -- updates the current digest with an additional string\n\
364digest() -- return the current digest value\n\
365hexdigest() -- return the current digest as a string of hexadecimal digits\n\
366copy() -- return a copy of the current hash object\n\
367\n\
368Attributes:\n\
369\n\
370name -- the hash algorithm being used by this object\n\
371digest_size -- number of bytes in this hashes output\n");
372
373static PyTypeObject EVPtype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000374 PyVarObject_HEAD_INIT(NULL, 0)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000375 "_hashlib.HASH", /*tp_name*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000376 sizeof(EVPobject), /*tp_basicsize*/
377 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000378 /* methods */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000379 (destructor)EVP_dealloc, /*tp_dealloc*/
380 0, /*tp_print*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000381 0, /*tp_getattr*/
382 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000383 0, /*tp_reserved*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000384 (reprfunc)EVP_repr, /*tp_repr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000385 0, /*tp_as_number*/
386 0, /*tp_as_sequence*/
387 0, /*tp_as_mapping*/
388 0, /*tp_hash*/
389 0, /*tp_call*/
390 0, /*tp_str*/
391 0, /*tp_getattro*/
392 0, /*tp_setattro*/
393 0, /*tp_as_buffer*/
394 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
395 hashtype_doc, /*tp_doc*/
396 0, /*tp_traverse*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000397 0, /*tp_clear*/
398 0, /*tp_richcompare*/
399 0, /*tp_weaklistoffset*/
400 0, /*tp_iter*/
401 0, /*tp_iternext*/
402 EVP_methods, /* tp_methods */
403 EVP_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000404 EVP_getseters, /* tp_getset */
405#if 1
406 0, /* tp_base */
407 0, /* tp_dict */
408 0, /* tp_descr_get */
409 0, /* tp_descr_set */
410 0, /* tp_dictoffset */
411#endif
412#if HASH_OBJ_CONSTRUCTOR
413 (initproc)EVP_tp_init, /* tp_init */
414#endif
415};
416
417static PyObject *
418EVPnew(PyObject *name_obj,
419 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000420 const unsigned char *cp, Py_ssize_t len)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000421{
422 EVPobject *self;
423
424 if (!digest && !initial_ctx) {
425 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
426 return NULL;
427 }
428
429 if ((self = newEVPobject(name_obj)) == NULL)
430 return NULL;
431
432 if (initial_ctx) {
433 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
434 } else {
435 EVP_DigestInit(&self->ctx, digest);
436 }
437
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000438 if (cp && len) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000439 if (len >= HASHLIB_GIL_MINSIZE) {
440 Py_BEGIN_ALLOW_THREADS
441 EVP_hash(self, cp, len);
442 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000443 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000444 EVP_hash(self, cp, len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000445 }
446 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000447
448 return (PyObject *)self;
449}
450
451
452/* The module-level function: new() */
453
454PyDoc_STRVAR(EVP_new__doc__,
455"Return a new hash object using the named algorithm.\n\
456An optional string argument may be provided and will be\n\
457automatically hashed.\n\
458\n\
459The MD5 and SHA1 algorithms are always supported.\n");
460
461static PyObject *
462EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
463{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000464 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000465 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000466 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000467 Py_buffer view = { 0 };
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000468 PyObject *ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000469 char *name;
470 const EVP_MD *digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000471
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000472 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
473 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000474 return NULL;
475 }
476
477 if (!PyArg_Parse(name_obj, "s", &name)) {
478 PyErr_SetString(PyExc_TypeError, "name must be a string");
479 return NULL;
480 }
481
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000482 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000483 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000484
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000485 digest = EVP_get_digestbyname(name);
486
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000487 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000488
489 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000490 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000491 return ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000492}
493
494/*
495 * This macro generates constructor function definitions for specific
496 * hash algorithms. These constructors are much faster than calling
497 * the generic one passing it a python string and are noticably
498 * faster than calling a python new() wrapper. Thats important for
499 * code that wants to make hashes of a bunch of small strings.
500 */
501#define GEN_CONSTRUCTOR(NAME) \
502 static PyObject * \
503 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
504 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000505 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000506 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000507 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000508 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000509 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000510 return NULL; \
511 } \
512 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000513 if (data_obj) \
Gregory P. Smith365a1862009-02-12 07:35:29 +0000514 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000515 \
516 ret_obj = EVPnew( \
517 CONST_ ## NAME ## _name_obj, \
518 NULL, \
519 CONST_new_ ## NAME ## _ctx_p, \
520 (unsigned char*)view.buf, \
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000521 view.len); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000522 \
523 if (data_obj) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000524 PyBuffer_Release(&view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000525 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000526 }
527
528/* a PyMethodDef structure for the constructor */
529#define CONSTRUCTOR_METH_DEF(NAME) \
530 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
531 PyDoc_STR("Returns a " #NAME \
532 " hash object; optionally initialized with a string") \
533 }
534
535/* used in the init function to setup a constructor */
536#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Neal Norwitzd6d2f2f2007-08-23 20:28:10 +0000537 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000538 if (EVP_get_digestbyname(#NAME)) { \
539 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
540 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
541 } \
542} while (0);
543
544GEN_CONSTRUCTOR(md5)
545GEN_CONSTRUCTOR(sha1)
546GEN_CONSTRUCTOR(sha224)
547GEN_CONSTRUCTOR(sha256)
548GEN_CONSTRUCTOR(sha384)
549GEN_CONSTRUCTOR(sha512)
550
551/* List of functions exported by this module */
552
553static struct PyMethodDef EVP_functions[] = {
554 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
555 CONSTRUCTOR_METH_DEF(md5),
556 CONSTRUCTOR_METH_DEF(sha1),
557 CONSTRUCTOR_METH_DEF(sha224),
558 CONSTRUCTOR_METH_DEF(sha256),
559 CONSTRUCTOR_METH_DEF(sha384),
560 CONSTRUCTOR_METH_DEF(sha512),
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000561 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000562};
563
564
565/* Initialize this module. */
566
Martin v. Löwis1a214512008-06-11 05:26:20 +0000567
568static struct PyModuleDef _hashlibmodule = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000569 PyModuleDef_HEAD_INIT,
570 "_hashlib",
571 NULL,
572 -1,
573 EVP_functions,
574 NULL,
575 NULL,
576 NULL,
577 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000578};
579
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000580PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000581PyInit__hashlib(void)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000582{
583 PyObject *m;
584
585 OpenSSL_add_all_digests();
586
587 /* TODO build EVP_functions openssl_* entries dynamically based
588 * on what hashes are supported rather than listing many
589 * but having some be unsupported. Only init appropriate
590 * constants. */
591
Christian Heimes90aa7642007-12-19 02:45:37 +0000592 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000593 if (PyType_Ready(&EVPtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000594 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000595
Martin v. Löwis1a214512008-06-11 05:26:20 +0000596 m = PyModule_Create(&_hashlibmodule);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000597 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000598 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000599
600#if HASH_OBJ_CONSTRUCTOR
601 Py_INCREF(&EVPtype);
602 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
603#endif
604
605 /* these constants are used by the convenience constructors */
606 INIT_CONSTRUCTOR_CONSTANTS(md5);
607 INIT_CONSTRUCTOR_CONSTANTS(sha1);
608 INIT_CONSTRUCTOR_CONSTANTS(sha224);
609 INIT_CONSTRUCTOR_CONSTANTS(sha256);
610 INIT_CONSTRUCTOR_CONSTANTS(sha384);
611 INIT_CONSTRUCTOR_CONSTANTS(sha512);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000612 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000613}