Sleds/gluster_status/Hash.h

86 lines
3.0 KiB
C

/*
Modifications copyright (c) 2013 IOnU Security Inc. All rights reserved
Added to IonU source repository in August 2013 by Kendrick Webster,
based on public-domain code by Kendrick Webster.
-----------------------------------------------------------------------------
Simple chaos-based hash/PRNG and stream cypher 1991 - 2013
Written by Ken Webster <ken@kenwebster.org>
[ other contributers can add their names here ]
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
See the CC0 Public Domain Dedication for details:
<http://creativecommons.org/publicdomain/zero/1.0/>
-----------------------------------------------------------------------------
This is a small-footprint hasher and PRNG that incrementally hashes input
bytes into an evolving chaotic state pool from which pseudo random output
bytes are extracted. The output passes all dieharder tests.
---------------------------------------------------------------------------*/
#pragma once
#include <stdint.h> /* for uint8_t, uint16_t, and uint32_t */
#if defined(__cplusplus)
extern "C" {
#endif
/*
state pool size in words (two octets each)
Code is most efficient when the size is a power of two.
Sizes below 16 may produce poor randomness (check with dieharder).
64 words is a reasonable state pool size for most applications.
*/
enum {SC_HASH_BUFFER_WORDS = 64};
/*
hash state
memset() to zero to initialize
*/
typedef struct
{
uint16_t a[SC_HASH_BUFFER_WORDS];
uint32_t b;
unsigned int i;
}
sc_hash_state_t;
/*
hash function
Each call to this function perturbs the pool's state with the supplied
input byte <x>, chaotically evolves the pool's state, and returns a byte
of output derived from the pool's state.
This function can be repeatedly called with <x> = 0 (or any constant) to
get a stream of pseudo-random output bytes of arbitrary length.
*/
uint8_t sc_hash(sc_hash_state_t* p, uint8_t x);
/* hash a vector <v> of length <len> into pool state <p> */
void sc_hash_vector(sc_hash_state_t* p, const uint8_t* v, unsigned int len);
/* extract a vector <v> of length <len> from pool state <p> */
void sc_get_vector(sc_hash_state_t* p, uint8_t* v, unsigned int len);
/*
hash a vector <v> of length <len> into pool state <p>,
return new <v> (buffer contents are replaced)
*/
void sc_hash_vector_replace(sc_hash_state_t* p, uint8_t* v, unsigned int len);
/* mix pool state <p> by hashing a sequence of <n> NUL (0) bytes */
void sc_hash_mix(sc_hash_state_t* p, unsigned int n);
/* use pool state <p> as key for stream cypher encrypting vector <v> of length <len> */
void sc_hash_encrypt(sc_hash_state_t* p, uint8_t* v, unsigned int len);
/* use pool state <p> as key for stream cypher decrypting vector <v> of length <len> */
void sc_hash_decrypt(sc_hash_state_t* p, uint8_t* v, unsigned int len);
#if defined(__cplusplus)
}
#endif