2005-08-21 15:45:59 -03:00
|
|
|
/* SHA512 module */
|
|
|
|
|
|
|
|
/* This module provides an interface to NIST's SHA-512 and SHA-384 Algorithms */
|
|
|
|
|
|
|
|
/* 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)
|
2023-02-14 05:25:16 -04:00
|
|
|
Jonathan Protzenko (jonathan@protzenko.fr)
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2007-09-09 03:44:34 -03:00
|
|
|
Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
|
2005-08-21 15:45:59 -03:00
|
|
|
Licensed to PSF under a Contributor Agreement.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* SHA objects */
|
2021-10-22 10:36:28 -03:00
|
|
|
#ifndef Py_BUILD_CORE_BUILTIN
|
|
|
|
# define Py_BUILD_CORE_MODULE 1
|
|
|
|
#endif
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
#include "Python.h"
|
2021-02-14 10:14:26 -04:00
|
|
|
#include "pycore_bitutils.h" // _Py_bswap64()
|
2021-10-13 10:22:35 -03:00
|
|
|
#include "pycore_strhex.h" // _Py_strhex()
|
2020-04-14 21:35:41 -03:00
|
|
|
#include "structmember.h" // PyMemberDef
|
2009-02-12 03:35:29 -04:00
|
|
|
#include "hashlib.h"
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2014-07-27 09:20:23 -03:00
|
|
|
/*[clinic input]
|
|
|
|
module _sha512
|
|
|
|
class SHA512Type "SHAobject *" "&PyType_Type"
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81a3ccde92bcfe8d]*/
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
/* The SHA block size and message digest sizes, in bytes */
|
|
|
|
|
|
|
|
#define SHA_BLOCKSIZE 128
|
|
|
|
#define SHA_DIGESTSIZE 64
|
|
|
|
|
2023-02-14 05:25:16 -04:00
|
|
|
/* The SHA2-384 and SHA2-512 implementations defer to the HACL* verified
|
|
|
|
* library. */
|
|
|
|
|
|
|
|
#include "_hacl/Hacl_Streaming_SHA2.h"
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
int digestsize;
|
2023-02-14 05:25:16 -04:00
|
|
|
Hacl_Streaming_SHA2_state_sha2_512 *state;
|
2005-08-21 15:45:59 -03:00
|
|
|
} SHAobject;
|
|
|
|
|
2015-04-03 17:53:51 -03:00
|
|
|
#include "clinic/sha512module.c.h"
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
static void SHAcopy(SHAobject *src, SHAobject *dest)
|
|
|
|
{
|
|
|
|
dest->digestsize = src->digestsize;
|
2023-02-14 05:25:16 -04:00
|
|
|
dest->state = Hacl_Streaming_SHA2_copy_512(src->state);
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
typedef struct {
|
|
|
|
PyTypeObject* sha384_type;
|
|
|
|
PyTypeObject* sha512_type;
|
|
|
|
} SHA512State;
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
static inline SHA512State*
|
|
|
|
sha512_get_state(PyObject *module)
|
|
|
|
{
|
|
|
|
void *state = PyModule_GetState(module);
|
|
|
|
assert(state != NULL);
|
|
|
|
return (SHA512State *)state;
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
static SHAobject *
|
2020-09-06 07:09:51 -03:00
|
|
|
newSHA384object(SHA512State *st)
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2021-05-27 04:48:19 -03:00
|
|
|
SHAobject *sha = (SHAobject *)PyObject_GC_New(SHAobject, st->sha384_type);
|
|
|
|
PyObject_GC_Track(sha);
|
|
|
|
return sha;
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static SHAobject *
|
2020-09-06 07:09:51 -03:00
|
|
|
newSHA512object(SHA512State *st)
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2021-05-27 04:48:19 -03:00
|
|
|
SHAobject *sha = (SHAobject *)PyObject_GC_New(SHAobject, st->sha512_type);
|
|
|
|
PyObject_GC_Track(sha);
|
|
|
|
return sha;
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Internal methods for a hash object */
|
2021-05-27 04:48:19 -03:00
|
|
|
static int
|
|
|
|
SHA_traverse(PyObject *ptr, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(Py_TYPE(ptr));
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
static void
|
2023-02-14 05:25:16 -04:00
|
|
|
SHA512_dealloc(SHAobject *ptr)
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2023-02-14 05:25:16 -04:00
|
|
|
Hacl_Streaming_SHA2_free_512(ptr->state);
|
2020-09-06 07:09:51 -03:00
|
|
|
PyTypeObject *tp = Py_TYPE(ptr);
|
2021-05-27 04:48:19 -03:00
|
|
|
PyObject_GC_UnTrack(ptr);
|
|
|
|
PyObject_GC_Del(ptr);
|
2020-09-06 07:09:51 -03:00
|
|
|
Py_DECREF(tp);
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
2023-02-14 05:25:16 -04:00
|
|
|
/* HACL* takes a uint32_t for the length of its parameter, but Py_ssize_t can be
|
|
|
|
* 64 bits. */
|
|
|
|
static void update_512(Hacl_Streaming_SHA2_state_sha2_512 *state, uint8_t *buf, Py_ssize_t len) {
|
|
|
|
/* Note: we explicitly ignore the error code on the basis that it would take >
|
|
|
|
* 1 billion years to overflow the maximum admissible length for this API
|
|
|
|
* (namely, 2^64-1 bytes). */
|
|
|
|
while (len > UINT32_MAX) {
|
|
|
|
Hacl_Streaming_SHA2_update_512(state, buf, UINT32_MAX);
|
|
|
|
len -= UINT32_MAX;
|
|
|
|
buf += UINT32_MAX;
|
|
|
|
}
|
|
|
|
/* Cast to uint32_t is safe: upon exiting the loop, len <= UINT32_MAX, and
|
|
|
|
* therefore fits in a uint32_t */
|
|
|
|
Hacl_Streaming_SHA2_update_512(state, buf, (uint32_t) len);
|
|
|
|
}
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
/* External methods for a hash object */
|
|
|
|
|
2014-07-27 09:20:23 -03:00
|
|
|
/*[clinic input]
|
|
|
|
SHA512Type.copy
|
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
cls: defining_class
|
|
|
|
|
2014-07-27 09:20:23 -03:00
|
|
|
Return a copy of the hash object.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2020-09-06 07:09:51 -03:00
|
|
|
SHA512Type_copy_impl(SHAobject *self, PyTypeObject *cls)
|
|
|
|
/*[clinic end generated code: output=85ea5b47837a08e6 input=f673a18f66527c90]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
|
|
|
SHAobject *newobj;
|
2020-09-06 07:09:51 -03:00
|
|
|
SHA512State *st = PyType_GetModuleState(cls);
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
if (Py_IS_TYPE((PyObject*)self, st->sha512_type)) {
|
|
|
|
if ( (newobj = newSHA512object(st))==NULL) {
|
2005-08-21 15:45:59 -03:00
|
|
|
return NULL;
|
2020-09-06 07:09:51 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ( (newobj = newSHA384object(st))==NULL) {
|
2005-08-21 15:45:59 -03:00
|
|
|
return NULL;
|
2020-09-06 07:09:51 -03:00
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
SHAcopy(self, newobj);
|
|
|
|
return (PyObject *)newobj;
|
|
|
|
}
|
|
|
|
|
2014-07-27 09:20:23 -03:00
|
|
|
/*[clinic input]
|
|
|
|
SHA512Type.digest
|
|
|
|
|
2018-10-19 14:42:53 -03:00
|
|
|
Return the digest value as a bytes object.
|
2014-07-27 09:20:23 -03:00
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
static PyObject *
|
2014-07-27 09:20:23 -03:00
|
|
|
SHA512Type_digest_impl(SHAobject *self)
|
2018-10-19 14:42:53 -03:00
|
|
|
/*[clinic end generated code: output=1080bbeeef7dde1b input=f6470dd359071f4b]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2023-02-14 05:25:16 -04:00
|
|
|
uint8_t digest[SHA_DIGESTSIZE];
|
|
|
|
// HACL performs copies under the hood so that self->state remains valid
|
|
|
|
// after this call.
|
|
|
|
Hacl_Streaming_SHA2_finish_512(self->state, digest);
|
2008-05-26 10:28:38 -03:00
|
|
|
return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
2014-07-27 09:20:23 -03:00
|
|
|
/*[clinic input]
|
|
|
|
SHA512Type.hexdigest
|
|
|
|
|
|
|
|
Return the digest value as a string of hexadecimal digits.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
SHA512Type_hexdigest_impl(SHAobject *self)
|
2015-04-03 17:53:51 -03:00
|
|
|
/*[clinic end generated code: output=7373305b8601e18b input=498b877b25cbe0a2]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2023-02-14 05:25:16 -04:00
|
|
|
uint8_t digest[SHA_DIGESTSIZE];
|
|
|
|
Hacl_Streaming_SHA2_finish_512(self->state, digest);
|
2015-04-25 20:22:26 -03:00
|
|
|
return _Py_strhex((const char *)digest, self->digestsize);
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
2014-07-27 09:20:23 -03:00
|
|
|
/*[clinic input]
|
|
|
|
SHA512Type.update
|
|
|
|
|
|
|
|
obj: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Update this hash object's state with the provided string.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
static PyObject *
|
2014-07-27 09:20:23 -03:00
|
|
|
SHA512Type_update(SHAobject *self, PyObject *obj)
|
2015-04-03 17:53:51 -03:00
|
|
|
/*[clinic end generated code: output=1cf333e73995a79e input=ded2b46656566283]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2009-02-12 03:35:29 -04:00
|
|
|
Py_buffer buf;
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2009-02-12 03:35:29 -04:00
|
|
|
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2023-02-14 05:25:16 -04:00
|
|
|
update_512(self->state, buf.buf, buf.len);
|
2009-02-12 03:35:29 -04:00
|
|
|
|
|
|
|
PyBuffer_Release(&buf);
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef SHA_methods[] = {
|
2014-07-27 09:20:23 -03:00
|
|
|
SHA512TYPE_COPY_METHODDEF
|
|
|
|
SHA512TYPE_DIGEST_METHODDEF
|
|
|
|
SHA512TYPE_HEXDIGEST_METHODDEF
|
|
|
|
SHA512TYPE_UPDATE_METHODDEF
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
2005-08-21 15:45:59 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
SHA512_get_block_size(PyObject *self, void *closure)
|
|
|
|
{
|
2007-12-02 10:31:20 -04:00
|
|
|
return PyLong_FromLong(SHA_BLOCKSIZE);
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
SHA512_get_name(PyObject *self, void *closure)
|
|
|
|
{
|
|
|
|
if (((SHAobject *)self)->digestsize == 64)
|
2013-08-15 13:31:48 -03:00
|
|
|
return PyUnicode_FromStringAndSize("sha512", 6);
|
2005-08-21 15:45:59 -03:00
|
|
|
else
|
2013-08-15 13:31:48 -03:00
|
|
|
return PyUnicode_FromStringAndSize("sha384", 6);
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef SHA_getseters[] = {
|
|
|
|
{"block_size",
|
|
|
|
(getter)SHA512_get_block_size, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL},
|
|
|
|
{"name",
|
|
|
|
(getter)SHA512_get_name, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyMemberDef SHA_members[] = {
|
|
|
|
{"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
static PyType_Slot sha512_sha384_type_slots[] = {
|
|
|
|
{Py_tp_dealloc, SHA512_dealloc},
|
|
|
|
{Py_tp_methods, SHA_methods},
|
|
|
|
{Py_tp_members, SHA_members},
|
|
|
|
{Py_tp_getset, SHA_getseters},
|
2021-05-27 04:48:19 -03:00
|
|
|
{Py_tp_traverse, SHA_traverse},
|
2020-09-06 07:09:51 -03:00
|
|
|
{0,0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyType_Spec sha512_sha384_type_spec = {
|
|
|
|
.name = "_sha512.sha384",
|
|
|
|
.basicsize = sizeof(SHAobject),
|
2021-05-27 04:48:19 -03:00
|
|
|
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
|
|
|
|
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
|
2020-09-06 07:09:51 -03:00
|
|
|
.slots = sha512_sha384_type_slots
|
2005-08-21 15:45:59 -03:00
|
|
|
};
|
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
// Using PyType_GetModuleState() on this type is safe since
|
|
|
|
// it cannot be subclassed: it does not have the Py_TPFLAGS_BASETYPE flag.
|
|
|
|
static PyType_Spec sha512_sha512_type_spec = {
|
|
|
|
.name = "_sha512.sha512",
|
|
|
|
.basicsize = sizeof(SHAobject),
|
2021-05-27 04:48:19 -03:00
|
|
|
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
|
|
|
|
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
|
2023-02-14 05:25:16 -04:00
|
|
|
.slots = sha512_sha384_type_slots
|
2020-09-06 07:09:51 -03:00
|
|
|
};
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
/* The single module-level function: new() */
|
|
|
|
|
2014-07-27 09:20:23 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_sha512.sha512
|
|
|
|
|
|
|
|
string: object(c_default="NULL") = b''
|
2019-09-12 21:30:00 -03:00
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
2014-07-27 09:20:23 -03:00
|
|
|
|
|
|
|
Return a new SHA-512 hash object; optionally initialized with a string.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2019-09-12 21:30:00 -03:00
|
|
|
_sha512_sha512_impl(PyObject *module, PyObject *string, int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=a8d9e5f9e6a0831c input=23b4daebc2ebb9c9]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
|
|
|
SHAobject *new;
|
2009-02-12 03:35:29 -04:00
|
|
|
Py_buffer buf;
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
SHA512State *st = sha512_get_state(module);
|
|
|
|
|
2014-07-27 09:20:23 -03:00
|
|
|
if (string)
|
|
|
|
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
|
2009-02-12 03:35:29 -04:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
if ((new = newSHA512object(st)) == NULL) {
|
2014-07-27 09:20:23 -03:00
|
|
|
if (string)
|
2009-03-03 03:49:01 -04:00
|
|
|
PyBuffer_Release(&buf);
|
2005-08-21 15:45:59 -03:00
|
|
|
return NULL;
|
2009-03-03 03:49:01 -04:00
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2023-02-14 05:25:16 -04:00
|
|
|
new->state = Hacl_Streaming_SHA2_create_in_512();
|
|
|
|
new->digestsize = 64;
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
Py_DECREF(new);
|
2014-07-27 09:20:23 -03:00
|
|
|
if (string)
|
2009-03-03 03:49:01 -04:00
|
|
|
PyBuffer_Release(&buf);
|
2005-08-21 15:45:59 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-27 09:20:23 -03:00
|
|
|
if (string) {
|
2023-02-14 05:25:16 -04:00
|
|
|
update_512(new->state, buf.buf, buf.len);
|
2009-02-12 03:35:29 -04:00
|
|
|
PyBuffer_Release(&buf);
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
return (PyObject *)new;
|
|
|
|
}
|
|
|
|
|
2014-07-27 09:20:23 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_sha512.sha384
|
|
|
|
|
|
|
|
string: object(c_default="NULL") = b''
|
2019-09-12 21:30:00 -03:00
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
2014-07-27 09:20:23 -03:00
|
|
|
|
|
|
|
Return a new SHA-384 hash object; optionally initialized with a string.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
static PyObject *
|
2019-09-12 21:30:00 -03:00
|
|
|
_sha512_sha384_impl(PyObject *module, PyObject *string, int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=da7d594a08027ac3 input=59ef72f039a6b431]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
|
|
|
SHAobject *new;
|
2009-02-12 03:35:29 -04:00
|
|
|
Py_buffer buf;
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
SHA512State *st = sha512_get_state(module);
|
|
|
|
|
2014-07-27 09:20:23 -03:00
|
|
|
if (string)
|
|
|
|
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
|
2009-02-12 03:35:29 -04:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
if ((new = newSHA384object(st)) == NULL) {
|
2014-07-27 09:20:23 -03:00
|
|
|
if (string)
|
2009-03-03 03:49:01 -04:00
|
|
|
PyBuffer_Release(&buf);
|
2005-08-21 15:45:59 -03:00
|
|
|
return NULL;
|
2009-03-03 03:49:01 -04:00
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2023-02-14 05:25:16 -04:00
|
|
|
new->state = Hacl_Streaming_SHA2_create_in_384();
|
|
|
|
new->digestsize = 48;
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
Py_DECREF(new);
|
2014-07-27 09:20:23 -03:00
|
|
|
if (string)
|
2009-03-03 03:49:01 -04:00
|
|
|
PyBuffer_Release(&buf);
|
2005-08-21 15:45:59 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-27 09:20:23 -03:00
|
|
|
if (string) {
|
2023-02-14 05:25:16 -04:00
|
|
|
update_512(new->state, buf.buf, buf.len);
|
2009-02-12 03:35:29 -04:00
|
|
|
PyBuffer_Release(&buf);
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
return (PyObject *)new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* List of functions exported by this module */
|
|
|
|
|
|
|
|
static struct PyMethodDef SHA_functions[] = {
|
2014-07-27 09:20:23 -03:00
|
|
|
_SHA512_SHA512_METHODDEF
|
|
|
|
_SHA512_SHA384_METHODDEF
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* Sentinel */
|
2005-08-21 15:45:59 -03:00
|
|
|
};
|
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
static int
|
|
|
|
_sha512_traverse(PyObject *module, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
SHA512State *state = sha512_get_state(module);
|
|
|
|
Py_VISIT(state->sha384_type);
|
|
|
|
Py_VISIT(state->sha512_type);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
static int
|
|
|
|
_sha512_clear(PyObject *module)
|
|
|
|
{
|
|
|
|
SHA512State *state = sha512_get_state(module);
|
|
|
|
Py_CLEAR(state->sha384_type);
|
|
|
|
Py_CLEAR(state->sha512_type);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
static void
|
|
|
|
_sha512_free(void *module)
|
|
|
|
{
|
|
|
|
_sha512_clear((PyObject *)module);
|
|
|
|
}
|
2008-06-11 02:26:20 -03:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
|
|
|
|
/* Initialize this module. */
|
|
|
|
static int
|
|
|
|
_sha512_exec(PyObject *m)
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2020-09-06 07:09:51 -03:00
|
|
|
SHA512State* st = sha512_get_state(m);
|
2013-10-22 10:05:23 -03:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
st->sha384_type = (PyTypeObject *)PyType_FromModuleAndSpec(
|
|
|
|
m, &sha512_sha384_type_spec, NULL);
|
|
|
|
|
|
|
|
st->sha512_type = (PyTypeObject *)PyType_FromModuleAndSpec(
|
|
|
|
m, &sha512_sha512_type_spec, NULL);
|
|
|
|
|
|
|
|
if (st->sha384_type == NULL || st->sha512_type == NULL) {
|
|
|
|
return -1;
|
2020-02-07 04:17:07 -04:00
|
|
|
}
|
2020-09-06 07:09:51 -03:00
|
|
|
|
|
|
|
Py_INCREF(st->sha384_type);
|
|
|
|
if (PyModule_AddObject(m, "SHA384Type", (PyObject *)st->sha384_type) < 0) {
|
|
|
|
Py_DECREF(st->sha384_type);
|
|
|
|
return -1;
|
2020-02-07 04:17:07 -04:00
|
|
|
}
|
2013-10-22 10:05:23 -03:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
Py_INCREF(st->sha512_type);
|
|
|
|
if (PyModule_AddObject(m, "SHA384Type", (PyObject *)st->sha512_type) < 0) {
|
|
|
|
Py_DECREF(st->sha512_type);
|
|
|
|
return -1;
|
2020-02-07 04:17:07 -04:00
|
|
|
}
|
2013-10-22 10:05:23 -03:00
|
|
|
|
2020-09-06 07:09:51 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyModuleDef_Slot _sha512_slots[] = {
|
|
|
|
{Py_mod_exec, _sha512_exec},
|
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct PyModuleDef _sha512module = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
.m_name = "_sha512",
|
|
|
|
.m_size = sizeof(SHA512State),
|
|
|
|
.m_methods = SHA_functions,
|
|
|
|
.m_slots = _sha512_slots,
|
|
|
|
.m_traverse = _sha512_traverse,
|
|
|
|
.m_clear = _sha512_clear,
|
|
|
|
.m_free = _sha512_free
|
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__sha512(void)
|
|
|
|
{
|
|
|
|
return PyModuleDef_Init(&_sha512module);
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|