You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

588 lines
15 KiB

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