Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2b7fd775b3
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 226 lines (166 sloc) 5.176 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
/* SHA3 module -- This module provides an interface to SHA-3, also
known as Keccak.

This code is based on sha256module.c in the standard Python 2
distribution, which has the following header:

---
Additional work performed by:

Andrew Kuchling (amk@amk.ca)
Greg Stein (gstein@lyra.org)
Trevor Perrin (trevp@trevp.net)

Copyright (C) 2005 Gregory P. Smith (greg@krypto.org)
Licensed to PSF under a Contributor Agreement.
---

This file, adapter for SHA-3, is written by Björn Edström <be@bjrn.se> 2012.

It is placed under the same license as the original module:
Licensed to PSF under a Contributor Agreement.
*/

#include "Python.h"
#include "structmember.h"
#include "KeccakReferenceAndOptimized/Sources/KeccakNISTInterface.h"


#define MAX_DIGEST_SIZE 64


typedef struct {
    PyObject_HEAD
    int hashbitlen;
    hashState state;
} SHAobject;


static void SHAcopy(SHAobject *src, SHAobject *dest)
{
    dest->hashbitlen = src->hashbitlen;
    memcpy(&dest->state, &src->state, sizeof(src->state));
}


static PyTypeObject SHA3type;


static SHAobject *
newSHA3object(void)
{
    return (SHAobject *)PyObject_New(SHAobject, &SHA3type);
}


static void
SHA_dealloc(PyObject *ptr)
{
    PyObject_Del(ptr);
}


PyDoc_STRVAR(SHA3_copy__doc__, "Return a copy of the hash object.");

static PyObject *
SHA3_copy(SHAobject *self, PyObject *unused)
{
    SHAobject *newobj = NULL;
    if ((newobj = newSHA3object()) == NULL) {
        return NULL;
    }
    SHAcopy(self, newobj);
    return (PyObject *)newobj;
}


PyDoc_STRVAR(SHA3_digest__doc__,
"Return the digest value as a string of binary data.");

static PyObject *
SHA3_digest(SHAobject *self, PyObject *unused)
{
    unsigned char digest[MAX_DIGEST_SIZE];
    SHAobject temp;

    SHAcopy(self, &temp);

    Final(&temp.state, digest);

    return PyString_FromStringAndSize((const char *)digest, self->hashbitlen / 8);
}


PyDoc_STRVAR(SHA3_update__doc__,
"Update this hash object's state with the provided string.");

static PyObject *
SHA3_update(SHAobject *self, PyObject *args)
{
    unsigned char *cp;
    int len;

    if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
        return NULL;

    Update(&self->state, cp, len * 8);

    Py_INCREF(Py_None);
    return Py_None;
}


PyDoc_STRVAR(SHA3_init__doc__,
"Init this hash object's state.");

static PyObject *
SHA3_init(SHAobject *self, PyObject *args)
{
    int hashbitlen;

    if (!PyArg_ParseTuple(args, "i", &hashbitlen))
        return NULL;

    self->hashbitlen = hashbitlen;
    Init(&self->state, hashbitlen);

    Py_INCREF(Py_None);
    return Py_None;
}


static PyMethodDef SHA_methods[] = {
    {"copy", (PyCFunction)SHA3_copy, METH_NOARGS, SHA3_copy__doc__},
    {"digest", (PyCFunction)SHA3_digest, METH_NOARGS, SHA3_digest__doc__},
    {"update", (PyCFunction)SHA3_update, METH_VARARGS, SHA3_update__doc__},
    {"init" , (PyCFunction)SHA3_init, METH_VARARGS, SHA3_init__doc__},
    {NULL, NULL} /* sentinel */
};


static PyGetSetDef SHA_getseters[] = {
    {NULL} /* Sentinel */
};


static PyMemberDef SHA_members[] = {
    {NULL} /* Sentinel */
};


static PyTypeObject SHA3type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "_sha3.sha3", /*tp_name*/
    sizeof(SHAobject), /*tp_size*/
    0, /*tp_itemsize*/
    /* methods */
    SHA_dealloc, /*tp_dealloc*/
    0, /*tp_print*/
    0, /*tp_getattr*/
    0, /*tp_setattr*/
    0, /*tp_compare*/
    0, /*tp_repr*/
    0, /*tp_as_number*/
    0, /*tp_as_sequence*/
    0, /*tp_as_mapping*/
    0, /*tp_hash*/
    0, /*tp_call*/
    0, /*tp_str*/
    0, /*tp_getattro*/
    0, /*tp_setattro*/
    0, /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT, /*tp_flags*/
    0, /*tp_doc*/
    0, /*tp_traverse*/
    0, /*tp_clear*/
    0, /*tp_richcompare*/
    0, /*tp_weaklistoffset*/
    0, /*tp_iter*/
    0, /*tp_iternext*/
    SHA_methods, /* tp_methods */
    SHA_members, /* tp_members */
    SHA_getseters, /* tp_getset */
};


PyDoc_STRVAR(SHA3_new__doc__,
"Return a new SHA-3 hash object.");

static PyObject *
SHA3_new(PyObject *self, PyObject *args, PyObject *kwdict)
{
    SHAobject *new;

    if ((new = newSHA3object()) == NULL)
        return NULL;

    if (PyErr_Occurred()) {
        Py_DECREF(new);
        return NULL;
    }

    return (PyObject *)new;
}


static struct PyMethodDef SHA_functions[] = {
    {"sha3", (PyCFunction)SHA3_new, METH_VARARGS|METH_KEYWORDS, SHA3_new__doc__},
    {NULL, NULL} /* Sentinel */
};


PyMODINIT_FUNC
init_sha3(void)
{
    PyObject *m;

    Py_TYPE(&SHA3type) = &PyType_Type;
    if (PyType_Ready(&SHA3type) < 0) {
        return;
    }
    m = Py_InitModule("_sha3", SHA_functions);
    if (m == NULL) {
        return;
    }
}
Something went wrong with that request. Please try again.