blob: 1a11bb5b513ebbd9c5461e1f9ee5c6a5c1c37d9c [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>
Christian Heimese7236222013-10-19 14:24:44 +020023#include <openssl/hmac.h>
Gregory P. Smith13b55292010-09-06 08:30:23 +000024/* We use the object interface to discover what hashes OpenSSL supports. */
25#include <openssl/objects.h>
Christian Heimese92ef132013-10-13 00:52:43 +020026#include "openssl/err.h"
Gregory P. Smith3f61d612009-05-04 00:45:33 +000027
28#define MUNCH_SIZE INT_MAX
29
Gregory P. Smith3f61d612009-05-04 00:45:33 +000030#ifndef HASH_OBJ_CONSTRUCTOR
31#define HASH_OBJ_CONSTRUCTOR 0
32#endif
33
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000034/* Minimum OpenSSL version needed to support sha224 and higher. */
35#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x00908000)
36#define _OPENSSL_SUPPORTS_SHA2
37#endif
Gregory P. Smith3f61d612009-05-04 00:45:33 +000038
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000039typedef struct {
40 PyObject_HEAD
41 PyObject *name; /* name of this hash algorithm */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000042 EVP_MD_CTX ctx; /* OpenSSL message digest context */
43#ifdef WITH_THREAD
44 PyThread_type_lock lock; /* OpenSSL context lock */
45#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000046} EVPobject;
47
48
49static PyTypeObject EVPtype;
50
51
52#define DEFINE_CONSTS_FOR_NEW(Name) \
Gregory P. Smithaded2e52013-02-01 17:05:29 -080053 static PyObject *CONST_ ## Name ## _name_obj = NULL; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000054 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
55 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
56
Neal Norwitzf0459142006-01-07 21:20:24 +000057DEFINE_CONSTS_FOR_NEW(md5)
58DEFINE_CONSTS_FOR_NEW(sha1)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000059#ifdef _OPENSSL_SUPPORTS_SHA2
Neal Norwitzf0459142006-01-07 21:20:24 +000060DEFINE_CONSTS_FOR_NEW(sha224)
61DEFINE_CONSTS_FOR_NEW(sha256)
62DEFINE_CONSTS_FOR_NEW(sha384)
63DEFINE_CONSTS_FOR_NEW(sha512)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +000064#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000065
66
67static EVPobject *
68newEVPobject(PyObject *name)
69{
70 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
71
72 /* save the name for .name to return */
73 if (retval != NULL) {
74 Py_INCREF(name);
75 retval->name = name;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000076#ifdef WITH_THREAD
77 retval->lock = NULL;
78#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000079 }
80
81 return retval;
82}
83
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000084static void
85EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
86{
87 unsigned int process;
88 const unsigned char *cp = (const unsigned char *)vp;
89 while (0 < len) {
90 if (len > (Py_ssize_t)MUNCH_SIZE)
91 process = MUNCH_SIZE;
92 else
93 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
94 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
95 len -= process;
96 cp += process;
97 }
98}
99
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000100/* Internal methods for a hash object */
101
102static void
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000103EVP_dealloc(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000104{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000105#ifdef WITH_THREAD
106 if (self->lock != NULL)
107 PyThread_free_lock(self->lock);
108#endif
109 EVP_MD_CTX_cleanup(&self->ctx);
110 Py_XDECREF(self->name);
111 PyObject_Del(self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000112}
113
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000114static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
115{
116 ENTER_HASHLIB(self);
117 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
118 LEAVE_HASHLIB(self);
119}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000120
121/* External methods for a hash object */
122
123PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
124
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000125
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000126static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000127EVP_copy(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000128{
129 EVPobject *newobj;
130
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000131 if ( (newobj = newEVPobject(self->name))==NULL)
132 return NULL;
133
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000134 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000135 return (PyObject *)newobj;
136}
137
138PyDoc_STRVAR(EVP_digest__doc__,
139"Return the digest value as a string of binary data.");
140
141static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000142EVP_digest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000143{
144 unsigned char digest[EVP_MAX_MD_SIZE];
145 EVP_MD_CTX temp_ctx;
146 PyObject *retval;
147 unsigned int digest_size;
148
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000149 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000150 digest_size = EVP_MD_CTX_size(&temp_ctx);
Neal Norwitzf0459142006-01-07 21:20:24 +0000151 EVP_DigestFinal(&temp_ctx, digest, NULL);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000152
Christian Heimes72b710a2008-05-26 13:28:38 +0000153 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000154 EVP_MD_CTX_cleanup(&temp_ctx);
155 return retval;
156}
157
158PyDoc_STRVAR(EVP_hexdigest__doc__,
159"Return the digest value as a string of hexadecimal digits.");
160
161static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000162EVP_hexdigest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000163{
164 unsigned char digest[EVP_MAX_MD_SIZE];
165 EVP_MD_CTX temp_ctx;
166 PyObject *retval;
167 char *hex_digest;
168 unsigned int i, j, digest_size;
169
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000170 /* Get the raw (binary) digest value */
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000171 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000172 digest_size = EVP_MD_CTX_size(&temp_ctx);
173 EVP_DigestFinal(&temp_ctx, digest, NULL);
174
175 EVP_MD_CTX_cleanup(&temp_ctx);
176
Guido van Rossumf8953072007-07-10 13:20:29 +0000177 /* Allocate a new buffer */
178 hex_digest = PyMem_Malloc(digest_size * 2 + 1);
179 if (!hex_digest)
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000180 return PyErr_NoMemory();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000181
182 /* Make hex version of the digest */
183 for(i=j=0; i<digest_size; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200184 unsigned char c;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000185 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200186 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000187 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200188 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000189 }
Guido van Rossumf8953072007-07-10 13:20:29 +0000190 retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2);
191 PyMem_Free(hex_digest);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000192 return retval;
193}
194
195PyDoc_STRVAR(EVP_update__doc__,
196"Update this hash object's state with the provided string.");
197
198static PyObject *
199EVP_update(EVPobject *self, PyObject *args)
200{
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000201 PyObject *obj;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000202 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000203
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000204 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000205 return NULL;
206
Gregory P. Smith365a1862009-02-12 07:35:29 +0000207 GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000208
209#ifdef WITH_THREAD
210 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
211 self->lock = PyThread_allocate_lock();
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000212 /* fail? lock = NULL and we fail over to non-threaded code. */
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000213 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000214
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000215 if (self->lock != NULL) {
216 Py_BEGIN_ALLOW_THREADS
217 PyThread_acquire_lock(self->lock, 1);
218 EVP_hash(self, view.buf, view.len);
219 PyThread_release_lock(self->lock);
220 Py_END_ALLOW_THREADS
221 } else {
222 EVP_hash(self, view.buf, view.len);
223 }
224#else
225 EVP_hash(self, view.buf, view.len);
226#endif
227
228 PyBuffer_Release(&view);
229 Py_RETURN_NONE;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000230}
231
232static PyMethodDef EVP_methods[] = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000233 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
234 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000235 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000236 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
237 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000238};
239
240static PyObject *
241EVP_get_block_size(EVPobject *self, void *closure)
242{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000243 long block_size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000244 block_size = EVP_MD_CTX_block_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000245 return PyLong_FromLong(block_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000246}
247
248static PyObject *
249EVP_get_digest_size(EVPobject *self, void *closure)
250{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000251 long size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000252 size = EVP_MD_CTX_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000253 return PyLong_FromLong(size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000254}
255
256static PyMemberDef EVP_members[] = {
257 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
258 {NULL} /* Sentinel */
259};
260
261static PyGetSetDef EVP_getseters[] = {
262 {"digest_size",
263 (getter)EVP_get_digest_size, NULL,
264 NULL,
265 NULL},
266 {"block_size",
267 (getter)EVP_get_block_size, NULL,
268 NULL,
269 NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000270 {NULL} /* Sentinel */
271};
272
273
274static PyObject *
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000275EVP_repr(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000276{
Victor Stinner3f1af5c2010-03-12 17:00:41 +0000277 return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000278}
279
280#if HASH_OBJ_CONSTRUCTOR
281static int
282EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
283{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000284 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000285 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000286 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000287 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000288 char *nameStr;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000289 const EVP_MD *digest;
290
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000291 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
292 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000293 return -1;
294 }
295
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000296 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000297 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000298
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000299 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
300 PyErr_SetString(PyExc_TypeError, "name must be a string");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000301 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000302 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000303 return -1;
304 }
305
306 digest = EVP_get_digestbyname(nameStr);
307 if (!digest) {
308 PyErr_SetString(PyExc_ValueError, "unknown hash function");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000309 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000310 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000311 return -1;
312 }
313 EVP_DigestInit(&self->ctx, digest);
314
315 self->name = name_obj;
316 Py_INCREF(self->name);
317
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000318 if (data_obj) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000319 if (view.len >= HASHLIB_GIL_MINSIZE) {
320 Py_BEGIN_ALLOW_THREADS
321 EVP_hash(self, view.buf, view.len);
322 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000323 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000324 EVP_hash(self, view.buf, view.len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000325 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000326 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000327 }
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000328
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000329 return 0;
330}
331#endif
332
333
334PyDoc_STRVAR(hashtype_doc,
335"A hash represents the object used to calculate a checksum of a\n\
336string of information.\n\
337\n\
338Methods:\n\
339\n\
340update() -- updates the current digest with an additional string\n\
341digest() -- return the current digest value\n\
342hexdigest() -- return the current digest as a string of hexadecimal digits\n\
343copy() -- return a copy of the current hash object\n\
344\n\
345Attributes:\n\
346\n\
347name -- the hash algorithm being used by this object\n\
348digest_size -- number of bytes in this hashes output\n");
349
350static PyTypeObject EVPtype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000351 PyVarObject_HEAD_INIT(NULL, 0)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000352 "_hashlib.HASH", /*tp_name*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000353 sizeof(EVPobject), /*tp_basicsize*/
354 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000355 /* methods */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000356 (destructor)EVP_dealloc, /*tp_dealloc*/
357 0, /*tp_print*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000358 0, /*tp_getattr*/
359 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000360 0, /*tp_reserved*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000361 (reprfunc)EVP_repr, /*tp_repr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000362 0, /*tp_as_number*/
363 0, /*tp_as_sequence*/
364 0, /*tp_as_mapping*/
365 0, /*tp_hash*/
366 0, /*tp_call*/
367 0, /*tp_str*/
368 0, /*tp_getattro*/
369 0, /*tp_setattro*/
370 0, /*tp_as_buffer*/
371 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
372 hashtype_doc, /*tp_doc*/
373 0, /*tp_traverse*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000374 0, /*tp_clear*/
375 0, /*tp_richcompare*/
376 0, /*tp_weaklistoffset*/
377 0, /*tp_iter*/
378 0, /*tp_iternext*/
379 EVP_methods, /* tp_methods */
380 EVP_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000381 EVP_getseters, /* tp_getset */
382#if 1
383 0, /* tp_base */
384 0, /* tp_dict */
385 0, /* tp_descr_get */
386 0, /* tp_descr_set */
387 0, /* tp_dictoffset */
388#endif
389#if HASH_OBJ_CONSTRUCTOR
390 (initproc)EVP_tp_init, /* tp_init */
391#endif
392};
393
394static PyObject *
395EVPnew(PyObject *name_obj,
396 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000397 const unsigned char *cp, Py_ssize_t len)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000398{
399 EVPobject *self;
400
401 if (!digest && !initial_ctx) {
402 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
403 return NULL;
404 }
405
406 if ((self = newEVPobject(name_obj)) == NULL)
407 return NULL;
408
409 if (initial_ctx) {
410 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
411 } else {
412 EVP_DigestInit(&self->ctx, digest);
413 }
414
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000415 if (cp && len) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000416 if (len >= HASHLIB_GIL_MINSIZE) {
417 Py_BEGIN_ALLOW_THREADS
418 EVP_hash(self, cp, len);
419 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000420 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000421 EVP_hash(self, cp, len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000422 }
423 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000424
425 return (PyObject *)self;
426}
427
428
429/* The module-level function: new() */
430
431PyDoc_STRVAR(EVP_new__doc__,
432"Return a new hash object using the named algorithm.\n\
433An optional string argument may be provided and will be\n\
434automatically hashed.\n\
435\n\
436The MD5 and SHA1 algorithms are always supported.\n");
437
438static PyObject *
439EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
440{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000441 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000442 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000443 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000444 Py_buffer view = { 0 };
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000445 PyObject *ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000446 char *name;
447 const EVP_MD *digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000448
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000449 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
450 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000451 return NULL;
452 }
453
454 if (!PyArg_Parse(name_obj, "s", &name)) {
455 PyErr_SetString(PyExc_TypeError, "name must be a string");
456 return NULL;
457 }
458
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000459 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000460 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000461
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000462 digest = EVP_get_digestbyname(name);
463
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000464 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000465
466 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000467 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000468 return ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000469}
470
Christian Heimese7236222013-10-19 14:24:44 +0200471
472
Christian Heimes351f5392013-10-19 17:59:48 +0200473#if (OPENSSL_VERSION_NUMBER >= 0x10000000 && !defined(OPENSSL_NO_HMAC) \
474 && !defined(OPENSSL_NO_SHA))
Christian Heimese7236222013-10-19 14:24:44 +0200475
Christian Heimese92ef132013-10-13 00:52:43 +0200476#define PY_PBKDF2_HMAC 1
477
Christian Heimese7236222013-10-19 14:24:44 +0200478/* Improved implementation of PKCS5_PBKDF2_HMAC()
479 *
480 * PKCS5_PBKDF2_HMAC_fast() hashes the password exactly one time instead of
481 * `iter` times. Today (2013) the iteration count is typically 100,000 or
482 * more. The improved algorithm is not subject to a Denial-of-Service
483 * vulnerability with overly large passwords.
484 *
485 * Also OpenSSL < 1.0 don't provide PKCS5_PBKDF2_HMAC(), only
486 * PKCS5_PBKDF2_SHA1.
487 */
Christian Heimesc6564b92013-10-20 13:23:03 +0200488static int
489PKCS5_PBKDF2_HMAC_fast(const char *pass, int passlen,
490 const unsigned char *salt, int saltlen,
491 int iter, const EVP_MD *digest,
492 int keylen, unsigned char *out)
Christian Heimese7236222013-10-19 14:24:44 +0200493{
494 unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
495 int cplen, j, k, tkeylen, mdlen;
496 unsigned long i = 1;
497 HMAC_CTX hctx_tpl, hctx;
498
499 mdlen = EVP_MD_size(digest);
500 if (mdlen < 0)
501 return 0;
502
503 HMAC_CTX_init(&hctx_tpl);
504 HMAC_CTX_init(&hctx);
505 p = out;
506 tkeylen = keylen;
507 if (!pass)
508 passlen = 0;
509 else if(passlen == -1)
510 passlen = strlen(pass);
511 if (!HMAC_Init_ex(&hctx_tpl, pass, passlen, digest, NULL)) {
512 HMAC_CTX_cleanup(&hctx_tpl);
513 return 0;
514 }
515 while(tkeylen) {
516 if(tkeylen > mdlen)
517 cplen = mdlen;
518 else
519 cplen = tkeylen;
520 /* We are unlikely to ever use more than 256 blocks (5120 bits!)
521 * but just in case...
522 */
523 itmp[0] = (unsigned char)((i >> 24) & 0xff);
524 itmp[1] = (unsigned char)((i >> 16) & 0xff);
525 itmp[2] = (unsigned char)((i >> 8) & 0xff);
526 itmp[3] = (unsigned char)(i & 0xff);
527 if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) {
528 HMAC_CTX_cleanup(&hctx_tpl);
529 return 0;
530 }
531 if (!HMAC_Update(&hctx, salt, saltlen)
532 || !HMAC_Update(&hctx, itmp, 4)
533 || !HMAC_Final(&hctx, digtmp, NULL)) {
534 HMAC_CTX_cleanup(&hctx_tpl);
535 HMAC_CTX_cleanup(&hctx);
536 return 0;
537 }
Christian Heimes68531082013-11-06 17:25:17 +0100538 HMAC_CTX_cleanup(&hctx);
Christian Heimese7236222013-10-19 14:24:44 +0200539 memcpy(p, digtmp, cplen);
540 for (j = 1; j < iter; j++) {
541 if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) {
542 HMAC_CTX_cleanup(&hctx_tpl);
543 return 0;
544 }
545 if (!HMAC_Update(&hctx, digtmp, mdlen)
546 || !HMAC_Final(&hctx, digtmp, NULL)) {
547 HMAC_CTX_cleanup(&hctx_tpl);
548 HMAC_CTX_cleanup(&hctx);
549 return 0;
550 }
551 HMAC_CTX_cleanup(&hctx);
552 for (k = 0; k < cplen; k++) {
553 p[k] ^= digtmp[k];
554 }
555 }
556 tkeylen-= cplen;
557 i++;
558 p+= cplen;
559 }
560 HMAC_CTX_cleanup(&hctx_tpl);
561 return 1;
562}
563
Brett Cannon2be28a62013-11-01 10:25:13 -0400564static PyObject *
565_setException(PyObject *exc)
566{
567 unsigned long errcode;
568 const char *lib, *func, *reason;
569
570 errcode = ERR_peek_last_error();
571 if (!errcode) {
572 PyErr_SetString(exc, "unknown reasons");
573 return NULL;
574 }
575 ERR_clear_error();
576
577 lib = ERR_lib_error_string(errcode);
578 func = ERR_func_error_string(errcode);
579 reason = ERR_reason_error_string(errcode);
580
581 if (lib && func) {
582 PyErr_Format(exc, "[%s: %s] %s", lib, func, reason);
583 }
584 else if (lib) {
585 PyErr_Format(exc, "[%s] %s", lib, reason);
586 }
587 else {
588 PyErr_SetString(exc, reason);
589 }
590 return NULL;
591}
Christian Heimese7236222013-10-19 14:24:44 +0200592
Christian Heimese92ef132013-10-13 00:52:43 +0200593PyDoc_STRVAR(pbkdf2_hmac__doc__,
594"pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None) -> key\n\
595\n\
596Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as\n\
597pseudorandom function.");
598
599static PyObject *
600pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict)
601{
602 static char *kwlist[] = {"hash_name", "password", "salt", "iterations",
603 "dklen", NULL};
604 PyObject *key_obj = NULL, *dklen_obj = Py_None;
605 char *name, *key;
606 Py_buffer password, salt;
607 long iterations, dklen;
608 int retval;
609 const EVP_MD *digest;
610
611 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "sy*y*l|O:pbkdf2_hmac",
612 kwlist, &name, &password, &salt,
613 &iterations, &dklen_obj)) {
614 return NULL;
615 }
616
617 digest = EVP_get_digestbyname(name);
618 if (digest == NULL) {
619 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
620 goto end;
621 }
622
623 if (password.len > INT_MAX) {
624 PyErr_SetString(PyExc_OverflowError,
625 "password is too long.");
626 goto end;
627 }
628
629 if (salt.len > INT_MAX) {
630 PyErr_SetString(PyExc_OverflowError,
631 "salt is too long.");
632 goto end;
633 }
634
635 if (iterations < 1) {
636 PyErr_SetString(PyExc_ValueError,
637 "iteration value must be greater than 0.");
638 goto end;
639 }
640 if (iterations > INT_MAX) {
641 PyErr_SetString(PyExc_OverflowError,
642 "iteration value is too great.");
643 goto end;
644 }
645
646 if (dklen_obj == Py_None) {
647 dklen = EVP_MD_size(digest);
648 } else {
649 dklen = PyLong_AsLong(dklen_obj);
650 if ((dklen == -1) && PyErr_Occurred()) {
651 goto end;
652 }
653 }
654 if (dklen < 1) {
655 PyErr_SetString(PyExc_ValueError,
656 "key length must be greater than 0.");
657 goto end;
658 }
659 if (dklen > INT_MAX) {
660 /* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */
661 PyErr_SetString(PyExc_OverflowError,
662 "key length is too great.");
663 goto end;
664 }
665
666 key_obj = PyBytes_FromStringAndSize(NULL, dklen);
667 if (key_obj == NULL) {
668 goto end;
669 }
670 key = PyBytes_AS_STRING(key_obj);
671
672 Py_BEGIN_ALLOW_THREADS
Christian Heimese7236222013-10-19 14:24:44 +0200673 retval = PKCS5_PBKDF2_HMAC_fast((char*)password.buf, password.len,
674 (unsigned char *)salt.buf, salt.len,
675 iterations, digest, dklen,
676 (unsigned char *)key);
Christian Heimese92ef132013-10-13 00:52:43 +0200677 Py_END_ALLOW_THREADS
678
679 if (!retval) {
680 Py_CLEAR(key_obj);
681 _setException(PyExc_ValueError);
682 goto end;
683 }
684
685 end:
686 PyBuffer_Release(&password);
687 PyBuffer_Release(&salt);
688 return key_obj;
689}
690
691#endif
Gregory P. Smith13b55292010-09-06 08:30:23 +0000692
693/* State for our callback function so that it can accumulate a result. */
694typedef struct _internal_name_mapper_state {
695 PyObject *set;
696 int error;
697} _InternalNameMapperState;
698
699
700/* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
701static void
702_openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg)
703{
704 _InternalNameMapperState *state = (_InternalNameMapperState *)arg;
705 PyObject *py_name;
706
707 assert(state != NULL);
708 if (openssl_obj_name == NULL)
709 return;
710 /* Ignore aliased names, they pollute the list and OpenSSL appears to
711 * have a its own definition of alias as the resulting list still
712 * contains duplicate and alternate names for several algorithms. */
713 if (openssl_obj_name->alias)
714 return;
715
716 py_name = PyUnicode_FromString(openssl_obj_name->name);
717 if (py_name == NULL) {
718 state->error = 1;
719 } else {
720 if (PySet_Add(state->set, py_name) != 0) {
Gregory P. Smith13b55292010-09-06 08:30:23 +0000721 state->error = 1;
722 }
Christian Heimesdb816d62013-10-29 12:14:55 +0100723 Py_DECREF(py_name);
Gregory P. Smith13b55292010-09-06 08:30:23 +0000724 }
725}
726
727
728/* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */
729static PyObject*
730generate_hash_name_list(void)
731{
732 _InternalNameMapperState state;
733 state.set = PyFrozenSet_New(NULL);
734 if (state.set == NULL)
735 return NULL;
736 state.error = 0;
737
738 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state);
739
740 if (state.error) {
741 Py_DECREF(state.set);
742 return NULL;
743 }
744 return state.set;
745}
746
747
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000748/*
749 * This macro generates constructor function definitions for specific
750 * hash algorithms. These constructors are much faster than calling
751 * the generic one passing it a python string and are noticably
752 * faster than calling a python new() wrapper. Thats important for
753 * code that wants to make hashes of a bunch of small strings.
754 */
755#define GEN_CONSTRUCTOR(NAME) \
756 static PyObject * \
757 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
758 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000759 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000760 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000761 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000762 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000763 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000764 return NULL; \
765 } \
766 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000767 if (data_obj) \
Gregory P. Smith365a1862009-02-12 07:35:29 +0000768 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000769 \
770 ret_obj = EVPnew( \
771 CONST_ ## NAME ## _name_obj, \
772 NULL, \
773 CONST_new_ ## NAME ## _ctx_p, \
774 (unsigned char*)view.buf, \
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000775 view.len); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000776 \
777 if (data_obj) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000778 PyBuffer_Release(&view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000779 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000780 }
781
782/* a PyMethodDef structure for the constructor */
783#define CONSTRUCTOR_METH_DEF(NAME) \
784 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
785 PyDoc_STR("Returns a " #NAME \
786 " hash object; optionally initialized with a string") \
787 }
788
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800789/* used in the init function to setup a constructor: initialize OpenSSL
790 constructor constants if they haven't been initialized already. */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000791#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800792 if (CONST_ ## NAME ## _name_obj == NULL) { \
793 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
794 if (EVP_get_digestbyname(#NAME)) { \
795 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
796 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
797 } \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000798 } \
799} while (0);
800
801GEN_CONSTRUCTOR(md5)
802GEN_CONSTRUCTOR(sha1)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000803#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000804GEN_CONSTRUCTOR(sha224)
805GEN_CONSTRUCTOR(sha256)
806GEN_CONSTRUCTOR(sha384)
807GEN_CONSTRUCTOR(sha512)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000808#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000809
810/* List of functions exported by this module */
811
812static struct PyMethodDef EVP_functions[] = {
813 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
Christian Heimese92ef132013-10-13 00:52:43 +0200814#ifdef PY_PBKDF2_HMAC
815 {"pbkdf2_hmac", (PyCFunction)pbkdf2_hmac, METH_VARARGS|METH_KEYWORDS,
816 pbkdf2_hmac__doc__},
817#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000818 CONSTRUCTOR_METH_DEF(md5),
819 CONSTRUCTOR_METH_DEF(sha1),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000820#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000821 CONSTRUCTOR_METH_DEF(sha224),
822 CONSTRUCTOR_METH_DEF(sha256),
823 CONSTRUCTOR_METH_DEF(sha384),
824 CONSTRUCTOR_METH_DEF(sha512),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000825#endif
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000826 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000827};
828
829
830/* Initialize this module. */
831
Martin v. Löwis1a214512008-06-11 05:26:20 +0000832
833static struct PyModuleDef _hashlibmodule = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000834 PyModuleDef_HEAD_INIT,
835 "_hashlib",
836 NULL,
837 -1,
838 EVP_functions,
839 NULL,
840 NULL,
841 NULL,
842 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000843};
844
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000845PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000846PyInit__hashlib(void)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000847{
Gregory P. Smith13b55292010-09-06 08:30:23 +0000848 PyObject *m, *openssl_md_meth_names;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000849
850 OpenSSL_add_all_digests();
Christian Heimesb7ddbc82013-10-21 19:48:22 +0200851 ERR_load_crypto_strings();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000852
853 /* TODO build EVP_functions openssl_* entries dynamically based
854 * on what hashes are supported rather than listing many
855 * but having some be unsupported. Only init appropriate
856 * constants. */
857
Christian Heimes90aa7642007-12-19 02:45:37 +0000858 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000859 if (PyType_Ready(&EVPtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000860 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000861
Martin v. Löwis1a214512008-06-11 05:26:20 +0000862 m = PyModule_Create(&_hashlibmodule);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000863 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000864 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000865
Gregory P. Smith13b55292010-09-06 08:30:23 +0000866 openssl_md_meth_names = generate_hash_name_list();
867 if (openssl_md_meth_names == NULL) {
868 Py_DECREF(m);
869 return NULL;
870 }
871 if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) {
872 Py_DECREF(m);
873 return NULL;
874 }
875
Christian Heimes327dd732013-10-22 15:05:23 +0200876 Py_INCREF((PyObject *)&EVPtype);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000877 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000878
879 /* these constants are used by the convenience constructors */
880 INIT_CONSTRUCTOR_CONSTANTS(md5);
881 INIT_CONSTRUCTOR_CONSTANTS(sha1);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000882#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000883 INIT_CONSTRUCTOR_CONSTANTS(sha224);
884 INIT_CONSTRUCTOR_CONSTANTS(sha256);
885 INIT_CONSTRUCTOR_CONSTANTS(sha384);
886 INIT_CONSTRUCTOR_CONSTANTS(sha512);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000887#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +0000888 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000889}