/* 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. ---------------------------------------------------------------------------*/ #include "Hash.h" #define ror16(w) (((w) >> 1) | (((w) & 1) ? 0x8000 : 0)) #define rol32(d) (((d) << 1) | (((d) & 0x80000000) ? 1 : 0)) uint8_t sc_hash(sc_hash_state_t* p, uint8_t x) { uint16_t w; p->b += x; w = p->a[p->i]; w ^= (uint16_t)(p->b & 0xFFFF); w = ror16(w); w = (w + 0x5ca3) & 0xFFFF; p->a[p->i] = w; p->b += w; p->b = rol32(p->b); p->i = (p->i + 1) % SC_HASH_BUFFER_WORDS; return (uint8_t)(w & 0xFF); } void sc_hash_vector(sc_hash_state_t* p, const uint8_t* v, unsigned int len) { while (len--) { sc_hash(p, *(v++)); } } void sc_get_vector(sc_hash_state_t* p, uint8_t* v, unsigned int len) { while (len--) { *(v++) = sc_hash(p, 0); } } void sc_hash_vector_replace(sc_hash_state_t* p, uint8_t* v, unsigned int len) { while (len--) { *v = sc_hash(p, *v); ++v; } } void sc_hash_mix(sc_hash_state_t* p, unsigned int n) { while (n--) { sc_hash(p, 0); } } void sc_hash_encrypt(sc_hash_state_t* p, uint8_t* v, unsigned int len) { uint8_t c, d = sc_hash(p, 0); while (len--) { c = sc_hash(p, *v); *v ^= d; d = c; ++v; } } void sc_hash_decrypt(sc_hash_state_t* p, uint8_t* v, unsigned int len) { uint8_t d = sc_hash(p, 0); while (len--) { *v ^= d; d = sc_hash(p, *v); ++v; } }