summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
Diffstat (limited to 'includes')
-rw-r--r--includes/BCypher.h79
-rw-r--r--includes/BHashes.h31
2 files changed, 94 insertions, 16 deletions
diff --git a/includes/BCypher.h b/includes/BCypher.h
new file mode 100644
index 0000000..ffb59b0
--- /dev/null
+++ b/includes/BCypher.h
@@ -0,0 +1,79 @@
+#pragma once
+
+#include <Exceptions.h>
+#include <tomcrypt.h>
+
+namespace Balau {
+
+template<const struct ltc_prng_descriptor * desc>
+class PRNG {
+ public:
+ PRNG() { start(); }
+ ~PRNG() { done(); }
+ void reset() {
+ done();
+ start();
+ }
+ void setKey(const uint8_t * key, size_t len) {
+ int r = desc->add_entropy(key, len, &m_state);
+ IAssert(r == CRYPT_OK, "add_entropy for %s returned %i", desc->name, r);
+ r = desc->ready(&m_state);
+ IAssert(r == CRYPT_OK, "ready for %s returned %i", desc->name, r);
+ }
+ size_t generate(uint8_t * out, size_t len) {
+ size_t actualLen = desc->read(out, len, &m_state);
+ IAssert(len == actualLen, "Couldn't get enough bytes for %s", desc->name);
+ return actualLen;
+ }
+
+ template<size_t L>
+ void setKey(const char (&key)[L]) { setKey((const uint8_t *) key, L - 1); }
+ void setKey(const String & key) { setKey((const uint8_t *) key.to_charp(), key.strlen()); }
+ template<size_t L>
+ void generate(const uint8_t (&out)[L]) { generate(out, L); }
+
+ static const char * name() { return desc->name; }
+
+ private:
+ void start() {
+ int r = desc->start(&m_state);
+ IAssert(r == CRYPT_OK, "start for %s returned %i", desc->name, r);
+ }
+ void done() {
+ int r = desc->done(&m_state);
+ IAssert(r == CRYPT_OK, "done for %s returned %i", desc->name, r);
+ }
+ prng_state m_state;
+};
+
+template<class PRNG>
+class Cypher : public PRNG {
+public:
+ void process(uint8_t * buf, size_t len) {
+ while (len) {
+ if (!m_len) {
+ m_ptr = m_buffer;
+ m_len = sizeof(m_buffer);
+ generate(m_buffer, sizeof(m_buffer));
+ }
+
+ size_t chunkLen = std::min(m_len, len);
+ m_len -= chunkLen;
+ len -= chunkLen;
+
+ while (chunkLen--)
+ *buf++ ^= *m_ptr++;
+ }
+ }
+private:
+ uint8_t m_buffer[64], * m_ptr = m_buffer;
+ size_t m_len = 0;
+};
+
+typedef PRNG<&yarrow_desc> Yarrow;
+typedef PRNG<&fortuna_desc> Fortuna;
+typedef PRNG<&rc4_desc> RC4;
+typedef PRNG<&sprng_desc> SPRNG;
+typedef PRNG<&sober128_desc> Sober128;
+
+}
diff --git a/includes/BHashes.h b/includes/BHashes.h
index 6f4010c..9fcf0fd 100644
--- a/includes/BHashes.h
+++ b/includes/BHashes.h
@@ -1,6 +1,5 @@
#pragma once
-#include <array>
#include <Exceptions.h>
#include <tomcrypt.h>
@@ -73,20 +72,20 @@ class HMAC {
hmac_state m_state;
};
-class CHC : public Hash<&chc_desc> { };
-class Whirlpool : public Hash<&whirlpool_desc> { };
-class SHA512 : public Hash<&sha512_desc> { };
-class SHA384 : public Hash<&sha384_desc> { };
-class SHA256 : public Hash<&sha256_desc> { };
-class SHA224 : public Hash<&sha224_desc> { };
-class SHA1 : public Hash<&sha1_desc> { };
-class MD5 : public Hash<&md5_desc> { };
-class MD4 : public Hash<&md4_desc> { };
-class MD2 : public Hash<&md2_desc> { };
-class Tiger : public Hash<&tiger_desc> { };
-class RMD128 : public Hash<&rmd128_desc> { };
-class RMD160 : public Hash<&rmd160_desc> { };
-class RMD256 : public Hash<&rmd256_desc> { };
-class RMD320 : public Hash<&rmd320_desc> { };
+typedef Hash<&chc_desc> CHC;
+typedef Hash<&whirlpool_desc> Whirlpool;
+typedef Hash<&sha512_desc> SHA512;
+typedef Hash<&sha384_desc> SHA384;
+typedef Hash<&sha256_desc> SHA256;
+typedef Hash<&sha224_desc> SHA224;
+typedef Hash<&sha1_desc> SHA1;
+typedef Hash<&md5_desc> MD5;
+typedef Hash<&md4_desc> MD4;
+typedef Hash<&md2_desc> MD2;
+typedef Hash<&tiger_desc> Tiger;
+typedef Hash<&rmd128_desc> RMD128;
+typedef Hash<&rmd160_desc> RMD160;
+typedef Hash<&rmd256_desc> RMD256;
+typedef Hash<&rmd320_desc> RMD320;
};