blob: b2e65a0a5ffd227f36038736724c3fbfe5507ede [file] [log] [blame]
Gregory P. Smith2f21eb32007-09-09 06:44:34 +00001/* MD5 module */
2
3/* This module provides an interface to the MD5 algorithm */
4
5/* See below for information about the original code this module was
6 based upon. Additional work performed by:
7
8 Andrew Kuchling (amk@amk.ca)
9 Greg Stein (gstein@lyra.org)
10 Trevor Perrin (trevp@trevp.net)
11
12 Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
13 Licensed to PSF under a Contributor Agreement.
14
15*/
16
17/* MD5 objects */
18
19#include "Python.h"
Gregory P. Smith365a1862009-02-12 07:35:29 +000020#include "hashlib.h"
Gregory P. Smith4dff6f62015-04-25 23:42:38 +000021#include "pystrhex.h"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000022
Martin v. Löwis501b13c2014-07-27 14:20:23 +020023/*[clinic input]
24module _md5
25class MD5Type "MD5object *" "&PyType_Type"
26[clinic start generated code]*/
27/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000028
29/* Some useful types */
30
31#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032typedef unsigned int MD5_INT32; /* 32-bit integer */
Benjamin Petersonaf580df2016-09-06 10:46:49 -070033typedef long long MD5_INT64; /* 64-bit integer */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000034#else
35/* not defined. compilation will die. */
36#endif
37
38/* The MD5 block size and message digest sizes, in bytes */
39
40#define MD5_BLOCKSIZE 64
41#define MD5_DIGESTSIZE 16
42
43/* The structure for storing MD5 info */
44
45struct md5_state {
46 MD5_INT64 length;
47 MD5_INT32 state[4], curlen;
48 unsigned char buf[MD5_BLOCKSIZE];
49};
50
51typedef struct {
52 PyObject_HEAD
53
54 struct md5_state hash_state;
55} MD5object;
56
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030057#include "clinic/md5module.c.h"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000058
59/* ------------------------------------------------------------------------
60 *
61 * This code for the MD5 algorithm was noted as public domain. The
62 * original headers are pasted below.
63 *
64 * Several changes have been made to make it more compatible with the
65 * Python environment and desired interface.
66 *
67 */
68
69/* LibTomCrypt, modular cryptographic library -- Tom St Denis
70 *
71 * LibTomCrypt is a library that provides various cryptographic
72 * algorithms in a highly modular and flexible manner.
73 *
74 * The library is free for all purposes without any express
75 * guarantee it works.
76 *
Erlend Egeberg Aasland5ec7d532021-02-12 11:34:11 +010077 * Tom St Denis, tomstdenis@gmail.com, https://www.libtom.net
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000078 */
79
80/* rotate the hard way (platform optimizations could be done) */
81#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
82
83/* Endian Neutral macros that work on all platforms */
84
85#define STORE32L(x, y) \
86 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
87 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
88
89#define LOAD32L(x, y) \
90 { x = ((unsigned long)((y)[3] & 255)<<24) | \
91 ((unsigned long)((y)[2] & 255)<<16) | \
92 ((unsigned long)((y)[1] & 255)<<8) | \
93 ((unsigned long)((y)[0] & 255)); }
94
95#define STORE64L(x, y) \
96 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
97 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
98 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
99 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
100
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000101
102/* MD5 macros */
103
104#define F(x,y,z) (z ^ (x & (y ^ z)))
105#define G(x,y,z) (y ^ (z & (y ^ x)))
106#define H(x,y,z) (x^y^z)
107#define I(x,y,z) (y^(x|(~z)))
108
109#define FF(a,b,c,d,M,s,t) \
110 a = (a + F(b,c,d) + M + t); a = ROLc(a, s) + b;
111
112#define GG(a,b,c,d,M,s,t) \
113 a = (a + G(b,c,d) + M + t); a = ROLc(a, s) + b;
114
115#define HH(a,b,c,d,M,s,t) \
116 a = (a + H(b,c,d) + M + t); a = ROLc(a, s) + b;
117
118#define II(a,b,c,d,M,s,t) \
119 a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b;
120
121
Andy Lester597ebed2020-02-12 22:53:01 -0600122static void md5_compress(struct md5_state *md5, const unsigned char *buf)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000123{
124 MD5_INT32 i, W[16], a, b, c, d;
125
126 assert(md5 != NULL);
127 assert(buf != NULL);
128
129 /* copy the state into 512-bits into W[0..15] */
130 for (i = 0; i < 16; i++) {
131 LOAD32L(W[i], buf + (4*i));
132 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000134 /* copy state */
135 a = md5->state[0];
136 b = md5->state[1];
137 c = md5->state[2];
138 d = md5->state[3];
139
140 FF(a,b,c,d,W[0],7,0xd76aa478UL)
141 FF(d,a,b,c,W[1],12,0xe8c7b756UL)
142 FF(c,d,a,b,W[2],17,0x242070dbUL)
143 FF(b,c,d,a,W[3],22,0xc1bdceeeUL)
144 FF(a,b,c,d,W[4],7,0xf57c0fafUL)
145 FF(d,a,b,c,W[5],12,0x4787c62aUL)
146 FF(c,d,a,b,W[6],17,0xa8304613UL)
147 FF(b,c,d,a,W[7],22,0xfd469501UL)
148 FF(a,b,c,d,W[8],7,0x698098d8UL)
149 FF(d,a,b,c,W[9],12,0x8b44f7afUL)
150 FF(c,d,a,b,W[10],17,0xffff5bb1UL)
151 FF(b,c,d,a,W[11],22,0x895cd7beUL)
152 FF(a,b,c,d,W[12],7,0x6b901122UL)
153 FF(d,a,b,c,W[13],12,0xfd987193UL)
154 FF(c,d,a,b,W[14],17,0xa679438eUL)
155 FF(b,c,d,a,W[15],22,0x49b40821UL)
156 GG(a,b,c,d,W[1],5,0xf61e2562UL)
157 GG(d,a,b,c,W[6],9,0xc040b340UL)
158 GG(c,d,a,b,W[11],14,0x265e5a51UL)
159 GG(b,c,d,a,W[0],20,0xe9b6c7aaUL)
160 GG(a,b,c,d,W[5],5,0xd62f105dUL)
161 GG(d,a,b,c,W[10],9,0x02441453UL)
162 GG(c,d,a,b,W[15],14,0xd8a1e681UL)
163 GG(b,c,d,a,W[4],20,0xe7d3fbc8UL)
164 GG(a,b,c,d,W[9],5,0x21e1cde6UL)
165 GG(d,a,b,c,W[14],9,0xc33707d6UL)
166 GG(c,d,a,b,W[3],14,0xf4d50d87UL)
167 GG(b,c,d,a,W[8],20,0x455a14edUL)
168 GG(a,b,c,d,W[13],5,0xa9e3e905UL)
169 GG(d,a,b,c,W[2],9,0xfcefa3f8UL)
170 GG(c,d,a,b,W[7],14,0x676f02d9UL)
171 GG(b,c,d,a,W[12],20,0x8d2a4c8aUL)
172 HH(a,b,c,d,W[5],4,0xfffa3942UL)
173 HH(d,a,b,c,W[8],11,0x8771f681UL)
174 HH(c,d,a,b,W[11],16,0x6d9d6122UL)
175 HH(b,c,d,a,W[14],23,0xfde5380cUL)
176 HH(a,b,c,d,W[1],4,0xa4beea44UL)
177 HH(d,a,b,c,W[4],11,0x4bdecfa9UL)
178 HH(c,d,a,b,W[7],16,0xf6bb4b60UL)
179 HH(b,c,d,a,W[10],23,0xbebfbc70UL)
180 HH(a,b,c,d,W[13],4,0x289b7ec6UL)
181 HH(d,a,b,c,W[0],11,0xeaa127faUL)
182 HH(c,d,a,b,W[3],16,0xd4ef3085UL)
183 HH(b,c,d,a,W[6],23,0x04881d05UL)
184 HH(a,b,c,d,W[9],4,0xd9d4d039UL)
185 HH(d,a,b,c,W[12],11,0xe6db99e5UL)
186 HH(c,d,a,b,W[15],16,0x1fa27cf8UL)
187 HH(b,c,d,a,W[2],23,0xc4ac5665UL)
188 II(a,b,c,d,W[0],6,0xf4292244UL)
189 II(d,a,b,c,W[7],10,0x432aff97UL)
190 II(c,d,a,b,W[14],15,0xab9423a7UL)
191 II(b,c,d,a,W[5],21,0xfc93a039UL)
192 II(a,b,c,d,W[12],6,0x655b59c3UL)
193 II(d,a,b,c,W[3],10,0x8f0ccc92UL)
194 II(c,d,a,b,W[10],15,0xffeff47dUL)
195 II(b,c,d,a,W[1],21,0x85845dd1UL)
196 II(a,b,c,d,W[8],6,0x6fa87e4fUL)
197 II(d,a,b,c,W[15],10,0xfe2ce6e0UL)
198 II(c,d,a,b,W[6],15,0xa3014314UL)
199 II(b,c,d,a,W[13],21,0x4e0811a1UL)
200 II(a,b,c,d,W[4],6,0xf7537e82UL)
201 II(d,a,b,c,W[11],10,0xbd3af235UL)
202 II(c,d,a,b,W[2],15,0x2ad7d2bbUL)
203 II(b,c,d,a,W[9],21,0xeb86d391UL)
204
205 md5->state[0] = md5->state[0] + a;
206 md5->state[1] = md5->state[1] + b;
207 md5->state[2] = md5->state[2] + c;
208 md5->state[3] = md5->state[3] + d;
209}
210
211
212/**
213 Initialize the hash state
Jakub Jelend5d05212020-10-20 11:10:43 +0200214 @param md5 The hash state you wish to initialize
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000215*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200216static void
217md5_init(struct md5_state *md5)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000218{
219 assert(md5 != NULL);
220 md5->state[0] = 0x67452301UL;
221 md5->state[1] = 0xefcdab89UL;
222 md5->state[2] = 0x98badcfeUL;
223 md5->state[3] = 0x10325476UL;
224 md5->curlen = 0;
225 md5->length = 0;
226}
227
228/**
229 Process a block of memory though the hash
Jakub Jelend5d05212020-10-20 11:10:43 +0200230 @param md5 The hash state
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000231 @param in The data to hash
232 @param inlen The length of the data (octets)
233*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200234static void
235md5_process(struct md5_state *md5, const unsigned char *in, Py_ssize_t inlen)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000236{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000237 Py_ssize_t n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000238
239 assert(md5 != NULL);
240 assert(in != NULL);
241 assert(md5->curlen <= sizeof(md5->buf));
242
243 while (inlen > 0) {
244 if (md5->curlen == 0 && inlen >= MD5_BLOCKSIZE) {
Andy Lester597ebed2020-02-12 22:53:01 -0600245 md5_compress(md5, in);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000246 md5->length += MD5_BLOCKSIZE * 8;
247 in += MD5_BLOCKSIZE;
248 inlen -= MD5_BLOCKSIZE;
249 } else {
Victor Stinner640c35c2013-06-04 23:14:37 +0200250 n = Py_MIN(inlen, (Py_ssize_t)(MD5_BLOCKSIZE - md5->curlen));
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000251 memcpy(md5->buf + md5->curlen, in, (size_t)n);
Victor Stinner56cb1252012-10-31 00:33:57 +0100252 md5->curlen += (MD5_INT32)n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000253 in += n;
254 inlen -= n;
255 if (md5->curlen == MD5_BLOCKSIZE) {
256 md5_compress(md5, md5->buf);
257 md5->length += 8*MD5_BLOCKSIZE;
258 md5->curlen = 0;
259 }
260 }
261 }
262}
263
264/**
265 Terminate the hash to get the digest
Jakub Jelend5d05212020-10-20 11:10:43 +0200266 @param md5 The hash state
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000267 @param out [out] The destination of the hash (16 bytes)
268*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200269static void
270md5_done(struct md5_state *md5, unsigned char *out)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000271{
272 int i;
273
274 assert(md5 != NULL);
275 assert(out != NULL);
276 assert(md5->curlen < sizeof(md5->buf));
277
278 /* increase the length of the message */
279 md5->length += md5->curlen * 8;
280
281 /* append the '1' bit */
282 md5->buf[md5->curlen++] = (unsigned char)0x80;
283
284 /* if the length is currently above 56 bytes we append zeros
285 * then compress. Then we can fall back to padding zeros and length
286 * encoding like normal.
287 */
288 if (md5->curlen > 56) {
289 while (md5->curlen < 64) {
290 md5->buf[md5->curlen++] = (unsigned char)0;
291 }
292 md5_compress(md5, md5->buf);
293 md5->curlen = 0;
294 }
295
Leo Ariasc3d95082018-02-03 18:36:10 -0600296 /* pad up to 56 bytes of zeroes */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000297 while (md5->curlen < 56) {
298 md5->buf[md5->curlen++] = (unsigned char)0;
299 }
300
301 /* store length */
302 STORE64L(md5->length, md5->buf+56);
303 md5_compress(md5, md5->buf);
304
305 /* copy output */
306 for (i = 0; i < 4; i++) {
307 STORE32L(md5->state[i], out+(4*i));
308 }
309}
310
311/* .Source: /cvs/libtom/libtomcrypt/src/hashes/md5.c,v $ */
312/* .Revision: 1.10 $ */
313/* .Date: 2007/05/12 14:25:28 $ */
314
315/*
316 * End of copied MD5 code.
317 *
318 * ------------------------------------------------------------------------
319 */
320
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500321typedef struct {
322 PyTypeObject* md5_type;
323} MD5State;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000324
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500325static inline MD5State*
326md5_get_state(PyObject *module)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000327{
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500328 void *state = PyModule_GetState(module);
329 assert(state != NULL);
330 return (MD5State *)state;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000331}
332
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500333static MD5object *
334newMD5object(MD5State * st)
335{
336 return (MD5object *)PyObject_New(MD5object, st->md5_type);
337}
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000338
339/* Internal methods for a hash object */
340
341static void
342MD5_dealloc(PyObject *ptr)
343{
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500344 PyTypeObject *tp = Py_TYPE(ptr);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100345 PyObject_Free(ptr);
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500346 Py_DECREF(tp);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000347}
348
349
350/* External methods for a hash object */
351
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200352/*[clinic input]
353MD5Type.copy
354
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500355 cls: defining_class
356
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200357Return a copy of the hash object.
358[clinic start generated code]*/
359
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200360static PyObject *
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500361MD5Type_copy_impl(MD5object *self, PyTypeObject *cls)
362/*[clinic end generated code: output=bf055e08244bf5ee input=d89087dcfb2a8620]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000363{
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500364 MD5State *st = PyType_GetModuleState(cls);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000365
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500366 MD5object *newobj;
367 if ((newobj = newMD5object(st))==NULL)
Christian Heimes29fbd212015-04-16 17:29:11 +0200368 return NULL;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000369
370 newobj->hash_state = self->hash_state;
371 return (PyObject *)newobj;
372}
373
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200374/*[clinic input]
375MD5Type.digest
376
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530377Return the digest value as a bytes object.
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200378[clinic start generated code]*/
379
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200380static PyObject *
381MD5Type_digest_impl(MD5object *self)
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530382/*[clinic end generated code: output=eb691dc4190a07ec input=bc0c4397c2994be6]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000383{
384 unsigned char digest[MD5_DIGESTSIZE];
385 struct md5_state temp;
386
387 temp = self->hash_state;
388 md5_done(&temp, digest);
Christian Heimes72b710a2008-05-26 13:28:38 +0000389 return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000390}
391
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200392/*[clinic input]
393MD5Type.hexdigest
394
395Return the digest value as a string of hexadecimal digits.
396[clinic start generated code]*/
397
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200398static PyObject *
399MD5Type_hexdigest_impl(MD5object *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300400/*[clinic end generated code: output=17badced1f3ac932 input=b60b19de644798dd]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000401{
402 unsigned char digest[MD5_DIGESTSIZE];
403 struct md5_state temp;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000404
405 /* Get the raw (binary) digest value */
406 temp = self->hash_state;
407 md5_done(&temp, digest);
408
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000409 return _Py_strhex((const char*)digest, MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000410}
411
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200412/*[clinic input]
413MD5Type.update
414
415 obj: object
416 /
417
418Update this hash object's state with the provided string.
419[clinic start generated code]*/
420
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000421static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200422MD5Type_update(MD5object *self, PyObject *obj)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300423/*[clinic end generated code: output=f6ad168416338423 input=6e1efcd9ecf17032]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000424{
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000425 Py_buffer buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426
Gregory P. Smith365a1862009-02-12 07:35:29 +0000427 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
428
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000429 md5_process(&self->hash_state, buf.buf, buf.len);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000430
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000431 PyBuffer_Release(&buf);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200432 Py_RETURN_NONE;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000433}
434
435static PyMethodDef MD5_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200436 MD5TYPE_COPY_METHODDEF
437 MD5TYPE_DIGEST_METHODDEF
438 MD5TYPE_HEXDIGEST_METHODDEF
439 MD5TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 {NULL, NULL} /* sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000441};
442
443static PyObject *
444MD5_get_block_size(PyObject *self, void *closure)
445{
Christian Heimes217cfd12007-12-02 14:31:20 +0000446 return PyLong_FromLong(MD5_BLOCKSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000447}
448
449static PyObject *
450MD5_get_name(PyObject *self, void *closure)
451{
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200452 return PyUnicode_FromStringAndSize("md5", 3);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000453}
454
455static PyObject *
456md5_get_digest_size(PyObject *self, void *closure)
457{
Christian Heimes217cfd12007-12-02 14:31:20 +0000458 return PyLong_FromLong(MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000459}
460
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000461static PyGetSetDef MD5_getseters[] = {
462 {"block_size",
463 (getter)MD5_get_block_size, NULL,
464 NULL,
465 NULL},
466 {"name",
467 (getter)MD5_get_name, NULL,
468 NULL,
469 NULL},
470 {"digest_size",
471 (getter)md5_get_digest_size, NULL,
472 NULL,
473 NULL},
474 {NULL} /* Sentinel */
475};
476
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500477static PyType_Slot md5_type_slots[] = {
478 {Py_tp_dealloc, MD5_dealloc},
479 {Py_tp_methods, MD5_methods},
480 {Py_tp_getset, MD5_getseters},
481 {0,0}
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000482};
483
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500484static PyType_Spec md5_type_spec = {
485 .name = "_md5.md5",
486 .basicsize = sizeof(MD5object),
487 .flags = Py_TPFLAGS_DEFAULT,
488 .slots = md5_type_slots
489};
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000490
491/* The single module-level function: new() */
492
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200493/*[clinic input]
494_md5.md5
495
496 string: object(c_default="NULL") = b''
Christian Heimes7cad53e2019-09-13 02:30:00 +0200497 *
498 usedforsecurity: bool = True
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200499
500Return a new MD5 hash object; optionally initialized with a string.
501[clinic start generated code]*/
502
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200503static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200504_md5_md5_impl(PyObject *module, PyObject *string, int usedforsecurity)
505/*[clinic end generated code: output=587071f76254a4ac input=7a144a1905636985]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200506{
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000507 MD5object *new;
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000508 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000509
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200510 if (string)
511 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000512
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500513 MD5State *st = md5_get_state(module);
514 if ((new = newMD5object(st)) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200515 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000516 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000517 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000518 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000519
520 md5_init(&new->hash_state);
521
522 if (PyErr_Occurred()) {
523 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200524 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000525 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000526 return NULL;
527 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200528 if (string) {
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000529 md5_process(&new->hash_state, buf.buf, buf.len);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000530 PyBuffer_Release(&buf);
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000531 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000532
533 return (PyObject *)new;
534}
535
536
537/* List of functions exported by this module */
538
539static struct PyMethodDef MD5_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200540 _MD5_MD5_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 {NULL, NULL} /* Sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000542};
543
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500544static int
545_md5_traverse(PyObject *module, visitproc visit, void *arg)
546{
547 MD5State *state = md5_get_state(module);
548 Py_VISIT(state->md5_type);
549 return 0;
550}
551
552static int
553_md5_clear(PyObject *module)
554{
555 MD5State *state = md5_get_state(module);
556 Py_CLEAR(state->md5_type);
557 return 0;
558}
559
560static void
561_md5_free(void *module)
562{
563 _md5_clear((PyObject *)module);
564}
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000565
566/* Initialize this module. */
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500567static int
568md5_exec(PyObject *m)
569{
570 MD5State *st = md5_get_state(m);
571
572 st->md5_type = (PyTypeObject *)PyType_FromModuleAndSpec(
573 m, &md5_type_spec, NULL);
574
575 if (st->md5_type == NULL) {
576 return -1;
577 }
578
579 Py_INCREF((PyObject *)st->md5_type);
580 if (PyModule_AddObject(m, "MD5Type", (PyObject *)st->md5_type) < 0) {
581 Py_DECREF(st->md5_type);
582 return -1;
583 }
584
585 return 0;
586}
587
588static PyModuleDef_Slot _md5_slots[] = {
589 {Py_mod_exec, md5_exec},
590 {0, NULL}
591};
592
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000593
Martin v. Löwis1a214512008-06-11 05:26:20 +0000594static struct PyModuleDef _md5module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 PyModuleDef_HEAD_INIT,
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500596 .m_name = "_md5",
597 .m_size = sizeof(MD5State),
598 .m_methods = MD5_functions,
599 .m_slots = _md5_slots,
600 .m_traverse = _md5_traverse,
601 .m_clear = _md5_clear,
602 .m_free = _md5_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +0000603};
604
605PyMODINIT_FUNC
606PyInit__md5(void)
607{
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500608 return PyModuleDef_Init(&_md5module);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000609}