From eb47cce9ad176d191a89c526532a8fbdb8c3637e Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Sat, 5 Apr 2014 14:33:25 +0200 Subject: [PATCH] symkey: primitive for symmetric encryption --- src/serialization.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++ src/symkey.cpp | 31 +++++++++++++++ src/symkey.h | 48 ++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 src/symkey.cpp create mode 100644 src/symkey.h diff --git a/src/serialization.cpp b/src/serialization.cpp index 3280d5a..ae08b43 100644 --- a/src/serialization.cpp +++ b/src/serialization.cpp @@ -29,6 +29,7 @@ #include "fmtseq.h" #include "message.h" #include "hashfile.h" +#include "symkey.h" static sencode* serialize_uint_vector (std::vector*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::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::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 (s); + if (!L) return false; + if (L->items.size() != 5) return false; + + sencode_bytes*ID; + + ID = dynamic_cast (L->items[0]); + if (!ID) return false; + if (ID->b != SYMKEY_IDENT) return false; + + sencode_int*bs = dynamic_cast (L->items[3]); + if (!bs) return false; + blocksize = bs->i; + + sencode_bytes*B; + + B = dynamic_cast (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 (L->items[1]); + if (!LL) return false; + ciphers.clear(); + for (i = 0; i < LL->items.size(); ++i) { + B = dynamic_cast (LL->items[i]); + if (!B) return false; + if (ciphers.count (B->b) ) return false; + ciphers.insert (B->b); + } + + LL = dynamic_cast (L->items[2]); + if (!LL) return false; + hashes.clear(); + for (i = 0; i < LL->items.size(); ++i) { + B = dynamic_cast (LL->items[i]); + if (!B) return false; + hashes.push_back (B->b); + } + + return true; +} diff --git a/src/symkey.cpp b/src/symkey.cpp new file mode 100644 index 0000000..336dbcf --- /dev/null +++ b/src/symkey.cpp @@ -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 . + */ + +#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; +} diff --git a/src/symkey.h b/src/symkey.h new file mode 100644 index 0000000..f0e960c --- /dev/null +++ b/src/symkey.h @@ -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 . + */ + +#ifndef _ccr_symkey_h_ +#define _ccr_symkey_h_ + +#include +#include +#include +#include +#include + +#include "types.h" +#include "sencode.h" + +class symkey +{ +public: + std::set ciphers; + std::list hashes; + + uint blocksize; + + std::vector seed; + + sencode* serialize(); + bool unserialize (sencode*); + + bool encrypt (std::istream&, std::ostream&); + int decrypt (std::istream&, std::ostream&); +}; + +#endif