blob: 0850d7b8a49b8499010494e117c206ce3f245fb7 [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
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{
291 char buf[100];
292 PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000293 _PyUnicode_AsString(self->name), self);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000294 return PyUnicode_FromString(buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000295}
296
297#if HASH_OBJ_CONSTRUCTOR
298static int
299EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
300{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000301 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000302 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000303 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000304 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000305 char *nameStr;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000306 const EVP_MD *digest;
307
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000308 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
309 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000310 return -1;
311 }
312
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000313 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000314 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000315
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000316 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
317 PyErr_SetString(PyExc_TypeError, "name must be a string");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000318 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000319 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000320 return -1;
321 }
322
323 digest = EVP_get_digestbyname(nameStr);
324 if (!digest) {
325 PyErr_SetString(PyExc_ValueError, "unknown hash function");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000326 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000327 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000328 return -1;
329 }
330 EVP_DigestInit(&self->ctx, digest);
331
332 self->name = name_obj;
333 Py_INCREF(self->name);
334
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000335 if (data_obj) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000336 if (view.len >= HASHLIB_GIL_MINSIZE) {
337 Py_BEGIN_ALLOW_THREADS
338 EVP_hash(self, view.buf, view.len);
339 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000340 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000341 EVP_hash(self, view.buf, view.len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000342 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000343 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000344 }
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000345
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000346 return 0;
347}
348#endif
349
350
351PyDoc_STRVAR(hashtype_doc,
352"A hash represents the object used to calculate a checksum of a\n\
353string of information.\n\
354\n\
355Methods:\n\
356\n\
357update() -- updates the current digest with an additional string\n\
358digest() -- return the current digest value\n\
359hexdigest() -- return the current digest as a string of hexadecimal digits\n\
360copy() -- return a copy of the current hash object\n\
361\n\
362Attributes:\n\
363\n\
364name -- the hash algorithm being used by this object\n\
365digest_size -- number of bytes in this hashes output\n");
366
367static PyTypeObject EVPtype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000368 PyVarObject_HEAD_INIT(NULL, 0)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000369 "_hashlib.HASH", /*tp_name*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000370 sizeof(EVPobject), /*tp_basicsize*/
371 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000372 /* methods */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000373 (destructor)EVP_dealloc, /*tp_dealloc*/
374 0, /*tp_print*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000375 0, /*tp_getattr*/
376 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000377 0, /*tp_reserved*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000378 (reprfunc)EVP_repr, /*tp_repr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000379 0, /*tp_as_number*/
380 0, /*tp_as_sequence*/
381 0, /*tp_as_mapping*/
382 0, /*tp_hash*/
383 0, /*tp_call*/
384 0, /*tp_str*/
385 0, /*tp_getattro*/
386 0, /*tp_setattro*/
387 0, /*tp_as_buffer*/
388 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
389 hashtype_doc, /*tp_doc*/
390 0, /*tp_traverse*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000391 0, /*tp_clear*/
392 0, /*tp_richcompare*/
393 0, /*tp_weaklistoffset*/
394 0, /*tp_iter*/
395 0, /*tp_iternext*/
396 EVP_methods, /* tp_methods */
397 EVP_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000398 EVP_getseters, /* tp_getset */
399#if 1
400 0, /* tp_base */
401 0, /* tp_dict */
402 0, /* tp_descr_get */
403 0, /* tp_descr_set */
404 0, /* tp_dictoffset */
405#endif
406#if HASH_OBJ_CONSTRUCTOR
407 (initproc)EVP_tp_init, /* tp_init */
408#endif
409};
410
411static PyObject *
412EVPnew(PyObject *name_obj,
413 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000414 const unsigned char *cp, Py_ssize_t len)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000415{
416 EVPobject *self;
417
418 if (!digest && !initial_ctx) {
419 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
420 return NULL;
421 }
422
423 if ((self = newEVPobject(name_obj)) == NULL)
424 return NULL;
425
426 if (initial_ctx) {
427 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
428 } else {
429 EVP_DigestInit(&self->ctx, digest);
430 }
431
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000432 if (cp && len) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000433 if (len >= HASHLIB_GIL_MINSIZE) {
434 Py_BEGIN_ALLOW_THREADS
435 EVP_hash(self, cp, len);
436 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000437 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000438 EVP_hash(self, cp, len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000439 }
440 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000441
442 return (PyObject *)self;
443}
444
445
446/* The module-level function: new() */
447
448PyDoc_STRVAR(EVP_new__doc__,
449"Return a new hash object using the named algorithm.\n\
450An optional string argument may be provided and will be\n\
451automatically hashed.\n\
452\n\
453The MD5 and SHA1 algorithms are always supported.\n");
454
455static PyObject *
456EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
457{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000458 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000459 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000460 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000461 Py_buffer view = { 0 };
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000462 PyObject *ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000463 char *name;
464 const EVP_MD *digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000465
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000466 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
467 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000468 return NULL;
469 }
470
471 if (!PyArg_Parse(name_obj, "s", &name)) {
472 PyErr_SetString(PyExc_TypeError, "name must be a string");
473 return NULL;
474 }
475
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000476 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000477 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000478
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000479 digest = EVP_get_digestbyname(name);
480
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000481 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000482
483 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000484 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000485 return ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000486}
487
488/*
489 * This macro generates constructor function definitions for specific
490 * hash algorithms. These constructors are much faster than calling
491 * the generic one passing it a python string and are noticably
492 * faster than calling a python new() wrapper. Thats important for
493 * code that wants to make hashes of a bunch of small strings.
494 */
495#define GEN_CONSTRUCTOR(NAME) \
496 static PyObject * \
497 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
498 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000499 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000500 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000501 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000502 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000503 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000504 return NULL; \
505 } \
506 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000507 if (data_obj) \
Gregory P. Smith365a1862009-02-12 07:35:29 +0000508 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000509 \
510 ret_obj = EVPnew( \
511 CONST_ ## NAME ## _name_obj, \
512 NULL, \
513 CONST_new_ ## NAME ## _ctx_p, \
514 (unsigned char*)view.buf, \
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000515 view.len); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000516 \
517 if (data_obj) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000518 PyBuffer_Release(&view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000519 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000520 }
521
522/* a PyMethodDef structure for the constructor */
523#define CONSTRUCTOR_METH_DEF(NAME) \
524 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
525 PyDoc_STR("Returns a " #NAME \
526 " hash object; optionally initialized with a string") \
527 }
528
529/* used in the init function to setup a constructor */
530#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Neal Norwitzd6d2f2f2007-08-23 20:28:10 +0000531 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000532 if (EVP_get_digestbyname(#NAME)) { \
533 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
534 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
535 } \
536} while (0);
537
538GEN_CONSTRUCTOR(md5)
539GEN_CONSTRUCTOR(sha1)
540GEN_CONSTRUCTOR(sha224)
541GEN_CONSTRUCTOR(sha256)
542GEN_CONSTRUCTOR(sha384)
543GEN_CONSTRUCTOR(sha512)
544
545/* List of functions exported by this module */
546
547static struct PyMethodDef EVP_functions[] = {
548 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
549 CONSTRUCTOR_METH_DEF(md5),
550 CONSTRUCTOR_METH_DEF(sha1),
551 CONSTRUCTOR_METH_DEF(sha224),
552 CONSTRUCTOR_METH_DEF(sha256),
553 CONSTRUCTOR_METH_DEF(sha384),
554 CONSTRUCTOR_METH_DEF(sha512),
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000555 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000556};
557
558
559/* Initialize this module. */
560
Martin v. Löwis1a214512008-06-11 05:26:20 +0000561
562static struct PyModuleDef _hashlibmodule = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000563 PyModuleDef_HEAD_INIT,
564 "_hashlib",
565 NULL,
566 -1,
567 EVP_functions,
568 NULL,
569 NULL,
570 NULL,
571 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000572};
573
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000574PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000575PyInit__hashlib(void)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000576{
577 PyObject *m;
578
579 OpenSSL_add_all_digests();
580
581 /* TODO build EVP_functions openssl_* entries dynamically based
582 * on what hashes are supported rather than listing many
583 * but having some be unsupported. Only init appropriate
584 * constants. */
585
Christian Heimes90aa7642007-12-19 02:45:37 +0000586 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000587 if (PyType_Ready(&EVPtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000588 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000589
Martin v. Löwis1a214512008-06-11 05:26:20 +0000590 m = PyModule_Create(&_hashlibmodule);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000591 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000592 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000593
594#if HASH_OBJ_CONSTRUCTOR
595 Py_INCREF(&EVPtype);
596 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
597#endif
598
599 /* these constants are used by the convenience constructors */
600 INIT_CONSTRUCTOR_CONSTANTS(md5);
601 INIT_CONSTRUCTOR_CONSTANTS(sha1);
602 INIT_CONSTRUCTOR_CONSTANTS(sha224);
603 INIT_CONSTRUCTOR_CONSTANTS(sha256);
604 INIT_CONSTRUCTOR_CONSTANTS(sha384);
605 INIT_CONSTRUCTOR_CONSTANTS(sha512);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000606 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000607}