• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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"
20 #include "hashlib.h"
21 #include "pystrhex.h"
22 
23 /*[clinic input]
24 module _md5
25 class MD5Type "MD5object *" "&PyType_Type"
26 [clinic start generated code]*/
27 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/
28 
29 /* Some useful types */
30 
31 #if SIZEOF_INT == 4
32 typedef unsigned int MD5_INT32; /* 32-bit integer */
33 typedef long long MD5_INT64; /* 64-bit integer */
34 #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 
45 struct md5_state {
46     MD5_INT64 length;
47     MD5_INT32 state[4], curlen;
48     unsigned char buf[MD5_BLOCKSIZE];
49 };
50 
51 typedef struct {
52     PyObject_HEAD
53 
54     struct md5_state hash_state;
55 } MD5object;
56 
57 #include "clinic/md5module.c.h"
58 
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  *
77  * Tom St Denis, tomstdenis@gmail.com, https://www.libtom.net
78  */
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 
101 
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 
md5_compress(struct md5_state * md5,const unsigned char * buf)122 static void md5_compress(struct md5_state *md5, const unsigned char *buf)
123 {
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     }
133 
134     /* 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
214    @param md5   The hash state you wish to initialize
215 */
216 static void
md5_init(struct md5_state * md5)217 md5_init(struct md5_state *md5)
218 {
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
230    @param md5   The hash state
231    @param in     The data to hash
232    @param inlen  The length of the data (octets)
233 */
234 static void
md5_process(struct md5_state * md5,const unsigned char * in,Py_ssize_t inlen)235 md5_process(struct md5_state *md5, const unsigned char *in, Py_ssize_t inlen)
236 {
237     Py_ssize_t n;
238 
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) {
245            md5_compress(md5, in);
246            md5->length    += MD5_BLOCKSIZE * 8;
247            in             += MD5_BLOCKSIZE;
248            inlen          -= MD5_BLOCKSIZE;
249         } else {
250            n = Py_MIN(inlen, (Py_ssize_t)(MD5_BLOCKSIZE - md5->curlen));
251            memcpy(md5->buf + md5->curlen, in, (size_t)n);
252            md5->curlen    += (MD5_INT32)n;
253            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
266    @param md5  The hash state
267    @param out [out] The destination of the hash (16 bytes)
268 */
269 static void
md5_done(struct md5_state * md5,unsigned char * out)270 md5_done(struct md5_state *md5, unsigned char *out)
271 {
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 
296     /* pad up to 56 bytes of zeroes */
297     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 
321 typedef struct {
322     PyTypeObject* md5_type;
323 } MD5State;
324 
325 static inline MD5State*
md5_get_state(PyObject * module)326 md5_get_state(PyObject *module)
327 {
328     void *state = PyModule_GetState(module);
329     assert(state != NULL);
330     return (MD5State *)state;
331 }
332 
333 static MD5object *
newMD5object(MD5State * st)334 newMD5object(MD5State * st)
335 {
336     MD5object *md5 = (MD5object *)PyObject_GC_New(MD5object, st->md5_type);
337     PyObject_GC_Track(md5);
338     return md5;
339 }
340 
341 /* Internal methods for a hash object */
342 static int
MD5_traverse(PyObject * ptr,visitproc visit,void * arg)343 MD5_traverse(PyObject *ptr, visitproc visit, void *arg)
344 {
345     Py_VISIT(Py_TYPE(ptr));
346     return 0;
347 }
348 
349 static void
MD5_dealloc(PyObject * ptr)350 MD5_dealloc(PyObject *ptr)
351 {
352     PyTypeObject *tp = Py_TYPE(ptr);
353     PyObject_GC_UnTrack(ptr);
354     PyObject_GC_Del(ptr);
355     Py_DECREF(tp);
356 }
357 
358 
359 /* External methods for a hash object */
360 
361 /*[clinic input]
362 MD5Type.copy
363 
364     cls: defining_class
365 
366 Return a copy of the hash object.
367 [clinic start generated code]*/
368 
369 static PyObject *
MD5Type_copy_impl(MD5object * self,PyTypeObject * cls)370 MD5Type_copy_impl(MD5object *self, PyTypeObject *cls)
371 /*[clinic end generated code: output=bf055e08244bf5ee input=d89087dcfb2a8620]*/
372 {
373     MD5State *st = PyType_GetModuleState(cls);
374 
375     MD5object *newobj;
376     if ((newobj = newMD5object(st))==NULL)
377         return NULL;
378 
379     newobj->hash_state = self->hash_state;
380     return (PyObject *)newobj;
381 }
382 
383 /*[clinic input]
384 MD5Type.digest
385 
386 Return the digest value as a bytes object.
387 [clinic start generated code]*/
388 
389 static PyObject *
MD5Type_digest_impl(MD5object * self)390 MD5Type_digest_impl(MD5object *self)
391 /*[clinic end generated code: output=eb691dc4190a07ec input=bc0c4397c2994be6]*/
392 {
393     unsigned char digest[MD5_DIGESTSIZE];
394     struct md5_state temp;
395 
396     temp = self->hash_state;
397     md5_done(&temp, digest);
398     return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE);
399 }
400 
401 /*[clinic input]
402 MD5Type.hexdigest
403 
404 Return the digest value as a string of hexadecimal digits.
405 [clinic start generated code]*/
406 
407 static PyObject *
MD5Type_hexdigest_impl(MD5object * self)408 MD5Type_hexdigest_impl(MD5object *self)
409 /*[clinic end generated code: output=17badced1f3ac932 input=b60b19de644798dd]*/
410 {
411     unsigned char digest[MD5_DIGESTSIZE];
412     struct md5_state temp;
413 
414     /* Get the raw (binary) digest value */
415     temp = self->hash_state;
416     md5_done(&temp, digest);
417 
418     return _Py_strhex((const char*)digest, MD5_DIGESTSIZE);
419 }
420 
421 /*[clinic input]
422 MD5Type.update
423 
424     obj: object
425     /
426 
427 Update this hash object's state with the provided string.
428 [clinic start generated code]*/
429 
430 static PyObject *
MD5Type_update(MD5object * self,PyObject * obj)431 MD5Type_update(MD5object *self, PyObject *obj)
432 /*[clinic end generated code: output=f6ad168416338423 input=6e1efcd9ecf17032]*/
433 {
434     Py_buffer buf;
435 
436     GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
437 
438     md5_process(&self->hash_state, buf.buf, buf.len);
439 
440     PyBuffer_Release(&buf);
441     Py_RETURN_NONE;
442 }
443 
444 static PyMethodDef MD5_methods[] = {
445     MD5TYPE_COPY_METHODDEF
446     MD5TYPE_DIGEST_METHODDEF
447     MD5TYPE_HEXDIGEST_METHODDEF
448     MD5TYPE_UPDATE_METHODDEF
449     {NULL,        NULL}         /* sentinel */
450 };
451 
452 static PyObject *
MD5_get_block_size(PyObject * self,void * closure)453 MD5_get_block_size(PyObject *self, void *closure)
454 {
455     return PyLong_FromLong(MD5_BLOCKSIZE);
456 }
457 
458 static PyObject *
MD5_get_name(PyObject * self,void * closure)459 MD5_get_name(PyObject *self, void *closure)
460 {
461     return PyUnicode_FromStringAndSize("md5", 3);
462 }
463 
464 static PyObject *
md5_get_digest_size(PyObject * self,void * closure)465 md5_get_digest_size(PyObject *self, void *closure)
466 {
467     return PyLong_FromLong(MD5_DIGESTSIZE);
468 }
469 
470 static PyGetSetDef MD5_getseters[] = {
471     {"block_size",
472      (getter)MD5_get_block_size, NULL,
473      NULL,
474      NULL},
475     {"name",
476      (getter)MD5_get_name, NULL,
477      NULL,
478      NULL},
479     {"digest_size",
480      (getter)md5_get_digest_size, NULL,
481      NULL,
482      NULL},
483     {NULL}  /* Sentinel */
484 };
485 
486 static PyType_Slot md5_type_slots[] = {
487     {Py_tp_dealloc, MD5_dealloc},
488     {Py_tp_methods, MD5_methods},
489     {Py_tp_getset, MD5_getseters},
490     {Py_tp_traverse, MD5_traverse},
491     {0,0}
492 };
493 
494 static PyType_Spec md5_type_spec = {
495     .name = "_md5.md5",
496     .basicsize =  sizeof(MD5object),
497     .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
498               Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
499     .slots = md5_type_slots
500 };
501 
502 /* The single module-level function: new() */
503 
504 /*[clinic input]
505 _md5.md5
506 
507     string: object(c_default="NULL") = b''
508     *
509     usedforsecurity: bool = True
510 
511 Return a new MD5 hash object; optionally initialized with a string.
512 [clinic start generated code]*/
513 
514 static PyObject *
_md5_md5_impl(PyObject * module,PyObject * string,int usedforsecurity)515 _md5_md5_impl(PyObject *module, PyObject *string, int usedforsecurity)
516 /*[clinic end generated code: output=587071f76254a4ac input=7a144a1905636985]*/
517 {
518     MD5object *new;
519     Py_buffer buf;
520 
521     if (string)
522         GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
523 
524     MD5State *st = md5_get_state(module);
525     if ((new = newMD5object(st)) == NULL) {
526         if (string)
527             PyBuffer_Release(&buf);
528         return NULL;
529     }
530 
531     md5_init(&new->hash_state);
532 
533     if (PyErr_Occurred()) {
534         Py_DECREF(new);
535         if (string)
536             PyBuffer_Release(&buf);
537         return NULL;
538     }
539     if (string) {
540         md5_process(&new->hash_state, buf.buf, buf.len);
541         PyBuffer_Release(&buf);
542     }
543 
544     return (PyObject *)new;
545 }
546 
547 
548 /* List of functions exported by this module */
549 
550 static struct PyMethodDef MD5_functions[] = {
551     _MD5_MD5_METHODDEF
552     {NULL,      NULL}            /* Sentinel */
553 };
554 
555 static int
_md5_traverse(PyObject * module,visitproc visit,void * arg)556 _md5_traverse(PyObject *module, visitproc visit, void *arg)
557 {
558     MD5State *state = md5_get_state(module);
559     Py_VISIT(state->md5_type);
560     return 0;
561 }
562 
563 static int
_md5_clear(PyObject * module)564 _md5_clear(PyObject *module)
565 {
566     MD5State *state = md5_get_state(module);
567     Py_CLEAR(state->md5_type);
568     return 0;
569 }
570 
571 static void
_md5_free(void * module)572 _md5_free(void *module)
573 {
574     _md5_clear((PyObject *)module);
575 }
576 
577 /* Initialize this module. */
578 static int
md5_exec(PyObject * m)579 md5_exec(PyObject *m)
580 {
581     MD5State *st = md5_get_state(m);
582 
583     st->md5_type = (PyTypeObject *)PyType_FromModuleAndSpec(
584         m, &md5_type_spec, NULL);
585 
586     if (st->md5_type == NULL) {
587         return -1;
588     }
589 
590     Py_INCREF((PyObject *)st->md5_type);
591     if (PyModule_AddObject(m, "MD5Type", (PyObject *)st->md5_type) < 0) {
592          Py_DECREF(st->md5_type);
593         return -1;
594     }
595 
596     return 0;
597 }
598 
599 static PyModuleDef_Slot _md5_slots[] = {
600     {Py_mod_exec, md5_exec},
601     {0, NULL}
602 };
603 
604 
605 static struct PyModuleDef _md5module = {
606         PyModuleDef_HEAD_INIT,
607         .m_name = "_md5",
608         .m_size = sizeof(MD5State),
609         .m_methods = MD5_functions,
610         .m_slots = _md5_slots,
611         .m_traverse = _md5_traverse,
612         .m_clear = _md5_clear,
613         .m_free = _md5_free,
614 };
615 
616 PyMODINIT_FUNC
PyInit__md5(void)617 PyInit__md5(void)
618 {
619     return PyModuleDef_Init(&_md5module);
620 }
621