generator: remove rc4 usage, some cleaning

This commit is contained in:
Mirek Kratochvil 2014-04-05 11:36:50 +02:00
parent 3659cc837e
commit fd489ae69f
8 changed files with 33 additions and 27 deletions

View file

@ -94,7 +94,7 @@ int action_gen_key (const std::string& algspec, const std::string&name,
} }
sencode *pub, *priv; sencode *pub, *priv;
arcfour_rng r; ccr_rng r;
err ("Gathering random seed bits from kernel..."); err ("Gathering random seed bits from kernel...");
err ("If nothing happens, move mouse, type random stuff on keyboard,"); err ("If nothing happens, move mouse, type random stuff on keyboard,");
@ -188,7 +188,7 @@ int action_encrypt (const std::string&recipient, bool armor,
//encryption part //encryption part
encrypted_msg msg; encrypted_msg msg;
arcfour_rng r; ccr_rng r;
r.seed (256); r.seed (256);
bvector plaintext; bvector plaintext;
@ -341,8 +341,8 @@ int action_hash_sign (bool armor, const std::string&symmetric)
std::vector<std::string> parts; std::vector<std::string> parts;
parts.resize (1); parts.resize (1);
base64_encode (data, parts[0]); base64_encode (data, parts[0]);
arcfour_rng r; ccr_rng r;
r.seed (256); r.seed (128);
data = envelope_format (ENVELOPE_HASHFILE, parts, r); data = envelope_format (ENVELOPE_HASHFILE, parts, r);
} }
@ -431,7 +431,7 @@ int action_sign (const std::string&user, bool armor, const std::string&detach,
//signature production part //signature production part
signed_msg msg; signed_msg msg;
arcfour_rng r; ccr_rng r;
r.seed (256); r.seed (256);
bvector message; bvector message;
@ -869,7 +869,7 @@ int action_sign_encrypt (const std::string&user, const std::string&recipient,
//make a signature //make a signature
signed_msg smsg; signed_msg smsg;
arcfour_rng r; ccr_rng r;
r.seed (256); r.seed (256);
bvector bv; bvector bv;
@ -1270,7 +1270,7 @@ int action_export (bool armor,
std::vector<std::string> parts; std::vector<std::string> parts;
parts.resize (1); parts.resize (1);
base64_encode (data, parts[0]); base64_encode (data, parts[0]);
arcfour_rng r; ccr_rng r;
r.seed (128); r.seed (128);
data = envelope_format (ENVELOPE_PUBKEYS, parts, r); data = envelope_format (ENVELOPE_PUBKEYS, parts, r);
} }
@ -1526,7 +1526,7 @@ int action_export_sec (bool armor, bool yes,
std::vector<std::string> parts; std::vector<std::string> parts;
parts.resize (1); parts.resize (1);
base64_encode (data, parts[0]); base64_encode (data, parts[0]);
arcfour_rng r; ccr_rng r;
r.seed (128); r.seed (128);
data = envelope_format (ENVELOPE_SECRETS, parts, r); data = envelope_format (ENVELOPE_SECRETS, parts, r);
} }

View file

@ -306,7 +306,7 @@ static int fo_encrypt (const bvector&plain, bvector&cipher,
scipher sc; scipher sc;
sc.init (); sc.init ();
//whole key must be tossed in, so split if when necessary //whole key must be tossed in, so split if when necessary
sc.load_key (K); sc.load_key_vector (K);
//encrypt //encrypt
for (i = 0; i < M.size(); ++i) M[i] = M[i] ^ sc.gen(); for (i = 0; i < M.size(); ++i) M[i] = M[i] ^ sc.gen();
@ -369,7 +369,7 @@ static int fo_decrypt (const bvector&cipher, bvector&plain,
scipher sc; scipher sc;
sc.init (); sc.init ();
//stuff in the whole key //stuff in the whole key
sc.load_key (K); sc.load_key_vector (K);
//decrypt the message part //decrypt the message part
for (i = 0; i < M.size(); ++i) M[i] = M[i] ^ sc.gen(); for (i = 0; i < M.size(); ++i) M[i] = M[i] ^ sc.gen();

View file

@ -71,7 +71,7 @@ static void msg_pad (const bvector&in, std::vector<byte>&out, size_t minsize)
padding_generator g; padding_generator g;
g.init (); g.init ();
//stuff in as much seed material as possible //stuff in as much seed material as possible
g.load_key (out); g.load_key_vector (out);
i = out.size(); i = out.size();
out.resize (minsize); out.resize (minsize);

View file

@ -67,10 +67,6 @@ public:
discard (disc_bytes); discard (disc_bytes);
} }
void load_key (const std::vector<inttype>&K) {
load_key (& (K[0]), & (K[K.size()]) );
}
inttype gen() { inttype gen() {
I = (I + 1) & mask; I = (I + 1) & mask;
J = (J + S[I]) & mask; J = (J + S[I]) & mask;

View file

@ -27,14 +27,14 @@ void prepare_keygen (privgen& kg, const std::vector<byte>&SK, uint idx)
{ {
kg.clear(); kg.clear();
kg.init (); kg.init ();
kg.load_key (SK); kg.load_key_vector (SK);
std::vector<byte>tmp; std::vector<byte>tmp;
while (idx) { while (idx) {
tmp.push_back (idx & 0xff); tmp.push_back (idx & 0xff);
idx >>= 8; idx >>= 8;
} }
tmp.resize (16, 0); //prevent chaining to other numbers tmp.resize (16, 0); //prevent chaining to other numbers
kg.load_key (tmp); kg.load_key_vector (tmp);
kg.discard (4096); kg.discard (4096);
//discarding is done manually here, //discarding is done manually here,
//for the purpose of double key loading. //for the purpose of double key loading.

View file

@ -26,7 +26,7 @@ static inline uint bytes (uint bits)
return (bits >> 3) + ( (bits & 7) ? 1 : 0); return (bits >> 3) + ( (bits & 7) ? 1 : 0);
} }
void arcfour_rng::seed (uint bits, bool quick) void ccr_rng::seed (uint bits, bool quick)
{ {
std::vector<byte> s; std::vector<byte> s;
std::ifstream f; std::ifstream f;
@ -40,6 +40,6 @@ void arcfour_rng::seed (uint bits, bool quick)
for (uint i = 0; i < b; ++i) f >> s[i]; for (uint i = 0; i < b; ++i) f >> s[i];
f.close(); f.close();
r.load_key (s); r.load_key_vector (s);
} }

View file

@ -19,28 +19,31 @@
#ifndef _ccr_generator_h_ #ifndef _ccr_generator_h_
#define _ccr_generator_h_ #define _ccr_generator_h_
#include "arcfour.h" #include "chacha.h"
#include "prng.h" #include "prng.h"
class arcfour_rng : public prng #include <stdint.h>
#define randmax_type uint64_t
class ccr_rng : public prng
{ {
public: public:
arcfour<byte, 8, 4096> r; chacha20 r;
arcfour_rng() { ccr_rng() {
r.init (); r.init ();
} }
~arcfour_rng() { ~ccr_rng() {
r.clear(); r.clear();
} }
void seed (uint bits, bool quick = true); void seed (uint bits, bool quick = true);
uint random (uint n) { uint random (uint n) {
//rand_max is 2^32. randmax_type i;
return ( (r.gen() << 24) | (r.gen() << 16) r.gen (sizeof (randmax_type), (byte*) &i);
| (r.gen() << 8) | r.gen() ) % n; return i % n;
} }
}; };

View file

@ -23,6 +23,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <vector>
class streamcipher class streamcipher
{ {
public: public:
@ -39,6 +41,11 @@ public:
void discard (size_t n) { void discard (size_t n) {
gen (n, 0); gen (n, 0);
} }
void load_key_vector (const std::vector<byte>&K) {
load_key (& (K[0]), & (K[K.size()]) );
}
}; };
#endif #endif