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.

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