mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 12:00:56 +01:00
re-organized c++ header(s), added/fixed encryption support, streamlined the api
This commit is contained in:
@@ -22,5 +22,5 @@ AM_CXXFLAGS = -I../../include -Wall -g
|
|||||||
|
|
||||||
lib_LTLIBRARIES = libpcp1++.la
|
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
|
include_HEADERS = pcp++.h
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ am__uninstall_files_from_dir = { \
|
|||||||
am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(includedir)"
|
am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(includedir)"
|
||||||
LTLIBRARIES = $(lib_LTLIBRARIES)
|
LTLIBRARIES = $(lib_LTLIBRARIES)
|
||||||
libpcp1___la_LIBADD =
|
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)
|
libpcp1___la_OBJECTS = $(am_libpcp1___la_OBJECTS)
|
||||||
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/pcp
|
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/pcp
|
||||||
depcomp = $(SHELL) $(top_srcdir)/config/depcomp
|
depcomp = $(SHELL) $(top_srcdir)/config/depcomp
|
||||||
@@ -272,7 +272,7 @@ top_srcdir = @top_srcdir@
|
|||||||
#
|
#
|
||||||
AM_CXXFLAGS = -I../../include -Wall -g
|
AM_CXXFLAGS = -I../../include -Wall -g
|
||||||
lib_LTLIBRARIES = libpcp1++.la
|
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
|
include_HEADERS = pcp++.h
|
||||||
all: all-am
|
all: all-am
|
||||||
|
|
||||||
@@ -351,6 +351,7 @@ mostlyclean-compile:
|
|||||||
distclean-compile:
|
distclean-compile:
|
||||||
-rm -f *.tab.c
|
-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)/key.Plo@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vault.Plo@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vault.Plo@am__quote@
|
||||||
|
|
||||||
|
|||||||
61
bindings/cpp/crypto++.h
Normal file
61
bindings/cpp/crypto++.h
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
You can contact me by mail: <tlinden AT cpan DOT org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _HAVE_PCPPP_CRYPTO_H
|
||||||
|
#define _HAVE_PCPPP_CRYPTO_H
|
||||||
|
|
||||||
|
#include <pcp.h>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#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<unsigned char> 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
|
||||||
152
bindings/cpp/crypto.cpp
Normal file
152
bindings/cpp/crypto.cpp
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
You can contact me by mail: <tlinden AT cpan DOT org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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<unsigned char> message) {
|
||||||
|
unsigned char *m = (unsigned char *)ucmalloc(message.size());
|
||||||
|
for(size_t i=0; i<message.size(); ++i)
|
||||||
|
m[i] = message[i];
|
||||||
|
return Crypto::encrypt(m, message.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
string Crypto::encrypt(unsigned char *message, size_t mlen) {
|
||||||
|
size_t clen, zlen, rlen;
|
||||||
|
unsigned char *cipher;
|
||||||
|
|
||||||
|
cipher = pcp_box_encrypt(S.K, P.K, message, mlen, &clen);
|
||||||
|
|
||||||
|
if(cipher == NULL)
|
||||||
|
throw exception();
|
||||||
|
|
||||||
|
rlen = clen + crypto_hash_BYTES;
|
||||||
|
unsigned char *combined = (unsigned char *)ucmalloc(rlen);
|
||||||
|
unsigned char *hash = (unsigned char *)ucmalloc(crypto_hash_BYTES);
|
||||||
|
|
||||||
|
crypto_hash(hash, (unsigned char*)S.K->id, 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<dlen; ++i)
|
||||||
|
r.Vector.push_back(decrypted[i]);
|
||||||
|
|
||||||
|
free(combined);
|
||||||
|
free(hash);
|
||||||
|
free(check);
|
||||||
|
free(encrypted);
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
69
bindings/cpp/helpers++.h
Normal file
69
bindings/cpp/helpers++.h
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
You can contact me by mail: <tlinden AT cpan DOT org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _HAVE_PCPPP_HELPERS_H
|
||||||
|
#define _HAVE_PCPPP_HELPERS_H
|
||||||
|
|
||||||
|
#include <pcp.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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<unsigned char> Vector;
|
||||||
|
unsigned char *Uchar;
|
||||||
|
size_t Size;
|
||||||
|
|
||||||
|
~ResultSet() { free(Uchar); }
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _HAVE_PCPPP_HELPERS_H
|
||||||
118
bindings/cpp/key++.h
Normal file
118
bindings/cpp/key++.h
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
You can contact me by mail: <tlinden AT cpan DOT org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _HAVE_PCPPP_KEY_H
|
||||||
|
#define _HAVE_PCPPP_KEY_H
|
||||||
|
|
||||||
|
#include <pcp.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#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
|
||||||
@@ -19,7 +19,8 @@
|
|||||||
You can contact me by mail: <tlinden AT cpan DOT org>.
|
You can contact me by mail: <tlinden AT cpan DOT org>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "pcp++.h"
|
#include "vault++.h"
|
||||||
|
#include "key++.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace pcp;
|
using namespace pcp;
|
||||||
@@ -166,19 +167,18 @@ string Key::to_text() {
|
|||||||
return z85;
|
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) {
|
ostream& pcp::operator<<(ostream& output, Key& k) {
|
||||||
output << k.to_text();
|
output << k.to_text();
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool pcp::operator!(Key& k) {
|
||||||
|
if(k.K == NULL)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void Key::encrypt(const string& passphrase) {
|
void Key::encrypt(const string& passphrase) {
|
||||||
K = pcpkey_encrypt(K, (char *)passphrase.c_str());
|
K = pcpkey_encrypt(K, (char *)passphrase.c_str());
|
||||||
if(PCP_ERRSET == 1)
|
if(PCP_ERRSET == 1)
|
||||||
@@ -233,81 +233,7 @@ bool Key::is_encrypted() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
string Key::encrypt(PubKey &recipient, string message) {
|
// class Key ends here.
|
||||||
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<unsigned char> message) {
|
|
||||||
unsigned char *m = (unsigned char *)ucmalloc(message.size());
|
|
||||||
for(size_t i=0; i<message.size(); ++i)
|
|
||||||
m[i] = message[i];
|
|
||||||
return Key::encrypt(recipient, m, message.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
string Key::encrypt(PubKey &recipient, unsigned char *message, size_t mlen) {
|
|
||||||
size_t clen, zlen, rlen;
|
|
||||||
unsigned char *cipher;
|
|
||||||
|
|
||||||
cipher = pcp_box_encrypt(K, recipient.K, message, mlen, &clen);
|
|
||||||
|
|
||||||
if(cipher == NULL)
|
|
||||||
throw exception();
|
|
||||||
|
|
||||||
rlen = clen + crypto_hash_BYTES;
|
|
||||||
unsigned char *combined = (unsigned char *)ucmalloc(rlen);
|
|
||||||
unsigned char *hash = (unsigned char *)ucmalloc(crypto_hash_BYTES);
|
|
||||||
|
|
||||||
crypto_hash(hash, (unsigned char*)K->id, 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<dlen; ++i)
|
|
||||||
r.Vector.push_back(decrypted[i]);
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -334,8 +260,13 @@ PubKey::PubKey(string &z85encoded) {
|
|||||||
if(z85encoded.length() == 0)
|
if(z85encoded.length() == 0)
|
||||||
throw pcp::exception("Error: zero length input");
|
throw pcp::exception("Error: zero length input");
|
||||||
|
|
||||||
|
|
||||||
size_t clen;
|
size_t clen;
|
||||||
unsigned char *z85decoded = pcp_z85_decode((char *)z85encoded.c_str(), &clen);
|
unsigned char *z85decoded =
|
||||||
|
pcp_z85_decode(
|
||||||
|
pcp_readz85string((unsigned char *)z85encoded.c_str(),
|
||||||
|
z85encoded.length()),
|
||||||
|
&clen); // FIXME: too complicated, must be more wrapperish
|
||||||
|
|
||||||
if(z85decoded == NULL)
|
if(z85decoded == NULL)
|
||||||
throw pcp::exception("Error: could not decode input - it's probably not Z85.\n");
|
throw pcp::exception("Error: could not decode input - it's probably not Z85.\n");
|
||||||
@@ -358,8 +289,7 @@ PubKey::PubKey(string &z85encoded) {
|
|||||||
throw pcp::exception();
|
throw pcp::exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
*this = PubKey(key);
|
K = key;
|
||||||
free(key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PubKey::~PubKey() {
|
PubKey::~PubKey() {
|
||||||
@@ -466,19 +396,20 @@ string PubKey::to_text() {
|
|||||||
return z85;
|
return z85;
|
||||||
}
|
}
|
||||||
|
|
||||||
istream& pcp::operator>>(istream& input, PubKey& k) {
|
|
||||||
string z85;
|
|
||||||
input >> z85;
|
|
||||||
k = PubKey(z85);
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ostream& pcp::operator<<(ostream& output, PubKey& k) {
|
ostream& pcp::operator<<(ostream& output, PubKey& k) {
|
||||||
output << k.to_text();
|
output << k.to_text();
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool pcp::operator!(PubKey& k) {
|
||||||
|
if(k.K == NULL) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
string PubKey::get_id() {
|
string PubKey::get_id() {
|
||||||
string id = K->id;
|
string id = K->id;
|
||||||
return id;
|
return id;
|
||||||
|
|||||||
@@ -31,166 +31,9 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "key++.h"
|
||||||
namespace pcp {
|
#include "vault++.h"
|
||||||
|
#include "crypto++.h"
|
||||||
class exception : public std::runtime_error {
|
#include "helpers++.h"
|
||||||
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<unsigned char> 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<unsigned char> 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<std::string, Key> KeyMap;
|
|
||||||
typedef std::map<std::string, PubKey> PubKeyMap;
|
|
||||||
|
|
||||||
typedef std::map<std::string,Key>::iterator KeyIterator;
|
|
||||||
typedef std::map<std::string,PubKey>::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_H
|
#endif // _HAVE_PCPPP_H
|
||||||
|
|||||||
77
bindings/cpp/vault++.h
Normal file
77
bindings/cpp/vault++.h
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
You can contact me by mail: <tlinden AT cpan DOT org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _HAVE_PCPPP_VAULT_H
|
||||||
|
#define _HAVE_PCPPP_VAULT_H
|
||||||
|
|
||||||
|
#include <pcp.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <map>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "key++.h"
|
||||||
|
|
||||||
|
namespace pcp {
|
||||||
|
|
||||||
|
typedef std::map<std::string, Key> KeyMap;
|
||||||
|
typedef std::map<std::string, PubKey> PubKeyMap;
|
||||||
|
|
||||||
|
typedef std::map<std::string,Key>::iterator KeyIterator;
|
||||||
|
typedef std::map<std::string,PubKey>::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
|
||||||
Reference in New Issue
Block a user