1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-06-24 07:48:13 +00:00
codecrypt/src/message.cpp

123 lines
2.9 KiB
C++
Raw Normal View History

2013-01-04 11:43:36 +00:00
/*
* This file is part of Codecrypt.
*
2016-04-17 13:47:47 +00:00
* Copyright (C) 2013-2016 Mirek Kratochvil <exa.exa@gmail.com>
*
2013-01-04 11:43:36 +00:00
* 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;
2015-10-31 21:58:17 +00:00
if (algs.count (alg_id)) {
2013-01-07 23:13:19 +00:00
alg = algs[alg_id];
2015-10-31 21:58:17 +00:00
if (!alg->provides_encryption())
2013-01-07 23:13:19 +00:00
alg = NULL;
}
if (!alg) return 1;
2013-04-01 15:49:58 +00:00
keyring::pubkey_entry*pk = kr.get_pubkey (key_id);
if (!pk) return 2; //PK not found
2013-01-04 11:43:36 +00:00
if (pk->alg != alg_id) return 3; //algorithm mismatch
2013-04-01 15:49:58 +00:00
return alg->encrypt (msg, ciphertext, pk->key, 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;
2015-10-31 21:58:17 +00:00
if (algs.count (alg_id)) {
2013-01-07 23:13:19 +00:00
alg = algs[alg_id];
2015-10-31 21:58:17 +00:00
if (!alg->provides_encryption())
2013-01-07 23:13:19 +00:00
alg = NULL;
}
if (!alg) return 1;
2013-04-01 15:49:58 +00:00
keyring::keypair_entry*k = kr.get_keypair (key_id);
if (!k) return 2;
2013-01-04 11:43:36 +00:00
if (k->pub.alg != alg_id) return 3;
2013-04-01 15:49:58 +00:00
return alg->decrypt (ciphertext, msg, k->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;
2015-10-31 21:58:17 +00:00
if (algs.count (alg_id)) {
2013-01-07 23:13:19 +00:00
alg = algs[alg_id];
2015-10-31 21:58:17 +00:00
if (!alg->provides_signatures())
2013-01-07 23:13:19 +00:00
alg = NULL;
}
if (!alg) return 1;
2013-04-01 15:49:58 +00:00
keyring::keypair_entry *k = kr.get_keypair (key_id);
if (!k) return 2;
//note that someone has to prepare the k->privkey in advance!
2013-01-07 23:13:19 +00:00
if (k->pub.alg != alg_id) return 3;
2013-01-07 23:13:19 +00:00
int r;
r = alg->sign (message, signature, & (k->privkey), k->dirty, rng);
2013-01-07 23:13:19 +00:00
if (r) return r;
if (k->dirty) {
2013-01-27 14:02:54 +00:00
//we can't output a signature without storing privkey changes!
if (!kr.save (rng)) return 4;
2013-01-08 19:06:59 +00:00
}
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;
2015-10-31 21:58:17 +00:00
if (algs.count (alg_id)) {
2013-01-07 23:13:19 +00:00
alg = algs[alg_id];
2015-10-31 21:58:17 +00:00
if (!alg->provides_signatures())
2013-01-07 23:13:19 +00:00
alg = NULL;
}
if (!alg) return 1;
2013-04-01 15:49:58 +00:00
keyring::pubkey_entry*pk = kr.get_pubkey (key_id);
if (!pk) return 2;
2013-01-04 11:43:36 +00:00
if (pk->alg != alg_id) return 3;
2013-04-01 15:49:58 +00:00
return alg->verify (signature, message, pk->key);
2013-01-04 11:43:36 +00:00
}