blob: c8db5ed2c86ff58ffafb30b1e5f3965c68789bb2 [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
Gregory P. Smith3f61d612009-05-04 00:45:33 +000021/* EVP is the preferred interface to hashing in OpenSSL */
22#include <openssl/evp.h>
Gregory P. Smith13b55292010-09-06 08:30:23 +000023/* We use the object interface to discover what hashes OpenSSL supports. */
24#include <openssl/objects.h>
Gregory P. Smith3f61d612009-05-04 00:45:33 +000025
26#define MUNCH_SIZE INT_MAX
27
Gregory P. Smith3f61d612009-05-04 00:45:33 +000028#ifndef HASH_OBJ_CONSTRUCTOR
29#define HASH_OBJ_CONSTRUCTOR 0
30#endif
31
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000032/* Minimum OpenSSL version needed to support sha224 and higher. */
33#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x00908000)
34#define _OPENSSL_SUPPORTS_SHA2
35#endif
Gregory P. Smith3f61d612009-05-04 00:45:33 +000036
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000037typedef struct {
38 PyObject_HEAD
39 PyObject *name; /* name of this hash algorithm */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000040 EVP_MD_CTX ctx; /* OpenSSL message digest context */
41#ifdef WITH_THREAD
42 PyThread_type_lock lock; /* OpenSSL context lock */
43#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000044} EVPobject;
45
46
47static PyTypeObject EVPtype;
48
49
50#define DEFINE_CONSTS_FOR_NEW(Name) \
Gregory P. Smithaded2e52013-02-01 17:05:29 -080051 static PyObject *CONST_ ## Name ## _name_obj = NULL; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000052 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
53 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
54
Neal Norwitzf0459142006-01-07 21:20:24 +000055DEFINE_CONSTS_FOR_NEW(md5)
56DEFINE_CONSTS_FOR_NEW(sha1)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000057#ifdef _OPENSSL_SUPPORTS_SHA2
Neal Norwitzf0459142006-01-07 21:20:24 +000058DEFINE_CONSTS_FOR_NEW(sha224)
59DEFINE_CONSTS_FOR_NEW(sha256)
60DEFINE_CONSTS_FOR_NEW(sha384)
61DEFINE_CONSTS_FOR_NEW(sha512)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000062#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000063
64
65static EVPobject *
66newEVPobject(PyObject *name)
67{
68 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
69
70 /* save the name for .name to return */
71 if (retval != NULL) {
72 Py_INCREF(name);
73 retval->name = name;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000074#ifdef WITH_THREAD
75 retval->lock = NULL;
76#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000077 }
78
79 return retval;
80}
81
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000082static void
83EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
84{
85 unsigned int process;
86 const unsigned char *cp = (const unsigned char *)vp;
87 while (0 < len) {
88 if (len > (Py_ssize_t)MUNCH_SIZE)
89 process = MUNCH_SIZE;
90 else
91 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
92 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
93 len -= process;
94 cp += process;
95 }
96}
97
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000098/* Internal methods for a hash object */
99
100static void
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000101EVP_dealloc(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000102{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000103#ifdef WITH_THREAD
104 if (self->lock != NULL)
105 PyThread_free_lock(self->lock);
106#endif
107 EVP_MD_CTX_cleanup(&self->ctx);
108 Py_XDECREF(self->name);
109 PyObject_Del(self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000110}
111
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000112static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
113{
114 ENTER_HASHLIB(self);
115 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
116 LEAVE_HASHLIB(self);
117}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000118
119/* External methods for a hash object */
120
121PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
122
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000123
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000124static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000125EVP_copy(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000126{
127 EVPobject *newobj;
128
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000129 if ( (newobj = newEVPobject(self->name))==NULL)
130 return NULL;
131
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000132 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000133 return (PyObject *)newobj;
134}
135
136PyDoc_STRVAR(EVP_digest__doc__,
137"Return the digest value as a string of binary data.");
138
139static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000140EVP_digest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000141{
142 unsigned char digest[EVP_MAX_MD_SIZE];
143 EVP_MD_CTX temp_ctx;
144 PyObject *retval;
145 unsigned int digest_size;
146
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000147 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000148 digest_size = EVP_MD_CTX_size(&temp_ctx);
Neal Norwitzf0459142006-01-07 21:20:24 +0000149 EVP_DigestFinal(&temp_ctx, digest, NULL);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000150
Christian Heimes72b710a2008-05-26 13:28:38 +0000151 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000152 EVP_MD_CTX_cleanup(&temp_ctx);
153 return retval;
154}
155
156PyDoc_STRVAR(EVP_hexdigest__doc__,
157"Return the digest value as a string of hexadecimal digits.");
158
159static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000160EVP_hexdigest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000161{
162 unsigned char digest[EVP_MAX_MD_SIZE];
163 EVP_MD_CTX temp_ctx;
164 PyObject *retval;
165 char *hex_digest;
166 unsigned int i, j, digest_size;
167
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000168 /* Get the raw (binary) digest value */
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000169 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000170 digest_size = EVP_MD_CTX_size(&temp_ctx);
171 EVP_DigestFinal(&temp_ctx, digest, NULL);
172
173 EVP_MD_CTX_cleanup(&temp_ctx);
174
Guido van Rossumf8953072007-07-10 13:20:29 +0000175 /* Allocate a new buffer */
176 hex_digest = PyMem_Malloc(digest_size * 2 + 1);
177 if (!hex_digest)
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000178 return PyErr_NoMemory();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000179
180 /* Make hex version of the digest */
181 for(i=j=0; i<digest_size; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200182 unsigned char c;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000183 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200184 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000185 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200186 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000187 }
Guido van Rossumf8953072007-07-10 13:20:29 +0000188 retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2);
189 PyMem_Free(hex_digest);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000190 return retval;
191}
192
193PyDoc_STRVAR(EVP_update__doc__,
194"Update this hash object's state with the provided string.");
195
196static PyObject *
197EVP_update(EVPobject *self, PyObject *args)
198{
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000199 PyObject *obj;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000200 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000201
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000202 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000203 return NULL;
204
Gregory P. Smith365a1862009-02-12 07:35:29 +0000205 GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000206
207#ifdef WITH_THREAD
208 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
209 self->lock = PyThread_allocate_lock();
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000210 /* fail? lock = NULL and we fail over to non-threaded code. */
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000211 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000212
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000213 if (self->lock != NULL) {
214 Py_BEGIN_ALLOW_THREADS
215 PyThread_acquire_lock(self->lock, 1);
216 EVP_hash(self, view.buf, view.len);
217 PyThread_release_lock(self->lock);
218 Py_END_ALLOW_THREADS
219 } else {
220 EVP_hash(self, view.buf, view.len);
221 }
222#else
223 EVP_hash(self, view.buf, view.len);
224#endif
225
226 PyBuffer_Release(&view);
227 Py_RETURN_NONE;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000228}
229
230static PyMethodDef EVP_methods[] = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000231 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
232 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000233 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000234 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
235 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000236};
237
238static PyObject *
239EVP_get_block_size(EVPobject *self, void *closure)
240{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000241 long block_size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000242 block_size = EVP_MD_CTX_block_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000243 return PyLong_FromLong(block_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000244}
245
246static PyObject *
247EVP_get_digest_size(EVPobject *self, void *closure)
248{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000249 long size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000250 size = EVP_MD_CTX_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000251 return PyLong_FromLong(size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000252}
253
254static PyMemberDef EVP_members[] = {
255 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
256 {NULL} /* Sentinel */
257};
258
259static PyGetSetDef EVP_getseters[] = {
260 {"digest_size",
261 (getter)EVP_get_digest_size, NULL,
262 NULL,
263 NULL},
264 {"block_size",
265 (getter)EVP_get_block_size, NULL,
266 NULL,
267 NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000268 {NULL} /* Sentinel */
269};
270
271
272static PyObject *
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000273EVP_repr(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000274{
Victor Stinner3f1af5c2010-03-12 17:00:41 +0000275 return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000276}
277
278#if HASH_OBJ_CONSTRUCTOR
279static int
280EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
281{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000282 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000283 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000284 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000285 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000286 char *nameStr;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000287 const EVP_MD *digest;
288
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000289 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
290 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000291 return -1;
292 }
293
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000294 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000295 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000296
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000297 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
298 PyErr_SetString(PyExc_TypeError, "name must be a string");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000299 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000300 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000301 return -1;
302 }
303
304 digest = EVP_get_digestbyname(nameStr);
305 if (!digest) {
306 PyErr_SetString(PyExc_ValueError, "unknown hash function");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000307 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000308 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000309 return -1;
310 }
311 EVP_DigestInit(&self->ctx, digest);
312
313 self->name = name_obj;
314 Py_INCREF(self->name);
315
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000316 if (data_obj) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000317 if (view.len >= HASHLIB_GIL_MINSIZE) {
318 Py_BEGIN_ALLOW_THREADS
319 EVP_hash(self, view.buf, view.len);
320 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000321 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000322 EVP_hash(self, view.buf, view.len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000323 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000324 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000325 }
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000326
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000327 return 0;
328}
329#endif
330
331
332PyDoc_STRVAR(hashtype_doc,
333"A hash represents the object used to calculate a checksum of a\n\
334string of information.\n\
335\n\
336Methods:\n\
337\n\
338update() -- updates the current digest with an additional string\n\
339digest() -- return the current digest value\n\
340hexdigest() -- return the current digest as a string of hexadecimal digits\n\
341copy() -- return a copy of the current hash object\n\
342\n\
343Attributes:\n\
344\n\
345name -- the hash algorithm being used by this object\n\
346digest_size -- number of bytes in this hashes output\n");
347
348static PyTypeObject EVPtype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000349 PyVarObject_HEAD_INIT(NULL, 0)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000350 "_hashlib.HASH", /*tp_name*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000351 sizeof(EVPobject), /*tp_basicsize*/
352 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000353 /* methods */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000354 (destructor)EVP_dealloc, /*tp_dealloc*/
355 0, /*tp_print*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000356 0, /*tp_getattr*/
357 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000358 0, /*tp_reserved*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000359 (reprfunc)EVP_repr, /*tp_repr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000360 0, /*tp_as_number*/
361 0, /*tp_as_sequence*/
362 0, /*tp_as_mapping*/
363 0, /*tp_hash*/
364 0, /*tp_call*/
365 0, /*tp_str*/
366 0, /*tp_getattro*/
367 0, /*tp_setattro*/
368 0, /*tp_as_buffer*/
369 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
370 hashtype_doc, /*tp_doc*/
371 0, /*tp_traverse*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000372 0, /*tp_clear*/
373 0, /*tp_richcompare*/
374 0, /*tp_weaklistoffset*/
375 0, /*tp_iter*/
376 0, /*tp_iternext*/
377 EVP_methods, /* tp_methods */
378 EVP_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000379 EVP_getseters, /* tp_getset */
380#if 1
381 0, /* tp_base */
382 0, /* tp_dict */
383 0, /* tp_descr_get */
384 0, /* tp_descr_set */
385 0, /* tp_dictoffset */
386#endif
387#if HASH_OBJ_CONSTRUCTOR
388 (initproc)EVP_tp_init, /* tp_init */
389#endif
390};
391
392static PyObject *
393EVPnew(PyObject *name_obj,
394 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000395 const unsigned char *cp, Py_ssize_t len)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000396{
397 EVPobject *self;
398
399 if (!digest && !initial_ctx) {
400 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
401 return NULL;
402 }
403
404 if ((self = newEVPobject(name_obj)) == NULL)
405 return NULL;
406
407 if (initial_ctx) {
408 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
409 } else {
410 EVP_DigestInit(&self->ctx, digest);
411 }
412
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000413 if (cp && len) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000414 if (len >= HASHLIB_GIL_MINSIZE) {
415 Py_BEGIN_ALLOW_THREADS
416 EVP_hash(self, cp, len);
417 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000418 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000419 EVP_hash(self, cp, len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000420 }
421 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000422
423 return (PyObject *)self;
424}
425
426
427/* The module-level function: new() */
428
429PyDoc_STRVAR(EVP_new__doc__,
430"Return a new hash object using the named algorithm.\n\
431An optional string argument may be provided and will be\n\
432automatically hashed.\n\
433\n\
434The MD5 and SHA1 algorithms are always supported.\n");
435
436static PyObject *
437EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
438{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000439 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000440 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000441 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000442 Py_buffer view = { 0 };
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000443 PyObject *ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000444 char *name;
445 const EVP_MD *digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000446
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000447 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
448 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000449 return NULL;
450 }
451
452 if (!PyArg_Parse(name_obj, "s", &name)) {
453 PyErr_SetString(PyExc_TypeError, "name must be a string");
454 return NULL;
455 }
456
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000457 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000458 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000459
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000460 digest = EVP_get_digestbyname(name);
461
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000462 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000463
464 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000465 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000466 return ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000467}
468
Gregory P. Smith13b55292010-09-06 08:30:23 +0000469
470/* State for our callback function so that it can accumulate a result. */
471typedef struct _internal_name_mapper_state {
472 PyObject *set;
473 int error;
474} _InternalNameMapperState;
475
476
477/* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
478static void
479_openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg)
480{
481 _InternalNameMapperState *state = (_InternalNameMapperState *)arg;
482 PyObject *py_name;
483
484 assert(state != NULL);
485 if (openssl_obj_name == NULL)
486 return;
487 /* Ignore aliased names, they pollute the list and OpenSSL appears to
488 * have a its own definition of alias as the resulting list still
489 * contains duplicate and alternate names for several algorithms. */
490 if (openssl_obj_name->alias)
491 return;
492
493 py_name = PyUnicode_FromString(openssl_obj_name->name);
494 if (py_name == NULL) {
495 state->error = 1;
496 } else {
497 if (PySet_Add(state->set, py_name) != 0) {
498 Py_DECREF(py_name);
499 state->error = 1;
500 }
501 }
502}
503
504
505/* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */
506static PyObject*
507generate_hash_name_list(void)
508{
509 _InternalNameMapperState state;
510 state.set = PyFrozenSet_New(NULL);
511 if (state.set == NULL)
512 return NULL;
513 state.error = 0;
514
515 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state);
516
517 if (state.error) {
518 Py_DECREF(state.set);
519 return NULL;
520 }
521 return state.set;
522}
523
524
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000525/*
526 * This macro generates constructor function definitions for specific
527 * hash algorithms. These constructors are much faster than calling
528 * the generic one passing it a python string and are noticably
529 * faster than calling a python new() wrapper. Thats important for
530 * code that wants to make hashes of a bunch of small strings.
531 */
532#define GEN_CONSTRUCTOR(NAME) \
533 static PyObject * \
534 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
535 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000536 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000537 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000538 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000539 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000540 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000541 return NULL; \
542 } \
543 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000544 if (data_obj) \
Gregory P. Smith365a1862009-02-12 07:35:29 +0000545 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000546 \
547 ret_obj = EVPnew( \
548 CONST_ ## NAME ## _name_obj, \
549 NULL, \
550 CONST_new_ ## NAME ## _ctx_p, \
551 (unsigned char*)view.buf, \
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000552 view.len); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000553 \
554 if (data_obj) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000555 PyBuffer_Release(&view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000556 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000557 }
558
559/* a PyMethodDef structure for the constructor */
560#define CONSTRUCTOR_METH_DEF(NAME) \
561 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
562 PyDoc_STR("Returns a " #NAME \
563 " hash object; optionally initialized with a string") \
564 }
565
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800566/* used in the init function to setup a constructor: initialize OpenSSL
567 constructor constants if they haven't been initialized already. */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000568#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800569 if (CONST_ ## NAME ## _name_obj == NULL) { \
570 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
571 if (EVP_get_digestbyname(#NAME)) { \
572 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
573 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
574 } \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000575 } \
576} while (0);
577
578GEN_CONSTRUCTOR(md5)
579GEN_CONSTRUCTOR(sha1)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000580#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000581GEN_CONSTRUCTOR(sha224)
582GEN_CONSTRUCTOR(sha256)
583GEN_CONSTRUCTOR(sha384)
584GEN_CONSTRUCTOR(sha512)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000585#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000586
587/* List of functions exported by this module */
588
589static struct PyMethodDef EVP_functions[] = {
590 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
591 CONSTRUCTOR_METH_DEF(md5),
592 CONSTRUCTOR_METH_DEF(sha1),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000593#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000594 CONSTRUCTOR_METH_DEF(sha224),
595 CONSTRUCTOR_METH_DEF(sha256),
596 CONSTRUCTOR_METH_DEF(sha384),
597 CONSTRUCTOR_METH_DEF(sha512),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000598#endif
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000599 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000600};
601
602
603/* Initialize this module. */
604
Martin v. Löwis1a214512008-06-11 05:26:20 +0000605
606static struct PyModuleDef _hashlibmodule = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000607 PyModuleDef_HEAD_INIT,
608 "_hashlib",
609 NULL,
610 -1,
611 EVP_functions,
612 NULL,
613 NULL,
614 NULL,
615 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000616};
617
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000618PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000619PyInit__hashlib(void)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000620{
Gregory P. Smith13b55292010-09-06 08:30:23 +0000621 PyObject *m, *openssl_md_meth_names;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000622
623 OpenSSL_add_all_digests();
624
625 /* TODO build EVP_functions openssl_* entries dynamically based
626 * on what hashes are supported rather than listing many
627 * but having some be unsupported. Only init appropriate
628 * constants. */
629
Christian Heimes90aa7642007-12-19 02:45:37 +0000630 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000631 if (PyType_Ready(&EVPtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000632 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000633
Martin v. Löwis1a214512008-06-11 05:26:20 +0000634 m = PyModule_Create(&_hashlibmodule);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000635 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000636 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000637
Gregory P. Smith13b55292010-09-06 08:30:23 +0000638 openssl_md_meth_names = generate_hash_name_list();
639 if (openssl_md_meth_names == NULL) {
640 Py_DECREF(m);
641 return NULL;
642 }
643 if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) {
644 Py_DECREF(m);
645 return NULL;
646 }
647
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000648#if HASH_OBJ_CONSTRUCTOR
649 Py_INCREF(&EVPtype);
650 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
651#endif
652
653 /* these constants are used by the convenience constructors */
654 INIT_CONSTRUCTOR_CONSTANTS(md5);
655 INIT_CONSTRUCTOR_CONSTANTS(sha1);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000656#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000657 INIT_CONSTRUCTOR_CONSTANTS(sha224);
658 INIT_CONSTRUCTOR_CONSTANTS(sha256);
659 INIT_CONSTRUCTOR_CONSTANTS(sha384);
660 INIT_CONSTRUCTOR_CONSTANTS(sha512);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000661#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +0000662 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000663}