Actually call on_reset callback
[lsnes.git] / include / library / skein.hpp
blobbeaa74ec3d5a4f0a62bd536aaaa394074c9aa610
1 #ifndef _library__skein__hpp__included__
2 #define _library__skein__hpp__included__
4 #include <cstdint>
5 #include <cstdlib>
6 #include <stdexcept>
8 namespace skein
10 /**
11 * Skein hash function (v1.3).
13 struct hash
15 /**
16 * Variant to use (256-bit, 512-bit, 1024-bit)
18 enum variant { PIPE_256, PIPE_512, PIPE_1024 };
19 /**
20 * Data type for piece of data.
22 enum datatype
24 T_KEY = 0,
25 T_PERSONALIZATION = 8,
26 T_PUBKEY = 12,
27 T_KEYID = 16,
28 T_NONCE = 20,
29 T_MESSAGE = 48
31 /**
32 * Create a new hash state.
34 * Parameter v: The variant to use.
35 * Parameter outbits: Number of output bits.
36 * Throws std::runtime_error: Variant is invalid.
38 hash(variant v, uint64_t outbits);
39 /**
40 * Dtor
42 ~hash() throw();
43 /**
44 * Write data to be hashed.
46 * Parameter data: The data to append.
47 * Parameter datalen: Number of bytes in data.
48 * Parameter type: The data type. Must be monotonically increasing.
49 * Throws std::runtime_error: Types not monotonic, or invalid type.
51 * Note: Data types 4 (CONFIG) and 63 (OUTPUT) are not allowed.
53 void write(const uint8_t* data, size_t datalen, datatype type = T_MESSAGE);
54 /**
55 * Read the output hash.
57 * Parameter output: Buffer to store the output to.
59 void read(uint8_t* output) throw();
60 /**
61 * Read partial output hash.
63 * Parameter output: Buffer to store the output to.
64 * Parameter startblock: The block number (each block is 256/512/1024 bits depending on variant) to start from.
65 * Parameter bits: Number of bits to output.
67 void read_partial(uint8_t* output, uint64_t startblock, uint64_t bits) throw();
68 private:
69 void typechange(uint8_t newtype);
70 void configure();
71 void flush_buffer(uint8_t type, bool final);
72 uint64_t chain[16];
73 uint8_t buffer[128];
74 void (*compress)(uint64_t* out, const uint64_t* data, const uint64_t* key, const uint64_t* tweak);
75 unsigned bufferfill;
76 unsigned fullbuffer;
77 uint64_t data_low;
78 uint64_t data_high;
79 uint64_t outbits;
80 int8_t last_type;
83 /**
84 * Skein PRNG.
86 struct prng
88 public:
89 /**
90 * Construct a PRNG.
92 * Note: To seed the PRNG, write the initial seed there.
94 prng() throw();
95 /**
96 * (Re)seed the PRNG and mark it seeded.
98 * Parameter buffer: Buffer to read the seed from.
99 * Parameter size: Number of bytes in seed.
101 void write(const void* buffer, size_t size) throw();
103 * Read data from PRNG.
105 * Parameter buffer: Buffer to write the data to.
106 * Parameter size: Number of random bytes to write.
107 * Throws std::runtime_error: Generator is not seeded.
109 void read(void* buffer, size_t size);
111 * Is seeded?
113 bool is_seeded() const throw();
114 private:
115 uint8_t state[128];
116 bool _is_seeded;
120 * Zeroize a block of memory.
122 * Parameter ptr: Pointer to start of block.
123 * Parameter size: Size of block to zeroize.
125 void zeroize(void* ptr, size_t size);
128 #endif