Ejemplo n.º 1
0
static PyObject *
py_sha3_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
    SHA3object *self = NULL;
    Py_buffer buf = {NULL, NULL};
    HashReturn res;
    PyObject *data = NULL;

    if (!_PyArg_NoKeywords(_PyType_Name(type), kwargs)) {
        return NULL;
    }
    if (!PyArg_UnpackTuple(args, _PyType_Name(type), 0, 1, &data)) {
        return NULL;
    }

    self = newSHA3object(type);
    if (self == NULL) {
        goto error;
    }

    if (type == &SHA3_224type) {
        res = Keccak_HashInitialize_SHA3_224(&self->hash_state);
    } else if (type == &SHA3_256type) {
        res = Keccak_HashInitialize_SHA3_256(&self->hash_state);
    } else if (type == &SHA3_384type) {
        res = Keccak_HashInitialize_SHA3_384(&self->hash_state);
    } else if (type == &SHA3_512type) {
        res = Keccak_HashInitialize_SHA3_512(&self->hash_state);
#ifdef PY_WITH_KECCAK
    } else if (type == &Keccak_224type) {
        res = Keccak_HashInitialize(&self->hash_state, 1152, 448, 224, 0x01);
    } else if (type == &Keccak_256type) {
        res = Keccak_HashInitialize(&self->hash_state, 1088, 512, 256, 0x01);
    } else if (type == &Keccak_384type) {
        res = Keccak_HashInitialize(&self->hash_state, 832, 768, 384, 0x01);
    } else if (type == &Keccak_512type) {
        res = Keccak_HashInitialize(&self->hash_state, 576, 1024, 512, 0x01);
#endif
    } else if (type == &SHAKE128type) {
        res = Keccak_HashInitialize_SHAKE128(&self->hash_state);
    } else if (type == &SHAKE256type) {
        res = Keccak_HashInitialize_SHAKE256(&self->hash_state);
    } else {
        PyErr_BadInternalCall();
        goto error;
    }

    if (data) {
        GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error);
        if (buf.len >= HASHLIB_GIL_MINSIZE) {
            /* invariant: New objects can't be accessed by other code yet,
             * thus it's safe to release the GIL without locking the object.
             */
            Py_BEGIN_ALLOW_THREADS
            res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
            Py_END_ALLOW_THREADS
        }
        else {
Ejemplo n.º 2
0
static PyObject *
py_sha3_new_impl(PyTypeObject *type, PyObject *data)
/*[clinic end generated code: output=8d5c34279e69bf09 input=d7c582b950a858b6]*/
{
    SHA3object *self = NULL;
    Py_buffer buf = {NULL, NULL};
    HashReturn res;

    self = newSHA3object(type);
    if (self == NULL) {
        goto error;
    }

    if (type == &SHA3_224type) {
        res = Keccak_HashInitialize_SHA3_224(&self->hash_state);
    } else if (type == &SHA3_256type) {
        res = Keccak_HashInitialize_SHA3_256(&self->hash_state);
    } else if (type == &SHA3_384type) {
        res = Keccak_HashInitialize_SHA3_384(&self->hash_state);
    } else if (type == &SHA3_512type) {
        res = Keccak_HashInitialize_SHA3_512(&self->hash_state);
#ifdef PY_WITH_KECCAK
    } else if (type == &Keccak_224type) {
        res = Keccak_HashInitialize(&self->hash_state, 1152, 448, 224, 0x01);
    } else if (type == &Keccak_256type) {
        res = Keccak_HashInitialize(&self->hash_state, 1088, 512, 256, 0x01);
    } else if (type == &Keccak_384type) {
        res = Keccak_HashInitialize(&self->hash_state, 832, 768, 384, 0x01);
    } else if (type == &Keccak_512type) {
        res = Keccak_HashInitialize(&self->hash_state, 576, 1024, 512, 0x01);
#endif
    } else if (type == &SHAKE128type) {
        res = Keccak_HashInitialize_SHAKE128(&self->hash_state);
    } else if (type == &SHAKE256type) {
        res = Keccak_HashInitialize_SHAKE256(&self->hash_state);
    } else {
        PyErr_BadInternalCall();
        goto error;
    }

    if (data) {
        GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error);
#ifdef WITH_THREAD
        if (buf.len >= HASHLIB_GIL_MINSIZE) {
            /* invariant: New objects can't be accessed by other code yet,
             * thus it's safe to release the GIL without locking the object.
             */
            Py_BEGIN_ALLOW_THREADS
            res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
            Py_END_ALLOW_THREADS
        }
        else {
Ejemplo n.º 3
0
unsigned char *SHA3_320(const unsigned char *dataIn, size_t nBytesIn, unsigned char *md)
{
    Keccak_HashInstance h;
    static BitSequence  m[SHA3_320_DL];

    if (md == NULL) {
        md = m;
    }
    Keccak_HashInitialize(&h, SHA3_320_R, SHA3_320_C, SHA3_320_L, SHA3_320_P);
    Keccak_HashUpdate(&h, dataIn, (DataLength)nBytesIn * 8);
    Keccak_HashFinal(&h, md);

    return(md);
}
Ejemplo n.º 4
0
static int crypt_all(int *pcount, struct db_salt *salt)
{
	const int count = *pcount;
	int index = 0;
#ifdef _OPENMP
#pragma omp parallel for
#endif
	for (index = 0; index < count; index++)
	{
		Keccak_HashInstance hash;
		Keccak_HashInitialize(&hash, 1088, 512, 256, 0x01);
		Keccak_HashUpdate(&hash, (unsigned char*)saved_key[index], saved_len[index] * 8);
		Keccak_HashFinal(&hash, (unsigned char*)crypt_out[index]);
	}
	return count;
}
Ejemplo n.º 5
0
int SHAKE80(const unsigned char *dataIn, size_t nBitsIn, unsigned char *md, int nOutBytes)
{
    Keccak_HashInstance h;

    if (md == NULL || nOutBytes == 0) {
        return 0;
    }
    if (nOutBytes > SHAKE_MAX_BITS / 8) {
        nOutBytes = SHAKE_MAX_BITS / 8;
    }
    Keccak_HashInitialize(&h, SHAKE80_R, SHAKE80_C, 0, SHAKE80_P);
    Keccak_HashUpdate(&h, dataIn, (DataLength)nBitsIn);
    Keccak_HashFinal(&h, NULL);
    Keccak_HashSqueeze(&h, md, nOutBytes * 8);

    return nOutBytes;
}