1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-06-27 09:18:16 +00:00

named collections for hashes and stream ciphers

also, with the funny factoryof<> and instanceof<> primitives that kindof
solve the memory trouble.
This commit is contained in:
Mirek Kratochvil 2014-04-05 23:45:01 +02:00
parent 654b2f58ef
commit 9c4287c636
6 changed files with 198 additions and 20 deletions

83
src/factoryof.h Normal file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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 base_type>
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 base_type, class derived_type = base_type> class factoryof;
template<class base_type, class derived_type>
class factoryof : public factoryof<base_type, base_type>
{
public:
base_type* get() {
return new derived_type;
}
};
template<class base_type>
class factoryof<base_type, base_type>
{
public:
virtual base_type* get() = 0;
};
#endif

43
src/hash.cpp Normal file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<hash_proc,type> 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;
}

@ -20,7 +20,10 @@
#define _ccr_hash_h_
#include <vector>
#include <string>
#include <map>
#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<byte>&) = 0;
virtual std::vector<byte> finish() = 0;
virtual ~hash_proc() {}
typedef std::map<std::string, factoryof<hash_proc>*> suite_t;
static suite_t& suite();
};
#endif

@ -21,11 +21,9 @@
#include <map>
using namespace std;
#include "rmd_hash.h"
#include "sha_hash.h"
#include "tiger_hash.h"
#include "cube_hash.h"
#include <stdint.h>
#include "hash.h"
#include "iohelpers.h"
/*
@ -63,24 +61,21 @@ class size64proc : public hash_proc
* list of hash functions available
*/
typedef map<string, hash_proc*> hashmap;
typedef map<string, instanceof<hash_proc> > 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() ) {

41
src/sc.cpp Normal file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<streamcipher,type> type##_var; \
s[name]=&type##_var;
if (s.empty() ) {
do_cipher ("ARCFOUR", arcfour_t);
do_cipher ("CHACHA20", chacha20);
do_cipher ("XSYND", xsynd);
}
return s;
}

@ -20,10 +20,13 @@
#define _ccr_sc_h_
#include "types.h"
#include "factoryof.h"
#include <sys/types.h>
#include <vector>
#include <map>
#include <string>
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<std::string, factoryof<streamcipher>*> suite_t;
static suite_t& suite();
};
#endif