mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 20:00:58 +01:00
re-implemented signature system to match the standard (orinal input, including sig for full sig; or 32k blockwise hash of input and sig from hash attached to original input without the hash), verify_buffered currently not implemented, armored sig only for output.
This commit is contained in:
@@ -42,7 +42,7 @@ namespace pcp {
|
||||
PubKey P;
|
||||
Key S;
|
||||
Vault vault;
|
||||
pcp_sig_t *sig;
|
||||
unsigned char *sig;
|
||||
|
||||
// constructors
|
||||
Signature(Key &skey); // sign only
|
||||
@@ -55,14 +55,12 @@ namespace pcp {
|
||||
|
||||
// PK signature methods
|
||||
// sender pubkey is P
|
||||
std::string sign(std::vector<unsigned char> message);
|
||||
std::string sign(std::string message);
|
||||
std::string sign(unsigned char *message, size_t mlen);
|
||||
unsigned char *sign(std::vector<unsigned char> message);
|
||||
unsigned char *sign(unsigned char *message, size_t mlen);
|
||||
|
||||
// verify using P or use vault if defined
|
||||
bool verify(std::string signature, std::string message);
|
||||
bool verify(std::string signature, std::vector<unsigned char> message);
|
||||
bool verify(std::string signature, unsigned char *message, size_t mlen);
|
||||
bool verify(std::vector<unsigned char> message);
|
||||
bool verify(unsigned char *signature, size_t mlen);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -55,101 +55,46 @@ Signature::~Signature() {
|
||||
free(sig);
|
||||
}
|
||||
|
||||
std::string Signature::sign(std::vector<unsigned char> message) {
|
||||
unsigned char *Signature::sign(std::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];
|
||||
string _s = Signature::sign(m, message.size());
|
||||
free(m);
|
||||
return _s;
|
||||
return Signature::sign(m, message.size());
|
||||
}
|
||||
|
||||
std::string Signature::sign(std::string message) {
|
||||
unsigned char *m = (unsigned char *)ucmalloc(message.size() + 1);
|
||||
memcpy(m, message.c_str(), message.size());
|
||||
string _s = Signature::sign(m, message.size() + 1);
|
||||
free(m);
|
||||
return _s;
|
||||
}
|
||||
|
||||
std::string Signature::sign(unsigned char *message, size_t mlen) {
|
||||
unsigned char *Signature::sign(unsigned char *message, size_t mlen) {
|
||||
if(! S)
|
||||
throw exception("Error: cannot sign without a secret key, use another constructor.");
|
||||
|
||||
if(S.is_encrypted())
|
||||
throw exception("Error: cannot sign with an encrypted secret key, decrypt it before using.");
|
||||
|
||||
size_t zlen;
|
||||
sig = pcp_ed_sign(message, mlen, S.K);
|
||||
|
||||
if(sig == NULL)
|
||||
throw exception();
|
||||
|
||||
sig2be(sig);
|
||||
char *encoded = pcp_z85_encode((unsigned char *)sig, sizeof(pcp_sig_t), &zlen);
|
||||
sig2native(sig);
|
||||
|
||||
if(encoded == NULL)
|
||||
throw exception();
|
||||
|
||||
// FIXME: who free()s encoced?
|
||||
return string((char *)encoded);
|
||||
return sig;
|
||||
}
|
||||
|
||||
bool Signature::verify(string signature, vector<unsigned char> message) {
|
||||
bool Signature::verify(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];
|
||||
bool _b = Signature::verify(signature, m, message.size());
|
||||
bool _b = Signature::verify(m, message.size());
|
||||
free(m);
|
||||
return _b;
|
||||
}
|
||||
|
||||
bool Signature::verify(string signature, string message) {
|
||||
unsigned char *m = (unsigned char *)ucmalloc(message.size() + 1);
|
||||
memcpy(m, message.c_str(), message.size());
|
||||
bool _b = Signature::verify(signature, m, message.size() + 1);
|
||||
free(m);
|
||||
return _b;
|
||||
}
|
||||
|
||||
bool Signature::verify(string signature, unsigned char *message, size_t mlen) {
|
||||
size_t clen;
|
||||
unsigned char *decoded = pcp_z85_decode((char *)signature.c_str(), &clen);
|
||||
|
||||
if(decoded == NULL)
|
||||
throw exception();
|
||||
|
||||
if(clen != sizeof(pcp_sig_t)) {
|
||||
free(decoded);
|
||||
throw exception("Error: decoded signature didn't result to a proper sized sig!");
|
||||
}
|
||||
|
||||
sig = (pcp_sig_t *)decoded;
|
||||
sig2native(sig);
|
||||
|
||||
string sigid = string((char *)sig->id);
|
||||
bool Signature::verify(unsigned char *signature, size_t mlen) {
|
||||
unsigned char *message;
|
||||
|
||||
if(!P) {
|
||||
if(havevault) {
|
||||
if(vault.pubkey_exists(sigid)) {
|
||||
P = vault.get_public(sigid);
|
||||
}
|
||||
else {
|
||||
throw exception("Unable to verify, signed using an unknown key.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw exception("No public key and no vault specified, unable to verify.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(P.get_id() != sigid) {
|
||||
throw exception("Specified public key doesn't match the signers key.");
|
||||
}
|
||||
throw exception("No public key specified, unable to verify.");
|
||||
}
|
||||
|
||||
if(pcp_ed_verify(message, mlen, sig, P.K) == 0) {
|
||||
message = pcp_ed_verify(signature, mlen, P.K);
|
||||
if(message != NULL) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user