From e901f292b2e4ea57dc5126675e2898f0262fcd31 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Sat, 12 Sep 2015 02:36:44 -0500 Subject: [PATCH] Comment kdf some more and fix issue where index wasn't updating. --- rai/node.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rai/node.cpp b/rai/node.cpp index 75f9cc11..2ef4b5d5 100644 --- a/rai/node.cpp +++ b/rai/node.cpp @@ -1672,14 +1672,17 @@ data (new uint64_t [entries_a]) rai::uint256_union rai::kdf::generate (std::string const & password_a, rai::uint256_union const & salt_a) { rai::uint256_union input; + // Compress password string to 256 bits blake2b_state hash; blake2b_init (&hash, 32); blake2b_update (&hash, reinterpret_cast (password_a.data ()), password_a.size ()); blake2b_final (&hash, input.bytes.data (), input.bytes.size ()); + // Mix compressed password with salt input ^= salt_a; blake2b_init (&hash, 32); auto entries_l (entries); auto mask (entries_l - 1); + // Seed our random sequence with the mixed input xorshift1024star rng; rng.s [0] = input.qwords [0]; rng.s [1] = input.qwords [1]; @@ -1700,9 +1703,9 @@ rai::uint256_union rai::kdf::generate (std::string const & password_a, rai::uint for (size_t i (0), n (entries); i != n; ++i) { auto index (previous & mask); - auto value (rng.next ()); + previous = rng.next (); // Use the index from the previous random value so LSB (data[index]) != value - data [index] = value; + data [index] = previous; } // Random-read buffer to prevent partial memorization union @@ -1713,6 +1716,7 @@ rai::uint256_union rai::kdf::generate (std::string const & password_a, rai::uint // Hash the memory buffer to derive encryption key for (size_t i (0), n (entries); i != n; i += stepping) { + // Pick up `stepping' entries at a time and hash them all at once for lower function call overhead for (size_t j (0), m (stepping); j != m; ++j) { auto index (rng.next () % (entries_l - (i + j)));