/* * Copyright (c) 2015 Cryptonomex, Inc., and contributors. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Any modified source or binaries are used only with the BitShares network. * * 2. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * * 3. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include #include namespace graphene { namespace chain { void memo_data::set_message(const fc::ecc::private_key& priv, const fc::ecc::public_key& pub, const string& msg, uint64_t custom_nonce) { if( priv != fc::ecc::private_key() && public_key_type(pub) != public_key_type() ) { from = priv.get_public_key(); to = pub; if( custom_nonce == 0 ) { uint64_t entropy = fc::sha224::hash(fc::ecc::private_key::generate())._hash[0]; entropy <<= 32; entropy &= 0xff00000000000000; nonce = (fc::time_point::now().time_since_epoch().count() & 0x00ffffffffffffff) | entropy; } else nonce = custom_nonce; auto secret = priv.get_shared_secret(pub); auto nonce_plus_secret = fc::sha512::hash(fc::to_string(nonce) + secret.str()); string text = memo_message(digest_type::hash(msg)._hash[0], msg).serialize(); message = fc::aes_encrypt( nonce_plus_secret, vector(text.begin(), text.end()) ); } else { auto text = memo_message(0, msg).serialize(); message = vector(text.begin(), text.end()); } } string memo_data::get_message(const fc::ecc::private_key& priv, const fc::ecc::public_key& pub)const { if( from != public_key_type() ) { auto secret = priv.get_shared_secret(pub); auto nonce_plus_secret = fc::sha512::hash(fc::to_string(nonce) + secret.str()); auto plain_text = fc::aes_decrypt( nonce_plus_secret, message ); auto result = memo_message::deserialize(string(plain_text.begin(), plain_text.end())); FC_ASSERT( result.checksum == uint32_t(digest_type::hash(result.text)._hash[0]) ); return result.text; } else { return memo_message::deserialize(string(message.begin(), message.end())).text; } } string memo_message::serialize() const { auto serial_checksum = string(sizeof(checksum), ' '); (uint32_t&)(*serial_checksum.data()) = checksum; return serial_checksum + text; } memo_message memo_message::deserialize(const string& serial) { memo_message result; FC_ASSERT( serial.size() >= sizeof(result.checksum) ); result.checksum = ((uint32_t&)(*serial.data())); result.text = serial.substr(sizeof(result.checksum)); return result; } } } // graphene::chain