ยปCore Development>Code coverage>Modules/md5module.c

Python code coverage for Modules/md5module.c

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