blob: 5b0a7be12b07c8cb7aca1ea4767a3d86ccb3048b [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;
Christian Heimese7236222013-10-19 14:24:44 +0200507 if (!HMAC_Init_ex(&hctx_tpl, pass, passlen, digest, NULL)) {
508 HMAC_CTX_cleanup(&hctx_tpl);
509 return 0;
510 }
Benjamin Peterson3c0769d2015-09-27 02:13:40 -0700511 while (tkeylen) {
512 if (tkeylen > mdlen)
Christian Heimese7236222013-10-19 14:24:44 +0200513 cplen = mdlen;
514 else
515 cplen = tkeylen;
516 /* We are unlikely to ever use more than 256 blocks (5120 bits!)
517 * but just in case...
518 */
519 itmp[0] = (unsigned char)((i >> 24) & 0xff);
520 itmp[1] = (unsigned char)((i >> 16) & 0xff);
521 itmp[2] = (unsigned char)((i >> 8) & 0xff);
522 itmp[3] = (unsigned char)(i & 0xff);
523 if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) {
524 HMAC_CTX_cleanup(&hctx_tpl);
525 return 0;
526 }
527 if (!HMAC_Update(&hctx, salt, saltlen)
528 || !HMAC_Update(&hctx, itmp, 4)
529 || !HMAC_Final(&hctx, digtmp, NULL)) {
530 HMAC_CTX_cleanup(&hctx_tpl);
531 HMAC_CTX_cleanup(&hctx);
532 return 0;
533 }
Christian Heimes68531082013-11-06 17:25:17 +0100534 HMAC_CTX_cleanup(&hctx);
Christian Heimese7236222013-10-19 14:24:44 +0200535 memcpy(p, digtmp, cplen);
536 for (j = 1; j < iter; j++) {
537 if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) {
538 HMAC_CTX_cleanup(&hctx_tpl);
539 return 0;
540 }
541 if (!HMAC_Update(&hctx, digtmp, mdlen)
542 || !HMAC_Final(&hctx, digtmp, NULL)) {
543 HMAC_CTX_cleanup(&hctx_tpl);
544 HMAC_CTX_cleanup(&hctx);
545 return 0;
546 }
547 HMAC_CTX_cleanup(&hctx);
548 for (k = 0; k < cplen; k++) {
549 p[k] ^= digtmp[k];
550 }
551 }
552 tkeylen-= cplen;
553 i++;
554 p+= cplen;
555 }
556 HMAC_CTX_cleanup(&hctx_tpl);
557 return 1;
558}
559
Christian Heimes48b7df72013-12-05 07:38:13 +0100560/* LCOV_EXCL_START */
Brett Cannon2be28a62013-11-01 10:25:13 -0400561static PyObject *
562_setException(PyObject *exc)
563{
564 unsigned long errcode;
565 const char *lib, *func, *reason;
566
567 errcode = ERR_peek_last_error();
568 if (!errcode) {
569 PyErr_SetString(exc, "unknown reasons");
570 return NULL;
571 }
572 ERR_clear_error();
573
574 lib = ERR_lib_error_string(errcode);
575 func = ERR_func_error_string(errcode);
576 reason = ERR_reason_error_string(errcode);
577
578 if (lib && func) {
579 PyErr_Format(exc, "[%s: %s] %s", lib, func, reason);
580 }
581 else if (lib) {
582 PyErr_Format(exc, "[%s] %s", lib, reason);
583 }
584 else {
585 PyErr_SetString(exc, reason);
586 }
587 return NULL;
588}
Christian Heimes48b7df72013-12-05 07:38:13 +0100589/* LCOV_EXCL_STOP */
Christian Heimese7236222013-10-19 14:24:44 +0200590
Christian Heimese92ef132013-10-13 00:52:43 +0200591PyDoc_STRVAR(pbkdf2_hmac__doc__,
592"pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None) -> key\n\
593\n\
594Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as\n\
595pseudorandom function.");
596
597static PyObject *
598pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict)
599{
600 static char *kwlist[] = {"hash_name", "password", "salt", "iterations",
601 "dklen", NULL};
602 PyObject *key_obj = NULL, *dklen_obj = Py_None;
603 char *name, *key;
604 Py_buffer password, salt;
605 long iterations, dklen;
606 int retval;
607 const EVP_MD *digest;
608
609 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "sy*y*l|O:pbkdf2_hmac",
610 kwlist, &name, &password, &salt,
611 &iterations, &dklen_obj)) {
612 return NULL;
613 }
614
615 digest = EVP_get_digestbyname(name);
616 if (digest == NULL) {
617 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
618 goto end;
619 }
620
621 if (password.len > INT_MAX) {
622 PyErr_SetString(PyExc_OverflowError,
623 "password is too long.");
624 goto end;
625 }
626
627 if (salt.len > INT_MAX) {
628 PyErr_SetString(PyExc_OverflowError,
629 "salt is too long.");
630 goto end;
631 }
632
633 if (iterations < 1) {
634 PyErr_SetString(PyExc_ValueError,
635 "iteration value must be greater than 0.");
636 goto end;
637 }
638 if (iterations > INT_MAX) {
639 PyErr_SetString(PyExc_OverflowError,
640 "iteration value is too great.");
641 goto end;
642 }
643
644 if (dklen_obj == Py_None) {
645 dklen = EVP_MD_size(digest);
646 } else {
647 dklen = PyLong_AsLong(dklen_obj);
648 if ((dklen == -1) && PyErr_Occurred()) {
649 goto end;
650 }
651 }
652 if (dklen < 1) {
653 PyErr_SetString(PyExc_ValueError,
654 "key length must be greater than 0.");
655 goto end;
656 }
657 if (dklen > INT_MAX) {
658 /* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */
659 PyErr_SetString(PyExc_OverflowError,
660 "key length is too great.");
661 goto end;
662 }
663
664 key_obj = PyBytes_FromStringAndSize(NULL, dklen);
665 if (key_obj == NULL) {
666 goto end;
667 }
668 key = PyBytes_AS_STRING(key_obj);
669
670 Py_BEGIN_ALLOW_THREADS
Victor Stinnerc1a57d32013-11-16 00:27:16 +0100671 retval = PKCS5_PBKDF2_HMAC_fast((char*)password.buf, (int)password.len,
Christian Heimescc6cdce2013-11-18 09:59:44 +0100672 (unsigned char *)salt.buf, (int)salt.len,
Christian Heimese7236222013-10-19 14:24:44 +0200673 iterations, digest, dklen,
674 (unsigned char *)key);
Christian Heimese92ef132013-10-13 00:52:43 +0200675 Py_END_ALLOW_THREADS
676
677 if (!retval) {
678 Py_CLEAR(key_obj);
679 _setException(PyExc_ValueError);
680 goto end;
681 }
682
683 end:
684 PyBuffer_Release(&password);
685 PyBuffer_Release(&salt);
686 return key_obj;
687}
688
689#endif
Gregory P. Smith13b55292010-09-06 08:30:23 +0000690
691/* State for our callback function so that it can accumulate a result. */
692typedef struct _internal_name_mapper_state {
693 PyObject *set;
694 int error;
695} _InternalNameMapperState;
696
697
698/* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
699static void
700_openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg)
701{
702 _InternalNameMapperState *state = (_InternalNameMapperState *)arg;
703 PyObject *py_name;
704
705 assert(state != NULL);
706 if (openssl_obj_name == NULL)
707 return;
708 /* Ignore aliased names, they pollute the list and OpenSSL appears to
Martin Panter7462b6492015-11-02 03:37:02 +0000709 * have its own definition of alias as the resulting list still
Gregory P. Smith13b55292010-09-06 08:30:23 +0000710 * contains duplicate and alternate names for several algorithms. */
711 if (openssl_obj_name->alias)
712 return;
713
714 py_name = PyUnicode_FromString(openssl_obj_name->name);
715 if (py_name == NULL) {
716 state->error = 1;
717 } else {
718 if (PySet_Add(state->set, py_name) != 0) {
Gregory P. Smith13b55292010-09-06 08:30:23 +0000719 state->error = 1;
720 }
Christian Heimesdb816d62013-10-29 12:14:55 +0100721 Py_DECREF(py_name);
Gregory P. Smith13b55292010-09-06 08:30:23 +0000722 }
723}
724
725
726/* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */
727static PyObject*
728generate_hash_name_list(void)
729{
730 _InternalNameMapperState state;
731 state.set = PyFrozenSet_New(NULL);
732 if (state.set == NULL)
733 return NULL;
734 state.error = 0;
735
736 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state);
737
738 if (state.error) {
739 Py_DECREF(state.set);
740 return NULL;
741 }
742 return state.set;
743}
744
745
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000746/*
747 * This macro generates constructor function definitions for specific
748 * hash algorithms. These constructors are much faster than calling
749 * the generic one passing it a python string and are noticably
750 * faster than calling a python new() wrapper. Thats important for
751 * code that wants to make hashes of a bunch of small strings.
752 */
753#define GEN_CONSTRUCTOR(NAME) \
754 static PyObject * \
755 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
756 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000757 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000758 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000759 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000760 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000761 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000762 return NULL; \
763 } \
764 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000765 if (data_obj) \
Gregory P. Smith365a1862009-02-12 07:35:29 +0000766 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000767 \
768 ret_obj = EVPnew( \
769 CONST_ ## NAME ## _name_obj, \
770 NULL, \
771 CONST_new_ ## NAME ## _ctx_p, \
772 (unsigned char*)view.buf, \
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000773 view.len); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000774 \
775 if (data_obj) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000776 PyBuffer_Release(&view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000777 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000778 }
779
780/* a PyMethodDef structure for the constructor */
781#define CONSTRUCTOR_METH_DEF(NAME) \
782 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
783 PyDoc_STR("Returns a " #NAME \
784 " hash object; optionally initialized with a string") \
785 }
786
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800787/* used in the init function to setup a constructor: initialize OpenSSL
788 constructor constants if they haven't been initialized already. */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000789#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800790 if (CONST_ ## NAME ## _name_obj == NULL) { \
791 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
792 if (EVP_get_digestbyname(#NAME)) { \
793 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
794 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
795 } \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000796 } \
797} while (0);
798
799GEN_CONSTRUCTOR(md5)
800GEN_CONSTRUCTOR(sha1)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000801#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000802GEN_CONSTRUCTOR(sha224)
803GEN_CONSTRUCTOR(sha256)
804GEN_CONSTRUCTOR(sha384)
805GEN_CONSTRUCTOR(sha512)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000806#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000807
808/* List of functions exported by this module */
809
810static struct PyMethodDef EVP_functions[] = {
811 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
Christian Heimese92ef132013-10-13 00:52:43 +0200812#ifdef PY_PBKDF2_HMAC
813 {"pbkdf2_hmac", (PyCFunction)pbkdf2_hmac, METH_VARARGS|METH_KEYWORDS,
814 pbkdf2_hmac__doc__},
815#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000816 CONSTRUCTOR_METH_DEF(md5),
817 CONSTRUCTOR_METH_DEF(sha1),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000818#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000819 CONSTRUCTOR_METH_DEF(sha224),
820 CONSTRUCTOR_METH_DEF(sha256),
821 CONSTRUCTOR_METH_DEF(sha384),
822 CONSTRUCTOR_METH_DEF(sha512),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000823#endif
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000824 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000825};
826
827
828/* Initialize this module. */
829
Martin v. Löwis1a214512008-06-11 05:26:20 +0000830
831static struct PyModuleDef _hashlibmodule = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000832 PyModuleDef_HEAD_INIT,
833 "_hashlib",
834 NULL,
835 -1,
836 EVP_functions,
837 NULL,
838 NULL,
839 NULL,
840 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000841};
842
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000843PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000844PyInit__hashlib(void)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000845{
Gregory P. Smith13b55292010-09-06 08:30:23 +0000846 PyObject *m, *openssl_md_meth_names;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000847
848 OpenSSL_add_all_digests();
Christian Heimesb7ddbc82013-10-21 19:48:22 +0200849 ERR_load_crypto_strings();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000850
851 /* TODO build EVP_functions openssl_* entries dynamically based
852 * on what hashes are supported rather than listing many
853 * but having some be unsupported. Only init appropriate
854 * constants. */
855
Christian Heimes90aa7642007-12-19 02:45:37 +0000856 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000857 if (PyType_Ready(&EVPtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000858 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000859
Martin v. Löwis1a214512008-06-11 05:26:20 +0000860 m = PyModule_Create(&_hashlibmodule);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000861 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000862 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000863
Gregory P. Smith13b55292010-09-06 08:30:23 +0000864 openssl_md_meth_names = generate_hash_name_list();
865 if (openssl_md_meth_names == NULL) {
866 Py_DECREF(m);
867 return NULL;
868 }
869 if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) {
870 Py_DECREF(m);
871 return NULL;
872 }
873
Christian Heimes327dd732013-10-22 15:05:23 +0200874 Py_INCREF((PyObject *)&EVPtype);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000875 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000876
877 /* these constants are used by the convenience constructors */
878 INIT_CONSTRUCTOR_CONSTANTS(md5);
879 INIT_CONSTRUCTOR_CONSTANTS(sha1);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000880#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000881 INIT_CONSTRUCTOR_CONSTANTS(sha224);
882 INIT_CONSTRUCTOR_CONSTANTS(sha256);
883 INIT_CONSTRUCTOR_CONSTANTS(sha384);
884 INIT_CONSTRUCTOR_CONSTANTS(sha512);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000885#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +0000886 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000887}