diff --git a/src/factoryof.h b/src/factoryof.h new file mode 100644 index 0000000..98b764a --- /dev/null +++ b/src/factoryof.h @@ -0,0 +1,83 @@ + +/* + * This file is part of Codecrypt. + * + * Codecrypt is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Codecrypt is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Codecrypt. If not, see . + */ + +#ifndef _ccr_factoryof_h_ +#define _ccr_factoryof_h_ + +/* + * This is a generic tool for safe spawning of instances of various derived + * ciphers on the fly. + */ + +template +class instanceof +{ + base_type*ptr; + bool deletable; +public: + base_type& operator*() { + return *ptr; + } + const base_type& operator*() const { + return *ptr; + } + + base_type* operator->() { + return ptr; + } + const base_type* operator->() const { + return ptr; + } + + instanceof (base_type*p) : ptr (p), deletable (false) {} + instanceof (const instanceof&x) : ptr (x.ptr), deletable (false) {} + instanceof () { + deletable = false; + } + + void collect() { + deletable = true; + } + void forget() { + deletable = false; + } + + ~instanceof() { + if (deletable) delete ptr; + } +}; + +template class factoryof; + +template +class factoryof : public factoryof +{ +public: + base_type* get() { + return new derived_type; + } +}; + +template +class factoryof +{ +public: + virtual base_type* get() = 0; +}; + +#endif diff --git a/src/hash.cpp b/src/hash.cpp new file mode 100644 index 0000000..e62d5c6 --- /dev/null +++ b/src/hash.cpp @@ -0,0 +1,43 @@ + +/* + * This file is part of Codecrypt. + * + * Codecrypt is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Codecrypt is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Codecrypt. If not, see . + */ + +#include "hash.h" + +#include "sha_hash.h" +#include "rmd_hash.h" +#include "tiger_hash.h" +#include "cube_hash.h" + +hash_proc::suite_t& hash_proc::suite() +{ + static suite_t s; + +#define do_hash(name,type) \ + static factoryof type##_var; \ + s[name]=&type##_var; + + if (s.empty() ) { + do_hash ("CUBE512", cube512proc); + do_hash ("RIPEMD128", rmd128proc); + do_hash ("TIGER192", tiger192proc); + do_hash ("SHA256", sha256proc); + do_hash ("SHA512", sha512proc); + } + + return s; +} diff --git a/src/hash.h b/src/hash.h index ccb8298..2a49cfa 100644 --- a/src/hash.h +++ b/src/hash.h @@ -20,7 +20,10 @@ #define _ccr_hash_h_ #include +#include +#include #include "types.h" +#include "factoryof.h" /* * hash-providing functor class, meant to be instantiated by user. @@ -39,6 +42,10 @@ public: virtual void init() = 0; virtual void eat (const std::vector&) = 0; virtual std::vector finish() = 0; + virtual ~hash_proc() {} + + typedef std::map*> suite_t; + static suite_t& suite(); }; #endif diff --git a/src/hashfile.cpp b/src/hashfile.cpp index 55ba6fd..db746a1 100644 --- a/src/hashfile.cpp +++ b/src/hashfile.cpp @@ -21,11 +21,9 @@ #include using namespace std; -#include "rmd_hash.h" -#include "sha_hash.h" -#include "tiger_hash.h" -#include "cube_hash.h" +#include +#include "hash.h" #include "iohelpers.h" /* @@ -63,24 +61,21 @@ class size64proc : public hash_proc * list of hash functions available */ -typedef map hashmap; +typedef map > hashmap; void fill_hashmap (hashmap&t) { -#if HAVE_CRYPTOPP==1 - static rmd128proc rh; - t["RIPEMD128"] = &rh; - static tiger192proc th; - t["TIGER192"] = &th; - static sha256proc sh256; - t["SHA256"] = &sh256; - static sha512proc sh512; - t["SHA512"] = &sh512; -#endif //HAVE_CRYPTOPP - static cube512proc c512; - t["CUBE512"] = &c512; - static size64proc sh; - t["SIZE64"] = &sh; + //copy contents of the hash suite + for (hash_proc::suite_t::iterator + i = hash_proc::suite().begin(), + e = hash_proc::suite().end(); + i != e; ++i) { + t[i->first] = i->second->get(); + t[i->first].collect(); + } + + //add size64 check + t["SIZE64"] = new size64proc; } bool hashfile::create (istream&in) @@ -120,8 +115,10 @@ int hashfile::verify (istream&in) fill_hashmap (hm_all); for (hashes_t::iterator i = hashes.begin(), e = hashes.end(); i != e; ++i) - if (hm_all.count (i->first) ) + if (hm_all.count (i->first) ) { hm[i->first] = hm_all[i->first]; + hm_all[i->first].forget(); + } if (hm.empty() ) { diff --git a/src/sc.cpp b/src/sc.cpp new file mode 100644 index 0000000..69dacaf --- /dev/null +++ b/src/sc.cpp @@ -0,0 +1,41 @@ + +/* + * This file is part of Codecrypt. + * + * Codecrypt is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Codecrypt is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Codecrypt. If not, see . + */ + +#include "sc.h" + +#include "arcfour.h" +#include "xsynd.h" +#include "chacha.h" + +typedef arcfour<> arcfour_t; //template god demands sacrifice + +streamcipher::suite_t& streamcipher::suite() +{ + static suite_t s; +#define do_cipher(name,type) \ + static factoryof type##_var; \ + s[name]=&type##_var; + + if (s.empty() ) { + do_cipher ("ARCFOUR", arcfour_t); + do_cipher ("CHACHA20", chacha20); + do_cipher ("XSYND", xsynd); + } + + return s; +} diff --git a/src/sc.h b/src/sc.h index c7320e3..148e3a5 100644 --- a/src/sc.h +++ b/src/sc.h @@ -20,10 +20,13 @@ #define _ccr_sc_h_ #include "types.h" +#include "factoryof.h" #include #include +#include +#include class streamcipher { @@ -38,6 +41,8 @@ public: virtual size_t key_size() = 0; virtual size_t block_size() = 0; + virtual ~streamcipher() {} + void discard (size_t n) { gen (n, 0); } @@ -46,6 +51,8 @@ public: load_key (& (K[0]), & (K[K.size()]) ); } + typedef std::map*> suite_t; + static suite_t& suite(); }; #endif