Skip to content

Commit

Permalink
reworked crypto objects passthrough
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Boldyrev <miboldyrev@gmail.com>
  • Loading branch information
MBoldyrev committed Oct 28, 2019
1 parent a885c5e commit 5d4ee85
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 50 deletions.
8 changes: 6 additions & 2 deletions iroha-cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ namespace fs = boost::filesystem;

iroha::keypair_t *makeOldModel(const shared_model::crypto::Keypair &keypair) {
return new iroha::keypair_t{
iroha::pubkey_t::from_string(toBinaryString(keypair.publicKey())),
iroha::privkey_t::from_string(toBinaryString(keypair.privateKey()))};
iroha::expected::resultToOptionalValue(
iroha::pubkey_t::from_string(toBinaryString(keypair.publicKey())))
.value(),
iroha::expected::resultToOptionalValue(
iroha::privkey_t::from_string(toBinaryString(keypair.privateKey())))
.value()};
}

int main(int argc, char *argv[]) {
Expand Down
12 changes: 9 additions & 3 deletions irohad/model/converters/impl/pb_block_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,19 @@ namespace iroha {

block.txs_number = static_cast<uint16_t>(pl.tx_number());
block.height = pl.height();
block.prev_hash = hash256_t::from_string(pl.prev_block_hash());
block.prev_hash = iroha::expected::resultToOptionalValue(
hash256_t::from_string(pl.prev_block_hash()))
.value();
block.created_ts = pl.created_time();

for (const auto &pb_sig : pb_block.block_v1().signatures()) {
model::Signature sig;
sig.signature = sig_t::from_string(pb_sig.signature());
sig.pubkey = pubkey_t::from_string(pb_sig.public_key());
sig.signature = iroha::expected::resultToOptionalValue(
sig_t::from_string(pb_sig.signature()))
.value();
sig.pubkey = iroha::expected::resultToOptionalValue(
pubkey_t::from_string(pb_sig.public_key()))
.value();
block.sigs.push_back(std::move(sig));
}

Expand Down
23 changes: 15 additions & 8 deletions irohad/model/converters/impl/pb_query_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,15 @@ namespace iroha {
// Convert to get transactions
const auto &pb_cast = pl.get_transactions();
auto query = GetTransactions();
std::transform(pb_cast.tx_hashes().begin(),
pb_cast.tx_hashes().end(),
std::back_inserter(query.tx_hashes),
[](auto tx_hash) {
return iroha::hash256_t::from_hexstring(tx_hash);
});
std::transform(
pb_cast.tx_hashes().begin(),
pb_cast.tx_hashes().end(),
std::back_inserter(query.tx_hashes),
[](auto tx_hash) {
return iroha::expected::resultToOptionalValue(
iroha::hash256_t::from_hexstring(tx_hash))
.value();
});
val = std::make_shared<GetTransactions>(query);
break;
}
Expand Down Expand Up @@ -137,8 +140,12 @@ namespace iroha {
const auto &pb_sign = pb_query.signature();

Signature sign{};
sign.pubkey = pubkey_t::from_hexstring(pb_sign.public_key());
sign.signature = sig_t::from_hexstring(pb_sign.signature());
sign.pubkey = iroha::expected::resultToOptionalValue(
pubkey_t::from_hexstring(pb_sign.public_key()))
.value();
sign.signature = iroha::expected::resultToOptionalValue(
sig_t::from_hexstring(pb_sign.signature()))
.value();

val->query_counter = pl.meta().query_counter();
val->signature = sign;
Expand Down
9 changes: 7 additions & 2 deletions irohad/model/converters/impl/pb_transaction_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include "model/converters/pb_transaction_factory.hpp"

#include "model/commands/add_asset_quantity.hpp"
#include "model/converters/pb_command_factory.hpp"

Expand Down Expand Up @@ -47,8 +48,12 @@ namespace iroha {

for (const auto &pb_sig : pb_tx.signatures()) {
model::Signature sig{};
sig.pubkey = pubkey_t::from_hexstring(pb_sig.public_key());
sig.signature = sig_t::from_hexstring(pb_sig.signature());
sig.pubkey = iroha::expected::resultToOptionalValue(
pubkey_t::from_hexstring(pb_sig.public_key()))
.value();
sig.signature = iroha::expected::resultToOptionalValue(
sig_t::from_hexstring(pb_sig.signature()))
.value();
tx.signatures.push_back(sig);
}

Expand Down
12 changes: 9 additions & 3 deletions irohad/model/generators/impl/transaction_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include "model/generators/transaction_generator.hpp"

#include "crypto/keys_manager_impl.hpp"
#include "cryptography/ed25519_sha3_impl/internal/sha3_hash.hpp"
#include "datetime/time.hpp"
Expand All @@ -17,9 +18,14 @@ namespace iroha {
iroha::keypair_t *makeOldModel(
const shared_model::crypto::Keypair &keypair) {
return new iroha::keypair_t{
iroha::pubkey_t::from_string(toBinaryString(keypair.publicKey())),
iroha::privkey_t::from_string(
toBinaryString(keypair.privateKey()))};
iroha::expected::resultToOptionalValue(
iroha::pubkey_t::from_string(
toBinaryString(keypair.publicKey())))
.value(),
iroha::expected::resultToOptionalValue(
iroha::privkey_t::from_string(
toBinaryString(keypair.privateKey())))
.value()};
}

Transaction TransactionGenerator::generateGenesisTransaction(
Expand Down
29 changes: 17 additions & 12 deletions libs/common/blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string>

#include "common/hexutils.hpp"
#include "common/result.hpp"

namespace iroha {
using BadFormatException = std::invalid_argument;
Expand Down Expand Up @@ -84,25 +85,29 @@ namespace iroha {
return res;
}

static blob_t<size_> from_string(const std::string &data) {
static blob_t<size_> from_raw(const byte_t data[size_]) {
blob_t<size_> b;
std::copy(data, data + size_, b.begin());
return b;
}

static expected::Result<blob_t<size_>, std::string> from_string(
const std::string &data) {
if (data.size() != size_) {
std::string value = "blob_t: input string has incorrect length. Found: "
return expected::makeError(
std::string{"blob_t: input string has incorrect length. Found: "}
+ std::to_string(data.size())
+ +", required: " + std::to_string(size_);
throw BadFormatException(value.c_str());
+ +", required: " + std::to_string(size_));
}

blob_t<size_> b;
std::copy(data.begin(), data.end(), b.begin());

return b;
return from_raw(reinterpret_cast<const byte_t *>(data.data()));
}

static blob_t<size_> from_hexstring(const std::string &hex) {
static expected::Result<blob_t<size_>, std::string> from_hexstring(
const std::string &hex) {
auto bytes = iroha::hexstringToBytestring(hex);
if (not bytes) {
throw BadFormatException(
"Provided data (" + hex
return expected::makeError(
std::string{"Provided data ("} + hex
+ ") is not a valid hex value for blob of size ("
+ std::to_string(size_) + ").");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,11 @@ namespace shared_model {
}

Keypair CryptoProviderEd25519Sha3::generateKeypair(const Seed &seed) {
assert(seed.size() == kSeedLength);
auto keypair = iroha::create_keypair(
iroha::blob_t<32>::from_string(toBinaryString(seed)));
iroha::blob_t<kSeedLength>::from_raw(seed.blob().data()));
return Keypair(PublicKey(keypair.pubkey.to_string()),
PrivateKey(keypair.privkey.to_string()));
}

const size_t CryptoProviderEd25519Sha3::kHashLength = 256 / 8;
const size_t CryptoProviderEd25519Sha3::kPublicKeyLength = 256 / 8;
const size_t CryptoProviderEd25519Sha3::kPrivateKeyLength = 256 / 8;
const size_t CryptoProviderEd25519Sha3::kSignatureLength = 512 / 8;
const size_t CryptoProviderEd25519Sha3::kSeedLength = 256 / 8;
} // namespace crypto
} // namespace shared_model
10 changes: 5 additions & 5 deletions shared_model/cryptography/ed25519_sha3_impl/crypto_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ namespace shared_model {
*/
static Keypair generateKeypair(const Seed &seed);

static const size_t kHashLength;
static const size_t kPublicKeyLength;
static const size_t kPrivateKeyLength;
static const size_t kSignatureLength;
static const size_t kSeedLength;
static constexpr size_t kHashLength = 256 / 8;
static constexpr size_t kPublicKeyLength = 256 / 8;
static constexpr size_t kPrivateKeyLength = 256 / 8;
static constexpr size_t kSignatureLength = 512 / 8;
static constexpr size_t kSeedLength = 256 / 8;
};
} // namespace crypto
} // namespace shared_model
Expand Down
8 changes: 5 additions & 3 deletions shared_model/cryptography/ed25519_sha3_impl/signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
*/

#include "cryptography/ed25519_sha3_impl/signer.hpp"

#include "crypto/hash_types.hpp"
#include "cryptography/ed25519_sha3_impl/internal/ed25519_impl.hpp"
#include "cryptography/ed25519_sha3_impl/internal/sha3_hash.hpp"

namespace shared_model {
namespace crypto {
Signed Signer::sign(const Blob &blob, const Keypair &keypair) {
assert(keypair.publicKey().size() == iroha::pubkey_t::size());
assert(keypair.privateKey().size() == iroha::privkey_t::size());
return Signed(
iroha::sign(
iroha::sha3_256(crypto::toBinaryString(blob)).to_string(),
iroha::pubkey_t::from_string(toBinaryString(keypair.publicKey())),
iroha::privkey_t::from_string(
toBinaryString(keypair.privateKey())))
iroha::pubkey_t::from_raw(keypair.publicKey().blob().data()),
iroha::privkey_t::from_raw(keypair.privateKey().blob().data()))
.to_string());
}
} // namespace crypto
Expand Down
14 changes: 9 additions & 5 deletions shared_model/cryptography/ed25519_sha3_impl/verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include "verifier.hpp"
#include "cryptography/ed25519_sha3_impl/verifier.hpp"

#include "cryptography/ed25519_sha3_impl/internal/ed25519_impl.hpp"
#include "cryptography/ed25519_sha3_impl/internal/sha3_hash.hpp"

Expand All @@ -12,10 +13,13 @@ namespace shared_model {
bool Verifier::verify(const Signed &signedData,
const Blob &orig,
const PublicKey &publicKey) {
return iroha::verify(
iroha::sha3_256(crypto::toBinaryString(orig)).to_string(),
iroha::pubkey_t::from_string(toBinaryString(publicKey)),
iroha::sig_t::from_string(toBinaryString(signedData)));
auto blob_hash = iroha::sha3_256(orig.blob());
return publicKey.size() == iroha::pubkey_t::size()
and signedData.size() == iroha::sig_t::size()
and iroha::verify(blob_hash.data(),
blob_hash.size(),
iroha::pubkey_t::from_raw(publicKey.blob().data()),
iroha::sig_t::from_raw(signedData.blob().data()));
}
} // namespace crypto
} // namespace shared_model

0 comments on commit 5d4ee85

Please sign in to comment.