algos_enc: works
This commit is contained in:
parent
932cd938a4
commit
805df12176
|
@ -28,7 +28,14 @@
|
||||||
class algorithm;
|
class algorithm;
|
||||||
typedef std::map<std::string, algorithm*> algorithm_suite;
|
typedef std::map<std::string, algorithm*> algorithm_suite;
|
||||||
|
|
||||||
//virtual interface definition for all cryptographic algorithm instances
|
/*
|
||||||
|
* virtual interface definition for all cryptographic algorithm instances.
|
||||||
|
*
|
||||||
|
* Note that the whole class could be defined static, but we really enjoy
|
||||||
|
* having the tiny virtual pointers stored in some cool structure along with
|
||||||
|
* the handy algorithm name.
|
||||||
|
*/
|
||||||
|
|
||||||
class algorithm
|
class algorithm
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -182,12 +182,12 @@ static int fo_encrypt (const bvector&plain, bvector&cipher,
|
||||||
|
|
||||||
//load the key
|
//load the key
|
||||||
pubkey_type Pub;
|
pubkey_type Pub;
|
||||||
if (Pub.unserialize (pubkey) ) return 1;
|
if (!Pub.unserialize (pubkey) ) return 1;
|
||||||
|
|
||||||
//verify that key parameters match our scheme
|
//verify that key parameters match our scheme
|
||||||
if (Pub.plain_size() != plainsize) return 1;
|
if (Pub.plain_size() != plainsize) return 2;
|
||||||
if (Pub.cipher_size() != ciphersize) return 1;
|
if (Pub.cipher_size() != ciphersize) return 3;
|
||||||
if (Pub.error_count() != errorcount) return 1;
|
if (Pub.error_count() != errorcount) return 4;
|
||||||
|
|
||||||
//create the unencrypted message part
|
//create the unencrypted message part
|
||||||
std::vector<byte> M;
|
std::vector<byte> M;
|
||||||
|
@ -217,10 +217,10 @@ static int fo_encrypt (const bvector&plain, bvector&cipher,
|
||||||
//prepare plaintext
|
//prepare plaintext
|
||||||
bvector mce_plain;
|
bvector mce_plain;
|
||||||
mce_plain.resize (plainsize);
|
mce_plain.resize (plainsize);
|
||||||
for (i = 0; i < plainsize; ++i) mce_plain[i] = 1 & (M[i >> 3] >> (i & 0x7) );
|
for (i = 0; i < plainsize; ++i) mce_plain[i] = 1 & (K[i >> 3] >> (i & 0x7) );
|
||||||
|
|
||||||
//run McEliece
|
//run McEliece
|
||||||
if (Pub.encrypt (mce_plain, cipher, ev) ) return 2;
|
if (Pub.encrypt (mce_plain, cipher, ev) ) return 5;
|
||||||
|
|
||||||
//encrypt the message part (xor with arcfour)
|
//encrypt the message part (xor with arcfour)
|
||||||
arcfour<byte> arc;
|
arcfour<byte> arc;
|
||||||
|
@ -256,22 +256,24 @@ static int fo_decrypt (const bvector&cipher, bvector&plain,
|
||||||
|
|
||||||
//load the key
|
//load the key
|
||||||
privkey_type Priv;
|
privkey_type Priv;
|
||||||
if (Priv.unserialize (privkey) ) return 1;
|
if (!Priv.unserialize (privkey) ) return 1;
|
||||||
|
|
||||||
|
if (Priv.prepare() ) return 100;
|
||||||
|
|
||||||
//verify that key parameters match the scheme
|
//verify that key parameters match the scheme
|
||||||
if (Priv.plain_size() != plainsize) return 1;
|
if (Priv.plain_size() != plainsize) return 2;
|
||||||
if (Priv.cipher_size() != ciphersize) return 1;
|
if (Priv.cipher_size() != ciphersize) return 3;
|
||||||
if (Priv.error_count() != errorcount) return 1;
|
if (Priv.error_count() != errorcount) return 4;
|
||||||
|
|
||||||
//get the McE part
|
//get the McE part
|
||||||
if (cipher.size() < ciphersize) return 2;
|
if (cipher.size() < ciphersize) return 5;
|
||||||
bvector mce_cipher, mce_plain, ev;
|
bvector mce_cipher, mce_plain, ev;
|
||||||
mce_cipher.insert (mce_cipher.end(),
|
mce_cipher.insert (mce_cipher.end(),
|
||||||
cipher.begin(),
|
cipher.begin(),
|
||||||
cipher.begin() + ciphersize);
|
cipher.begin() + ciphersize);
|
||||||
|
|
||||||
//decrypt the symmetric key
|
//decrypt the symmetric key
|
||||||
if (Priv.decrypt (mce_cipher, mce_plain, ev) ) return 2;
|
if (Priv.decrypt (mce_cipher, mce_plain, ev) ) return 6;
|
||||||
|
|
||||||
//convert stuff to byte vectors
|
//convert stuff to byte vectors
|
||||||
std::vector<byte> K, M;
|
std::vector<byte> K, M;
|
||||||
|
@ -280,7 +282,7 @@ static int fo_decrypt (const bvector&cipher, bvector&plain,
|
||||||
if (mce_plain[i]) K[i >> 3] |= 1 << (i & 0x7);
|
if (mce_plain[i]) K[i >> 3] |= 1 << (i & 0x7);
|
||||||
|
|
||||||
uint msize = cipher.size() - ciphersize;
|
uint msize = cipher.size() - ciphersize;
|
||||||
if (msize & 0x7) return 2;
|
if (msize & 0x7) return 7;
|
||||||
M.resize (msize >> 3, 0);
|
M.resize (msize >> 3, 0);
|
||||||
for (i = 0; i < msize; ++i)
|
for (i = 0; i < msize; ++i)
|
||||||
if (cipher[ciphersize + i]) M[i >> 3] |= 1 << (i & 0x7);
|
if (cipher[ciphersize + i]) M[i >> 3] |= 1 << (i & 0x7);
|
||||||
|
@ -318,10 +320,10 @@ static int fo_decrypt (const bvector&cipher, bvector&plain,
|
||||||
ev_rank.colex_unrank (ev2, ciphersize, errorcount);
|
ev_rank.colex_unrank (ev2, ciphersize, errorcount);
|
||||||
|
|
||||||
//now it should match, otherwise someone mangled the message.
|
//now it should match, otherwise someone mangled the message.
|
||||||
if (ev != ev2) return 3;
|
if (ev != ev2) return 8;
|
||||||
|
|
||||||
//if the message seems okay, unpad and return it.
|
//if the message seems okay, unpad and return it.
|
||||||
if (!message_unpad (M, plain) ) return 2;
|
if (!message_unpad (M, plain) ) return 9;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue