diff --git a/src/actions.cpp b/src/actions.cpp index 5939815..be720d4 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -94,7 +94,7 @@ int action_gen_key (const std::string& algspec, const std::string&name, } sencode *pub, *priv; - arcfour_rng r; + ccr_rng r; err ("Gathering random seed bits from kernel..."); 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 encrypted_msg msg; - arcfour_rng r; + ccr_rng r; r.seed (256); bvector plaintext; @@ -341,8 +341,8 @@ int action_hash_sign (bool armor, const std::string&symmetric) std::vector parts; parts.resize (1); base64_encode (data, parts[0]); - arcfour_rng r; - r.seed (256); + ccr_rng r; + r.seed (128); 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 signed_msg msg; - arcfour_rng r; + ccr_rng r; r.seed (256); bvector message; @@ -869,7 +869,7 @@ int action_sign_encrypt (const std::string&user, const std::string&recipient, //make a signature signed_msg smsg; - arcfour_rng r; + ccr_rng r; r.seed (256); bvector bv; @@ -1270,7 +1270,7 @@ int action_export (bool armor, std::vector parts; parts.resize (1); base64_encode (data, parts[0]); - arcfour_rng r; + ccr_rng r; r.seed (128); data = envelope_format (ENVELOPE_PUBKEYS, parts, r); } @@ -1526,7 +1526,7 @@ int action_export_sec (bool armor, bool yes, std::vector parts; parts.resize (1); base64_encode (data, parts[0]); - arcfour_rng r; + ccr_rng r; r.seed (128); data = envelope_format (ENVELOPE_SECRETS, parts, r); } diff --git a/src/algos_enc.cpp b/src/algos_enc.cpp index 3b1f702..629b5c8 100644 --- a/src/algos_enc.cpp +++ b/src/algos_enc.cpp @@ -306,7 +306,7 @@ static int fo_encrypt (const bvector&plain, bvector&cipher, scipher sc; sc.init (); //whole key must be tossed in, so split if when necessary - sc.load_key (K); + sc.load_key_vector (K); //encrypt 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; sc.init (); //stuff in the whole key - sc.load_key (K); + sc.load_key_vector (K); //decrypt the message part for (i = 0; i < M.size(); ++i) M[i] = M[i] ^ sc.gen(); diff --git a/src/algos_sig.cpp b/src/algos_sig.cpp index f9a7477..104c04c 100644 --- a/src/algos_sig.cpp +++ b/src/algos_sig.cpp @@ -71,7 +71,7 @@ static void msg_pad (const bvector&in, std::vector&out, size_t minsize) padding_generator g; g.init (); //stuff in as much seed material as possible - g.load_key (out); + g.load_key_vector (out); i = out.size(); out.resize (minsize); diff --git a/src/arcfour.h b/src/arcfour.h index 0c2dbbb..f74e82f 100644 --- a/src/arcfour.h +++ b/src/arcfour.h @@ -67,10 +67,6 @@ public: discard (disc_bytes); } - void load_key (const std::vector&K) { - load_key (& (K[0]), & (K[K.size()]) ); - } - inttype gen() { I = (I + 1) & mask; J = (J + S[I]) & mask; diff --git a/src/fmtseq.cpp b/src/fmtseq.cpp index 856dd43..dfd2d36 100644 --- a/src/fmtseq.cpp +++ b/src/fmtseq.cpp @@ -27,14 +27,14 @@ void prepare_keygen (privgen& kg, const std::vector&SK, uint idx) { kg.clear(); kg.init (); - kg.load_key (SK); + kg.load_key_vector (SK); std::vectortmp; while (idx) { tmp.push_back (idx & 0xff); idx >>= 8; } tmp.resize (16, 0); //prevent chaining to other numbers - kg.load_key (tmp); + kg.load_key_vector (tmp); kg.discard (4096); //discarding is done manually here, //for the purpose of double key loading. diff --git a/src/generator.cpp b/src/generator.cpp index e593497..0fc0850 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -26,7 +26,7 @@ static inline uint bytes (uint bits) 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 s; 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]; f.close(); - r.load_key (s); + r.load_key_vector (s); } diff --git a/src/generator.h b/src/generator.h index 44c67a1..9a44c05 100644 --- a/src/generator.h +++ b/src/generator.h @@ -19,28 +19,31 @@ #ifndef _ccr_generator_h_ #define _ccr_generator_h_ -#include "arcfour.h" +#include "chacha.h" #include "prng.h" -class arcfour_rng : public prng +#include +#define randmax_type uint64_t + +class ccr_rng : public prng { public: - arcfour r; + chacha20 r; - arcfour_rng() { + ccr_rng() { r.init (); } - ~arcfour_rng() { + ~ccr_rng() { r.clear(); } void seed (uint bits, bool quick = true); uint random (uint n) { - //rand_max is 2^32. - return ( (r.gen() << 24) | (r.gen() << 16) - | (r.gen() << 8) | r.gen() ) % n; + randmax_type i; + r.gen (sizeof (randmax_type), (byte*) &i); + return i % n; } }; diff --git a/src/sc.h b/src/sc.h index f760558..c7320e3 100644 --- a/src/sc.h +++ b/src/sc.h @@ -23,6 +23,8 @@ #include +#include + class streamcipher { public: @@ -39,6 +41,11 @@ public: void discard (size_t n) { gen (n, 0); } + + void load_key_vector (const std::vector&K) { + load_key (& (K[0]), & (K[K.size()]) ); + } + }; #endif