# # add_file "hmac.cc" # # add_file "hmac.hh" # # patch "ChangeLog" # from [a7d78e5b4cb7e07bdf3aba7a92ea4fc609a03316] # to [5293c86ecc14f6f36a61630082f5cd634840d21f] # # patch "hmac.cc" # from [] # to [39bc2c1c84cb059b9d4095dd23daa369c33c4cbf] # # patch "hmac.hh" # from [] # to [1aca898a8a90b9054cd1b4c0e966b298854cc924] # # patch "transforms.hh" # from [9fb4f54ab766397cc5a40885457b2ddbabbb300f] # to [10da66c1dec345841caf96a4c7bc2291b7d3ecb3] # --- ChangeLog +++ ChangeLog @@ -1,5 +1,10 @@ 2005-06-26 Matt Johnston + * transforms.hh: remove extraneous #ifdef + * hmac.cc, hmac.hh: actually add them + +2005-06-26 Matt Johnston + * netcmd.cc (netcmd::read, netcmd::write): change to using a HMACs chained by including the previous HMAC in the input data, rather than altering the key each time. --- hmac.cc +++ hmac.cc @@ -0,0 +1,43 @@ +#include + +#include "cryptopp/hmac.h" +#include "cryptopp/sha.h" + +#include "sanity.hh" +#include "hmac.hh" +#include "vocab.hh" +#include "constants.hh" + +chained_hmac::chained_hmac(netsync_session_key const & session_key) : + key(session_key) +{ + memset(chain_val, 0, sizeof(chain_val)); +} + +void +chained_hmac::set_key(netsync_session_key const & session_key) +{ + key = session_key; +} + +std::string +chained_hmac::process(std::string const & str, size_t pos, size_t n) +{ + I(pos < str.size()); + if (n == std::string::npos) + n = str.size() - pos; + + I(pos + n <= str.size()); + + CryptoPP::HMAC + hmac(reinterpret_cast(key().data()), + constants::netsync_session_key_length_in_bytes); + hmac.Update(reinterpret_cast(chain_val), + sizeof(chain_val)); + hmac.Update(reinterpret_cast(str.data() + pos), + n); + hmac.Final(reinterpret_cast(chain_val)); + + std::string out(chain_val, sizeof(chain_val)); + return out; +} --- hmac.hh +++ hmac.hh @@ -0,0 +1,28 @@ +#ifndef __HMAC_HH__ +#define __HMAC_HH__ + +#include + +#include "cryptopp/hmac.h" +#include "cryptopp/sha.h" + +#include "vocab.hh" + +struct chained_hmac +{ + public: + chained_hmac(netsync_session_key const & session_key); + void set_key(netsync_session_key const & session_key); + std::string process(std::string const & str, size_t pos = 0, + size_t n = std::string::npos); + + private: + netsync_session_key key; + char chain_val[CryptoPP::SHA::DIGESTSIZE]; +}; + + + + +#endif // __HMAC_HH__ + --- transforms.hh +++ transforms.hh @@ -78,7 +78,6 @@ void encode_gzip(std::string const & in, gzip & out) { out = xform(in); } -#endif // both at once (this is relatively common) template