From fa955d817087b773c95b5b57086a550574e26763 Mon Sep 17 00:00:00 2001 From: TLINDEN Date: Mon, 2 Dec 2013 22:53:03 +0100 Subject: [PATCH] re-organized c++ header(s), added/fixed encryption support, streamlined the api --- bindings/cpp/Makefile.am | 2 +- bindings/cpp/Makefile.in | 5 +- bindings/cpp/crypto++.h | 61 +++++++++++++++ bindings/cpp/crypto.cpp | 152 ++++++++++++++++++++++++++++++++++++ bindings/cpp/helpers++.h | 69 ++++++++++++++++ bindings/cpp/key++.h | 118 ++++++++++++++++++++++++++++ bindings/cpp/key.cpp | 121 ++++++---------------------- bindings/cpp/pcp++.h | 165 +-------------------------------------- bindings/cpp/vault++.h | 77 ++++++++++++++++++ 9 files changed, 511 insertions(+), 259 deletions(-) create mode 100644 bindings/cpp/crypto++.h create mode 100644 bindings/cpp/crypto.cpp create mode 100644 bindings/cpp/helpers++.h create mode 100644 bindings/cpp/key++.h create mode 100644 bindings/cpp/vault++.h diff --git a/bindings/cpp/Makefile.am b/bindings/cpp/Makefile.am index dd21d05..bcb9240 100644 --- a/bindings/cpp/Makefile.am +++ b/bindings/cpp/Makefile.am @@ -22,5 +22,5 @@ AM_CXXFLAGS = -I../../include -Wall -g lib_LTLIBRARIES = libpcp1++.la -libpcp1___la_SOURCES = pcp++.h key.cpp vault.cpp +libpcp1___la_SOURCES = pcp++.h key.cpp vault.cpp crypto.cpp include_HEADERS = pcp++.h diff --git a/bindings/cpp/Makefile.in b/bindings/cpp/Makefile.in index b67b279..5c2828b 100644 --- a/bindings/cpp/Makefile.in +++ b/bindings/cpp/Makefile.in @@ -96,7 +96,7 @@ am__uninstall_files_from_dir = { \ am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(includedir)" LTLIBRARIES = $(lib_LTLIBRARIES) libpcp1___la_LIBADD = -am_libpcp1___la_OBJECTS = key.lo vault.lo +am_libpcp1___la_OBJECTS = key.lo vault.lo crypto.lo libpcp1___la_OBJECTS = $(am_libpcp1___la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/pcp depcomp = $(SHELL) $(top_srcdir)/config/depcomp @@ -272,7 +272,7 @@ top_srcdir = @top_srcdir@ # AM_CXXFLAGS = -I../../include -Wall -g lib_LTLIBRARIES = libpcp1++.la -libpcp1___la_SOURCES = pcp++.h key.cpp vault.cpp +libpcp1___la_SOURCES = pcp++.h key.cpp vault.cpp crypto.cpp include_HEADERS = pcp++.h all: all-am @@ -351,6 +351,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/crypto.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/key.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vault.Plo@am__quote@ diff --git a/bindings/cpp/crypto++.h b/bindings/cpp/crypto++.h new file mode 100644 index 0000000..92b8f0a --- /dev/null +++ b/bindings/cpp/crypto++.h @@ -0,0 +1,61 @@ +/* + This file is part of Pretty Curved Privacy (pcp1). + + Copyright (C) 2013 T.Linden. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + You can contact me by mail: . +*/ + + +#ifndef _HAVE_PCPPP_CRYPTO_H +#define _HAVE_PCPPP_CRYPTO_H + +#include +#include +#include + +#include "vault++.h" +#include "key++.h" +#include "helpers++.h" + +namespace pcp { + + class Crypto { + private: + bool havevault; + + public: + PubKey P; + Key S; + Vault vault; + + // constructors + Crypto(Key &skey, PubKey &pkey); + Crypto(Vault &v, Key &skey, PubKey &pkey); + + // PK encryption methods + // sender pubkey is P + std::string encrypt(std::vector message); + std::string encrypt(std::string message); + std::string encrypt(unsigned char *message, size_t mlen); + + // decrypt using P or use vault if defined + ResultSet decrypt(std::string cipher); + }; +}; + + +#endif // _HAVE_PCPPP_CRYPTO_H diff --git a/bindings/cpp/crypto.cpp b/bindings/cpp/crypto.cpp new file mode 100644 index 0000000..ab815d0 --- /dev/null +++ b/bindings/cpp/crypto.cpp @@ -0,0 +1,152 @@ +/* + This file is part of Pretty Curved Privacy (pcp1). + + Copyright (C) 2013 T.Linden. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + You can contact me by mail: . +*/ + +#include "crypto++.h" + + +using namespace std; +using namespace pcp; + +Crypto::Crypto(Key &skey, PubKey &pkey) { + P = pkey; + S = skey; + havevault = false; +} + +Crypto::Crypto(Vault &v, Key &skey, PubKey &pkey) { + P = pkey; + S = skey; + vault = v; + havevault = true; +} + +string Crypto::encrypt(string message) { + unsigned char *m = (unsigned char *)ucmalloc(message.size() + 1); + memcpy(m, message.c_str(), message.size()); + return Crypto::encrypt(m, message.size() + 1); +} + +string Crypto::encrypt(vector message) { + unsigned char *m = (unsigned char *)ucmalloc(message.size()); + for(size_t i=0; iid, 16); + memcpy(combined, hash, crypto_hash_BYTES); + memcpy(&combined[crypto_hash_BYTES], cipher, clen); + + // combined consists of: + // keyid|nonce|cipher + char *encoded = pcp_z85_encode(combined, rlen, &zlen); + + if(encoded == NULL) + throw exception(); + + return string((char *)encoded); +} + +ResultSet Crypto::decrypt(string cipher) { + size_t clen; + unsigned char *combined = pcp_z85_decode((char *)cipher.c_str(), &clen); + + if(combined == NULL) + throw exception(); + + unsigned char *encrypted = (unsigned char*)ucmalloc(clen - crypto_hash_BYTES); + unsigned char *hash = (unsigned char*)ucmalloc(crypto_hash_BYTES); + unsigned char *check = (unsigned char*)ucmalloc(crypto_hash_BYTES); + + memcpy(hash, combined, crypto_hash_BYTES); + memcpy(encrypted, &combined[crypto_hash_BYTES], clen - crypto_hash_BYTES); + + PubKey sender; + crypto_hash(check, (unsigned char*)P.K->id, 16); + + if(memcmp(check, hash, crypto_hash_BYTES) != 0) { + if(havevault) { + PubKeyMap pmap = vault.pubkeys(); + for(PubKeyIterator it=pmap.begin(); it != pmap.end(); ++it) { + crypto_hash(check, (unsigned char*)it->first.c_str(), 16); + if(memcmp(check, hash, crypto_hash_BYTES) == 0) { + sender = it->second; + break; + } + } + } + } + else { + sender = P; + } + + if(!sender) { + free(combined); + free(hash); + free(check); + free(encrypted); + throw exception("No public key usable for decryption found!"); + } + + size_t dlen; + unsigned char *decrypted = (unsigned char*)pcp_box_decrypt(S.K, sender.K, + encrypted, + clen - crypto_hash_BYTES, &dlen); + + if(decrypted == NULL) { + free(combined); + free(hash); + free(check); + free(encrypted); + throw exception(); + } + + ResultSet r; + r.Uchar = decrypted; + r.String = string((char *)decrypted); + r.Size = dlen; + + for(size_t i=0; i. + + You can contact me by mail: . +*/ + + +#ifndef _HAVE_PCPPP_HELPERS_H +#define _HAVE_PCPPP_HELPERS_H + +#include +#include +#include +#include +#include +#include + +namespace pcp { + + class exception : public std::runtime_error { + private: + std::string getfatals() { + std::string msg; + if(PCP_ERRSET == 1) { + msg = PCP_ERR; + } + if(errno) { + msg += std::string("\nError: ") + + std::string(strerror(errno)) + + std::string("\n"); + } + return msg; + } + public: + exception(const std::string & msg) : runtime_error(msg) { } + exception() : runtime_error(getfatals()) { } + }; + + + + class ResultSet { + public: + std::string String; + std::vector Vector; + unsigned char *Uchar; + size_t Size; + + ~ResultSet() { free(Uchar); } + }; + +}; + + +#endif // _HAVE_PCPPP_HELPERS_H diff --git a/bindings/cpp/key++.h b/bindings/cpp/key++.h new file mode 100644 index 0000000..848edea --- /dev/null +++ b/bindings/cpp/key++.h @@ -0,0 +1,118 @@ +/* + This file is part of Pretty Curved Privacy (pcp1). + + Copyright (C) 2013 T.Linden. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + You can contact me by mail: . +*/ + + +#ifndef _HAVE_PCPPP_KEY_H +#define _HAVE_PCPPP_KEY_H + +#include +#include +#include +#include + +#include "helpers++.h" + +namespace pcp { + + class PubKey { + private: + bool stored; + + public: + pcp_pubkey_t *K; + + // constructors + PubKey(); + PubKey(pcp_pubkey_t *k); + PubKey(pcp_pubkey_t *k, bool store); + PubKey(std::string &z85encoded); + + // destructors + ~PubKey(); + + // operators + PubKey& operator = (const PubKey &k); + + std::string get_id(); + std::string get_owner(); + std::string get_mail(); + + void is_stored(bool s); + bool is_stored(); + + std::string to_text(); + }; + + bool operator!(PubKey& k); + std::ostream& operator<<(std::ostream& output, PubKey& k); + + + + class Key { + private: + bool stored; + + public: + // make access to the underlying struct easier + pcp_key_t *K; + + // constructors + Key(); + Key(bool generate); + Key(const std::string& passphrase); + Key(const std::string& passphrase, + const std::string& owner, + const std::string& mail); + Key(pcp_key_t *k); + Key(pcp_key_t *k, bool store); + Key(std::string &z85encoded); + + // destructor + ~Key(); + + // operators + Key& operator = (const Key &k); + + // methods + void encrypt(const std::string& passphrase); + void decrypt(const std::string& passphrase); + PubKey get_public(); + std::string get_id(); + std::string get_owner(); + std::string get_mail(); + + void set_owner(const std::string& owner); + void set_mail(const std::string& mail); + void is_stored(bool s); + bool is_stored(); + bool is_encrypted(); + bool is_primary(); + + std::string to_text(); + }; + + // << and >> operators + bool operator!(Key& k); + std::ostream& operator<<(std::ostream& output, Key& k); +}; + + +#endif // _HAVE_PCPPP_KEY_H diff --git a/bindings/cpp/key.cpp b/bindings/cpp/key.cpp index 6dc8ee7..a9c0ee8 100644 --- a/bindings/cpp/key.cpp +++ b/bindings/cpp/key.cpp @@ -19,7 +19,8 @@ You can contact me by mail: . */ -#include "pcp++.h" +#include "vault++.h" +#include "key++.h" using namespace std; using namespace pcp; @@ -166,19 +167,18 @@ string Key::to_text() { return z85; } -istream& pcp::operator>>(istream& input, Key& k) { - string z85; - input >> z85; - Key t = new Key(z85); // use the import constructor, FIXME: use a method - k.K = t.K; - return input; -} - ostream& pcp::operator<<(ostream& output, Key& k) { output << k.to_text(); return output; } +bool pcp::operator!(Key& k) { + if(k.K == NULL) + return true; + else + return false; +} + void Key::encrypt(const string& passphrase) { K = pcpkey_encrypt(K, (char *)passphrase.c_str()); if(PCP_ERRSET == 1) @@ -233,81 +233,7 @@ bool Key::is_encrypted() { return false; } -string Key::encrypt(PubKey &recipient, string message) { - unsigned char *m = (unsigned char *)ucmalloc(message.size() + 1); - memcpy(m, message.c_str(), message.size()); - return Key::encrypt(recipient, m, message.size() + 1); -} - -string Key::encrypt(PubKey &recipient, vector message) { - unsigned char *m = (unsigned char *)ucmalloc(message.size()); - for(size_t i=0; iid, 16); - memcpy(combined, hash, crypto_hash_BYTES); - memcpy(&combined[crypto_hash_BYTES], cipher, clen); - - // combined consists of: - // keyid|nonce|cipher - char *encoded = pcp_z85_encode(combined, rlen, &zlen); - - if(encoded == NULL) - throw exception(); - - return string((char *)encoded); -} - -ResultSet Key::decrypt(PubKey &sender, std::string cipher) { - - size_t clen; - unsigned char *combined = pcp_z85_decode((char *)cipher.c_str(), &clen); - - if(combined == NULL) - throw exception(); - - unsigned char *encrypted = (unsigned char*)ucmalloc(clen - crypto_hash_BYTES); - memcpy(encrypted, &combined[crypto_hash_BYTES], clen - crypto_hash_BYTES); - - size_t dlen; - unsigned char *decrypted = (unsigned char*)pcp_box_decrypt(K, sender.K, - encrypted, - clen - crypto_hash_BYTES, &dlen); - - if(decrypted == NULL) { - free(combined); - throw exception(); - } - - ResultSet r; - r.Uchar = decrypted; - r.String = string((char *)decrypted); - r.Size = dlen; - - for(size_t i=0; i>(istream& input, PubKey& k) { - string z85; - input >> z85; - k = PubKey(z85); - return input; -} - - ostream& pcp::operator<<(ostream& output, PubKey& k) { output << k.to_text(); return output; } +bool pcp::operator!(PubKey& k) { + if(k.K == NULL) { + return true; + } + else { + return false; + } +} + string PubKey::get_id() { string id = K->id; return id; diff --git a/bindings/cpp/pcp++.h b/bindings/cpp/pcp++.h index fa809ae..b263c4d 100644 --- a/bindings/cpp/pcp++.h +++ b/bindings/cpp/pcp++.h @@ -31,166 +31,9 @@ #include #include - -namespace pcp { - - class exception : public std::runtime_error { - private: - std::string getfatals() { - std::string msg; - if(PCP_ERRSET == 1) { - msg = PCP_ERR; - } - if(errno) { - msg += std::string("\nError: ") + std::string(strerror(errno)) + std::string("\n"); - } - return msg; - } - public: - exception(const std::string & msg) : runtime_error(msg) { } - exception() : runtime_error(getfatals()) { } - }; - - - - class ResultSet { - public: - std::string String; - std::vector Vector; - unsigned char *Uchar; - size_t Size; - - ~ResultSet() { free(Uchar); } - }; - - - class PubKey { - private: - bool stored; - - public: - pcp_pubkey_t *K; - - // constructors - PubKey(); - PubKey(pcp_pubkey_t *k); - PubKey(pcp_pubkey_t *k, bool store); - PubKey(std::string &z85encoded); - - // destructors - ~PubKey(); - - // operators - PubKey& operator = (const PubKey &k); - - std::string get_id(); - std::string get_owner(); - std::string get_mail(); - - void is_stored(bool s); - bool is_stored(); - - std::string to_text(); - }; - - std::istream& operator>>(std::istream& input, PubKey& k); - std::ostream& operator<<(std::ostream& output, PubKey& k); - - - - class Key { - private: - bool stored; - - public: - // make access to the underlying struct easier - pcp_key_t *K; - - // constructors - Key(); - Key(bool generate); - Key(const std::string& passphrase); - Key(const std::string& passphrase, - const std::string& owner, - const std::string& mail); - Key(pcp_key_t *k); - Key(pcp_key_t *k, bool store); - Key(std::string &z85encoded); - - // destructor - ~Key(); - - // operators - Key& operator = (const Key &k); - - // methods - void encrypt(const std::string& passphrase); - void decrypt(const std::string& passphrase); - PubKey get_public(); - std::string get_id(); - std::string get_owner(); - std::string get_mail(); - - void set_owner(const std::string& owner); - void set_mail(const std::string& mail); - void is_stored(bool s); - bool is_stored(); - bool is_encrypted(); - bool is_primary(); - - std::string to_text(); - - std::string encrypt(PubKey &recipient, std::vector message); - std::string encrypt(PubKey &recipient, std::string message); - std::string encrypt(PubKey &recipient, unsigned char *message, size_t mlen); - - ResultSet decrypt(PubKey &sender, std::string cipher); - }; - - // << and >> operators - std::istream& operator>>(std::istream& input, Key& k); - std::ostream& operator<<(std::ostream& output, Key& k); - - - typedef std::map KeyMap; - typedef std::map PubKeyMap; - - typedef std::map::iterator KeyIterator; - typedef std::map::iterator PubKeyIterator; - - // the vault - class Vault { - private: - vault_t *V; - - public: - // constructors - Vault(); - Vault(std::string filename); - - // destructor - ~Vault(); - - // methods - KeyMap keys(); - PubKeyMap pubkeys(); - - bool key_exists(std::string &id); - bool pubkey_exists(std::string &id); - - int key_count(); - int pubkey_count(); - - void key_add(Key &key); - void pubkey_add(PubKey &key); - - void key_delete(std::string &id); - }; - - -}; - - - +#include "key++.h" +#include "vault++.h" +#include "crypto++.h" +#include "helpers++.h" #endif // _HAVE_PCPPP_H diff --git a/bindings/cpp/vault++.h b/bindings/cpp/vault++.h new file mode 100644 index 0000000..31b94d1 --- /dev/null +++ b/bindings/cpp/vault++.h @@ -0,0 +1,77 @@ +/* + This file is part of Pretty Curved Privacy (pcp1). + + Copyright (C) 2013 T.Linden. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + You can contact me by mail: . +*/ + + +#ifndef _HAVE_PCPPP_VAULT_H +#define _HAVE_PCPPP_VAULT_H + +#include +#include +#include +#include +#include +#include +#include + +#include "key++.h" + +namespace pcp { + + typedef std::map KeyMap; + typedef std::map PubKeyMap; + + typedef std::map::iterator KeyIterator; + typedef std::map::iterator PubKeyIterator; + + + // the vault + class Vault { + private: + vault_t *V; + + public: + // constructors + Vault(); + Vault(std::string filename); + + // destructor + ~Vault(); + + // methods + KeyMap keys(); + PubKeyMap pubkeys(); + + bool key_exists(std::string &id); + bool pubkey_exists(std::string &id); + + int key_count(); + int pubkey_count(); + + void key_add(Key &key); + void pubkey_add(PubKey &key); + + void key_delete(std::string &id); + }; + + +}; + +#endif // _HAVE_PCPPP_VAULT_H