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

Python code coverage for Modules/sha1module.c

#countcontent
1n/a/* SHA1 module */
2n/a
3n/a/* This module provides an interface to the SHA1 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/* SHA1 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 _sha1
25n/aclass SHA1Type "SHA1object *" "&PyType_Type"
26n/a[clinic start generated code]*/
27n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/
28n/a
29n/a/* Some useful types */
30n/a
31n/a#if SIZEOF_INT == 4
32n/atypedef unsigned int SHA1_INT32; /* 32-bit integer */
33n/atypedef long long SHA1_INT64; /* 64-bit integer */
34n/a#else
35n/a/* not defined. compilation will die. */
36n/a#endif
37n/a
38n/a/* The SHA1 block size and message digest sizes, in bytes */
39n/a
40n/a#define SHA1_BLOCKSIZE 64
41n/a#define SHA1_DIGESTSIZE 20
42n/a
43n/a/* The structure for storing SHA1 info */
44n/a
45n/astruct sha1_state {
46n/a SHA1_INT64 length;
47n/a SHA1_INT32 state[5], curlen;
48n/a unsigned char buf[SHA1_BLOCKSIZE];
49n/a};
50n/a
51n/atypedef struct {
52n/a PyObject_HEAD
53n/a
54n/a struct sha1_state hash_state;
55n/a} SHA1object;
56n/a
57n/a#include "clinic/sha1module.c.h"
58n/a
59n/a/* ------------------------------------------------------------------------
60n/a *
61n/a * This code for the SHA1 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 ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
82n/a#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
83n/a
84n/a/* Endian Neutral macros that work on all platforms */
85n/a
86n/a#define STORE32H(x, y) \
87n/a { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
88n/a (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
89n/a
90n/a#define LOAD32H(x, y) \
91n/a { x = ((unsigned long)((y)[0] & 255)<<24) | \
92n/a ((unsigned long)((y)[1] & 255)<<16) | \
93n/a ((unsigned long)((y)[2] & 255)<<8) | \
94n/a ((unsigned long)((y)[3] & 255)); }
95n/a
96n/a#define STORE64H(x, y) \
97n/a { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
98n/a (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
99n/a (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
100n/a (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
101n/a
102n/a
103n/a/* SHA1 macros */
104n/a
105n/a#define F0(x,y,z) (z ^ (x & (y ^ z)))
106n/a#define F1(x,y,z) (x ^ y ^ z)
107n/a#define F2(x,y,z) ((x & y) | (z & (x | y)))
108n/a#define F3(x,y,z) (x ^ y ^ z)
109n/a
110n/astatic void sha1_compress(struct sha1_state *sha1, unsigned char *buf)
111n/a{
112n/a SHA1_INT32 a,b,c,d,e,W[80],i;
113n/a
114n/a /* copy the state into 512-bits into W[0..15] */
115n/a for (i = 0; i < 16; i++) {
116n/a LOAD32H(W[i], buf + (4*i));
117n/a }
118n/a
119n/a /* copy state */
120n/a a = sha1->state[0];
121n/a b = sha1->state[1];
122n/a c = sha1->state[2];
123n/a d = sha1->state[3];
124n/a e = sha1->state[4];
125n/a
126n/a /* expand it */
127n/a for (i = 16; i < 80; i++) {
128n/a W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
129n/a }
130n/a
131n/a /* compress */
132n/a /* round one */
133n/a #define FF_0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30);
134n/a #define FF_1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
135n/a #define FF_2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
136n/a #define FF_3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
137n/a
138n/a for (i = 0; i < 20; ) {
139n/a FF_0(a,b,c,d,e,i++);
140n/a FF_0(e,a,b,c,d,i++);
141n/a FF_0(d,e,a,b,c,i++);
142n/a FF_0(c,d,e,a,b,i++);
143n/a FF_0(b,c,d,e,a,i++);
144n/a }
145n/a
146n/a /* round two */
147n/a for (; i < 40; ) {
148n/a FF_1(a,b,c,d,e,i++);
149n/a FF_1(e,a,b,c,d,i++);
150n/a FF_1(d,e,a,b,c,i++);
151n/a FF_1(c,d,e,a,b,i++);
152n/a FF_1(b,c,d,e,a,i++);
153n/a }
154n/a
155n/a /* round three */
156n/a for (; i < 60; ) {
157n/a FF_2(a,b,c,d,e,i++);
158n/a FF_2(e,a,b,c,d,i++);
159n/a FF_2(d,e,a,b,c,i++);
160n/a FF_2(c,d,e,a,b,i++);
161n/a FF_2(b,c,d,e,a,i++);
162n/a }
163n/a
164n/a /* round four */
165n/a for (; i < 80; ) {
166n/a FF_3(a,b,c,d,e,i++);
167n/a FF_3(e,a,b,c,d,i++);
168n/a FF_3(d,e,a,b,c,i++);
169n/a FF_3(c,d,e,a,b,i++);
170n/a FF_3(b,c,d,e,a,i++);
171n/a }
172n/a
173n/a #undef FF_0
174n/a #undef FF_1
175n/a #undef FF_2
176n/a #undef FF_3
177n/a
178n/a /* store */
179n/a sha1->state[0] = sha1->state[0] + a;
180n/a sha1->state[1] = sha1->state[1] + b;
181n/a sha1->state[2] = sha1->state[2] + c;
182n/a sha1->state[3] = sha1->state[3] + d;
183n/a sha1->state[4] = sha1->state[4] + e;
184n/a}
185n/a
186n/a/**
187n/a Initialize the hash state
188n/a @param sha1 The hash state you wish to initialize
189n/a*/
190n/astatic void
191n/asha1_init(struct sha1_state *sha1)
192n/a{
193n/a assert(sha1 != NULL);
194n/a sha1->state[0] = 0x67452301UL;
195n/a sha1->state[1] = 0xefcdab89UL;
196n/a sha1->state[2] = 0x98badcfeUL;
197n/a sha1->state[3] = 0x10325476UL;
198n/a sha1->state[4] = 0xc3d2e1f0UL;
199n/a sha1->curlen = 0;
200n/a sha1->length = 0;
201n/a}
202n/a
203n/a/**
204n/a Process a block of memory though the hash
205n/a @param sha1 The hash state
206n/a @param in The data to hash
207n/a @param inlen The length of the data (octets)
208n/a*/
209n/astatic void
210n/asha1_process(struct sha1_state *sha1,
211n/a const unsigned char *in, Py_ssize_t inlen)
212n/a{
213n/a Py_ssize_t n;
214n/a
215n/a assert(sha1 != NULL);
216n/a assert(in != NULL);
217n/a assert(sha1->curlen <= sizeof(sha1->buf));
218n/a
219n/a while (inlen > 0) {
220n/a if (sha1->curlen == 0 && inlen >= SHA1_BLOCKSIZE) {
221n/a sha1_compress(sha1, (unsigned char *)in);
222n/a sha1->length += SHA1_BLOCKSIZE * 8;
223n/a in += SHA1_BLOCKSIZE;
224n/a inlen -= SHA1_BLOCKSIZE;
225n/a } else {
226n/a n = Py_MIN(inlen, (Py_ssize_t)(SHA1_BLOCKSIZE - sha1->curlen));
227n/a memcpy(sha1->buf + sha1->curlen, in, (size_t)n);
228n/a sha1->curlen += (SHA1_INT32)n;
229n/a in += n;
230n/a inlen -= n;
231n/a if (sha1->curlen == SHA1_BLOCKSIZE) {
232n/a sha1_compress(sha1, sha1->buf);
233n/a sha1->length += 8*SHA1_BLOCKSIZE;
234n/a sha1->curlen = 0;
235n/a }
236n/a }
237n/a }
238n/a}
239n/a
240n/a/**
241n/a Terminate the hash to get the digest
242n/a @param sha1 The hash state
243n/a @param out [out] The destination of the hash (20 bytes)
244n/a*/
245n/astatic void
246n/asha1_done(struct sha1_state *sha1, unsigned char *out)
247n/a{
248n/a int i;
249n/a
250n/a assert(sha1 != NULL);
251n/a assert(out != NULL);
252n/a assert(sha1->curlen < sizeof(sha1->buf));
253n/a
254n/a /* increase the length of the message */
255n/a sha1->length += sha1->curlen * 8;
256n/a
257n/a /* append the '1' bit */
258n/a sha1->buf[sha1->curlen++] = (unsigned char)0x80;
259n/a
260n/a /* if the length is currently above 56 bytes we append zeros
261n/a * then compress. Then we can fall back to padding zeros and length
262n/a * encoding like normal.
263n/a */
264n/a if (sha1->curlen > 56) {
265n/a while (sha1->curlen < 64) {
266n/a sha1->buf[sha1->curlen++] = (unsigned char)0;
267n/a }
268n/a sha1_compress(sha1, sha1->buf);
269n/a sha1->curlen = 0;
270n/a }
271n/a
272n/a /* pad upto 56 bytes of zeroes */
273n/a while (sha1->curlen < 56) {
274n/a sha1->buf[sha1->curlen++] = (unsigned char)0;
275n/a }
276n/a
277n/a /* store length */
278n/a STORE64H(sha1->length, sha1->buf+56);
279n/a sha1_compress(sha1, sha1->buf);
280n/a
281n/a /* copy output */
282n/a for (i = 0; i < 5; i++) {
283n/a STORE32H(sha1->state[i], out+(4*i));
284n/a }
285n/a}
286n/a
287n/a
288n/a/* .Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */
289n/a/* .Revision: 1.10 $ */
290n/a/* .Date: 2007/05/12 14:25:28 $ */
291n/a
292n/a/*
293n/a * End of copied SHA1 code.
294n/a *
295n/a * ------------------------------------------------------------------------
296n/a */
297n/a
298n/astatic PyTypeObject SHA1type;
299n/a
300n/a
301n/astatic SHA1object *
302n/anewSHA1object(void)
303n/a{
304n/a return (SHA1object *)PyObject_New(SHA1object, &SHA1type);
305n/a}
306n/a
307n/a
308n/a/* Internal methods for a hash object */
309n/a
310n/astatic void
311n/aSHA1_dealloc(PyObject *ptr)
312n/a{
313n/a PyObject_Del(ptr);
314n/a}
315n/a
316n/a
317n/a/* External methods for a hash object */
318n/a
319n/a/*[clinic input]
320n/aSHA1Type.copy
321n/a
322n/aReturn a copy of the hash object.
323n/a[clinic start generated code]*/
324n/a
325n/astatic PyObject *
326n/aSHA1Type_copy_impl(SHA1object *self)
327n/a/*[clinic end generated code: output=b4e001264620f02a input=b7eae10df6f89b36]*/
328n/a{
329n/a SHA1object *newobj;
330n/a
331n/a if ((newobj = newSHA1object()) == NULL)
332n/a return NULL;
333n/a
334n/a newobj->hash_state = self->hash_state;
335n/a return (PyObject *)newobj;
336n/a}
337n/a
338n/a/*[clinic input]
339n/aSHA1Type.digest
340n/a
341n/aReturn the digest value as a string of binary data.
342n/a[clinic start generated code]*/
343n/a
344n/astatic PyObject *
345n/aSHA1Type_digest_impl(SHA1object *self)
346n/a/*[clinic end generated code: output=2f05302a7aa2b5cb input=205d47e1927fd009]*/
347n/a{
348n/a unsigned char digest[SHA1_DIGESTSIZE];
349n/a struct sha1_state temp;
350n/a
351n/a temp = self->hash_state;
352n/a sha1_done(&temp, digest);
353n/a return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE);
354n/a}
355n/a
356n/a/*[clinic input]
357n/aSHA1Type.hexdigest
358n/a
359n/aReturn the digest value as a string of hexadecimal digits.
360n/a[clinic start generated code]*/
361n/a
362n/astatic PyObject *
363n/aSHA1Type_hexdigest_impl(SHA1object *self)
364n/a/*[clinic end generated code: output=4161fd71e68c6659 input=97691055c0c74ab0]*/
365n/a{
366n/a unsigned char digest[SHA1_DIGESTSIZE];
367n/a struct sha1_state temp;
368n/a
369n/a /* Get the raw (binary) digest value */
370n/a temp = self->hash_state;
371n/a sha1_done(&temp, digest);
372n/a
373n/a return _Py_strhex((const char *)digest, SHA1_DIGESTSIZE);
374n/a}
375n/a
376n/a/*[clinic input]
377n/aSHA1Type.update
378n/a
379n/a obj: object
380n/a /
381n/a
382n/aUpdate this hash object's state with the provided string.
383n/a[clinic start generated code]*/
384n/a
385n/astatic PyObject *
386n/aSHA1Type_update(SHA1object *self, PyObject *obj)
387n/a/*[clinic end generated code: output=d9902f0e5015e9ae input=aad8e07812edbba3]*/
388n/a{
389n/a Py_buffer buf;
390n/a
391n/a GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
392n/a
393n/a sha1_process(&self->hash_state, buf.buf, buf.len);
394n/a
395n/a PyBuffer_Release(&buf);
396n/a Py_RETURN_NONE;
397n/a}
398n/a
399n/astatic PyMethodDef SHA1_methods[] = {
400n/a SHA1TYPE_COPY_METHODDEF
401n/a SHA1TYPE_DIGEST_METHODDEF
402n/a SHA1TYPE_HEXDIGEST_METHODDEF
403n/a SHA1TYPE_UPDATE_METHODDEF
404n/a {NULL, NULL} /* sentinel */
405n/a};
406n/a
407n/astatic PyObject *
408n/aSHA1_get_block_size(PyObject *self, void *closure)
409n/a{
410n/a return PyLong_FromLong(SHA1_BLOCKSIZE);
411n/a}
412n/a
413n/astatic PyObject *
414n/aSHA1_get_name(PyObject *self, void *closure)
415n/a{
416n/a return PyUnicode_FromStringAndSize("sha1", 4);
417n/a}
418n/a
419n/astatic PyObject *
420n/asha1_get_digest_size(PyObject *self, void *closure)
421n/a{
422n/a return PyLong_FromLong(SHA1_DIGESTSIZE);
423n/a}
424n/a
425n/a
426n/astatic PyGetSetDef SHA1_getseters[] = {
427n/a {"block_size",
428n/a (getter)SHA1_get_block_size, NULL,
429n/a NULL,
430n/a NULL},
431n/a {"name",
432n/a (getter)SHA1_get_name, NULL,
433n/a NULL,
434n/a NULL},
435n/a {"digest_size",
436n/a (getter)sha1_get_digest_size, NULL,
437n/a NULL,
438n/a NULL},
439n/a {NULL} /* Sentinel */
440n/a};
441n/a
442n/astatic PyTypeObject SHA1type = {
443n/a PyVarObject_HEAD_INIT(NULL, 0)
444n/a "_sha1.sha1", /*tp_name*/
445n/a sizeof(SHA1object), /*tp_size*/
446n/a 0, /*tp_itemsize*/
447n/a /* methods */
448n/a SHA1_dealloc, /*tp_dealloc*/
449n/a 0, /*tp_print*/
450n/a 0, /*tp_getattr*/
451n/a 0, /*tp_setattr*/
452n/a 0, /*tp_reserved*/
453n/a 0, /*tp_repr*/
454n/a 0, /*tp_as_number*/
455n/a 0, /*tp_as_sequence*/
456n/a 0, /*tp_as_mapping*/
457n/a 0, /*tp_hash*/
458n/a 0, /*tp_call*/
459n/a 0, /*tp_str*/
460n/a 0, /*tp_getattro*/
461n/a 0, /*tp_setattro*/
462n/a 0, /*tp_as_buffer*/
463n/a Py_TPFLAGS_DEFAULT, /*tp_flags*/
464n/a 0, /*tp_doc*/
465n/a 0, /*tp_traverse*/
466n/a 0, /*tp_clear*/
467n/a 0, /*tp_richcompare*/
468n/a 0, /*tp_weaklistoffset*/
469n/a 0, /*tp_iter*/
470n/a 0, /*tp_iternext*/
471n/a SHA1_methods, /* tp_methods */
472n/a NULL, /* tp_members */
473n/a SHA1_getseters, /* tp_getset */
474n/a};
475n/a
476n/a
477n/a/* The single module-level function: new() */
478n/a
479n/a/*[clinic input]
480n/a_sha1.sha1
481n/a
482n/a string: object(c_default="NULL") = b''
483n/a
484n/aReturn a new SHA1 hash object; optionally initialized with a string.
485n/a[clinic start generated code]*/
486n/a
487n/astatic PyObject *
488n/a_sha1_sha1_impl(PyObject *module, PyObject *string)
489n/a/*[clinic end generated code: output=e5982830d1dece51 input=27ea54281d995ec2]*/
490n/a{
491n/a SHA1object *new;
492n/a Py_buffer buf;
493n/a
494n/a if (string)
495n/a GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
496n/a
497n/a if ((new = newSHA1object()) == NULL) {
498n/a if (string)
499n/a PyBuffer_Release(&buf);
500n/a return NULL;
501n/a }
502n/a
503n/a sha1_init(&new->hash_state);
504n/a
505n/a if (PyErr_Occurred()) {
506n/a Py_DECREF(new);
507n/a if (string)
508n/a PyBuffer_Release(&buf);
509n/a return NULL;
510n/a }
511n/a if (string) {
512n/a sha1_process(&new->hash_state, buf.buf, buf.len);
513n/a PyBuffer_Release(&buf);
514n/a }
515n/a
516n/a return (PyObject *)new;
517n/a}
518n/a
519n/a
520n/a/* List of functions exported by this module */
521n/a
522n/astatic struct PyMethodDef SHA1_functions[] = {
523n/a _SHA1_SHA1_METHODDEF
524n/a {NULL, NULL} /* Sentinel */
525n/a};
526n/a
527n/a
528n/a/* Initialize this module. */
529n/a
530n/a#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
531n/a
532n/a
533n/astatic struct PyModuleDef _sha1module = {
534n/a PyModuleDef_HEAD_INIT,
535n/a "_sha1",
536n/a NULL,
537n/a -1,
538n/a SHA1_functions,
539n/a NULL,
540n/a NULL,
541n/a NULL,
542n/a NULL
543n/a};
544n/a
545n/aPyMODINIT_FUNC
546n/aPyInit__sha1(void)
547n/a{
548n/a PyObject *m;
549n/a
550n/a Py_TYPE(&SHA1type) = &PyType_Type;
551n/a if (PyType_Ready(&SHA1type) < 0)
552n/a return NULL;
553n/a
554n/a m = PyModule_Create(&_sha1module);
555n/a if (m == NULL)
556n/a return NULL;
557n/a
558n/a Py_INCREF((PyObject *)&SHA1type);
559n/a PyModule_AddObject(m, "SHA1Type", (PyObject *)&SHA1type);
560n/a return m;
561n/a}