blob: a866fbf7f95dfd88c702f98ad41b5dfeed20817b [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 }
511 while(tkeylen) {
512 if(tkeylen > mdlen)
513 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
Brett Cannon2be28a62013-11-01 10:25:13 -0400560static PyObject *
561_setException(PyObject *exc)
562{
563 unsigned long errcode;
564 const char *lib, *func, *reason;
565
566 errcode = ERR_peek_last_error();
567 if (!errcode) {
568 PyErr_SetString(exc, "unknown reasons");
569 return NULL;
570 }
571 ERR_clear_error();
572
573 lib = ERR_lib_error_string(errcode);
574 func = ERR_func_error_string(errcode);
575 reason = ERR_reason_error_string(errcode);
576
577 if (lib && func) {
578 PyErr_Format(exc, "[%s: %s] %s", lib, func, reason);
579 }
580 else if (lib) {
581 PyErr_Format(exc, "[%s] %s", lib, reason);
582 }
583 else {
584 PyErr_SetString(exc, reason);
585 }
586 return NULL;
587}
Christian Heimese7236222013-10-19 14:24:44 +0200588
Christian Heimese92ef132013-10-13 00:52:43 +0200589PyDoc_STRVAR(pbkdf2_hmac__doc__,
590"pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None) -> key\n\
591\n\
592Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as\n\
593pseudorandom function.");
594
595static PyObject *
596pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict)
597{
598 static char *kwlist[] = {"hash_name", "password", "salt", "iterations",
599 "dklen", NULL};
600 PyObject *key_obj = NULL, *dklen_obj = Py_None;
601 char *name, *key;
602 Py_buffer password, salt;
603 long iterations, dklen;
604 int retval;
605 const EVP_MD *digest;
606
607 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "sy*y*l|O:pbkdf2_hmac",
608 kwlist, &name, &password, &salt,
609 &iterations, &dklen_obj)) {
610 return NULL;
611 }
612
613 digest = EVP_get_digestbyname(name);
614 if (digest == NULL) {
615 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
616 goto end;
617 }
618
619 if (password.len > INT_MAX) {
620 PyErr_SetString(PyExc_OverflowError,
621 "password is too long.");
622 goto end;
623 }
624
625 if (salt.len > INT_MAX) {
626 PyErr_SetString(PyExc_OverflowError,
627 "salt is too long.");
628 goto end;
629 }
630
631 if (iterations < 1) {
632 PyErr_SetString(PyExc_ValueError,
633 "iteration value must be greater than 0.");
634 goto end;
635 }
636 if (iterations > INT_MAX) {
637 PyErr_SetString(PyExc_OverflowError,
638 "iteration value is too great.");
639 goto end;
640 }
641
642 if (dklen_obj == Py_None) {
643 dklen = EVP_MD_size(digest);
644 } else {
645 dklen = PyLong_AsLong(dklen_obj);
646 if ((dklen == -1) && PyErr_Occurred()) {
647 goto end;
648 }
649 }
650 if (dklen < 1) {
651 PyErr_SetString(PyExc_ValueError,
652 "key length must be greater than 0.");
653 goto end;
654 }
655 if (dklen > INT_MAX) {
656 /* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */
657 PyErr_SetString(PyExc_OverflowError,
658 "key length is too great.");
659 goto end;
660 }
661
662 key_obj = PyBytes_FromStringAndSize(NULL, dklen);
663 if (key_obj == NULL) {
664 goto end;
665 }
666 key = PyBytes_AS_STRING(key_obj);
667
668 Py_BEGIN_ALLOW_THREADS
Victor Stinnerc1a57d32013-11-16 00:27:16 +0100669 retval = PKCS5_PBKDF2_HMAC_fast((char*)password.buf, (int)password.len,
Christian Heimescc6cdce2013-11-18 09:59:44 +0100670 (unsigned char *)salt.buf, (int)salt.len,
Christian Heimese7236222013-10-19 14:24:44 +0200671 iterations, digest, dklen,
672 (unsigned char *)key);
Christian Heimese92ef132013-10-13 00:52:43 +0200673 Py_END_ALLOW_THREADS
674
675 if (!retval) {
676 Py_CLEAR(key_obj);
677 _setException(PyExc_ValueError);
678 goto end;
679 }
680
681 end:
682 PyBuffer_Release(&password);
683 PyBuffer_Release(&salt);
684 return key_obj;
685}
686
687#endif
Gregory P. Smith13b55292010-09-06 08:30:23 +0000688
689/* State for our callback function so that it can accumulate a result. */
690typedef struct _internal_name_mapper_state {
691 PyObject *set;
692 int error;
693} _InternalNameMapperState;
694
695
696/* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
697static void
698_openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg)
699{
700 _InternalNameMapperState *state = (_InternalNameMapperState *)arg;
701 PyObject *py_name;
702
703 assert(state != NULL);
704 if (openssl_obj_name == NULL)
705 return;
706 /* Ignore aliased names, they pollute the list and OpenSSL appears to
707 * have a its own definition of alias as the resulting list still
708 * contains duplicate and alternate names for several algorithms. */
709 if (openssl_obj_name->alias)
710 return;
711
712 py_name = PyUnicode_FromString(openssl_obj_name->name);
713 if (py_name == NULL) {
714 state->error = 1;
715 } else {
716 if (PySet_Add(state->set, py_name) != 0) {
Gregory P. Smith13b55292010-09-06 08:30:23 +0000717 state->error = 1;
718 }
Christian Heimesdb816d62013-10-29 12:14:55 +0100719 Py_DECREF(py_name);
Gregory P. Smith13b55292010-09-06 08:30:23 +0000720 }
721}
722
723
724/* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */
725static PyObject*
726generate_hash_name_list(void)
727{
728 _InternalNameMapperState state;
729 state.set = PyFrozenSet_New(NULL);
730 if (state.set == NULL)
731 return NULL;
732 state.error = 0;
733
734 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state);
735
736 if (state.error) {
737 Py_DECREF(state.set);
738 return NULL;
739 }
740 return state.set;
741}
742
743
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000744/*
745 * This macro generates constructor function definitions for specific
746 * hash algorithms. These constructors are much faster than calling
747 * the generic one passing it a python string and are noticably
748 * faster than calling a python new() wrapper. Thats important for
749 * code that wants to make hashes of a bunch of small strings.
750 */
751#define GEN_CONSTRUCTOR(NAME) \
752 static PyObject * \
753 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
754 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000755 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000756 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000757 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000758 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000759 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000760 return NULL; \
761 } \
762 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000763 if (data_obj) \
Gregory P. Smith365a1862009-02-12 07:35:29 +0000764 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000765 \
766 ret_obj = EVPnew( \
767 CONST_ ## NAME ## _name_obj, \
768 NULL, \
769 CONST_new_ ## NAME ## _ctx_p, \
770 (unsigned char*)view.buf, \
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000771 view.len); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000772 \
773 if (data_obj) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000774 PyBuffer_Release(&view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000775 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000776 }
777
778/* a PyMethodDef structure for the constructor */
779#define CONSTRUCTOR_METH_DEF(NAME) \
780 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
781 PyDoc_STR("Returns a " #NAME \
782 " hash object; optionally initialized with a string") \
783 }
784
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800785/* used in the init function to setup a constructor: initialize OpenSSL
786 constructor constants if they haven't been initialized already. */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000787#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800788 if (CONST_ ## NAME ## _name_obj == NULL) { \
789 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
790 if (EVP_get_digestbyname(#NAME)) { \
791 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
792 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
793 } \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000794 } \
795} while (0);
796
797GEN_CONSTRUCTOR(md5)
798GEN_CONSTRUCTOR(sha1)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000799#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000800GEN_CONSTRUCTOR(sha224)
801GEN_CONSTRUCTOR(sha256)
802GEN_CONSTRUCTOR(sha384)
803GEN_CONSTRUCTOR(sha512)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000804#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000805
806/* List of functions exported by this module */
807
808static struct PyMethodDef EVP_functions[] = {
809 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
Christian Heimese92ef132013-10-13 00:52:43 +0200810#ifdef PY_PBKDF2_HMAC
811 {"pbkdf2_hmac", (PyCFunction)pbkdf2_hmac, METH_VARARGS|METH_KEYWORDS,
812 pbkdf2_hmac__doc__},
813#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000814 CONSTRUCTOR_METH_DEF(md5),
815 CONSTRUCTOR_METH_DEF(sha1),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000816#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000817 CONSTRUCTOR_METH_DEF(sha224),
818 CONSTRUCTOR_METH_DEF(sha256),
819 CONSTRUCTOR_METH_DEF(sha384),
820 CONSTRUCTOR_METH_DEF(sha512),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000821#endif
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000822 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000823};
824
825
826/* Initialize this module. */
827
Martin v. Löwis1a214512008-06-11 05:26:20 +0000828
829static struct PyModuleDef _hashlibmodule = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000830 PyModuleDef_HEAD_INIT,
831 "_hashlib",
832 NULL,
833 -1,
834 EVP_functions,
835 NULL,
836 NULL,
837 NULL,
838 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000839};
840
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000841PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000842PyInit__hashlib(void)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000843{
Gregory P. Smith13b55292010-09-06 08:30:23 +0000844 PyObject *m, *openssl_md_meth_names;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000845
846 OpenSSL_add_all_digests();
Christian Heimesb7ddbc82013-10-21 19:48:22 +0200847 ERR_load_crypto_strings();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000848
849 /* TODO build EVP_functions openssl_* entries dynamically based
850 * on what hashes are supported rather than listing many
851 * but having some be unsupported. Only init appropriate
852 * constants. */
853
Christian Heimes90aa7642007-12-19 02:45:37 +0000854 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000855 if (PyType_Ready(&EVPtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000856 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000857
Martin v. Löwis1a214512008-06-11 05:26:20 +0000858 m = PyModule_Create(&_hashlibmodule);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000859 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000860 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000861
Gregory P. Smith13b55292010-09-06 08:30:23 +0000862 openssl_md_meth_names = generate_hash_name_list();
863 if (openssl_md_meth_names == NULL) {
864 Py_DECREF(m);
865 return NULL;
866 }
867 if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) {
868 Py_DECREF(m);
869 return NULL;
870 }
871
Christian Heimes327dd732013-10-22 15:05:23 +0200872 Py_INCREF((PyObject *)&EVPtype);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000873 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000874
875 /* these constants are used by the convenience constructors */
876 INIT_CONSTRUCTOR_CONSTANTS(md5);
877 INIT_CONSTRUCTOR_CONSTANTS(sha1);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000878#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000879 INIT_CONSTRUCTOR_CONSTANTS(sha224);
880 INIT_CONSTRUCTOR_CONSTANTS(sha256);
881 INIT_CONSTRUCTOR_CONSTANTS(sha384);
882 INIT_CONSTRUCTOR_CONSTANTS(sha512);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000883#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +0000884 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000885}