2007-09-09 14:44:34 +08:00
|
|
|
/* SHA1 module */
|
|
|
|
|
|
|
|
/* This module provides an interface to the SHA1 algorithm */
|
|
|
|
|
|
|
|
/* See below for information about the original code this module was
|
|
|
|
based upon. Additional work performed by:
|
|
|
|
|
|
|
|
Andrew Kuchling (amk@amk.ca)
|
|
|
|
Greg Stein (gstein@lyra.org)
|
|
|
|
Trevor Perrin (trevp@trevp.net)
|
|
|
|
|
|
|
|
Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
|
|
|
|
Licensed to PSF under a Contributor Agreement.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* SHA1 objects */
|
2021-10-22 21:36:28 +08:00
|
|
|
#ifndef Py_BUILD_CORE_BUILTIN
|
|
|
|
# define Py_BUILD_CORE_MODULE 1
|
|
|
|
#endif
|
2007-09-09 14:44:34 +08:00
|
|
|
|
|
|
|
#include "Python.h"
|
2009-02-12 15:35:29 +08:00
|
|
|
#include "hashlib.h"
|
2021-10-13 21:22:35 +08:00
|
|
|
#include "pycore_strhex.h" // _Py_strhex()
|
2023-02-25 04:16:29 +08:00
|
|
|
#include "pycore_typeobject.h" // _PyType_GetModuleState()
|
2007-09-09 14:44:34 +08:00
|
|
|
|
2014-07-27 20:20:23 +08:00
|
|
|
/*[clinic input]
|
|
|
|
module _sha1
|
|
|
|
class SHA1Type "SHA1object *" "&PyType_Type"
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/
|
2007-09-09 14:44:34 +08:00
|
|
|
|
|
|
|
/* Some useful types */
|
|
|
|
|
|
|
|
#if SIZEOF_INT == 4
|
2010-05-09 23:52:27 +08:00
|
|
|
typedef unsigned int SHA1_INT32; /* 32-bit integer */
|
2016-09-07 01:46:49 +08:00
|
|
|
typedef long long SHA1_INT64; /* 64-bit integer */
|
2007-09-09 14:44:34 +08:00
|
|
|
#else
|
|
|
|
/* not defined. compilation will die. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* The SHA1 block size and message digest sizes, in bytes */
|
|
|
|
|
|
|
|
#define SHA1_BLOCKSIZE 64
|
|
|
|
#define SHA1_DIGESTSIZE 20
|
|
|
|
|
2023-02-23 05:18:43 +08:00
|
|
|
#include "_hacl/Hacl_Hash_SHA1.h"
|
2007-09-09 14:44:34 +08:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
|
2023-02-23 05:18:43 +08:00
|
|
|
Hacl_Streaming_SHA1_state *hash_state;
|
2007-09-09 14:44:34 +08:00
|
|
|
} SHA1object;
|
|
|
|
|
2015-04-04 04:53:51 +08:00
|
|
|
#include "clinic/sha1module.c.h"
|
2007-09-09 14:44:34 +08:00
|
|
|
|
|
|
|
|
2020-09-06 18:09:51 +08:00
|
|
|
typedef struct {
|
|
|
|
PyTypeObject* sha1_type;
|
|
|
|
} SHA1State;
|
2007-09-09 14:44:34 +08:00
|
|
|
|
2020-09-06 18:09:51 +08:00
|
|
|
static inline SHA1State*
|
|
|
|
sha1_get_state(PyObject *module)
|
|
|
|
{
|
|
|
|
void *state = PyModule_GetState(module);
|
|
|
|
assert(state != NULL);
|
|
|
|
return (SHA1State *)state;
|
|
|
|
}
|
2007-09-09 14:44:34 +08:00
|
|
|
|
|
|
|
static SHA1object *
|
2020-09-06 18:09:51 +08:00
|
|
|
newSHA1object(SHA1State *st)
|
2007-09-09 14:44:34 +08:00
|
|
|
{
|
2021-05-27 15:48:19 +08:00
|
|
|
SHA1object *sha = (SHA1object *)PyObject_GC_New(SHA1object, st->sha1_type);
|
|
|
|
PyObject_GC_Track(sha);
|
|
|
|
return sha;
|
2007-09-09 14:44:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Internal methods for a hash object */
|
2021-05-27 15:48:19 +08:00
|
|
|
static int
|
|
|
|
SHA1_traverse(PyObject *ptr, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(Py_TYPE(ptr));
|
|
|
|
return 0;
|
|
|
|
}
|
2007-09-09 14:44:34 +08:00
|
|
|
|
|
|
|
static void
|
2023-02-23 05:18:43 +08:00
|
|
|
SHA1_dealloc(SHA1object *ptr)
|
2007-09-09 14:44:34 +08:00
|
|
|
{
|
2023-02-23 05:18:43 +08:00
|
|
|
Hacl_Streaming_SHA1_legacy_free(ptr->hash_state);
|
2020-09-06 18:09:51 +08:00
|
|
|
PyTypeObject *tp = Py_TYPE(ptr);
|
2021-05-27 15:48:19 +08:00
|
|
|
PyObject_GC_UnTrack(ptr);
|
|
|
|
PyObject_GC_Del(ptr);
|
2020-09-06 18:09:51 +08:00
|
|
|
Py_DECREF(tp);
|
2007-09-09 14:44:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* External methods for a hash object */
|
|
|
|
|
2014-07-27 20:20:23 +08:00
|
|
|
/*[clinic input]
|
|
|
|
SHA1Type.copy
|
|
|
|
|
2020-09-06 18:09:51 +08:00
|
|
|
cls: defining_class
|
|
|
|
|
2014-07-27 20:20:23 +08:00
|
|
|
Return a copy of the hash object.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2007-09-09 14:44:34 +08:00
|
|
|
static PyObject *
|
2020-09-06 18:09:51 +08:00
|
|
|
SHA1Type_copy_impl(SHA1object *self, PyTypeObject *cls)
|
|
|
|
/*[clinic end generated code: output=b32d4461ce8bc7a7 input=6c22e66fcc34c58e]*/
|
2007-09-09 14:44:34 +08:00
|
|
|
{
|
2023-02-25 04:16:29 +08:00
|
|
|
SHA1State *st = _PyType_GetModuleState(cls);
|
2007-09-09 14:44:34 +08:00
|
|
|
|
2020-09-06 18:09:51 +08:00
|
|
|
SHA1object *newobj;
|
|
|
|
if ((newobj = newSHA1object(st)) == NULL)
|
2013-04-16 09:38:25 +08:00
|
|
|
return NULL;
|
2007-09-09 14:44:34 +08:00
|
|
|
|
2023-02-23 05:18:43 +08:00
|
|
|
newobj->hash_state = Hacl_Streaming_SHA1_legacy_copy(self->hash_state);
|
2007-09-09 14:44:34 +08:00
|
|
|
return (PyObject *)newobj;
|
|
|
|
}
|
|
|
|
|
2014-07-27 20:20:23 +08:00
|
|
|
/*[clinic input]
|
|
|
|
SHA1Type.digest
|
|
|
|
|
2018-10-20 01:42:53 +08:00
|
|
|
Return the digest value as a bytes object.
|
2014-07-27 20:20:23 +08:00
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
SHA1Type_digest_impl(SHA1object *self)
|
2018-10-20 01:42:53 +08:00
|
|
|
/*[clinic end generated code: output=2f05302a7aa2b5cb input=13824b35407444bd]*/
|
2007-09-09 14:44:34 +08:00
|
|
|
{
|
|
|
|
unsigned char digest[SHA1_DIGESTSIZE];
|
2023-02-23 05:18:43 +08:00
|
|
|
Hacl_Streaming_SHA1_legacy_finish(self->hash_state, digest);
|
2008-05-26 21:28:38 +08:00
|
|
|
return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE);
|
2007-09-09 14:44:34 +08:00
|
|
|
}
|
|
|
|
|
2014-07-27 20:20:23 +08:00
|
|
|
/*[clinic input]
|
|
|
|
SHA1Type.hexdigest
|
|
|
|
|
|
|
|
Return the digest value as a string of hexadecimal digits.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
SHA1Type_hexdigest_impl(SHA1object *self)
|
2015-04-04 04:53:51 +08:00
|
|
|
/*[clinic end generated code: output=4161fd71e68c6659 input=97691055c0c74ab0]*/
|
2007-09-09 14:44:34 +08:00
|
|
|
{
|
|
|
|
unsigned char digest[SHA1_DIGESTSIZE];
|
2023-02-23 05:18:43 +08:00
|
|
|
Hacl_Streaming_SHA1_legacy_finish(self->hash_state, digest);
|
2015-04-26 07:22:26 +08:00
|
|
|
return _Py_strhex((const char *)digest, SHA1_DIGESTSIZE);
|
2007-09-09 14:44:34 +08:00
|
|
|
}
|
|
|
|
|
2023-02-23 05:18:43 +08:00
|
|
|
static void update(Hacl_Streaming_SHA1_state *state, uint8_t *buf, Py_ssize_t len) {
|
|
|
|
#if PY_SSIZE_T_MAX > UINT32_MAX
|
|
|
|
while (len > UINT32_MAX) {
|
|
|
|
Hacl_Streaming_SHA1_legacy_update(state, buf, UINT32_MAX);
|
|
|
|
len -= UINT32_MAX;
|
|
|
|
buf += UINT32_MAX;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
Hacl_Streaming_SHA1_legacy_update(state, buf, (uint32_t) len);
|
|
|
|
}
|
|
|
|
|
2014-07-27 20:20:23 +08:00
|
|
|
/*[clinic input]
|
|
|
|
SHA1Type.update
|
|
|
|
|
|
|
|
obj: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Update this hash object's state with the provided string.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2007-09-09 14:44:34 +08:00
|
|
|
static PyObject *
|
2014-07-27 20:20:23 +08:00
|
|
|
SHA1Type_update(SHA1object *self, PyObject *obj)
|
2015-04-04 04:53:51 +08:00
|
|
|
/*[clinic end generated code: output=d9902f0e5015e9ae input=aad8e07812edbba3]*/
|
2007-09-09 14:44:34 +08:00
|
|
|
{
|
2008-08-14 23:52:23 +08:00
|
|
|
Py_buffer buf;
|
2007-09-09 14:44:34 +08:00
|
|
|
|
2009-02-12 15:35:29 +08:00
|
|
|
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
|
|
|
|
|
2023-02-23 05:18:43 +08:00
|
|
|
update(self->hash_state, buf.buf, buf.len);
|
2007-09-09 14:44:34 +08:00
|
|
|
|
2008-08-14 23:52:23 +08:00
|
|
|
PyBuffer_Release(&buf);
|
2017-01-23 15:47:21 +08:00
|
|
|
Py_RETURN_NONE;
|
2007-09-09 14:44:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef SHA1_methods[] = {
|
2014-07-27 20:20:23 +08:00
|
|
|
SHA1TYPE_COPY_METHODDEF
|
|
|
|
SHA1TYPE_DIGEST_METHODDEF
|
|
|
|
SHA1TYPE_HEXDIGEST_METHODDEF
|
|
|
|
SHA1TYPE_UPDATE_METHODDEF
|
2010-05-09 23:52:27 +08:00
|
|
|
{NULL, NULL} /* sentinel */
|
2007-09-09 14:44:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
SHA1_get_block_size(PyObject *self, void *closure)
|
|
|
|
{
|
2007-12-02 22:31:20 +08:00
|
|
|
return PyLong_FromLong(SHA1_BLOCKSIZE);
|
2007-09-09 14:44:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
SHA1_get_name(PyObject *self, void *closure)
|
|
|
|
{
|
2013-08-16 00:31:48 +08:00
|
|
|
return PyUnicode_FromStringAndSize("sha1", 4);
|
2007-09-09 14:44:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
sha1_get_digest_size(PyObject *self, void *closure)
|
|
|
|
{
|
2007-12-02 22:31:20 +08:00
|
|
|
return PyLong_FromLong(SHA1_DIGESTSIZE);
|
2007-09-09 14:44:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef SHA1_getseters[] = {
|
|
|
|
{"block_size",
|
|
|
|
(getter)SHA1_get_block_size, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL},
|
|
|
|
{"name",
|
|
|
|
(getter)SHA1_get_name, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL},
|
|
|
|
{"digest_size",
|
|
|
|
(getter)sha1_get_digest_size, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2020-09-06 18:09:51 +08:00
|
|
|
static PyType_Slot sha1_type_slots[] = {
|
|
|
|
{Py_tp_dealloc, SHA1_dealloc},
|
|
|
|
{Py_tp_methods, SHA1_methods},
|
|
|
|
{Py_tp_getset, SHA1_getseters},
|
2021-05-27 15:48:19 +08:00
|
|
|
{Py_tp_traverse, SHA1_traverse},
|
2020-09-06 18:09:51 +08:00
|
|
|
{0,0}
|
2007-09-09 14:44:34 +08:00
|
|
|
};
|
|
|
|
|
2020-09-06 18:09:51 +08:00
|
|
|
static PyType_Spec sha1_type_spec = {
|
|
|
|
.name = "_sha1.sha1",
|
|
|
|
.basicsize = sizeof(SHA1object),
|
2021-05-27 15:48:19 +08:00
|
|
|
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
|
|
|
|
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
|
2020-09-06 18:09:51 +08:00
|
|
|
.slots = sha1_type_slots
|
|
|
|
};
|
2007-09-09 14:44:34 +08:00
|
|
|
|
|
|
|
/* The single module-level function: new() */
|
|
|
|
|
2014-07-27 20:20:23 +08:00
|
|
|
/*[clinic input]
|
|
|
|
_sha1.sha1
|
|
|
|
|
|
|
|
string: object(c_default="NULL") = b''
|
2019-09-13 08:30:00 +08:00
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
2014-07-27 20:20:23 +08:00
|
|
|
|
|
|
|
Return a new SHA1 hash object; optionally initialized with a string.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2019-09-13 08:30:00 +08:00
|
|
|
_sha1_sha1_impl(PyObject *module, PyObject *string, int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=6f8b3af05126e18e input=bd54b68e2bf36a8a]*/
|
2007-09-09 14:44:34 +08:00
|
|
|
{
|
|
|
|
SHA1object *new;
|
2008-08-14 23:52:23 +08:00
|
|
|
Py_buffer buf;
|
2007-09-09 14:44:34 +08:00
|
|
|
|
2014-07-27 20:20:23 +08:00
|
|
|
if (string)
|
|
|
|
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
|
2009-02-12 15:35:29 +08:00
|
|
|
|
2020-09-06 18:09:51 +08:00
|
|
|
SHA1State *st = sha1_get_state(module);
|
|
|
|
if ((new = newSHA1object(st)) == NULL) {
|
2014-07-27 20:20:23 +08:00
|
|
|
if (string)
|
2009-03-03 15:49:01 +08:00
|
|
|
PyBuffer_Release(&buf);
|
2007-09-09 14:44:34 +08:00
|
|
|
return NULL;
|
2009-03-03 15:49:01 +08:00
|
|
|
}
|
2007-09-09 14:44:34 +08:00
|
|
|
|
2023-02-23 05:18:43 +08:00
|
|
|
new->hash_state = Hacl_Streaming_SHA1_legacy_create_in();
|
2007-09-09 14:44:34 +08:00
|
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
Py_DECREF(new);
|
2014-07-27 20:20:23 +08:00
|
|
|
if (string)
|
2009-03-03 15:49:01 +08:00
|
|
|
PyBuffer_Release(&buf);
|
2007-09-09 14:44:34 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-27 20:20:23 +08:00
|
|
|
if (string) {
|
2023-02-23 05:18:43 +08:00
|
|
|
update(new->hash_state, buf.buf, buf.len);
|
2009-03-03 15:49:01 +08:00
|
|
|
PyBuffer_Release(&buf);
|
2008-08-14 23:52:23 +08:00
|
|
|
}
|
2007-09-09 14:44:34 +08:00
|
|
|
|
|
|
|
return (PyObject *)new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* List of functions exported by this module */
|
|
|
|
|
|
|
|
static struct PyMethodDef SHA1_functions[] = {
|
2014-07-27 20:20:23 +08:00
|
|
|
_SHA1_SHA1_METHODDEF
|
2010-05-09 23:52:27 +08:00
|
|
|
{NULL, NULL} /* Sentinel */
|
2007-09-09 14:44:34 +08:00
|
|
|
};
|
|
|
|
|
2020-09-06 18:09:51 +08:00
|
|
|
static int
|
|
|
|
_sha1_traverse(PyObject *module, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
SHA1State *state = sha1_get_state(module);
|
|
|
|
Py_VISIT(state->sha1_type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_sha1_clear(PyObject *module)
|
|
|
|
{
|
|
|
|
SHA1State *state = sha1_get_state(module);
|
|
|
|
Py_CLEAR(state->sha1_type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_sha1_free(void *module)
|
|
|
|
{
|
|
|
|
_sha1_clear((PyObject *)module);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_sha1_exec(PyObject *module)
|
|
|
|
{
|
|
|
|
SHA1State* st = sha1_get_state(module);
|
|
|
|
|
|
|
|
st->sha1_type = (PyTypeObject *)PyType_FromModuleAndSpec(
|
|
|
|
module, &sha1_type_spec, NULL);
|
|
|
|
|
|
|
|
if (st->sha1_type == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(st->sha1_type);
|
2021-05-01 00:40:30 +08:00
|
|
|
if (PyModule_AddObject(module,
|
2020-09-06 18:09:51 +08:00
|
|
|
"SHA1Type",
|
|
|
|
(PyObject *)st->sha1_type) < 0) {
|
|
|
|
Py_DECREF(st->sha1_type);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-09 14:44:34 +08:00
|
|
|
|
|
|
|
/* Initialize this module. */
|
|
|
|
|
2020-09-06 18:09:51 +08:00
|
|
|
static PyModuleDef_Slot _sha1_slots[] = {
|
|
|
|
{Py_mod_exec, _sha1_exec},
|
2023-05-06 05:11:27 +08:00
|
|
|
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
2020-09-06 18:09:51 +08:00
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
|
2008-06-11 13:26:20 +08:00
|
|
|
static struct PyModuleDef _sha1module = {
|
2010-05-09 23:52:27 +08:00
|
|
|
PyModuleDef_HEAD_INIT,
|
2020-09-06 18:09:51 +08:00
|
|
|
.m_name = "_sha1",
|
|
|
|
.m_size = sizeof(SHA1State),
|
|
|
|
.m_methods = SHA1_functions,
|
|
|
|
.m_slots = _sha1_slots,
|
|
|
|
.m_traverse = _sha1_traverse,
|
|
|
|
.m_clear = _sha1_clear,
|
|
|
|
.m_free = _sha1_free
|
2008-06-11 13:26:20 +08:00
|
|
|
};
|
|
|
|
|
2007-09-09 14:44:34 +08:00
|
|
|
PyMODINIT_FUNC
|
2008-06-11 13:26:20 +08:00
|
|
|
PyInit__sha1(void)
|
2007-09-09 14:44:34 +08:00
|
|
|
{
|
2020-09-06 18:09:51 +08:00
|
|
|
return PyModuleDef_Init(&_sha1module);
|
2007-09-09 14:44:34 +08:00
|
|
|
}
|