New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hash x #1097
Closed
Closed
Hash x #1097
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
132b060
hash(tx) is not a pubkey
vogel 739dd60
extracted SignatureChecker class
vogel e915e94
implement hash(x) signing
vogel 2b8cc84
test hash(x) signing
vogel 13abcb6
remove duplication from SignatureChecker
vogel 111ce8e
remove duplication from TxEnvelopeTests
vogel 93d6b1b
add tests for SignatureUtils
vogel 8d9e4c8
add more generic versions of getHint and hasHint
vogel b1999c4
use signature hint also for hash(x)
vogel 78551ac
do not assume 'x' in hash x is a valid string
vogel 10dccb6
rename hasHint to doesHintMatch
vogel 4302168
remove unneeded functions from PubKeyUtils
vogel File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2016 Stellar Development Foundation and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the COPYING file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
#include "SignatureChecker.h" | ||
|
||
#include "crypto/KeyUtils.h" | ||
#include "crypto/SecretKey.h" | ||
#include "crypto/SHA.h" | ||
#include "crypto/SignerKey.h" | ||
#include "util/Algoritm.h" | ||
|
||
namespace stellar | ||
{ | ||
|
||
using xdr::operator < ; | ||
using xdr::operator == ; | ||
|
||
SignatureChecker::SignatureChecker(Hash const &contentsHash, xdr::xvector<DecoratedSignature,20> const &signatures) : | ||
mContentsHash(contentsHash), | ||
mSignatures(signatures) | ||
{ | ||
mUsedSignatures.resize(mSignatures.size()); | ||
} | ||
|
||
bool SignatureChecker::checkSignature(AccountID const &accountID, std::vector<Signer> const &signersV, int neededWeight) | ||
{ | ||
auto signers = split(signersV, [](const Signer &s){ return s.key.type(); }); | ||
|
||
// calculate the weight of the signatures | ||
int totalWeight = 0; | ||
|
||
// compare all available SIGNER_KEY_TYPE_HASH_TX with current transaction hash | ||
// current transaction hash is not stored in getEnvelope().signatures - it is | ||
// computed with getContentsHash() method | ||
for (auto const &signerKey : signers[SIGNER_KEY_TYPE_HASH_TX]) | ||
{ | ||
if (signerKey.key.hashTx() == mContentsHash) | ||
{ | ||
mUsedOneTimeSignerKeys[accountID].insert(signerKey.key); | ||
totalWeight += signerKey.weight; | ||
if (totalWeight >= neededWeight) | ||
return true; | ||
} | ||
} | ||
|
||
auto &hashXKeyWeights = signers[SIGNER_KEY_TYPE_HASH_X]; | ||
for (size_t i = 0; i < mSignatures.size(); i++) | ||
{ | ||
auto x = std::string{mSignatures[i].signature.begin(), mSignatures[i].signature.end()}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the preimage a string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using just mSignatures[i].signature now |
||
auto hash = sha256(x); | ||
|
||
for (auto it = hashXKeyWeights.begin(); it != hashXKeyWeights.end(); ++it) | ||
{ | ||
auto &signerKey = *it; | ||
if (signerKey.key.hashX() == hash) | ||
{ | ||
mUsedSignatures[i] = true; | ||
totalWeight += signerKey.weight; | ||
if (totalWeight >= neededWeight) | ||
return true; | ||
|
||
hashXKeyWeights.erase(it); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
auto &accountKeyWeights = signers[SIGNER_KEY_TYPE_ED25519]; | ||
for (size_t i = 0; i < mSignatures.size(); i++) | ||
{ | ||
auto const& sig = mSignatures[i]; | ||
|
||
for (auto it = accountKeyWeights.begin(); it != accountKeyWeights.end(); ++it) | ||
{ | ||
auto &signerKey = *it; | ||
auto pubKey = KeyUtils::convertKey<PublicKey>(signerKey.key); | ||
if (PubKeyUtils::hasHint(pubKey, sig.hint) && | ||
PubKeyUtils::verifySig(pubKey, sig.signature, | ||
mContentsHash)) | ||
{ | ||
mUsedSignatures[i] = true; | ||
totalWeight += signerKey.weight; | ||
if (totalWeight >= neededWeight) | ||
return true; | ||
|
||
accountKeyWeights.erase(it); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool | ||
SignatureChecker::checkAllSignaturesUsed() const | ||
{ | ||
for (auto sigb : mUsedSignatures) | ||
{ | ||
if (!sigb) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
const UsedOneTimeSignerKeys & | ||
SignatureChecker::usedOneTimeSignerKeys() const | ||
{ | ||
return mUsedOneTimeSignerKeys; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
|
||
// Copyright 2016 Stellar Development Foundation and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the COPYING file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
#include "xdr/Stellar-ledger-entries.h" | ||
#include "xdr/Stellar-transaction.h" | ||
#include "xdr/Stellar-types.h" | ||
|
||
#include <map> | ||
#include <set> | ||
#include <stdint.h> | ||
#include <vector> | ||
|
||
namespace stellar | ||
{ | ||
|
||
using UsedOneTimeSignerKeys = std::map<AccountID, std::set<SignerKey>>; | ||
|
||
class SignatureChecker | ||
{ | ||
public: | ||
explicit SignatureChecker(Hash const &contentsHash, xdr::xvector<DecoratedSignature,20> const &signatures); | ||
|
||
bool checkSignature(AccountID const &accountID, std::vector<Signer> const &signersV, int32_t neededWeight); | ||
bool checkAllSignaturesUsed() const; | ||
|
||
const UsedOneTimeSignerKeys & usedOneTimeSignerKeys() const; | ||
|
||
private: | ||
Hash const &mContentsHash; | ||
xdr::xvector<DecoratedSignature,20> const &mSignatures; | ||
|
||
std::vector<bool> mUsedSignatures; | ||
UsedOneTimeSignerKeys mUsedOneTimeSignerKeys; | ||
}; | ||
|
||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's talk about this.