1 /* Module that wraps all OpenSSL hash algorithms */
4 * Copyright (C) 2005-2010 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"
21 #define ENTER_HASHLIB(obj) \
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 \
29 #define LEAVE_HASHLIB(obj) \
31 PyThread_release_lock((obj)->lock); \
34 #define ENTER_HASHLIB(obj)
35 #define LEAVE_HASHLIB(obj)
38 /* EVP is the preferred interface to hashing in OpenSSL */
39 #include <openssl/evp.h>
41 #define MUNCH_SIZE INT_MAX
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
47 #ifndef HASH_OBJ_CONSTRUCTOR
48 #define HASH_OBJ_CONSTRUCTOR 0
54 PyObject
*name
; /* name of this hash algorithm */
55 EVP_MD_CTX ctx
; /* OpenSSL message digest context */
57 PyThread_type_lock lock
; /* OpenSSL context lock */
62 static PyTypeObject EVPtype
;
65 #define DEFINE_CONSTS_FOR_NEW(Name) \
66 static PyObject *CONST_ ## Name ## _name_obj; \
67 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
68 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
70 DEFINE_CONSTS_FOR_NEW(md5
)
71 DEFINE_CONSTS_FOR_NEW(sha1
)
72 DEFINE_CONSTS_FOR_NEW(sha224
)
73 DEFINE_CONSTS_FOR_NEW(sha256
)
74 DEFINE_CONSTS_FOR_NEW(sha384
)
75 DEFINE_CONSTS_FOR_NEW(sha512
)
79 newEVPobject(PyObject
*name
)
81 EVPobject
*retval
= (EVPobject
*)PyObject_New(EVPobject
, &EVPtype
);
83 /* save the name for .name to return */
96 EVP_hash(EVPobject
*self
, const void *vp
, Py_ssize_t len
)
99 const unsigned char *cp
= (const unsigned char *)vp
;
102 if (len
> (Py_ssize_t
)MUNCH_SIZE
)
103 process
= MUNCH_SIZE
;
105 process
= Py_SAFE_DOWNCAST(len
, Py_ssize_t
, unsigned int);
106 EVP_DigestUpdate(&self
->ctx
, (const void*)cp
, process
);
112 /* Internal methods for a hash object */
115 EVP_dealloc(EVPobject
*self
)
118 if (self
->lock
!= NULL
)
119 PyThread_free_lock(self
->lock
);
121 EVP_MD_CTX_cleanup(&self
->ctx
);
122 Py_XDECREF(self
->name
);
126 static void locked_EVP_MD_CTX_copy(EVP_MD_CTX
*new_ctx_p
, EVPobject
*self
)
129 EVP_MD_CTX_copy(new_ctx_p
, &self
->ctx
);
133 /* External methods for a hash object */
135 PyDoc_STRVAR(EVP_copy__doc__
, "Return a copy of the hash object.");
139 EVP_copy(EVPobject
*self
, PyObject
*unused
)
143 if ( (newobj
= newEVPobject(self
->name
))==NULL
)
146 locked_EVP_MD_CTX_copy(&newobj
->ctx
, self
);
147 return (PyObject
*)newobj
;
150 PyDoc_STRVAR(EVP_digest__doc__
,
151 "Return the digest value as a string of binary data.");
154 EVP_digest(EVPobject
*self
, PyObject
*unused
)
156 unsigned char digest
[EVP_MAX_MD_SIZE
];
159 unsigned int digest_size
;
161 locked_EVP_MD_CTX_copy(&temp_ctx
, self
);
162 digest_size
= EVP_MD_CTX_size(&temp_ctx
);
163 EVP_DigestFinal(&temp_ctx
, digest
, NULL
);
165 retval
= PyString_FromStringAndSize((const char *)digest
, digest_size
);
166 EVP_MD_CTX_cleanup(&temp_ctx
);
170 PyDoc_STRVAR(EVP_hexdigest__doc__
,
171 "Return the digest value as a string of hexadecimal digits.");
174 EVP_hexdigest(EVPobject
*self
, PyObject
*unused
)
176 unsigned char digest
[EVP_MAX_MD_SIZE
];
180 unsigned int i
, j
, digest_size
;
182 /* Get the raw (binary) digest value */
183 locked_EVP_MD_CTX_copy(&temp_ctx
, self
);
184 digest_size
= EVP_MD_CTX_size(&temp_ctx
);
185 EVP_DigestFinal(&temp_ctx
, digest
, NULL
);
187 EVP_MD_CTX_cleanup(&temp_ctx
);
189 /* Create a new string */
190 /* NOTE: not thread safe! modifying an already created string object */
191 /* (not a problem because we hold the GIL by default) */
192 retval
= PyString_FromStringAndSize(NULL
, digest_size
* 2);
195 hex_digest
= PyString_AsString(retval
);
201 /* Make hex version of the digest */
202 for(i
=j
=0; i
<digest_size
; i
++) {
204 c
= (digest
[i
] >> 4) & 0xf;
205 c
= (c
>9) ? c
+'a'-10 : c
+ '0';
207 c
= (digest
[i
] & 0xf);
208 c
= (c
>9) ? c
+'a'-10 : c
+ '0';
214 PyDoc_STRVAR(EVP_update__doc__
,
215 "Update this hash object's state with the provided string.");
218 EVP_update(EVPobject
*self
, PyObject
*args
)
222 if (!PyArg_ParseTuple(args
, "s*:update", &view
))
226 if (self
->lock
== NULL
&& view
.len
>= HASHLIB_GIL_MINSIZE
) {
227 self
->lock
= PyThread_allocate_lock();
228 /* fail? lock = NULL and we fail over to non-threaded code. */
231 if (self
->lock
!= NULL
) {
232 Py_BEGIN_ALLOW_THREADS
233 PyThread_acquire_lock(self
->lock
, 1);
234 EVP_hash(self
, view
.buf
, view
.len
);
235 PyThread_release_lock(self
->lock
);
241 EVP_hash(self
, view
.buf
, view
.len
);
244 PyBuffer_Release(&view
);
249 static PyMethodDef EVP_methods
[] = {
250 {"update", (PyCFunction
)EVP_update
, METH_VARARGS
, EVP_update__doc__
},
251 {"digest", (PyCFunction
)EVP_digest
, METH_NOARGS
, EVP_digest__doc__
},
252 {"hexdigest", (PyCFunction
)EVP_hexdigest
, METH_NOARGS
, EVP_hexdigest__doc__
},
253 {"copy", (PyCFunction
)EVP_copy
, METH_NOARGS
, EVP_copy__doc__
},
254 {NULL
, NULL
} /* sentinel */
258 EVP_get_block_size(EVPobject
*self
, void *closure
)
261 block_size
= EVP_MD_CTX_block_size(&self
->ctx
);
262 return PyLong_FromLong(block_size
);
266 EVP_get_digest_size(EVPobject
*self
, void *closure
)
269 size
= EVP_MD_CTX_size(&self
->ctx
);
270 return PyLong_FromLong(size
);
273 static PyMemberDef EVP_members
[] = {
274 {"name", T_OBJECT
, offsetof(EVPobject
, name
), READONLY
, PyDoc_STR("algorithm name.")},
275 {NULL
} /* Sentinel */
278 static PyGetSetDef EVP_getseters
[] = {
280 (getter
)EVP_get_digest_size
, NULL
,
284 (getter
)EVP_get_block_size
, NULL
,
287 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
288 * the old sha module also supported 'digestsize'. ugh. */
290 (getter
)EVP_get_digest_size
, NULL
,
293 {NULL
} /* Sentinel */
298 EVP_repr(PyObject
*self
)
301 PyOS_snprintf(buf
, sizeof(buf
), "<%s HASH object @ %p>",
302 PyString_AsString(((EVPobject
*)self
)->name
), self
);
303 return PyString_FromString(buf
);
306 #if HASH_OBJ_CONSTRUCTOR
308 EVP_tp_init(EVPobject
*self
, PyObject
*args
, PyObject
*kwds
)
310 static char *kwlist
[] = {"name", "string", NULL
};
311 PyObject
*name_obj
= NULL
;
312 Py_buffer view
= { 0 };
314 const EVP_MD
*digest
;
316 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|s*:HASH", kwlist
,
321 if (!PyArg_Parse(name_obj
, "s", &nameStr
)) {
322 PyErr_SetString(PyExc_TypeError
, "name must be a string");
323 PyBuffer_Release(&view
);
327 digest
= EVP_get_digestbyname(nameStr
);
329 PyErr_SetString(PyExc_ValueError
, "unknown hash function");
330 PyBuffer_Release(&view
);
333 EVP_DigestInit(&self
->ctx
, digest
);
335 self
->name
= name_obj
;
336 Py_INCREF(self
->name
);
339 if (view
.len
>= HASHLIB_GIL_MINSIZE
) {
340 Py_BEGIN_ALLOW_THREADS
341 EVP_hash(self
, view
.buf
, view
.len
);
344 EVP_hash(self
, view
.buf
, view
.len
);
346 PyBuffer_Release(&view
);
354 PyDoc_STRVAR(hashtype_doc
,
355 "A hash represents the object used to calculate a checksum of a\n\
356 string of information.\n\
360 update() -- updates the current digest with an additional string\n\
361 digest() -- return the current digest value\n\
362 hexdigest() -- return the current digest as a string of hexadecimal digits\n\
363 copy() -- return a copy of the current hash object\n\
367 name -- the hash algorithm being used by this object\n\
368 digest_size -- number of bytes in this hashes output\n");
370 static PyTypeObject EVPtype
= {
371 PyVarObject_HEAD_INIT(NULL
, 0)
372 "_hashlib.HASH", /*tp_name*/
373 sizeof(EVPobject
), /*tp_basicsize*/
376 (destructor
)EVP_dealloc
, /*tp_dealloc*/
381 EVP_repr
, /*tp_repr*/
383 0, /*tp_as_sequence*/
391 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
392 hashtype_doc
, /*tp_doc*/
395 0, /*tp_richcompare*/
396 0, /*tp_weaklistoffset*/
399 EVP_methods
, /* tp_methods */
400 EVP_members
, /* tp_members */
401 EVP_getseters
, /* tp_getset */
405 0, /* tp_descr_get */
406 0, /* tp_descr_set */
407 0, /* tp_dictoffset */
409 #if HASH_OBJ_CONSTRUCTOR
410 (initproc
)EVP_tp_init
, /* tp_init */
415 EVPnew(PyObject
*name_obj
,
416 const EVP_MD
*digest
, const EVP_MD_CTX
*initial_ctx
,
417 const unsigned char *cp
, Py_ssize_t len
)
421 if (!digest
&& !initial_ctx
) {
422 PyErr_SetString(PyExc_ValueError
, "unsupported hash type");
426 if ((self
= newEVPobject(name_obj
)) == NULL
)
430 EVP_MD_CTX_copy(&self
->ctx
, initial_ctx
);
432 EVP_DigestInit(&self
->ctx
, digest
);
436 if (len
>= HASHLIB_GIL_MINSIZE
) {
437 Py_BEGIN_ALLOW_THREADS
438 EVP_hash(self
, cp
, len
);
441 EVP_hash(self
, cp
, len
);
445 return (PyObject
*)self
;
449 /* The module-level function: new() */
451 PyDoc_STRVAR(EVP_new__doc__
,
452 "Return a new hash object using the named algorithm.\n\
453 An optional string argument may be provided and will be\n\
454 automatically hashed.\n\
456 The MD5 and SHA1 algorithms are always supported.\n");
459 EVP_new(PyObject
*self
, PyObject
*args
, PyObject
*kwdict
)
461 static char *kwlist
[] = {"name", "string", NULL
};
462 PyObject
*name_obj
= NULL
;
463 Py_buffer view
= { 0 };
466 const EVP_MD
*digest
;
468 if (!PyArg_ParseTupleAndKeywords(args
, kwdict
, "O|s*:new", kwlist
,
473 if (!PyArg_Parse(name_obj
, "s", &name
)) {
474 PyErr_SetString(PyExc_TypeError
, "name must be a string");
478 digest
= EVP_get_digestbyname(name
);
480 ret_obj
= EVPnew(name_obj
, digest
, NULL
, (unsigned char*)view
.buf
,
482 PyBuffer_Release(&view
);
488 * This macro generates constructor function definitions for specific
489 * hash algorithms. These constructors are much faster than calling
490 * the generic one passing it a python string and are noticably
491 * faster than calling a python new() wrapper. Thats important for
492 * code that wants to make hashes of a bunch of small strings.
494 #define GEN_CONSTRUCTOR(NAME) \
496 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
498 Py_buffer view = { 0 }; \
501 if (!PyArg_ParseTuple(args, "|s*:" #NAME , &view)) { \
506 CONST_ ## NAME ## _name_obj, \
508 CONST_new_ ## NAME ## _ctx_p, \
509 (unsigned char*)view.buf, view.len); \
510 PyBuffer_Release(&view); \
514 /* a PyMethodDef structure for the constructor */
515 #define CONSTRUCTOR_METH_DEF(NAME) \
516 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
517 PyDoc_STR("Returns a " #NAME \
518 " hash object; optionally initialized with a string") \
521 /* used in the init function to setup a constructor */
522 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
523 CONST_ ## NAME ## _name_obj = PyString_FromString(#NAME); \
524 if (EVP_get_digestbyname(#NAME)) { \
525 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
526 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
531 GEN_CONSTRUCTOR(sha1
)
532 GEN_CONSTRUCTOR(sha224
)
533 GEN_CONSTRUCTOR(sha256
)
534 GEN_CONSTRUCTOR(sha384
)
535 GEN_CONSTRUCTOR(sha512
)
537 /* List of functions exported by this module */
539 static struct PyMethodDef EVP_functions
[] = {
540 {"new", (PyCFunction
)EVP_new
, METH_VARARGS
|METH_KEYWORDS
, EVP_new__doc__
},
541 CONSTRUCTOR_METH_DEF(md5
),
542 CONSTRUCTOR_METH_DEF(sha1
),
543 CONSTRUCTOR_METH_DEF(sha224
),
544 CONSTRUCTOR_METH_DEF(sha256
),
545 CONSTRUCTOR_METH_DEF(sha384
),
546 CONSTRUCTOR_METH_DEF(sha512
),
547 {NULL
, NULL
} /* Sentinel */
551 /* Initialize this module. */
558 OpenSSL_add_all_digests();
560 /* TODO build EVP_functions openssl_* entries dynamically based
561 * on what hashes are supported rather than listing many
562 * but having some be unsupported. Only init appropriate
565 Py_TYPE(&EVPtype
) = &PyType_Type
;
566 if (PyType_Ready(&EVPtype
) < 0)
569 m
= Py_InitModule("_hashlib", EVP_functions
);
573 #if HASH_OBJ_CONSTRUCTOR
575 PyModule_AddObject(m
, "HASH", (PyObject
*)&EVPtype
);
578 /* these constants are used by the convenience constructors */
579 INIT_CONSTRUCTOR_CONSTANTS(md5
);
580 INIT_CONSTRUCTOR_CONSTANTS(sha1
);
581 INIT_CONSTRUCTOR_CONSTANTS(sha224
);
582 INIT_CONSTRUCTOR_CONSTANTS(sha256
);
583 INIT_CONSTRUCTOR_CONSTANTS(sha384
);
584 INIT_CONSTRUCTOR_CONSTANTS(sha512
);