blob: ba18aa27edec9d622548664c22102865d8f8ae94 [file] [log] [blame]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001/* Module that wraps all OpenSSL hash algorithms */
2
3/*
Gregory P. Smithd02eeda2009-05-04 00:16:49 +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. Smithea388262009-02-13 03:00:00 +000018#include "hashlib.h"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000019
Gregory P. Smithd02eeda2009-05-04 00:16:49 +000020#ifdef WITH_THREAD
21#include "pythread.h"
22 #define ENTER_HASHLIB(obj) \
Gregory P. Smitheeb51a92009-05-04 00:48:41 +000023 if ((obj)->lock) { \
24 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
Gregory P. Smithd02eeda2009-05-04 00:16:49 +000025 Py_BEGIN_ALLOW_THREADS \
26 PyThread_acquire_lock((obj)->lock, 1); \
27 Py_END_ALLOW_THREADS \
28 } \
29 }
30 #define LEAVE_HASHLIB(obj) \
Gregory P. Smitheeb51a92009-05-04 00:48:41 +000031 if ((obj)->lock) { \
Gregory P. Smithd02eeda2009-05-04 00:16:49 +000032 PyThread_release_lock((obj)->lock); \
33 }
34#else
35 #define ENTER_HASHLIB(obj)
36 #define LEAVE_HASHLIB(obj)
37#endif
38
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000039/* EVP is the preferred interface to hashing in OpenSSL */
40#include <openssl/evp.h>
41
Benjamin Peterson8c2b7dc2008-09-18 01:22:16 +000042#define MUNCH_SIZE INT_MAX
43
Gregory P. Smithd02eeda2009-05-04 00:16:49 +000044/* 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
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000047
Neal Norwitze4ab5f52006-01-08 01:08:09 +000048#ifndef HASH_OBJ_CONSTRUCTOR
49#define HASH_OBJ_CONSTRUCTOR 0
50#endif
51
Gregory P. Smithd02eeda2009-05-04 00:16:49 +000052
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000053typedef struct {
54 PyObject_HEAD
55 PyObject *name; /* name of this hash algorithm */
56 EVP_MD_CTX ctx; /* OpenSSL message digest context */
Gregory P. Smithd02eeda2009-05-04 00:16:49 +000057#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;
Gregory P. Smithd02eeda2009-05-04 00:16:49 +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
Gregory P. Smithd02eeda2009-05-04 00:16:49 +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 {
103 if (len > (Py_ssize_t)MUNCH_SIZE)
104 process = MUNCH_SIZE;
105 else
106 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
107 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
108 len -= process;
109 cp += process;
110 }
111}
112
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000113/* Internal methods for a hash object */
114
115static void
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000116EVP_dealloc(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000117{
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000118#ifdef WITH_THREAD
119 if (self->lock != NULL)
120 PyThread_free_lock(self->lock);
121#endif
122 EVP_MD_CTX_cleanup(&self->ctx);
123 Py_XDECREF(self->name);
124 PyObject_Del(self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000125}
126
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000127static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
128{
129 ENTER_HASHLIB(self);
130 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
131 LEAVE_HASHLIB(self);
132}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000133
134/* External methods for a hash object */
135
136PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
137
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000138
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000139static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000140EVP_copy(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000141{
142 EVPobject *newobj;
143
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000144 if ( (newobj = newEVPobject(self->name))==NULL)
145 return NULL;
146
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000147 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000148 return (PyObject *)newobj;
149}
150
151PyDoc_STRVAR(EVP_digest__doc__,
152"Return the digest value as a string of binary data.");
153
154static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000155EVP_digest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000156{
157 unsigned char digest[EVP_MAX_MD_SIZE];
158 EVP_MD_CTX temp_ctx;
159 PyObject *retval;
160 unsigned int digest_size;
161
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000162 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000163 digest_size = EVP_MD_CTX_size(&temp_ctx);
Neal Norwitzf0459142006-01-07 21:20:24 +0000164 EVP_DigestFinal(&temp_ctx, digest, NULL);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000165
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000166 retval = PyString_FromStringAndSize((const char *)digest, digest_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000167 EVP_MD_CTX_cleanup(&temp_ctx);
168 return retval;
169}
170
171PyDoc_STRVAR(EVP_hexdigest__doc__,
172"Return the digest value as a string of hexadecimal digits.");
173
174static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000175EVP_hexdigest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000176{
177 unsigned char digest[EVP_MAX_MD_SIZE];
178 EVP_MD_CTX temp_ctx;
179 PyObject *retval;
180 char *hex_digest;
181 unsigned int i, j, digest_size;
182
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000183 /* Get the raw (binary) digest value */
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000184 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000185 digest_size = EVP_MD_CTX_size(&temp_ctx);
186 EVP_DigestFinal(&temp_ctx, digest, NULL);
187
188 EVP_MD_CTX_cleanup(&temp_ctx);
189
190 /* Create a new string */
191 /* NOTE: not thread safe! modifying an already created string object */
192 /* (not a problem because we hold the GIL by default) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000193 retval = PyString_FromStringAndSize(NULL, digest_size * 2);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000194 if (!retval)
195 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000196 hex_digest = PyString_AsString(retval);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000197 if (!hex_digest) {
198 Py_DECREF(retval);
199 return NULL;
200 }
201
202 /* Make hex version of the digest */
203 for(i=j=0; i<digest_size; i++) {
204 char c;
205 c = (digest[i] >> 4) & 0xf;
206 c = (c>9) ? c+'a'-10 : c + '0';
207 hex_digest[j++] = c;
208 c = (digest[i] & 0xf);
209 c = (c>9) ? c+'a'-10 : c + '0';
210 hex_digest[j++] = c;
211 }
212 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. Smithea388262009-02-13 03:00:00 +0000221 PyObject *obj;
222 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000223
Gregory P. Smithea388262009-02-13 03:00:00 +0000224 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000225 return NULL;
226
Gregory P. Smithea388262009-02-13 03:00:00 +0000227 GET_BUFFER_VIEW_OR_ERROUT(obj, &view, NULL);
228
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000229#ifdef WITH_THREAD
Gregory P. Smitheeb51a92009-05-04 00:48:41 +0000230 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000231 self->lock = PyThread_allocate_lock();
232 /* fail? lock = NULL and we fail over to non-threaded code. */
Benjamin Peterson8c2b7dc2008-09-18 01:22:16 +0000233 }
Gregory P. Smithea388262009-02-13 03:00:00 +0000234
Gregory P. Smitheeb51a92009-05-04 00:48:41 +0000235 if (self->lock != NULL) {
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000236 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
Gregory P. Smithea388262009-02-13 03:00:00 +0000248 PyBuffer_Release(&view);
249
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000250 Py_INCREF(Py_None);
251 return Py_None;
252}
253
254static PyMethodDef EVP_methods[] = {
255 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
Georg Brandl96a8c392006-05-29 21:04:52 +0000256 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
257 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
258 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000259 {NULL, NULL} /* sentinel */
260};
261
262static PyObject *
263EVP_get_block_size(EVPobject *self, void *closure)
264{
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000265 long block_size;
266 block_size = EVP_MD_CTX_block_size(&self->ctx);
267 return PyLong_FromLong(block_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000268}
269
270static PyObject *
271EVP_get_digest_size(EVPobject *self, void *closure)
272{
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000273 long size;
274 size = EVP_MD_CTX_size(&self->ctx);
275 return PyLong_FromLong(size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000276}
277
278static PyMemberDef EVP_members[] = {
279 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
280 {NULL} /* Sentinel */
281};
282
283static PyGetSetDef EVP_getseters[] = {
284 {"digest_size",
285 (getter)EVP_get_digest_size, NULL,
286 NULL,
287 NULL},
288 {"block_size",
289 (getter)EVP_get_block_size, NULL,
290 NULL,
291 NULL},
292 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
293 * the old sha module also supported 'digestsize'. ugh. */
294 {"digestsize",
295 (getter)EVP_get_digest_size, NULL,
296 NULL,
297 NULL},
298 {NULL} /* Sentinel */
299};
300
301
302static PyObject *
303EVP_repr(PyObject *self)
304{
305 char buf[100];
306 PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000307 PyString_AsString(((EVPobject *)self)->name), self);
308 return PyString_FromString(buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000309}
310
311#if HASH_OBJ_CONSTRUCTOR
312static int
313EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
314{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000315 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000316 PyObject *name_obj = NULL;
Gregory P. Smithea388262009-02-13 03:00:00 +0000317 PyObject *data_obj = NULL;
318 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000319 char *nameStr;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000320 const EVP_MD *digest;
321
Gregory P. Smithea388262009-02-13 03:00:00 +0000322 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
323 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000324 return -1;
325 }
326
Gregory P. Smithea388262009-02-13 03:00:00 +0000327 if (data_obj)
328 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, -1);
329
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000330 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
331 PyErr_SetString(PyExc_TypeError, "name must be a string");
Gregory P. Smithea388262009-02-13 03:00:00 +0000332 if (data_obj)
333 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000334 return -1;
335 }
336
337 digest = EVP_get_digestbyname(nameStr);
338 if (!digest) {
339 PyErr_SetString(PyExc_ValueError, "unknown hash function");
Gregory P. Smithea388262009-02-13 03:00:00 +0000340 if (data_obj)
341 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000342 return -1;
343 }
344 EVP_DigestInit(&self->ctx, digest);
345
346 self->name = name_obj;
347 Py_INCREF(self->name);
348
Gregory P. Smithea388262009-02-13 03:00:00 +0000349 if (data_obj) {
Gregory P. Smitheeb51a92009-05-04 00:48:41 +0000350 if (view.len >= HASHLIB_GIL_MINSIZE) {
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000351 Py_BEGIN_ALLOW_THREADS
352 EVP_hash(self, view.buf, view.len);
353 Py_END_ALLOW_THREADS
Benjamin Peterson8c2b7dc2008-09-18 01:22:16 +0000354 } else {
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000355 EVP_hash(self, view.buf, view.len);
Gregory P. Smitheeb51a92009-05-04 00:48:41 +0000356 }
Gregory P. Smithea388262009-02-13 03:00:00 +0000357 PyBuffer_Release(&view);
Benjamin Peterson8c2b7dc2008-09-18 01:22:16 +0000358 }
Gregory P. Smithea388262009-02-13 03:00:00 +0000359
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000360 return 0;
361}
362#endif
363
364
365PyDoc_STRVAR(hashtype_doc,
366"A hash represents the object used to calculate a checksum of a\n\
367string of information.\n\
368\n\
369Methods:\n\
370\n\
371update() -- updates the current digest with an additional string\n\
372digest() -- return the current digest value\n\
373hexdigest() -- return the current digest as a string of hexadecimal digits\n\
374copy() -- return a copy of the current hash object\n\
375\n\
376Attributes:\n\
377\n\
378name -- the hash algorithm being used by this object\n\
379digest_size -- number of bytes in this hashes output\n");
380
381static PyTypeObject EVPtype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000382 PyVarObject_HEAD_INIT(NULL, 0)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000383 "_hashlib.HASH", /*tp_name*/
384 sizeof(EVPobject), /*tp_basicsize*/
385 0, /*tp_itemsize*/
386 /* methods */
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000387 (destructor)EVP_dealloc, /*tp_dealloc*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000388 0, /*tp_print*/
389 0, /*tp_getattr*/
390 0, /*tp_setattr*/
391 0, /*tp_compare*/
392 EVP_repr, /*tp_repr*/
393 0, /*tp_as_number*/
394 0, /*tp_as_sequence*/
395 0, /*tp_as_mapping*/
396 0, /*tp_hash*/
397 0, /*tp_call*/
398 0, /*tp_str*/
399 0, /*tp_getattro*/
400 0, /*tp_setattro*/
401 0, /*tp_as_buffer*/
402 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
403 hashtype_doc, /*tp_doc*/
404 0, /*tp_traverse*/
405 0, /*tp_clear*/
406 0, /*tp_richcompare*/
407 0, /*tp_weaklistoffset*/
408 0, /*tp_iter*/
409 0, /*tp_iternext*/
410 EVP_methods, /* tp_methods */
411 EVP_members, /* tp_members */
412 EVP_getseters, /* tp_getset */
413#if 1
414 0, /* tp_base */
415 0, /* tp_dict */
416 0, /* tp_descr_get */
417 0, /* tp_descr_set */
418 0, /* tp_dictoffset */
419#endif
420#if HASH_OBJ_CONSTRUCTOR
421 (initproc)EVP_tp_init, /* tp_init */
422#endif
423};
424
425static PyObject *
426EVPnew(PyObject *name_obj,
427 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
Benjamin Peterson8c2b7dc2008-09-18 01:22:16 +0000428 const unsigned char *cp, Py_ssize_t len)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000429{
430 EVPobject *self;
431
432 if (!digest && !initial_ctx) {
433 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
434 return NULL;
435 }
436
437 if ((self = newEVPobject(name_obj)) == NULL)
438 return NULL;
439
440 if (initial_ctx) {
441 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
442 } else {
443 EVP_DigestInit(&self->ctx, digest);
444 }
445
Benjamin Peterson8c2b7dc2008-09-18 01:22:16 +0000446 if (cp && len) {
Gregory P. Smitheeb51a92009-05-04 00:48:41 +0000447 if (len >= HASHLIB_GIL_MINSIZE) {
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000448 Py_BEGIN_ALLOW_THREADS
449 EVP_hash(self, cp, len);
450 Py_END_ALLOW_THREADS
Benjamin Peterson8c2b7dc2008-09-18 01:22:16 +0000451 } else {
Gregory P. Smithd02eeda2009-05-04 00:16:49 +0000452 EVP_hash(self, cp, len);
Benjamin Peterson8c2b7dc2008-09-18 01:22:16 +0000453 }
454 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000455
456 return (PyObject *)self;
457}
458
459
460/* The module-level function: new() */
461
462PyDoc_STRVAR(EVP_new__doc__,
463"Return a new hash object using the named algorithm.\n\
464An optional string argument may be provided and will be\n\
465automatically hashed.\n\
466\n\
467The MD5 and SHA1 algorithms are always supported.\n");
468
469static PyObject *
470EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
471{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000472 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000473 PyObject *name_obj = NULL;
Gregory P. Smithea388262009-02-13 03:00:00 +0000474 PyObject *data_obj = NULL;
475 Py_buffer view = { 0 };
476 PyObject *ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000477 char *name;
478 const EVP_MD *digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000479
Gregory P. Smithea388262009-02-13 03:00:00 +0000480 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
481 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000482 return NULL;
483 }
484
485 if (!PyArg_Parse(name_obj, "s", &name)) {
486 PyErr_SetString(PyExc_TypeError, "name must be a string");
487 return NULL;
488 }
489
Gregory P. Smithea388262009-02-13 03:00:00 +0000490 if (data_obj)
491 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
492
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000493 digest = EVP_get_digestbyname(name);
494
Gregory P. Smithea388262009-02-13 03:00:00 +0000495 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
496 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
497
498 if (data_obj)
499 PyBuffer_Release(&view);
500 return ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000501}
502
503/*
504 * This macro generates constructor function definitions for specific
505 * hash algorithms. These constructors are much faster than calling
506 * the generic one passing it a python string and are noticably
507 * faster than calling a python new() wrapper. Thats important for
508 * code that wants to make hashes of a bunch of small strings.
509 */
510#define GEN_CONSTRUCTOR(NAME) \
511 static PyObject * \
512 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
513 { \
Gregory P. Smithea388262009-02-13 03:00:00 +0000514 PyObject *data_obj = NULL; \
515 Py_buffer view = { 0 }; \
516 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000517 \
Gregory P. Smithea388262009-02-13 03:00:00 +0000518 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000519 return NULL; \
520 } \
521 \
Gregory P. Smithea388262009-02-13 03:00:00 +0000522 if (data_obj) \
523 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL); \
524 \
525 ret_obj = EVPnew( \
526 CONST_ ## NAME ## _name_obj, \
527 NULL, \
528 CONST_new_ ## NAME ## _ctx_p, \
529 (unsigned char*)view.buf, \
530 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); \
531 \
532 if (data_obj) \
533 PyBuffer_Release(&view); \
534 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000535 }
536
537/* a PyMethodDef structure for the constructor */
538#define CONSTRUCTOR_METH_DEF(NAME) \
539 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
540 PyDoc_STR("Returns a " #NAME \
541 " hash object; optionally initialized with a string") \
542 }
543
544/* used in the init function to setup a constructor */
545#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000546 CONST_ ## NAME ## _name_obj = PyString_FromString(#NAME); \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000547 if (EVP_get_digestbyname(#NAME)) { \
548 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
549 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
550 } \
551} while (0);
552
553GEN_CONSTRUCTOR(md5)
554GEN_CONSTRUCTOR(sha1)
555GEN_CONSTRUCTOR(sha224)
556GEN_CONSTRUCTOR(sha256)
557GEN_CONSTRUCTOR(sha384)
558GEN_CONSTRUCTOR(sha512)
559
560/* List of functions exported by this module */
561
562static struct PyMethodDef EVP_functions[] = {
563 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
564 CONSTRUCTOR_METH_DEF(md5),
565 CONSTRUCTOR_METH_DEF(sha1),
566 CONSTRUCTOR_METH_DEF(sha224),
567 CONSTRUCTOR_METH_DEF(sha256),
568 CONSTRUCTOR_METH_DEF(sha384),
569 CONSTRUCTOR_METH_DEF(sha512),
570 {NULL, NULL} /* Sentinel */
571};
572
573
574/* Initialize this module. */
575
576PyMODINIT_FUNC
577init_hashlib(void)
578{
579 PyObject *m;
580
581 OpenSSL_add_all_digests();
582
583 /* TODO build EVP_functions openssl_* entries dynamically based
584 * on what hashes are supported rather than listing many
585 * but having some be unsupported. Only init appropriate
586 * constants. */
587
Christian Heimese93237d2007-12-19 02:37:44 +0000588 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000589 if (PyType_Ready(&EVPtype) < 0)
590 return;
591
592 m = Py_InitModule("_hashlib", EVP_functions);
593 if (m == NULL)
594 return;
595
596#if HASH_OBJ_CONSTRUCTOR
597 Py_INCREF(&EVPtype);
598 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
599#endif
600
601 /* these constants are used by the convenience constructors */
602 INIT_CONSTRUCTOR_CONSTANTS(md5);
603 INIT_CONSTRUCTOR_CONSTANTS(sha1);
604 INIT_CONSTRUCTOR_CONSTANTS(sha224);
605 INIT_CONSTRUCTOR_CONSTANTS(sha256);
606 INIT_CONSTRUCTOR_CONSTANTS(sha384);
607 INIT_CONSTRUCTOR_CONSTANTS(sha512);
608}