algorithm abstraction
This commit is contained in:
parent
814c7642af
commit
fa99f07b12
61
src/algorithm.h
Normal file
61
src/algorithm.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef _ccr_algorithm_h_
|
||||
#define _ccr_algorithm_h_
|
||||
|
||||
#include "bvector.h"
|
||||
#include "prng.h"
|
||||
#include "sencode.h"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
class algorithm;
|
||||
typedef std::map<std::string, algorithm*> algorithm_suite;
|
||||
|
||||
//virtual interface definition for all cryptographic algorithm instances
|
||||
class algorithm
|
||||
{
|
||||
public:
|
||||
virtual bool provides_signatures() = 0;
|
||||
virtual bool provides_encryption() = 0;
|
||||
virtual std::string get_alg_id() = 0;
|
||||
|
||||
void register_into_suite (algorithm_suite&s) {
|
||||
s[this->get_alg_id()] = this;
|
||||
}
|
||||
|
||||
/*
|
||||
* note that these functions should be ready for different
|
||||
* plaintext/ciphertext/message lengths, usually padding them somehow.
|
||||
*/
|
||||
virtual int encrypt (const bvector&plain, bvector&cipher,
|
||||
sencode* pubkey, prng&rng) = 0;
|
||||
|
||||
virtual int decrypt (const bvector&cipher, bvector&plain,
|
||||
sencode* privkey) = 0;
|
||||
|
||||
virtual int sign (const bvector&msg, bvector&sig,
|
||||
sencode* privkey, prng&rng) = 0;
|
||||
|
||||
virtual int verify (const bvector&sig, const bvector&msg,
|
||||
sencode* pubkey) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -18,11 +18,10 @@
|
|||
|
||||
#include "message.h"
|
||||
|
||||
#include "mce_qd.h"
|
||||
|
||||
int encrypted_msg::encrypt (const bvector&msg,
|
||||
const std::string& Alg_id, const std::string& Key_id,
|
||||
keyring&kr, prng&rng)
|
||||
const std::string& Alg_id,
|
||||
const std::string& Key_id,
|
||||
algorithm_suite&algs, keyring&kr, prng&rng)
|
||||
{
|
||||
key_id = Key_id;
|
||||
alg_id = Alg_id;
|
||||
|
@ -30,36 +29,21 @@ int encrypted_msg::encrypt (const bvector&msg,
|
|||
sencode*pubkey = kr.get_pubkey (key_id);
|
||||
if (!pubkey) return 1; //PK not found
|
||||
|
||||
if (alg_id == "MCEQD-128") {
|
||||
} else if (alg_id == "MCEQD-256") {
|
||||
mce_qd::pubkey pk;
|
||||
if (!pk.unserialize (pubkey) ) return 3; //Key unreadable
|
||||
|
||||
//TODO fujisaki-okamoto
|
||||
} else return 2; //unknown algorithm
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int encrypted_msg::decrypt (bvector&msg, keyring&kr)
|
||||
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
|
||||
|
||||
if (alg_id == "MCEQD-128") {
|
||||
} else if (alg_id == "MCEQD-256") {
|
||||
mce_qd::privkey sk;
|
||||
if (!sk.unserialize (privkey) ) return 3; //key unreadable
|
||||
|
||||
//TODO fujisaki-okamoto
|
||||
} else return 2; //unknown algorithm
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int signed_msg::sign (const bvector&msg,
|
||||
const std::string& Alg_id, const std::string&Key_id,
|
||||
keyring&kr, prng&rng)
|
||||
const std::string& Alg_id,
|
||||
const std::string& Key_id,
|
||||
algorithm_suite&algs, keyring&kr, prng&rng)
|
||||
{
|
||||
key_id = Key_id;
|
||||
alg_id = Alg_id;
|
||||
|
@ -68,25 +52,13 @@ int signed_msg::sign (const bvector&msg,
|
|||
sencode*privkey = kr.get_privkey (key_id);
|
||||
if (!privkey) return 1;
|
||||
|
||||
if (alg_id == "FMTSEQ-S256-128") {
|
||||
|
||||
} else if (alg_id == "FMTSEQ-S256-256") {
|
||||
|
||||
//TODO produce a reasonable signature
|
||||
} else return 2; //unknown algorithm
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int signed_msg::verify (keyring&kr)
|
||||
int signed_msg::verify (algorithm_suite&algs, keyring&kr)
|
||||
{
|
||||
sencode*pubkey = kr.get_pubkey (key_id);
|
||||
if (!pubkey) return 1;
|
||||
if (alg_id == "FMTSEQ-S256-128") {
|
||||
|
||||
//TODO check it
|
||||
} else if (alg_id == "FMTSEQ-S256-256") {
|
||||
|
||||
} else return 2; //unknown algorithm
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <string>
|
||||
#include "bvector.h"
|
||||
#include "sencode.h"
|
||||
#include "algorithm.h"
|
||||
#include "keyring.h"
|
||||
#include "prng.h"
|
||||
|
||||
|
@ -31,12 +32,11 @@ public:
|
|||
bvector message;
|
||||
std::string alg_id, key_id;
|
||||
|
||||
int decrypt (bvector&, keyring&);
|
||||
int decrypt (bvector&, algorithm_suite&, keyring&);
|
||||
int encrypt (const bvector& msg,
|
||||
const std::string& alg_id,
|
||||
const std::string& key_id,
|
||||
keyring&, prng&);
|
||||
|
||||
algorithm_suite&, keyring&, prng&);
|
||||
|
||||
sencode* serialize();
|
||||
bool unserialize (sencode*);
|
||||
|
@ -48,11 +48,11 @@ public:
|
|||
bvector message, signature;
|
||||
std::string alg_id, key_id;
|
||||
|
||||
int verify (keyring&);
|
||||
int verify (algorithm_suite&, keyring&);
|
||||
int sign (const bvector&msg,
|
||||
const std::string&alg_id,
|
||||
const std::string&key_id,
|
||||
keyring&, prng&);
|
||||
algorithm_suite&, keyring&, prng&);
|
||||
|
||||
sencode* serialize();
|
||||
bool unserialize (sencode*);
|
||||
|
|
Loading…
Reference in a new issue