1 /* Module that wraps all OpenSSL hash algorithms */
4 * Copyright (C) 2005 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"
19 /* EVP is the preferred interface to hashing in OpenSSL */
20 #include <openssl/evp.h>
23 #ifndef HASH_OBJ_CONSTRUCTOR
24 #define HASH_OBJ_CONSTRUCTOR 0
29 PyObject
*name
; /* name of this hash algorithm */
30 EVP_MD_CTX ctx
; /* OpenSSL message digest context */
34 static PyTypeObject EVPtype
;
37 #define DEFINE_CONSTS_FOR_NEW(Name) \
38 static PyObject *CONST_ ## Name ## _name_obj; \
39 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
40 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
42 DEFINE_CONSTS_FOR_NEW(md5
)
43 DEFINE_CONSTS_FOR_NEW(sha1
)
44 DEFINE_CONSTS_FOR_NEW(sha224
)
45 DEFINE_CONSTS_FOR_NEW(sha256
)
46 DEFINE_CONSTS_FOR_NEW(sha384
)
47 DEFINE_CONSTS_FOR_NEW(sha512
)
51 newEVPobject(PyObject
*name
)
53 EVPobject
*retval
= (EVPobject
*)PyObject_New(EVPobject
, &EVPtype
);
55 /* save the name for .name to return */
64 /* Internal methods for a hash object */
67 EVP_dealloc(PyObject
*ptr
)
69 EVP_MD_CTX_cleanup(&((EVPobject
*)ptr
)->ctx
);
70 Py_XDECREF(((EVPobject
*)ptr
)->name
);
75 /* External methods for a hash object */
77 PyDoc_STRVAR(EVP_copy__doc__
, "Return a copy of the hash object.");
80 EVP_copy(EVPobject
*self
, PyObject
*unused
)
84 if ( (newobj
= newEVPobject(self
->name
))==NULL
)
87 EVP_MD_CTX_copy(&newobj
->ctx
, &self
->ctx
);
88 return (PyObject
*)newobj
;
91 PyDoc_STRVAR(EVP_digest__doc__
,
92 "Return the digest value as a string of binary data.");
95 EVP_digest(EVPobject
*self
, PyObject
*unused
)
97 unsigned char digest
[EVP_MAX_MD_SIZE
];
100 unsigned int digest_size
;
102 EVP_MD_CTX_copy(&temp_ctx
, &self
->ctx
);
103 digest_size
= EVP_MD_CTX_size(&temp_ctx
);
104 EVP_DigestFinal(&temp_ctx
, digest
, NULL
);
106 retval
= PyBytes_FromStringAndSize((const char *)digest
, digest_size
);
107 EVP_MD_CTX_cleanup(&temp_ctx
);
111 PyDoc_STRVAR(EVP_hexdigest__doc__
,
112 "Return the digest value as a string of hexadecimal digits.");
115 EVP_hexdigest(EVPobject
*self
, PyObject
*unused
)
117 unsigned char digest
[EVP_MAX_MD_SIZE
];
121 unsigned int i
, j
, digest_size
;
123 /* Get the raw (binary) digest value */
124 EVP_MD_CTX_copy(&temp_ctx
, &self
->ctx
);
125 digest_size
= EVP_MD_CTX_size(&temp_ctx
);
126 EVP_DigestFinal(&temp_ctx
, digest
, NULL
);
128 EVP_MD_CTX_cleanup(&temp_ctx
);
130 /* Create a new string */
131 /* NOTE: not thread safe! modifying an already created string object */
132 /* (not a problem because we hold the GIL by default) */
133 retval
= PyBytes_FromStringAndSize(NULL
, digest_size
* 2);
136 hex_digest
= PyBytes_AsString(retval
);
142 /* Make hex version of the digest */
143 for(i
=j
=0; i
<digest_size
; i
++) {
145 c
= (digest
[i
] >> 4) & 0xf;
146 c
= (c
>9) ? c
+'a'-10 : c
+ '0';
148 c
= (digest
[i
] & 0xf);
149 c
= (c
>9) ? c
+'a'-10 : c
+ '0';
155 PyDoc_STRVAR(EVP_update__doc__
,
156 "Update this hash object's state with the provided string.");
159 EVP_update(EVPobject
*self
, PyObject
*args
)
164 if (!PyArg_ParseTuple(args
, "s#:update", &cp
, &len
))
167 EVP_DigestUpdate(&self
->ctx
, cp
, Py_SAFE_DOWNCAST(len
, Py_ssize_t
,
174 static PyMethodDef EVP_methods
[] = {
175 {"update", (PyCFunction
)EVP_update
, METH_VARARGS
, EVP_update__doc__
},
176 {"digest", (PyCFunction
)EVP_digest
, METH_NOARGS
, EVP_digest__doc__
},
177 {"hexdigest", (PyCFunction
)EVP_hexdigest
, METH_NOARGS
, EVP_hexdigest__doc__
},
178 {"copy", (PyCFunction
)EVP_copy
, METH_NOARGS
, EVP_copy__doc__
},
179 {NULL
, NULL
} /* sentinel */
183 EVP_get_block_size(EVPobject
*self
, void *closure
)
185 return PyInt_FromLong(EVP_MD_CTX_block_size(&((EVPobject
*)self
)->ctx
));
189 EVP_get_digest_size(EVPobject
*self
, void *closure
)
191 return PyInt_FromLong(EVP_MD_CTX_size(&((EVPobject
*)self
)->ctx
));
194 static PyMemberDef EVP_members
[] = {
195 {"name", T_OBJECT
, offsetof(EVPobject
, name
), READONLY
, PyDoc_STR("algorithm name.")},
196 {NULL
} /* Sentinel */
199 static PyGetSetDef EVP_getseters
[] = {
201 (getter
)EVP_get_digest_size
, NULL
,
205 (getter
)EVP_get_block_size
, NULL
,
208 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
209 * the old sha module also supported 'digestsize'. ugh. */
211 (getter
)EVP_get_digest_size
, NULL
,
214 {NULL
} /* Sentinel */
219 EVP_repr(PyObject
*self
)
222 PyOS_snprintf(buf
, sizeof(buf
), "<%s HASH object @ %p>",
223 PyBytes_AsString(((EVPobject
*)self
)->name
), self
);
224 return PyBytes_FromString(buf
);
227 #if HASH_OBJ_CONSTRUCTOR
229 EVP_tp_init(EVPobject
*self
, PyObject
*args
, PyObject
*kwds
)
231 static char *kwlist
[] = {"name", "string", NULL
};
232 PyObject
*name_obj
= NULL
;
234 unsigned char *cp
= NULL
;
236 const EVP_MD
*digest
;
238 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|s#:HASH", kwlist
,
239 &name_obj
, &cp
, &len
)) {
243 if (!PyArg_Parse(name_obj
, "s", &nameStr
)) {
244 PyErr_SetString(PyExc_TypeError
, "name must be a string");
248 digest
= EVP_get_digestbyname(nameStr
);
250 PyErr_SetString(PyExc_ValueError
, "unknown hash function");
253 EVP_DigestInit(&self
->ctx
, digest
);
255 self
->name
= name_obj
;
256 Py_INCREF(self
->name
);
259 EVP_DigestUpdate(&self
->ctx
, cp
, Py_SAFE_DOWNCAST(len
, Py_ssize_t
,
267 PyDoc_STRVAR(hashtype_doc
,
268 "A hash represents the object used to calculate a checksum of a\n\
269 string of information.\n\
273 update() -- updates the current digest with an additional string\n\
274 digest() -- return the current digest value\n\
275 hexdigest() -- return the current digest as a string of hexadecimal digits\n\
276 copy() -- return a copy of the current hash object\n\
280 name -- the hash algorithm being used by this object\n\
281 digest_size -- number of bytes in this hashes output\n");
283 static PyTypeObject EVPtype
= {
284 PyVarObject_HEAD_INIT(NULL
, 0)
285 "_hashlib.HASH", /*tp_name*/
286 sizeof(EVPobject
), /*tp_basicsize*/
289 EVP_dealloc
, /*tp_dealloc*/
294 EVP_repr
, /*tp_repr*/
296 0, /*tp_as_sequence*/
304 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
305 hashtype_doc
, /*tp_doc*/
308 0, /*tp_richcompare*/
309 0, /*tp_weaklistoffset*/
312 EVP_methods
, /* tp_methods */
313 EVP_members
, /* tp_members */
314 EVP_getseters
, /* tp_getset */
318 0, /* tp_descr_get */
319 0, /* tp_descr_set */
320 0, /* tp_dictoffset */
322 #if HASH_OBJ_CONSTRUCTOR
323 (initproc
)EVP_tp_init
, /* tp_init */
328 EVPnew(PyObject
*name_obj
,
329 const EVP_MD
*digest
, const EVP_MD_CTX
*initial_ctx
,
330 const unsigned char *cp
, unsigned int len
)
334 if (!digest
&& !initial_ctx
) {
335 PyErr_SetString(PyExc_ValueError
, "unsupported hash type");
339 if ((self
= newEVPobject(name_obj
)) == NULL
)
343 EVP_MD_CTX_copy(&self
->ctx
, initial_ctx
);
345 EVP_DigestInit(&self
->ctx
, digest
);
349 EVP_DigestUpdate(&self
->ctx
, cp
, len
);
351 return (PyObject
*)self
;
355 /* The module-level function: new() */
357 PyDoc_STRVAR(EVP_new__doc__
,
358 "Return a new hash object using the named algorithm.\n\
359 An optional string argument may be provided and will be\n\
360 automatically hashed.\n\
362 The MD5 and SHA1 algorithms are always supported.\n");
365 EVP_new(PyObject
*self
, PyObject
*args
, PyObject
*kwdict
)
367 static char *kwlist
[] = {"name", "string", NULL
};
368 PyObject
*name_obj
= NULL
;
370 const EVP_MD
*digest
;
371 unsigned char *cp
= NULL
;
374 if (!PyArg_ParseTupleAndKeywords(args
, kwdict
, "O|s#:new", kwlist
,
375 &name_obj
, &cp
, &len
)) {
379 if (!PyArg_Parse(name_obj
, "s", &name
)) {
380 PyErr_SetString(PyExc_TypeError
, "name must be a string");
384 digest
= EVP_get_digestbyname(name
);
386 return EVPnew(name_obj
, digest
, NULL
, cp
, Py_SAFE_DOWNCAST(len
, Py_ssize_t
,
391 * This macro generates constructor function definitions for specific
392 * hash algorithms. These constructors are much faster than calling
393 * the generic one passing it a python string and are noticably
394 * faster than calling a python new() wrapper. Thats important for
395 * code that wants to make hashes of a bunch of small strings.
397 #define GEN_CONSTRUCTOR(NAME) \
399 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
401 unsigned char *cp = NULL; \
402 Py_ssize_t len = 0; \
404 if (!PyArg_ParseTuple(args, "|s#:" #NAME , &cp, &len)) { \
409 CONST_ ## NAME ## _name_obj, \
411 CONST_new_ ## NAME ## _ctx_p, \
412 cp, Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int)); \
415 /* a PyMethodDef structure for the constructor */
416 #define CONSTRUCTOR_METH_DEF(NAME) \
417 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
418 PyDoc_STR("Returns a " #NAME \
419 " hash object; optionally initialized with a string") \
422 /* used in the init function to setup a constructor */
423 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
424 CONST_ ## NAME ## _name_obj = PyBytes_FromString(#NAME); \
425 if (EVP_get_digestbyname(#NAME)) { \
426 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
427 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
432 GEN_CONSTRUCTOR(sha1
)
433 GEN_CONSTRUCTOR(sha224
)
434 GEN_CONSTRUCTOR(sha256
)
435 GEN_CONSTRUCTOR(sha384
)
436 GEN_CONSTRUCTOR(sha512
)
438 /* List of functions exported by this module */
440 static struct PyMethodDef EVP_functions
[] = {
441 {"new", (PyCFunction
)EVP_new
, METH_VARARGS
|METH_KEYWORDS
, EVP_new__doc__
},
442 CONSTRUCTOR_METH_DEF(md5
),
443 CONSTRUCTOR_METH_DEF(sha1
),
444 CONSTRUCTOR_METH_DEF(sha224
),
445 CONSTRUCTOR_METH_DEF(sha256
),
446 CONSTRUCTOR_METH_DEF(sha384
),
447 CONSTRUCTOR_METH_DEF(sha512
),
448 {NULL
, NULL
} /* Sentinel */
452 /* Initialize this module. */
459 OpenSSL_add_all_digests();
461 /* TODO build EVP_functions openssl_* entries dynamically based
462 * on what hashes are supported rather than listing many
463 * but having some be unsupported. Only init appropriate
466 Py_TYPE(&EVPtype
) = &PyType_Type
;
467 if (PyType_Ready(&EVPtype
) < 0)
470 m
= Py_InitModule("_hashlib", EVP_functions
);
474 #if HASH_OBJ_CONSTRUCTOR
476 PyModule_AddObject(m
, "HASH", (PyObject
*)&EVPtype
);
479 /* these constants are used by the convenience constructors */
480 INIT_CONSTRUCTOR_CONSTANTS(md5
);
481 INIT_CONSTRUCTOR_CONSTANTS(sha1
);
482 INIT_CONSTRUCTOR_CONSTANTS(sha224
);
483 INIT_CONSTRUCTOR_CONSTANTS(sha256
);
484 INIT_CONSTRUCTOR_CONSTANTS(sha384
);
485 INIT_CONSTRUCTOR_CONSTANTS(sha512
);