blob: ecbe01c9dbae60341f7cdeb38759e1d1c482d775 [file] [log] [blame]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001/* Module that wraps all OpenSSL hash algorithms */
2
3/*
Gregory P. Smith9406f5c2007-08-26 02:58:36 +00004 * Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00005 * 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
Thomas Wouters9bc844e2006-03-01 21:50:07 +000014#define PY_SSIZE_T_CLEAN
15
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000016#include "Python.h"
17#include "structmember.h"
18
19/* EVP is the preferred interface to hashing in OpenSSL */
20#include <openssl/evp.h>
21
22
Neal Norwitze4ab5f52006-01-08 01:08:09 +000023#ifndef HASH_OBJ_CONSTRUCTOR
24#define HASH_OBJ_CONSTRUCTOR 0
25#endif
26
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000027typedef struct {
28 PyObject_HEAD
29 PyObject *name; /* name of this hash algorithm */
30 EVP_MD_CTX ctx; /* OpenSSL message digest context */
Gregory P. Smith9406f5c2007-08-26 02:58:36 +000031 /*
32 * TODO investigate performance impact of including a lock for this object
33 * here and releasing the Python GIL while hash updates are in progress.
34 * (perhaps only release GIL if input length will take long to process?)
35 */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000036} EVPobject;
37
38
39static PyTypeObject EVPtype;
40
41
42#define DEFINE_CONSTS_FOR_NEW(Name) \
43 static PyObject *CONST_ ## Name ## _name_obj; \
44 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
45 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
46
Neal Norwitzf0459142006-01-07 21:20:24 +000047DEFINE_CONSTS_FOR_NEW(md5)
48DEFINE_CONSTS_FOR_NEW(sha1)
49DEFINE_CONSTS_FOR_NEW(sha224)
50DEFINE_CONSTS_FOR_NEW(sha256)
51DEFINE_CONSTS_FOR_NEW(sha384)
52DEFINE_CONSTS_FOR_NEW(sha512)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000053
54
55static EVPobject *
56newEVPobject(PyObject *name)
57{
58 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
59
60 /* save the name for .name to return */
61 if (retval != NULL) {
62 Py_INCREF(name);
63 retval->name = name;
64 }
65
66 return retval;
67}
68
69/* Internal methods for a hash object */
70
71static void
72EVP_dealloc(PyObject *ptr)
73{
74 EVP_MD_CTX_cleanup(&((EVPobject *)ptr)->ctx);
75 Py_XDECREF(((EVPobject *)ptr)->name);
76 PyObject_Del(ptr);
77}
78
79
80/* External methods for a hash object */
81
82PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
83
84static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000085EVP_copy(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000086{
87 EVPobject *newobj;
88
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000089 if ( (newobj = newEVPobject(self->name))==NULL)
90 return NULL;
91
92 EVP_MD_CTX_copy(&newobj->ctx, &self->ctx);
93 return (PyObject *)newobj;
94}
95
96PyDoc_STRVAR(EVP_digest__doc__,
97"Return the digest value as a string of binary data.");
98
99static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000100EVP_digest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000101{
102 unsigned char digest[EVP_MAX_MD_SIZE];
103 EVP_MD_CTX temp_ctx;
104 PyObject *retval;
105 unsigned int digest_size;
106
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000107 EVP_MD_CTX_copy(&temp_ctx, &self->ctx);
108 digest_size = EVP_MD_CTX_size(&temp_ctx);
Neal Norwitzf0459142006-01-07 21:20:24 +0000109 EVP_DigestFinal(&temp_ctx, digest, NULL);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000110
Christian Heimes72b710a2008-05-26 13:28:38 +0000111 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000112 EVP_MD_CTX_cleanup(&temp_ctx);
113 return retval;
114}
115
116PyDoc_STRVAR(EVP_hexdigest__doc__,
117"Return the digest value as a string of hexadecimal digits.");
118
119static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000120EVP_hexdigest(EVPobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000121{
122 unsigned char digest[EVP_MAX_MD_SIZE];
123 EVP_MD_CTX temp_ctx;
124 PyObject *retval;
125 char *hex_digest;
126 unsigned int i, j, digest_size;
127
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000128 /* Get the raw (binary) digest value */
129 EVP_MD_CTX_copy(&temp_ctx, &self->ctx);
130 digest_size = EVP_MD_CTX_size(&temp_ctx);
131 EVP_DigestFinal(&temp_ctx, digest, NULL);
132
133 EVP_MD_CTX_cleanup(&temp_ctx);
134
Guido van Rossumf8953072007-07-10 13:20:29 +0000135 /* Allocate a new buffer */
136 hex_digest = PyMem_Malloc(digest_size * 2 + 1);
137 if (!hex_digest)
138 return PyErr_NoMemory();
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000139
140 /* Make hex version of the digest */
141 for(i=j=0; i<digest_size; i++) {
142 char c;
143 c = (digest[i] >> 4) & 0xf;
144 c = (c>9) ? c+'a'-10 : c + '0';
145 hex_digest[j++] = c;
146 c = (digest[i] & 0xf);
147 c = (c>9) ? c+'a'-10 : c + '0';
148 hex_digest[j++] = c;
149 }
Guido van Rossumf8953072007-07-10 13:20:29 +0000150 retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2);
151 PyMem_Free(hex_digest);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000152 return retval;
153}
154
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000155#define MY_GET_BUFFER_VIEW_OR_ERROUT(obj, viewp) do { \
Guido van Rossum3227af42007-08-29 14:21:45 +0000156 if (PyUnicode_Check(obj) || !PyObject_CheckBuffer((obj))) { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000157 PyErr_SetString(PyExc_TypeError, \
158 "object supporting the buffer API required"); \
159 return NULL; \
160 } \
Guido van Rossum3227af42007-08-29 14:21:45 +0000161 if (PyObject_GetBuffer((obj), (viewp), PyBUF_SIMPLE) == -1) { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000162 return NULL; \
163 } \
164 if ((viewp)->ndim > 1) { \
165 PyErr_SetString(PyExc_BufferError, \
166 "Buffer must be single dimension"); \
167 PyObject_ReleaseBuffer((obj), (viewp)); \
168 return NULL; \
169 } \
170 } while(0);
171
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000172PyDoc_STRVAR(EVP_update__doc__,
173"Update this hash object's state with the provided string.");
174
175static PyObject *
176EVP_update(EVPobject *self, PyObject *args)
177{
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000178 PyObject *obj;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000179 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000180
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000181 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000182 return NULL;
183
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000184 MY_GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
185
186 EVP_DigestUpdate(&self->ctx, (unsigned char*)view.buf,
187 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
188
189 PyObject_ReleaseBuffer(obj, &view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000190
191 Py_INCREF(Py_None);
192 return Py_None;
193}
194
195static PyMethodDef EVP_methods[] = {
196 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000197 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
198 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
199 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000200 {NULL, NULL} /* sentinel */
201};
202
203static PyObject *
204EVP_get_block_size(EVPobject *self, void *closure)
205{
Christian Heimes217cfd12007-12-02 14:31:20 +0000206 return PyLong_FromLong(EVP_MD_CTX_block_size(&((EVPobject *)self)->ctx));
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000207}
208
209static PyObject *
210EVP_get_digest_size(EVPobject *self, void *closure)
211{
Christian Heimes217cfd12007-12-02 14:31:20 +0000212 return PyLong_FromLong(EVP_MD_CTX_size(&((EVPobject *)self)->ctx));
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000213}
214
215static PyMemberDef EVP_members[] = {
216 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
217 {NULL} /* Sentinel */
218};
219
220static PyGetSetDef EVP_getseters[] = {
221 {"digest_size",
222 (getter)EVP_get_digest_size, NULL,
223 NULL,
224 NULL},
225 {"block_size",
226 (getter)EVP_get_block_size, NULL,
227 NULL,
228 NULL},
229 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
230 * the old sha module also supported 'digestsize'. ugh. */
231 {"digestsize",
232 (getter)EVP_get_digest_size, NULL,
233 NULL,
234 NULL},
235 {NULL} /* Sentinel */
236};
237
238
239static PyObject *
240EVP_repr(PyObject *self)
241{
242 char buf[100];
243 PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
Neal Norwitzd6d2f2f2007-08-23 20:28:10 +0000244 PyUnicode_AsString(((EVPobject *)self)->name), self);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000245 return PyUnicode_FromString(buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000246}
247
248#if HASH_OBJ_CONSTRUCTOR
249static int
250EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
251{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000252 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000253 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000254 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000255 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000256 char *nameStr;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000257 const EVP_MD *digest;
258
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000259 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
260 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000261 return -1;
262 }
263
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000264 if (data_obj)
265 MY_GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
266
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000267 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
268 PyErr_SetString(PyExc_TypeError, "name must be a string");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000269 if (data_obj)
270 PyObject_ReleaseBuffer(data_obj, &view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000271 return -1;
272 }
273
274 digest = EVP_get_digestbyname(nameStr);
275 if (!digest) {
276 PyErr_SetString(PyExc_ValueError, "unknown hash function");
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000277 if (data_obj)
278 PyObject_ReleaseBuffer(data_obj, &view);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000279 return -1;
280 }
281 EVP_DigestInit(&self->ctx, digest);
282
283 self->name = name_obj;
284 Py_INCREF(self->name);
285
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000286 if (data_obj) {
287 EVP_DigestUpdate(&self->ctx, (unsigned char*)view.buf,
288 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
289 PyObject_ReleaseBuffer(data_obj, &view);
290 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000291
292 return 0;
293}
294#endif
295
296
297PyDoc_STRVAR(hashtype_doc,
298"A hash represents the object used to calculate a checksum of a\n\
299string of information.\n\
300\n\
301Methods:\n\
302\n\
303update() -- updates the current digest with an additional string\n\
304digest() -- return the current digest value\n\
305hexdigest() -- return the current digest as a string of hexadecimal digits\n\
306copy() -- return a copy of the current hash object\n\
307\n\
308Attributes:\n\
309\n\
310name -- the hash algorithm being used by this object\n\
311digest_size -- number of bytes in this hashes output\n");
312
313static PyTypeObject EVPtype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000314 PyVarObject_HEAD_INIT(NULL, 0)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000315 "_hashlib.HASH", /*tp_name*/
316 sizeof(EVPobject), /*tp_basicsize*/
317 0, /*tp_itemsize*/
318 /* methods */
319 EVP_dealloc, /*tp_dealloc*/
320 0, /*tp_print*/
321 0, /*tp_getattr*/
322 0, /*tp_setattr*/
323 0, /*tp_compare*/
324 EVP_repr, /*tp_repr*/
325 0, /*tp_as_number*/
326 0, /*tp_as_sequence*/
327 0, /*tp_as_mapping*/
328 0, /*tp_hash*/
329 0, /*tp_call*/
330 0, /*tp_str*/
331 0, /*tp_getattro*/
332 0, /*tp_setattro*/
333 0, /*tp_as_buffer*/
334 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
335 hashtype_doc, /*tp_doc*/
336 0, /*tp_traverse*/
337 0, /*tp_clear*/
338 0, /*tp_richcompare*/
339 0, /*tp_weaklistoffset*/
340 0, /*tp_iter*/
341 0, /*tp_iternext*/
342 EVP_methods, /* tp_methods */
343 EVP_members, /* tp_members */
344 EVP_getseters, /* tp_getset */
345#if 1
346 0, /* tp_base */
347 0, /* tp_dict */
348 0, /* tp_descr_get */
349 0, /* tp_descr_set */
350 0, /* tp_dictoffset */
351#endif
352#if HASH_OBJ_CONSTRUCTOR
353 (initproc)EVP_tp_init, /* tp_init */
354#endif
355};
356
357static PyObject *
358EVPnew(PyObject *name_obj,
359 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
Neal Norwitzf0459142006-01-07 21:20:24 +0000360 const unsigned char *cp, unsigned int len)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000361{
362 EVPobject *self;
363
364 if (!digest && !initial_ctx) {
365 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
366 return NULL;
367 }
368
369 if ((self = newEVPobject(name_obj)) == NULL)
370 return NULL;
371
372 if (initial_ctx) {
373 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
374 } else {
375 EVP_DigestInit(&self->ctx, digest);
376 }
377
378 if (cp && len)
379 EVP_DigestUpdate(&self->ctx, cp, len);
380
381 return (PyObject *)self;
382}
383
384
385/* The module-level function: new() */
386
387PyDoc_STRVAR(EVP_new__doc__,
388"Return a new hash object using the named algorithm.\n\
389An optional string argument may be provided and will be\n\
390automatically hashed.\n\
391\n\
392The MD5 and SHA1 algorithms are always supported.\n");
393
394static PyObject *
395EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
396{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000397 static char *kwlist[] = {"name", "string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000398 PyObject *name_obj = NULL;
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000399 PyObject *data_obj = NULL;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000400 Py_buffer view = { 0 };
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000401 PyObject *ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000402 char *name;
403 const EVP_MD *digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000404
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000405 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
406 &name_obj, &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000407 return NULL;
408 }
409
410 if (!PyArg_Parse(name_obj, "s", &name)) {
411 PyErr_SetString(PyExc_TypeError, "name must be a string");
412 return NULL;
413 }
414
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000415 if (data_obj)
416 MY_GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
417
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000418 digest = EVP_get_digestbyname(name);
419
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000420 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
421 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
422
423 if (data_obj)
424 PyObject_ReleaseBuffer(data_obj, &view);
425 return ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000426}
427
428/*
429 * This macro generates constructor function definitions for specific
430 * hash algorithms. These constructors are much faster than calling
431 * the generic one passing it a python string and are noticably
432 * faster than calling a python new() wrapper. Thats important for
433 * code that wants to make hashes of a bunch of small strings.
434 */
435#define GEN_CONSTRUCTOR(NAME) \
436 static PyObject * \
437 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
438 { \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000439 PyObject *data_obj = NULL; \
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000440 Py_buffer view = { 0 }; \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000441 PyObject *ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000442 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000443 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000444 return NULL; \
445 } \
446 \
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000447 if (data_obj) \
448 MY_GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
449 \
450 ret_obj = EVPnew( \
451 CONST_ ## NAME ## _name_obj, \
452 NULL, \
453 CONST_new_ ## NAME ## _ctx_p, \
454 (unsigned char*)view.buf, \
455 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); \
456 \
457 if (data_obj) \
458 PyObject_ReleaseBuffer(data_obj, &view); \
459 return ret_obj; \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000460 }
461
462/* a PyMethodDef structure for the constructor */
463#define CONSTRUCTOR_METH_DEF(NAME) \
464 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
465 PyDoc_STR("Returns a " #NAME \
466 " hash object; optionally initialized with a string") \
467 }
468
469/* used in the init function to setup a constructor */
470#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
Neal Norwitzd6d2f2f2007-08-23 20:28:10 +0000471 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000472 if (EVP_get_digestbyname(#NAME)) { \
473 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
474 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
475 } \
476} while (0);
477
478GEN_CONSTRUCTOR(md5)
479GEN_CONSTRUCTOR(sha1)
480GEN_CONSTRUCTOR(sha224)
481GEN_CONSTRUCTOR(sha256)
482GEN_CONSTRUCTOR(sha384)
483GEN_CONSTRUCTOR(sha512)
484
485/* List of functions exported by this module */
486
487static struct PyMethodDef EVP_functions[] = {
488 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
489 CONSTRUCTOR_METH_DEF(md5),
490 CONSTRUCTOR_METH_DEF(sha1),
491 CONSTRUCTOR_METH_DEF(sha224),
492 CONSTRUCTOR_METH_DEF(sha256),
493 CONSTRUCTOR_METH_DEF(sha384),
494 CONSTRUCTOR_METH_DEF(sha512),
495 {NULL, NULL} /* Sentinel */
496};
497
498
499/* Initialize this module. */
500
501PyMODINIT_FUNC
502init_hashlib(void)
503{
504 PyObject *m;
505
506 OpenSSL_add_all_digests();
507
508 /* TODO build EVP_functions openssl_* entries dynamically based
509 * on what hashes are supported rather than listing many
510 * but having some be unsupported. Only init appropriate
511 * constants. */
512
Christian Heimes90aa7642007-12-19 02:45:37 +0000513 Py_TYPE(&EVPtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000514 if (PyType_Ready(&EVPtype) < 0)
515 return;
516
517 m = Py_InitModule("_hashlib", EVP_functions);
518 if (m == NULL)
519 return;
520
521#if HASH_OBJ_CONSTRUCTOR
522 Py_INCREF(&EVPtype);
523 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
524#endif
525
526 /* these constants are used by the convenience constructors */
527 INIT_CONSTRUCTOR_CONSTANTS(md5);
528 INIT_CONSTRUCTOR_CONSTANTS(sha1);
529 INIT_CONSTRUCTOR_CONSTANTS(sha224);
530 INIT_CONSTRUCTOR_CONSTANTS(sha256);
531 INIT_CONSTRUCTOR_CONSTANTS(sha384);
532 INIT_CONSTRUCTOR_CONSTANTS(sha512);
533}