message implementation

This commit is contained in:
Mirek Kratochvil 2013-01-08 00:13:19 +01:00
parent db7a33fa4d
commit 9346b63bdd
3 changed files with 61 additions and 12 deletions

View file

@ -51,10 +51,13 @@ public:
sencode* privkey) = 0;
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,
sencode* pubkey) = 0;
virtual int create_keypair (sencode**pub, sencode**priv, prng&rng) = 0;
};
#endif

View file

@ -26,18 +26,36 @@ int encrypted_msg::encrypt (const bvector&msg,
key_id = Key_id;
alg_id = Alg_id;
sencode*pubkey = kr.get_pubkey (key_id);
if (!pubkey) return 1; //PK not found
algorithm*alg = NULL;
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)
{
sencode*privkey = kr.get_privkey (key_id);
if (!privkey) return 1; //no key found
algorithm*alg = NULL;
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,
@ -49,17 +67,45 @@ int signed_msg::sign (const bvector&msg,
alg_id = Alg_id;
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);
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;
}
int signed_msg::verify (algorithm_suite&algs, keyring&kr)
{
sencode*pubkey = kr.get_pubkey (key_id);
if (!pubkey) return 1;
algorithm*alg = NULL;
if (algs.count (alg_id) ) {
alg = algs[alg_id];
if (!alg->provides_signatures() )
alg = NULL;
}
return 0;
if (!alg) return 1;
sencode*pubkey = kr.get_pubkey (key_id);
if (!pubkey) return 2;
return alg->verify (signature, message, pubkey);
}

View file

@ -29,7 +29,7 @@
class encrypted_msg
{
public:
bvector message;
bvector ciphertext;
std::string alg_id, key_id;
int decrypt (bvector&, algorithm_suite&, keyring&);