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

symkey: primitive for symmetric encryption

This commit is contained in:
Mirek Kratochvil 2014-04-05 14:33:25 +02:00
parent 958e878fd6
commit eb47cce9ad
3 changed files with 172 additions and 0 deletions

@ -29,6 +29,7 @@
#include "fmtseq.h"
#include "message.h"
#include "hashfile.h"
#include "symkey.h"
static sencode* serialize_uint_vector (std::vector<uint>*v)
{
@ -727,3 +728,95 @@ bool hashfile::unserialize (sencode*s)
return true;
}
/*
* Symmetric key structure:
*
* ( CCR-SYMKEY
* ( streamcipher1 streamcipher2 )
* ( hash1 hash2 hash3 )
* int_blocksize
* seed_data
* )
*/
#define SYMKEY_IDENT "CCR-SYMKEY"
sencode* symkey::serialize()
{
int k;
sencode_list*L = new sencode_list(), *LL;
L->items.resize (5);
L->items[0] = new sencode_bytes (SYMKEY_IDENT);
L->items[3] = new sencode_int (blocksize);
L->items[4] = new sencode_bytes (seed);
LL = new sencode_list();
LL->items.resize (ciphers.size() );
k = 0;
for (std::set<std::string>::iterator
i = ciphers.begin(), e = ciphers.end();
i != e; ++i)
LL->items[k++] = new sencode_bytes (*i);
L->items[1] = LL;
LL = new sencode_list();
LL->items.resize (hashes.size() );
k = 0;
for (std::list<std::string>::iterator
i = hashes.begin(), e = hashes.end();
i != e; ++i)
LL->items[k++] = new sencode_bytes (*i);
L->items[2] = LL;
return L;
}
bool symkey::unserialize (sencode*s)
{
sencode_list*L = dynamic_cast<sencode_list*> (s);
if (!L) return false;
if (L->items.size() != 5) return false;
sencode_bytes*ID;
ID = dynamic_cast<sencode_bytes*> (L->items[0]);
if (!ID) return false;
if (ID->b != SYMKEY_IDENT) return false;
sencode_int*bs = dynamic_cast<sencode_int*> (L->items[3]);
if (!bs) return false;
blocksize = bs->i;
sencode_bytes*B;
B = dynamic_cast<sencode_bytes*> (L->items[4]);
if (!B) return false;
seed.clear();
seed.insert (seed.begin(), B->b.begin(), B->b.end() );
sencode_list*LL;
uint i;
LL = dynamic_cast<sencode_list*> (L->items[1]);
if (!LL) return false;
ciphers.clear();
for (i = 0; i < LL->items.size(); ++i) {
B = dynamic_cast<sencode_bytes*> (LL->items[i]);
if (!B) return false;
if (ciphers.count (B->b) ) return false;
ciphers.insert (B->b);
}
LL = dynamic_cast<sencode_list*> (L->items[2]);
if (!LL) return false;
hashes.clear();
for (i = 0; i < LL->items.size(); ++i) {
B = dynamic_cast<sencode_bytes*> (LL->items[i]);
if (!B) return false;
hashes.push_back (B->b);
}
return true;
}

31
src/symkey.cpp Normal file

@ -0,0 +1,31 @@
/*
* 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 "symkey.h"
bool symkey::encrypt (std::istream&in, std::ostream&out)
{
return false;
}
int symkey::decrypt (std::istream&in, std::ostream&out)
{
return 1;
}

48
src/symkey.h Normal file

@ -0,0 +1,48 @@
/*
* 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_symkey_h_
#define _ccr_symkey_h_
#include <iostream>
#include <string>
#include <list>
#include <set>
#include <vector>
#include "types.h"
#include "sencode.h"
class symkey
{
public:
std::set<std::string> ciphers;
std::list<std::string> hashes;
uint blocksize;
std::vector<byte> seed;
sencode* serialize();
bool unserialize (sencode*);
bool encrypt (std::istream&, std::ostream&);
int decrypt (std::istream&, std::ostream&);
};
#endif