blob: ee149e117b1470c2e3c504051ac06592ad8e7580 [file] [log] [blame]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001/* Module that wraps all OpenSSL hash algorithms */
2
3/*
Benjamin Petersonffeda292010-01-09 18:48:46 +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
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},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000284 {NULL} /* Sentinel */
285};
286
287
288static PyObject *
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000289EVP_repr(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000290{
Victor Stinner38c36f82010-03-21 21:05:53 +0000291 return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000292}
293
294#if HASH_OBJ_CONSTRUCTOR
295static int
296EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
297{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000298 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000299 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000300 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000301 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000302 char *nameStr;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000303 const EVP_MD *digest;
304
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000305 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
306 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000307 return -1;
308 }
309
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000310 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000311 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000312
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000313 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
314 PyErr_SetString(PyExc_TypeError, "name must be a string");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000315 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000316 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000317 return -1;
318 }
319
320 digest = EVP_get_digestbyname(nameStr);
321 if (!digest) {
322 PyErr_SetString(PyExc_ValueError, "unknown hash function");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000323 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000324 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000325 return -1;
326 }
327 EVP_DigestInit(&self->ctx, digest);
328
329 self->name = name_obj;
330 Py_INCREF(self->name);
331
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000332 if (data_obj) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000333 if (view.len >= HASHLIB_GIL_MINSIZE) {
334 Py_BEGIN_ALLOW_THREADS
335 EVP_hash(self, view.buf, view.len);
336 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000337 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000338 EVP_hash(self, view.buf, view.len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000339 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000340 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000341 }
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000342
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000343 return 0;
344}
345#endif
346
347
348PyDoc_STRVAR(hashtype_doc,
349"A hash represents the object used to calculate a checksum of a\n\
350string of information.\n\
351\n\
352Methods:\n\
353\n\
354update() -- updates the current digest with an additional string\n\
355digest() -- return the current digest value\n\
356hexdigest() -- return the current digest as a string of hexadecimal digits\n\
357copy() -- return a copy of the current hash object\n\
358\n\
359Attributes:\n\
360\n\
361name -- the hash algorithm being used by this object\n\
362digest_size -- number of bytes in this hashes output\n");
363
364static PyTypeObject EVPtype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000365 PyVarObject_HEAD_INIT(NULL, 0)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000366 "_hashlib.HASH", /*tp_name*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000367 sizeof(EVPobject), /*tp_basicsize*/
368 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000369 /* methods */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000370 (destructor)EVP_dealloc, /*tp_dealloc*/
371 0, /*tp_print*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000372 0, /*tp_getattr*/
373 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000374 0, /*tp_reserved*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000375 (reprfunc)EVP_repr, /*tp_repr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000376 0, /*tp_as_number*/
377 0, /*tp_as_sequence*/
378 0, /*tp_as_mapping*/
379 0, /*tp_hash*/
380 0, /*tp_call*/
381 0, /*tp_str*/
382 0, /*tp_getattro*/
383 0, /*tp_setattro*/
384 0, /*tp_as_buffer*/
385 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
386 hashtype_doc, /*tp_doc*/
387 0, /*tp_traverse*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000388 0, /*tp_clear*/
389 0, /*tp_richcompare*/
390 0, /*tp_weaklistoffset*/
391 0, /*tp_iter*/
392 0, /*tp_iternext*/
393 EVP_methods, /* tp_methods */
394 EVP_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000395 EVP_getseters, /* tp_getset */
396#if 1
397 0, /* tp_base */
398 0, /* tp_dict */
399 0, /* tp_descr_get */
400 0, /* tp_descr_set */
401 0, /* tp_dictoffset */
402#endif
403#if HASH_OBJ_CONSTRUCTOR
404 (initproc)EVP_tp_init, /* tp_init */
405#endif
406};
407
408static PyObject *
409EVPnew(PyObject *name_obj,
410 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000411 const unsigned char *cp, Py_ssize_t len)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000412{
413 EVPobject *self;
414
415 if (!digest && !initial_ctx) {
416 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
417 return NULL;
418 }
419
420 if ((self = newEVPobject(name_obj)) == NULL)
421 return NULL;
422
423 if (initial_ctx) {
424 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
425 } else {
426 EVP_DigestInit(&self->ctx, digest);
427 }
428
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000429 if (cp && len) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000430 if (len >= HASHLIB_GIL_MINSIZE) {
431 Py_BEGIN_ALLOW_THREADS
432 EVP_hash(self, cp, len);
433 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000434 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000435 EVP_hash(self, cp, len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000436 }
437 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000438
439 return (PyObject *)self;
440}
441
442
443/* The module-level function: new() */
444
445PyDoc_STRVAR(EVP_new__doc__,
446"Return a new hash object using the named algorithm.\n\
447An optional string argument may be provided and will be\n\
448automatically hashed.\n\
449\n\
450The MD5 and SHA1 algorithms are always supported.\n");
451
452static PyObject *
453EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
454{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000455 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000456 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000457 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000458 Py_buffer view = { 0 };
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000459 PyObject *ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000460 char *name;
461 const EVP_MD *digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000462
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000463 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
464 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000465 return NULL;
466 }
467
468 if (!PyArg_Parse(name_obj, "s", &name)) {
469 PyErr_SetString(PyExc_TypeError, "name must be a string");
470 return NULL;
471 }
472
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000473 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000474 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000475
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000476 digest = EVP_get_digestbyname(name);
477
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000478 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000479
480 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000481 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000482 return ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000483}
484
485/*
486 * This macro generates constructor function definitions for specific
487 * hash algorithms. These constructors are much faster than calling
488 * the generic one passing it a python string and are noticably
489 * faster than calling a python new() wrapper. Thats important for
490 * code that wants to make hashes of a bunch of small strings.
491 */
492#define GEN_CONSTRUCTOR(NAME) \
493 static PyObject * \
494 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
495 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000496 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000497 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000498 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000499 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000500 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000501 return NULL; \
502 } \
503 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000504 if (data_obj) \
Gregory P. Smith365a1862009-02-12 07:35:29 +0000505 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000506 \
507 ret_obj = EVPnew( \
508 CONST_ ## NAME ## _name_obj, \
509 NULL, \
510 CONST_new_ ## NAME ## _ctx_p, \
511 (unsigned char*)view.buf, \
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000512 view.len); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000513 \
514 if (data_obj) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000515 PyBuffer_Release(&view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000516 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000517 }
518
519/* a PyMethodDef structure for the constructor */
520#define CONSTRUCTOR_METH_DEF(NAME) \
521 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
522 PyDoc_STR("Returns a " #NAME \
523 " hash object; optionally initialized with a string") \
524 }
525
526/* used in the init function to setup a constructor */
527#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Neal Norwitzd6d2f2f2007-08-23 20:28:10 +0000528 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000529 if (EVP_get_digestbyname(#NAME)) { \
530 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
531 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
532 } \
533} while (0);
534
535GEN_CONSTRUCTOR(md5)
536GEN_CONSTRUCTOR(sha1)
537GEN_CONSTRUCTOR(sha224)
538GEN_CONSTRUCTOR(sha256)
539GEN_CONSTRUCTOR(sha384)
540GEN_CONSTRUCTOR(sha512)
541
542/* List of functions exported by this module */
543
544static struct PyMethodDef EVP_functions[] = {
545 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
546 CONSTRUCTOR_METH_DEF(md5),
547 CONSTRUCTOR_METH_DEF(sha1),
548 CONSTRUCTOR_METH_DEF(sha224),
549 CONSTRUCTOR_METH_DEF(sha256),
550 CONSTRUCTOR_METH_DEF(sha384),
551 CONSTRUCTOR_METH_DEF(sha512),
Benjamin Petersonc8f55402010-03-18 21:27:29 +0000552 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000553};
554
555
556/* Initialize this module. */
557
Martin v. Löwis1a214512008-06-11 05:26:20 +0000558
559static struct PyModuleDef _hashlibmodule = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000560 PyModuleDef_HEAD_INIT,
561 "_hashlib",
562 NULL,
563 -1,
564 EVP_functions,
565 NULL,
566 NULL,
567 NULL,
568 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000569};
570
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000571PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000572PyInit__hashlib(void)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000573{
574 PyObject *m;
575
576 OpenSSL_add_all_digests();
577
578 /* TODO build EVP_functions openssl_* entries dynamically based
579 * on what hashes are supported rather than listing many
580 * but having some be unsupported. Only init appropriate
581 * constants. */
582
Christian Heimes90aa7642007-12-19 02:45:37 +0000583 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000584 if (PyType_Ready(&EVPtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000585 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000586
Martin v. Löwis1a214512008-06-11 05:26:20 +0000587 m = PyModule_Create(&_hashlibmodule);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000588 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000589 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000590
591#if HASH_OBJ_CONSTRUCTOR
592 Py_INCREF(&EVPtype);
593 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
594#endif
595
596 /* these constants are used by the convenience constructors */
597 INIT_CONSTRUCTOR_CONSTANTS(md5);
598 INIT_CONSTRUCTOR_CONSTANTS(sha1);
599 INIT_CONSTRUCTOR_CONSTANTS(sha224);
600 INIT_CONSTRUCTOR_CONSTANTS(sha256);
601 INIT_CONSTRUCTOR_CONSTANTS(sha384);
602 INIT_CONSTRUCTOR_CONSTANTS(sha512);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000603 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000604}