summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2014-06-21 16:18:38 -0700
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2014-06-21 16:18:38 -0700
commitf7916cb7c797a26140e50a7bd07a5716c25bf85c (patch)
tree1d2869051792bfa568dda706714a90d90262b999
parent43f245e54a1640b149d4fa240127058e1ff38cff (diff)
More cleanup and adding cyphers.
-rw-r--r--includes/BCypher.h79
-rw-r--r--includes/BHashes.h31
-rw-r--r--win32/project/Balau.vcxproj1
-rw-r--r--win32/project/Balau.vcxproj.filters3
4 files changed, 98 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;
};
diff --git a/win32/project/Balau.vcxproj b/win32/project/Balau.vcxproj
index 5cc49d5..2f220fc 100644
--- a/win32/project/Balau.vcxproj
+++ b/win32/project/Balau.vcxproj
@@ -267,6 +267,7 @@
<ClInclude Include="..\..\includes\Async.h" />
<ClInclude Include="..\..\includes\AtStartExit.h" />
<ClInclude Include="..\..\includes\Base64.h" />
+ <ClInclude Include="..\..\includes\BCypher.h" />
<ClInclude Include="..\..\includes\BHashes.h" />
<ClInclude Include="..\..\includes\BigInt.h" />
<ClInclude Include="..\..\includes\BLua.h" />
diff --git a/win32/project/Balau.vcxproj.filters b/win32/project/Balau.vcxproj.filters
index 98b3ffd..cbaa322 100644
--- a/win32/project/Balau.vcxproj.filters
+++ b/win32/project/Balau.vcxproj.filters
@@ -335,6 +335,9 @@
<ClInclude Include="..\..\includes\BHashes.h">
<Filter>Headers</Filter>
</ClInclude>
+ <ClInclude Include="..\..\includes\BCypher.h">
+ <Filter>Headers</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\jsoncpp\src\json_internalarray.inl">