1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-06-28 09:53:46 +00:00
codecrypt/src/message.cpp

117 lines
2.8 KiB
C++
Raw Normal View History

2013-01-04 11:43:36 +00:00
/*
* This file is part of Codecrypt.
*
* Codecrypt is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Codecrypt 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Codecrypt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "message.h"
int encrypted_msg::encrypt (const bvector&msg,
2013-01-07 21:29:29 +00:00
const std::string& Alg_id,
const std::string& Key_id,
algorithm_suite&algs, keyring&kr, prng&rng)
2013-01-04 11:43:36 +00:00
{
key_id = Key_id;
alg_id = Alg_id;
2013-01-07 23:13:19 +00:00
algorithm*alg = NULL;
if (algs.count (alg_id) ) {
alg = algs[alg_id];
if (!alg->provides_encryption() )
alg = NULL;
}
if (!alg) return 1;
2013-01-04 11:43:36 +00:00
sencode*pubkey = kr.get_pubkey (key_id);
2013-01-07 23:13:19 +00:00
if (!pubkey) return 2; //PK not found
2013-01-04 11:43:36 +00:00
2013-01-07 23:13:19 +00:00
return alg->encrypt (msg, ciphertext, pubkey, rng);
2013-01-04 11:43:36 +00:00
}
2013-01-07 21:29:29 +00:00
int encrypted_msg::decrypt (bvector& msg, algorithm_suite&algs, keyring& kr)
2013-01-04 11:43:36 +00:00
{
2013-01-07 23:13:19 +00:00
algorithm*alg = NULL;
if (algs.count (alg_id) ) {
alg = algs[alg_id];
if (!alg->provides_encryption() )
alg = NULL;
}
if (!alg) return 1;
2013-01-04 11:43:36 +00:00
sencode*privkey = kr.get_privkey (key_id);
2013-01-07 23:13:19 +00:00
if (!privkey) return 2;
2013-01-04 11:43:36 +00:00
2013-01-07 23:13:19 +00:00
return alg->decrypt (ciphertext, msg, privkey);
2013-01-04 11:43:36 +00:00
}
int signed_msg::sign (const bvector&msg,
2013-01-07 21:29:29 +00:00
const std::string& Alg_id,
const std::string& Key_id,
algorithm_suite&algs, keyring&kr, prng&rng)
2013-01-04 11:43:36 +00:00
{
key_id = Key_id;
alg_id = Alg_id;
message = msg;
2013-01-07 23:13:19 +00:00
algorithm*alg = NULL;
if (algs.count (alg_id) ) {
alg = algs[alg_id];
if (!alg->provides_signatures() )
alg = NULL;
}
if (!alg) return 1;
2013-01-04 11:43:36 +00:00
sencode*privkey = kr.get_privkey (key_id);
2013-01-07 23:13:19 +00:00
if (!privkey) return 2;
bool privkey_dirty = false;
int r;
r = alg->sign (message, signature, privkey, privkey_dirty, rng);
if (r) return r;
2013-01-08 19:06:59 +00:00
if (privkey_dirty) {
kr.remove_privkey (key_id);
//this actually shouldn't fail, key_id is not present
kr.store_privkey (key_id, privkey);
//we can't output a signature without storing privkey changes
if (!kr.disk_sync() ) return 3;
}
2013-01-04 11:43:36 +00:00
2013-01-07 21:29:29 +00:00
return 0;
2013-01-04 11:43:36 +00:00
}
2013-01-07 21:29:29 +00:00
int signed_msg::verify (algorithm_suite&algs, keyring&kr)
2013-01-04 11:43:36 +00:00
{
2013-01-07 23:13:19 +00:00
algorithm*alg = NULL;
if (algs.count (alg_id) ) {
alg = algs[alg_id];
if (!alg->provides_signatures() )
alg = NULL;
}
if (!alg) return 1;
2013-01-04 11:43:36 +00:00
sencode*pubkey = kr.get_pubkey (key_id);
2013-01-07 23:13:19 +00:00
if (!pubkey) return 2;
2013-01-04 11:43:36 +00:00
2013-01-07 23:13:19 +00:00
return alg->verify (signature, message, pubkey);
2013-01-04 11:43:36 +00:00
}