blob: 0f3c6d9449cd127ed6865091abbae57376d5f95d [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"
20
21
22/* Some useful types */
23
24#if SIZEOF_INT == 4
25typedef unsigned int MD5_INT32; /* 32-bit integer */
26typedef PY_LONG_LONG MD5_INT64; /* 64-bit integer */
27#else
28/* not defined. compilation will die. */
29#endif
30
31/* The MD5 block size and message digest sizes, in bytes */
32
33#define MD5_BLOCKSIZE 64
34#define MD5_DIGESTSIZE 16
35
36/* The structure for storing MD5 info */
37
38struct md5_state {
39 MD5_INT64 length;
40 MD5_INT32 state[4], curlen;
41 unsigned char buf[MD5_BLOCKSIZE];
42};
43
44typedef struct {
45 PyObject_HEAD
46
47 struct md5_state hash_state;
48} MD5object;
49
50
51/* ------------------------------------------------------------------------
52 *
53 * This code for the MD5 algorithm was noted as public domain. The
54 * original headers are pasted below.
55 *
56 * Several changes have been made to make it more compatible with the
57 * Python environment and desired interface.
58 *
59 */
60
61/* LibTomCrypt, modular cryptographic library -- Tom St Denis
62 *
63 * LibTomCrypt is a library that provides various cryptographic
64 * algorithms in a highly modular and flexible manner.
65 *
66 * The library is free for all purposes without any express
67 * guarantee it works.
68 *
69 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
70 */
71
72/* rotate the hard way (platform optimizations could be done) */
73#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
74
75/* Endian Neutral macros that work on all platforms */
76
77#define STORE32L(x, y) \
78 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
79 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
80
81#define LOAD32L(x, y) \
82 { x = ((unsigned long)((y)[3] & 255)<<24) | \
83 ((unsigned long)((y)[2] & 255)<<16) | \
84 ((unsigned long)((y)[1] & 255)<<8) | \
85 ((unsigned long)((y)[0] & 255)); }
86
87#define STORE64L(x, y) \
88 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
89 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
90 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
91 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
92
93#ifndef MIN
94 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
95#endif
96
97
98/* MD5 macros */
99
100#define F(x,y,z) (z ^ (x & (y ^ z)))
101#define G(x,y,z) (y ^ (z & (y ^ x)))
102#define H(x,y,z) (x^y^z)
103#define I(x,y,z) (y^(x|(~z)))
104
105#define FF(a,b,c,d,M,s,t) \
106 a = (a + F(b,c,d) + M + t); a = ROLc(a, s) + b;
107
108#define GG(a,b,c,d,M,s,t) \
109 a = (a + G(b,c,d) + M + t); a = ROLc(a, s) + b;
110
111#define HH(a,b,c,d,M,s,t) \
112 a = (a + H(b,c,d) + M + t); a = ROLc(a, s) + b;
113
114#define II(a,b,c,d,M,s,t) \
115 a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b;
116
117
118static void md5_compress(struct md5_state *md5, unsigned char *buf)
119{
120 MD5_INT32 i, W[16], a, b, c, d;
121
122 assert(md5 != NULL);
123 assert(buf != NULL);
124
125 /* copy the state into 512-bits into W[0..15] */
126 for (i = 0; i < 16; i++) {
127 LOAD32L(W[i], buf + (4*i));
128 }
129
130 /* copy state */
131 a = md5->state[0];
132 b = md5->state[1];
133 c = md5->state[2];
134 d = md5->state[3];
135
136 FF(a,b,c,d,W[0],7,0xd76aa478UL)
137 FF(d,a,b,c,W[1],12,0xe8c7b756UL)
138 FF(c,d,a,b,W[2],17,0x242070dbUL)
139 FF(b,c,d,a,W[3],22,0xc1bdceeeUL)
140 FF(a,b,c,d,W[4],7,0xf57c0fafUL)
141 FF(d,a,b,c,W[5],12,0x4787c62aUL)
142 FF(c,d,a,b,W[6],17,0xa8304613UL)
143 FF(b,c,d,a,W[7],22,0xfd469501UL)
144 FF(a,b,c,d,W[8],7,0x698098d8UL)
145 FF(d,a,b,c,W[9],12,0x8b44f7afUL)
146 FF(c,d,a,b,W[10],17,0xffff5bb1UL)
147 FF(b,c,d,a,W[11],22,0x895cd7beUL)
148 FF(a,b,c,d,W[12],7,0x6b901122UL)
149 FF(d,a,b,c,W[13],12,0xfd987193UL)
150 FF(c,d,a,b,W[14],17,0xa679438eUL)
151 FF(b,c,d,a,W[15],22,0x49b40821UL)
152 GG(a,b,c,d,W[1],5,0xf61e2562UL)
153 GG(d,a,b,c,W[6],9,0xc040b340UL)
154 GG(c,d,a,b,W[11],14,0x265e5a51UL)
155 GG(b,c,d,a,W[0],20,0xe9b6c7aaUL)
156 GG(a,b,c,d,W[5],5,0xd62f105dUL)
157 GG(d,a,b,c,W[10],9,0x02441453UL)
158 GG(c,d,a,b,W[15],14,0xd8a1e681UL)
159 GG(b,c,d,a,W[4],20,0xe7d3fbc8UL)
160 GG(a,b,c,d,W[9],5,0x21e1cde6UL)
161 GG(d,a,b,c,W[14],9,0xc33707d6UL)
162 GG(c,d,a,b,W[3],14,0xf4d50d87UL)
163 GG(b,c,d,a,W[8],20,0x455a14edUL)
164 GG(a,b,c,d,W[13],5,0xa9e3e905UL)
165 GG(d,a,b,c,W[2],9,0xfcefa3f8UL)
166 GG(c,d,a,b,W[7],14,0x676f02d9UL)
167 GG(b,c,d,a,W[12],20,0x8d2a4c8aUL)
168 HH(a,b,c,d,W[5],4,0xfffa3942UL)
169 HH(d,a,b,c,W[8],11,0x8771f681UL)
170 HH(c,d,a,b,W[11],16,0x6d9d6122UL)
171 HH(b,c,d,a,W[14],23,0xfde5380cUL)
172 HH(a,b,c,d,W[1],4,0xa4beea44UL)
173 HH(d,a,b,c,W[4],11,0x4bdecfa9UL)
174 HH(c,d,a,b,W[7],16,0xf6bb4b60UL)
175 HH(b,c,d,a,W[10],23,0xbebfbc70UL)
176 HH(a,b,c,d,W[13],4,0x289b7ec6UL)
177 HH(d,a,b,c,W[0],11,0xeaa127faUL)
178 HH(c,d,a,b,W[3],16,0xd4ef3085UL)
179 HH(b,c,d,a,W[6],23,0x04881d05UL)
180 HH(a,b,c,d,W[9],4,0xd9d4d039UL)
181 HH(d,a,b,c,W[12],11,0xe6db99e5UL)
182 HH(c,d,a,b,W[15],16,0x1fa27cf8UL)
183 HH(b,c,d,a,W[2],23,0xc4ac5665UL)
184 II(a,b,c,d,W[0],6,0xf4292244UL)
185 II(d,a,b,c,W[7],10,0x432aff97UL)
186 II(c,d,a,b,W[14],15,0xab9423a7UL)
187 II(b,c,d,a,W[5],21,0xfc93a039UL)
188 II(a,b,c,d,W[12],6,0x655b59c3UL)
189 II(d,a,b,c,W[3],10,0x8f0ccc92UL)
190 II(c,d,a,b,W[10],15,0xffeff47dUL)
191 II(b,c,d,a,W[1],21,0x85845dd1UL)
192 II(a,b,c,d,W[8],6,0x6fa87e4fUL)
193 II(d,a,b,c,W[15],10,0xfe2ce6e0UL)
194 II(c,d,a,b,W[6],15,0xa3014314UL)
195 II(b,c,d,a,W[13],21,0x4e0811a1UL)
196 II(a,b,c,d,W[4],6,0xf7537e82UL)
197 II(d,a,b,c,W[11],10,0xbd3af235UL)
198 II(c,d,a,b,W[2],15,0x2ad7d2bbUL)
199 II(b,c,d,a,W[9],21,0xeb86d391UL)
200
201 md5->state[0] = md5->state[0] + a;
202 md5->state[1] = md5->state[1] + b;
203 md5->state[2] = md5->state[2] + c;
204 md5->state[3] = md5->state[3] + d;
205}
206
207
208/**
209 Initialize the hash state
210 @param sha1 The hash state you wish to initialize
211*/
212void md5_init(struct md5_state *md5)
213{
214 assert(md5 != NULL);
215 md5->state[0] = 0x67452301UL;
216 md5->state[1] = 0xefcdab89UL;
217 md5->state[2] = 0x98badcfeUL;
218 md5->state[3] = 0x10325476UL;
219 md5->curlen = 0;
220 md5->length = 0;
221}
222
223/**
224 Process a block of memory though the hash
225 @param sha1 The hash state
226 @param in The data to hash
227 @param inlen The length of the data (octets)
228*/
229void md5_process(struct md5_state *md5,
230 const unsigned char *in, unsigned long inlen)
231{
232 unsigned long n;
233
234 assert(md5 != NULL);
235 assert(in != NULL);
236 assert(md5->curlen <= sizeof(md5->buf));
237
238 while (inlen > 0) {
239 if (md5->curlen == 0 && inlen >= MD5_BLOCKSIZE) {
240 md5_compress(md5, (unsigned char *)in);
241 md5->length += MD5_BLOCKSIZE * 8;
242 in += MD5_BLOCKSIZE;
243 inlen -= MD5_BLOCKSIZE;
244 } else {
245 n = MIN(inlen, (MD5_BLOCKSIZE - md5->curlen));
246 memcpy(md5->buf + md5->curlen, in, (size_t)n);
247 md5->curlen += n;
248 in += n;
249 inlen -= n;
250 if (md5->curlen == MD5_BLOCKSIZE) {
251 md5_compress(md5, md5->buf);
252 md5->length += 8*MD5_BLOCKSIZE;
253 md5->curlen = 0;
254 }
255 }
256 }
257}
258
259/**
260 Terminate the hash to get the digest
261 @param sha1 The hash state
262 @param out [out] The destination of the hash (16 bytes)
263*/
264void md5_done(struct md5_state *md5, unsigned char *out)
265{
266 int i;
267
268 assert(md5 != NULL);
269 assert(out != NULL);
270 assert(md5->curlen < sizeof(md5->buf));
271
272 /* increase the length of the message */
273 md5->length += md5->curlen * 8;
274
275 /* append the '1' bit */
276 md5->buf[md5->curlen++] = (unsigned char)0x80;
277
278 /* if the length is currently above 56 bytes we append zeros
279 * then compress. Then we can fall back to padding zeros and length
280 * encoding like normal.
281 */
282 if (md5->curlen > 56) {
283 while (md5->curlen < 64) {
284 md5->buf[md5->curlen++] = (unsigned char)0;
285 }
286 md5_compress(md5, md5->buf);
287 md5->curlen = 0;
288 }
289
290 /* pad upto 56 bytes of zeroes */
291 while (md5->curlen < 56) {
292 md5->buf[md5->curlen++] = (unsigned char)0;
293 }
294
295 /* store length */
296 STORE64L(md5->length, md5->buf+56);
297 md5_compress(md5, md5->buf);
298
299 /* copy output */
300 for (i = 0; i < 4; i++) {
301 STORE32L(md5->state[i], out+(4*i));
302 }
303}
304
305/* .Source: /cvs/libtom/libtomcrypt/src/hashes/md5.c,v $ */
306/* .Revision: 1.10 $ */
307/* .Date: 2007/05/12 14:25:28 $ */
308
309/*
310 * End of copied MD5 code.
311 *
312 * ------------------------------------------------------------------------
313 */
314
315static PyTypeObject MD5type;
316
317
318static MD5object *
319newMD5object(void)
320{
321 return (MD5object *)PyObject_New(MD5object, &MD5type);
322}
323
324
325/* Internal methods for a hash object */
326
327static void
328MD5_dealloc(PyObject *ptr)
329{
330 PyObject_Del(ptr);
331}
332
333
334/* External methods for a hash object */
335
336PyDoc_STRVAR(MD5_copy__doc__, "Return a copy of the hash object.");
337
338static PyObject *
339MD5_copy(MD5object *self, PyObject *unused)
340{
341 MD5object *newobj;
342
Christian Heimes90aa7642007-12-19 02:45:37 +0000343 if (Py_TYPE(self) == &MD5type) {
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000344 if ( (newobj = newMD5object())==NULL)
345 return NULL;
346 } else {
347 if ( (newobj = newMD5object())==NULL)
348 return NULL;
349 }
350
351 newobj->hash_state = self->hash_state;
352 return (PyObject *)newobj;
353}
354
355PyDoc_STRVAR(MD5_digest__doc__,
356"Return the digest value as a string of binary data.");
357
358static PyObject *
359MD5_digest(MD5object *self, PyObject *unused)
360{
361 unsigned char digest[MD5_DIGESTSIZE];
362 struct md5_state temp;
363
364 temp = self->hash_state;
365 md5_done(&temp, digest);
Christian Heimes72b710a2008-05-26 13:28:38 +0000366 return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000367}
368
369PyDoc_STRVAR(MD5_hexdigest__doc__,
370"Return the digest value as a string of hexadecimal digits.");
371
372static PyObject *
373MD5_hexdigest(MD5object *self, PyObject *unused)
374{
375 unsigned char digest[MD5_DIGESTSIZE];
376 struct md5_state temp;
377 PyObject *retval;
378 Py_UNICODE *hex_digest;
379 int i, j;
380
381 /* Get the raw (binary) digest value */
382 temp = self->hash_state;
383 md5_done(&temp, digest);
384
385 /* Create a new string */
386 retval = PyUnicode_FromStringAndSize(NULL, MD5_DIGESTSIZE * 2);
387 if (!retval)
388 return NULL;
389 hex_digest = PyUnicode_AS_UNICODE(retval);
390 if (!hex_digest) {
391 Py_DECREF(retval);
392 return NULL;
393 }
394
395 /* Make hex version of the digest */
396 for(i=j=0; i<MD5_DIGESTSIZE; i++) {
397 char c;
398 c = (digest[i] >> 4) & 0xf;
399 c = (c>9) ? c+'a'-10 : c + '0';
400 hex_digest[j++] = c;
401 c = (digest[i] & 0xf);
402 c = (c>9) ? c+'a'-10 : c + '0';
403 hex_digest[j++] = c;
404 }
405 return retval;
406}
407
408PyDoc_STRVAR(MD5_update__doc__,
409"Update this hash object's state with the provided string.");
410
411static PyObject *
412MD5_update(MD5object *self, PyObject *args)
413{
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000414 Py_buffer buf;
415
416 if (!PyArg_ParseTuple(args, "s*:update", &buf))
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000417 return NULL;
418
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000419 md5_process(&self->hash_state, buf.buf, buf.len);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000420
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000421 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000422 Py_INCREF(Py_None);
423 return Py_None;
424}
425
426static PyMethodDef MD5_methods[] = {
427 {"copy", (PyCFunction)MD5_copy, METH_NOARGS, MD5_copy__doc__},
428 {"digest", (PyCFunction)MD5_digest, METH_NOARGS, MD5_digest__doc__},
429 {"hexdigest", (PyCFunction)MD5_hexdigest, METH_NOARGS, MD5_hexdigest__doc__},
430 {"update", (PyCFunction)MD5_update, METH_VARARGS, MD5_update__doc__},
431 {NULL, NULL} /* sentinel */
432};
433
434static PyObject *
435MD5_get_block_size(PyObject *self, void *closure)
436{
Christian Heimes217cfd12007-12-02 14:31:20 +0000437 return PyLong_FromLong(MD5_BLOCKSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000438}
439
440static PyObject *
441MD5_get_name(PyObject *self, void *closure)
442{
443 return PyUnicode_FromStringAndSize("MD5", 3);
444}
445
446static PyObject *
447md5_get_digest_size(PyObject *self, void *closure)
448{
Christian Heimes217cfd12007-12-02 14:31:20 +0000449 return PyLong_FromLong(MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000450}
451
452
453static PyGetSetDef MD5_getseters[] = {
454 {"block_size",
455 (getter)MD5_get_block_size, NULL,
456 NULL,
457 NULL},
458 {"name",
459 (getter)MD5_get_name, NULL,
460 NULL,
461 NULL},
462 {"digest_size",
463 (getter)md5_get_digest_size, NULL,
464 NULL,
465 NULL},
466 {NULL} /* Sentinel */
467};
468
469static PyTypeObject MD5type = {
470 PyVarObject_HEAD_INIT(NULL, 0)
471 "_md5.md5", /*tp_name*/
472 sizeof(MD5object), /*tp_size*/
473 0, /*tp_itemsize*/
474 /* methods */
475 MD5_dealloc, /*tp_dealloc*/
476 0, /*tp_print*/
477 0, /*tp_getattr*/
478 0, /*tp_setattr*/
479 0, /*tp_compare*/
480 0, /*tp_repr*/
481 0, /*tp_as_number*/
482 0, /*tp_as_sequence*/
483 0, /*tp_as_mapping*/
484 0, /*tp_hash*/
485 0, /*tp_call*/
486 0, /*tp_str*/
487 0, /*tp_getattro*/
488 0, /*tp_setattro*/
489 0, /*tp_as_buffer*/
490 Py_TPFLAGS_DEFAULT, /*tp_flags*/
491 0, /*tp_doc*/
492 0, /*tp_traverse*/
493 0, /*tp_clear*/
494 0, /*tp_richcompare*/
495 0, /*tp_weaklistoffset*/
496 0, /*tp_iter*/
497 0, /*tp_iternext*/
498 MD5_methods, /* tp_methods */
499 NULL, /* tp_members */
500 MD5_getseters, /* tp_getset */
501};
502
503
504/* The single module-level function: new() */
505
506PyDoc_STRVAR(MD5_new__doc__,
507"Return a new MD5 hash object; optionally initialized with a string.");
508
509static PyObject *
510MD5_new(PyObject *self, PyObject *args, PyObject *kwdict)
511{
512 static char *kwlist[] = {"string", NULL};
513 MD5object *new;
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000514 Py_buffer buf;
515 buf.buf = NULL;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000516
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000517 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
518 &buf)) {
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000519 return NULL;
520 }
521
522 if ((new = newMD5object()) == NULL)
523 return NULL;
524
525 md5_init(&new->hash_state);
526
527 if (PyErr_Occurred()) {
528 Py_DECREF(new);
529 return NULL;
530 }
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000531 if (buf.buf) {
532 md5_process(&new->hash_state, buf.buf, buf.len);
533 PyBuffer_Release(&buf);
534 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000535
536 return (PyObject *)new;
537}
538
539
540/* List of functions exported by this module */
541
542static struct PyMethodDef MD5_functions[] = {
543 {"md5", (PyCFunction)MD5_new, METH_VARARGS|METH_KEYWORDS, MD5_new__doc__},
544 {NULL, NULL} /* Sentinel */
545};
546
547
548/* Initialize this module. */
549
550#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
551
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000552
Martin v. Löwis1a214512008-06-11 05:26:20 +0000553static struct PyModuleDef _md5module = {
554 PyModuleDef_HEAD_INIT,
555 "_md5",
556 NULL,
557 -1,
558 MD5_functions,
559 NULL,
560 NULL,
561 NULL,
562 NULL
563};
564
565PyMODINIT_FUNC
566PyInit__md5(void)
567{
Christian Heimes90aa7642007-12-19 02:45:37 +0000568 Py_TYPE(&MD5type) = &PyType_Type;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000569 if (PyType_Ready(&MD5type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000570 return NULL;
Martin v. Löwis3447bee2008-06-11 05:37:58 +0000571 return PyModule_Create(&_md5module);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000572}