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"
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
;
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
= PyBytes_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 /* Allocate a new buffer */
190 hex_digest
= PyMem_Malloc(digest_size
* 2 + 1);
192 return PyErr_NoMemory();
194 /* Make hex version of the digest */
195 for(i
=j
=0; i
<digest_size
; i
++) {
197 c
= (digest
[i
] >> 4) & 0xf;
198 c
= (c
>9) ? c
+'a'-10 : c
+ '0';
200 c
= (digest
[i
] & 0xf);
201 c
= (c
>9) ? c
+'a'-10 : c
+ '0';
204 retval
= PyUnicode_FromStringAndSize(hex_digest
, digest_size
* 2);
205 PyMem_Free(hex_digest
);
209 PyDoc_STRVAR(EVP_update__doc__
,
210 "Update this hash object's state with the provided string.");
213 EVP_update(EVPobject
*self
, PyObject
*args
)
218 if (!PyArg_ParseTuple(args
, "O:update", &obj
))
221 GET_BUFFER_VIEW_OR_ERROUT(obj
, &view
);
224 if (self
->lock
== NULL
&& view
.len
>= HASHLIB_GIL_MINSIZE
) {
225 self
->lock
= PyThread_allocate_lock();
226 /* fail? lock = NULL and we fail over to non-threaded code. */
229 if (self
->lock
!= NULL
) {
230 Py_BEGIN_ALLOW_THREADS
231 PyThread_acquire_lock(self
->lock
, 1);
232 EVP_hash(self
, view
.buf
, view
.len
);
233 PyThread_release_lock(self
->lock
);
236 EVP_hash(self
, view
.buf
, view
.len
);
239 EVP_hash(self
, view
.buf
, view
.len
);
242 PyBuffer_Release(&view
);
246 static PyMethodDef EVP_methods
[] = {
247 {"update", (PyCFunction
)EVP_update
, METH_VARARGS
, EVP_update__doc__
},
248 {"digest", (PyCFunction
)EVP_digest
, METH_NOARGS
, EVP_digest__doc__
},
249 {"hexdigest", (PyCFunction
)EVP_hexdigest
, METH_NOARGS
, EVP_hexdigest__doc__
},
250 {"copy", (PyCFunction
)EVP_copy
, METH_NOARGS
, EVP_copy__doc__
},
251 {NULL
, NULL
} /* sentinel */
255 EVP_get_block_size(EVPobject
*self
, void *closure
)
258 block_size
= EVP_MD_CTX_block_size(&self
->ctx
);
259 return PyLong_FromLong(block_size
);
263 EVP_get_digest_size(EVPobject
*self
, void *closure
)
266 size
= EVP_MD_CTX_size(&self
->ctx
);
267 return PyLong_FromLong(size
);
270 static PyMemberDef EVP_members
[] = {
271 {"name", T_OBJECT
, offsetof(EVPobject
, name
), READONLY
, PyDoc_STR("algorithm name.")},
272 {NULL
} /* Sentinel */
275 static PyGetSetDef EVP_getseters
[] = {
277 (getter
)EVP_get_digest_size
, NULL
,
281 (getter
)EVP_get_block_size
, NULL
,
284 {NULL
} /* Sentinel */
289 EVP_repr(EVPobject
*self
)
291 return PyUnicode_FromFormat("<%U HASH object @ %p>", self
->name
, self
);
294 #if HASH_OBJ_CONSTRUCTOR
296 EVP_tp_init(EVPobject
*self
, PyObject
*args
, PyObject
*kwds
)
298 static char *kwlist
[] = {"name", "string", NULL
};
299 PyObject
*name_obj
= NULL
;
300 PyObject
*data_obj
= NULL
;
303 const EVP_MD
*digest
;
305 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|O:HASH", kwlist
,
306 &name_obj
, &data_obj
)) {
311 GET_BUFFER_VIEW_OR_ERROUT(data_obj
, &view
);
313 if (!PyArg_Parse(name_obj
, "s", &nameStr
)) {
314 PyErr_SetString(PyExc_TypeError
, "name must be a string");
316 PyBuffer_Release(&view
);
320 digest
= EVP_get_digestbyname(nameStr
);
322 PyErr_SetString(PyExc_ValueError
, "unknown hash function");
324 PyBuffer_Release(&view
);
327 EVP_DigestInit(&self
->ctx
, digest
);
329 self
->name
= name_obj
;
330 Py_INCREF(self
->name
);
333 if (view
.len
>= HASHLIB_GIL_MINSIZE
) {
334 Py_BEGIN_ALLOW_THREADS
335 EVP_hash(self
, view
.buf
, view
.len
);
338 EVP_hash(self
, view
.buf
, view
.len
);
340 PyBuffer_Release(&view
);
348 PyDoc_STRVAR(hashtype_doc
,
349 "A hash represents the object used to calculate a checksum of a\n\
350 string of information.\n\
354 update() -- updates the current digest with an additional string\n\
355 digest() -- return the current digest value\n\
356 hexdigest() -- return the current digest as a string of hexadecimal digits\n\
357 copy() -- return a copy of the current hash object\n\
361 name -- the hash algorithm being used by this object\n\
362 digest_size -- number of bytes in this hashes output\n");
364 static PyTypeObject EVPtype
= {
365 PyVarObject_HEAD_INIT(NULL
, 0)
366 "_hashlib.HASH", /*tp_name*/
367 sizeof(EVPobject
), /*tp_basicsize*/
370 (destructor
)EVP_dealloc
, /*tp_dealloc*/
375 (reprfunc
)EVP_repr
, /*tp_repr*/
377 0, /*tp_as_sequence*/
385 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
386 hashtype_doc
, /*tp_doc*/
389 0, /*tp_richcompare*/
390 0, /*tp_weaklistoffset*/
393 EVP_methods
, /* tp_methods */
394 EVP_members
, /* tp_members */
395 EVP_getseters
, /* tp_getset */
399 0, /* tp_descr_get */
400 0, /* tp_descr_set */
401 0, /* tp_dictoffset */
403 #if HASH_OBJ_CONSTRUCTOR
404 (initproc
)EVP_tp_init
, /* tp_init */
409 EVPnew(PyObject
*name_obj
,
410 const EVP_MD
*digest
, const EVP_MD_CTX
*initial_ctx
,
411 const unsigned char *cp
, Py_ssize_t len
)
415 if (!digest
&& !initial_ctx
) {
416 PyErr_SetString(PyExc_ValueError
, "unsupported hash type");
420 if ((self
= newEVPobject(name_obj
)) == NULL
)
424 EVP_MD_CTX_copy(&self
->ctx
, initial_ctx
);
426 EVP_DigestInit(&self
->ctx
, digest
);
430 if (len
>= HASHLIB_GIL_MINSIZE
) {
431 Py_BEGIN_ALLOW_THREADS
432 EVP_hash(self
, cp
, len
);
435 EVP_hash(self
, cp
, len
);
439 return (PyObject
*)self
;
443 /* The module-level function: new() */
445 PyDoc_STRVAR(EVP_new__doc__
,
446 "Return a new hash object using the named algorithm.\n\
447 An optional string argument may be provided and will be\n\
448 automatically hashed.\n\
450 The MD5 and SHA1 algorithms are always supported.\n");
453 EVP_new(PyObject
*self
, PyObject
*args
, PyObject
*kwdict
)
455 static char *kwlist
[] = {"name", "string", NULL
};
456 PyObject
*name_obj
= NULL
;
457 PyObject
*data_obj
= NULL
;
458 Py_buffer view
= { 0 };
461 const EVP_MD
*digest
;
463 if (!PyArg_ParseTupleAndKeywords(args
, kwdict
, "O|O:new", kwlist
,
464 &name_obj
, &data_obj
)) {
468 if (!PyArg_Parse(name_obj
, "s", &name
)) {
469 PyErr_SetString(PyExc_TypeError
, "name must be a string");
474 GET_BUFFER_VIEW_OR_ERROUT(data_obj
, &view
);
476 digest
= EVP_get_digestbyname(name
);
478 ret_obj
= EVPnew(name_obj
, digest
, NULL
, (unsigned char*)view
.buf
, view
.len
);
481 PyBuffer_Release(&view
);
486 * This macro generates constructor function definitions for specific
487 * hash algorithms. These constructors are much faster than calling
488 * the generic one passing it a python string and are noticably
489 * faster than calling a python new() wrapper. Thats important for
490 * code that wants to make hashes of a bunch of small strings.
492 #define GEN_CONSTRUCTOR(NAME) \
494 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
496 PyObject *data_obj = NULL; \
497 Py_buffer view = { 0 }; \
500 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
505 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
508 CONST_ ## NAME ## _name_obj, \
510 CONST_new_ ## NAME ## _ctx_p, \
511 (unsigned char*)view.buf, \
515 PyBuffer_Release(&view); \
519 /* a PyMethodDef structure for the constructor */
520 #define CONSTRUCTOR_METH_DEF(NAME) \
521 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
522 PyDoc_STR("Returns a " #NAME \
523 " hash object; optionally initialized with a string") \
526 /* used in the init function to setup a constructor */
527 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
528 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
529 if (EVP_get_digestbyname(#NAME)) { \
530 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
531 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
536 GEN_CONSTRUCTOR(sha1
)
537 GEN_CONSTRUCTOR(sha224
)
538 GEN_CONSTRUCTOR(sha256
)
539 GEN_CONSTRUCTOR(sha384
)
540 GEN_CONSTRUCTOR(sha512
)
542 /* List of functions exported by this module */
544 static struct PyMethodDef EVP_functions
[] = {
545 {"new", (PyCFunction
)EVP_new
, METH_VARARGS
|METH_KEYWORDS
, EVP_new__doc__
},
546 CONSTRUCTOR_METH_DEF(md5
),
547 CONSTRUCTOR_METH_DEF(sha1
),
548 CONSTRUCTOR_METH_DEF(sha224
),
549 CONSTRUCTOR_METH_DEF(sha256
),
550 CONSTRUCTOR_METH_DEF(sha384
),
551 CONSTRUCTOR_METH_DEF(sha512
),
552 {NULL
, NULL
} /* Sentinel */
556 /* Initialize this module. */
559 static struct PyModuleDef _hashlibmodule
= {
560 PyModuleDef_HEAD_INIT
,
572 PyInit__hashlib(void)
576 OpenSSL_add_all_digests();
578 /* TODO build EVP_functions openssl_* entries dynamically based
579 * on what hashes are supported rather than listing many
580 * but having some be unsupported. Only init appropriate
583 Py_TYPE(&EVPtype
) = &PyType_Type
;
584 if (PyType_Ready(&EVPtype
) < 0)
587 m
= PyModule_Create(&_hashlibmodule
);
591 #if HASH_OBJ_CONSTRUCTOR
593 PyModule_AddObject(m
, "HASH", (PyObject
*)&EVPtype
);
596 /* these constants are used by the convenience constructors */
597 INIT_CONSTRUCTOR_CONSTANTS(md5
);
598 INIT_CONSTRUCTOR_CONSTANTS(sha1
);
599 INIT_CONSTRUCTOR_CONSTANTS(sha224
);
600 INIT_CONSTRUCTOR_CONSTANTS(sha256
);
601 INIT_CONSTRUCTOR_CONSTANTS(sha384
);
602 INIT_CONSTRUCTOR_CONSTANTS(sha512
);