blob: a157fbb14a0b8340601842ce54efcae3467ca387 [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. Smith4dff6f62015-04-25 23:42:38 +000019#include "pystrhex.h"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000020
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000021
Gregory P. Smith3f61d612009-05-04 00:45:33 +000022/* EVP is the preferred interface to hashing in OpenSSL */
23#include <openssl/evp.h>
Christian Heimese7236222013-10-19 14:24:44 +020024#include <openssl/hmac.h>
Gregory P. Smith13b55292010-09-06 08:30:23 +000025/* We use the object interface to discover what hashes OpenSSL supports. */
26#include <openssl/objects.h>
Christian Heimese92ef132013-10-13 00:52:43 +020027#include "openssl/err.h"
Gregory P. Smith3f61d612009-05-04 00:45:33 +000028
29#define MUNCH_SIZE INT_MAX
30
Gregory P. Smith3f61d612009-05-04 00:45:33 +000031#ifndef HASH_OBJ_CONSTRUCTOR
32#define HASH_OBJ_CONSTRUCTOR 0
33#endif
34
35
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000036typedef struct {
37 PyObject_HEAD
38 PyObject *name; /* name of this hash algorithm */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000039 EVP_MD_CTX ctx; /* OpenSSL message digest context */
40#ifdef WITH_THREAD
41 PyThread_type_lock lock; /* OpenSSL context lock */
42#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000043} EVPobject;
44
45
46static PyTypeObject EVPtype;
47
48
49#define DEFINE_CONSTS_FOR_NEW(Name) \
Gregory P. Smithaded2e52013-02-01 17:05:29 -080050 static PyObject *CONST_ ## Name ## _name_obj = NULL; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000051 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
52 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
53
Neal Norwitzf0459142006-01-07 21:20:24 +000054DEFINE_CONSTS_FOR_NEW(md5)
55DEFINE_CONSTS_FOR_NEW(sha1)
56DEFINE_CONSTS_FOR_NEW(sha224)
57DEFINE_CONSTS_FOR_NEW(sha256)
58DEFINE_CONSTS_FOR_NEW(sha384)
59DEFINE_CONSTS_FOR_NEW(sha512)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000060
61
62static EVPobject *
63newEVPobject(PyObject *name)
64{
65 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
66
67 /* save the name for .name to return */
68 if (retval != NULL) {
69 Py_INCREF(name);
70 retval->name = name;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000071#ifdef WITH_THREAD
72 retval->lock = NULL;
73#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000074 }
75
76 return retval;
77}
78
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000079static void
80EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
81{
82 unsigned int process;
83 const unsigned char *cp = (const unsigned char *)vp;
84 while (0 < len) {
85 if (len > (Py_ssize_t)MUNCH_SIZE)
86 process = MUNCH_SIZE;
87 else
88 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
89 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
90 len -= process;
91 cp += process;
92 }
93}
94
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000095/* Internal methods for a hash object */
96
97static void
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000098EVP_dealloc(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000099{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000100#ifdef WITH_THREAD
101 if (self->lock != NULL)
102 PyThread_free_lock(self->lock);
103#endif
104 EVP_MD_CTX_cleanup(&self->ctx);
105 Py_XDECREF(self->name);
106 PyObject_Del(self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000107}
108
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000109static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
110{
111 ENTER_HASHLIB(self);
112 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
113 LEAVE_HASHLIB(self);
114}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000115
116/* External methods for a hash object */
117
118PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
119
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000120
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000121static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000122EVP_copy(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000123{
124 EVPobject *newobj;
125
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000126 if ( (newobj = newEVPobject(self->name))==NULL)
127 return NULL;
128
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000129 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000130 return (PyObject *)newobj;
131}
132
133PyDoc_STRVAR(EVP_digest__doc__,
134"Return the digest value as a string of binary data.");
135
136static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000137EVP_digest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000138{
139 unsigned char digest[EVP_MAX_MD_SIZE];
140 EVP_MD_CTX temp_ctx;
141 PyObject *retval;
142 unsigned int digest_size;
143
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000144 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000145 digest_size = EVP_MD_CTX_size(&temp_ctx);
Neal Norwitzf0459142006-01-07 21:20:24 +0000146 EVP_DigestFinal(&temp_ctx, digest, NULL);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000147
Christian Heimes72b710a2008-05-26 13:28:38 +0000148 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000149 EVP_MD_CTX_cleanup(&temp_ctx);
150 return retval;
151}
152
153PyDoc_STRVAR(EVP_hexdigest__doc__,
154"Return the digest value as a string of hexadecimal digits.");
155
156static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000157EVP_hexdigest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000158{
159 unsigned char digest[EVP_MAX_MD_SIZE];
160 EVP_MD_CTX temp_ctx;
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000161 unsigned int digest_size;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000162
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000163 /* Get the raw (binary) digest value */
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000164 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000165 digest_size = EVP_MD_CTX_size(&temp_ctx);
166 EVP_DigestFinal(&temp_ctx, digest, NULL);
167
168 EVP_MD_CTX_cleanup(&temp_ctx);
169
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000170 return _Py_strhex((const char *)digest, digest_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000171}
172
173PyDoc_STRVAR(EVP_update__doc__,
174"Update this hash object's state with the provided string.");
175
176static PyObject *
177EVP_update(EVPobject *self, PyObject *args)
178{
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000179 PyObject *obj;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000180 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000181
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000182 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000183 return NULL;
184
Gregory P. Smith365a1862009-02-12 07:35:29 +0000185 GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000186
187#ifdef WITH_THREAD
188 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
189 self->lock = PyThread_allocate_lock();
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000190 /* fail? lock = NULL and we fail over to non-threaded code. */
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000191 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000192
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000193 if (self->lock != NULL) {
194 Py_BEGIN_ALLOW_THREADS
195 PyThread_acquire_lock(self->lock, 1);
196 EVP_hash(self, view.buf, view.len);
197 PyThread_release_lock(self->lock);
198 Py_END_ALLOW_THREADS
199 } else {
200 EVP_hash(self, view.buf, view.len);
201 }
202#else
203 EVP_hash(self, view.buf, view.len);
204#endif
205
206 PyBuffer_Release(&view);
207 Py_RETURN_NONE;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000208}
209
210static PyMethodDef EVP_methods[] = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000211 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
212 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000213 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000214 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
215 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000216};
217
218static PyObject *
219EVP_get_block_size(EVPobject *self, void *closure)
220{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000221 long block_size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000222 block_size = EVP_MD_CTX_block_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000223 return PyLong_FromLong(block_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000224}
225
226static PyObject *
227EVP_get_digest_size(EVPobject *self, void *closure)
228{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000229 long size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000230 size = EVP_MD_CTX_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000231 return PyLong_FromLong(size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000232}
233
234static PyMemberDef EVP_members[] = {
235 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
236 {NULL} /* Sentinel */
237};
238
239static PyGetSetDef EVP_getseters[] = {
240 {"digest_size",
241 (getter)EVP_get_digest_size, NULL,
242 NULL,
243 NULL},
244 {"block_size",
245 (getter)EVP_get_block_size, NULL,
246 NULL,
247 NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000248 {NULL} /* Sentinel */
249};
250
251
252static PyObject *
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000253EVP_repr(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000254{
Victor Stinner3f1af5c2010-03-12 17:00:41 +0000255 return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000256}
257
258#if HASH_OBJ_CONSTRUCTOR
259static int
260EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
261{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000262 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000263 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000264 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000265 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000266 char *nameStr;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000267 const EVP_MD *digest;
268
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000269 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
270 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000271 return -1;
272 }
273
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000274 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000275 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000276
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000277 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
278 PyErr_SetString(PyExc_TypeError, "name must be a string");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000279 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000280 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000281 return -1;
282 }
283
284 digest = EVP_get_digestbyname(nameStr);
285 if (!digest) {
286 PyErr_SetString(PyExc_ValueError, "unknown hash function");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000287 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000288 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000289 return -1;
290 }
291 EVP_DigestInit(&self->ctx, digest);
292
293 self->name = name_obj;
294 Py_INCREF(self->name);
295
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000296 if (data_obj) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000297 if (view.len >= HASHLIB_GIL_MINSIZE) {
298 Py_BEGIN_ALLOW_THREADS
299 EVP_hash(self, view.buf, view.len);
300 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000301 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000302 EVP_hash(self, view.buf, view.len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000303 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000304 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000305 }
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000306
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000307 return 0;
308}
309#endif
310
311
312PyDoc_STRVAR(hashtype_doc,
313"A hash represents the object used to calculate a checksum of a\n\
314string of information.\n\
315\n\
316Methods:\n\
317\n\
318update() -- updates the current digest with an additional string\n\
319digest() -- return the current digest value\n\
320hexdigest() -- return the current digest as a string of hexadecimal digits\n\
321copy() -- return a copy of the current hash object\n\
322\n\
323Attributes:\n\
324\n\
325name -- the hash algorithm being used by this object\n\
326digest_size -- number of bytes in this hashes output\n");
327
328static PyTypeObject EVPtype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000329 PyVarObject_HEAD_INIT(NULL, 0)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000330 "_hashlib.HASH", /*tp_name*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000331 sizeof(EVPobject), /*tp_basicsize*/
332 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000333 /* methods */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000334 (destructor)EVP_dealloc, /*tp_dealloc*/
335 0, /*tp_print*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000336 0, /*tp_getattr*/
337 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000338 0, /*tp_reserved*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000339 (reprfunc)EVP_repr, /*tp_repr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000340 0, /*tp_as_number*/
341 0, /*tp_as_sequence*/
342 0, /*tp_as_mapping*/
343 0, /*tp_hash*/
344 0, /*tp_call*/
345 0, /*tp_str*/
346 0, /*tp_getattro*/
347 0, /*tp_setattro*/
348 0, /*tp_as_buffer*/
349 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
350 hashtype_doc, /*tp_doc*/
351 0, /*tp_traverse*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000352 0, /*tp_clear*/
353 0, /*tp_richcompare*/
354 0, /*tp_weaklistoffset*/
355 0, /*tp_iter*/
356 0, /*tp_iternext*/
357 EVP_methods, /* tp_methods */
358 EVP_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000359 EVP_getseters, /* tp_getset */
360#if 1
361 0, /* tp_base */
362 0, /* tp_dict */
363 0, /* tp_descr_get */
364 0, /* tp_descr_set */
365 0, /* tp_dictoffset */
366#endif
367#if HASH_OBJ_CONSTRUCTOR
368 (initproc)EVP_tp_init, /* tp_init */
369#endif
370};
371
372static PyObject *
373EVPnew(PyObject *name_obj,
374 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000375 const unsigned char *cp, Py_ssize_t len)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000376{
377 EVPobject *self;
378
379 if (!digest && !initial_ctx) {
380 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
381 return NULL;
382 }
383
384 if ((self = newEVPobject(name_obj)) == NULL)
385 return NULL;
386
387 if (initial_ctx) {
388 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
389 } else {
390 EVP_DigestInit(&self->ctx, digest);
391 }
392
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000393 if (cp && len) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000394 if (len >= HASHLIB_GIL_MINSIZE) {
395 Py_BEGIN_ALLOW_THREADS
396 EVP_hash(self, cp, len);
397 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000398 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000399 EVP_hash(self, cp, len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000400 }
401 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000402
403 return (PyObject *)self;
404}
405
406
407/* The module-level function: new() */
408
409PyDoc_STRVAR(EVP_new__doc__,
410"Return a new hash object using the named algorithm.\n\
411An optional string argument may be provided and will be\n\
412automatically hashed.\n\
413\n\
414The MD5 and SHA1 algorithms are always supported.\n");
415
416static PyObject *
417EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
418{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000419 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000420 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000421 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000422 Py_buffer view = { 0 };
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000423 PyObject *ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000424 char *name;
425 const EVP_MD *digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000426
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000427 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
428 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000429 return NULL;
430 }
431
432 if (!PyArg_Parse(name_obj, "s", &name)) {
433 PyErr_SetString(PyExc_TypeError, "name must be a string");
434 return NULL;
435 }
436
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000437 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000438 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000439
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000440 digest = EVP_get_digestbyname(name);
441
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000442 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000443
444 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000445 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000446 return ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000447}
448
Christian Heimese7236222013-10-19 14:24:44 +0200449
450
Christian Heimes351f5392013-10-19 17:59:48 +0200451#if (OPENSSL_VERSION_NUMBER >= 0x10000000 && !defined(OPENSSL_NO_HMAC) \
452 && !defined(OPENSSL_NO_SHA))
Christian Heimese7236222013-10-19 14:24:44 +0200453
Christian Heimese92ef132013-10-13 00:52:43 +0200454#define PY_PBKDF2_HMAC 1
455
Christian Heimese7236222013-10-19 14:24:44 +0200456/* Improved implementation of PKCS5_PBKDF2_HMAC()
457 *
458 * PKCS5_PBKDF2_HMAC_fast() hashes the password exactly one time instead of
459 * `iter` times. Today (2013) the iteration count is typically 100,000 or
460 * more. The improved algorithm is not subject to a Denial-of-Service
461 * vulnerability with overly large passwords.
462 *
463 * Also OpenSSL < 1.0 don't provide PKCS5_PBKDF2_HMAC(), only
464 * PKCS5_PBKDF2_SHA1.
465 */
Christian Heimesc6564b92013-10-20 13:23:03 +0200466static int
467PKCS5_PBKDF2_HMAC_fast(const char *pass, int passlen,
468 const unsigned char *salt, int saltlen,
469 int iter, const EVP_MD *digest,
470 int keylen, unsigned char *out)
Christian Heimese7236222013-10-19 14:24:44 +0200471{
472 unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
473 int cplen, j, k, tkeylen, mdlen;
474 unsigned long i = 1;
475 HMAC_CTX hctx_tpl, hctx;
476
477 mdlen = EVP_MD_size(digest);
478 if (mdlen < 0)
479 return 0;
480
481 HMAC_CTX_init(&hctx_tpl);
482 HMAC_CTX_init(&hctx);
483 p = out;
484 tkeylen = keylen;
Christian Heimese7236222013-10-19 14:24:44 +0200485 if (!HMAC_Init_ex(&hctx_tpl, pass, passlen, digest, NULL)) {
486 HMAC_CTX_cleanup(&hctx_tpl);
487 return 0;
488 }
489 while(tkeylen) {
490 if(tkeylen > mdlen)
491 cplen = mdlen;
492 else
493 cplen = tkeylen;
494 /* We are unlikely to ever use more than 256 blocks (5120 bits!)
495 * but just in case...
496 */
497 itmp[0] = (unsigned char)((i >> 24) & 0xff);
498 itmp[1] = (unsigned char)((i >> 16) & 0xff);
499 itmp[2] = (unsigned char)((i >> 8) & 0xff);
500 itmp[3] = (unsigned char)(i & 0xff);
501 if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) {
502 HMAC_CTX_cleanup(&hctx_tpl);
503 return 0;
504 }
505 if (!HMAC_Update(&hctx, salt, saltlen)
506 || !HMAC_Update(&hctx, itmp, 4)
507 || !HMAC_Final(&hctx, digtmp, NULL)) {
508 HMAC_CTX_cleanup(&hctx_tpl);
509 HMAC_CTX_cleanup(&hctx);
510 return 0;
511 }
Christian Heimes68531082013-11-06 17:25:17 +0100512 HMAC_CTX_cleanup(&hctx);
Christian Heimese7236222013-10-19 14:24:44 +0200513 memcpy(p, digtmp, cplen);
514 for (j = 1; j < iter; j++) {
515 if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) {
516 HMAC_CTX_cleanup(&hctx_tpl);
517 return 0;
518 }
519 if (!HMAC_Update(&hctx, digtmp, mdlen)
520 || !HMAC_Final(&hctx, digtmp, NULL)) {
521 HMAC_CTX_cleanup(&hctx_tpl);
522 HMAC_CTX_cleanup(&hctx);
523 return 0;
524 }
525 HMAC_CTX_cleanup(&hctx);
526 for (k = 0; k < cplen; k++) {
527 p[k] ^= digtmp[k];
528 }
529 }
530 tkeylen-= cplen;
531 i++;
532 p+= cplen;
533 }
534 HMAC_CTX_cleanup(&hctx_tpl);
535 return 1;
536}
537
Christian Heimes48b7df72013-12-05 07:38:13 +0100538/* LCOV_EXCL_START */
Brett Cannon2be28a62013-11-01 10:25:13 -0400539static PyObject *
540_setException(PyObject *exc)
541{
542 unsigned long errcode;
543 const char *lib, *func, *reason;
544
545 errcode = ERR_peek_last_error();
546 if (!errcode) {
547 PyErr_SetString(exc, "unknown reasons");
548 return NULL;
549 }
550 ERR_clear_error();
551
552 lib = ERR_lib_error_string(errcode);
553 func = ERR_func_error_string(errcode);
554 reason = ERR_reason_error_string(errcode);
555
556 if (lib && func) {
557 PyErr_Format(exc, "[%s: %s] %s", lib, func, reason);
558 }
559 else if (lib) {
560 PyErr_Format(exc, "[%s] %s", lib, reason);
561 }
562 else {
563 PyErr_SetString(exc, reason);
564 }
565 return NULL;
566}
Christian Heimes48b7df72013-12-05 07:38:13 +0100567/* LCOV_EXCL_STOP */
Christian Heimese7236222013-10-19 14:24:44 +0200568
Christian Heimese92ef132013-10-13 00:52:43 +0200569PyDoc_STRVAR(pbkdf2_hmac__doc__,
570"pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None) -> key\n\
571\n\
572Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as\n\
573pseudorandom function.");
574
575static PyObject *
576pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict)
577{
578 static char *kwlist[] = {"hash_name", "password", "salt", "iterations",
579 "dklen", NULL};
580 PyObject *key_obj = NULL, *dklen_obj = Py_None;
581 char *name, *key;
582 Py_buffer password, salt;
583 long iterations, dklen;
584 int retval;
585 const EVP_MD *digest;
586
587 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "sy*y*l|O:pbkdf2_hmac",
588 kwlist, &name, &password, &salt,
589 &iterations, &dklen_obj)) {
590 return NULL;
591 }
592
593 digest = EVP_get_digestbyname(name);
594 if (digest == NULL) {
595 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
596 goto end;
597 }
598
599 if (password.len > INT_MAX) {
600 PyErr_SetString(PyExc_OverflowError,
601 "password is too long.");
602 goto end;
603 }
604
605 if (salt.len > INT_MAX) {
606 PyErr_SetString(PyExc_OverflowError,
607 "salt is too long.");
608 goto end;
609 }
610
611 if (iterations < 1) {
612 PyErr_SetString(PyExc_ValueError,
613 "iteration value must be greater than 0.");
614 goto end;
615 }
616 if (iterations > INT_MAX) {
617 PyErr_SetString(PyExc_OverflowError,
618 "iteration value is too great.");
619 goto end;
620 }
621
622 if (dklen_obj == Py_None) {
623 dklen = EVP_MD_size(digest);
624 } else {
625 dklen = PyLong_AsLong(dklen_obj);
626 if ((dklen == -1) && PyErr_Occurred()) {
627 goto end;
628 }
629 }
630 if (dklen < 1) {
631 PyErr_SetString(PyExc_ValueError,
632 "key length must be greater than 0.");
633 goto end;
634 }
635 if (dklen > INT_MAX) {
636 /* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */
637 PyErr_SetString(PyExc_OverflowError,
638 "key length is too great.");
639 goto end;
640 }
641
642 key_obj = PyBytes_FromStringAndSize(NULL, dklen);
643 if (key_obj == NULL) {
644 goto end;
645 }
646 key = PyBytes_AS_STRING(key_obj);
647
648 Py_BEGIN_ALLOW_THREADS
Victor Stinnerc1a57d32013-11-16 00:27:16 +0100649 retval = PKCS5_PBKDF2_HMAC_fast((char*)password.buf, (int)password.len,
Christian Heimescc6cdce2013-11-18 09:59:44 +0100650 (unsigned char *)salt.buf, (int)salt.len,
Christian Heimese7236222013-10-19 14:24:44 +0200651 iterations, digest, dklen,
652 (unsigned char *)key);
Christian Heimese92ef132013-10-13 00:52:43 +0200653 Py_END_ALLOW_THREADS
654
655 if (!retval) {
656 Py_CLEAR(key_obj);
657 _setException(PyExc_ValueError);
658 goto end;
659 }
660
661 end:
662 PyBuffer_Release(&password);
663 PyBuffer_Release(&salt);
664 return key_obj;
665}
666
667#endif
Gregory P. Smith13b55292010-09-06 08:30:23 +0000668
669/* State for our callback function so that it can accumulate a result. */
670typedef struct _internal_name_mapper_state {
671 PyObject *set;
672 int error;
673} _InternalNameMapperState;
674
675
676/* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
677static void
678_openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg)
679{
680 _InternalNameMapperState *state = (_InternalNameMapperState *)arg;
681 PyObject *py_name;
682
683 assert(state != NULL);
684 if (openssl_obj_name == NULL)
685 return;
686 /* Ignore aliased names, they pollute the list and OpenSSL appears to
687 * have a its own definition of alias as the resulting list still
688 * contains duplicate and alternate names for several algorithms. */
689 if (openssl_obj_name->alias)
690 return;
691
692 py_name = PyUnicode_FromString(openssl_obj_name->name);
693 if (py_name == NULL) {
694 state->error = 1;
695 } else {
696 if (PySet_Add(state->set, py_name) != 0) {
Gregory P. Smith13b55292010-09-06 08:30:23 +0000697 state->error = 1;
698 }
Christian Heimesdb816d62013-10-29 12:14:55 +0100699 Py_DECREF(py_name);
Gregory P. Smith13b55292010-09-06 08:30:23 +0000700 }
701}
702
703
704/* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */
705static PyObject*
706generate_hash_name_list(void)
707{
708 _InternalNameMapperState state;
709 state.set = PyFrozenSet_New(NULL);
710 if (state.set == NULL)
711 return NULL;
712 state.error = 0;
713
714 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state);
715
716 if (state.error) {
717 Py_DECREF(state.set);
718 return NULL;
719 }
720 return state.set;
721}
722
723
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000724/*
725 * This macro generates constructor function definitions for specific
726 * hash algorithms. These constructors are much faster than calling
727 * the generic one passing it a python string and are noticably
728 * faster than calling a python new() wrapper. Thats important for
729 * code that wants to make hashes of a bunch of small strings.
730 */
731#define GEN_CONSTRUCTOR(NAME) \
732 static PyObject * \
733 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
734 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000735 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000736 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000737 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000738 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000739 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000740 return NULL; \
741 } \
742 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000743 if (data_obj) \
Gregory P. Smith365a1862009-02-12 07:35:29 +0000744 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000745 \
746 ret_obj = EVPnew( \
747 CONST_ ## NAME ## _name_obj, \
748 NULL, \
749 CONST_new_ ## NAME ## _ctx_p, \
750 (unsigned char*)view.buf, \
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000751 view.len); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000752 \
753 if (data_obj) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000754 PyBuffer_Release(&view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000755 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000756 }
757
758/* a PyMethodDef structure for the constructor */
759#define CONSTRUCTOR_METH_DEF(NAME) \
760 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
761 PyDoc_STR("Returns a " #NAME \
762 " hash object; optionally initialized with a string") \
763 }
764
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800765/* used in the init function to setup a constructor: initialize OpenSSL
766 constructor constants if they haven't been initialized already. */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000767#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800768 if (CONST_ ## NAME ## _name_obj == NULL) { \
769 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
770 if (EVP_get_digestbyname(#NAME)) { \
771 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
772 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
773 } \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000774 } \
775} while (0);
776
777GEN_CONSTRUCTOR(md5)
778GEN_CONSTRUCTOR(sha1)
779GEN_CONSTRUCTOR(sha224)
780GEN_CONSTRUCTOR(sha256)
781GEN_CONSTRUCTOR(sha384)
782GEN_CONSTRUCTOR(sha512)
783
784/* List of functions exported by this module */
785
786static struct PyMethodDef EVP_functions[] = {
787 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
Christian Heimese92ef132013-10-13 00:52:43 +0200788#ifdef PY_PBKDF2_HMAC
789 {"pbkdf2_hmac", (PyCFunction)pbkdf2_hmac, METH_VARARGS|METH_KEYWORDS,
790 pbkdf2_hmac__doc__},
791#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000792 CONSTRUCTOR_METH_DEF(md5),
793 CONSTRUCTOR_METH_DEF(sha1),
794 CONSTRUCTOR_METH_DEF(sha224),
795 CONSTRUCTOR_METH_DEF(sha256),
796 CONSTRUCTOR_METH_DEF(sha384),
797 CONSTRUCTOR_METH_DEF(sha512),
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000798 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000799};
800
801
802/* Initialize this module. */
803
Martin v. Löwis1a214512008-06-11 05:26:20 +0000804
805static struct PyModuleDef _hashlibmodule = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000806 PyModuleDef_HEAD_INIT,
807 "_hashlib",
808 NULL,
809 -1,
810 EVP_functions,
811 NULL,
812 NULL,
813 NULL,
814 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000815};
816
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000817PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000818PyInit__hashlib(void)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000819{
Gregory P. Smith13b55292010-09-06 08:30:23 +0000820 PyObject *m, *openssl_md_meth_names;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000821
822 OpenSSL_add_all_digests();
Christian Heimesb7ddbc82013-10-21 19:48:22 +0200823 ERR_load_crypto_strings();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000824
825 /* TODO build EVP_functions openssl_* entries dynamically based
826 * on what hashes are supported rather than listing many
827 * but having some be unsupported. Only init appropriate
828 * constants. */
829
Christian Heimes90aa7642007-12-19 02:45:37 +0000830 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000831 if (PyType_Ready(&EVPtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000832 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000833
Martin v. Löwis1a214512008-06-11 05:26:20 +0000834 m = PyModule_Create(&_hashlibmodule);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000835 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000836 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000837
Gregory P. Smith13b55292010-09-06 08:30:23 +0000838 openssl_md_meth_names = generate_hash_name_list();
839 if (openssl_md_meth_names == NULL) {
840 Py_DECREF(m);
841 return NULL;
842 }
843 if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) {
844 Py_DECREF(m);
845 return NULL;
846 }
847
Christian Heimes327dd732013-10-22 15:05:23 +0200848 Py_INCREF((PyObject *)&EVPtype);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000849 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000850
851 /* these constants are used by the convenience constructors */
852 INIT_CONSTRUCTOR_CONSTANTS(md5);
853 INIT_CONSTRUCTOR_CONSTANTS(sha1);
854 INIT_CONSTRUCTOR_CONSTANTS(sha224);
855 INIT_CONSTRUCTOR_CONSTANTS(sha256);
856 INIT_CONSTRUCTOR_CONSTANTS(sha384);
857 INIT_CONSTRUCTOR_CONSTANTS(sha512);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000858 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000859}