message implementation
This commit is contained in:
parent
db7a33fa4d
commit
9346b63bdd
|
@ -51,10 +51,13 @@ public:
|
||||||
sencode* privkey) = 0;
|
sencode* privkey) = 0;
|
||||||
|
|
||||||
virtual int sign (const bvector&msg, bvector&sig,
|
virtual int sign (const bvector&msg, bvector&sig,
|
||||||
sencode* privkey, prng&rng) = 0;
|
sencode* privkey, bool&dirty,
|
||||||
|
prng&rng) = 0;
|
||||||
|
|
||||||
virtual int verify (const bvector&sig, const bvector&msg,
|
virtual int verify (const bvector&sig, const bvector&msg,
|
||||||
sencode* pubkey) = 0;
|
sencode* pubkey) = 0;
|
||||||
|
|
||||||
|
virtual int create_keypair (sencode**pub, sencode**priv, prng&rng) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -26,18 +26,36 @@ int encrypted_msg::encrypt (const bvector&msg,
|
||||||
key_id = Key_id;
|
key_id = Key_id;
|
||||||
alg_id = Alg_id;
|
alg_id = Alg_id;
|
||||||
|
|
||||||
sencode*pubkey = kr.get_pubkey (key_id);
|
algorithm*alg = NULL;
|
||||||
if (!pubkey) return 1; //PK not found
|
if (algs.count (alg_id) ) {
|
||||||
|
alg = algs[alg_id];
|
||||||
|
if (!alg->provides_encryption() )
|
||||||
|
alg = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
if (!alg) return 1;
|
||||||
|
|
||||||
|
sencode*pubkey = kr.get_pubkey (key_id);
|
||||||
|
if (!pubkey) return 2; //PK not found
|
||||||
|
|
||||||
|
return alg->encrypt (msg, ciphertext, pubkey, rng);
|
||||||
}
|
}
|
||||||
|
|
||||||
int encrypted_msg::decrypt (bvector& msg, algorithm_suite&algs, keyring& kr)
|
int encrypted_msg::decrypt (bvector& msg, algorithm_suite&algs, keyring& kr)
|
||||||
{
|
{
|
||||||
sencode*privkey = kr.get_privkey (key_id);
|
algorithm*alg = NULL;
|
||||||
if (!privkey) return 1; //no key found
|
if (algs.count (alg_id) ) {
|
||||||
|
alg = algs[alg_id];
|
||||||
|
if (!alg->provides_encryption() )
|
||||||
|
alg = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
if (!alg) return 1;
|
||||||
|
|
||||||
|
sencode*privkey = kr.get_privkey (key_id);
|
||||||
|
if (!privkey) return 2;
|
||||||
|
|
||||||
|
return alg->decrypt (ciphertext, msg, privkey);
|
||||||
}
|
}
|
||||||
|
|
||||||
int signed_msg::sign (const bvector&msg,
|
int signed_msg::sign (const bvector&msg,
|
||||||
|
@ -49,17 +67,45 @@ int signed_msg::sign (const bvector&msg,
|
||||||
alg_id = Alg_id;
|
alg_id = Alg_id;
|
||||||
message = msg;
|
message = msg;
|
||||||
|
|
||||||
|
algorithm*alg = NULL;
|
||||||
|
if (algs.count (alg_id) ) {
|
||||||
|
alg = algs[alg_id];
|
||||||
|
if (!alg->provides_signatures() )
|
||||||
|
alg = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!alg) return 1;
|
||||||
|
|
||||||
sencode*privkey = kr.get_privkey (key_id);
|
sencode*privkey = kr.get_privkey (key_id);
|
||||||
if (!privkey) return 1;
|
if (!privkey) return 2;
|
||||||
|
|
||||||
|
bool privkey_dirty = false;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
r = alg->sign (message, signature, privkey, privkey_dirty, rng);
|
||||||
|
|
||||||
|
if (r) return r;
|
||||||
|
|
||||||
|
//make sure the modified privkey gets stored correctly
|
||||||
|
//TODO
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int signed_msg::verify (algorithm_suite&algs, keyring&kr)
|
int signed_msg::verify (algorithm_suite&algs, keyring&kr)
|
||||||
{
|
{
|
||||||
sencode*pubkey = kr.get_pubkey (key_id);
|
algorithm*alg = NULL;
|
||||||
if (!pubkey) return 1;
|
if (algs.count (alg_id) ) {
|
||||||
|
alg = algs[alg_id];
|
||||||
return 0;
|
if (!alg->provides_signatures() )
|
||||||
|
alg = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!alg) return 1;
|
||||||
|
|
||||||
|
sencode*pubkey = kr.get_pubkey (key_id);
|
||||||
|
if (!pubkey) return 2;
|
||||||
|
|
||||||
|
return alg->verify (signature, message, pubkey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
class encrypted_msg
|
class encrypted_msg
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
bvector message;
|
bvector ciphertext;
|
||||||
std::string alg_id, key_id;
|
std::string alg_id, key_id;
|
||||||
|
|
||||||
int decrypt (bvector&, algorithm_suite&, keyring&);
|
int decrypt (bvector&, algorithm_suite&, keyring&);
|
||||||
|
|
Loading…
Reference in a new issue