diff --git a/libs/crypto/keys_manager.hpp b/libs/crypto/keys_manager.hpp index 99a17cdbb6..44814919b7 100644 --- a/libs/crypto/keys_manager.hpp +++ b/libs/crypto/keys_manager.hpp @@ -29,17 +29,22 @@ namespace iroha { virtual ~KeysManager() = default; /** - * Load keys associated with account - * Validate loaded keypair by signing and verifying signature - * of test message - * @param account_name - * @return nullopt if no keypair found locally, or verification failure + * Create a new keypair and store it as is on disk + * @return false if create account failed + */ + virtual bool createKeys() = 0; + + /** + * Load plain-text keys associated with the manager, then validate loaded + * keypair by signing and verifying signature of test message + * @return nullopt if no keypair found locally, or verification failure; + * related keypair otherwise */ virtual nonstd::optional loadKeys() = 0; /** * Create keys a new keypair and store it encrypted on disk - * @param pass_phrase is password for the keys + * @param pass_phrase is a password for the keys * @return false if create account failed */ virtual bool createKeys(const std::string &pass_phrase) = 0; @@ -47,18 +52,12 @@ namespace iroha { /** * Load encrypted keys associated with the manager, then validate loaded * keypair by signing and verifying signature of test message - * @param pass_phrase is the key for decryption - * @return nullopt if no keypair found locally, or verification failure + * @param pass_phrase is a password for decryption + * @return nullopt if no keypair found locally, or verification failure; + * related keypair otherwise */ virtual nonstd::optional loadKeys( const std::string &pass_phrase) = 0; - - /** - * Create a new keypair and store it as is on disk - * @param pass_phrase is password for the keys - * @return false if create account failed - */ - virtual bool createKeys() = 0; }; } // namespace iroha diff --git a/libs/crypto/keys_manager_impl.cpp b/libs/crypto/keys_manager_impl.cpp index 60235c38cf..3177cd928c 100644 --- a/libs/crypto/keys_manager_impl.cpp +++ b/libs/crypto/keys_manager_impl.cpp @@ -29,13 +29,13 @@ using iroha::operator|; namespace iroha { /** - * Return function which will try to deserialize specified value to specified - * field in given keypair + * Return a function which will try deserialize the value to + * specified field in given keypair * @tparam T - keypair field type * @tparam V - value type to deserialize * @param field - keypair field to be deserialized * @param value - value to be deserialized - * @return keypair on success, otherwise nullopt + * @return function that will return keypair on success, otherwise nullopt */ template auto deserializeKeypairField(T keypair_t::*field, const V &value) { @@ -45,6 +45,12 @@ namespace iroha { }; } + /** + * Function for the private key encryption via XOR + * @param privkey is a private key + * @param pass_phrase is a key for encryption + * @return encrypted string + */ std::string encrypt(const privkey_t &privkey, const std::string &pass_phrase) { std::string ciphertext(privkey.size(), '\0'); @@ -56,6 +62,14 @@ namespace iroha { return ciphertext; } + /** + * Return a function which will try to deserialize and then decrypt private + * key via XORing with pass phrase + * @param s is an encrypted data from file + * @param pass_phrase for decryption + * @return function that will set keypair::privkey on successful + * deserialization and decryption + */ auto deserializedEncrypted(const std::string &s, const std::string &pass_phrase) { constexpr auto size = privkey_t::size(); @@ -136,19 +150,8 @@ namespace iroha { }; } - keypair_t generate() { - blob_t<32> seed; - std::generate(seed.begin(), seed.end(), [] { - static std::random_device rd; - static std::uniform_int_distribution<> dist; - return dist(rd); - }); - - return create_keypair(seed); - } - bool KeysManagerImpl::createKeys() { - auto key_pairs = generate(); + auto key_pairs = create_keypair(); auto pub = key_pairs.pubkey.to_hexstring(); auto priv = key_pairs.privkey.to_hexstring(); @@ -156,7 +159,7 @@ namespace iroha { } bool KeysManagerImpl::createKeys(const std::string &pass_phrase) { - auto key_pairs = generate(); + auto key_pairs = create_keypair(); auto pub = key_pairs.pubkey.to_hexstring(); auto priv = bytestringToHexstring(encrypt(key_pairs.privkey, pass_phrase)); diff --git a/libs/crypto/keys_manager_impl.hpp b/libs/crypto/keys_manager_impl.hpp index 2535a04baf..f7b29a58a6 100644 --- a/libs/crypto/keys_manager_impl.hpp +++ b/libs/crypto/keys_manager_impl.hpp @@ -54,6 +54,12 @@ namespace iroha { */ bool loadFile(const std::string &filename, std::string &res); + /** + * Stores strings, that represent public and private keys on disk + * @param pub is a public key + * @param priv is a private key + * @return true, if saving was successful + */ bool store(const std::string &pub, const std::string &priv); std::string account_name_;