source: python/trunk/Modules/_hashopenssl.c

Last change on this file was 391, checked in by dmik, 11 years ago

python: Merge vendor 2.7.6 to trunk.

  • Property svn:eol-style set to native
File size: 16.1 KB
Line 
1/* Module that wraps all OpenSSL hash algorithms */
2
3/*
4 * Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
5 * 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
14#define PY_SSIZE_T_CLEAN
15
16#include "Python.h"
17#include "structmember.h"
18
19#ifdef WITH_THREAD
20#include "pythread.h"
21 #define ENTER_HASHLIB(obj) \
22 if ((obj)->lock) { \
23 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
24 Py_BEGIN_ALLOW_THREADS \
25 PyThread_acquire_lock((obj)->lock, 1); \
26 Py_END_ALLOW_THREADS \
27 } \
28 }
29 #define LEAVE_HASHLIB(obj) \
30 if ((obj)->lock) { \
31 PyThread_release_lock((obj)->lock); \
32 }
33#else
34 #define ENTER_HASHLIB(obj)
35 #define LEAVE_HASHLIB(obj)
36#endif
37
38/* EVP is the preferred interface to hashing in OpenSSL */
39#include <openssl/evp.h>
40
41#define MUNCH_SIZE INT_MAX
42
43/* TODO(gps): We should probably make this a module or EVPobject attribute
44 * to allow the user to optimize based on the platform they're using. */
45#define HASHLIB_GIL_MINSIZE 2048
46
47#ifndef HASH_OBJ_CONSTRUCTOR
48#define HASH_OBJ_CONSTRUCTOR 0
49#endif
50
51/* Minimum OpenSSL version needed to support sha224 and higher. */
52#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x00908000)
53#define _OPENSSL_SUPPORTS_SHA2
54#endif
55
56typedef struct {
57 PyObject_HEAD
58 PyObject *name; /* name of this hash algorithm */
59 EVP_MD_CTX ctx; /* OpenSSL message digest context */
60#ifdef WITH_THREAD
61 PyThread_type_lock lock; /* OpenSSL context lock */
62#endif
63} EVPobject;
64
65
66static PyTypeObject EVPtype;
67
68
69#define DEFINE_CONSTS_FOR_NEW(Name) \
70 static PyObject *CONST_ ## Name ## _name_obj = NULL; \
71 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
72 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
73
74DEFINE_CONSTS_FOR_NEW(md5)
75DEFINE_CONSTS_FOR_NEW(sha1)
76#ifdef _OPENSSL_SUPPORTS_SHA2
77DEFINE_CONSTS_FOR_NEW(sha224)
78DEFINE_CONSTS_FOR_NEW(sha256)
79DEFINE_CONSTS_FOR_NEW(sha384)
80DEFINE_CONSTS_FOR_NEW(sha512)
81#endif
82
83
84static EVPobject *
85newEVPobject(PyObject *name)
86{
87 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
88
89 /* save the name for .name to return */
90 if (retval != NULL) {
91 Py_INCREF(name);
92 retval->name = name;
93#ifdef WITH_THREAD
94 retval->lock = NULL;
95#endif
96 }
97
98 return retval;
99}
100
101static void
102EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
103{
104 unsigned int process;
105 const unsigned char *cp = (const unsigned char *)vp;
106 while (0 < len)
107 {
108 if (len > (Py_ssize_t)MUNCH_SIZE)
109 process = MUNCH_SIZE;
110 else
111 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
112 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
113 len -= process;
114 cp += process;
115 }
116}
117
118/* Internal methods for a hash object */
119
120static void
121EVP_dealloc(EVPobject *self)
122{
123#ifdef WITH_THREAD
124 if (self->lock != NULL)
125 PyThread_free_lock(self->lock);
126#endif
127 EVP_MD_CTX_cleanup(&self->ctx);
128 Py_XDECREF(self->name);
129 PyObject_Del(self);
130}
131
132static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
133{
134 ENTER_HASHLIB(self);
135 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
136 LEAVE_HASHLIB(self);
137}
138
139/* External methods for a hash object */
140
141PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
142
143
144static PyObject *
145EVP_copy(EVPobject *self, PyObject *unused)
146{
147 EVPobject *newobj;
148
149 if ( (newobj = newEVPobject(self->name))==NULL)
150 return NULL;
151
152 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
153 return (PyObject *)newobj;
154}
155
156PyDoc_STRVAR(EVP_digest__doc__,
157"Return the digest value as a string of binary data.");
158
159static PyObject *
160EVP_digest(EVPobject *self, PyObject *unused)
161{
162 unsigned char digest[EVP_MAX_MD_SIZE];
163 EVP_MD_CTX temp_ctx;
164 PyObject *retval;
165 unsigned int digest_size;
166
167 locked_EVP_MD_CTX_copy(&temp_ctx, self);
168 digest_size = EVP_MD_CTX_size(&temp_ctx);
169 EVP_DigestFinal(&temp_ctx, digest, NULL);
170
171 retval = PyString_FromStringAndSize((const char *)digest, digest_size);
172 EVP_MD_CTX_cleanup(&temp_ctx);
173 return retval;
174}
175
176PyDoc_STRVAR(EVP_hexdigest__doc__,
177"Return the digest value as a string of hexadecimal digits.");
178
179static PyObject *
180EVP_hexdigest(EVPobject *self, PyObject *unused)
181{
182 unsigned char digest[EVP_MAX_MD_SIZE];
183 EVP_MD_CTX temp_ctx;
184 PyObject *retval;
185 char *hex_digest;
186 unsigned int i, j, digest_size;
187
188 /* Get the raw (binary) digest value */
189 locked_EVP_MD_CTX_copy(&temp_ctx, self);
190 digest_size = EVP_MD_CTX_size(&temp_ctx);
191 EVP_DigestFinal(&temp_ctx, digest, NULL);
192
193 EVP_MD_CTX_cleanup(&temp_ctx);
194
195 /* Create a new string */
196 /* NOTE: not thread safe! modifying an already created string object */
197 /* (not a problem because we hold the GIL by default) */
198 retval = PyString_FromStringAndSize(NULL, digest_size * 2);
199 if (!retval)
200 return NULL;
201 hex_digest = PyString_AsString(retval);
202 if (!hex_digest) {
203 Py_DECREF(retval);
204 return NULL;
205 }
206
207 /* Make hex version of the digest */
208 for(i=j=0; i<digest_size; i++) {
209 char c;
210 c = (digest[i] >> 4) & 0xf;
211 c = (c>9) ? c+'a'-10 : c + '0';
212 hex_digest[j++] = c;
213 c = (digest[i] & 0xf);
214 c = (c>9) ? c+'a'-10 : c + '0';
215 hex_digest[j++] = c;
216 }
217 return retval;
218}
219
220PyDoc_STRVAR(EVP_update__doc__,
221"Update this hash object's state with the provided string.");
222
223static PyObject *
224EVP_update(EVPobject *self, PyObject *args)
225{
226 Py_buffer view;
227
228 if (!PyArg_ParseTuple(args, "s*:update", &view))
229 return NULL;
230
231#ifdef WITH_THREAD
232 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
233 self->lock = PyThread_allocate_lock();
234 /* fail? lock = NULL and we fail over to non-threaded code. */
235 }
236
237 if (self->lock != NULL) {
238 Py_BEGIN_ALLOW_THREADS
239 PyThread_acquire_lock(self->lock, 1);
240 EVP_hash(self, view.buf, view.len);
241 PyThread_release_lock(self->lock);
242 Py_END_ALLOW_THREADS
243 }
244 else
245#endif
246 {
247 EVP_hash(self, view.buf, view.len);
248 }
249
250 PyBuffer_Release(&view);
251
252 Py_RETURN_NONE;
253}
254
255static PyMethodDef EVP_methods[] = {
256 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
257 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
258 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
259 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
260 {NULL, NULL} /* sentinel */
261};
262
263static PyObject *
264EVP_get_block_size(EVPobject *self, void *closure)
265{
266 long block_size;
267 block_size = EVP_MD_CTX_block_size(&self->ctx);
268 return PyLong_FromLong(block_size);
269}
270
271static PyObject *
272EVP_get_digest_size(EVPobject *self, void *closure)
273{
274 long size;
275 size = EVP_MD_CTX_size(&self->ctx);
276 return PyLong_FromLong(size);
277}
278
279static PyMemberDef EVP_members[] = {
280 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
281 {NULL} /* Sentinel */
282};
283
284static PyGetSetDef EVP_getseters[] = {
285 {"digest_size",
286 (getter)EVP_get_digest_size, NULL,
287 NULL,
288 NULL},
289 {"block_size",
290 (getter)EVP_get_block_size, NULL,
291 NULL,
292 NULL},
293 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
294 * the old sha module also supported 'digestsize'. ugh. */
295 {"digestsize",
296 (getter)EVP_get_digest_size, NULL,
297 NULL,
298 NULL},
299 {NULL} /* Sentinel */
300};
301
302
303static PyObject *
304EVP_repr(PyObject *self)
305{
306 char buf[100];
307 PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
308 PyString_AsString(((EVPobject *)self)->name), self);
309 return PyString_FromString(buf);
310}
311
312#if HASH_OBJ_CONSTRUCTOR
313static int
314EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
315{
316 static char *kwlist[] = {"name", "string", NULL};
317 PyObject *name_obj = NULL;
318 Py_buffer view = { 0 };
319 char *nameStr;
320 const EVP_MD *digest;
321
322 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s*:HASH", kwlist,
323 &name_obj, &view)) {
324 return -1;
325 }
326
327 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
328 PyErr_SetString(PyExc_TypeError, "name must be a string");
329 PyBuffer_Release(&view);
330 return -1;
331 }
332
333 digest = EVP_get_digestbyname(nameStr);
334 if (!digest) {
335 PyErr_SetString(PyExc_ValueError, "unknown hash function");
336 PyBuffer_Release(&view);
337 return -1;
338 }
339 EVP_DigestInit(&self->ctx, digest);
340
341 self->name = name_obj;
342 Py_INCREF(self->name);
343
344 if (view.obj) {
345 if (view.len >= HASHLIB_GIL_MINSIZE) {
346 Py_BEGIN_ALLOW_THREADS
347 EVP_hash(self, view.buf, view.len);
348 Py_END_ALLOW_THREADS
349 } else {
350 EVP_hash(self, view.buf, view.len);
351 }
352 PyBuffer_Release(&view);
353 }
354
355 return 0;
356}
357#endif
358
359
360PyDoc_STRVAR(hashtype_doc,
361"A hash represents the object used to calculate a checksum of a\n\
362string of information.\n\
363\n\
364Methods:\n\
365\n\
366update() -- updates the current digest with an additional string\n\
367digest() -- return the current digest value\n\
368hexdigest() -- return the current digest as a string of hexadecimal digits\n\
369copy() -- return a copy of the current hash object\n\
370\n\
371Attributes:\n\
372\n\
373name -- the hash algorithm being used by this object\n\
374digest_size -- number of bytes in this hashes output\n");
375
376static PyTypeObject EVPtype = {
377 PyVarObject_HEAD_INIT(NULL, 0)
378 "_hashlib.HASH", /*tp_name*/
379 sizeof(EVPobject), /*tp_basicsize*/
380 0, /*tp_itemsize*/
381 /* methods */
382 (destructor)EVP_dealloc, /*tp_dealloc*/
383 0, /*tp_print*/
384 0, /*tp_getattr*/
385 0, /*tp_setattr*/
386 0, /*tp_compare*/
387 EVP_repr, /*tp_repr*/
388 0, /*tp_as_number*/
389 0, /*tp_as_sequence*/
390 0, /*tp_as_mapping*/
391 0, /*tp_hash*/
392 0, /*tp_call*/
393 0, /*tp_str*/
394 0, /*tp_getattro*/
395 0, /*tp_setattro*/
396 0, /*tp_as_buffer*/
397 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
398 hashtype_doc, /*tp_doc*/
399 0, /*tp_traverse*/
400 0, /*tp_clear*/
401 0, /*tp_richcompare*/
402 0, /*tp_weaklistoffset*/
403 0, /*tp_iter*/
404 0, /*tp_iternext*/
405 EVP_methods, /* tp_methods */
406 EVP_members, /* tp_members */
407 EVP_getseters, /* tp_getset */
408#if 1
409 0, /* tp_base */
410 0, /* tp_dict */
411 0, /* tp_descr_get */
412 0, /* tp_descr_set */
413 0, /* tp_dictoffset */
414#endif
415#if HASH_OBJ_CONSTRUCTOR
416 (initproc)EVP_tp_init, /* tp_init */
417#endif
418};
419
420static PyObject *
421EVPnew(PyObject *name_obj,
422 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
423 const unsigned char *cp, Py_ssize_t len)
424{
425 EVPobject *self;
426
427 if (!digest && !initial_ctx) {
428 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
429 return NULL;
430 }
431
432 if ((self = newEVPobject(name_obj)) == NULL)
433 return NULL;
434
435 if (initial_ctx) {
436 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
437 } else {
438 EVP_DigestInit(&self->ctx, digest);
439 }
440
441 if (cp && len) {
442 if (len >= HASHLIB_GIL_MINSIZE) {
443 Py_BEGIN_ALLOW_THREADS
444 EVP_hash(self, cp, len);
445 Py_END_ALLOW_THREADS
446 } else {
447 EVP_hash(self, cp, len);
448 }
449 }
450
451 return (PyObject *)self;
452}
453
454
455/* The module-level function: new() */
456
457PyDoc_STRVAR(EVP_new__doc__,
458"Return a new hash object using the named algorithm.\n\
459An optional string argument may be provided and will be\n\
460automatically hashed.\n\
461\n\
462The MD5 and SHA1 algorithms are always supported.\n");
463
464static PyObject *
465EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
466{
467 static char *kwlist[] = {"name", "string", NULL};
468 PyObject *name_obj = NULL;
469 Py_buffer view = { 0 };
470 PyObject *ret_obj;
471 char *name;
472 const EVP_MD *digest;
473
474 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|s*:new", kwlist,
475 &name_obj, &view)) {
476 return NULL;
477 }
478
479 if (!PyArg_Parse(name_obj, "s", &name)) {
480 PyBuffer_Release(&view);
481 PyErr_SetString(PyExc_TypeError, "name must be a string");
482 return NULL;
483 }
484
485 digest = EVP_get_digestbyname(name);
486
487 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
488 view.len);
489 PyBuffer_Release(&view);
490
491 return ret_obj;
492}
493
494/*
495 * This macro generates constructor function definitions for specific
496 * hash algorithms. These constructors are much faster than calling
497 * the generic one passing it a python string and are noticably
498 * faster than calling a python new() wrapper. Thats important for
499 * code that wants to make hashes of a bunch of small strings.
500 */
501#define GEN_CONSTRUCTOR(NAME) \
502 static PyObject * \
503 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
504 { \
505 Py_buffer view = { 0 }; \
506 PyObject *ret_obj; \
507 \
508 if (!PyArg_ParseTuple(args, "|s*:" #NAME , &view)) { \
509 return NULL; \
510 } \
511 \
512 ret_obj = EVPnew( \
513 CONST_ ## NAME ## _name_obj, \
514 NULL, \
515 CONST_new_ ## NAME ## _ctx_p, \
516 (unsigned char*)view.buf, view.len); \
517 PyBuffer_Release(&view); \
518 return ret_obj; \
519 }
520
521/* a PyMethodDef structure for the constructor */
522#define CONSTRUCTOR_METH_DEF(NAME) \
523 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
524 PyDoc_STR("Returns a " #NAME \
525 " hash object; optionally initialized with a string") \
526 }
527
528/* used in the init function to setup a constructor: initialize OpenSSL
529 constructor constants if they haven't been initialized already. */
530#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
531 if (CONST_ ## NAME ## _name_obj == NULL) { \
532 CONST_ ## NAME ## _name_obj = PyString_FromString(#NAME); \
533 if (EVP_get_digestbyname(#NAME)) { \
534 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
535 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
536 } \
537 } \
538} while (0);
539
540GEN_CONSTRUCTOR(md5)
541GEN_CONSTRUCTOR(sha1)
542#ifdef _OPENSSL_SUPPORTS_SHA2
543GEN_CONSTRUCTOR(sha224)
544GEN_CONSTRUCTOR(sha256)
545GEN_CONSTRUCTOR(sha384)
546GEN_CONSTRUCTOR(sha512)
547#endif
548
549/* List of functions exported by this module */
550
551static struct PyMethodDef EVP_functions[] = {
552 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
553 CONSTRUCTOR_METH_DEF(md5),
554 CONSTRUCTOR_METH_DEF(sha1),
555#ifdef _OPENSSL_SUPPORTS_SHA2
556 CONSTRUCTOR_METH_DEF(sha224),
557 CONSTRUCTOR_METH_DEF(sha256),
558 CONSTRUCTOR_METH_DEF(sha384),
559 CONSTRUCTOR_METH_DEF(sha512),
560#endif
561 {NULL, NULL} /* Sentinel */
562};
563
564
565/* Initialize this module. */
566
567PyMODINIT_FUNC
568init_hashlib(void)
569{
570 PyObject *m;
571
572 OpenSSL_add_all_digests();
573
574 /* TODO build EVP_functions openssl_* entries dynamically based
575 * on what hashes are supported rather than listing many
576 * but having some be unsupported. Only init appropriate
577 * constants. */
578
579 Py_TYPE(&EVPtype) = &PyType_Type;
580 if (PyType_Ready(&EVPtype) < 0)
581 return;
582
583 m = Py_InitModule("_hashlib", EVP_functions);
584 if (m == NULL)
585 return;
586
587#if HASH_OBJ_CONSTRUCTOR
588 Py_INCREF(&EVPtype);
589 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
590#endif
591
592 /* these constants are used by the convenience constructors */
593 INIT_CONSTRUCTOR_CONSTANTS(md5);
594 INIT_CONSTRUCTOR_CONSTANTS(sha1);
595#ifdef _OPENSSL_SUPPORTS_SHA2
596 INIT_CONSTRUCTOR_CONSTANTS(sha224);
597 INIT_CONSTRUCTOR_CONSTANTS(sha256);
598 INIT_CONSTRUCTOR_CONSTANTS(sha384);
599 INIT_CONSTRUCTOR_CONSTANTS(sha512);
600#endif
601}
Note: See TracBrowser for help on using the repository browser.