blob: a4f610450e432c269778edc353253c5ff53b4b08 [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
Christian Heimese92ef132013-10-13 00:52:43 +020066static PyObject *
67_setException(PyObject *exc)
68{
69 unsigned long errcode;
70 const char *lib, *func, *reason;
71
72 errcode = ERR_peek_last_error();
73 if (!errcode) {
74 PyErr_SetString(exc, "unknown reasons");
75 return NULL;
76 }
77 ERR_clear_error();
78
79 lib = ERR_lib_error_string(errcode);
80 func = ERR_func_error_string(errcode);
81 reason = ERR_reason_error_string(errcode);
82
83 if (lib && func) {
84 PyErr_Format(exc, "[%s: %s] %s", lib, func, reason);
85 }
86 else if (lib) {
87 PyErr_Format(exc, "[%s] %s", lib, reason);
88 }
89 else {
90 PyErr_SetString(exc, reason);
91 }
92 return NULL;
93}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000094
95static EVPobject *
96newEVPobject(PyObject *name)
97{
98 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
99
100 /* save the name for .name to return */
101 if (retval != NULL) {
102 Py_INCREF(name);
103 retval->name = name;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000104#ifdef WITH_THREAD
105 retval->lock = NULL;
106#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000107 }
108
109 return retval;
110}
111
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000112static void
113EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
114{
115 unsigned int process;
116 const unsigned char *cp = (const unsigned char *)vp;
117 while (0 < len) {
118 if (len > (Py_ssize_t)MUNCH_SIZE)
119 process = MUNCH_SIZE;
120 else
121 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
122 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
123 len -= process;
124 cp += process;
125 }
126}
127
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000128/* Internal methods for a hash object */
129
130static void
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000131EVP_dealloc(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000132{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000133#ifdef WITH_THREAD
134 if (self->lock != NULL)
135 PyThread_free_lock(self->lock);
136#endif
137 EVP_MD_CTX_cleanup(&self->ctx);
138 Py_XDECREF(self->name);
139 PyObject_Del(self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000140}
141
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000142static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
143{
144 ENTER_HASHLIB(self);
145 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
146 LEAVE_HASHLIB(self);
147}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000148
149/* External methods for a hash object */
150
151PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
152
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000153
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000154static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000155EVP_copy(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000156{
157 EVPobject *newobj;
158
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000159 if ( (newobj = newEVPobject(self->name))==NULL)
160 return NULL;
161
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000162 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000163 return (PyObject *)newobj;
164}
165
166PyDoc_STRVAR(EVP_digest__doc__,
167"Return the digest value as a string of binary data.");
168
169static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000170EVP_digest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000171{
172 unsigned char digest[EVP_MAX_MD_SIZE];
173 EVP_MD_CTX temp_ctx;
174 PyObject *retval;
175 unsigned int digest_size;
176
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000177 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000178 digest_size = EVP_MD_CTX_size(&temp_ctx);
Neal Norwitzf0459142006-01-07 21:20:24 +0000179 EVP_DigestFinal(&temp_ctx, digest, NULL);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000180
Christian Heimes72b710a2008-05-26 13:28:38 +0000181 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000182 EVP_MD_CTX_cleanup(&temp_ctx);
183 return retval;
184}
185
186PyDoc_STRVAR(EVP_hexdigest__doc__,
187"Return the digest value as a string of hexadecimal digits.");
188
189static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000190EVP_hexdigest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000191{
192 unsigned char digest[EVP_MAX_MD_SIZE];
193 EVP_MD_CTX temp_ctx;
194 PyObject *retval;
195 char *hex_digest;
196 unsigned int i, j, digest_size;
197
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000198 /* Get the raw (binary) digest value */
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000199 locked_EVP_MD_CTX_copy(&temp_ctx, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000200 digest_size = EVP_MD_CTX_size(&temp_ctx);
201 EVP_DigestFinal(&temp_ctx, digest, NULL);
202
203 EVP_MD_CTX_cleanup(&temp_ctx);
204
Guido van Rossumf8953072007-07-10 13:20:29 +0000205 /* Allocate a new buffer */
206 hex_digest = PyMem_Malloc(digest_size * 2 + 1);
207 if (!hex_digest)
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000208 return PyErr_NoMemory();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000209
210 /* Make hex version of the digest */
211 for(i=j=0; i<digest_size; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200212 unsigned char c;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000213 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200214 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000215 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200216 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000217 }
Guido van Rossumf8953072007-07-10 13:20:29 +0000218 retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2);
219 PyMem_Free(hex_digest);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000220 return retval;
221}
222
223PyDoc_STRVAR(EVP_update__doc__,
224"Update this hash object's state with the provided string.");
225
226static PyObject *
227EVP_update(EVPobject *self, PyObject *args)
228{
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000229 PyObject *obj;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000230 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000231
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000232 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000233 return NULL;
234
Gregory P. Smith365a1862009-02-12 07:35:29 +0000235 GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000236
237#ifdef WITH_THREAD
238 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
239 self->lock = PyThread_allocate_lock();
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000240 /* fail? lock = NULL and we fail over to non-threaded code. */
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000241 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000242
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000243 if (self->lock != NULL) {
244 Py_BEGIN_ALLOW_THREADS
245 PyThread_acquire_lock(self->lock, 1);
246 EVP_hash(self, view.buf, view.len);
247 PyThread_release_lock(self->lock);
248 Py_END_ALLOW_THREADS
249 } else {
250 EVP_hash(self, view.buf, view.len);
251 }
252#else
253 EVP_hash(self, view.buf, view.len);
254#endif
255
256 PyBuffer_Release(&view);
257 Py_RETURN_NONE;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000258}
259
260static PyMethodDef EVP_methods[] = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000261 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
262 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000263 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000264 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
265 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000266};
267
268static PyObject *
269EVP_get_block_size(EVPobject *self, void *closure)
270{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000271 long block_size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000272 block_size = EVP_MD_CTX_block_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000273 return PyLong_FromLong(block_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000274}
275
276static PyObject *
277EVP_get_digest_size(EVPobject *self, void *closure)
278{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000279 long size;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000280 size = EVP_MD_CTX_size(&self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000281 return PyLong_FromLong(size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000282}
283
284static PyMemberDef EVP_members[] = {
285 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
286 {NULL} /* Sentinel */
287};
288
289static PyGetSetDef EVP_getseters[] = {
290 {"digest_size",
291 (getter)EVP_get_digest_size, NULL,
292 NULL,
293 NULL},
294 {"block_size",
295 (getter)EVP_get_block_size, NULL,
296 NULL,
297 NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000298 {NULL} /* Sentinel */
299};
300
301
302static PyObject *
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000303EVP_repr(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000304{
Victor Stinner3f1af5c2010-03-12 17:00:41 +0000305 return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000306}
307
308#if HASH_OBJ_CONSTRUCTOR
309static int
310EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
311{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000312 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000313 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000314 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000315 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000316 char *nameStr;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000317 const EVP_MD *digest;
318
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000319 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
320 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000321 return -1;
322 }
323
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000324 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000325 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000326
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000327 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
328 PyErr_SetString(PyExc_TypeError, "name must be a string");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000329 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000330 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000331 return -1;
332 }
333
334 digest = EVP_get_digestbyname(nameStr);
335 if (!digest) {
336 PyErr_SetString(PyExc_ValueError, "unknown hash function");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000337 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000338 PyBuffer_Release(&view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000339 return -1;
340 }
341 EVP_DigestInit(&self->ctx, digest);
342
343 self->name = name_obj;
344 Py_INCREF(self->name);
345
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000346 if (data_obj) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000347 if (view.len >= HASHLIB_GIL_MINSIZE) {
348 Py_BEGIN_ALLOW_THREADS
349 EVP_hash(self, view.buf, view.len);
350 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000351 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000352 EVP_hash(self, view.buf, view.len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000353 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000354 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000355 }
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000356
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000357 return 0;
358}
359#endif
360
361
362PyDoc_STRVAR(hashtype_doc,
363"A hash represents the object used to calculate a checksum of a\n\
364string of information.\n\
365\n\
366Methods:\n\
367\n\
368update() -- updates the current digest with an additional string\n\
369digest() -- return the current digest value\n\
370hexdigest() -- return the current digest as a string of hexadecimal digits\n\
371copy() -- return a copy of the current hash object\n\
372\n\
373Attributes:\n\
374\n\
375name -- the hash algorithm being used by this object\n\
376digest_size -- number of bytes in this hashes output\n");
377
378static PyTypeObject EVPtype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000379 PyVarObject_HEAD_INIT(NULL, 0)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000380 "_hashlib.HASH", /*tp_name*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000381 sizeof(EVPobject), /*tp_basicsize*/
382 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000383 /* methods */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000384 (destructor)EVP_dealloc, /*tp_dealloc*/
385 0, /*tp_print*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000386 0, /*tp_getattr*/
387 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000388 0, /*tp_reserved*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000389 (reprfunc)EVP_repr, /*tp_repr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000390 0, /*tp_as_number*/
391 0, /*tp_as_sequence*/
392 0, /*tp_as_mapping*/
393 0, /*tp_hash*/
394 0, /*tp_call*/
395 0, /*tp_str*/
396 0, /*tp_getattro*/
397 0, /*tp_setattro*/
398 0, /*tp_as_buffer*/
399 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
400 hashtype_doc, /*tp_doc*/
401 0, /*tp_traverse*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000402 0, /*tp_clear*/
403 0, /*tp_richcompare*/
404 0, /*tp_weaklistoffset*/
405 0, /*tp_iter*/
406 0, /*tp_iternext*/
407 EVP_methods, /* tp_methods */
408 EVP_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000409 EVP_getseters, /* tp_getset */
410#if 1
411 0, /* tp_base */
412 0, /* tp_dict */
413 0, /* tp_descr_get */
414 0, /* tp_descr_set */
415 0, /* tp_dictoffset */
416#endif
417#if HASH_OBJ_CONSTRUCTOR
418 (initproc)EVP_tp_init, /* tp_init */
419#endif
420};
421
422static PyObject *
423EVPnew(PyObject *name_obj,
424 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000425 const unsigned char *cp, Py_ssize_t len)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000426{
427 EVPobject *self;
428
429 if (!digest && !initial_ctx) {
430 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
431 return NULL;
432 }
433
434 if ((self = newEVPobject(name_obj)) == NULL)
435 return NULL;
436
437 if (initial_ctx) {
438 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
439 } else {
440 EVP_DigestInit(&self->ctx, digest);
441 }
442
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000443 if (cp && len) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000444 if (len >= HASHLIB_GIL_MINSIZE) {
445 Py_BEGIN_ALLOW_THREADS
446 EVP_hash(self, cp, len);
447 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000448 } else {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000449 EVP_hash(self, cp, len);
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000450 }
451 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000452
453 return (PyObject *)self;
454}
455
456
457/* The module-level function: new() */
458
459PyDoc_STRVAR(EVP_new__doc__,
460"Return a new hash object using the named algorithm.\n\
461An optional string argument may be provided and will be\n\
462automatically hashed.\n\
463\n\
464The MD5 and SHA1 algorithms are always supported.\n");
465
466static PyObject *
467EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
468{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000469 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000470 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000471 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000472 Py_buffer view = { 0 };
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000473 PyObject *ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000474 char *name;
475 const EVP_MD *digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000476
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000477 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
478 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000479 return NULL;
480 }
481
482 if (!PyArg_Parse(name_obj, "s", &name)) {
483 PyErr_SetString(PyExc_TypeError, "name must be a string");
484 return NULL;
485 }
486
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000487 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000488 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000489
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000490 digest = EVP_get_digestbyname(name);
491
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000492 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000493
494 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000495 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000496 return ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000497}
498
Christian Heimese7236222013-10-19 14:24:44 +0200499
500
Christian Heimes351f5392013-10-19 17:59:48 +0200501#if (OPENSSL_VERSION_NUMBER >= 0x10000000 && !defined(OPENSSL_NO_HMAC) \
502 && !defined(OPENSSL_NO_SHA))
Christian Heimese7236222013-10-19 14:24:44 +0200503
Christian Heimese92ef132013-10-13 00:52:43 +0200504#define PY_PBKDF2_HMAC 1
505
Christian Heimese7236222013-10-19 14:24:44 +0200506/* Improved implementation of PKCS5_PBKDF2_HMAC()
507 *
508 * PKCS5_PBKDF2_HMAC_fast() hashes the password exactly one time instead of
509 * `iter` times. Today (2013) the iteration count is typically 100,000 or
510 * more. The improved algorithm is not subject to a Denial-of-Service
511 * vulnerability with overly large passwords.
512 *
513 * Also OpenSSL < 1.0 don't provide PKCS5_PBKDF2_HMAC(), only
514 * PKCS5_PBKDF2_SHA1.
515 */
Christian Heimesc6564b92013-10-20 13:23:03 +0200516static int
517PKCS5_PBKDF2_HMAC_fast(const char *pass, int passlen,
518 const unsigned char *salt, int saltlen,
519 int iter, const EVP_MD *digest,
520 int keylen, unsigned char *out)
Christian Heimese7236222013-10-19 14:24:44 +0200521{
522 unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
523 int cplen, j, k, tkeylen, mdlen;
524 unsigned long i = 1;
525 HMAC_CTX hctx_tpl, hctx;
526
527 mdlen = EVP_MD_size(digest);
528 if (mdlen < 0)
529 return 0;
530
531 HMAC_CTX_init(&hctx_tpl);
532 HMAC_CTX_init(&hctx);
533 p = out;
534 tkeylen = keylen;
535 if (!pass)
536 passlen = 0;
537 else if(passlen == -1)
538 passlen = strlen(pass);
539 if (!HMAC_Init_ex(&hctx_tpl, pass, passlen, digest, NULL)) {
540 HMAC_CTX_cleanup(&hctx_tpl);
541 return 0;
542 }
543 while(tkeylen) {
544 if(tkeylen > mdlen)
545 cplen = mdlen;
546 else
547 cplen = tkeylen;
548 /* We are unlikely to ever use more than 256 blocks (5120 bits!)
549 * but just in case...
550 */
551 itmp[0] = (unsigned char)((i >> 24) & 0xff);
552 itmp[1] = (unsigned char)((i >> 16) & 0xff);
553 itmp[2] = (unsigned char)((i >> 8) & 0xff);
554 itmp[3] = (unsigned char)(i & 0xff);
555 if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) {
556 HMAC_CTX_cleanup(&hctx_tpl);
557 return 0;
558 }
559 if (!HMAC_Update(&hctx, salt, saltlen)
560 || !HMAC_Update(&hctx, itmp, 4)
561 || !HMAC_Final(&hctx, digtmp, NULL)) {
562 HMAC_CTX_cleanup(&hctx_tpl);
563 HMAC_CTX_cleanup(&hctx);
564 return 0;
565 }
566 memcpy(p, digtmp, cplen);
567 for (j = 1; j < iter; j++) {
568 if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) {
569 HMAC_CTX_cleanup(&hctx_tpl);
570 return 0;
571 }
572 if (!HMAC_Update(&hctx, digtmp, mdlen)
573 || !HMAC_Final(&hctx, digtmp, NULL)) {
574 HMAC_CTX_cleanup(&hctx_tpl);
575 HMAC_CTX_cleanup(&hctx);
576 return 0;
577 }
578 HMAC_CTX_cleanup(&hctx);
579 for (k = 0; k < cplen; k++) {
580 p[k] ^= digtmp[k];
581 }
582 }
583 tkeylen-= cplen;
584 i++;
585 p+= cplen;
586 }
587 HMAC_CTX_cleanup(&hctx_tpl);
588 return 1;
589}
590
591
Christian Heimese92ef132013-10-13 00:52:43 +0200592PyDoc_STRVAR(pbkdf2_hmac__doc__,
593"pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None) -> key\n\
594\n\
595Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as\n\
596pseudorandom function.");
597
598static PyObject *
599pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict)
600{
601 static char *kwlist[] = {"hash_name", "password", "salt", "iterations",
602 "dklen", NULL};
603 PyObject *key_obj = NULL, *dklen_obj = Py_None;
604 char *name, *key;
605 Py_buffer password, salt;
606 long iterations, dklen;
607 int retval;
608 const EVP_MD *digest;
609
610 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "sy*y*l|O:pbkdf2_hmac",
611 kwlist, &name, &password, &salt,
612 &iterations, &dklen_obj)) {
613 return NULL;
614 }
615
616 digest = EVP_get_digestbyname(name);
617 if (digest == NULL) {
618 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
619 goto end;
620 }
621
622 if (password.len > INT_MAX) {
623 PyErr_SetString(PyExc_OverflowError,
624 "password is too long.");
625 goto end;
626 }
627
628 if (salt.len > INT_MAX) {
629 PyErr_SetString(PyExc_OverflowError,
630 "salt is too long.");
631 goto end;
632 }
633
634 if (iterations < 1) {
635 PyErr_SetString(PyExc_ValueError,
636 "iteration value must be greater than 0.");
637 goto end;
638 }
639 if (iterations > INT_MAX) {
640 PyErr_SetString(PyExc_OverflowError,
641 "iteration value is too great.");
642 goto end;
643 }
644
645 if (dklen_obj == Py_None) {
646 dklen = EVP_MD_size(digest);
647 } else {
648 dklen = PyLong_AsLong(dklen_obj);
649 if ((dklen == -1) && PyErr_Occurred()) {
650 goto end;
651 }
652 }
653 if (dklen < 1) {
654 PyErr_SetString(PyExc_ValueError,
655 "key length must be greater than 0.");
656 goto end;
657 }
658 if (dklen > INT_MAX) {
659 /* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */
660 PyErr_SetString(PyExc_OverflowError,
661 "key length is too great.");
662 goto end;
663 }
664
665 key_obj = PyBytes_FromStringAndSize(NULL, dklen);
666 if (key_obj == NULL) {
667 goto end;
668 }
669 key = PyBytes_AS_STRING(key_obj);
670
671 Py_BEGIN_ALLOW_THREADS
Christian Heimese7236222013-10-19 14:24:44 +0200672 retval = PKCS5_PBKDF2_HMAC_fast((char*)password.buf, password.len,
673 (unsigned char *)salt.buf, salt.len,
674 iterations, digest, dklen,
675 (unsigned char *)key);
Christian Heimese92ef132013-10-13 00:52:43 +0200676 Py_END_ALLOW_THREADS
677
678 if (!retval) {
679 Py_CLEAR(key_obj);
680 _setException(PyExc_ValueError);
681 goto end;
682 }
683
684 end:
685 PyBuffer_Release(&password);
686 PyBuffer_Release(&salt);
687 return key_obj;
688}
689
690#endif
Gregory P. Smith13b55292010-09-06 08:30:23 +0000691
692/* State for our callback function so that it can accumulate a result. */
693typedef struct _internal_name_mapper_state {
694 PyObject *set;
695 int error;
696} _InternalNameMapperState;
697
698
699/* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
700static void
701_openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg)
702{
703 _InternalNameMapperState *state = (_InternalNameMapperState *)arg;
704 PyObject *py_name;
705
706 assert(state != NULL);
707 if (openssl_obj_name == NULL)
708 return;
709 /* Ignore aliased names, they pollute the list and OpenSSL appears to
710 * have a its own definition of alias as the resulting list still
711 * contains duplicate and alternate names for several algorithms. */
712 if (openssl_obj_name->alias)
713 return;
714
715 py_name = PyUnicode_FromString(openssl_obj_name->name);
716 if (py_name == NULL) {
717 state->error = 1;
718 } else {
719 if (PySet_Add(state->set, py_name) != 0) {
Gregory P. Smith13b55292010-09-06 08:30:23 +0000720 state->error = 1;
721 }
Christian Heimesdb816d62013-10-29 12:14:55 +0100722 Py_DECREF(py_name);
Gregory P. Smith13b55292010-09-06 08:30:23 +0000723 }
724}
725
726
727/* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */
728static PyObject*
729generate_hash_name_list(void)
730{
731 _InternalNameMapperState state;
732 state.set = PyFrozenSet_New(NULL);
733 if (state.set == NULL)
734 return NULL;
735 state.error = 0;
736
737 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state);
738
739 if (state.error) {
740 Py_DECREF(state.set);
741 return NULL;
742 }
743 return state.set;
744}
745
746
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000747/*
748 * This macro generates constructor function definitions for specific
749 * hash algorithms. These constructors are much faster than calling
750 * the generic one passing it a python string and are noticably
751 * faster than calling a python new() wrapper. Thats important for
752 * code that wants to make hashes of a bunch of small strings.
753 */
754#define GEN_CONSTRUCTOR(NAME) \
755 static PyObject * \
756 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
757 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000758 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000759 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000760 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000761 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000762 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000763 return NULL; \
764 } \
765 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000766 if (data_obj) \
Gregory P. Smith365a1862009-02-12 07:35:29 +0000767 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000768 \
769 ret_obj = EVPnew( \
770 CONST_ ## NAME ## _name_obj, \
771 NULL, \
772 CONST_new_ ## NAME ## _ctx_p, \
773 (unsigned char*)view.buf, \
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000774 view.len); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000775 \
776 if (data_obj) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000777 PyBuffer_Release(&view); \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000778 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000779 }
780
781/* a PyMethodDef structure for the constructor */
782#define CONSTRUCTOR_METH_DEF(NAME) \
783 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
784 PyDoc_STR("Returns a " #NAME \
785 " hash object; optionally initialized with a string") \
786 }
787
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800788/* used in the init function to setup a constructor: initialize OpenSSL
789 constructor constants if they haven't been initialized already. */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000790#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Gregory P. Smithaded2e52013-02-01 17:05:29 -0800791 if (CONST_ ## NAME ## _name_obj == NULL) { \
792 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
793 if (EVP_get_digestbyname(#NAME)) { \
794 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
795 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
796 } \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000797 } \
798} while (0);
799
800GEN_CONSTRUCTOR(md5)
801GEN_CONSTRUCTOR(sha1)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000802#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000803GEN_CONSTRUCTOR(sha224)
804GEN_CONSTRUCTOR(sha256)
805GEN_CONSTRUCTOR(sha384)
806GEN_CONSTRUCTOR(sha512)
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000807#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000808
809/* List of functions exported by this module */
810
811static struct PyMethodDef EVP_functions[] = {
812 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
Christian Heimese92ef132013-10-13 00:52:43 +0200813#ifdef PY_PBKDF2_HMAC
814 {"pbkdf2_hmac", (PyCFunction)pbkdf2_hmac, METH_VARARGS|METH_KEYWORDS,
815 pbkdf2_hmac__doc__},
816#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000817 CONSTRUCTOR_METH_DEF(md5),
818 CONSTRUCTOR_METH_DEF(sha1),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000819#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000820 CONSTRUCTOR_METH_DEF(sha224),
821 CONSTRUCTOR_METH_DEF(sha256),
822 CONSTRUCTOR_METH_DEF(sha384),
823 CONSTRUCTOR_METH_DEF(sha512),
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000824#endif
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000825 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000826};
827
828
829/* Initialize this module. */
830
Martin v. Löwis1a214512008-06-11 05:26:20 +0000831
832static struct PyModuleDef _hashlibmodule = {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000833 PyModuleDef_HEAD_INIT,
834 "_hashlib",
835 NULL,
836 -1,
837 EVP_functions,
838 NULL,
839 NULL,
840 NULL,
841 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000842};
843
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000844PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000845PyInit__hashlib(void)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000846{
Gregory P. Smith13b55292010-09-06 08:30:23 +0000847 PyObject *m, *openssl_md_meth_names;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000848
849 OpenSSL_add_all_digests();
Christian Heimesb7ddbc82013-10-21 19:48:22 +0200850 ERR_load_crypto_strings();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000851
852 /* TODO build EVP_functions openssl_* entries dynamically based
853 * on what hashes are supported rather than listing many
854 * but having some be unsupported. Only init appropriate
855 * constants. */
856
Christian Heimes90aa7642007-12-19 02:45:37 +0000857 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000858 if (PyType_Ready(&EVPtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000859 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000860
Martin v. Löwis1a214512008-06-11 05:26:20 +0000861 m = PyModule_Create(&_hashlibmodule);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000862 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000863 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000864
Gregory P. Smith13b55292010-09-06 08:30:23 +0000865 openssl_md_meth_names = generate_hash_name_list();
866 if (openssl_md_meth_names == NULL) {
867 Py_DECREF(m);
868 return NULL;
869 }
870 if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) {
871 Py_DECREF(m);
872 return NULL;
873 }
874
Christian Heimes327dd732013-10-22 15:05:23 +0200875 Py_INCREF((PyObject *)&EVPtype);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000876 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000877
878 /* these constants are used by the convenience constructors */
879 INIT_CONSTRUCTOR_CONSTANTS(md5);
880 INIT_CONSTRUCTOR_CONSTANTS(sha1);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000881#ifdef _OPENSSL_SUPPORTS_SHA2
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000882 INIT_CONSTRUCTOR_CONSTANTS(sha224);
883 INIT_CONSTRUCTOR_CONSTANTS(sha256);
884 INIT_CONSTRUCTOR_CONSTANTS(sha384);
885 INIT_CONSTRUCTOR_CONSTANTS(sha512);
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000886#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +0000887 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000888}