1 /* Module that wraps all OpenSSL hash algorithms */
4 * Copyright (C) 2005-2009 Gregory P. Smith (greg@krypto.org)
5 * Licensed to PSF under a Contributor Agreement.
7 * Derived from a skeleton of shamodule.c containing work performed by:
9 * Andrew Kuchling (amk@amk.ca)
10 * Greg Stein (gstein@lyra.org)
14 #define PY_SSIZE_T_CLEAN
17 #include "structmember.h"
22 #define ENTER_HASHLIB(obj) \
24 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
25 Py_BEGIN_ALLOW_THREADS \
26 PyThread_acquire_lock((obj)->lock, 1); \
27 Py_END_ALLOW_THREADS \
30 #define LEAVE_HASHLIB(obj) \
32 PyThread_release_lock((obj)->lock); \
35 #define ENTER_HASHLIB(obj)
36 #define LEAVE_HASHLIB(obj)
39 /* EVP is the preferred interface to hashing in OpenSSL */
40 #include <openssl/evp.h>
42 #define MUNCH_SIZE INT_MAX
44 /* TODO(gps): We should probably make this a module or EVPobject attribute
45 * to allow the user to optimize based on the platform they're using. */
46 #define HASHLIB_GIL_MINSIZE 2048
48 #ifndef HASH_OBJ_CONSTRUCTOR
49 #define HASH_OBJ_CONSTRUCTOR 0
55 PyObject
*name
; /* name of this hash algorithm */
56 EVP_MD_CTX ctx
; /* OpenSSL message digest context */
58 PyThread_type_lock lock
; /* OpenSSL context lock */
63 static PyTypeObject EVPtype
;
66 #define DEFINE_CONSTS_FOR_NEW(Name) \
67 static PyObject *CONST_ ## Name ## _name_obj; \
68 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
69 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
71 DEFINE_CONSTS_FOR_NEW(md5
)
72 DEFINE_CONSTS_FOR_NEW(sha1
)
73 DEFINE_CONSTS_FOR_NEW(sha224
)
74 DEFINE_CONSTS_FOR_NEW(sha256
)
75 DEFINE_CONSTS_FOR_NEW(sha384
)
76 DEFINE_CONSTS_FOR_NEW(sha512
)
80 newEVPobject(PyObject
*name
)
82 EVPobject
*retval
= (EVPobject
*)PyObject_New(EVPobject
, &EVPtype
);
84 /* save the name for .name to return */
97 EVP_hash(EVPobject
*self
, const void *vp
, Py_ssize_t len
)
100 const unsigned char *cp
= (const unsigned char *)vp
;
103 if (len
> (Py_ssize_t
)MUNCH_SIZE
)
104 process
= MUNCH_SIZE
;
106 process
= Py_SAFE_DOWNCAST(len
, Py_ssize_t
, unsigned int);
107 EVP_DigestUpdate(&self
->ctx
, (const void*)cp
, process
);
113 /* Internal methods for a hash object */
116 EVP_dealloc(EVPobject
*self
)
119 if (self
->lock
!= NULL
)
120 PyThread_free_lock(self
->lock
);
122 EVP_MD_CTX_cleanup(&self
->ctx
);
123 Py_XDECREF(self
->name
);
127 static void locked_EVP_MD_CTX_copy(EVP_MD_CTX
*new_ctx_p
, EVPobject
*self
)
130 EVP_MD_CTX_copy(new_ctx_p
, &self
->ctx
);
134 /* External methods for a hash object */
136 PyDoc_STRVAR(EVP_copy__doc__
, "Return a copy of the hash object.");
140 EVP_copy(EVPobject
*self
, PyObject
*unused
)
144 if ( (newobj
= newEVPobject(self
->name
))==NULL
)
147 locked_EVP_MD_CTX_copy(&newobj
->ctx
, self
);
148 return (PyObject
*)newobj
;
151 PyDoc_STRVAR(EVP_digest__doc__
,
152 "Return the digest value as a string of binary data.");
155 EVP_digest(EVPobject
*self
, PyObject
*unused
)
157 unsigned char digest
[EVP_MAX_MD_SIZE
];
160 unsigned int digest_size
;
162 locked_EVP_MD_CTX_copy(&temp_ctx
, self
);
163 digest_size
= EVP_MD_CTX_size(&temp_ctx
);
164 EVP_DigestFinal(&temp_ctx
, digest
, NULL
);
166 retval
= PyString_FromStringAndSize((const char *)digest
, digest_size
);
167 EVP_MD_CTX_cleanup(&temp_ctx
);
171 PyDoc_STRVAR(EVP_hexdigest__doc__
,
172 "Return the digest value as a string of hexadecimal digits.");
175 EVP_hexdigest(EVPobject
*self
, PyObject
*unused
)
177 unsigned char digest
[EVP_MAX_MD_SIZE
];
181 unsigned int i
, j
, digest_size
;
183 /* Get the raw (binary) digest value */
184 locked_EVP_MD_CTX_copy(&temp_ctx
, self
);
185 digest_size
= EVP_MD_CTX_size(&temp_ctx
);
186 EVP_DigestFinal(&temp_ctx
, digest
, NULL
);
188 EVP_MD_CTX_cleanup(&temp_ctx
);
190 /* Create a new string */
191 /* NOTE: not thread safe! modifying an already created string object */
192 /* (not a problem because we hold the GIL by default) */
193 retval
= PyString_FromStringAndSize(NULL
, digest_size
* 2);
196 hex_digest
= PyString_AsString(retval
);
202 /* Make hex version of the digest */
203 for(i
=j
=0; i
<digest_size
; i
++) {
205 c
= (digest
[i
] >> 4) & 0xf;
206 c
= (c
>9) ? c
+'a'-10 : c
+ '0';
208 c
= (digest
[i
] & 0xf);
209 c
= (c
>9) ? c
+'a'-10 : c
+ '0';
215 PyDoc_STRVAR(EVP_update__doc__
,
216 "Update this hash object's state with the provided string.");
219 EVP_update(EVPobject
*self
, PyObject
*args
)
224 if (!PyArg_ParseTuple(args
, "O:update", &obj
))
227 GET_BUFFER_VIEW_OR_ERROUT(obj
, &view
, NULL
);
230 if (self
->lock
== NULL
&& view
.len
>= HASHLIB_GIL_MINSIZE
) {
231 self
->lock
= PyThread_allocate_lock();
232 /* fail? lock = NULL and we fail over to non-threaded code. */
235 if (self
->lock
!= NULL
) {
236 Py_BEGIN_ALLOW_THREADS
237 PyThread_acquire_lock(self
->lock
, 1);
238 EVP_hash(self
, view
.buf
, view
.len
);
239 PyThread_release_lock(self
->lock
);
242 EVP_hash(self
, view
.buf
, view
.len
);
245 EVP_hash(self
, view
.buf
, view
.len
);
248 PyBuffer_Release(&view
);
254 static PyMethodDef EVP_methods
[] = {
255 {"update", (PyCFunction
)EVP_update
, METH_VARARGS
, EVP_update__doc__
},
256 {"digest", (PyCFunction
)EVP_digest
, METH_NOARGS
, EVP_digest__doc__
},
257 {"hexdigest", (PyCFunction
)EVP_hexdigest
, METH_NOARGS
, EVP_hexdigest__doc__
},
258 {"copy", (PyCFunction
)EVP_copy
, METH_NOARGS
, EVP_copy__doc__
},
259 {NULL
, NULL
} /* sentinel */
263 EVP_get_block_size(EVPobject
*self
, void *closure
)
266 block_size
= EVP_MD_CTX_block_size(&self
->ctx
);
267 return PyLong_FromLong(block_size
);
271 EVP_get_digest_size(EVPobject
*self
, void *closure
)
274 size
= EVP_MD_CTX_size(&self
->ctx
);
275 return PyLong_FromLong(size
);
278 static PyMemberDef EVP_members
[] = {
279 {"name", T_OBJECT
, offsetof(EVPobject
, name
), READONLY
, PyDoc_STR("algorithm name.")},
280 {NULL
} /* Sentinel */
283 static PyGetSetDef EVP_getseters
[] = {
285 (getter
)EVP_get_digest_size
, NULL
,
289 (getter
)EVP_get_block_size
, NULL
,
292 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
293 * the old sha module also supported 'digestsize'. ugh. */
295 (getter
)EVP_get_digest_size
, NULL
,
298 {NULL
} /* Sentinel */
303 EVP_repr(PyObject
*self
)
306 PyOS_snprintf(buf
, sizeof(buf
), "<%s HASH object @ %p>",
307 PyString_AsString(((EVPobject
*)self
)->name
), self
);
308 return PyString_FromString(buf
);
311 #if HASH_OBJ_CONSTRUCTOR
313 EVP_tp_init(EVPobject
*self
, PyObject
*args
, PyObject
*kwds
)
315 static char *kwlist
[] = {"name", "string", NULL
};
316 PyObject
*name_obj
= NULL
;
317 PyObject
*data_obj
= NULL
;
320 const EVP_MD
*digest
;
322 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|O:HASH", kwlist
,
323 &name_obj
, &data_obj
)) {
328 GET_BUFFER_VIEW_OR_ERROUT(data_obj
, &view
, -1);
330 if (!PyArg_Parse(name_obj
, "s", &nameStr
)) {
331 PyErr_SetString(PyExc_TypeError
, "name must be a string");
333 PyBuffer_Release(&view
);
337 digest
= EVP_get_digestbyname(nameStr
);
339 PyErr_SetString(PyExc_ValueError
, "unknown hash function");
341 PyBuffer_Release(&view
);
344 EVP_DigestInit(&self
->ctx
, digest
);
346 self
->name
= name_obj
;
347 Py_INCREF(self
->name
);
350 if (view
.len
>= HASHLIB_GIL_MINSIZE
) {
351 Py_BEGIN_ALLOW_THREADS
352 EVP_hash(self
, view
.buf
, view
.len
);
355 EVP_hash(self
, view
.buf
, view
.len
);
357 PyBuffer_Release(&view
);
365 PyDoc_STRVAR(hashtype_doc
,
366 "A hash represents the object used to calculate a checksum of a\n\
367 string of information.\n\
371 update() -- updates the current digest with an additional string\n\
372 digest() -- return the current digest value\n\
373 hexdigest() -- return the current digest as a string of hexadecimal digits\n\
374 copy() -- return a copy of the current hash object\n\
378 name -- the hash algorithm being used by this object\n\
379 digest_size -- number of bytes in this hashes output\n");
381 static PyTypeObject EVPtype
= {
382 PyVarObject_HEAD_INIT(NULL
, 0)
383 "_hashlib.HASH", /*tp_name*/
384 sizeof(EVPobject
), /*tp_basicsize*/
387 (destructor
)EVP_dealloc
, /*tp_dealloc*/
392 EVP_repr
, /*tp_repr*/
394 0, /*tp_as_sequence*/
402 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
403 hashtype_doc
, /*tp_doc*/
406 0, /*tp_richcompare*/
407 0, /*tp_weaklistoffset*/
410 EVP_methods
, /* tp_methods */
411 EVP_members
, /* tp_members */
412 EVP_getseters
, /* tp_getset */
416 0, /* tp_descr_get */
417 0, /* tp_descr_set */
418 0, /* tp_dictoffset */
420 #if HASH_OBJ_CONSTRUCTOR
421 (initproc
)EVP_tp_init
, /* tp_init */
426 EVPnew(PyObject
*name_obj
,
427 const EVP_MD
*digest
, const EVP_MD_CTX
*initial_ctx
,
428 const unsigned char *cp
, Py_ssize_t len
)
432 if (!digest
&& !initial_ctx
) {
433 PyErr_SetString(PyExc_ValueError
, "unsupported hash type");
437 if ((self
= newEVPobject(name_obj
)) == NULL
)
441 EVP_MD_CTX_copy(&self
->ctx
, initial_ctx
);
443 EVP_DigestInit(&self
->ctx
, digest
);
447 if (len
>= HASHLIB_GIL_MINSIZE
) {
448 Py_BEGIN_ALLOW_THREADS
449 EVP_hash(self
, cp
, len
);
452 EVP_hash(self
, cp
, len
);
456 return (PyObject
*)self
;
460 /* The module-level function: new() */
462 PyDoc_STRVAR(EVP_new__doc__
,
463 "Return a new hash object using the named algorithm.\n\
464 An optional string argument may be provided and will be\n\
465 automatically hashed.\n\
467 The MD5 and SHA1 algorithms are always supported.\n");
470 EVP_new(PyObject
*self
, PyObject
*args
, PyObject
*kwdict
)
472 static char *kwlist
[] = {"name", "string", NULL
};
473 PyObject
*name_obj
= NULL
;
474 PyObject
*data_obj
= NULL
;
475 Py_buffer view
= { 0 };
478 const EVP_MD
*digest
;
480 if (!PyArg_ParseTupleAndKeywords(args
, kwdict
, "O|O:new", kwlist
,
481 &name_obj
, &data_obj
)) {
485 if (!PyArg_Parse(name_obj
, "s", &name
)) {
486 PyErr_SetString(PyExc_TypeError
, "name must be a string");
491 GET_BUFFER_VIEW_OR_ERROUT(data_obj
, &view
, NULL
);
493 digest
= EVP_get_digestbyname(name
);
495 ret_obj
= EVPnew(name_obj
, digest
, NULL
, (unsigned char*)view
.buf
,
496 Py_SAFE_DOWNCAST(view
.len
, Py_ssize_t
, unsigned int));
499 PyBuffer_Release(&view
);
504 * This macro generates constructor function definitions for specific
505 * hash algorithms. These constructors are much faster than calling
506 * the generic one passing it a python string and are noticably
507 * faster than calling a python new() wrapper. Thats important for
508 * code that wants to make hashes of a bunch of small strings.
510 #define GEN_CONSTRUCTOR(NAME) \
512 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
514 PyObject *data_obj = NULL; \
515 Py_buffer view = { 0 }; \
518 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
523 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL); \
526 CONST_ ## NAME ## _name_obj, \
528 CONST_new_ ## NAME ## _ctx_p, \
529 (unsigned char*)view.buf, \
530 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); \
533 PyBuffer_Release(&view); \
537 /* a PyMethodDef structure for the constructor */
538 #define CONSTRUCTOR_METH_DEF(NAME) \
539 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
540 PyDoc_STR("Returns a " #NAME \
541 " hash object; optionally initialized with a string") \
544 /* used in the init function to setup a constructor */
545 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
546 CONST_ ## NAME ## _name_obj = PyString_FromString(#NAME); \
547 if (EVP_get_digestbyname(#NAME)) { \
548 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
549 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
554 GEN_CONSTRUCTOR(sha1
)
555 GEN_CONSTRUCTOR(sha224
)
556 GEN_CONSTRUCTOR(sha256
)
557 GEN_CONSTRUCTOR(sha384
)
558 GEN_CONSTRUCTOR(sha512
)
560 /* List of functions exported by this module */
562 static struct PyMethodDef EVP_functions
[] = {
563 {"new", (PyCFunction
)EVP_new
, METH_VARARGS
|METH_KEYWORDS
, EVP_new__doc__
},
564 CONSTRUCTOR_METH_DEF(md5
),
565 CONSTRUCTOR_METH_DEF(sha1
),
566 CONSTRUCTOR_METH_DEF(sha224
),
567 CONSTRUCTOR_METH_DEF(sha256
),
568 CONSTRUCTOR_METH_DEF(sha384
),
569 CONSTRUCTOR_METH_DEF(sha512
),
570 {NULL
, NULL
} /* Sentinel */
574 /* Initialize this module. */
581 OpenSSL_add_all_digests();
583 /* TODO build EVP_functions openssl_* entries dynamically based
584 * on what hashes are supported rather than listing many
585 * but having some be unsupported. Only init appropriate
588 Py_TYPE(&EVPtype
) = &PyType_Type
;
589 if (PyType_Ready(&EVPtype
) < 0)
592 m
= Py_InitModule("_hashlib", EVP_functions
);
596 #if HASH_OBJ_CONSTRUCTOR
598 PyModule_AddObject(m
, "HASH", (PyObject
*)&EVPtype
);
601 /* these constants are used by the convenience constructors */
602 INIT_CONSTRUCTOR_CONSTANTS(md5
);
603 INIT_CONSTRUCTOR_CONSTANTS(sha1
);
604 INIT_CONSTRUCTOR_CONSTANTS(sha224
);
605 INIT_CONSTRUCTOR_CONSTANTS(sha256
);
606 INIT_CONSTRUCTOR_CONSTANTS(sha384
);
607 INIT_CONSTRUCTOR_CONSTANTS(sha512
);