| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | /* MD5 module */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* This module provides an interface to the MD5 algorithm */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* See below for information about the original code this module was
 | 
					
						
							|  |  |  |    based upon. Additional work performed by: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Andrew Kuchling (amk@amk.ca) | 
					
						
							|  |  |  |    Greg Stein (gstein@lyra.org) | 
					
						
							|  |  |  |    Trevor Perrin (trevp@trevp.net) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Copyright (C) 2005-2007   Gregory P. Smith (greg@krypto.org) | 
					
						
							|  |  |  |    Licensed to PSF under a Contributor Agreement. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* MD5 objects */ | 
					
						
							| 
									
										
										
										
											2023-10-17 12:57:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  | #ifndef Py_BUILD_CORE_BUILTIN
 | 
					
						
							|  |  |  | #  define Py_BUILD_CORE_MODULE 1
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											2009-02-12 07:35:29 +00:00
										 |  |  | #include "hashlib.h"
 | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | module _md5 | 
					
						
							|  |  |  | class MD5Type "MD5object *" "&PyType_Type" | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/ | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Some useful types */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if SIZEOF_INT == 4
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | typedef unsigned int MD5_INT32; /* 32-bit integer */ | 
					
						
							| 
									
										
										
										
											2016-09-06 10:46:49 -07:00
										 |  |  | typedef long long MD5_INT64; /* 64-bit integer */ | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | /* not defined. compilation will die. */ | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* The MD5 block size and message digest sizes, in bytes */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define MD5_BLOCKSIZE    64
 | 
					
						
							|  |  |  | #define MD5_DIGESTSIZE   16
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 13:18:43 -08:00
										 |  |  | #include "_hacl/Hacl_Hash_MD5.h"
 | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     PyObject_HEAD | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     // Prevents undefined behavior via multiple threads entering the C API.
 | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  |     bool use_mutex; | 
					
						
							|  |  |  |     PyMutex mutex; | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     Hacl_Hash_MD5_state_t *hash_state; | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | } MD5object; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-03 23:53:51 +03:00
										 |  |  | #include "clinic/md5module.c.h"
 | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     PyTypeObject* md5_type; | 
					
						
							|  |  |  | } MD5State; | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  | static inline MD5State* | 
					
						
							|  |  |  | md5_get_state(PyObject *module) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     void *state = PyModule_GetState(module); | 
					
						
							|  |  |  |     assert(state != NULL); | 
					
						
							|  |  |  |     return (MD5State *)state; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static MD5object * | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  | newMD5object(MD5State * st) | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 09:48:19 +02:00
										 |  |  |     MD5object *md5 = (MD5object *)PyObject_GC_New(MD5object, st->md5_type); | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  |     if (!md5) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     HASHLIB_INIT_MUTEX(md5); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-27 09:48:19 +02:00
										 |  |  |     PyObject_GC_Track(md5); | 
					
						
							|  |  |  |     return md5; | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Internal methods for a hash object */ | 
					
						
							| 
									
										
										
										
											2021-05-27 09:48:19 +02:00
										 |  |  | static int | 
					
						
							|  |  |  | MD5_traverse(PyObject *ptr, visitproc visit, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_VISIT(Py_TYPE(ptr)); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2023-02-22 13:18:43 -08:00
										 |  |  | MD5_dealloc(MD5object *ptr) | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     Hacl_Hash_MD5_free(ptr->hash_state); | 
					
						
							| 
									
										
										
										
											2023-10-17 12:57:41 +02:00
										 |  |  |     PyTypeObject *tp = Py_TYPE((PyObject*)ptr); | 
					
						
							| 
									
										
										
										
											2021-05-27 09:48:19 +02:00
										 |  |  |     PyObject_GC_UnTrack(ptr); | 
					
						
							|  |  |  |     PyObject_GC_Del(ptr); | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  |     Py_DECREF(tp); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* External methods for a hash object */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | MD5Type.copy | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  |     cls: defining_class | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  | Return a copy of the hash object. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  | MD5Type_copy_impl(MD5object *self, PyTypeObject *cls) | 
					
						
							|  |  |  | /*[clinic end generated code: output=bf055e08244bf5ee input=d89087dcfb2a8620]*/ | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-10-17 12:57:41 +02:00
										 |  |  |     MD5State *st = PyType_GetModuleState(cls); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  |     MD5object *newobj; | 
					
						
							|  |  |  |     if ((newobj = newMD5object(st))==NULL) | 
					
						
							| 
									
										
										
										
											2015-04-16 17:29:11 +02:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     ENTER_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     newobj->hash_state = Hacl_Hash_MD5_copy(self->hash_state); | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     LEAVE_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  |     return (PyObject *)newobj; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | MD5Type.digest | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 23:12:53 +05:30
										 |  |  | Return the digest value as a bytes object. | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | MD5Type_digest_impl(MD5object *self) | 
					
						
							| 
									
										
										
										
											2018-10-19 23:12:53 +05:30
										 |  |  | /*[clinic end generated code: output=eb691dc4190a07ec input=bc0c4397c2994be6]*/ | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     unsigned char digest[MD5_DIGESTSIZE]; | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     ENTER_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     Hacl_Hash_MD5_digest(self->hash_state, digest); | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     LEAVE_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2008-05-26 13:28:38 +00:00
										 |  |  |     return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | MD5Type.hexdigest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return the digest value as a string of hexadecimal digits. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | MD5Type_hexdigest_impl(MD5object *self) | 
					
						
							| 
									
										
										
										
											2015-04-03 23:53:51 +03:00
										 |  |  | /*[clinic end generated code: output=17badced1f3ac932 input=b60b19de644798dd]*/ | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     unsigned char digest[MD5_DIGESTSIZE]; | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     ENTER_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     Hacl_Hash_MD5_digest(self->hash_state, digest); | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     LEAVE_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2023-10-17 12:57:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const char *hexdigits = "0123456789abcdef"; | 
					
						
							|  |  |  |     char digest_hex[MD5_DIGESTSIZE * 2]; | 
					
						
							|  |  |  |     char *str = digest_hex; | 
					
						
							|  |  |  |     for (size_t i=0; i < MD5_DIGESTSIZE; i++) { | 
					
						
							|  |  |  |         unsigned char byte = digest[i]; | 
					
						
							|  |  |  |         *str++ = hexdigits[byte >> 4]; | 
					
						
							|  |  |  |         *str++ = hexdigits[byte & 0x0f]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return PyUnicode_FromStringAndSize(digest_hex, sizeof(digest_hex)); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  | static void update(Hacl_Hash_MD5_state_t *state, uint8_t *buf, Py_ssize_t len) { | 
					
						
							| 
									
										
										
										
											2023-02-22 13:18:43 -08:00
										 |  |  | #if PY_SSIZE_T_MAX > UINT32_MAX
 | 
					
						
							|  |  |  |   while (len > UINT32_MAX) { | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     Hacl_Hash_MD5_update(state, buf, UINT32_MAX); | 
					
						
							| 
									
										
										
										
											2023-02-22 13:18:43 -08:00
										 |  |  |     len -= UINT32_MAX; | 
					
						
							|  |  |  |     buf += UINT32_MAX; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |   Hacl_Hash_MD5_update(state, buf, (uint32_t) len); | 
					
						
							| 
									
										
										
										
											2023-02-22 13:18:43 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | MD5Type.update | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     obj: object | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Update this hash object's state with the provided string. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  | MD5Type_update(MD5object *self, PyObject *obj) | 
					
						
							| 
									
										
										
										
											2015-04-03 23:53:51 +03:00
										 |  |  | /*[clinic end generated code: output=f6ad168416338423 input=6e1efcd9ecf17032]*/ | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-08-14 15:52:23 +00:00
										 |  |  |     Py_buffer buf; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-12 07:35:29 +00:00
										 |  |  |     GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  |     if (!self->use_mutex && buf.len >= HASHLIB_GIL_MINSIZE) { | 
					
						
							|  |  |  |         self->use_mutex = true; | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  |     if (self->use_mutex) { | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |         Py_BEGIN_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  |         PyMutex_Lock(&self->mutex); | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |         update(self->hash_state, buf.buf, buf.len); | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  |         PyMutex_Unlock(&self->mutex); | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |         Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         update(self->hash_state, buf.buf, buf.len); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-14 15:52:23 +00:00
										 |  |  |     PyBuffer_Release(&buf); | 
					
						
							| 
									
										
										
										
											2017-01-23 09:47:21 +02:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef MD5_methods[] = { | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  |     MD5TYPE_COPY_METHODDEF | 
					
						
							|  |  |  |     MD5TYPE_DIGEST_METHODDEF | 
					
						
							|  |  |  |     MD5TYPE_HEXDIGEST_METHODDEF | 
					
						
							|  |  |  |     MD5TYPE_UPDATE_METHODDEF | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     {NULL,        NULL}         /* sentinel */ | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | MD5_get_block_size(PyObject *self, void *closure) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-12-02 14:31:20 +00:00
										 |  |  |     return PyLong_FromLong(MD5_BLOCKSIZE); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | MD5_get_name(PyObject *self, void *closure) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-15 18:31:48 +02:00
										 |  |  |     return PyUnicode_FromStringAndSize("md5", 3); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | md5_get_digest_size(PyObject *self, void *closure) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-12-02 14:31:20 +00:00
										 |  |  |     return PyLong_FromLong(MD5_DIGESTSIZE); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyGetSetDef MD5_getseters[] = { | 
					
						
							|  |  |  |     {"block_size", | 
					
						
							|  |  |  |      (getter)MD5_get_block_size, NULL, | 
					
						
							|  |  |  |      NULL, | 
					
						
							|  |  |  |      NULL}, | 
					
						
							|  |  |  |     {"name", | 
					
						
							|  |  |  |      (getter)MD5_get_name, NULL, | 
					
						
							|  |  |  |      NULL, | 
					
						
							|  |  |  |      NULL}, | 
					
						
							|  |  |  |     {"digest_size", | 
					
						
							|  |  |  |      (getter)md5_get_digest_size, NULL, | 
					
						
							|  |  |  |      NULL, | 
					
						
							|  |  |  |      NULL}, | 
					
						
							|  |  |  |     {NULL}  /* Sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  | static PyType_Slot md5_type_slots[] = { | 
					
						
							|  |  |  |     {Py_tp_dealloc, MD5_dealloc}, | 
					
						
							|  |  |  |     {Py_tp_methods, MD5_methods}, | 
					
						
							|  |  |  |     {Py_tp_getset, MD5_getseters}, | 
					
						
							| 
									
										
										
										
											2021-05-27 09:48:19 +02:00
										 |  |  |     {Py_tp_traverse, MD5_traverse}, | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  |     {0,0} | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  | static PyType_Spec md5_type_spec = { | 
					
						
							|  |  |  |     .name = "_md5.md5", | 
					
						
							|  |  |  |     .basicsize =  sizeof(MD5object), | 
					
						
							| 
									
										
										
										
											2021-05-27 09:48:19 +02:00
										 |  |  |     .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | | 
					
						
							|  |  |  |               Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC), | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  |     .slots = md5_type_slots | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* The single module-level function: new() */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _md5.md5 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     string: object(c_default="NULL") = b'' | 
					
						
							| 
									
										
										
										
											2019-09-13 02:30:00 +02:00
										 |  |  |     * | 
					
						
							|  |  |  |     usedforsecurity: bool = True | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | Return a new MD5 hash object; optionally initialized with a string. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2019-09-13 02:30:00 +02:00
										 |  |  | _md5_md5_impl(PyObject *module, PyObject *string, int usedforsecurity) | 
					
						
							|  |  |  | /*[clinic end generated code: output=587071f76254a4ac input=7a144a1905636985]*/ | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     MD5object *new; | 
					
						
							| 
									
										
										
										
											2008-08-14 15:52:23 +00:00
										 |  |  |     Py_buffer buf; | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  |     if (string) | 
					
						
							|  |  |  |         GET_BUFFER_VIEW_OR_ERROUT(string, &buf); | 
					
						
							| 
									
										
										
										
											2009-02-12 07:35:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  |     MD5State *st = md5_get_state(module); | 
					
						
							|  |  |  |     if ((new = newMD5object(st)) == NULL) { | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  |         if (string) | 
					
						
							| 
									
										
										
										
											2009-03-03 07:49:01 +00:00
										 |  |  |             PyBuffer_Release(&buf); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2009-03-03 07:49:01 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     new->hash_state = Hacl_Hash_MD5_malloc(); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (PyErr_Occurred()) { | 
					
						
							|  |  |  |         Py_DECREF(new); | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  |         if (string) | 
					
						
							| 
									
										
										
										
											2009-03-03 07:49:01 +00:00
										 |  |  |             PyBuffer_Release(&buf); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  |     if (string) { | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |         if (buf.len >= HASHLIB_GIL_MINSIZE) { | 
					
						
							|  |  |  |             /* We do not initialize self->lock here as this is the constructor
 | 
					
						
							|  |  |  |              * where it is not yet possible to have concurrent access. */ | 
					
						
							|  |  |  |             Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |             update(new->hash_state, buf.buf, buf.len); | 
					
						
							|  |  |  |             Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             update(new->hash_state, buf.buf, buf.len); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-03 07:49:01 +00:00
										 |  |  |         PyBuffer_Release(&buf); | 
					
						
							| 
									
										
										
										
											2008-08-14 15:52:23 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return (PyObject *)new; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* List of functions exported by this module */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyMethodDef MD5_functions[] = { | 
					
						
							| 
									
										
										
										
											2014-07-27 14:20:23 +02:00
										 |  |  |     _MD5_MD5_METHODDEF | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     {NULL,      NULL}            /* Sentinel */ | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  | static int | 
					
						
							|  |  |  | _md5_traverse(PyObject *module, visitproc visit, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MD5State *state = md5_get_state(module); | 
					
						
							|  |  |  |     Py_VISIT(state->md5_type); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | _md5_clear(PyObject *module) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MD5State *state = md5_get_state(module); | 
					
						
							|  |  |  |     Py_CLEAR(state->md5_type); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | _md5_free(void *module) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     _md5_clear((PyObject *)module); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Initialize this module. */ | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  | static int | 
					
						
							|  |  |  | md5_exec(PyObject *m) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MD5State *st = md5_get_state(m); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     st->md5_type = (PyTypeObject *)PyType_FromModuleAndSpec( | 
					
						
							|  |  |  |         m, &md5_type_spec, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 14:34:49 +03:00
										 |  |  |     if (PyModule_AddObjectRef(m, "MD5Type", (PyObject *)st->md5_type) < 0) { | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyModuleDef_Slot _md5_slots[] = { | 
					
						
							|  |  |  |     {Py_mod_exec, md5_exec}, | 
					
						
							| 
									
										
										
										
											2023-05-05 15:11:27 -06:00
										 |  |  |     {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, | 
					
						
							| 
									
										
										
										
											2024-05-03 08:30:55 -07:00
										 |  |  |     {Py_mod_gil, Py_MOD_GIL_NOT_USED}, | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  |     {0, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | static struct PyModuleDef _md5module = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         PyModuleDef_HEAD_INIT, | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  |         .m_name = "_md5", | 
					
						
							|  |  |  |         .m_size = sizeof(MD5State), | 
					
						
							|  |  |  |         .m_methods = MD5_functions, | 
					
						
							|  |  |  |         .m_slots = _md5_slots, | 
					
						
							|  |  |  |         .m_traverse = _md5_traverse, | 
					
						
							|  |  |  |         .m_clear = _md5_clear, | 
					
						
							|  |  |  |         .m_free = _md5_free, | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | PyMODINIT_FUNC | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | PyInit__md5(void) | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-06 05:09:51 -05:00
										 |  |  |     return PyModuleDef_Init(&_md5module); | 
					
						
							| 
									
										
										
										
											2007-09-09 06:44:34 +00:00
										 |  |  | } |