/* 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 [ 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: ----------------------------------------------------------------------------- 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 /* 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 , 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 = 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 of length into pool state

*/ void sc_hash_vector(sc_hash_state_t* p, const uint8_t* v, unsigned int len); /* extract a vector of length from pool state

*/ void sc_get_vector(sc_hash_state_t* p, uint8_t* v, unsigned int len); /* hash a vector of length into pool state

, return new (buffer contents are replaced) */ void sc_hash_vector_replace(sc_hash_state_t* p, uint8_t* v, unsigned int len); /* mix pool state

by hashing a sequence of NUL (0) bytes */ void sc_hash_mix(sc_hash_state_t* p, unsigned int n); /* use pool state

as key for stream cypher encrypting vector of length */ void sc_hash_encrypt(sc_hash_state_t* p, uint8_t* v, unsigned int len); /* use pool state

as key for stream cypher decrypting vector of length */ void sc_hash_decrypt(sc_hash_state_t* p, uint8_t* v, unsigned int len); #if defined(__cplusplus) } #endif