No issues found
1 /* Module that wraps all OpenSSL hash algorithms */
2
3 /*
4 * Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
5 * 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
14 #define PY_SSIZE_T_CLEAN
15
16 #include "Python.h"
17 #include "structmember.h"
18
19 #ifdef WITH_THREAD
20 #include "pythread.h"
21 #define ENTER_HASHLIB(obj) \
22 if ((obj)->lock) { \
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 \
27 } \
28 }
29 #define LEAVE_HASHLIB(obj) \
30 if ((obj)->lock) { \
31 PyThread_release_lock((obj)->lock); \
32 }
33 #else
34 #define ENTER_HASHLIB(obj)
35 #define LEAVE_HASHLIB(obj)
36 #endif
37
38 /* EVP is the preferred interface to hashing in OpenSSL */
39 #include <openssl/ssl.h>
40 #include <openssl/err.h>
41 #include <openssl/evp.h>
42
43 #define MUNCH_SIZE INT_MAX
44
45 /* TODO(gps): We should probably make this a module or EVPobject attribute
46 * to allow the user to optimize based on the platform they're using. */
47 #define HASHLIB_GIL_MINSIZE 2048
48
49 #ifndef HASH_OBJ_CONSTRUCTOR
50 #define HASH_OBJ_CONSTRUCTOR 0
51 #endif
52
53 /* Minimum OpenSSL version needed to support sha224 and higher. */
54 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x00908000)
55 #define _OPENSSL_SUPPORTS_SHA2
56 #endif
57
58 typedef struct {
59 PyObject_HEAD
60 PyObject *name; /* name of this hash algorithm */
61 EVP_MD_CTX ctx; /* OpenSSL message digest context */
62 #ifdef WITH_THREAD
63 PyThread_type_lock lock; /* OpenSSL context lock */
64 #endif
65 } EVPobject;
66
67
68 static PyTypeObject EVPtype;
69
70 /* Struct to hold all the cached information we need on a specific algorithm.
71 We have one of these per algorithm */
72 typedef struct {
73 PyObject *name_obj;
74 EVP_MD_CTX ctxs[2];
75 /* ctx_ptrs will point to ctxs unless an error occurred, when it will
76 be NULL: */
77 EVP_MD_CTX *ctx_ptrs[2];
78 PyObject *error_msgs[2];
79 } EVPCachedInfo;
80
81 #define DEFINE_CONSTS_FOR_NEW(Name) \
82 static EVPCachedInfo cached_info_ ##Name;
83
84 DEFINE_CONSTS_FOR_NEW(md5)
85 DEFINE_CONSTS_FOR_NEW(sha1)
86 #ifdef _OPENSSL_SUPPORTS_SHA2
87 DEFINE_CONSTS_FOR_NEW(sha224)
88 DEFINE_CONSTS_FOR_NEW(sha256)
89 DEFINE_CONSTS_FOR_NEW(sha384)
90 DEFINE_CONSTS_FOR_NEW(sha512)
91 #endif
92
93
94 static EVPobject *
95 newEVPobject(PyObject *name)
96 {
97 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
98
99 /* save the name for .name to return */
100 if (retval != NULL) {
101 Py_INCREF(name);
102 retval->name = name;
103 #ifdef WITH_THREAD
104 retval->lock = NULL;
105 #endif
106 }
107
108 return retval;
109 }
110
111 static void
112 EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
113 {
114 unsigned int process;
115 const unsigned char *cp = (const unsigned char *)vp;
116 while (0 < len)
117 {
118 if (len > (Py_ssize_t)MUNCH_SIZE)
119 process = MUNCH_SIZE;
120 else
121 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
122 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
123 len -= process;
124 cp += process;
125 }
126 }
127
128 static void
129 mc_ctx_init(EVP_MD_CTX *ctx, int usedforsecurity)
130 {
131 EVP_MD_CTX_init(ctx);
132
133 /*
134 If the user has declared that this digest is being used in a
135 non-security role (e.g. indexing into a data structure), set
136 the exception flag for openssl to allow it
137 */
138 if (!usedforsecurity) {
139 #ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
140 EVP_MD_CTX_set_flags(ctx,
141 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
142 #endif
143 }
144 }
145
146 /* Get an error msg for the last error as a PyObject */
147 static PyObject *
148 error_msg_for_last_error(void)
149 {
150 char *errstr;
151
152 errstr = ERR_error_string(ERR_peek_last_error(), NULL);
153 ERR_clear_error();
154
155 return PyString_FromString(errstr); /* Can be NULL */
156 }
157
158 static void
159 set_evp_exception(void)
160 {
161 char *errstr;
162
163 errstr = ERR_error_string(ERR_peek_last_error(), NULL);
164 ERR_clear_error();
165
166 PyErr_SetString(PyExc_ValueError, errstr);
167 }
168
169
170 /* Internal methods for a hash object */
171
172 static void
173 EVP_dealloc(EVPobject *self)
174 {
175 #ifdef WITH_THREAD
176 if (self->lock != NULL)
177 PyThread_free_lock(self->lock);
178 #endif
179 EVP_MD_CTX_cleanup(&self->ctx);
180 Py_XDECREF(self->name);
181 PyObject_Del(self);
182 }
183
184 static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
185 {
186 ENTER_HASHLIB(self);
187 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
188 LEAVE_HASHLIB(self);
189 }
190
191 /* External methods for a hash object */
192
193 PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
194
195
196 static PyObject *
197 EVP_copy(EVPobject *self, PyObject *unused)
198 {
199 EVPobject *newobj;
200
201 if ( (newobj = newEVPobject(self->name))==NULL)
202 return NULL;
203
204 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
205 return (PyObject *)newobj;
206 }
207
208 PyDoc_STRVAR(EVP_digest__doc__,
209 "Return the digest value as a string of binary data.");
210
211 static PyObject *
212 EVP_digest(EVPobject *self, PyObject *unused)
213 {
214 unsigned char digest[EVP_MAX_MD_SIZE];
215 EVP_MD_CTX temp_ctx;
216 PyObject *retval;
217 unsigned int digest_size;
218
219 locked_EVP_MD_CTX_copy(&temp_ctx, self);
220 digest_size = EVP_MD_CTX_size(&temp_ctx);
221 EVP_DigestFinal(&temp_ctx, digest, NULL);
222
223 retval = PyString_FromStringAndSize((const char *)digest, digest_size);
224 EVP_MD_CTX_cleanup(&temp_ctx);
225 return retval;
226 }
227
228 PyDoc_STRVAR(EVP_hexdigest__doc__,
229 "Return the digest value as a string of hexadecimal digits.");
230
231 static PyObject *
232 EVP_hexdigest(EVPobject *self, PyObject *unused)
233 {
234 unsigned char digest[EVP_MAX_MD_SIZE];
235 EVP_MD_CTX temp_ctx;
236 PyObject *retval;
237 char *hex_digest;
238 unsigned int i, j, digest_size;
239
240 /* Get the raw (binary) digest value */
241 locked_EVP_MD_CTX_copy(&temp_ctx, self);
242 digest_size = EVP_MD_CTX_size(&temp_ctx);
243 EVP_DigestFinal(&temp_ctx, digest, NULL);
244
245 EVP_MD_CTX_cleanup(&temp_ctx);
246
247 /* Create a new string */
248 /* NOTE: not thread safe! modifying an already created string object */
249 /* (not a problem because we hold the GIL by default) */
250 retval = PyString_FromStringAndSize(NULL, digest_size * 2);
251 if (!retval)
252 return NULL;
253 hex_digest = PyString_AsString(retval);
254 if (!hex_digest) {
255 Py_DECREF(retval);
256 return NULL;
257 }
258
259 /* Make hex version of the digest */
260 for(i=j=0; i<digest_size; i++) {
261 char c;
262 c = (digest[i] >> 4) & 0xf;
263 c = (c>9) ? c+'a'-10 : c + '0';
264 hex_digest[j++] = c;
265 c = (digest[i] & 0xf);
266 c = (c>9) ? c+'a'-10 : c + '0';
267 hex_digest[j++] = c;
268 }
269 return retval;
270 }
271
272 PyDoc_STRVAR(EVP_update__doc__,
273 "Update this hash object's state with the provided string.");
274
275 static PyObject *
276 EVP_update(EVPobject *self, PyObject *args)
277 {
278 Py_buffer view;
279
280 if (!PyArg_ParseTuple(args, "s*:update", &view))
281 return NULL;
282
283 #ifdef WITH_THREAD
284 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
285 self->lock = PyThread_allocate_lock();
286 /* fail? lock = NULL and we fail over to non-threaded code. */
287 }
288
289 if (self->lock != NULL) {
290 Py_BEGIN_ALLOW_THREADS
291 PyThread_acquire_lock(self->lock, 1);
292 EVP_hash(self, view.buf, view.len);
293 PyThread_release_lock(self->lock);
294 Py_END_ALLOW_THREADS
295 }
296 else
297 #endif
298 {
299 EVP_hash(self, view.buf, view.len);
300 }
301
302 PyBuffer_Release(&view);
303
304 Py_RETURN_NONE;
305 }
306
307 static PyMethodDef EVP_methods[] = {
308 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
309 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
310 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
311 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
312 {NULL, NULL} /* sentinel */
313 };
314
315 static PyObject *
316 EVP_get_block_size(EVPobject *self, void *closure)
317 {
318 long block_size;
319 block_size = EVP_MD_CTX_block_size(&self->ctx);
320 return PyLong_FromLong(block_size);
321 }
322
323 static PyObject *
324 EVP_get_digest_size(EVPobject *self, void *closure)
325 {
326 long size;
327 size = EVP_MD_CTX_size(&self->ctx);
328 return PyLong_FromLong(size);
329 }
330
331 static PyMemberDef EVP_members[] = {
332 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
333 {NULL} /* Sentinel */
334 };
335
336 static PyGetSetDef EVP_getseters[] = {
337 {"digest_size",
338 (getter)EVP_get_digest_size, NULL,
339 NULL,
340 NULL},
341 {"block_size",
342 (getter)EVP_get_block_size, NULL,
343 NULL,
344 NULL},
345 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
346 * the old sha module also supported 'digestsize'. ugh. */
347 {"digestsize",
348 (getter)EVP_get_digest_size, NULL,
349 NULL,
350 NULL},
351 {NULL} /* Sentinel */
352 };
353
354
355 static PyObject *
356 EVP_repr(PyObject *self)
357 {
358 char buf[100];
359 PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
360 PyString_AsString(((EVPobject *)self)->name), self);
361 return PyString_FromString(buf);
362 }
363
364 #if HASH_OBJ_CONSTRUCTOR
365 static int
366 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
367 {
368 static char *kwlist[] = {"name", "string", "usedforsecurity", NULL};
369 PyObject *name_obj = NULL;
370 int usedforsecurity = 1;
371 Py_buffer view = { 0 };
372 char *nameStr;
373 const EVP_MD *digest;
374
375 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s*i:HASH", kwlist,
376 &name_obj, &view, &usedforsecurity)) {
377 return -1;
378 }
379
380 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
381 PyErr_SetString(PyExc_TypeError, "name must be a string");
382 PyBuffer_Release(&view);
383 return -1;
384 }
385
386 digest = EVP_get_digestbyname(nameStr);
387 if (!digest) {
388 PyErr_SetString(PyExc_ValueError, "unknown hash function");
389 PyBuffer_Release(&view);
390 return -1;
391 }
392 mc_ctx_init(&self->ctx, usedforsecurity);
393 if (!EVP_DigestInit_ex(&self->ctx, digest, NULL)) {
394 set_evp_exception();
395 PyBuffer_Release(&view);
396 return -1;
397 }
398
399 self->name = name_obj;
400 Py_INCREF(self->name);
401
402 if (view.obj) {
403 if (view.len >= HASHLIB_GIL_MINSIZE) {
404 Py_BEGIN_ALLOW_THREADS
405 EVP_hash(self, view.buf, view.len);
406 Py_END_ALLOW_THREADS
407 } else {
408 EVP_hash(self, view.buf, view.len);
409 }
410 PyBuffer_Release(&view);
411 }
412
413 return 0;
414 }
415 #endif
416
417
418 PyDoc_STRVAR(hashtype_doc,
419 "A hash represents the object used to calculate a checksum of a\n\
420 string of information.\n\
421 \n\
422 Methods:\n\
423 \n\
424 update() -- updates the current digest with an additional string\n\
425 digest() -- return the current digest value\n\
426 hexdigest() -- return the current digest as a string of hexadecimal digits\n\
427 copy() -- return a copy of the current hash object\n\
428 \n\
429 Attributes:\n\
430 \n\
431 name -- the hash algorithm being used by this object\n\
432 digest_size -- number of bytes in this hashes output\n");
433
434 static PyTypeObject EVPtype = {
435 PyVarObject_HEAD_INIT(NULL, 0)
436 "_hashlib.HASH", /*tp_name*/
437 sizeof(EVPobject), /*tp_basicsize*/
438 0, /*tp_itemsize*/
439 /* methods */
440 (destructor)EVP_dealloc, /*tp_dealloc*/
441 0, /*tp_print*/
442 0, /*tp_getattr*/
443 0, /*tp_setattr*/
444 0, /*tp_compare*/
445 EVP_repr, /*tp_repr*/
446 0, /*tp_as_number*/
447 0, /*tp_as_sequence*/
448 0, /*tp_as_mapping*/
449 0, /*tp_hash*/
450 0, /*tp_call*/
451 0, /*tp_str*/
452 0, /*tp_getattro*/
453 0, /*tp_setattro*/
454 0, /*tp_as_buffer*/
455 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
456 hashtype_doc, /*tp_doc*/
457 0, /*tp_traverse*/
458 0, /*tp_clear*/
459 0, /*tp_richcompare*/
460 0, /*tp_weaklistoffset*/
461 0, /*tp_iter*/
462 0, /*tp_iternext*/
463 EVP_methods, /* tp_methods */
464 EVP_members, /* tp_members */
465 EVP_getseters, /* tp_getset */
466 #if 1
467 0, /* tp_base */
468 0, /* tp_dict */
469 0, /* tp_descr_get */
470 0, /* tp_descr_set */
471 0, /* tp_dictoffset */
472 #endif
473 #if HASH_OBJ_CONSTRUCTOR
474 (initproc)EVP_tp_init, /* tp_init */
475 #endif
476 };
477
478 static PyObject *
479 EVPnew(PyObject *name_obj,
480 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
481 const unsigned char *cp, Py_ssize_t len,
482 int usedforsecurity)
483 {
484 EVPobject *self;
485
486 if (!digest && !initial_ctx) {
487 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
488 return NULL;
489 }
490
491 if ((self = newEVPobject(name_obj)) == NULL)
492 return NULL;
493
494 if (initial_ctx) {
495 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
496 } else {
497 mc_ctx_init(&self->ctx, usedforsecurity);
498 if (!EVP_DigestInit_ex(&self->ctx, digest, NULL)) {
499 set_evp_exception();
500 Py_DECREF(self);
501 return NULL;
502 }
503 }
504
505 if (cp && len) {
506 if (len >= HASHLIB_GIL_MINSIZE) {
507 Py_BEGIN_ALLOW_THREADS
508 EVP_hash(self, cp, len);
509 Py_END_ALLOW_THREADS
510 } else {
511 EVP_hash(self, cp, len);
512 }
513 }
514
515 return (PyObject *)self;
516 }
517
518
519 /* The module-level function: new() */
520
521 PyDoc_STRVAR(EVP_new__doc__,
522 "Return a new hash object using the named algorithm.\n\
523 An optional string argument may be provided and will be\n\
524 automatically hashed.\n\
525 \n\
526 The MD5 and SHA1 algorithms are always supported.\n\
527 \n\
528 An optional \"usedforsecurity=True\" keyword argument is provided for use in\n\
529 environments that enforce FIPS-based restrictions. Some implementations of\n\
530 OpenSSL can be configured to prevent the usage of non-secure algorithms (such\n\
531 as MD5). If you have a non-security use for these algorithms (e.g. a hash\n\
532 table), you can override this argument by marking the callsite as\n\
533 \"usedforsecurity=False\".");
534
535 static PyObject *
536 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
537 {
538 static char *kwlist[] = {"name", "string", "usedforsecurity", NULL};
539 PyObject *name_obj = NULL;
540 Py_buffer view = { 0 };
541 PyObject *ret_obj;
542 char *name;
543 const EVP_MD *digest;
544 int usedforsecurity = 1;
545
546 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|s*i:new", kwlist,
547 &name_obj, &view, &usedforsecurity)) {
548 return NULL;
549 }
550
551 if (!PyArg_Parse(name_obj, "s", &name)) {
552 PyBuffer_Release(&view);
553 PyErr_SetString(PyExc_TypeError, "name must be a string");
554 return NULL;
555 }
556
557 digest = EVP_get_digestbyname(name);
558
559 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
560 view.len, usedforsecurity);
561 PyBuffer_Release(&view);
562
563 return ret_obj;
564 }
565
566 /*
567 * This macro and function generates a family of constructor function
568 * definitions for specific hash algorithms. These constructors are much
569 * faster than calling the generic one passing it a python string and are
570 * noticably faster than calling a python new() wrapper. That's important for
571 * code that wants to make hashes of a bunch of small strings.
572 */
573 #define GEN_CONSTRUCTOR(NAME) \
574 static PyObject * \
575 EVP_new_ ## NAME (PyObject *self, PyObject *args, PyObject *kwdict) \
576 { \
577 return implement_specific_EVP_new(self, args, kwdict, \
578 "|s*i:" #NAME, \
579 &cached_info_ ## NAME ); \
580 }
581
582 static PyObject *
583 implement_specific_EVP_new(PyObject *self, PyObject *args, PyObject *kwdict,
584 const char *format,
585 EVPCachedInfo *cached_info)
586 {
587 static char *kwlist[] = {"string", "usedforsecurity", NULL};
588 Py_buffer view = { 0 };
589 int usedforsecurity = 1;
590 int idx;
591 PyObject *ret_obj = NULL;
592
593 assert(cached_info);
594
595 if (!PyArg_ParseTupleAndKeywords(args, kwdict, format, kwlist,
596 &view, &usedforsecurity)) {
597 return NULL;
598 }
599
600 idx = usedforsecurity ? 1 : 0;
601
602 /*
603 * If an error occurred during creation of the global content, the ctx_ptr
604 * will be NULL, and the error_msg will hopefully be non-NULL:
605 */
606 if (cached_info->ctx_ptrs[idx]) {
607 /* We successfully initialized this context; copy it: */
608 ret_obj = EVPnew(cached_info->name_obj,
609 NULL,
610 cached_info->ctx_ptrs[idx],
611 (unsigned char*)view.buf, view.len,
612 usedforsecurity);
613 } else {
614 /* Some kind of error happened initializing the global context for
615 this (digest, usedforsecurity) pair.
616 Raise an exception with the saved error message: */
617 if (cached_info->error_msgs[idx]) {
618 PyErr_SetObject(PyExc_ValueError, cached_info->error_msgs[idx]);
619 } else {
620 PyErr_SetString(PyExc_ValueError, "Error initializing hash");
621 }
622 }
623
624 PyBuffer_Release(&view);
625
626 return ret_obj;
627 }
628
629 /* a PyMethodDef structure for the constructor */
630 #define CONSTRUCTOR_METH_DEF(NAME) \
631 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, \
632 METH_VARARGS |METH_KEYWORDS, \
633 PyDoc_STR("Returns a " #NAME \
634 " hash object; optionally initialized with a string") \
635 }
636
637 /*
638 Macro/function pair to set up the constructors.
639
640 Try to initialize a context for each hash twice, once with
641 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW and once without.
642
643 Any that have errors during initialization will end up wit a NULL ctx_ptrs
644 entry, and err_msgs will be set (unless we're very low on memory)
645 */
646 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
647 init_constructor_constant(&cached_info_ ## NAME, #NAME); \
648 } while (0);
649
650 static void
651 init_constructor_constant(EVPCachedInfo *cached_info, const char *name)
652 {
653 assert(cached_info);
654 cached_info->name_obj = PyString_FromString(name);
655 if (EVP_get_digestbyname(name)) {
656 int i;
657 for (i=0; i<2; i++) {
658 mc_ctx_init(&cached_info->ctxs[i], i);
659 if (EVP_DigestInit_ex(&cached_info->ctxs[i],
660 EVP_get_digestbyname(name), NULL)) {
661 /* Success: */
662 cached_info->ctx_ptrs[i] = &cached_info->ctxs[i];
663 } else {
664 /* Failure: */
665 cached_info->ctx_ptrs[i] = NULL;
666 cached_info->error_msgs[i] = error_msg_for_last_error();
667 }
668 }
669 }
670 }
671
672 GEN_CONSTRUCTOR(md5)
673 GEN_CONSTRUCTOR(sha1)
674 #ifdef _OPENSSL_SUPPORTS_SHA2
675 GEN_CONSTRUCTOR(sha224)
676 GEN_CONSTRUCTOR(sha256)
677 GEN_CONSTRUCTOR(sha384)
678 GEN_CONSTRUCTOR(sha512)
679 #endif
680
681 /* List of functions exported by this module */
682
683 static struct PyMethodDef EVP_functions[] = {
684 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
685 CONSTRUCTOR_METH_DEF(md5),
686 CONSTRUCTOR_METH_DEF(sha1),
687 #ifdef _OPENSSL_SUPPORTS_SHA2
688 CONSTRUCTOR_METH_DEF(sha224),
689 CONSTRUCTOR_METH_DEF(sha256),
690 CONSTRUCTOR_METH_DEF(sha384),
691 CONSTRUCTOR_METH_DEF(sha512),
692 #endif
693 {NULL, NULL} /* Sentinel */
694 };
695
696
697 /* Initialize this module. */
698
699 PyMODINIT_FUNC
700 init_hashlib(void)
701 {
702 PyObject *m;
703
704 SSL_load_error_strings();
705 SSL_library_init();
706 OpenSSL_add_all_digests();
707
708 Py_TYPE(&EVPtype) = &PyType_Type;
709 if (PyType_Ready(&EVPtype) < 0)
710 return;
711
712 m = Py_InitModule("_hashlib", EVP_functions);
713 if (m == NULL)
714 return;
715
716 #if HASH_OBJ_CONSTRUCTOR
717 Py_INCREF(&EVPtype);
718 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
719 #endif
720
721 /* these constants are used by the convenience constructors */
722 INIT_CONSTRUCTOR_CONSTANTS(md5);
723 INIT_CONSTRUCTOR_CONSTANTS(sha1);
724 #ifdef _OPENSSL_SUPPORTS_SHA2
725 INIT_CONSTRUCTOR_CONSTANTS(sha224);
726 INIT_CONSTRUCTOR_CONSTANTS(sha256);
727 INIT_CONSTRUCTOR_CONSTANTS(sha384);
728 INIT_CONSTRUCTOR_CONSTANTS(sha512);
729 #endif
730 }